Updated cloner3 and start mcelog daemon
[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 msg -n "Mounting /cloner/mnt/proc"
160 mount -t proc none /cloner/mnt/proc
161 if [ "$?" = "0" ]
162 then
163         ok_msg
164 else
165         fail_msg
166 fi
167         
168 msg -n "Mounting /cloner/mnt/sys"
169 mount -t sysfs none /cloner/mnt/sys
170 if [ "$?" = "0" ]
171 then
172         ok_msg
173 else
174         fail_msg
175 fi
176         
177
178 header "Installing bootloader"
179 while read line
180 do
181         GRUB2=0
182         BL_DEVICE=`echo $line | awk '{print $1}'`
183         BL_TYPE=`echo $line | awk '{print $2}'`
184
185         if [ "${BL_TYPE}" = "grub" ]
186         then
187                 msg -n "Installing grub bootloader on ${ANSI_BLUE}${BL_DEVICE}${ANSI_DONE}"
188                 server_log "Installing grub on ${BL_DEVICE}" "${_node}"
189
190                 GRUB_PATH=""
191                 if [ -x /cloner/mnt/sbin/grub-install ]
192                 then
193                         GRUB_PATH="/sbin/grub-install"
194                 elif  [ -x /cloner/mnt/usr/sbin/grub-install ]
195                 then
196                         GRUB_PATH="/usr/sbin/grub-install"
197                 elif [ -x /cloner/mnt/sbin/grub2-install ]
198                 then
199                         GRUB_PATH="/sbin/grub2-install --force "
200                         GRUB2=1
201                 elif  [ -x /cloner/mnt/usr/sbin/grub2-install ]
202                 then
203                         GRUB_PATH="/usr/sbin/grub2-install --force "
204                         GRUB2=1
205                 else
206                         clone_fatal_error "Can't find the grub-install binary on mounted filesystem, no bootloader installed"
207                 fi
208
209                 chroot /cloner/mnt ${GRUB_PATH} --no-floppy ${BL_DEVICE} \
210                         >> /tmp/stdout.log 2>> /tmp/stderr.log
211                 if [ "$?" = "0" ]
212                 then
213                         ok_msg
214                 else
215                         fail_msg
216                         msg -n "Trying alternative grub installation "
217                         server_log "Trying alternative grub install" "${_node}"
218                         echo "root (hd0,0)" > /cloner/mnt/grub.txt
219                         echo "setup (hd0)" >> /cloner/mnt/grub.txt
220
221                         GRUB_BIN_PATH=""
222                         if [ -x /cloner/mnt/sbin/grub ]
223                         then
224                                 GRUB_BIN_PATH="/sbin/grub"
225                         elif  [ -x /cloner/mnt/usr/sbin/grub ]
226                         then
227                                 GRUB_BIN_PATH="/usr/sbin/grub"
228                         else
229                                 clone_fatal_error "Can't find the grub binary on mounted filesystem, no bootloader installed"
230                         fi
231
232                         chroot /cloner/mnt ""$GRUB_BIN_PATH --device-map=/boot/grub/device.map --no-floppy --batch < /cloner/mnt/grub.txt"" \ 
233                                 >> /tmp/stdout.log 2>> /tmp/stderr.log
234                         if [ "$?" = "0" ]
235                         then
236                                 ok_msg
237                         else
238                                 fail_msg
239                                 clone_fatal_error "Failed to install grub bootloader"
240                         fi
241                 fi
242         elif [ "${BL_TYPE}" = "lilo" ]
243         then
244                 msg -n "Installing lilo bootloader on ${ANSI_BLUE}${BL_DEVICE}${ANSI_DONE}"
245                 server_log "Installing lilo on ${BL_DEVICE}" "${_node}"
246                 chroot /cloner/mnt /sbin/lilo >> /tmp/stdout.log 2>> /tmp/stderr.log
247                 if [ "$?" = "0" ]
248                 then
249                         ok_msg
250                 else
251                         fail_msg
252                         clone_fatal_error "Failed to install lilo bootloader"
253                 fi
254         fi
255
256         # if grub2 we generate a grub2 config file too
257         if [ "${GRUB2}" = "1" ]
258         then
259                 if [ -x /cloner/mnt/sbin/grub2-mkconfig ]
260                 then
261                         GRUB_MKCONFIG="/sbin/grub2-mkconfig "
262                 elif  [ -x /cloner/mnt/usr/sbin/grub2-mkconfig ]
263                 then
264                         GRUB_MKCONFIG="/usr/sbin/grub2-mkconfig "
265                 fi
266
267                 msg -n "Creating grub2 config file"
268                 server_log "Creating grub2 config file" "${_node}"
269                 chroot /cloner/mnt ${GRUB_MKCONFIG} -o /boot/grub2/grub.cfg >> /tmp/stdout.log 2>> /tmp/stderr.log
270                 if [ "$?" = "0" ]
271                 then
272                         ok_msg
273                 else
274                         fail_msg
275                         clone_fatal_error "Failed to create grub2 config file"
276                 fi
277         fi
278
279 done < /cloner/setup/bootloader
280
281 # if a post install script exists we execute it
282 if [ -e /cloner/setup/post_install.sh ]
283 then
284         msg -n "Found a post_install.sh script executing it"
285         server_log "Executing post_install.sh '${_node}' '${_image}' '${_server}'" "${_node}"
286
287         cp /cloner/setup/post_install.sh /cloner/mnt/tmp/post_install.sh
288         chmod +x /cloner/mnt/tmp/post_install.sh
289
290         chroot /cloner/mnt /tmp/post_install.sh "${_node}" "${_image}" "${_server}"
291         rm -f /cloner/mnt/tmp/post_install.sh
292         
293 fi
294
295 # we want to sort our filesystems via the mntpoint field so we can unmount them in
296 # the reverse order
297 sort -r -k2 /cloner/setup/filesystems \
298         > /cloner/setup/filesystems.revsorted 2>> /tmp/stderr.log
299
300 header "Unmounting filesystems"
301 server_log "Unmounting filesystems" "${_node}"
302
303 umount /cloner/mnt/proc
304 umount /cloner/mnt/sys
305
306 while read line
307 do
308         FS_DEVICE=`echo $line | awk '{print $1}'`
309         FS_MNTPOINT=`echo $line | awk '{print $2}'`
310         FS_TYPE=`echo $line | awk '{print $3}'`
311         FS_LABEL=`echo $line | awk '{print $4}'`
312
313         case "${FS_TYPE}"
314         in
315                 swap)
316                         msg -n "Turning off swap partitions"
317                         swapoff ${FS_DEVICE} >> /tmp/stdout.log 2>> /tmp/stderr.log
318                         ok_or_fail $?
319                         ;;
320                 *)
321                         msg -n "Unmounting ${FS_DEVICE} -> ${FS_MNTPOINT}"
322                         umount /cloner/mnt${FS_MNTPOINT} >> /tmp/stdout.log 2>> /tmp/stderr.log
323                         ok_or_fail $?
324                         ;;
325         esac
326 done < /cloner/setup/filesystems.revsorted
327