1 |
e2ac9a14
|
Paolo Dongilli
|
#!/bin/bash
|
2 |
|
|
#
|
3 |
|
|
# Script for automated cloning and renaming of Fuss 9 client computers
|
4 |
|
|
# using mac-address as unique identifier.
|
5 |
|
|
# Donato Florio 11 April 2019
|
6 |
|
|
#
|
7 |
|
|
# Last modified 02 May 2019 --> added modification to support cloning on nvme* devices (HP G450)
|
8 |
a1471122
|
Donato Florio
|
# Last modified 07 May 2019 --> added various stuff for experimental autojoin to a domain
|
9 |
|
|
# Last modified 21 June 2019 --> added various stuff for experimental join to a cluster
|
10 |
|
|
# Last modified 01 July 2019 --> added ssh to script and changed disk detection method
|
11 |
c28a889b
|
Marco Marinello
|
#
|
12 |
e2ac9a14
|
Paolo Dongilli
|
# PRT 1 ---------------------------
|
13 |
|
|
|
14 |
a1471122
|
Donato Florio
|
service ssh start
|
15 |
e2ac9a14
|
Paolo Dongilli
|
|
16 |
|
|
getNetStuff(){
|
17 |
|
|
if [ $1 -eq 1 ];then
|
18 |
|
|
ls /sys/class/net/ -1 | grep -v lo | grep -v wlan
|
19 |
|
|
else
|
20 |
|
|
cat /sys/class/net/$(getNetStuff 1)/address
|
21 |
|
|
fi
|
22 |
|
|
}
|
23 |
|
|
|
24 |
|
|
getDisk(){
|
25 |
d803d4cf
|
Paolo Dongilli
|
for i in $(lsblk -d -n -o NAME | grep -Ev '(loop|sr|dvd|cd)')
|
26 |
a1471122
|
Donato Florio
|
do
|
27 |
|
|
if [ $(cat /sys/block/$i/removable) -eq 0 ];then
|
28 |
|
|
myDevice="${myDevice} $i"
|
29 |
|
|
fi
|
30 |
|
|
done
|
31 |
|
|
|
32 |
|
|
if [ $(echo $myDevice | wc -w) -gt 1 ];then
|
33 |
|
|
echo "ATTENZIONE! Questo computer sembra provvisto di più di un disco!"
|
34 |
|
|
echo ""
|
35 |
|
|
echo "La procedura automatica non prevede l'installazione su sistemi"
|
36 |
|
|
echo "multidisco. Contattare il tecnico informatico."
|
37 |
|
|
echo ""
|
38 |
|
|
echo "Per sicurezza il computer verrà spento tra 10 secondi."
|
39 |
e2ac9a14
|
Paolo Dongilli
|
sleep 10
|
40 |
|
|
shutdown -h now
|
41 |
|
|
else
|
42 |
a1471122
|
Donato Florio
|
diskVar="$(echo $myDevice | tr -d [:blank:])"
|
43 |
e2ac9a14
|
Paolo Dongilli
|
fi
|
44 |
c28a889b
|
Marco Marinello
|
|
45 |
a1471122
|
Donato Florio
|
# SSD of HP g450 laptops
|
46 |
e2ac9a14
|
Paolo Dongilli
|
if [ $diskVar == "nvme0n1" ];then
|
47 |
|
|
rootPartition="p$rootPartition"
|
48 |
|
|
fi
|
49 |
|
|
# FINE g450
|
50 |
|
|
}
|
51 |
|
|
|
52 |
|
|
rootPartition=2
|
53 |
|
|
getDisk
|
54 |
|
|
rootDisk="/dev/$diskVar"
|
55 |
|
|
mountPoint="/mnt"
|
56 |
a1471122
|
Donato Florio
|
computerList="/home/partimag/computerList.txt"
|
57 |
e2ac9a14
|
Paolo Dongilli
|
macAddress=$(getNetStuff 2)
|
58 |
c2f6c7f4
|
Marco Marinello
|
|
59 |
c28a889b
|
Marco Marinello
|
if [ "$1" = "check" ]; then
|
60 |
|
|
myLine=$(grep $macAddress $computerList | tail -1)
|
61 |
|
|
hostname=$(echo $myLine | awk '{print $1}')
|
62 |
|
|
img=$(echo $myLine | awk '{print $3}')
|
63 |
|
|
join=$(echo $myLine | awk '{print $4}')
|
64 |
|
|
cluster=$(echo $myLine | awk '{print $5}')
|
65 |
|
|
if ! dialog --title "FUSS FUCC" --yesno "Hostname: $hostname \nMAC: $macAddress \nImage: $img \nJoin: $join \nCluster: $cluster \nContinue?" 13 70; then
|
66 |
|
|
grep -v $macAddress $computerList > ${computerList}.new
|
67 |
|
|
cp ${computerList}.new ${computerList}
|
68 |
|
|
rm ${computerList}.new
|
69 |
|
|
fi
|
70 |
|
|
fi
|
71 |
|
|
|
72 |
c2f6c7f4
|
Marco Marinello
|
if ! grep $macAddress $computerList; then
|
73 |
|
|
dialog --title "FUSS FUCC" --msgbox "Hi,\nunfortunatley, there is no valid configuration to setup this computer. Please go through the next few steps to configure how to install this PC.\nPlease make sure that the file /home/clonezilla/computerList.txt is owned and writable by clonezilla, otherwise we'll not be able to save the informations you're going to enter." 13 70
|
74 |
|
|
hostname=$(dialog --title "FUSS FUCC" --inputbox "Please, pick an HOSTNAME for this PC" --output-fd 1 10 70)
|
75 |
|
|
images=$(ls -1 /home/partimag|nl)
|
76 |
fd6fc808
|
Marco Marinello
|
_img=$(dialog --title "FUSS FUCC" --output-fd 1 --menu "Choose an image to be installed" 15 50 4 $images)
|
77 |
c2f6c7f4
|
Marco Marinello
|
img=$(ls -1 /home/partimag | sed -n "${_img}p")
|
78 |
|
|
dialog --title "FUSS FUCC" --msgbox "Set image $img" 13 70
|
79 |
8c54bc0c
|
Marco Marinello
|
if dialog --title "FUSS FUCC" --yesno "Join this client to the FUSS domain?" 13 70; then
|
80 |
c2f6c7f4
|
Marco Marinello
|
join="join"
|
81 |
|
|
else
|
82 |
|
|
join="no"
|
83 |
|
|
fi
|
84 |
|
|
cluster=$(dialog --title "FUSS FUCC" --inputbox "Please, pick a CLUSTER for this PC" --output-fd 1 10 70)
|
85 |
|
|
if dialog --title "FUSS FUCC" --yesno "Hostname: $hostname \nMAC: $macAddress \nImage: $img \nJoin: $join \nCluster: $cluster \nContinue?" 13 70; then
|
86 |
|
|
echo $hostname $macAddress $img $join $cluster >> $computerList
|
87 |
fd6fc808
|
Marco Marinello
|
dialog --title "FUSS FUCC" --msgbox "Configuration completed, now start cloning." 15 70
|
88 |
c2f6c7f4
|
Marco Marinello
|
else
|
89 |
|
|
dialog --title "FUSS FUCC" --msgbox "Will now reboot" 13 70
|
90 |
|
|
reboot
|
91 |
|
|
exit 0
|
92 |
|
|
fi
|
93 |
|
|
fi
|
94 |
|
|
|
95 |
e2ac9a14
|
Paolo Dongilli
|
imageName=$(cat $computerList | grep "$macAddress" | awk '{print $3}')
|
96 |
|
|
|
97 |
|
|
ocs-sr -b -g auto -e1 auto -e2 -r -j2 -scr -p true restoredisk $imageName $diskVar
|
98 |
|
|
|
99 |
|
|
# # PRT 2 ---------------------------
|
100 |
|
|
mount $rootDisk$rootPartition $mountPoint
|
101 |
|
|
|
102 |
|
|
currentName="$(cat $mountPoint/etc/hostname)"
|
103 |
|
|
newName="$(grep "$macAddress" $computerList | awk '{print $1}')"
|
104 |
|
|
|
105 |
c28a889b
|
Marco Marinello
|
echo "SETTING HOSTNAME"
|
106 |
e2ac9a14
|
Paolo Dongilli
|
|
107 |
|
|
for i in hostname hosts mailname
|
108 |
c28a889b
|
Marco Marinello
|
|
109 |
e2ac9a14
|
Paolo Dongilli
|
do
|
110 |
|
|
if [ -e $mountPoint/etc/$i ]; then
|
111 |
|
|
sed -ie "s/$currentName/$newName/g" $mountPoint/etc/$i
|
112 |
|
|
else
|
113 |
|
|
echo "The file $i is not present on this system"
|
114 |
c28a889b
|
Marco Marinello
|
fi
|
115 |
e2ac9a14
|
Paolo Dongilli
|
done
|
116 |
|
|
|
117 |
|
|
|
118 |
|
|
|
119 |
|
|
# ### Here I insert the part for the joining preparation of the machine
|
120 |
|
|
|
121 |
a1471122
|
Donato Florio
|
# Verify whether the machine has to be joined to the domain or not.
|
122 |
c28a889b
|
Marco Marinello
|
# If so the .ssh keys are needed and also a script .....
|
123 |
e2ac9a14
|
Paolo Dongilli
|
|
124 |
a1471122
|
Donato Florio
|
joinVar="$(cat $computerList | grep "$macAddress" | awk '{print $4}')"
|
125 |
|
|
|
126 |
|
|
clusterVar="$(cat $computerList | grep "$macAddress" | awk '{print $5}')"
|
127 |
e2ac9a14
|
Paolo Dongilli
|
|
128 |
|
|
if [ "$joinVar" == "join" ];then
|
129 |
|
|
rsync -a /home/partimag/.ssh/ /mnt/root/.ssh/
|
130 |
|
|
cp /root/.ssh/known_hosts /mnt/root/.ssh/known_hosts
|
131 |
|
|
cp /home/partimag/clientScripts/rc.local /mnt/etc/
|
132 |
|
|
chmod 770 /mnt/etc/rc.local
|
133 |
|
|
cp /home/partimag/clientScripts/clientScript /mnt/root/
|
134 |
63d48e7f
|
Marco Marinello
|
if [ -e /home/partimag/clientScripts/new_root_pw ] ; then
|
135 |
|
|
cp /home/partimag/clientScripts/new_root_pw /mnt/root/
|
136 |
|
|
fi
|
137 |
e2ac9a14
|
Paolo Dongilli
|
chmod 770 /mnt/root/clientScript
|
138 |
|
|
touch /mnt/root/reboot
|
139 |
|
|
fi
|
140 |
a1471122
|
Donato Florio
|
|
141 |
|
|
if [ "$clusterVar" != "" ];then
|
142 |
|
|
clusterJoin="-g $clusterVar"
|
143 |
|
|
echo "$clusterJoin" > /mnt/root/cluster
|
144 |
|
|
fi
|
145 |
|
|
|
146 |
|
|
|
147 |
|
|
|
148 |
e2ac9a14
|
Paolo Dongilli
|
# ### END of the preparation stuff
|
149 |
|
|
|
150 |
|
|
|
151 |
|
|
reboot
|