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