3 # Create bootstrap information files -- prelude to creating a
4 # Bacula Rescue data for USB key
6 # Kern Sibbald, December MMII
7 # This source distributed under the GPL
12 if [ ! "x$1" = "x" ] ; then
17 echo "Host specific data will be saved in the directory: ${host}"
20 di=${cwd}/$host/diskinfo
21 scr=${cwd}/$host/scripts
26 FreeBSD | SunOS | IRIX)
28 echo "This code is not yet adapted to this OS"
33 echo "Unknown operating system type: $os"
37 if [ ! `whoami` = "root" ] ; then
39 echo "You need to be root to run this script ..."
45 # First collect information
47 rm -rf format.* partition.* $di create-* $scr
48 echo "Begin collecting system info ..."
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
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
66 cat /proc/swaps > swaps.bsi
68 ifconfig >ifconfig.bsi
70 # Gather LVM information
73 if which lvm > /dev/null 2>/dev/null ; then
74 echo "Gathering LVM information"
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
82 echo "Done collecting info."
85 # Done collecting information
89 echo "Begin creating scripts ..."
91 # First create partitioning script(s)
94 for i in `cat disks.bsi`; do
95 j=`echo $i | cut -c6-`
96 cat >$scr/partition.$j <<END_OF_DATA
100 # Partition disk $i -- created by getdiskinfo
102 echo "This script will repartition disk $i."
104 echo "IT WILL DESTROY ALL DATA ON DISK $i !!!!"
106 echo -n "Are you sure you want to continue? yes/no: "
108 if [ x\$a != xyes ] ; then
111 echo "Partitioning disk $i"
113 dd if=/dev/zero of=$i bs=512 count=2
115 sfdisk $i <$di/sfdisk.make.$j.bsi | less
117 echo "The previous partitioning was:"
118 cat $di/sfdisk.$j.bsi
121 echo "The new partitioning is:"
124 echo "If the disk is correctly partitioned, you should"
125 echo "now run the \"format.$j\" script."
130 chmod 755 $scr/partition.$j
132 echo "Done making partitioning scripts"
134 if [ $dolvm -eq 1 ] ; then
136 # LVM phase 1: create the Physical Volumess
138 cat > $scr/create-pv <<EOF
141 # Create PVs for LVM -- created by getdiskinfo
143 echo "This script will format all partitions listed in diskinfo/lvm-pv.bsi"
145 echo "IT WILL DESTROY ALL DATA ON THESE PARTITIONS!!!!"
148 for pv in `cat lvm-pv.bsi | awk '{print $1}'`; do
149 echo "echo \" $pv\"" >> $scr/create-pv
152 cat >> $scr/create-pv <<EOF
154 echo -n "Are you sure you want to continue? yes/no: "
156 if [ x\$a != xyes ] ; then
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
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
169 # LVM phase 2: create Volume Groups
171 cat > $scr/create-vg <<EOF
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"
177 echo -n "Are you sure you want to continue? yes/no: "
179 if [ x\$a != xyes ] ; then
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
186 for pv in `cat lvm-pv.bsi | awk "\\\$2 == \"$vg\" { print \\\$1 }"` ; do
189 echo "lvm vgcreate $vg -v -s ${pesize}k $pelist" >> $scr/create-vg
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
197 # LVM phase 3: create Logical Volumes
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.
206 cat > $scr/create-lv <<EOF
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"
212 echo -n "Are you sure you want to continue? yes/no: "
214 if [ x\$a != xyes ] ; then
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
223 chmod +x $scr/create-lv
227 # Create formatting script(s)
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
235 # Format all partitions on disk $i -- created by getdiskinfo
238 echo "This script will format all partitions on disk $i."
240 echo "IT WILL DESTROY ALL DATA ON DISK $i !!!!"
242 echo -n "Are you sure you want to continue? yes/no: "
244 if [ x\$a != xyes ] ; then
247 echo "Do you want to do a disk check for bad blocks?"
248 echo -n "It is recommended, but takes time. yes/no: "
250 if [ x\$a = xyes ] ; then
257 # Find swap partitions in output from sfdisk
258 k=`grep "^$i.*82 Linux swap" sfdisk.$j.bsi | cut -d ' ' -f 1`
260 echo "echo \"Formatting $disk -- swap partition\"" >>$scr/format.$j
261 echo "mkswap $check $disk" >>$scr/format.$j
262 echo "echo \"\"" >>$scr/format.$j
264 # Find ext2 partitions in mount output
265 k=`grep "^$i" mount.ext2.bsi | cut -d ' ' -f 1`
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
272 echo "mke2fs -v \$check -L $label $disk" >>$scr/format.$j
274 echo "echo \"\"" >>$scr/format.$j
276 # Find ext3 partitions in mount output
277 k=`grep "^$i" mount.ext3.bsi | cut -d ' ' -f 1`
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
284 echo "mke2fs -v -j \$check -L $label $disk" >>$scr/format.$j
286 echo "echo \"\"" >>$scr/format.$j
288 # Find reiserfs partitions in mount output
289 k=`grep "^$i" mount.rei.bsi | cut -d ' ' -f 1`
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
296 echo "mkereiserfs -V \$check -L $label $disk" >>$scr/format.$j
298 echo "echo \"\"" >>$scr/format.$j
300 chmod 755 $scr/format.$j
304 # Create LVM formatting scripts(s)
306 # Somebody more clever than I with bash scripting
307 # could probably factor a lot of this code out.
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
315 # Format all partitions on VG $i -- created by getdiskinfo
318 echo "This script will format all LVs on VG $i."
320 echo "IT WILL DESTROY ALL DATA ON DISK $i !!!!"
322 echo -n "Are you sure you want to continue? yes/no: "
324 if [ x\$a != xyes ] ; then
327 echo "Do you want to do a disk check for bad blocks?"
328 echo -n "It is recommended, but takes time. yes/no: "
330 if [ x\$a = xyes ] ; then
337 # Find swap partitions in output from sfdisk
338 mapper="/dev/mapper/${i}"
339 k=`grep "^$mapper" swaps.bsi | awk '{print $1}'`
341 echo "echo \"Formatting $disk -- swap partition\"" >>$scr/format.$i
342 echo "mkswap $check $disk" >>$scr/format.$i
343 echo "echo \"\"" >>$scr/format.$i
345 # Find ext2 partitions in mount output
346 k=`grep "^$mapper" mount.ext2.bsi | cut -d ' ' -f 1`
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
353 echo "mke2fs -v \$check -L $label $disk" >>$scr/format.$i
355 echo "echo \"\"" >>$scr/format.$i
357 # Find ext3 partitions in mount output
358 k=`grep "^$mapper" mount.ext3.bsi | cut -d ' ' -f 1`
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
365 echo "mke2fs -v -j \$check -L $label $disk" >>$scr/format.$i
367 echo "echo \"\"" >>$scr/format.$i
369 # Find reiserfs partitions in mount output
370 k=`grep "^$mapper" mount.rei.bsi | cut -d ' ' -f 1`
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
377 echo "mkereiserfs -V \$check -L $label $disk" >>$scr/format.$i
379 echo "echo \"\"" >>$scr/format.$i
381 chmod 755 $scr/format.$i
388 # Create network start script
390 ifconfig eth0 2>/dev/null >/dev/null
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
396 # Start network -- created by getdiskinfo
401 ifconfig \$dev up \$ip
404 echo "Warning, no eth0 found."
405 echo "#!/bin/sh" >start_network
408 ifconfig eth1 2>/dev/null >/dev/null
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
414 ifconfig \$dev1 up \$ip1
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
423 chmod 755 start_network
425 cat >$scr/mount_drives <<END_OF_DATA
428 # Mount disk drives -- created by getdiskinfo
430 PATH=$PATH:/mnt/disk/sbin:/mnt/disk/bin
432 TMP1=`mktemp gdi.1.XXXXXXXXXX`
433 TMP2=`mktemp gdi.2.XXXXXXXXXX`
434 if test x${TMP1} = x; then
436 if test -f ${TMP1}; then
437 echo "Temp file security problem on: ${TMP1}"
441 if test x${TMP2} = x; then
443 if test -f ${TMP2}; then
444 echo "Temp file security problem on: ${TMP2}"
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
458 # Now build unmount_drives
459 cat >$scr/umount_drives <<END_OF_DATA
462 # Unmount disk drives -- created by getdiskinfo
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
471 rm -f ${TMP1} ${TMP2}
473 chmod 755 $scr/mount_drives $scr/umount_drives
474 echo "Done building scripts."