Added support for grub2 and parted
[bootimage-initfs.git] / etc / cloner / prep_disks.sh
1 #!/bin/ash
2
3 . /etc/library.sh
4 . /etc/cloner/include.sh
5
6
7 # TODO:
8 #
9 #  1. Finish software RAID setup
10 #
11
12 if [ "${_nomkfs}" = "" ]
13 then
14
15         header "Partiting disks and formating filesystems"
16
17         DELAY=15
18         msg "Going to partition and format disks in ${DELAY} seconds, press [CTRL-C] to abort"
19         msg -n "${ANSI_BLUE}"
20         while [ "${DELAY}" != "0" ]
21         do
22                 msg -n " ${ANSI_BLUE}${DELAY} ${ANSI_DONE} "
23                 sleep 1
24                 DELAY=`expr ${DELAY} - 1`
25         done
26         msg ""
27         
28
29         header "Partition the disk devices"
30         
31         for i in `ls -1 /cloner/setup/*.sfdisk`
32         do
33                 DEVICE=`basename ${i}`
34                 DEVICE=`echo ${DEVICE} | sed 's/\..*//g'`
35         
36         
37                 msg -n "Partition device ${ANSI_BLUE}${DEVICE}${ANSI_DONE} "
38         
39                 /usr/bin/sfdisk --force --no-reread -uM /dev/${DEVICE} < ${i} \
40                         >> /tmp/stdout.log 2>>/tmp/stderr.log
41         
42                 if [ "$?" != "0" ];
43                 then
44                         fail_msg
45                         fatal_error "Failed to partition disk device ${DEVICE} check /tmp/stdout.log or /tmp/stderr.log for details"
46                 else
47                         ok_msg
48                 fi
49         done
50         
51         for i in `ls -1 /cloner/setup/*.sec_fdisk 2> /dev/null`
52         do
53                 DEVICE=`basename ${i}`
54                 DEVICE=`echo ${DEVICE} | sed 's/\..*//g'`
55         
56                 msg -n "Partition device ${ANSI_BLUE}${DEVICE}${ANSI_DONE} "
57         
58                 /usr/bin/sfdisk --force --no-reread /dev/${DEVICE} < ${i} \
59                         >> /tmp/stdout.log 2>>/tmp/stderr.log
60         
61                 if [ "$?" != "0" ];
62                 then
63                         fail_msg
64                         fatal_error "Failed to partition disk device ${DEVICE} check /tmp/stdout.log or /tmp/stderr.log for details"
65                 else
66                         ok_msg
67                 fi
68         done
69
70         for i in `ls -1 /cloner/setup/*.parted 2> /dev/null`
71         do
72
73                 count=0
74                 while read line
75                 do
76                         if [ "${count}" == "0" ]
77                         then
78                                 echo -n ""              
79                         # this is the disk label info
80                         elif [ "${count}" == "1" ]
81                         then
82                                 PED_DEV_NAME=`echo $line | cut -d":" -f1`
83                                 PED_DEV_END=`echo $line | cut -d":" -f2`
84                                 PED_DEV_TRANS=`echo $line | cut -d":" -f3`
85                                 PED_DEV_SECTOR=`echo $line | cut -d":" -f4`
86                                 PED_DEV_PHYS=`echo $line | cut -d":" -f5`
87                                 PED_DEV_LABEL=`echo $line | cut -d":" -f6`
88                                 PED_DEV_MODEL=`echo $line | cut -d":" -f7`
89                 
90                                 msg -n "Creating ${ANSI_BLUE}${PED_DEV_LABEL}${ANSI_DONE} label on $PED_DEV_NAME"
91                                 CMD="mklabel $PED_DEV_LABEL"
92                                 /usr/sbin/parted -s $PED_DEV_NAME $CMD
93                 
94                                 if [ "$?" != 0 ]
95                                 then
96                                         fail_msg
97                                         fatal_error "Failed to create disk label"
98                                 else
99                                         ok_msg
100                                 fi
101
102                 
103                         # this is the partition
104                         else
105                                 PED_PART_NUM=`echo $line | cut -d":" -f1`
106                                 PED_PART_START=`echo $line | cut -d":" -f2`
107                                 PED_PART_END=`echo $line | cut -d":" -f3`
108                                 PED_PART_LENGTH=`echo $line | cut -d":" -f4`
109                                 PED_PART_FSTYPE=`echo $line | cut -d":" -f5`
110                                 PED_PART_NAME=`echo $line | cut -d":" -f6`
111                                 PED_PART_FLAGS=`echo $line | cut -d":" -f7`
112                                 PED_PART_FLAGS=`echo $PED_PART_FLAGS | sed s/\;//g`
113                                 PED_PART_FLAGS=`echo $PED_PART_FLAGS | sed s/\,//g`
114                 
115                                 if [ "${PED_DEV_LABEL}" == "msdos" ]
116                                 then
117                 
118                                         PED_PART_TYPE="primary"
119                                         if [ "${PED_PART_FLAGS}" == "lba" ]
120                                         then
121                                                 PED_PART_TYPE="extended"
122                                         elif [ "${PED_PART_NUM}" -gt "4" ]
123                                         then
124                                                 PED_PART_TYPE="logical"
125                                         fi
126                 
127                                         msg -n "Creating partition ${ANSI_BLUE}${PED_PART_START} - ${PED_PART_END}${ANSI_DONE} on $PED_DEV_NAME"
128                                         CMD="mkpart $PED_PART_TYPE $PED_PART_FSTYPE $PED_PART_START $PED_PART_END"
129                                         /usr/sbin/parted -s $PED_DEV_NAME $CMD
130         
131                                         if [ "$?" != 0 ]
132                                         then
133                                                 fail_msg
134                                                 fatal_error "Failed to create disk partition"
135                                         else
136                                                 ok_msg
137                                         fi
138
139                                 elif [ "${PED_DEV_LABEL}" == "gpt" ]
140                                 then
141                                         msg -n "Creating partition ${ANSI_BLUE}${PED_PART_START} - ${PED_PART_END}${ANSI_DONE} on $PED_DEV_NAME"
142                                         CMD="mkpart $PED_PART_FSTYPE $PED_PART_START $PED_PART_END"
143                                         /usr/sbin/parted -s $PED_DEV_NAME $CMD
144                                         if [ "$?" != 0 ]
145                                         then
146                                                 fail_msg
147                                                 fatal_error "Failed to create disk partition"
148                                         else
149                                                 ok_msg
150                                         fi
151                 
152                                 fi
153                                 if [ "${PED_PART_FLAGS}" != "" ]
154                                 then
155                                         for flag in ${PED_PART_FLAGS}
156                                         do
157                                                 msg -n "Setting flag ${ANSI_BLUE}${flag}${ANSI_DONE} on $PED_PART_NUM"
158                                                 CMD="set $PED_PART_NUM $flag on"
159                                                 /usr/sbin/parted -s $PED_DEV_NAME $CMD
160
161                                                 if [ "$?" != 0 ]
162                                                 then
163                                                         fail_msg
164                                                         fatal_error "Failed to create disk partition"
165                                                 else
166                                                         ok_msg
167                                                 fi
168                                         done
169                                 fi
170                 
171                         fi
172                 
173                         count=`expr ${count} + 1`
174                 done < $i
175         done
176 fi
177         
178 if [ -e /cloner/setup/raidconf2 ]
179 then
180         header "Setting up software RAID devices"
181
182         while read line
183         do
184                 MD_DEVICE=`echo $line | awk '{print $1}'`
185                 MD_LEVEL=`echo $line | awk '{print $2}'`
186                 MD_METADATA=`echo $line | awk '{print $3}'`
187                 MD_DISK_QTY=`echo $line | awk '{print $4}'`
188                 MD_SPARE_QTY=`echo $line | awk '{print $5}'`
189                 MD_DISKS=`echo $line | awk '{print $6}'`
190                 MD_SPARES=`echo $line | awk '{print $7}'`
191
192                 T1=`echo $MD_DISKS | sed 's/,/ /g'`
193                 T2=`echo $MD_SPARES | sed 's/,/ /g'`
194                 MD_DISK_STRING="${T1} ${T2}"
195                 
196
197                 msg -n "Creating ${ANSI_BLUE}${MD_DEVICE}${ANSI_DONE} - $MD_LEVEL on $MD_DISK_QTY disk(s)"
198                 if [ ${MD_LEVEL} == "raid0" ];
199                 then
200                         /usr/bin/mdadm --create ${MD_DEVICE} --metadata=${MD_METADATA} --force --run --level=${MD_LEVEL} --chunk=128 --raid-devices=${MD_DISK_QTY} ${MD_DISK_STRING} >> /tmp/stdout.log 2>> /tmp/stderr.log
201                 else
202                         /usr/bin/mdadm --create ${MD_DEVICE} --metadata=${MD_METADATA} --force --run --level=${MD_LEVEL} --chunk=128 --raid-devices=${MD_DISK_QTY} --spare-devices=${MD_SPARE_QTY} ${MD_DISK_STRING} >> /tmp/stdout.log 2>> /tmp/stderr.log
203                 fi
204
205                 if [ "$?" != "0" ];
206                 then
207                         fail_msg
208                         fatal_error "Failed to create RAID device ${MD_DEVICE}"
209                 else
210                         ok_msg
211                 fi
212         done < /cloner/setup/raidconf2
213 elif [ -e /cloner/setup/raidconf ]
214 then
215         header "Setting up software RAID devices"
216
217         while read line
218         do
219                 MD_DEVICE=`echo $line | awk '{print $1}'`
220                 MD_LEVEL=`echo $line | awk '{print $2}'`
221                 MD_DISK_QTY=`echo $line | awk '{print $3}'`
222                 MD_SPARE_QTY=`echo $line | awk '{print $4}'`
223                 MD_DISKS=`echo $line | awk '{print $5}'`
224                 MD_SPARES=`echo $line | awk '{print $6}'`
225
226                 T1=`echo $MD_DISKS | sed 's/,/ /g'`
227                 T2=`echo $MD_SPARES | sed 's/,/ /g'`
228                 MD_DISK_STRING="${T1} ${T2}"
229                 
230
231                 msg -n "Creating ${ANSI_BLUE}${MD_DEVICE}${ANSI_DONE} - $MD_LEVEL on $MD_DISK_QTY disk(s)"
232                 if [ ${MD_LEVEL} == "raid0" ];
233                 then
234                         /usr/bin/mdadm --create ${MD_DEVICE} -e 0.90 --force --run --level=${MD_LEVEL} --chunk=128 --raid-devices=${MD_DISK_QTY} ${MD_DISK_STRING} >> /tmp/stdout.log 2>> /tmp/stderr.log
235                 else
236                         /usr/bin/mdadm --create ${MD_DEVICE} -e 0.90 --force --run --level=${MD_LEVEL} --chunk=128 --raid-devices=${MD_DISK_QTY} --spare-devices=${MD_SPARE_QTY} ${MD_DISK_STRING} >> /tmp/stdout.log 2>> /tmp/stderr.log
237                 fi
238
239                 if [ "$?" != "0" ];
240                 then
241                         fail_msg
242                         fatal_error "Failed to create RAID device ${MD_DEVICE}"
243                 else
244                         ok_msg
245                 fi
246         done < /cloner/setup/raidconf
247
248 fi
249
250 if [ -e /cloner/setup/pv_devices ]
251 then
252
253         header "Setting up LVM groups"
254
255         while read line
256         do
257                 PV_DEVICE=`echo $line | awk '{print $1}'`
258                 PV_VG=`echo $line | awk '{print $2}'`
259                 PV_UUID=`echo $line | awk '{print $3}'`
260
261                 msg -n "Setting UUID on ${ANSI_BLUE}${PV_DEVICE}${ANSI_DONE}"
262                 /usr/bin/lvm pvcreate -ff -y --uuid ${PV_UUID} \
263                         --restorefile /cloner/setup/lvm.${PV_VG} \
264                         ${PV_DEVICE} >> /tmp/stdout.log 2>> /tmp/stderr.log
265
266                 if [ "$?" != "0" ];
267                 then
268                         fail_msg
269                         fatal_error "Failed to restore LVM UUID ${PV_UUID} to ${PV_DEVICE}";
270                 else
271                         ok_msg
272                 fi
273         done < /cloner/setup/pv_devices
274
275         for i in `ls -1 /cloner/setup/lvm.*`
276         do
277                 FILENAME=`basename $i`
278                 VG_NAME=`echo $FILENAME | cut -d"." -f2`        
279
280                 msg -n "Restoring volume group ${ANSI_BLUE}${VG_NAME}${ANSI_DONE}"
281                 /usr/bin/lvm vgcfgrestore -f $i ${VG_NAME} \
282                         >> /tmp/stdout.log 2>> /tmp/stderr.log
283
284                 if [ "$?" != "0" ];
285                 then
286                         fail_msg
287                         fatal_error "Failed to restore LVM volume ${VG_NAME}";
288                 else
289                         ok_msg
290                 fi
291                 msg -n "Activating volume group ${ANSI_BLUE}${VG_NAME}${ANSI_DONE}"
292                 /usr/bin/lvm vgchange -a y ${VG_NAME} \
293                         >> /tmp/stdout.log 2>> /tmp/stderr.log
294
295                 if [ "$?" != "0" ];
296                 then
297                         fail_msg
298                         fatal_error "Failed to bring LVM volume ${VG_NAME} online";
299                 else
300                         ok_msg
301                 fi
302         done
303 fi
304
305 # we want to sort our filesystems via the mntpoint field so we can mount them in
306 # the correct order
307 sort -k2 /cloner/setup/filesystems \
308         > /cloner/setup/filesystems.sorted 2>> /tmp/stderr.log
309
310 header "Creating/mounting filesystems"
311 while read line
312 do
313         FS_DEVICE=`echo $line | awk '{print $1}'`
314         FS_MNTPOINT=`echo $line | awk '{print $2}'`
315         FS_TYPE=`echo $line | awk '{print $3}'`
316         FS_LABEL=`echo $line | awk '{print $4}'`
317
318         if [ "${_noddfs}" = "1" ]
319         then
320                 msg "Skipping erasing first 5MB on partitions"
321         else
322                 msg -n "Erasing first 5MB of data on partition"
323                 dd if=/dev/zero of=${FS_DEVICE} bs=1M count=5 \
324                         >> /tmp/stdout.log 2>> /tmp/stderr.log
325                 ok_or_fail $?
326         fi
327
328         if [ "${_nomkfs}" = "" ]
329         then
330
331                 msg -n "Creating ${ANSI_BLUE}${FS_TYPE}${ANSI_DONE} filesystem on ${ANSI_BLUE}${FS_DEVICE}${ANSI_DONE}"
332
333                 case "${FS_TYPE}"
334                 in
335                         ext4)
336                                         CMD="/usr/bin/mke2fs -F -T ext4 ${FS_DEVICE}"
337                                         if [ "${FS_LABEL}" != "" ]
338                                         then
339                                                 CMD="/usr/bin/mke2fs -F -T ext4 -L ${FS_LABEL} ${FS_DEVICE}"
340                                         fi
341
342                                         $CMD >> /tmp/stdout.log 2>> /tmp/stderr.log
343                                         if [ "$?" = "0" ]
344                                         then
345                                                 # try to tune it out of fsck's but dont care if it fails
346                                                 /usr/bin/tune2fs -c 0 ${FS_DEVICE}
347                                                 ok_msg
348                                         else
349                                                 fail_msg
350                                                 fatal_error "Failed to make ext4 filesystem on ${FS_DEVICE}"
351                                         fi
352                                 ;;
353                         ext3)
354                                         CMD="/usr/bin/mke2fs -F -T ext3 ${FS_DEVICE}"
355                                         if [ "${FS_LABEL}" != "" ]
356                                         then    
357                                                 CMD="/usr/bin/mke2fs -F -T ext3 -L ${FS_LABEL} ${FS_DEVICE}"
358                                         fi
359         
360                                         $CMD >> /tmp/stdout.log 2>> /tmp/stderr.log
361                                         if [ "$?" = "0" ]
362                                         then
363                                                 # try to tune it out of fsck's but dont care if it fails
364                                                 /usr/bin/tune2fs -c 0 ${FS_DEVICE}
365                                                 ok_msg
366                                         else
367                                                 fail_msg
368                                                 fatal_error "Failed to make ext3 filesystem on ${FS_DEVICE}"
369                                         fi
370                                 ;;
371                         ext2)
372                                         CMD="/usr/bin/mke2fs -F -T ext2 ${FS_DEVICE}"
373                                         if [ "${FS_LABEL}" != "" ]
374                                         then    
375                                                 CMD="/usr/bin/mke2fs -F -T ext2 -L ${FS_LABEL} ${FS_DEVICE}"
376                                         fi
377         
378                                         $CMD >> /tmp/stdout.log 2>> /tmp/stderr.log
379                                         if [ "$?" = "0" ]
380                                         then
381                                                 ok_msg
382                                         else
383                                                 fail_msg
384                                                 fatal_error "Failed to make ext2 filesystem on ${FS_DEVICE}"
385                                         fi
386                                 ;;
387                         xfs)
388                                         /usr/bin/mkfs.xfs -f ${FS_DEVICE} \
389                                                 >> /tmp/stdout.log 2>> /tmp/stderr.log
390                                         if [ "$?" = "0" ]
391                                         then
392                                                 ok_msg
393                                         else
394                                                 fail_msg
395                                                 fatal_error "Failed to make xfs filesystem on ${FS_DEVICE}"
396                                         fi
397                                 ;;
398         
399                         reiserfs)
400                                         /usr/bin/mkreiserfs -ff ${FS_DEVICE} \
401                                                 >> /tmp/stdout.log 2>> /tmp/stderr.log
402         
403                                         if [ "$?" = "0" ]
404                                         then
405                                                 ok_msg
406                                         else
407                                                 fail_msg
408                                                 fatal_error "Failed to make reiserfs filesystem on ${FS_DEVICE}"
409                                         fi
410                                 ;;
411                         swap)
412                                         /usr/bin/mkswap -f ${FS_DEVICE} \
413                                                 >> /tmp/stdout.log 2>> /tmp/stderr.log
414                                         if [ "$?" = "0" ]
415                                         then
416                                                 ok_msg
417                                         else
418                                                 fail_msg
419                                                 fatal_error "Failed to make swap filesystem on ${FS_DEVICE}"
420                                         fi
421         
422                                         msg -n "Activating swap device ${FS_DEVICE}"
423                                         swapon ${FS_DEVICE} >> /tmp/stdout.log 2>> /tmp/stderr.log
424                                         if [ "$?" = "0" ]
425                                         then
426                                                 ok_msg
427                                         else
428                                                 fail_msg
429                                                 fatal_error "Failed to activate swap filesystem on ${FS_DEVICE}"
430                                         fi
431                                 ;;
432                         *)
433                                 fatal_error "Don't know how to handle filesystem type ${FS_TYPE}"
434                                 ;;
435                 esac
436
437         fi
438         
439         # if it starts with a / it's going to be mounted
440         if [ `echo ${FS_MNTPOINT} | grep '^/'` ]
441         then
442                 MNT_POINT=`echo ${FS_MNTPOINT} | sed 's/^\///'`
443                 msg -n "Mounting ${ANSI_BLUE}${FS_DEVICE}${ANSI_DONE} on ${ANSI_BLUE}/cloner/mnt/${MNT_POINT}${ANSI_DONE}"
444
445                 mkdir -p /cloner/mnt/${MNT_POINT} >> /tmp/stdout.log 2>> /tmp/stderr.log
446
447                 mount -t ${FS_TYPE} ${FS_DEVICE} /cloner/mnt/${MNT_POINT} >> /tmp/stdout.log 2>> /tmp/stderr.log
448                 if [ "$?" = "0" ]
449                 then
450                         ok_msg
451                 else
452                         fail_msg
453                         fatal_error "Failed to mount ${FS_DEVICE} to /cloner/mnt/${MNT_POINT}"
454                 fi
455         fi
456
457 done < /cloner/setup/filesystems.sorted
458
459