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