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