Project

General

Profile

Download (5.08 KB) Statistics
| Branch: | Tag: | Revision:

fuss-fucc / clonezilla / script @ master

1
#!/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
# 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
#
12
# PRT 1 ---------------------------
13

    
14
service ssh start
15

    
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
    for i in $(lsblk -d -n -o NAME | grep -Ev '(loop|sr|dvd|cd)')
26
    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
	sleep 10
40
	shutdown -h now
41
    else
42
        diskVar="$(echo $myDevice | tr -d [:blank:])"
43
    fi
44

    
45
    # SSD of HP g450 laptops
46
    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
computerList="/home/partimag/computerList.txt"
57
macAddress=$(getNetStuff 2)
58

    
59
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
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
	_img=$(dialog --title "FUSS FUCC" --output-fd 1 --menu "Choose an image to be installed" 15 50 4 $images)
77
	img=$(ls -1 /home/partimag | sed -n "${_img}p")
78
	dialog --title "FUSS FUCC" --msgbox "Set image $img" 13 70
79
	if dialog --title "FUSS FUCC" --yesno "Join this client to the FUSS domain?" 13 70; then
80
		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
		dialog --title "FUSS FUCC" --msgbox "Configuration completed, now start cloning." 15 70
88
	else
89
		dialog --title "FUSS FUCC" --msgbox "Will now reboot" 13 70
90
		reboot
91
		exit 0
92
	fi
93
fi
94

    
95
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
echo "SETTING HOSTNAME"
106

    
107
for i in hostname hosts mailname
108

    
109
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
    fi
115
done
116

    
117

    
118

    
119
# ###  Here I insert the part for the joining preparation of the machine
120

    
121
# Verify whether the machine has to be joined to the domain or not.
122
# If so the .ssh keys are needed and also a script .....
123

    
124
joinVar="$(cat $computerList | grep "$macAddress" | awk '{print $4}')"
125

    
126
clusterVar="$(cat $computerList | grep "$macAddress" | awk '{print $5}')"
127

    
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
    if [ -e /home/partimag/clientScripts/new_root_pw ] ; then
135
        cp /home/partimag/clientScripts/new_root_pw /mnt/root/
136
    fi
137
    chmod 770 /mnt/root/clientScript
138
    touch /mnt/root/reboot
139
fi
140

    
141
if [ "$clusterVar" != "" ];then
142
    clusterJoin="-g $clusterVar"
143
    echo "$clusterJoin" > /mnt/root/cluster
144
fi
145

    
146

    
147

    
148
# ### END of the preparation stuff
149

    
150

    
151
reboot
(2-2/2)