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