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