75fd7215cb51c589907f7b0353296c95e2cbee71
[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 fi
70         
71 if [ -e /cloner/setup/raidconf ]
72 then
73         header "Setting up software RAID devices"
74
75         while read line
76         do
77                 MD_DEVICE=`echo $line | awk '{print $1}'`
78                 MD_LEVEL=`echo $line | awk '{print $2}'`
79                 MD_DISK_QTY=`echo $line | awk '{print $3}'`
80                 MD_SPARE_QTY=`echo $line | awk '{print $4}'`
81                 MD_DISKS=`echo $line | awk '{print $5}'`
82                 MD_SPARES=`echo $line | awk '{print $6}'`
83
84                 T1=`echo $MD_DISKS | sed 's/,/ /g'`
85                 T2=`echo $MD_SPARES | sed 's/,/ /g'`
86                 MD_DISK_STRING="${T1} ${T2}"
87                 
88
89                 msg -n "Creating ${ANSI_BLUE}${MD_DEVICE}${ANSI_DONE} - $MD_LEVEL on $MD_DISK_QTY disk(s)"
90                 if [ ${MD_LEVEL} == "raid0" ];
91                 then
92                         /usr/bin/mdadm --create ${MD_DEVICE} --force --run --level=${MD_LEVEL} --chunk=128 --raid-devices=${MD_DISK_QTY} ${MD_DISK_STRING} >> /tmp/stdout.log 2>> /tmp/stderr.log
93                 else
94                         /usr/bin/mdadm --create ${MD_DEVICE} --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
95                 fi
96
97                 if [ "$?" != "0" ];
98                 then
99                         fail_msg
100                         fatal_error "Failed to create RAID device ${MD_DEVICE}"
101                 else
102                         ok_msg
103                 fi
104         done < /cloner/setup/raidconf
105
106 fi
107
108 if [ -e /cloner/setup/pv_devices ]
109 then
110
111         header "Setting up LVM groups"
112
113         while read line
114         do
115                 PV_DEVICE=`echo $line | awk '{print $1}'`
116                 PV_VG=`echo $line | awk '{print $2}'`
117                 PV_UUID=`echo $line | awk '{print $3}'`
118
119                 msg -n "Setting UUID on ${ANSI_BLUE}${PV_DEVICE}${ANSI_DONE}"
120                 /usr/bin/lvm pvcreate -ff -y --uuid ${PV_UUID} \
121                         --restorefile /cloner/setup/lvm.${PV_VG} \
122                         ${PV_DEVICE} >> /tmp/stdout.log 2>> /tmp/stderr.log
123
124                 if [ "$?" != "0" ];
125                 then
126                         fail_msg
127                         fatal_error "Failed to restore LVM UUID ${PV_UUID} to ${PV_DEVICE}";
128                 else
129                         ok_msg
130                 fi
131         done < /cloner/setup/pv_devices
132
133         for i in `ls -1 /cloner/setup/lvm.*`
134         do
135                 FILENAME=`basename $i`
136                 VG_NAME=`echo $FILENAME | cut -d"." -f2`        
137
138                 msg -n "Restoring volume group ${ANSI_BLUE}${VG_NAME}${ANSI_DONE}"
139                 /usr/bin/lvm vgcfgrestore -f $i ${VG_NAME} \
140                         >> /tmp/stdout.log 2>> /tmp/stderr.log
141
142                 if [ "$?" != "0" ];
143                 then
144                         fail_msg
145                         fatal_error "Failed to restore LVM volume ${VG_NAME}";
146                 else
147                         ok_msg
148                 fi
149                 msg -n "Activating volume group ${ANSI_BLUE}${VG_NAME}${ANSI_DONE}"
150                 /usr/bin/lvm vgchange -a y ${VG_NAME} \
151                         >> /tmp/stdout.log 2>> /tmp/stderr.log
152
153                 if [ "$?" != "0" ];
154                 then
155                         fail_msg
156                         fatal_error "Failed to bring LVM volume ${VG_NAME} online";
157                 else
158                         ok_msg
159                 fi
160         done
161 fi
162
163 # we want to sort our filesystems via the mntpoint field so we can mount them in
164 # the correct order
165 sort -k2 /cloner/setup/filesystems \
166         > /cloner/setup/filesystems.sorted 2>> /tmp/stderr.log
167
168 header "Creating/mounting filesystems"
169 while read line
170 do
171         FS_DEVICE=`echo $line | awk '{print $1}'`
172         FS_MNTPOINT=`echo $line | awk '{print $2}'`
173         FS_TYPE=`echo $line | awk '{print $3}'`
174         FS_LABEL=`echo $line | awk '{print $4}'`
175
176         if [ "${_noddfs}" = "1" ]
177         then
178                 msg "Skipping erasing first 5MB on partitions"
179         else
180                 msg -n "Erasing first 5MB of data on partition"
181                 dd if=/dev/zero of=${FS_DEVICE} bs=1M count=5 \
182                         >> /tmp/stdout.log 2>> /tmp/stderr.log
183                 ok_or_fail $?
184         fi
185
186         if [ "${_nomkfs}" = "" ]
187         then
188
189                 msg -n "Creating ${ANSI_BLUE}${FS_TYPE}${ANSI_DONE} filesystem on ${ANSI_BLUE}${FS_DEVICE}${ANSI_DONE}"
190
191                 case "${FS_TYPE}"
192                 in
193                         ext4)
194                                         CMD="/usr/bin/mke2fs -F -T ext4 ${FS_DEVICE}"
195                                         if [ "${FS_LABEL}" != "" ]
196                                         then
197                                                 CMD="/usr/bin/mke2fs -F -T ext4 -L ${FS_LABEL} ${FS_DEVICE}"
198                                         fi
199
200                                         $CMD >> /tmp/stdout.log 2>> /tmp/stderr.log
201                                         if [ "$?" = "0" ]
202                                         then
203                                                 # try to tune it out of fsck's but dont care if it fails
204                                                 /usr/bin/tune2fs -c 0 ${FS_DEVICE}
205                                                 ok_msg
206                                         else
207                                                 fail_msg
208                                                 fatal_error "Failed to make ext4 filesystem on ${FS_DEVICE}"
209                                         fi
210                                 ;;
211                         ext3)
212                                         CMD="/usr/bin/mke2fs -F -T ext3 ${FS_DEVICE}"
213                                         if [ "${FS_LABEL}" != "" ]
214                                         then    
215                                                 CMD="/usr/bin/mke2fs -F -T ext3 -L ${FS_LABEL} ${FS_DEVICE}"
216                                         fi
217         
218                                         $CMD >> /tmp/stdout.log 2>> /tmp/stderr.log
219                                         if [ "$?" = "0" ]
220                                         then
221                                                 # try to tune it out of fsck's but dont care if it fails
222                                                 /usr/bin/tune2fs -c 0 ${FS_DEVICE}
223                                                 ok_msg
224                                         else
225                                                 fail_msg
226                                                 fatal_error "Failed to make ext3 filesystem on ${FS_DEVICE}"
227                                         fi
228                                 ;;
229                         ext2)
230                                         CMD="/usr/bin/mke2fs -F -T ext2 ${FS_DEVICE}"
231                                         if [ "${FS_LABEL}" != "" ]
232                                         then    
233                                                 CMD="/usr/bin/mke2fs -F -T ext2 -L ${FS_LABEL} ${FS_DEVICE}"
234                                         fi
235         
236                                         $CMD >> /tmp/stdout.log 2>> /tmp/stderr.log
237                                         if [ "$?" = "0" ]
238                                         then
239                                                 ok_msg
240                                         else
241                                                 fail_msg
242                                                 fatal_error "Failed to make ext2 filesystem on ${FS_DEVICE}"
243                                         fi
244                                 ;;
245                         xfs)
246                                         /usr/bin/mkfs.xfs -f ${FS_DEVICE} \
247                                                 >> /tmp/stdout.log 2>> /tmp/stderr.log
248                                         if [ "$?" = "0" ]
249                                         then
250                                                 ok_msg
251                                         else
252                                                 fail_msg
253                                                 fatal_error "Failed to make xfs filesystem on ${FS_DEVICE}"
254                                         fi
255                                 ;;
256         
257                         reiserfs)
258                                         /usr/bin/mkreiserfs -ff ${FS_DEVICE} \
259                                                 >> /tmp/stdout.log 2>> /tmp/stderr.log
260         
261                                         if [ "$?" = "0" ]
262                                         then
263                                                 ok_msg
264                                         else
265                                                 fail_msg
266                                                 fatal_error "Failed to make reiserfs filesystem on ${FS_DEVICE}"
267                                         fi
268                                 ;;
269                         swap)
270                                         /usr/bin/mkswap -f ${FS_DEVICE} \
271                                                 >> /tmp/stdout.log 2>> /tmp/stderr.log
272                                         if [ "$?" = "0" ]
273                                         then
274                                                 ok_msg
275                                         else
276                                                 fail_msg
277                                                 fatal_error "Failed to make swap filesystem on ${FS_DEVICE}"
278                                         fi
279         
280                                         msg -n "Activating swap device ${FS_DEVICE}"
281                                         swapon ${FS_DEVICE} >> /tmp/stdout.log 2>> /tmp/stderr.log
282                                         if [ "$?" = "0" ]
283                                         then
284                                                 ok_msg
285                                         else
286                                                 fail_msg
287                                                 fatal_error "Failed to activate swap filesystem on ${FS_DEVICE}"
288                                         fi
289                                 ;;
290                         *)
291                                 fatal_error "Don't know how to handle filesystem type ${FS_TYPE}"
292                                 ;;
293                 esac
294
295         fi
296         
297         # if it starts with a / it's going to be mounted
298         if [ `echo ${FS_MNTPOINT} | grep '^/'` ]
299         then
300                 MNT_POINT=`echo ${FS_MNTPOINT} | sed 's/^\///'`
301                 msg -n "Mounting ${ANSI_BLUE}${FS_DEVICE}${ANSI_DONE} on ${ANSI_BLUE}/cloner/mnt/${MNT_POINT}${ANSI_DONE}"
302
303                 mkdir -p /cloner/mnt/${MNT_POINT} >> /tmp/stdout.log 2>> /tmp/stderr.log
304
305                 mount -t ${FS_TYPE} ${FS_DEVICE} /cloner/mnt/${MNT_POINT} >> /tmp/stdout.log 2>> /tmp/stderr.log
306                 if [ "$?" = "0" ]
307                 then
308                         ok_msg
309                 else
310                         fail_msg
311                         fatal_error "Failed to mount ${FS_DEVICE} to /cloner/mnt/${MNT_POINT}"
312                 fi
313         fi
314
315 done < /cloner/setup/filesystems.sorted
316
317