Added support for grub2 and parted
[bootimage-initfs.git] / etc / cloner / installer.sh
1 #!/bin/ash
2
3 . /etc/library.sh
4 . /etc/cloner/include.sh
5 . /tmp/cmdline.dat
6
7 # TODO
8 #  * Further testing of GRUB / LILO bootloader
9
10 header "Starting the installation process"
11
12 # --------------
13 #  Perform a multicast install
14 # --------------
15 if [ "${_multicast}" = "1" ]
16 then
17
18         msg "Starting multicast receiver, server will initiate transfer"
19         /usr/bin/udp-receiver --nokbd --pipe 'tar -xvf - -C /cloner/mnt'
20
21         if [ "$?" = "0" ]
22         then
23                 ok_msg
24         else
25                 fail_msg
26                 fatal_error "Failed to untar multicasted image"
27         fi
28
29 # --------------
30 #  A CD-DVD based install
31 # --------------
32 elif [ "${_srcpath}" != "" ]
33 then
34         msg "Starting the unpacking from CD/DVD-ROM"
35
36         # change to our install directory
37         cd /cloner/mnt >> /tmp/stdout.log 2>>/tmp/stderr.log
38
39         if [ -e /cloner/setup/disk_size ]
40         then
41                 . /cloner/setup/disk_size
42         else
43                 fatal_error "No disk size specified on setup directory"
44         fi
45
46         afio -i -Z -v -s $DISK_SIZE -H /etc/cloner/switch_cd.sh \
47                 /mnt/media/${_image}.afio
48
49         if [ "$?" != 0 ]
50         then
51                 fail_msg
52                 fatal_error "Failed to extract filesystem from CD/DVD drive"
53         fi
54
55         # revert back to our old directory
56         cd - >> /tmp/stdout.log 2>>/tmp/stderr.log
57
58 # --------------
59 #  A standard network RSYNC based install
60 # --------------
61 else
62
63         #msg "The installation process can take a while, check ${ANSI_RED}[ALT-F4]${ANSI_DONE} to view progress"
64
65         msg -n "Syncing data from ${ANSI_BLUE}${_server}${ANSI_DONE} image ${ANSI_BLUE}${_image}${ANSI_DONE}"
66         /usr/bin/rsync -avz --numeric-ids \
67                 ${_server}::${CLONER_IMAGE_PATH}/${_image}/data/  /cloner/mnt/
68         if [ "$?" = "0" ]
69         then
70                 ok_msg
71         else
72                 fail_msg
73                 fatal_error "Failed to sync filesystem data from server!"
74         fi
75         
76 fi
77         
78 # remake an directories we excluded from our rsync process
79 sort -k1 /cloner/setup/makedirectories \
80         > /cloner/setup/makedirectories.sorted 2>> /tmp/stderr.log
81
82 header "Making missing directories"
83 while read line
84 do
85
86         DIR=`echo $line | awk '{print $1}'`
87         DIR_MODE=`echo $line | awk '{print $2}'`
88         DIR_UID=`echo $line | awk '{print $3}'`
89         DIR_GID=`echo $line | awk '{print $4}'`
90
91         if [ "${DIR}" != "" ]
92         then
93                 msg -n "Making directory ${ANSI_BLUE}${DIR}${ANSI_DONE}"
94                 mkdir -p /cloner/mnt/${DIR} >> /tmp/stdout.log \
95                         2>> /tmp/stderr.log
96
97                 chmod ${DIR_MODE} /cloner/mnt/${DIR} >> \
98                         /tmp/stdout.log 2>> /tmp/stderr.log
99         
100                 chown ${DIR_UID}:${DIR_GID} /cloner/mnt/${DIR} \
101                         >> /tmp/stdout.log 2>> /tmp/stderr.log
102         
103                 if [ "$?" = "0" ]
104                 then
105                         ok_msg
106                 else
107                         fail_msg
108                 fi
109         fi
110
111 done < /cloner/setup/makedirectories.sorted
112         
113 if [ "${_node}" != "" ]
114 then
115
116         if [ "${_srcpath}" != "" ]
117         then
118                 header "Installing node specific data"
119                 msg -n "Syning node ${ANSI_BLUE}${_node}${ANSI_DONE} data from /mnt/media"
120                 rsync -avzc -I --numeric-ids --exclude /.valid \
121                         /cloner/setup/node/ /cloner/mnt \
122                         >> /tmp/stdout.log 2>> /tmp/stderr.log
123
124                 if [ "$?" = "0" ]
125                 then
126                         ok_msg
127                 else
128                         fail_msg
129                         fatal_error "Failed to sync node data from CD/DVD"
130                 fi
131         else
132                 header "Installing node specific data"
133                 msg -n "Syning node ${ANSI_BLUE}${_node}${ANSI_DONE} data from ${_server}"
134
135                 rsync -avzc --numeric-ids --exclude /.valid \
136                         ${_server}::${CLONER_IMAGE_PATH}/${_image}/nodes/${_node}/ \
137                         /cloner/mnt >> /tmp/stdout.log 2>> /tmp/stderr.log
138                 if [ "$?" = "0" ]
139                 then
140                         ok_msg
141                 else
142                         fail_msg
143                         fatal_error "Failed to sync node data from server!"
144                 fi
145         fi
146 fi
147
148 header "Installing bootloader"
149 while read line
150 do
151         BL_DEVICE=`echo $line | awk '{print $1}'`
152         BL_TYPE=`echo $line | awk '{print $2}'`
153
154         if [ "${BL_TYPE}" = "grub" ]
155         then
156                 msg -n "Installing grub bootloader on ${ANSI_BLUE}${BL_DEVICE}${ANSI_DONE}"
157
158                 GRUB_PATH=""
159                 if [ -x /cloner/mnt/sbin/grub-install ]
160                 then
161                         GRUB_PATH="/sbin/grub-install"
162                 elif  [ -x /cloner/mnt/usr/sbin/grub-install ]
163                 then
164                         GRUB_PATH="/usr/sbin/grub-install"
165                 elif [ -x /cloner/mnt/sbin/grub2-install ]
166                 then
167                         GRUB_PATH="/sbin/grub2-install"
168                 elif  [ -x /cloner/mnt/usr/sbin/grub2-install ]
169                 then
170                         GRUB_PATH="/usr/sbin/grub2-install"
171                 else
172                         fatal_error "Can't find the grub-install binary on mounted filesystem, no bootloader installed"
173                 fi
174
175                 chroot /cloner/mnt ${GRUB_PATH} --no-floppy ${BL_DEVICE} \
176                         >> /tmp/stdout.log 2>> /tmp/stderr.log
177                 if [ "$?" = "0" ]
178                 then
179                         ok_msg
180                 else
181                         fail_msg
182                         msg -n "Trying alternative grub installation "
183                         echo "root (hd0,0)" > /cloner/mnt/grub.txt
184                         echo "setup (hd0)" >> /cloner/mnt/grub.txt
185
186                         chroot /cloner/mnt ""/sbin/grub --device-map=/boot/grub/device.map --no-floppy --batch < /cloner/mnt/grub.txt"" \ 
187                                 >> /tmp/stdout.log 2>> /tmp/stderr.log
188                         if [ "$?" = "0" ]
189                         then
190                                 ok_msg
191                         else
192                                 fail_msg
193                                 fatal_error "Failed to install grub bootloader"
194                         fi
195                 fi
196         elif [ "${BL_TYPE}" = "lilo" ]
197         then
198                 msg -n "Installing lilo bootloader on ${ANSI_BLUE}${BL_DEVICE}${ANSI_DONE}"
199                 chroot /cloner/mnt /sbin/lilo >> /tmp/stdout.log 2>> /tmp/stderr.log
200                 if [ "$?" = "0" ]
201                 then
202                         ok_msg
203                 else
204                         fail_msg
205                         fatal_error "Failed to install grub bootloader"
206                 fi
207         fi
208
209 done < /cloner/setup/bootloader
210
211 # we want to sort our filesystems via the mntpoint field so we can unmount them in
212 # the reverse order
213 sort -r -k2 /cloner/setup/filesystems \
214         > /cloner/setup/filesystems.revsorted 2>> /tmp/stderr.log
215
216 header "Unmounting filesystems"
217 while read line
218 do
219         FS_DEVICE=`echo $line | awk '{print $1}'`
220         FS_MNTPOINT=`echo $line | awk '{print $2}'`
221         FS_TYPE=`echo $line | awk '{print $3}'`
222         FS_LABEL=`echo $line | awk '{print $4}'`
223
224         case "${FS_TYPE}"
225         in
226                 swap)
227                         msg -n "Turning off swap partitions"
228                         swapoff ${FS_DEVICE} >> /tmp/stdout.log 2>> /tmp/stderr.log
229                         ok_or_fail $?
230                         ;;
231                 *)
232                         msg -n "Unmounting ${FS_DEVICE} -> ${FS_MNTPOINT}"
233                         umount /cloner/mnt${FS_MNTPOINT} >> /tmp/stdout.log 2>> /tmp/stderr.log
234                         ok_or_fail $?
235                         ;;
236         esac
237 done < /cloner/setup/filesystems.revsorted
238