]> git.sur5r.net Git - bacula/rescue/blob - rescue/linux/floppy/getdiskinfo
Initial revision
[bacula/rescue] / rescue / linux / floppy / 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 
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 cp /etc/fstab fstab.bsi
46 cp /etc/mtab  mtab.bsi     
47 df -Tl >df.bsi
48 sfdisk -s >sfdisk.disks.bsi
49 grep "^/dev/" sfdisk.disks.bsi | sed -n 's%\(^/dev/[A-Za-z]*\):.*$%\1%p' >disks.bsi
50 for i in `cat disks.bsi`; do
51    j=`echo $i | cut -c6-`
52    sfdisk -l $i >sfdisk.$j.bsi 
53    sfdisk -d $i >sfdisk.make.$j.bsi
54 done
55 route -n >route.bsi
56 ifconfig >ifconfig.bsi
57 echo "Done collecting info."
58
59 #
60 # Done collecting information
61 #
62
63
64 echo "Begin creating scripts ..."
65 #
66 # First create partitioning script(s)
67 #
68 for i in `cat disks.bsi`; do
69   j=`echo $i | cut -c6-`
70   cat >$cwd/partition.$j <<END_OF_DATA
71 #!/bin/sh
72 #
73 #  Partition disk $i  -- created by getdiskinfo
74 echo ""
75 echo "This script will repartition disk $i."
76 echo ""
77 echo "IT WILL DESTROY ALL DATA ON DISK $i !!!!"
78 echo ""
79 echo -n "Are you sure you want to continue? yes/no: "
80 read a
81 if [ x\$a != xyes ] ; then
82    exit 1
83 fi 
84 echo "Partitioning disk $i"
85 # zap partition info
86 dd if=/dev/zero of=$i bs=512 count=2
87 # repartition
88 sfdisk $i <$di/sfdisk.make.$j.bsi | less
89 echo ""
90 echo "The previous partitioning was:"
91 cat $di/sfdisk.$j.bsi
92 #
93 echo ""
94 echo "The new partitioning is:"
95 sfdisk -l $i
96 echo ""
97 echo "If the disk is correctly partitioned, you should"
98 echo "now run the \"format.$j\" script."
99 echo ""
100 END_OF_DATA
101
102   chmod 755 $cwd/partition.$j
103 done
104 echo "Done making partitioning scripts"
105
106 #
107 # Create formatting script(s)
108 #
109 echo "Begin making formatting script(s) ..."
110 for i in `cat disks.bsi`; do
111   j=`echo $i | cut -c6-`
112   cat >$cwd/format.$j <<END_OF_DATA
113 #!/bin/sh
114 #
115 #  Format all partitions on disk $i -- created by getdiskinfo
116 #
117 echo ""
118 echo "This script will format all partitions on disk $i."
119 echo ""
120 echo "IT WILL DESTROY ALL DATA ON DISK $i !!!!"
121 echo ""
122 echo -n "Are you sure you want to continue? yes/no: "
123 read a
124 if [ x\$a != xyes ] ; then
125    exit 1
126 fi 
127 echo "Do you want to do a disk check for bad blocks?"
128 echo -n "It is recommended, but takes time. yes/no: "
129 read a
130 if [ x\$a = xyes ] ; then
131    check="-c"
132 else
133    check=
134 fi
135 END_OF_DATA
136
137    # Find swap partitions in output from sfdisk
138    k=`grep "^$i.*82  Linux swap" sfdisk.$j.bsi | cut -d ' ' -f 1`
139    for disk in $k; do
140       echo "echo \"Formatting $disk -- swap partition\"" >>$cwd/format.$j
141       echo "mkswap $check $disk" >>$cwd/format.$j
142       echo "echo \"\"" >>$cwd/format.$j
143    done
144    # Find ext2 partitions in mount output
145    k=`grep "^$i" mount.ext2.bsi | cut -d ' ' -f 1`
146    for disk in $k; do
147       echo "echo \"Formating $disk -- ext2 partition\"" >>$cwd/format.$j
148       label=`grep "^$disk" mount.ext2.bsi | cut -d ' ' -f 7 | cut -c2- | cut -d ] -f 1`
149       if [ x$label = x ] ; then
150          echo "mke2fs -v \$check $disk" >>$cwd/format.$j
151       else
152          echo "mke2fs -v \$check -L $label $disk" >>$cwd/format.$j
153       fi
154       echo "echo \"\"" >>$cwd/format.$j
155    done
156    # Find ext3 partitions in mount output
157    k=`grep "^$i" mount.ext3.bsi | cut -d ' ' -f 1`
158    for disk in $k; do
159       echo "echo \"Formating $disk -- ext3 partition\"" >>$cwd/format.$j
160       label=`grep "^$disk" mount.ext3.bsi | cut -d ' ' -f 7 | cut -c2- | cut -d ] -f 1`
161       if [ x$label = x ] ; then
162          echo "mke2fs -v -j \$check $disk" >>$cwd/format.$j
163       else
164          echo "mke2fs -v -j \$check -L $label $disk" >>$cwd/format.$j
165       fi
166       echo "echo \"\"" >>$cwd/format.$j
167    done
168    chmod 755 $cwd/format.$j
169 done
170
171 cd $cwd
172
173 #
174 # Create network start script
175 #
176 host=`hostname`
177 ip=`host $host | cut -d ' ' -f 4`
178 if [ $ip = "out;" ] ; then
179   ip=`ifconfig | grep inet | head -1 | sed -n 's/\ \+inet addr:\([0-9]\+\(\.[0-9]\+\)\{3\}\).*/\1/p'`
180 fi
181 cat >start_network <<END_OF_DATA
182 #!/bin/sh
183 #
184 #  Start network -- created by getdiskinfo
185 #
186 ip=$ip
187 dev=eth0
188 ifconfig lo up
189 ifconfig \$dev up \$ip
190 route add default gw \$ip dev \$dev
191 END_OF_DATA
192
193 chmod 755 start_network
194
195 cat >mount_drives <<END_OF_DATA
196 #!/bin/sh
197 #
198 #  Mount disk drives  -- created by getdiskinfo
199 #
200 END_OF_DATA
201 sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/\1 \/mnt\/disk\2/p' $di/mount.ext2.bsi >/tmp/1$$
202 sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/\1 \/mnt\/disk\2/p' $di/mount.ext3.bsi >>/tmp/1$$
203 sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/\1 \/mnt\/disk\2/p' $di/mount.rei.bsi >>/tmp/1$$
204 # sort so that / is first
205 sort -k 2 </tmp/1$$ >/tmp/2$$
206 # output mkdir followed by its mount
207 sed -n 's/\(^.*\)\ \(.*$\)/mkdir -p \2\nmount \1 \2/p' /tmp/2$$ >>mount_drives
208 rm -f /tmp/1$$ /tmp/2$$
209 rm -f /tmp/1$$
210
211 chmod 755 mount_drives
212
213 # copy sfdisk so we will have it
214 cp -f /sbin/sfdisk .
215 echo "Done building scripts."
216 echo " "
217 echo "You might want to do a:"
218 echo " "
219 echo "chown -R uuuu:gggg *"
220 echo " "
221 echo "where uuuu is your userid and gggg is your group"
222 echo "so that you can access all the files as non-root"
223 echo " "