#!/bin/sh # # Create bootstrap information files -- prelude to creating a # Bacula Rescue data for USB key # # Kern Sibbald, December MMII # This source distributed under the GPLv2 # export LANG=C cwd=`pwd` os=`uname -s` if [ ! "x$1" = "x" ] ; then host=$1 else host=`hostname` fi echo "Host specific data will be saved in the directory: ${host}" echo " " mkdir -p $host di=${cwd}/$host/diskinfo scr=${cwd}/$host/scripts case $os in Linux) ;; FreeBSD | SunOS | IRIX) echo "" echo "This code is not yet adapted to this OS" exit 1 ;; *) echo "" echo "Unknown operating system type: $os" exit 1 ;; esac if [ ! `whoami` = "root" ] ; then echo "" echo "You need to be root to run this script ..." echo "" exit 1 fi # # First collect information # rm -rf format.* partition.* $di create-* $scr echo "Begin collecting system info ..." mkdir -p $di cd $di mount -l >mount.bsi mount -l -t ext2 >mount.ext2.bsi mount -l -t ext3 >mount.ext3.bsi mount -l -t reiserfs >mount.rei.bsi cp /etc/fstab fstab.bsi cp /etc/mtab mtab.bsi df -Tl >df.bsi # Pickup all disks except USB_DEV sfdisk -s | grep -v "${USB_DEV}" >sfdisk.disks.bsi grep "^/dev/" sfdisk.disks.bsi | sed -n 's%\(^/dev/[A-Za-z]*\):.*$%\1%p' >disks.bsi for i in `cat disks.bsi`; do j=`echo $i | cut -c6-` sfdisk -l $i >sfdisk.$j.bsi sfdisk -d $i >sfdisk.make.$j.bsi done cat /proc/swaps > swaps.bsi route -n >route.bsi ifconfig >ifconfig.bsi # Gather LVM information # Only tested on LVM2 dolvm=0 if which lvm > /dev/null 2>/dev/null ; then echo "Gathering LVM information" dolvm=1 lvm pvs --noheadings --nosuffix -o pv_name,vg_name | cat > lvm-pv.bsi lvm vgs --noheadings --nosuffix -o vg_name,vg_extent_size --units=k | cat > lvm-vg.bsi lvm lvs --noheadings --nosuffix -o lv_name,vg_name,lv_size --units=k | cat > lvm-lv.bsi fi echo "Done collecting info." # # Done collecting information # echo "Begin creating scripts ..." # # First create partitioning script(s) # mkdir -p $scr for i in `cat disks.bsi`; do j=`echo $i | cut -c6-` cat >$scr/partition.$j < $scr/create-pv <> $scr/create-pv done cat >> $scr/create-pv <> $scr/create-pv echo "lvm pvcreate -ff -y -v $pv" >> $scr/create-pv done echo "echo \"Done.\"" >> $scr/create-pv echo "echo \"If there were no errors, run create-vg to create the volume groups.\"" >> $scr/create-pv chmod +x $scr/create-pv # # LVM phase 2: create Volume Groups # cat > $scr/create-vg <> $scr/create-vg pelist="" for pv in `cat lvm-pv.bsi | awk "\\\$2 == \"$vg\" { print \\\$1 }"` ; do pelist="$pelist $pv" done echo "lvm vgcreate $vg -v -s ${pesize}k $pelist" >> $scr/create-vg done echo "echo \"Done.\"" >> $scr/create-vg echo "echo \"If there were no errors, run create-lv to create the logical volumes.\"" >> $scr/create-vg chmod +x $scr/create-vg # # LVM phase 3: create Logical Volumes # # One quick sidenote: this script creates LVs with a size based on the # physical extent count, rather than the size in bytes. This is because # at the time of writing this script, lvdisplay erroneously prints out # the size in 512 byte sector count rather than 1024 byte blocks. # Using the extent count should allow this script to continue to work # even after the bug is fixed. cat > $scr/create-lv <> $scr/create-lv echo "lvm lvcreate -L ${lvsize}k $vg -n $lv" >> $scr/create-lv done chmod +x $scr/create-lv fi # # Create formatting script(s) # echo "Begin making formatting script(s) ..." for i in `cat disks.bsi`; do j=`echo $i | cut -c6-` cat >$scr/format.$j <>$scr/format.$j echo "mkswap $check $disk" >>$scr/format.$j echo "echo \"\"" >>$scr/format.$j done # Find ext2 partitions in mount output k=`grep "^$i" mount.ext2.bsi | cut -d ' ' -f 1` for disk in $k; do echo "echo \"Formating $disk -- ext2 partition\"" >>$scr/format.$j label=`grep "^$disk" mount.ext2.bsi | cut -d ' ' -f 7 | cut -c2- | cut -d ] -f 1` if [ x$label = x ] ; then echo "mke2fs -v \$check $disk" >>$scr/format.$j else echo "mke2fs -v \$check -L $label $disk" >>$scr/format.$j fi echo "echo \"\"" >>$scr/format.$j done # Find ext3 partitions in mount output k=`grep "^$i" mount.ext3.bsi | cut -d ' ' -f 1` for disk in $k; do echo "echo \"Formating $disk -- ext3 partition\"" >>$scr/format.$j label=`grep "^$disk" mount.ext3.bsi | cut -d ' ' -f 7 | cut -c2- | cut -d ] -f 1` if [ x$label = x ] ; then echo "mke2fs -v -j \$check $disk" >>$scr/format.$j else echo "mke2fs -v -j \$check -L $label $disk" >>$scr/format.$j fi echo "echo \"\"" >>$scr/format.$j done # Find reiserfs partitions in mount output k=`grep "^$i" mount.rei.bsi | cut -d ' ' -f 1` for disk in $k; do echo "echo \"Formating $disk -- reiser partition\"" >>$scr/format.$j label=`grep "^$disk" mount.rei.bsi | cut -d ' ' -f 7 | cut -c2- | cut -d ] -f 1` if [ x$label = x ] ; then echo "mkereiserfs -V \$check $disk" >>$scr/format.$j else echo "mkereiserfs -V \$check -L $label $disk" >>$scr/format.$j fi echo "echo \"\"" >>$scr/format.$j done chmod 755 $scr/format.$j done # # Create LVM formatting scripts(s) # # Somebody more clever than I with bash scripting # could probably factor a lot of this code out. # if [ $dolvm -eq 1 ] ; then echo "Begin making LVM formatting script(s) ..." for i in `cat lvm-vg.bsi | awk '{print $1}'`; do cat >$scr/format.$i <>$scr/format.$i echo "mkswap $check $disk" >>$scr/format.$i echo "echo \"\"" >>$scr/format.$i done # Find ext2 partitions in mount output k=`grep "^$mapper" mount.ext2.bsi | cut -d ' ' -f 1` for disk in $k; do echo "echo \"Formating $disk -- ext2 partition\"" >>$scr/format.$i label=`grep "^$disk on" mount.ext2.bsi | cut -d ' ' -f 7 | cut -c2- | cut -d ] -f 1` if [ x$label = x ] ; then echo "mke2fs -v \$check $disk" >>$scr/format.$i else echo "mke2fs -v \$check -L $label $disk" >>$scr/format.$i fi echo "echo \"\"" >>$scr/format.$i done # Find ext3 partitions in mount output k=`grep "^$mapper" mount.ext3.bsi | cut -d ' ' -f 1` for disk in $k; do echo "echo \"Formating $disk -- ext3 partition\"" >>$scr/format.$i label=`grep "^$disk on" mount.ext3.bsi | cut -d ' ' -f 7 | cut -c2- | cut -d ] -f 1` if [ x$label = x ] ; then echo "mke2fs -v -j \$check $disk" >>$scr/format.$i else echo "mke2fs -v -j \$check -L $label $disk" >>$scr/format.$i fi echo "echo \"\"" >>$scr/format.$i done # Find reiserfs partitions in mount output k=`grep "^$mapper" mount.rei.bsi | cut -d ' ' -f 1` for disk in $k; do echo "echo \"Formating $disk -- reiser partition\"" >>$scr/format.$i label=`grep "^$disk on" mount.rei.bsi | cut -d ' ' -f 7 | cut -c2- | cut -d ] -f 1` if [ x$label = x ] ; then echo "mkereiserfs -V \$check $disk" >>$scr/format.$i else echo "mkereiserfs -V \$check -L $label $disk" >>$scr/format.$i fi echo "echo \"\"" >>$scr/format.$i done chmod 755 $scr/format.$i done fi cd $scr # # Create network start script # ifconfig eth0 2>/dev/null >/dev/null if [ $? = 0 ] ; then ip=`ifconfig eth0 | grep inet | head -1 | sed -n 's/\ \+inet addr:\([0-9]\+\(\.[0-9]\+\)\{3\}\).*/\1/p'` cat >start_network <start_network fi # Try eth1 ifconfig eth1 2>/dev/null >/dev/null if [ $? = 0 ] ; then ip=`ifconfig eth1 | grep inet | head -1 | sed -n 's/\ \+inet addr:\([0-9]\+\(\.[0-9]\+\)\{3\}\).*/\1/p'` cat >>start_network <>start_network <$scr/mount_drives <${TMP1} sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/\1 \/mnt\/disk\2/p' $di/mount.ext3.bsi >>${TMP1} sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/\1 \/mnt\/disk\2/p' $di/mount.rei.bsi >>${TMP1} # sort so that / is first sort -k 2 <${TMP1} >${TMP2} # output mkdir followed by its mount sed -n 's/\(^.*\)\ \(.*$\)/mkdir -p \2\ mount \1 \2/p' ${TMP2} >>$scr/mount_drives # Now build unmount_drives cat >$scr/umount_drives <${TMP2} sed -n 's/\(^.*\)\ \(.*$\)/umount \2/p' ${TMP2} >>$scr/umount_drives echo "umount /mnt/cdrom 2>/dev/null >/dev/null" >>$scr/umount_drives echo "sync" >>$scr/umount_drives echo "sync" >>$scr/umount_drives rm -f ${TMP1} ${TMP2} chmod 755 $scr/mount_drives $scr/umount_drives echo "Done building scripts."