]> git.sur5r.net Git - bacula/rescue/blob - rescue/linux/usb/getdiskinfo
Update
[bacula/rescue] / rescue / linux / usb / getdiskinfo
1 #!/bin/sh
2 #
3 # Create bootstrap information files -- prelude to creating a
4 #   Bacula Rescue Disk
5 #
6 #   Kern Sibbald, December MMII
7 #      This source distributed under the GPL
8 #
9 export LANG=C
10 cwd=`pwd`
11 host=`uname -s`
12 di=${cwd}/diskinfo
13 scr=${cwd}/scripts
14
15 case $host in
16  Linux)
17   ;;
18  FreeBSD | SunOS | IRIX)
19    echo ""
20    echo "This code is not yet adapted to this OS"     
21    exit 1
22    ;;
23  *)
24    echo ""
25    echo "Unknown operating system type: $host"     
26    exit 1
27    ;;
28 esac
29 if [ ! `whoami` = "root" ] ; then
30   echo ""
31   echo "You need to be root to run this script ..."
32   echo ""
33   exit 1
34 fi
35
36 #
37 #  First collect information
38 #
39 rm -rf format.* partition.* $di create-* $scr
40 echo "Begin collecting system info ..."
41 mkdir -p $di
42 cd $di
43 mount -l >mount.bsi
44 mount -l -t ext2 >mount.ext2.bsi
45 mount -l -t ext3 >mount.ext3.bsi
46 mount -l -t reiserfs >mount.rei.bsi
47 cp /etc/fstab fstab.bsi
48 cp /etc/mtab  mtab.bsi     
49 df -Tl >df.bsi
50 sfdisk -s >sfdisk.disks.bsi
51 grep "^/dev/" sfdisk.disks.bsi | sed -n 's%\(^/dev/[A-Za-z]*\):.*$%\1%p' >disks.bsi
52 for i in `cat disks.bsi`; do
53    j=`echo $i | cut -c6-`
54    sfdisk -l $i >sfdisk.$j.bsi 
55    sfdisk -d $i >sfdisk.make.$j.bsi
56 done
57 cat /proc/swaps > swaps.bsi
58 route -n >route.bsi
59 ifconfig >ifconfig.bsi
60
61 # Gather LVM information
62 # Only tested on LVM2
63 dolvm=0
64 if  which lvm > /dev/null 2>/dev/null ; then
65     echo "Gathering LVM information"
66     dolvm=1
67
68     lvm pvs --noheadings --nosuffix -o pv_name,vg_name | cat > lvm-pv.bsi
69     lvm vgs --noheadings --nosuffix -o vg_name,vg_extent_size --units=k | cat > lvm-vg.bsi
70     lvm lvs --noheadings --nosuffix -o lv_name,vg_name,lv_size --units=k | cat > lvm-lv.bsi
71 fi
72
73 echo "Done collecting info."
74
75 #
76 # Done collecting information
77 #
78
79
80 echo "Begin creating scripts ..."
81 #
82 # First create partitioning script(s)
83 #
84 mkdir -p $scr
85 for i in `cat disks.bsi`; do
86   j=`echo $i | cut -c6-`
87   cat >$scr/partition.$j <<END_OF_DATA
88
89 #!/bin/sh
90 #
91 #  Partition disk $i  -- created by getdiskinfo
92 echo ""
93 echo "This script will repartition disk $i."
94 echo ""
95 echo "IT WILL DESTROY ALL DATA ON DISK $i !!!!"
96 echo ""
97 echo -n "Are you sure you want to continue? yes/no: "
98 read a
99 if [ x\$a != xyes ] ; then
100    exit 1
101 fi 
102 echo "Partitioning disk $i"
103 # zap partition info
104 dd if=/dev/zero of=$i bs=512 count=2
105 # repartition
106 sfdisk $i <$di/sfdisk.make.$j.bsi | less
107 echo ""
108 echo "The previous partitioning was:"
109 cat $di/sfdisk.$j.bsi
110 #
111 echo ""
112 echo "The new partitioning is:"
113 sfdisk -l $i
114 echo ""
115 echo "If the disk is correctly partitioned, you should"
116 echo "now run the \"format.$j\" script."
117 echo ""
118 END_OF_DATA
119
120
121 chmod 755 $scr/partition.$j
122 done
123 echo "Done making partitioning scripts"
124
125 if [ $dolvm -eq 1 ] ; then
126
127 # LVM phase 1: create the Physical Volumess
128 #
129     cat > $scr/create-pv <<EOF
130 #!/bin/sh
131 #
132 # Create PVs for LVM -- created by getdiskinfo
133 echo ""
134 echo "This script will format all partitions listed in diskinfo/lvm-pv.bsi"
135 echo ""
136 echo "IT WILL DESTROY ALL DATA ON THESE PARTITIONS!!!!"
137 echo ""
138 EOF
139     for pv in `cat lvm-pv.bsi | awk '{print $1}'`; do
140         echo "echo \"    $pv\"" >> $scr/create-pv
141     done
142
143     cat >> $scr/create-pv <<EOF
144 echo ""
145 echo -n "Are you sure you want to continue? yes/no: "
146 read a
147 if [ x\$a != xyes ] ; then
148    exit 1
149 fi 
150 EOF
151     for pv in `cat lvm-pv.bsi | awk '{print $1}'`; do
152         echo "echo \"Creating PV on $pv\"" >> $scr/create-pv
153         echo "lvm pvcreate -ff -y -v $pv" >> $scr/create-pv
154     done
155     echo "echo \"Done.\"" >> $scr/create-pv
156     echo "echo \"If there were no errors, run create-vg to create the volume groups.\"" >> $scr/create-pv
157     chmod +x $scr/create-pv
158
159 #
160 # LVM phase 2: create Volume Groups
161 #
162     cat > $scr/create-vg <<EOF
163 #!/bin/sh
164 echo ""
165 echo "This script will create all volume groups listed in diskinfo/lvm-vg.bsi"
166 echo "You must have sucesfully run create-pv to create the requesite PVs first"
167 echo ""
168 echo -n "Are you sure you want to continue? yes/no: "
169 read a
170 if [ x\$a != xyes ] ; then
171    exit 1
172 fi
173 EOF
174     cat lvm-vg.bsi | awk '{print $1, $2}' | while read vg pesize ; do
175         echo "echo \"Creating VG $vg PE extent size $pesize kbytes"\" >> $scr/create-vg
176         pelist=""
177         for pv in `cat lvm-pv.bsi | awk "\\\$2 == \"$vg\" { print \\\$1 }"` ; do
178             pelist="$pelist $pv"
179         done
180         echo "lvm vgcreate $vg -v -s ${pesize}k $pelist" >> $scr/create-vg
181     done
182     echo "echo \"Done.\"" >> $scr/create-vg
183     echo "echo \"If there were no errors, run create-lv to create the logical volumes.\"" >> $scr/create-vg
184     chmod +x $scr/create-vg
185
186
187 #
188 # LVM phase 3: create Logical Volumes 
189 #
190 # One quick sidenote: this script creates LVs with a size based on the
191 # physical extent count, rather than the size in bytes.  This is because
192 # at the time of writing this script, lvdisplay erroneously prints out
193 # the size in 512 byte sector count rather than 1024 byte blocks.
194 # Using the extent count should allow this script to continue to work
195 # even after the bug is fixed.
196
197     cat > $scr/create-lv <<EOF
198 #!/bin/sh
199 echo ""
200 echo "This script will create all logical volumes listed in diskinfo/lvm-lv.bsi"
201 echo "You must have sucesfully run create-vg to create the requesite VGs first"
202 echo ""
203 echo -n "Are you sure you want to continue? yes/no: "
204 read a
205 if [ x\$a != xyes ] ; then
206    exit 1
207 fi
208 EOF
209     cat lvm-lv.bsi | awk '{print $1, $2, $3}' | while read lv vg lvsize ; do
210         # lv=`echo $lv | sed -e 's/.*\///'`
211         echo "echo \"Creating LV $lv\"" >> $scr/create-lv
212         echo "lvm lvcreate -L ${lvsize}k $vg -n $lv" >> $scr/create-lv
213     done
214     chmod +x $scr/create-lv
215 fi
216
217 #
218 # Create formatting script(s)
219 #
220 echo "Begin making formatting script(s) ..."
221 for i in `cat disks.bsi`; do
222   j=`echo $i | cut -c6-`
223   cat >$scr/format.$j <<END_OF_DATA
224 #!/bin/sh
225 #
226 #  Format all partitions on disk $i -- created by getdiskinfo
227 #
228 echo ""
229 echo "This script will format all partitions on disk $i."
230 echo ""
231 echo "IT WILL DESTROY ALL DATA ON DISK $i !!!!"
232 echo ""
233 echo -n "Are you sure you want to continue? yes/no: "
234 read a
235 if [ x\$a != xyes ] ; then
236    exit 1
237 fi 
238 echo "Do you want to do a disk check for bad blocks?"
239 echo -n "It is recommended, but takes time. yes/no: "
240 read a
241 if [ x\$a = xyes ] ; then
242    check="-c"
243 else
244    check=
245 fi
246 END_OF_DATA
247
248    # Find swap partitions in output from sfdisk
249    k=`grep "^$i.*82  Linux swap" sfdisk.$j.bsi | cut -d ' ' -f 1`
250    for disk in $k; do
251       echo "echo \"Formatting $disk -- swap partition\"" >>$scr/format.$j
252       echo "mkswap $check $disk" >>$scr/format.$j
253       echo "echo \"\"" >>$scr/format.$j
254    done
255    # Find ext2 partitions in mount output
256    k=`grep "^$i" mount.ext2.bsi | cut -d ' ' -f 1`
257    for disk in $k; do
258       echo "echo \"Formating $disk -- ext2 partition\"" >>$scr/format.$j
259       label=`grep "^$disk" mount.ext2.bsi | cut -d ' ' -f 7 | cut -c2- | cut -d ] -f 1`
260       if [ x$label = x ] ; then
261          echo "mke2fs -v \$check $disk" >>$scr/format.$j
262       else
263          echo "mke2fs -v \$check -L $label $disk" >>$scr/format.$j
264       fi
265       echo "echo \"\"" >>$scr/format.$j
266    done
267    # Find ext3 partitions in mount output
268    k=`grep "^$i" mount.ext3.bsi | cut -d ' ' -f 1`
269    for disk in $k; do
270       echo "echo \"Formating $disk -- ext3 partition\"" >>$scr/format.$j
271       label=`grep "^$disk" mount.ext3.bsi | cut -d ' ' -f 7 | cut -c2- | cut -d ] -f 1`
272       if [ x$label = x ] ; then
273          echo "mke2fs -v -j \$check $disk" >>$scr/format.$j
274       else
275          echo "mke2fs -v -j \$check -L $label $disk" >>$scr/format.$j
276       fi
277       echo "echo \"\"" >>$scr/format.$j
278    done
279    # Find reiserfs partitions in mount output
280    k=`grep "^$i" mount.rei.bsi | cut -d ' ' -f 1`
281    for disk in $k; do
282       echo "echo \"Formating $disk -- reiser partition\"" >>$scr/format.$j
283       label=`grep "^$disk" mount.rei.bsi | cut -d ' ' -f 7 | cut -c2- | cut -d ] -f 1`
284       if [ x$label = x ] ; then
285          echo "mkereiserfs -V \$check $disk" >>$scr/format.$j
286       else
287          echo "mkereiserfs -V \$check -L $label $disk" >>$scr/format.$j
288       fi
289       echo "echo \"\"" >>$scr/format.$j
290    done
291    chmod 755 $scr/format.$j
292 done
293
294 #
295 # Create LVM formatting scripts(s)
296 #
297 # Somebody more clever than I with bash scripting
298 # could probably factor a lot of this code out.
299 #
300 if [ $dolvm -eq 1 ] ; then
301     echo "Begin making LVM formatting script(s) ..."
302     for i in `cat lvm-vg.bsi | awk '{print $1}'`; do
303         cat >$scr/format.$i <<END_OF_DATA
304 #!/bin/sh
305 #
306 #  Format all partitions on VG $i -- created by getdiskinfo
307 #
308 echo ""
309 echo "This script will format all LVs on VG $i."
310 echo ""
311 echo "IT WILL DESTROY ALL DATA ON DISK $i !!!!"
312 echo ""
313 echo -n "Are you sure you want to continue? yes/no: "
314 read a
315 if [ x\$a != xyes ] ; then
316    exit 1
317 fi
318 echo "Do you want to do a disk check for bad blocks?"
319 echo -n "It is recommended, but takes time. yes/no: "
320 read a
321 if [ x\$a = xyes ] ; then
322    check="-c"
323 else
324    check=
325 fi
326 END_OF_DATA
327 #   set -x
328    # Find swap partitions in output from sfdisk
329    mapper="/dev/mapper/${i}"
330    k=`grep "^$mapper" swaps.bsi | awk '{print $1}'`
331    for disk in $k; do
332       echo "echo \"Formatting $disk -- swap partition\"" >>$scr/format.$i
333       echo "mkswap $check $disk" >>$scr/format.$i
334       echo "echo \"\"" >>$scr/format.$i
335    done
336    # Find ext2 partitions in mount output
337    k=`grep "^$mapper" mount.ext2.bsi | cut -d ' ' -f 1`
338    for disk in $k; do
339       echo "echo \"Formating $disk -- ext2 partition\"" >>$scr/format.$i
340       label=`grep "^$disk on" mount.ext2.bsi | cut -d ' ' -f 7 | cut -c2- | cut -d ] -f 1`
341       if [ x$label = x ] ; then
342          echo "mke2fs -v \$check $disk" >>$scr/format.$i
343       else
344          echo "mke2fs -v \$check -L $label $disk" >>$scr/format.$i
345       fi
346       echo "echo \"\"" >>$scr/format.$i
347    done
348    # Find ext3 partitions in mount output
349    k=`grep "^$mapper" mount.ext3.bsi | cut -d ' ' -f 1`
350    for disk in $k; do
351       echo "echo \"Formating $disk -- ext3 partition\"" >>$scr/format.$i
352       label=`grep "^$disk on" mount.ext3.bsi | cut -d ' ' -f 7 | cut -c2- | cut -d ] -f 1`
353       if [ x$label = x ] ; then
354          echo "mke2fs -v -j \$check $disk" >>$scr/format.$i
355       else
356          echo "mke2fs -v -j \$check -L $label $disk" >>$scr/format.$i
357       fi
358       echo "echo \"\"" >>$scr/format.$i
359    done
360    # Find reiserfs partitions in mount output
361    k=`grep "^$mapper" mount.rei.bsi | cut -d ' ' -f 1`
362    for disk in $k; do
363       echo "echo \"Formating $disk -- reiser partition\"" >>$scr/format.$i
364       label=`grep "^$disk on" mount.rei.bsi | cut -d ' ' -f 7 | cut -c2- | cut -d ] -f 1`
365       if [ x$label = x ] ; then
366          echo "mkereiserfs -V \$check $disk" >>$scr/format.$i
367       else
368          echo "mkereiserfs -V \$check -L $label $disk" >>$scr/format.$i
369       fi
370       echo "echo \"\"" >>$scr/format.$i
371    done
372    chmod 755 $scr/format.$i
373    done
374 fi
375
376 cd $scr
377
378 #
379 # Create network start script
380 #
381 ifconfig eth0 2>/dev/null >/dev/null
382 if [ $? = 0 ] ; then
383   ip=`ifconfig eth0 | grep inet | head -1 | sed -n 's/\ \+inet addr:\([0-9]\+\(\.[0-9]\+\)\{3\}\).*/\1/p'`
384   cat >start_network <<END_OF_DATA
385 #!/bin/sh
386 #
387 #  Start network -- created by getdiskinfo
388 #
389 ip=$ip
390 dev=eth0
391 ifconfig lo up
392 ifconfig \$dev up \$ip
393 END_OF_DATA
394 else
395    echo "Warning, no eth0 found."
396    echo "#!/bin/sh" >start_network
397 fi
398 # Try eth1
399 ifconfig eth1 2>/dev/null >/dev/null
400 if [ $? = 0 ] ; then
401   ip=`ifconfig eth1 | grep inet | head -1 | sed -n 's/\ \+inet addr:\([0-9]\+\(\.[0-9]\+\)\{3\}\).*/\1/p'`
402   cat >>start_network <<END_OF_DATA
403 ip1=$ip
404 dev1=eth1
405 ifconfig \$dev1 up \$ip1
406 END_OF_DATA
407 fi
408 ip=`route -n | grep UG | head -1 | awk '{print $2}'`
409 dev=`route -n | grep UG | head -1 | awk '{print $8}'`
410 cat >>start_network <<END_OF_DATA
411 route add default gw $ip dev $dev
412 END_OF_DATA
413
414 chmod 755 start_network
415
416 cat >$scr/mount_drives <<END_OF_DATA
417 #!/bin/sh
418 #
419 #  Mount disk drives  -- created by getdiskinfo
420 #
421 PATH=$PATH:/mnt/disk/sbin:/mnt/disk/bin
422 END_OF_DATA
423 TMP1=`mktemp gdi.1.XXXXXXXXXX`
424 TMP2=`mktemp gdi.2.XXXXXXXXXX`
425 if test x${TMP1} = x; then
426    TMP1="/tmp/1$$"
427    if test -f ${TMP1}; then
428       echo "Temp file security problem on: ${TMP1}"
429       exit 1
430    fi
431 fi
432 if test x${TMP2} = x; then
433    TMP2="/tmp/2$$"
434    if test -f ${TMP2}; then
435       echo "Temp file security problem on: ${TMP2}"
436       rm -f ${TMP1}
437       exit 1
438    fi
439 fi
440 sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/\1 \/mnt\/disk\2/p' $di/mount.ext2.bsi >${TMP1}
441 sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/\1 \/mnt\/disk\2/p' $di/mount.ext3.bsi >>${TMP1}
442 sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/\1 \/mnt\/disk\2/p' $di/mount.rei.bsi >>${TMP1}
443 # sort so that / is first
444 sort -k 2 <${TMP1} >${TMP2}
445 # output mkdir followed by its mount
446 sed -n 's/\(^.*\)\ \(.*$\)/mkdir -p \2\
447 mount \1 \2/p' ${TMP2} >>$scr/mount_drives
448
449 # Now build unmount_drives
450 cat >$scr/umount_drives <<END_OF_DATA
451 #!/bin/sh
452 #
453 #  Unmount disk drives  -- created by getdiskinfo
454 #
455 END_OF_DATA
456 sort -r -k 2 <${TMP1} >${TMP2}
457 sed -n 's/\(^.*\)\ \(.*$\)/umount \2/p' ${TMP2} >>$scr/umount_drives
458 echo "umount /mnt/cdrom 2>/dev/null >/dev/null" >>$scr/umount_drives
459 echo "sync" >>$scr/umount_drives
460 echo "sync" >>$scr/umount_drives
461
462 rm -f ${TMP1} ${TMP2}
463
464 chmod 755 $scr/mount_drives $scr/umount_drives
465 echo "Done building scripts."