added uuid_fix to rewrite the fstab and mdadm.conf
[bootimage-initfs.git] / etc / cloner3 / uuid_fix.sh
1 #!/bin/sh
2
3 . /etc/library.sh
4 . /etc/cloner3/include.sh
5 . /etc/cloner3/cloner_server.sh
6 . /tmp/cmdline.dat
7
8 ORG_UUID_FILE="/cloner/setup/device_uuid"
9 NEW_UUID_FILE="/tmp/dest_uuid_map"
10
11 blkid > /tmp/dest_uuid_map
12
13 new_uuid_by_device() {
14         DEVICE=$1
15
16         RESULT=$(grep "^${DEVICE}:" ${NEW_UUID_FILE})
17         UUID=$(echo ${RESULT} | cut -d" " -f2 | sed 's/\"//g' | sed 's/^UUID=//i' )
18
19         echo ${UUID}
20 }
21
22 old_uuid_by_device() {
23         DEVICE=$1
24
25         RESULT=$(grep "^${DEVICE}:" ${ORG_UUID_FILE})
26         UUID=$(echo ${RESULT} | cut -d" " -f2 | sed 's/\"//g' | sed 's/^UUID=//i' )
27
28         echo ${UUID}
29 }
30
31 new_uuid_by_old_uuid() {
32         UUID=$1
33
34         # grep for the UUID and get the last one
35         RESULT=$(grep "UUID=\"${UUID}\"" ${ORG_UUID_FILE} | tail -1)
36         SRC_DEVICE=$(echo ${RESULT} | cut -d":" -f1)
37
38         NEW_UUID=$(new_uuid_by_device ${SRC_DEVICE})
39
40         echo ${NEW_UUID}
41 }
42
43
44 convert_fstab() {
45
46         SRC=$1
47         DEST=$2
48         
49         while read line
50         do
51                 DEVICE=$(echo ${line} | awk '{print $1}')
52                 REST=$(echo ${line} | awk '{$1 = ""; print $0}')
53                 IS_UUID=$(echo ${DEVICE} | grep "^UUID=")
54
55                 if [ "${IS_UUID}" != "" ]
56                 then
57                         OLD_UUID=$(echo ${DEVICE} | sed 's/UUID=//' | sed 's/\"//g')
58
59                         msg -n "Looking for replacement for ${ANSI_BLUE}${OLD_UUID}${ANSI_DONE}"
60                         NEW_UUID=$(new_uuid_by_old_uuid ${OLD_UUID})
61                         if [ "${NEW_UUID}" = "" ]
62                         then
63                                 clone_fatal_error "Can't find new UUID for ${DEVICE}"
64                                 return 1
65                         fi
66                         msg -n " found ${ANSI_BLUE}${NEW_UUID}${ANSI_DONE}"
67                         ok_msg
68
69                         echo "UUID=${NEW_UUID} ${REST}" >> ${DEST}
70                 else 
71                         echo ${line} >> ${DEST}
72                 fi
73
74         done < ${SRC}
75 }
76
77 header "Updating mdadm.conf with new UUIDs"
78 if [ -e "/cloner/mnt/etc/mdadm.conf" ]
79 then
80         
81         msg -n "Updating configuration file ${ANSI_BLUE}/etc/mdadm.conf${ANSI_DONE}"
82         server_log "Updating /etc/mdadm.conf configuration" "${_node}"
83         grep -v "^ARRAY" /cloner/mnt/etc/mdadm.conf" > /tmp/new-mdadm.conf
84         echo "### AUTOGENERATED BY CLONER ON `date`" >> /tmp/new-mdadm.conf
85         mdadm --detail --scan >> /tmp/new-mdadm.conf
86         mv /tmp/new-mdadm.conf /cloner/mnt/etc/mdadm.conf
87         ok_msg
88
89         if [ -e /cloner/mnt/usr/sbin/dracut ]
90         then
91                 for kver in `ls -1 /cloner/mnt/lib/modules`
92                 do
93                         msg -n "Updating dracut for kernel ${ANSI_BLUE}${kver}${ANSI_DONE}
94                         chroot /cloner/mnt /usr/sbin/dracut -f --kver=${kver} \
95                                 >> /tmp/stdout.log 2> /tmp/stderr.log
96
97                         if [ "$?" = "0" ]
98                         then
99                                 ok_msg
100                         else
101                                 clone_fatal_error "Can't update initrd for ${kver} with dracut"
102                         fi
103                 done
104         fi
105
106 fi
107
108 if [ ! -e ${ORIG_UUID_FILE} ]
109 then
110         exit 0
111 fi
112
113 header_msg "Updating UUIDs in /etc/fstab"
114 convert_fstab /cloner/mnt/etc/fstab /tmp/fstab.converted
115 if [ "$?" != "0" ]
116 then
117         clone_fatal_error "Can't convert UUIDs from old fstab to new."
118 fi
119 if [ -e /tmp/fstab.converted ]
120         mv /tmp/fstab.converted /cloner/mnt/etc/fstab
121 fi
122
123 return 0