Updated prep_disks to read raidconf3 file for making MD devices with a name and speci ...
[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_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_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_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_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_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/raidconf3 ]
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_NAME=`echo $line | awk '{print $2}'`
197                 MD_UUID=`echo $line | awk '{print $3}'`
198                 MD_LEVEL=`echo $line | awk '{print $4}'`
199                 MD_METADATA=`echo $line | awk '{print $5}'`
200                 MD_DISK_QTY=`echo $line | awk '{print $6}'`
201                 MD_SPARE_QTY=`echo $line | awk '{print $7}'`
202                 MD_DISKS=`echo $line | awk '{print $8}'`
203                 MD_SPARES=`echo $line | awk '{print $9}'`
204
205                 T1=`echo $MD_DISKS | sed 's/,/ /g'`
206                 T2=`echo $MD_SPARES | sed 's/,/ /g'`
207                 MD_DISK_STRING="${T1} ${T2}"
208                 
209
210                 msg -n "Creating ${ANSI_BLUE}${MD_DEVICE}${ANSI_DONE} - $MD_LEVEL on $MD_DISK_QTY disk(s)"
211                 server_log "Creating ${MD_DEVICE} - ${MD_LEVEL} on ${MD_DISK_QTY} disk(s)" "${_node}"
212                 if [ ${MD_LEVEL} == "raid0" ];
213                 then
214                         /usr/bin/mdadm --create ${MD_DEVICE} --uuid=${MD_UUID} --name=${MD_NAME} --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
215                 else
216                         /usr/bin/mdadm --create ${MD_DEVICE} --uuid=${MD_UUID} --name=${MD_NAME} --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
217                 fi
218
219                 if [ "$?" != "0" ];
220                 then
221                         fail_msg
222                         clone_fatal_error "Failed to create RAID device ${MD_DEVICE}"
223                 else
224                         ok_msg
225                 fi
226         done < /cloner/setup/raidconf3
227 elif [ -e /cloner/setup/raidconf2 ]
228 then
229         header "Setting up software RAID devices"
230
231         while read line
232         do
233                 MD_DEVICE=`echo $line | awk '{print $1}'`
234                 MD_LEVEL=`echo $line | awk '{print $2}'`
235                 MD_METADATA=`echo $line | awk '{print $3}'`
236                 MD_DISK_QTY=`echo $line | awk '{print $4}'`
237                 MD_SPARE_QTY=`echo $line | awk '{print $5}'`
238                 MD_DISKS=`echo $line | awk '{print $6}'`
239                 MD_SPARES=`echo $line | awk '{print $7}'`
240
241                 T1=`echo $MD_DISKS | sed 's/,/ /g'`
242                 T2=`echo $MD_SPARES | sed 's/,/ /g'`
243                 MD_DISK_STRING="${T1} ${T2}"
244                 
245
246                 msg -n "Creating ${ANSI_BLUE}${MD_DEVICE}${ANSI_DONE} - $MD_LEVEL on $MD_DISK_QTY disk(s)"
247                 server_log "Creating ${MD_DEVICE} - ${MD_LEVEL} on ${MD_DISK_QTY} disk(s)" "${_node}"
248                 if [ ${MD_LEVEL} == "raid0" ];
249                 then
250                         /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
251                 else
252                         /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
253                 fi
254
255                 if [ "$?" != "0" ];
256                 then
257                         fail_msg
258                         clone_fatal_error "Failed to create RAID device ${MD_DEVICE}"
259                 else
260                         ok_msg
261                 fi
262         done < /cloner/setup/raidconf2
263 elif [ -e /cloner/setup/raidconf ]
264 then
265         header "Setting up software RAID devices"
266
267         while read line
268         do
269                 MD_DEVICE=`echo $line | awk '{print $1}'`
270                 MD_LEVEL=`echo $line | awk '{print $2}'`
271                 MD_DISK_QTY=`echo $line | awk '{print $3}'`
272                 MD_SPARE_QTY=`echo $line | awk '{print $4}'`
273                 MD_DISKS=`echo $line | awk '{print $5}'`
274                 MD_SPARES=`echo $line | awk '{print $6}'`
275
276                 T1=`echo $MD_DISKS | sed 's/,/ /g'`
277                 T2=`echo $MD_SPARES | sed 's/,/ /g'`
278                 MD_DISK_STRING="${T1} ${T2}"
279                 
280
281                 msg -n "Creating ${ANSI_BLUE}${MD_DEVICE}${ANSI_DONE} - $MD_LEVEL on $MD_DISK_QTY disk(s)"
282                 server_log "Creating ${MD_DEVICE} - ${MD_LEVEL} on ${MD_DISK_QTY} disk(s)" "${_node}"
283                 if [ ${MD_LEVEL} == "raid0" ];
284                 then
285                         /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
286                 else
287                         /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
288                 fi
289
290                 if [ "$?" != "0" ];
291                 then
292                         fail_msg
293                         clone_fatal_error "Failed to create RAID device ${MD_DEVICE}"
294                 else
295                         ok_msg
296                 fi
297         done < /cloner/setup/raidconf
298
299 fi
300
301 if [ -e /cloner/setup/pv_devices ]
302 then
303
304         header "Setting up LVM groups"
305
306         while read line
307         do
308                 PV_DEVICE=`echo $line | awk '{print $1}'`
309                 PV_VG=`echo $line | awk '{print $2}'`
310                 PV_UUID=`echo $line | awk '{print $3}'`
311
312                 msg -n "Setting UUID on ${ANSI_BLUE}${PV_DEVICE}${ANSI_DONE}"
313                 server_log "Setting LVM UUID on ${PV_DEVICE}" "${_node}"
314
315                 /usr/bin/lvm pvcreate -ff -y --uuid ${PV_UUID} \
316                         --restorefile /cloner/setup/lvm.${PV_VG} \
317                         ${PV_DEVICE} >> /tmp/stdout.log 2>> /tmp/stderr.log
318
319                 if [ "$?" != "0" ];
320                 then
321                         fail_msg
322                         clone_fatal_error "Failed to restore LVM UUID ${PV_UUID} to ${PV_DEVICE}";
323                 else
324                         ok_msg
325                 fi
326         done < /cloner/setup/pv_devices
327
328         for i in `ls -1 /cloner/setup/lvm.*`
329         do
330                 FILENAME=`basename $i`
331                 VG_NAME=`echo $FILENAME | cut -d"." -f2`        
332
333                 msg -n "Restoring volume group ${ANSI_BLUE}${VG_NAME}${ANSI_DONE}"
334                 server_log "Restoring volume group ${VG_NAME}" "${_node}"
335
336                 /usr/bin/lvm vgcfgrestore -f $i ${VG_NAME} \
337                         >> /tmp/stdout.log 2>> /tmp/stderr.log
338
339                 if [ "$?" != "0" ];
340                 then
341                         fail_msg
342                         clone_fatal_error "Failed to restore LVM volume ${VG_NAME}";
343                 else
344                         ok_msg
345                 fi
346                 msg -n "Activating volume group ${ANSI_BLUE}${VG_NAME}${ANSI_DONE}"
347                 server_log "Activating volume group ${VG_NAME}" "${_node}"
348                 /usr/bin/lvm vgchange -a y ${VG_NAME} \
349                         >> /tmp/stdout.log 2>> /tmp/stderr.log
350
351                 if [ "$?" != "0" ];
352                 then
353                         fail_msg
354                         clone_fatal_error "Failed to bring LVM volume ${VG_NAME} online";
355                 else
356                         ok_msg
357                 fi
358         done
359 fi
360
361 # we want to sort our filesystems via the mntpoint field so we can mount them in
362 # the correct order
363 sort -k2 /cloner/setup/filesystems \
364         > /cloner/setup/filesystems.sorted 2>> /tmp/stderr.log
365
366 header "Creating/mounting filesystems"
367 while read line
368 do
369         FS_DEVICE=`echo $line | awk '{print $1}'`
370         FS_MNTPOINT=`echo $line | awk '{print $2}'`
371         FS_TYPE=`echo $line | awk '{print $3}'`
372         FS_LABEL=`echo $line | awk '{print $4}'`
373
374         if [ "${_noddfs}" = "1" ]
375         then
376                 msg "Skipping erasing first 5MB on partitions"
377         else
378                 msg -n "Erasing first 5MB of data on partition"
379                 dd if=/dev/zero of=${FS_DEVICE} bs=1M count=5 \
380                         >> /tmp/stdout.log 2>> /tmp/stderr.log
381                 ok_or_fail $?
382         fi
383
384         if [ "${_nomkfs}" = "" ]
385         then
386
387                 msg -n "Creating ${ANSI_BLUE}${FS_TYPE}${ANSI_DONE} filesystem on ${ANSI_BLUE}${FS_DEVICE}${ANSI_DONE}"
388                 server_log "Creating ${FS_TYPE} filesystem on ${FS_DEVICE}" "${_node}"
389
390                 case "${FS_TYPE}"
391                 in
392                         ext4)
393                                         CMD="/usr/bin/mke2fs -F -T ext4 ${FS_DEVICE}"
394                                         if [ "${FS_LABEL}" != "" ]
395                                         then
396                                                 CMD="/usr/bin/mke2fs -F -T ext4 -L ${FS_LABEL} ${FS_DEVICE}"
397                                         fi
398
399                                         $CMD >> /tmp/stdout.log 2>> /tmp/stderr.log
400                                         if [ "$?" = "0" ]
401                                         then
402                                                 # try to tune it out of fsck's but dont care if it fails
403                                                 /usr/bin/tune2fs -c 0 ${FS_DEVICE}
404                                                 ok_msg
405                                         else
406                                                 fail_msg
407                                                 clone_fatal_error "Failed to make ext4 filesystem on ${FS_DEVICE}"
408                                         fi
409                                 ;;
410                         ext3)
411                                         CMD="/usr/bin/mke2fs -F -T ext3 ${FS_DEVICE}"
412                                         if [ "${FS_LABEL}" != "" ]
413                                         then    
414                                                 CMD="/usr/bin/mke2fs -F -T ext3 -L ${FS_LABEL} ${FS_DEVICE}"
415                                         fi
416         
417                                         $CMD >> /tmp/stdout.log 2>> /tmp/stderr.log
418                                         if [ "$?" = "0" ]
419                                         then
420                                                 # try to tune it out of fsck's but dont care if it fails
421                                                 /usr/bin/tune2fs -c 0 ${FS_DEVICE}
422                                                 ok_msg
423                                         else
424                                                 fail_msg
425                                                 clone_fatal_error "Failed to make ext3 filesystem on ${FS_DEVICE}"
426                                         fi
427                                 ;;
428                         ext2)
429                                         CMD="/usr/bin/mke2fs -F -T ext2 ${FS_DEVICE}"
430                                         if [ "${FS_LABEL}" != "" ]
431                                         then    
432                                                 CMD="/usr/bin/mke2fs -F -T ext2 -L ${FS_LABEL} ${FS_DEVICE}"
433                                         fi
434         
435                                         $CMD >> /tmp/stdout.log 2>> /tmp/stderr.log
436                                         if [ "$?" = "0" ]
437                                         then
438                                                 ok_msg
439                                         else
440                                                 fail_msg
441                                                 clone_fatal_error "Failed to make ext2 filesystem on ${FS_DEVICE}"
442                                         fi
443                                 ;;
444                         xfs)
445                                         CMD="/usr/bin/mkfs.xfs -f ${FS_DEVICE}"
446                                         if [ "${FS_LABEL}" != "" ]
447                                         then    
448                                                 CMD="/usr/bin/mkfs.xfs -L ${FS_LABEL} -f ${FS_DEVICE}"
449                                         fi
450         
451
452                                         $CMD >> /tmp/stdout.log 2>> /tmp/stderr.log
453                                         if [ "$?" = "0" ]
454                                         then
455                                                 ok_msg
456                                         else
457                                                 fail_msg
458                                                 clone_fatal_error "Failed to make xfs filesystem on ${FS_DEVICE}"
459                                         fi
460                                 ;;
461         
462                         reiserfs)
463                                         /usr/bin/mkreiserfs -ff ${FS_DEVICE} \
464                                                 >> /tmp/stdout.log 2>> /tmp/stderr.log
465         
466                                         if [ "$?" = "0" ]
467                                         then
468                                                 ok_msg
469                                         else
470                                                 fail_msg
471                                                 clone_fatal_error "Failed to make reiserfs filesystem on ${FS_DEVICE}"
472                                         fi
473                                 ;;
474                         swap)
475                                         CMD="/usr/bin/mkswap -f ${FS_DEVICE}"
476                                         if [ "${FS_LABEL}" != "" ]
477                                         then    
478                                                 CMD="/usr/bin/mkswap -L ${FS_LABEL} -f ${FS_DEVICE}"
479                                         fi
480         
481                                         $CMD >> /tmp/stdout.log 2>> /tmp/stderr.log
482                                         if [ "$?" = "0" ]
483                                         then
484                                                 ok_msg
485                                         else
486                                                 fail_msg
487                                                 clone_fatal_error "Failed to make swap filesystem on ${FS_DEVICE}"
488                                         fi
489         
490                                         msg -n "Activating swap device ${FS_DEVICE}"
491                                         swapon ${FS_DEVICE} >> /tmp/stdout.log 2>> /tmp/stderr.log
492                                         if [ "$?" = "0" ]
493                                         then
494                                                 ok_msg
495                                         else
496                                                 fail_msg
497                                                 clone_fatal_error "Failed to activate swap filesystem on ${FS_DEVICE}"
498                                         fi
499                                 ;;
500                         *)
501                                 clone_fatal_error "Don't know how to handle filesystem type ${FS_TYPE}"
502                                 ;;
503                 esac
504
505         fi
506         
507         # if it starts with a / it's going to be mounted
508         if [ `echo ${FS_MNTPOINT} | grep '^/'` ]
509         then
510                 MNT_POINT=`echo ${FS_MNTPOINT} | sed 's/^\///'`
511                 msg -n "Mounting ${ANSI_BLUE}${FS_DEVICE}${ANSI_DONE} on ${ANSI_BLUE}/cloner/mnt/${MNT_POINT}${ANSI_DONE}"
512
513                 mkdir -p /cloner/mnt/${MNT_POINT} >> /tmp/stdout.log 2>> /tmp/stderr.log
514
515                 mount -t ${FS_TYPE} ${FS_DEVICE} /cloner/mnt/${MNT_POINT} >> /tmp/stdout.log 2>> /tmp/stderr.log
516                 if [ "$?" = "0" ]
517                 then
518                         ok_msg
519                 else
520                         fail_msg
521                         clone_fatal_error "Failed to mount ${FS_DEVICE} to /cloner/mnt/${MNT_POINT}"
522                 fi
523         fi
524
525 done < /cloner/setup/filesystems.sorted
526
527