3 # Create bootstrap information files -- prelude to creating a
6 # Kern Sibbald, December MMII
7 # This source distributed under the GPL
18 FreeBSD | SunOS | IRIX)
20 echo "This code is not yet adapted to this OS"
25 echo "Unknown operating system type: $host"
29 if [ ! `whoami` = "root" ] ; then
31 echo "You need to be root to run this script ..."
37 # First collect information
39 rm -rf format.* partition.* $di create-* $scr
40 echo "Begin collecting system info ..."
44 mount -l -t ext2 >mount.ext2.bsi
45 mount -l -t ext3 >mount.ext3.bsi
46 mount -l -t reiserfs >mount.rei.bsi
47 cp /etc/fstab fstab.bsi
50 sfdisk -s >sfdisk.disks.bsi
51 grep "^/dev/" sfdisk.disks.bsi | sed -n 's%\(^/dev/[A-Za-z]*\):.*$%\1%p' >disks.bsi
52 for i in `cat disks.bsi`; do
53 j=`echo $i | cut -c6-`
54 sfdisk -l $i >sfdisk.$j.bsi
55 sfdisk -d $i >sfdisk.make.$j.bsi
57 cat /proc/swaps > swaps.bsi
59 ifconfig >ifconfig.bsi
61 # Gather LVM information
64 if which lvm > /dev/null 2>/dev/null ; then
65 echo "Gathering LVM information"
68 lvm pvs --noheadings --nosuffix -o pv_name,vg_name | cat > lvm-pv.bsi
69 lvm vgs --noheadings --nosuffix -o vg_name,vg_extent_size --units=k | cat > lvm-vg.bsi
70 lvm lvs --noheadings --nosuffix -o lv_name,vg_name,lv_size --units=k | cat > lvm-lv.bsi
73 echo "Done collecting info."
76 # Done collecting information
80 echo "Begin creating scripts ..."
82 # First create partitioning script(s)
85 for i in `cat disks.bsi`; do
86 j=`echo $i | cut -c6-`
87 cat >$scr/partition.$j <<END_OF_DATA
91 # Partition disk $i -- created by getdiskinfo
93 echo "This script will repartition disk $i."
95 echo "IT WILL DESTROY ALL DATA ON DISK $i !!!!"
97 echo -n "Are you sure you want to continue? yes/no: "
99 if [ x\$a != xyes ] ; then
102 echo "Partitioning disk $i"
104 dd if=/dev/zero of=$i bs=512 count=2
106 sfdisk $i <$di/sfdisk.make.$j.bsi | less
108 echo "The previous partitioning was:"
109 cat $di/sfdisk.$j.bsi
112 echo "The new partitioning is:"
115 echo "If the disk is correctly partitioned, you should"
116 echo "now run the \"format.$j\" script."
121 chmod 755 $scr/partition.$j
123 echo "Done making partitioning scripts"
125 if [ $dolvm -eq 1 ] ; then
127 # LVM phase 1: create the Physical Volumess
129 cat > $scr/create-pv <<EOF
132 # Create PVs for LVM -- created by getdiskinfo
134 echo "This script will format all partitions listed in diskinfo/lvm-pv.bsi"
136 echo "IT WILL DESTROY ALL DATA ON THESE PARTITIONS!!!!"
139 for pv in `cat lvm-pv.bsi | awk '{print $1}'`; do
140 echo "echo \" $pv\"" >> $scr/create-pv
143 cat >> $scr/create-pv <<EOF
145 echo -n "Are you sure you want to continue? yes/no: "
147 if [ x\$a != xyes ] ; then
151 for pv in `cat lvm-pv.bsi | awk '{print $1}'`; do
152 echo "echo \"Creating PV on $pv\"" >> $scr/create-pv
153 echo "lvm pvcreate -ff -y -v $pv" >> $scr/create-pv
155 echo "echo \"Done.\"" >> $scr/create-pv
156 echo "echo \"If there were no errors, run create-vg to create the volume groups.\"" >> $scr/create-pv
157 chmod +x $scr/create-pv
160 # LVM phase 2: create Volume Groups
162 cat > $scr/create-vg <<EOF
165 echo "This script will create all volume groups listed in diskinfo/lvm-vg.bsi"
166 echo "You must have sucesfully run create-pv to create the requesite PVs first"
168 echo -n "Are you sure you want to continue? yes/no: "
170 if [ x\$a != xyes ] ; then
174 cat lvm-vg.bsi | awk '{print $1, $2}' | while read vg pesize ; do
175 echo "echo \"Creating VG $vg PE extent size $pesize kbytes"\" >> $scr/create-vg
177 for pv in `cat lvm-pv.bsi | awk "\\\$2 == \"$vg\" { print \\\$1 }"` ; do
180 echo "lvm vgcreate $vg -v -s ${pesize}k $pelist" >> $scr/create-vg
182 echo "echo \"Done.\"" >> $scr/create-vg
183 echo "echo \"If there were no errors, run create-lv to create the logical volumes.\"" >> $scr/create-vg
184 chmod +x $scr/create-vg
188 # LVM phase 3: create Logical Volumes
190 # One quick sidenote: this script creates LVs with a size based on the
191 # physical extent count, rather than the size in bytes. This is because
192 # at the time of writing this script, lvdisplay erroneously prints out
193 # the size in 512 byte sector count rather than 1024 byte blocks.
194 # Using the extent count should allow this script to continue to work
195 # even after the bug is fixed.
197 cat > $scr/create-lv <<EOF
200 echo "This script will create all logical volumes listed in diskinfo/lvm-lv.bsi"
201 echo "You must have sucesfully run create-vg to create the requesite VGs first"
203 echo -n "Are you sure you want to continue? yes/no: "
205 if [ x\$a != xyes ] ; then
209 cat lvm-lv.bsi | awk '{print $1, $2, $3}' | while read lv vg lvsize ; do
210 # lv=`echo $lv | sed -e 's/.*\///'`
211 echo "echo \"Creating LV $lv\"" >> $scr/create-lv
212 echo "lvm lvcreate -L ${lvsize}k $vg -n $lv" >> $scr/create-lv
214 chmod +x $scr/create-lv
218 # Create formatting script(s)
220 echo "Begin making formatting script(s) ..."
221 for i in `cat disks.bsi`; do
222 j=`echo $i | cut -c6-`
223 cat >$scr/format.$j <<END_OF_DATA
226 # Format all partitions on disk $i -- created by getdiskinfo
229 echo "This script will format all partitions on disk $i."
231 echo "IT WILL DESTROY ALL DATA ON DISK $i !!!!"
233 echo -n "Are you sure you want to continue? yes/no: "
235 if [ x\$a != xyes ] ; then
238 echo "Do you want to do a disk check for bad blocks?"
239 echo -n "It is recommended, but takes time. yes/no: "
241 if [ x\$a = xyes ] ; then
248 # Find swap partitions in output from sfdisk
249 k=`grep "^$i.*82 Linux swap" sfdisk.$j.bsi | cut -d ' ' -f 1`
251 echo "echo \"Formatting $disk -- swap partition\"" >>$scr/format.$j
252 echo "mkswap $check $disk" >>$scr/format.$j
253 echo "echo \"\"" >>$scr/format.$j
255 # Find ext2 partitions in mount output
256 k=`grep "^$i" mount.ext2.bsi | cut -d ' ' -f 1`
258 echo "echo \"Formating $disk -- ext2 partition\"" >>$scr/format.$j
259 label=`grep "^$disk" mount.ext2.bsi | cut -d ' ' -f 7 | cut -c2- | cut -d ] -f 1`
260 if [ x$label = x ] ; then
261 echo "mke2fs -v \$check $disk" >>$scr/format.$j
263 echo "mke2fs -v \$check -L $label $disk" >>$scr/format.$j
265 echo "echo \"\"" >>$scr/format.$j
267 # Find ext3 partitions in mount output
268 k=`grep "^$i" mount.ext3.bsi | cut -d ' ' -f 1`
270 echo "echo \"Formating $disk -- ext3 partition\"" >>$scr/format.$j
271 label=`grep "^$disk" mount.ext3.bsi | cut -d ' ' -f 7 | cut -c2- | cut -d ] -f 1`
272 if [ x$label = x ] ; then
273 echo "mke2fs -v -j \$check $disk" >>$scr/format.$j
275 echo "mke2fs -v -j \$check -L $label $disk" >>$scr/format.$j
277 echo "echo \"\"" >>$scr/format.$j
279 # Find reiserfs partitions in mount output
280 k=`grep "^$i" mount.rei.bsi | cut -d ' ' -f 1`
282 echo "echo \"Formating $disk -- reiser partition\"" >>$scr/format.$j
283 label=`grep "^$disk" mount.rei.bsi | cut -d ' ' -f 7 | cut -c2- | cut -d ] -f 1`
284 if [ x$label = x ] ; then
285 echo "mkereiserfs -V \$check $disk" >>$scr/format.$j
287 echo "mkereiserfs -V \$check -L $label $disk" >>$scr/format.$j
289 echo "echo \"\"" >>$scr/format.$j
291 chmod 755 $scr/format.$j
295 # Create LVM formatting scripts(s)
297 # Somebody more clever than I with bash scripting
298 # could probably factor a lot of this code out.
300 if [ $dolvm -eq 1 ] ; then
301 echo "Begin making LVM formatting script(s) ..."
302 for i in `cat lvm-vg.bsi | awk '{print $1}'`; do
303 cat >$scr/format.$i <<END_OF_DATA
306 # Format all partitions on VG $i -- created by getdiskinfo
309 echo "This script will format all LVs on VG $i."
311 echo "IT WILL DESTROY ALL DATA ON DISK $i !!!!"
313 echo -n "Are you sure you want to continue? yes/no: "
315 if [ x\$a != xyes ] ; then
318 echo "Do you want to do a disk check for bad blocks?"
319 echo -n "It is recommended, but takes time. yes/no: "
321 if [ x\$a = xyes ] ; then
328 # Find swap partitions in output from sfdisk
329 mapper="/dev/mapper/${i}"
330 k=`grep "^$mapper" swaps.bsi | awk '{print $1}'`
332 echo "echo \"Formatting $disk -- swap partition\"" >>$scr/format.$i
333 echo "mkswap $check $disk" >>$scr/format.$i
334 echo "echo \"\"" >>$scr/format.$i
336 # Find ext2 partitions in mount output
337 k=`grep "^$mapper" mount.ext2.bsi | cut -d ' ' -f 1`
339 echo "echo \"Formating $disk -- ext2 partition\"" >>$scr/format.$i
340 label=`grep "^$disk on" mount.ext2.bsi | cut -d ' ' -f 7 | cut -c2- | cut -d ] -f 1`
341 if [ x$label = x ] ; then
342 echo "mke2fs -v \$check $disk" >>$scr/format.$i
344 echo "mke2fs -v \$check -L $label $disk" >>$scr/format.$i
346 echo "echo \"\"" >>$scr/format.$i
348 # Find ext3 partitions in mount output
349 k=`grep "^$mapper" mount.ext3.bsi | cut -d ' ' -f 1`
351 echo "echo \"Formating $disk -- ext3 partition\"" >>$scr/format.$i
352 label=`grep "^$disk on" mount.ext3.bsi | cut -d ' ' -f 7 | cut -c2- | cut -d ] -f 1`
353 if [ x$label = x ] ; then
354 echo "mke2fs -v -j \$check $disk" >>$scr/format.$i
356 echo "mke2fs -v -j \$check -L $label $disk" >>$scr/format.$i
358 echo "echo \"\"" >>$scr/format.$i
360 # Find reiserfs partitions in mount output
361 k=`grep "^$mapper" mount.rei.bsi | cut -d ' ' -f 1`
363 echo "echo \"Formating $disk -- reiser partition\"" >>$scr/format.$i
364 label=`grep "^$disk on" mount.rei.bsi | cut -d ' ' -f 7 | cut -c2- | cut -d ] -f 1`
365 if [ x$label = x ] ; then
366 echo "mkereiserfs -V \$check $disk" >>$scr/format.$i
368 echo "mkereiserfs -V \$check -L $label $disk" >>$scr/format.$i
370 echo "echo \"\"" >>$scr/format.$i
372 chmod 755 $scr/format.$i
379 # Create network start script
381 ifconfig eth0 2>/dev/null >/dev/null
383 ip=`ifconfig eth0 | grep inet | head -1 | sed -n 's/\ \+inet addr:\([0-9]\+\(\.[0-9]\+\)\{3\}\).*/\1/p'`
384 cat >start_network <<END_OF_DATA
387 # Start network -- created by getdiskinfo
392 ifconfig \$dev up \$ip
395 echo "Warning, no eth0 found."
396 echo "#!/bin/sh" >start_network
399 ifconfig eth1 2>/dev/null >/dev/null
401 ip=`ifconfig eth1 | grep inet | head -1 | sed -n 's/\ \+inet addr:\([0-9]\+\(\.[0-9]\+\)\{3\}\).*/\1/p'`
402 cat >>start_network <<END_OF_DATA
405 ifconfig \$dev1 up \$ip1
408 ip=`route -n | grep UG | head -1 | awk '{print $2}'`
409 dev=`route -n | grep UG | head -1 | awk '{print $8}'`
410 cat >>start_network <<END_OF_DATA
411 route add default gw $ip dev $dev
414 chmod 755 start_network
416 cat >$scr/mount_drives <<END_OF_DATA
419 # Mount disk drives -- created by getdiskinfo
421 PATH=$PATH:/mnt/disk/sbin:/mnt/disk/bin
423 TMP1=`mktemp gdi.1.XXXXXXXXXX`
424 TMP2=`mktemp gdi.2.XXXXXXXXXX`
425 if test x${TMP1} = x; then
427 if test -f ${TMP1}; then
428 echo "Temp file security problem on: ${TMP1}"
432 if test x${TMP2} = x; then
434 if test -f ${TMP2}; then
435 echo "Temp file security problem on: ${TMP2}"
440 sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/\1 \/mnt\/disk\2/p' $di/mount.ext2.bsi >${TMP1}
441 sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/\1 \/mnt\/disk\2/p' $di/mount.ext3.bsi >>${TMP1}
442 sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/\1 \/mnt\/disk\2/p' $di/mount.rei.bsi >>${TMP1}
443 # sort so that / is first
444 sort -k 2 <${TMP1} >${TMP2}
445 # output mkdir followed by its mount
446 sed -n 's/\(^.*\)\ \(.*$\)/mkdir -p \2\
447 mount \1 \2/p' ${TMP2} >>$scr/mount_drives
449 # Now build unmount_drives
450 cat >$scr/umount_drives <<END_OF_DATA
453 # Unmount disk drives -- created by getdiskinfo
456 sort -r -k 2 <${TMP1} >${TMP2}
457 sed -n 's/\(^.*\)\ \(.*$\)/umount \2/p' ${TMP2} >>$scr/umount_drives
458 echo "umount /mnt/cdrom 2>/dev/null >/dev/null" >>$scr/umount_drives
459 echo "sync" >>$scr/umount_drives
460 echo "sync" >>$scr/umount_drives
462 rm -f ${TMP1} ${TMP2}
464 chmod 755 $scr/mount_drives $scr/umount_drives
465 echo "Done building scripts."