]> git.sur5r.net Git - bacula/rescue/blob - rescue/freebsd/getdiskinfo
Tweak version
[bacula/rescue] / rescue / freebsd / 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 di=diskinfo
10 cwd=`pwd`
11 host=`uname -s`
12 case $host in
13  Linux)
14   ;;
15  FreeBSD | SunOS | IRIX)
16    echo ""
17    echo "This code is not yet adapted to this OS"     
18    exit 1
19    ;;
20  *)
21    echo ""
22    echo "Unknown operating system type: $host"     
23    exit 1
24    ;;
25 esac
26 if [ ! `whoami` = "root" ] ; then
27   echo ""
28   echo "You need to be root to run this, otherwise"
29   echo "sfdisk produces no output. Continuing anyway ..."
30   echo ""
31 fi
32
33 #
34 #  First collect information
35 #
36 echo "Begin collecting system info"
37 mkdir -p $di
38 cd $di
39 mount -l >mount.bsi
40 mount -l -t ext2 >mount.ext2.bsi
41 mount -l -t ext3 >mount.ext3.bsi
42 cp /etc/fstab fstab.bsi
43 cp /etc/mtab  mtab.bsi     
44 df -Tl >df.bsi
45 sfdisk -s >sfdisk.disks.bsi
46 grep "^/dev/" sfdisk.disks.bsi | sed -n 's/\(^.*\):.*$/\1/p' >disks.bsi
47 for i in `cat disks.bsi`; do
48    j=`echo $i | cut -c6-`
49    sfdisk -l $i >sfdisk.$j.bsi 
50    sfdisk -d $i >sfdisk.make.$j.bsi
51 done
52 route -n >route.bsi
53 ifconfig >ifconfig.bsi
54 echo "Done collecting info. Building scripts ..."
55
56 #
57 # Done collecting information
58 #
59
60
61 #
62 # First create partitioning script(s)
63 #
64 for i in `cat disks.bsi`; do
65   j=`echo $i | cut -c6-`
66   cat >$cwd/partition.$j <<END_OF_DATA
67 #!/bin/sh
68 #
69 #  Partition disk $i  -- created by getdiskinfo
70 echo ""
71 echo "This script will repartition disk $i."
72 echo ""
73 echo "IT WILL DESTROY ALL DATA ON DISK $i !!!!"
74 echo ""
75 echo -n "Are you sure you want to continue? (yes/no): "
76 read a
77 if [ x\$a != xyes ] ; then
78    exit 1
79 fi 
80 echo "Partitioning disk $i"
81 # zap partition info
82 dd if=/dev/zero of=$i bs=512 count=2
83 # repartition
84 sfdisk $i <$di/sfdisk.make.$j.bsi | less
85 echo ""
86 echo "The previous partitioning was:"
87 cat $di/sfdisk.$j.bsi
88 #
89 echo ""
90 echo "The new partitioning is:"
91 sfdisk -l $i
92 echo ""
93 echo "If the disk is correctly partitioned, you should"
94 echo "now run the \"format.$j\" script."
95 echo ""
96 END_OF_DATA
97
98   chmod 755 $cwd/partition.$j
99 done
100
101 #
102 # Create formatting script(s)
103 #
104 for i in `cat disks.bsi`; do
105   j=`echo $i | cut -c6-`
106   cat >$cwd/format.$j <<END_OF_DATA
107 #!/bin/sh
108 #
109 #  Format all partitions on disk $i -- created by getdiskinfo
110 #
111 echo ""
112 echo "This script will format all partitions on disk $i."
113 echo ""
114 echo "IT WILL DESTROY ALL DATA ON DISK $i !!!!"
115 echo ""
116 echo -n "Are you sure you want to continue? (yes/no): "
117 read a
118 if [ x\$a != xyes ] ; then
119    exit 1
120 fi 
121 echo "Do you want to do a disk check for bad blocks?"
122 echo -n "It is recommended, but takes time. (yes/no): "
123 read a
124 if [ x\$a = xyes ] ; then
125    check="-c"
126 else
127    check=
128 fi
129 END_OF_DATA
130
131    # Find swap partitions in output from sfdisk
132    k=`grep "^$i.*82  Linux swap" sfdisk.$j.bsi | cut -d ' ' -f 1`
133    for disk in $k; do
134       echo "echo \"Formatting $disk -- swap partition\"" >>$cwd/format.$j
135       echo "mkswap $check $disk" >>$cwd/format.$j
136       echo "echo \"\"" >>$cwd/format.$j
137    done
138    # Find ext2 partitions in mount output
139    k=`grep "^$i" mount.ext2.bsi | cut -d ' ' -f 1`
140    for disk in $k; do
141       echo "echo \"Formating $disk -- ext2 partition\"" >>$cwd/format.$j
142       label=`grep "^$disk" mount.ext2.bsi | cut -d ' ' -f 7 | cut -c2- | cut -d ] -f 1`
143       if [ x$label = x ] ; then
144          echo "mke2fs -v \$check $disk" >>$cwd/format.$j
145       else
146          echo "mke2fs -v \$check -L $label $disk" >>$cwd/format.$j
147       fi
148       echo "echo \"\"" >>$cwd/format.$j
149    done
150    # Find ext3 partitions in mount output
151    k=`grep "^$i" mount.ext3.bsi | cut -d ' ' -f 1`
152    for disk in $k; do
153       echo "echo \"Formating $disk -- ext3 partition\"" >>$cwd/format.$j
154       label=`grep "^$disk" mount.ext3.bsi | cut -d ' ' -f 7 | cut -c2- | cut -d ] -f 1`
155       if [ x$label = x ] ; then
156          echo "mke2fs -v -j \$check $disk" >>$cwd/format.$j
157       else
158          echo "mke2fs -v -j \$check -L $label $disk" >>$cwd/format.$j
159       fi
160       echo "echo \"\"" >>$cwd/format.$j
161    done
162    chmod 755 $cwd/format.$j
163 done
164
165 cd $cwd
166
167 #
168 # Create network start script
169 #
170 host=`hostname`
171 ip=`host $host | cut -d ' ' -f 4`
172 cat >start_network <<END_OF_DATA
173 #!/bin/sh
174 #
175 #  Start network -- created by getdiskinfo
176 #
177 ip=$ip
178 dev=eth0
179 ifconfig lo up
180 ifconfig \$dev up \$ip
181 route add default gw \$ip dev \$dev
182 END_OF_DATA
183
184 chmod 755 start_network
185
186 cat >mount_drives <<END_OF_DATA
187 #!/bin/sh
188 #
189 #  Mount disk drives  -- created by getdiskinfo
190 #
191 END_OF_DATA
192 sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/mkdir -p \/mnt\/disk\2/p' $di/mount.ext2.bsi >>mount_drives
193 sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/mkdir -p \/mnt\/disk\2/p' $di/mount.ext3.bsi >>mount_drives
194 echo "#" >>mount_drives
195 sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/mount \1 \/mnt\/disk\2/p' $di/mount.ext2.bsi >/tmp/1$$     
196 sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/mount \1 \/mnt\/disk\2/p' $di/mount.ext3.bsi >>/tmp/1$$
197 # sort so that root is mounted first
198 sort -k 3 </tmp/1$$ >>mount_drives
199 rm -f /tmp/1$$
200
201 chmod 755 mount_drives
202
203 # copy sfdisk so we will have it
204 cp -f /sbin/sfdisk .