Added mounting of /proc and /sys into initrd for grub2 installation
[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 # --------------
61 #  A standard network RSYNC based install
62 # --------------
63 else
64
65         #msg "The installation process can take a while, check ${ANSI_RED}[ALT-F4]${ANSI_DONE} to view progress"
66
67         msg -n "Syncing data from ${ANSI_BLUE}${_server}${ANSI_DONE} image ${ANSI_BLUE}${_image}${ANSI_DONE}"
68         server_log "Syncing data from ${_server} image ${_image}" "${_node}"
69         /usr/bin/rsync -avz --numeric-ids \
70                 ${_server}::${CLONER_IMAGE_PATH}/${_image}/data/  /cloner/mnt/
71         if [ "$?" = "0" ]
72         then
73                 ok_msg
74         else
75                 fail_msg
76                 clone_fatal_error "Failed to sync filesystem data from server!"
77         fi
78         
79 fi
80         
81 # remake an directories we excluded from our rsync process
82 sort -k1 /cloner/setup/makedirectories \
83         > /cloner/setup/makedirectories.sorted 2>> /tmp/stderr.log
84
85 header "Making missing directories"
86 while read line
87 do
88
89         DIR=`echo $line | awk '{print $1}'`
90         DIR_MODE=`echo $line | awk '{print $2}'`
91         DIR_UID=`echo $line | awk '{print $3}'`
92         DIR_GID=`echo $line | awk '{print $4}'`
93
94         if [ "${DIR}" != "" ]
95         then
96                 msg -n "Making directory ${ANSI_BLUE}${DIR}${ANSI_DONE}"
97                 mkdir -p /cloner/mnt/${DIR} >> /tmp/stdout.log \
98                         2>> /tmp/stderr.log
99
100                 chmod ${DIR_MODE} /cloner/mnt/${DIR} >> \
101                         /tmp/stdout.log 2>> /tmp/stderr.log
102         
103                 chown ${DIR_UID}:${DIR_GID} /cloner/mnt/${DIR} \
104                         >> /tmp/stdout.log 2>> /tmp/stderr.log
105         
106                 if [ "$?" = "0" ]
107                 then
108                         ok_msg
109                 else
110                         fail_msg
111                 fi
112         fi
113
114 done < /cloner/setup/makedirectories.sorted
115         
116 if [ "${_node}" != "" ]
117 then
118
119         if [ "${_srcpath}" != "" ]
120         then
121                 header "Installing node specific data"
122                 msg -n "Syning node ${ANSI_BLUE}${_node}${ANSI_DONE} data from /mnt/media"
123                 rsync -avzc -I --numeric-ids --exclude /.valid \
124                         /cloner/setup/node/ /cloner/mnt \
125                         >> /tmp/stdout.log 2>> /tmp/stderr.log
126
127                 if [ "$?" = "0" ]
128                 then
129                         ok_msg
130                 else
131                         fail_msg
132                         fatal_error "Failed to sync node data from CD/DVD"
133                 fi
134         else
135                 header "Installing node specific data"
136                 server_log "Syncing node ${_node} data from ${_server}" "${_node}"
137                 msg -n "Syning node ${ANSI_BLUE}${_node}${ANSI_DONE} data from ${_server}"
138
139                 rsync -avzc --numeric-ids --exclude /.valid \
140                         ${_server}::${CLONER_IMAGE_PATH}/${_image}/nodes/${_node}/ \
141                         /cloner/mnt >> /tmp/stdout.log 2>> /tmp/stderr.log
142                 if [ "$?" = "0" ]
143                 then
144                         ok_msg
145                 else
146                         fail_msg
147                         #clone_fatal_error "Failed to sync node data from server!"
148                 fi
149                 
150                 header "Getting node netconfig settings"
151                 msg -n "Getting data from ${_server} for ${_node}"
152                 server_get_netcfg "${_node}"
153                 ok_msg
154         fi
155 fi
156
157 # mounting 
158 header "Mounting proc & sys filesystem in deployed image"
159 if [! -d /cloner/mnt/proc ]
160 then
161         mkdir /cloner/mnt/proc
162 fi
163
164 msg -n "Mounting /cloner/mnt/proc"
165 mount -t proc none /cloner/mnt/proc
166 if [ "$?" = "0" ]
167 then
168         ok_msg
169 else
170         fail_msg
171 fi
172         
173 if [! -d /cloner/mnt/sys ]
174 then
175         mkdir /cloner/mnt/sys
176 fi
177 msg -n "Mounting /cloner/mnt/proc"
178 mount -t sysfs none /cloner/mnt/sys
179 if [ "$?" = "0" ]
180 then
181         ok_msg
182 else
183         fail_msg
184 fi
185         
186
187 header "Installing bootloader"
188 while read line
189 do
190         GRUB2=0
191         BL_DEVICE=`echo $line | awk '{print $1}'`
192         BL_TYPE=`echo $line | awk '{print $2}'`
193
194         if [ "${BL_TYPE}" = "grub" ]
195         then
196                 msg -n "Installing grub bootloader on ${ANSI_BLUE}${BL_DEVICE}${ANSI_DONE}"
197                 server_log "Installing grub on ${BL_DEVICE}" "${_node}"
198
199                 GRUB_PATH=""
200                 if [ -x /cloner/mnt/sbin/grub-install ]
201                 then
202                         GRUB_PATH="/sbin/grub-install"
203                 elif  [ -x /cloner/mnt/usr/sbin/grub-install ]
204                 then
205                         GRUB_PATH="/usr/sbin/grub-install"
206                 elif [ -x /cloner/mnt/sbin/grub2-install ]
207                 then
208                         GRUB_PATH="/sbin/grub2-install --force "
209                         GRUB2=1
210                 elif  [ -x /cloner/mnt/usr/sbin/grub2-install ]
211                 then
212                         GRUB_PATH="/usr/sbin/grub2-install --force "
213                         GRUB2=1
214                 else
215                         clone_fatal_error "Can't find the grub-install binary on mounted filesystem, no bootloader installed"
216                 fi
217
218                 chroot /cloner/mnt ${GRUB_PATH} --no-floppy ${BL_DEVICE} \
219                         >> /tmp/stdout.log 2>> /tmp/stderr.log
220                 if [ "$?" = "0" ]
221                 then
222                         ok_msg
223                 else
224                         fail_msg
225                         msg -n "Trying alternative grub installation "
226                         server_log "Trying alternative grub install" "${_node}"
227                         echo "root (hd0,0)" > /cloner/mnt/grub.txt
228                         echo "setup (hd0)" >> /cloner/mnt/grub.txt
229
230                         GRUB_BIN_PATH=""
231                         if [ -x /cloner/mnt/sbin/grub ]
232                         then
233                                 GRUB_BIN_PATH="/sbin/grub"
234                         elif  [ -x /cloner/mnt/usr/sbin/grub ]
235                         then
236                                 GRUB_BIN_PATH="/usr/sbin/grub"
237                         else
238                                 clone_fatal_error "Can't find the grub binary on mounted filesystem, no bootloader installed"
239                         fi
240
241                         chroot /cloner/mnt ""$GRUB_BIN_PATH --device-map=/boot/grub/device.map --no-floppy --batch < /cloner/mnt/grub.txt"" \ 
242                                 >> /tmp/stdout.log 2>> /tmp/stderr.log
243                         if [ "$?" = "0" ]
244                         then
245                                 ok_msg
246                         else
247                                 fail_msg
248                                 clone_fatal_error "Failed to install grub bootloader"
249                         fi
250                 fi
251         elif [ "${BL_TYPE}" = "lilo" ]
252         then
253                 msg -n "Installing lilo bootloader on ${ANSI_BLUE}${BL_DEVICE}${ANSI_DONE}"
254                 server_log "Installing lilo on ${BL_DEVICE}" "${_node}"
255                 chroot /cloner/mnt /sbin/lilo >> /tmp/stdout.log 2>> /tmp/stderr.log
256                 if [ "$?" = "0" ]
257                 then
258                         ok_msg
259                 else
260                         fail_msg
261                         clone_fatal_error "Failed to install lilo bootloader"
262                 fi
263         fi
264
265         # if grub2 we generate a grub2 config file too
266         if [ "${GRUB2}" = "1" ]
267         then
268                 if [ -x /cloner/mnt/sbin/grub2-mkconfig ]
269                 then
270                         GRUB_MKCONFIG="/sbin/grub2-mkconfig "
271                 elif  [ -x /cloner/mnt/usr/sbin/grub2-mkconfig ]
272                 then
273                         GRUB_MKCONFIG="/usr/sbin/grub2-mkconfig "
274                 fi
275
276                 msg -n "Creating grub2 config file"
277                 server_log "Creating grub2 config file" "${_node}"
278                 chroot /cloner/mnt ${GRUB_MKCONFIG} -o /boot/grub2/grub.cfg >> /tmp/stdout.log 2>> /tmp/stderr.log
279                 if [ "$?" = "0" ]
280                 then
281                         ok_msg
282                 else
283                         fail_msg
284                         clone_fatal_error "Failed to create grub2 config file"
285                 fi
286         fi
287
288 done < /cloner/setup/bootloader
289
290 # if a post install script exists we execute it
291 if [ -e /cloner/setup/post_install.sh ]
292 then
293         msg -n "Found a post_install.sh script executing it"
294         server_log "Executing post_install.sh '${_node}' '${_image}' '${_server}'" "${_node}"
295
296         cp /cloner/setup/post_install.sh /cloner/mnt/tmp/post_install.sh
297         chmod +x /cloner/mnt/tmp/post_install.sh
298
299         chroot /cloner/mnt /tmp/post_install.sh "${_node}" "${_image}" "${_server}"
300         rm -f /cloner/mnt/tmp/post_install.sh
301         
302 fi
303
304 # we want to sort our filesystems via the mntpoint field so we can unmount them in
305 # the reverse order
306 sort -r -k2 /cloner/setup/filesystems \
307         > /cloner/setup/filesystems.revsorted 2>> /tmp/stderr.log
308
309 header "Unmounting filesystems"
310 server_log "Unmounting filesystems" "${_node}"
311
312 umount /cloner/mnt/proc
313 umount /cloner/mnt/sys
314
315 while read line
316 do
317         FS_DEVICE=`echo $line | awk '{print $1}'`
318         FS_MNTPOINT=`echo $line | awk '{print $2}'`
319         FS_TYPE=`echo $line | awk '{print $3}'`
320         FS_LABEL=`echo $line | awk '{print $4}'`
321
322         case "${FS_TYPE}"
323         in
324                 swap)
325                         msg -n "Turning off swap partitions"
326                         swapoff ${FS_DEVICE} >> /tmp/stdout.log 2>> /tmp/stderr.log
327                         ok_or_fail $?
328                         ;;
329                 *)
330                         msg -n "Unmounting ${FS_DEVICE} -> ${FS_MNTPOINT}"
331                         umount /cloner/mnt${FS_MNTPOINT} >> /tmp/stdout.log 2>> /tmp/stderr.log
332                         ok_or_fail $?
333                         ;;
334         esac
335 done < /cloner/setup/filesystems.revsorted
336