Updated cloner3 to support installing images from a disk partition with an ext4 LABEL=
[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
208 header "Installing bootloader"
209 while read line
210 do
211         GRUB2=0
212         BL_DEVICE=`echo $line | awk '{print $1}'`
213         BL_TYPE=`echo $line | awk '{print $2}'`
214
215         if [ "${BL_TYPE}" = "grub" ]
216         then
217                 msg -n "Installing grub bootloader on ${ANSI_BLUE}${BL_DEVICE}${ANSI_DONE}"
218                 server_log "Installing grub on ${BL_DEVICE}" "${_node}"
219
220                 GRUB_PATH=""
221                 if [ -x /cloner/mnt/sbin/grub-install ]
222                 then
223                         GRUB_PATH="/sbin/grub-install"
224                 elif  [ -x /cloner/mnt/usr/sbin/grub-install ]
225                 then
226                         GRUB_PATH="/usr/sbin/grub-install"
227                 elif [ -x /cloner/mnt/sbin/grub2-install ]
228                 then
229                         GRUB_PATH="/sbin/grub2-install --force "
230                         GRUB2=1
231                 elif  [ -x /cloner/mnt/usr/sbin/grub2-install ]
232                 then
233                         GRUB_PATH="/usr/sbin/grub2-install --force "
234                         GRUB2=1
235                 else
236                         clone_fatal_error "Can't find the grub-install binary on mounted filesystem, no bootloader installed"
237                 fi
238
239                 chroot /cloner/mnt ${GRUB_PATH} --no-floppy ${BL_DEVICE} \
240                         >> /tmp/stdout.log 2>> /tmp/stderr.log
241                 if [ "$?" = "0" ]
242                 then
243                         ok_msg
244                 else
245                         fail_msg
246                         msg -n "Trying alternative grub installation "
247                         server_log "Trying alternative grub install" "${_node}"
248                         echo "root (hd0,0)" > /cloner/mnt/grub.txt
249                         echo "setup (hd0)" >> /cloner/mnt/grub.txt
250
251                         GRUB_BIN_PATH=""
252                         if [ -x /cloner/mnt/sbin/grub ]
253                         then
254                                 GRUB_BIN_PATH="/sbin/grub"
255                         elif  [ -x /cloner/mnt/usr/sbin/grub ]
256                         then
257                                 GRUB_BIN_PATH="/usr/sbin/grub"
258                         else
259                                 clone_fatal_error "Can't find the grub binary on mounted filesystem, no bootloader installed"
260                         fi
261
262                         chroot /cloner/mnt ""$GRUB_BIN_PATH --device-map=/boot/grub/device.map --no-floppy --batch < /cloner/mnt/grub.txt"" \ 
263                                 >> /tmp/stdout.log 2>> /tmp/stderr.log
264                         if [ "$?" = "0" ]
265                         then
266                                 ok_msg
267                         else
268                                 fail_msg
269                                 clone_fatal_error "Failed to install grub bootloader"
270                         fi
271                 fi
272         elif [ "${BL_TYPE}" = "lilo" ]
273         then
274                 msg -n "Installing lilo bootloader on ${ANSI_BLUE}${BL_DEVICE}${ANSI_DONE}"
275                 server_log "Installing lilo on ${BL_DEVICE}" "${_node}"
276                 chroot /cloner/mnt /sbin/lilo >> /tmp/stdout.log 2>> /tmp/stderr.log
277                 if [ "$?" = "0" ]
278                 then
279                         ok_msg
280                 else
281                         fail_msg
282                         clone_fatal_error "Failed to install lilo bootloader"
283                 fi
284         fi
285
286         GRUB2_CONFIG="/boot/grub2/grub.cfg"
287         # if grub2 we generate a grub2 config file too
288         if [ "${GRUB2}" = "1" ]
289         then
290                 if [ -x /cloner/mnt/sbin/grub2-mkconfig ]
291                 then
292                         GRUB_MKCONFIG="/sbin/grub2-mkconfig "
293                 elif  [ -x /cloner/mnt/usr/sbin/grub2-mkconfig ]
294                 then
295                         GRUB_MKCONFIG="/usr/sbin/grub2-mkconfig "
296                 elif [ -x /cloner/mnt/usr/sbin/grub-mkconfig ]
297                 then
298                         GRUB_MKCONFIG="/usr/sbin/grub-mkconfig "
299                         GRUB2_CONFIG="/boot/grub/grub.cfg"
300                 fi
301
302                 msg -n "Creating grub2 config file"
303                 server_log "Creating grub2 config file" "${_node}"
304                 chroot /cloner/mnt ${GRUB_MKCONFIG} -o ${GRUB2_CONFIG} >> /tmp/stdout.log 2>> /tmp/stderr.log
305                 if [ "$?" = "0" ]
306                 then
307                         ok_msg
308                 else
309                         fail_msg
310                         clone_fatal_error "Failed to create grub2 config file"
311                 fi
312         fi
313
314 done < /cloner/setup/bootloader
315
316 # if a post install script exists we execute it
317 if [ -e /cloner/setup/post_install.sh ]
318 then
319         msg -n "Found a post_install.sh script executing it"
320         server_log "Executing post_install.sh '${_node}' '${_image}' '${_server}'" "${_node}"
321
322         cp /cloner/setup/post_install.sh /cloner/mnt/tmp/post_install.sh
323         chmod +x /cloner/mnt/tmp/post_install.sh
324
325         chroot /cloner/mnt /tmp/post_install.sh "${_node}" "${_image}" "${_server}"
326         rm -f /cloner/mnt/tmp/post_install.sh
327         
328 fi
329
330 # we want to sort our filesystems via the mntpoint field so we can unmount them in
331 # the reverse order
332 sort -r -k2 /cloner/setup/filesystems \
333         > /cloner/setup/filesystems.revsorted 2>> /tmp/stderr.log
334
335 header "Unmounting filesystems"
336 server_log "Unmounting filesystems" "${_node}"
337
338 umount /cloner/mnt/proc
339 umount /cloner/mnt/sys
340
341 while read line
342 do
343         FS_DEVICE=`echo $line | awk '{print $1}'`
344         FS_MNTPOINT=`echo $line | awk '{print $2}'`
345         FS_TYPE=`echo $line | awk '{print $3}'`
346         FS_LABEL=`echo $line | awk '{print $4}'`
347
348         case "${FS_TYPE}"
349         in
350                 swap)
351                         msg -n "Turning off swap partitions"
352                         swapoff ${FS_DEVICE} >> /tmp/stdout.log 2>> /tmp/stderr.log
353                         ok_or_fail $?
354                         ;;
355                 *)
356                         msg -n "Unmounting ${FS_DEVICE} -> ${FS_MNTPOINT}"
357                         umount /cloner/mnt${FS_MNTPOINT} >> /tmp/stdout.log 2>> /tmp/stderr.log
358                         ok_or_fail $?
359                         ;;
360         esac
361 done < /cloner/setup/filesystems.revsorted
362