added uuid_fix to rewrite the fstab and mdadm.conf
[bootimage-initfs.git] / etc / cloner3 / installer.sh
1 #!/bin/ash
2
3 . /etc/library.sh
4 . /etc/cloner3/include.sh
5 . /etc/cloner3/cloner_server.sh
6 . /tmp/cmdline.dat
7
8 # TODO
9 #  * Further testing of GRUB / LILO bootloader
10
11 header "Starting the installation process"
12
13 # --------------
14 #  Perform a multicast install
15 # --------------
16 if [ "${_multicast}" = "1" ]
17 then
18
19         msg "Starting multicast receiver, server will initiate transfer"
20         server_log "Starting multicast receiver, server will initiate transfer" "${_node}"
21         /usr/bin/udp-receiver --nokbd --pipe 'tar -xvf - -C /cloner/mnt'
22
23         if [ "$?" = "0" ]
24         then
25                 ok_msg
26         else
27                 fail_msg
28                 clone_fatal_error "Failed to untar multicasted image"
29         fi
30
31 # --------------
32 #  A CD-DVD based install
33 # --------------
34 elif [ "${_srcpath}" != "" ]
35 then
36         msg "Starting the unpacking from CD/DVD-ROM"
37
38         # change to our install directory
39         cd /cloner/mnt >> /tmp/stdout.log 2>>/tmp/stderr.log
40
41         if [ -e /cloner/setup/disk_size ]
42         then
43                 . /cloner/setup/disk_size
44         else
45                 fatal_error "No disk size specified on setup directory"
46         fi
47
48         afio -i -Z -v -s $DISK_SIZE -H /etc/cloner2/switch_cd.sh \
49                 /mnt/media/${_image}.afio
50
51         if [ "$?" != 0 ]
52         then
53                 fail_msg
54                 fatal_error "Failed to extract filesystem from CD/DVD drive"
55         fi
56
57         # revert back to our old directory
58         cd - >> /tmp/stdout.log 2>>/tmp/stderr.log
59
60 elif [ "${_srclabel}" != "" ]
61 then
62         msg -n "Syncing data from ${ANSI_BLUE}LABEL=${_srclabel}${ANSI_DONE} image ${ANSI_BLUE}${_image}${ANSI_DONE}"
63
64         /usr/bin/rsync -avz --numeric-ids \
65                 /mnt/media/${_image}/data/  /cloner/mnt/
66         if [ "$?" = "0" ]
67         then
68                 ok_msg
69         else
70                 fail_msg
71                 clone_fatal_error "Failed to sync filesystem data from LABEL=${_srclabel}!"
72         fi
73
74
75 # --------------
76 #  A standard network RSYNC based install
77 # --------------
78 else
79
80         #msg "The installation process can take a while, check ${ANSI_RED}[ALT-F4]${ANSI_DONE} to view progress"
81
82         msg -n "Syncing data from ${ANSI_BLUE}${_server}${ANSI_DONE} image ${ANSI_BLUE}${_image}${ANSI_DONE}"
83         server_log "Syncing data from ${_server} image ${_image}" "${_node}"
84         /usr/bin/rsync -avz --numeric-ids \
85                 ${_server}::${CLONER_IMAGE_PATH}/${_image}/data/  /cloner/mnt/
86         if [ "$?" = "0" ]
87         then
88                 ok_msg
89         else
90                 fail_msg
91                 clone_fatal_error "Failed to sync filesystem data from server!"
92         fi
93         
94 fi
95         
96 # remake an directories we excluded from our rsync process
97 sort -k1 /cloner/setup/makedirectories \
98         > /cloner/setup/makedirectories.sorted 2>> /tmp/stderr.log
99
100 header "Making missing directories"
101 while read line
102 do
103
104         DIR=`echo $line | awk '{print $1}'`
105         DIR_MODE=`echo $line | awk '{print $2}'`
106         DIR_UID=`echo $line | awk '{print $3}'`
107         DIR_GID=`echo $line | awk '{print $4}'`
108
109         if [ "${DIR}" != "" ]
110         then
111                 msg -n "Making directory ${ANSI_BLUE}${DIR}${ANSI_DONE}"
112                 mkdir -p /cloner/mnt/${DIR} >> /tmp/stdout.log \
113                         2>> /tmp/stderr.log
114
115                 chmod ${DIR_MODE} /cloner/mnt/${DIR} >> \
116                         /tmp/stdout.log 2>> /tmp/stderr.log
117         
118                 chown ${DIR_UID}:${DIR_GID} /cloner/mnt/${DIR} \
119                         >> /tmp/stdout.log 2>> /tmp/stderr.log
120         
121                 if [ "$?" = "0" ]
122                 then
123                         ok_msg
124                 else
125                         fail_msg
126                 fi
127         fi
128
129 done < /cloner/setup/makedirectories.sorted
130         
131 if [ "${_node}" != "" ]
132 then
133
134         if [ "${_srcpath}" != "" ]
135         then
136                 header "Installing node specific data"
137                 msg -n "Syning node ${ANSI_BLUE}${_node}${ANSI_DONE} data from /mnt/media"
138                 rsync -avzc -I --numeric-ids --exclude /.valid \
139                         /cloner/setup/node/ /cloner/mnt \
140                         >> /tmp/stdout.log 2>> /tmp/stderr.log
141
142                 if [ "$?" = "0" ]
143                 then
144                         ok_msg
145                 else
146                         fail_msg
147                         fatal_error "Failed to sync node data from CD/DVD"
148                 fi
149         elif [ "${_srclabel}" != "" ]
150         then
151                 header "Installing node specific data"
152                 msg -n "Syning node ${ANSI_BLUE}${_node}${ANSI_DONE} data from LABEL=${_srclabel}"
153                 rsync -avzc -I --numeric-ids --exclude /.valid \
154                         /cloner/setup/node/ /cloner/mnt \
155                         >> /tmp/stdout.log 2>> /tmp/stderr.log
156
157                 if [ "$?" = "0" ]
158                 then
159                         ok_msg
160                 else
161                         fail_msg
162                         fatal_error "Failed to sync node data from LABEL=${_srclabel}"
163                 fi
164         else
165                 header "Installing node specific data"
166                 server_log "Syncing node ${_node} data from ${_server}" "${_node}"
167                 msg -n "Syning node ${ANSI_BLUE}${_node}${ANSI_DONE} data from ${_server}"
168
169                 rsync -avzc --numeric-ids --exclude /.valid \
170                         ${_server}::${CLONER_IMAGE_PATH}/${_image}/nodes/${_node}/ \
171                         /cloner/mnt >> /tmp/stdout.log 2>> /tmp/stderr.log
172                 if [ "$?" = "0" ]
173                 then
174                         ok_msg
175                 else
176                         fail_msg
177                         #clone_fatal_error "Failed to sync node data from server!"
178                 fi
179                 
180                 header "Getting node netconfig settings"
181                 msg -n "Getting data from ${_server} for ${_node}"
182                 server_get_netcfg "${_node}"
183                 ok_msg
184         fi
185 fi
186
187 # mounting 
188 header "Mounting proc & sys filesystem in deployed image"
189 msg -n "Mounting /cloner/mnt/proc"
190 mount -t proc none /cloner/mnt/proc
191 if [ "$?" = "0" ]
192 then
193         ok_msg
194 else
195         fail_msg
196 fi
197         
198 msg -n "Mounting /cloner/mnt/sys"
199 mount -t sysfs none /cloner/mnt/sys
200 if [ "$?" = "0" ]
201 then
202         ok_msg
203 else
204         fail_msg
205 fi
206
207 # fix the filesystem UUID issues
208 /etc/cloner3/uuid_fix.sh
209         
210
211 header "Installing bootloader"
212 while read line
213 do
214         GRUB2=0
215         BL_DEVICE=`echo $line | awk '{print $1}'`
216         BL_TYPE=`echo $line | awk '{print $2}'`
217
218         if [ "${BL_TYPE}" = "grub" ]
219         then
220                 msg -n "Installing grub bootloader on ${ANSI_BLUE}${BL_DEVICE}${ANSI_DONE}"
221                 server_log "Installing grub on ${BL_DEVICE}" "${_node}"
222
223                 GRUB_PATH=""
224                 if [ -x /cloner/mnt/sbin/grub-install ]
225                 then
226                         GRUB_PATH="/sbin/grub-install"
227                 elif  [ -x /cloner/mnt/usr/sbin/grub-install ]
228                 then
229                         GRUB_PATH="/usr/sbin/grub-install"
230                 elif [ -x /cloner/mnt/sbin/grub2-install ]
231                 then
232                         GRUB_PATH="/sbin/grub2-install --force "
233                         GRUB2=1
234                 elif  [ -x /cloner/mnt/usr/sbin/grub2-install ]
235                 then
236                         GRUB_PATH="/usr/sbin/grub2-install --force "
237                         GRUB2=1
238                 else
239                         clone_fatal_error "Can't find the grub-install binary on mounted filesystem, no bootloader installed"
240                 fi
241
242                 chroot /cloner/mnt ${GRUB_PATH} --no-floppy ${BL_DEVICE} \
243                         >> /tmp/stdout.log 2>> /tmp/stderr.log
244                 if [ "$?" = "0" ]
245                 then
246                         ok_msg
247                 else
248                         fail_msg
249                         msg -n "Trying alternative grub installation "
250                         server_log "Trying alternative grub install" "${_node}"
251                         echo "root (hd0,0)" > /cloner/mnt/grub.txt
252                         echo "setup (hd0)" >> /cloner/mnt/grub.txt
253
254                         GRUB_BIN_PATH=""
255                         if [ -x /cloner/mnt/sbin/grub ]
256                         then
257                                 GRUB_BIN_PATH="/sbin/grub"
258                         elif  [ -x /cloner/mnt/usr/sbin/grub ]
259                         then
260                                 GRUB_BIN_PATH="/usr/sbin/grub"
261                         else
262                                 clone_fatal_error "Can't find the grub binary on mounted filesystem, no bootloader installed"
263                         fi
264
265                         chroot /cloner/mnt ""$GRUB_BIN_PATH --device-map=/boot/grub/device.map --no-floppy --batch < /cloner/mnt/grub.txt"" \ 
266                                 >> /tmp/stdout.log 2>> /tmp/stderr.log
267                         if [ "$?" = "0" ]
268                         then
269                                 ok_msg
270                         else
271                                 fail_msg
272                                 clone_fatal_error "Failed to install grub bootloader"
273                         fi
274                 fi
275         elif [ "${BL_TYPE}" = "lilo" ]
276         then
277                 msg -n "Installing lilo bootloader on ${ANSI_BLUE}${BL_DEVICE}${ANSI_DONE}"
278                 server_log "Installing lilo on ${BL_DEVICE}" "${_node}"
279                 chroot /cloner/mnt /sbin/lilo >> /tmp/stdout.log 2>> /tmp/stderr.log
280                 if [ "$?" = "0" ]
281                 then
282                         ok_msg
283                 else
284                         fail_msg
285                         clone_fatal_error "Failed to install lilo bootloader"
286                 fi
287         fi
288
289         GRUB2_CONFIG="/boot/grub2/grub.cfg"
290         # if grub2 we generate a grub2 config file too
291         if [ "${GRUB2}" = "1" ]
292         then
293                 if [ -x /cloner/mnt/sbin/grub2-mkconfig ]
294                 then
295                         GRUB_MKCONFIG="/sbin/grub2-mkconfig "
296                 elif  [ -x /cloner/mnt/usr/sbin/grub2-mkconfig ]
297                 then
298                         GRUB_MKCONFIG="/usr/sbin/grub2-mkconfig "
299                 elif [ -x /cloner/mnt/usr/sbin/grub-mkconfig ]
300                 then
301                         GRUB_MKCONFIG="/usr/sbin/grub-mkconfig "
302                         GRUB2_CONFIG="/boot/grub/grub.cfg"
303                 fi
304
305                 msg -n "Creating grub2 config file"
306                 server_log "Creating grub2 config file" "${_node}"
307                 chroot /cloner/mnt ${GRUB_MKCONFIG} -o ${GRUB2_CONFIG} >> /tmp/stdout.log 2>> /tmp/stderr.log
308                 if [ "$?" = "0" ]
309                 then
310                         ok_msg
311                 else
312                         fail_msg
313                         clone_fatal_error "Failed to create grub2 config file"
314                 fi
315         fi
316
317 done < /cloner/setup/bootloader
318
319 if [ -e /cloner/setup/efi ]
320 then
321
322         EFIBOOTMGR=/usr/sbin/efibootmgr
323         header "Setting up EFI"
324
325         EFIARGS=`cat /cloner/setup/efi`
326
327         msg -n "Running efibootmgr ${EFIARGS}"
328         server_log "Running efibootmgr ${EFIARGS}" "${_node}"
329         chroot /cloner/mnt ${EFIBOOTMGR} ${EFIARGS} >> /tmp/stdout.log 2>> /tmp/stderr.log
330         if [ "$?" = "0" ]
331         then
332                 ok_msg
333         else
334                 fail_msg
335                 clone_fatal_error "Failed to run ${EFIBOOTMGR} ${EFIARGS}"
336         fi
337
338         GRUB_MKCONFIG=""
339         GRUB2_CONFIG="/etc/grub2-efi.cfg"
340         if [ -x /cloner/mnt/sbin/grub2-mkconfig ]
341         then
342                 GRUB_MKCONFIG="/sbin/grub2-mkconfig "
343         elif  [ -x /cloner/mnt/usr/sbin/grub2-mkconfig ]
344         then
345                 GRUB_MKCONFIG="/usr/sbin/grub2-mkconfig "
346         fi
347
348         msg -n "Creating grub2 config file ${GRUB2_CONFIG}"
349         server_log "Creating grub2 config file ${GRUB2_CONFIG}" "${_node}"
350         chroot /cloner/mnt ${GRUB_MKCONFIG} -o ${GRUB2_CONFIG} >> /tmp/stdout.log 2>> /tmp/stderr.log
351         if [ "$?" = "0" ]
352         then
353                 ok_msg
354         else
355                 fail_msg
356                 clone_fatal_error "Failed to create grub2 config file"
357         fi
358
359 fi
360
361 # if a post install script exists we execute it
362 if [ -e /cloner/setup/post_install.sh ]
363 then
364         msg -n "Found a post_install.sh script executing it"
365         server_log "Executing post_install.sh '${_node}' '${_image}' '${_server}'" "${_node}"
366
367         cp /cloner/setup/post_install.sh /cloner/mnt/tmp/post_install.sh
368         chmod +x /cloner/mnt/tmp/post_install.sh
369
370         chroot /cloner/mnt /tmp/post_install.sh "${_node}" "${_image}" "${_server}"
371         rm -f /cloner/mnt/tmp/post_install.sh
372         
373 fi
374
375 # we want to sort our filesystems via the mntpoint field so we can unmount them in
376 # the reverse order
377 sort -r -k2 /cloner/setup/filesystems \
378         > /cloner/setup/filesystems.revsorted 2>> /tmp/stderr.log
379
380 header "Unmounting filesystems"
381 server_log "Unmounting filesystems" "${_node}"
382
383 umount /cloner/mnt/proc
384 umount /cloner/mnt/sys
385
386 while read line
387 do
388         FS_DEVICE=`echo $line | awk '{print $1}'`
389         FS_MNTPOINT=`echo $line | awk '{print $2}'`
390         FS_TYPE=`echo $line | awk '{print $3}'`
391         FS_LABEL=`echo $line | awk '{print $4}'`
392
393         case "${FS_TYPE}"
394         in
395                 swap)
396                         msg -n "Turning off swap partitions"
397                         swapoff ${FS_DEVICE} >> /tmp/stdout.log 2>> /tmp/stderr.log
398                         ok_or_fail $?
399                         ;;
400                 *)
401                         msg -n "Unmounting ${FS_DEVICE} -> ${FS_MNTPOINT}"
402                         umount /cloner/mnt${FS_MNTPOINT} >> /tmp/stdout.log 2>> /tmp/stderr.log
403                         ok_or_fail $?
404                         ;;
405         esac
406 done < /cloner/setup/filesystems.revsorted
407