| 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 | e2ac9a14 | Paolo Dongilli | # 
 | 
      
        | 12 |  |  | # 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 |  |  |     
 | 
      
        | 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 |  |  | imageName=$(cat $computerList | grep "$macAddress" | awk '{print $3}')
 | 
      
        | 59 |  |  | 
 | 
      
        | 60 |  |  | ocs-sr -b -g auto -e1 auto -e2 -r -j2 -scr -p true restoredisk $imageName $diskVar
 | 
      
        | 61 |  |  | 
 | 
      
        | 62 |  |  | # # PRT 2 ---------------------------
 | 
      
        | 63 |  |  | mount $rootDisk$rootPartition $mountPoint
 | 
      
        | 64 |  |  | 
 | 
      
        | 65 |  |  | currentName="$(cat $mountPoint/etc/hostname)"
 | 
      
        | 66 |  |  | newName="$(grep "$macAddress" $computerList | awk '{print $1}')"
 | 
      
        | 67 |  |  | 
 | 
      
        | 68 |  |  | echo "SETTING HOSTNAME" 
 | 
      
        | 69 |  |  | 
 | 
      
        | 70 |  |  | for i in hostname hosts mailname
 | 
      
        | 71 |  |  | 	 
 | 
      
        | 72 |  |  | do
 | 
      
        | 73 |  |  |     if [ -e $mountPoint/etc/$i ]; then
 | 
      
        | 74 |  |  | 	sed -ie "s/$currentName/$newName/g" $mountPoint/etc/$i
 | 
      
        | 75 |  |  |     else
 | 
      
        | 76 |  |  | 	echo "The file $i is not present on this system"
 | 
      
        | 77 |  |  |     fi    
 | 
      
        | 78 |  |  | done
 | 
      
        | 79 |  |  | 
 | 
      
        | 80 |  |  | 
 | 
      
        | 81 |  |  | 
 | 
      
        | 82 |  |  | # ###  Here I insert the part for the joining preparation of the machine
 | 
      
        | 83 |  |  | 
 | 
      
        | 84 | a1471122 | Donato Florio | # Verify whether the machine has to be joined to the domain or not.
 | 
      
        | 85 | e2ac9a14 | Paolo Dongilli | # If so the .ssh keys are needed and also a script ..... 
 | 
      
        | 86 |  |  | 
 | 
      
        | 87 | a1471122 | Donato Florio | joinVar="$(cat $computerList | grep "$macAddress" | awk '{print $4}')"
 | 
      
        | 88 |  |  | 
 | 
      
        | 89 |  |  | clusterVar="$(cat $computerList | grep "$macAddress" | awk '{print $5}')"
 | 
      
        | 90 | e2ac9a14 | Paolo Dongilli | 
 | 
      
        | 91 |  |  | if [ "$joinVar" == "join" ];then
 | 
      
        | 92 |  |  |     rsync -a /home/partimag/.ssh/ /mnt/root/.ssh/
 | 
      
        | 93 |  |  |     cp /root/.ssh/known_hosts /mnt/root/.ssh/known_hosts
 | 
      
        | 94 |  |  |     cp /home/partimag/clientScripts/rc.local /mnt/etc/
 | 
      
        | 95 |  |  |     chmod 770 /mnt/etc/rc.local
 | 
      
        | 96 |  |  |     cp /home/partimag/clientScripts/clientScript /mnt/root/
 | 
      
        | 97 |  |  |     chmod 770 /mnt/root/clientScript
 | 
      
        | 98 |  |  |     touch /mnt/root/reboot
 | 
      
        | 99 |  |  | fi
 | 
      
        | 100 | a1471122 | Donato Florio | 
 | 
      
        | 101 |  |  | if [ "$clusterVar" != "" ];then
 | 
      
        | 102 |  |  |     clusterJoin="-g $clusterVar"
 | 
      
        | 103 |  |  |     echo "$clusterJoin" > /mnt/root/cluster
 | 
      
        | 104 |  |  | fi
 | 
      
        | 105 |  |  | 
 | 
      
        | 106 |  |  | 
 | 
      
        | 107 |  |  | 
 | 
      
        | 108 | e2ac9a14 | Paolo Dongilli | # ### END of the preparation stuff
 | 
      
        | 109 |  |  | 
 | 
      
        | 110 |  |  | 
 | 
      
        | 111 |  |  | reboot
 | 
      
        | 112 |  |  | 
 |