3 # Create bootstrap information files -- prelude to creating a
6 # Kern Sibbald, December MMII
7 # This source distributed under the GPL
17 FreeBSD | SunOS | IRIX)
19 echo "This code is not yet adapted to this OS"
24 echo "Unknown operating system type: $host"
28 if [ ! `whoami` = "root" ] ; then
30 echo "You need to be root to run this, otherwise"
31 echo "sfdisk produces no output. Continuing anyway ..."
36 # First collect information
38 rm -rf format.* partition.* $di create-*
39 echo "Begin collecting system info ..."
43 mount -l -t ext2 >mount.ext2.bsi
44 mount -l -t ext3 >mount.ext3.bsi
45 mount -l -t reiserfs >mount.rei.bsi
46 cp /etc/fstab fstab.bsi
49 sfdisk -s >sfdisk.disks.bsi
50 grep "^/dev/" sfdisk.disks.bsi | sed -n 's%\(^/dev/[A-Za-z]*\):.*$%\1%p' >disks.bsi
51 for i in `cat disks.bsi`; do
52 j=`echo $i | cut -c6-`
53 sfdisk -l $i >sfdisk.$j.bsi
54 sfdisk -d $i >sfdisk.make.$j.bsi
56 cat /proc/swaps > swaps.bsi
58 ifconfig >ifconfig.bsi
60 # Gather LVM information
63 if which lvm > /dev/null 2>/dev/null ; then
64 echo "Gathering LVM information"
67 lvm pvs --noheadings --nosuffix -o pv_name,vg_name | cat > lvm-pv.bsi
68 lvm vgs --noheadings --nosuffix -o vg_name,vg_extent_size --units=k | cat > lvm-vg.bsi
69 lvm lvs --noheadings --nosuffix -o lv_name,vg_name,lv_size --units=k | cat > lvm-lv.bsi
72 echo "Done collecting info."
75 # Done collecting information
79 echo "Begin creating scripts ..."
81 # First create partitioning script(s)
83 for i in `cat disks.bsi`; do
84 j=`echo $i | cut -c6-`
85 cat >$cwd/partition.$j <<END_OF_DATA
89 # Partition disk $i -- created by getdiskinfo
91 echo "This script will repartition disk $i."
93 echo "IT WILL DESTROY ALL DATA ON DISK $i !!!!"
95 echo -n "Are you sure you want to continue? yes/no: "
97 if [ x\$a != xyes ] ; then
100 echo "Partitioning disk $i"
102 dd if=/dev/zero of=$i bs=512 count=2
104 sfdisk $i <$di/sfdisk.make.$j.bsi | less
106 echo "The previous partitioning was:"
107 cat $di/sfdisk.$j.bsi
110 echo "The new partitioning is:"
113 echo "If the disk is correctly partitioned, you should"
114 echo "now run the \"format.$j\" script."
119 chmod 755 $cwd/partition.$j
121 echo "Done making partitioning scripts"
123 if [ $dolvm -eq 1 ] ; then
125 # LVM phase 1: create the Physical Volumess
127 cat > $cwd/create-pv <<EOF
130 # Create PVs for LVM -- created by getdiskinfo
132 echo "This script will format all partitions listed in diskinfo/lvm-pv.bsi"
134 echo "IT WILL DESTROY ALL DATA ON THESE PARTITIONS!!!!"
137 for pv in `cat lvm-pv.bsi | awk '{print $1}'`; do
138 echo "echo \" $pv\"" >> $cwd/create-pv
141 cat >> $cwd/create-pv <<EOF
143 echo -n "Are you sure you want to continue? yes/no: "
145 if [ x\$a != xyes ] ; then
149 for pv in `cat lvm-pv.bsi | awk '{print $1}'`; do
150 echo "echo \"Creating PV on $pv\"" >> $cwd/create-pv
151 echo "lvm pvcreate -ff -y -v $pv" >> $cwd/create-pv
153 echo "echo \"Done.\"" >> $cwd/create-pv
154 echo "echo \"If there were no errors, run create-vg to create the volume groups.\"" >> $cwd/create-pv
155 chmod +x $cwd/create-pv
158 # LVM phase 2: create Volume Groups
160 cat > $cwd/create-vg <<EOF
163 echo "This script will create all volume groups listed in diskinfo/lvm-vg.bsi"
164 echo "You must have sucesfully run create-pv to create the requesite PVs first"
166 echo -n "Are you sure you want to continue? yes/no: "
168 if [ x\$a != xyes ] ; then
172 cat lvm-vg.bsi | awk '{print $1, $2}' | while read vg pesize ; do
173 echo "echo \"Creating VG $vg PE extent size $pesize kbytes"\" >> $cwd/create-vg
175 for pv in `cat lvm-pv.bsi | awk "\\\$2 == \"$vg\" { print \\\$1 }"` ; do
178 echo "lvm vgcreate $vg -v -s ${pesize}k $pelist" >> $cwd/create-vg
180 echo "echo \"Done.\"" >> $cwd/create-vg
181 echo "echo \"If there were no errors, run create-lv to create the logical volumes.\"" >> $cwd/create-vg
182 chmod +x $cwd/create-vg
186 # LVM phase 3: create Logical Volumes
188 # One quick sidenote: this script creates LVs with a size based on the
189 # physical extent count, rather than the size in bytes. This is because
190 # at the time of writing this script, lvdisplay erroneously prints out
191 # the size in 512 byte sector count rather than 1024 byte blocks.
192 # Using the extent count should allow this script to continue to work
193 # even after the bug is fixed.
195 cat > $cwd/create-lv <<EOF
198 echo "This script will create all logical volumes listed in diskinfo/lvm-lv.bsi"
199 echo "You must have sucesfully run create-vg to create the requesite VGs first"
201 echo -n "Are you sure you want to continue? yes/no: "
203 if [ x\$a != xyes ] ; then
207 cat lvm-lv.bsi | awk '{print $1, $2, $3}' | while read lv vg lvsize ; do
208 # lv=`echo $lv | sed -e 's/.*\///'`
209 echo "echo \"Creating LV $lv\"" >> $cwd/create-lv
210 echo "lvm lvcreate -L ${lvsize}k $vg -n $lv" >> $cwd/create-lv
212 chmod +x $cwd/create-lv
216 # Create formatting script(s)
218 echo "Begin making formatting script(s) ..."
219 for i in `cat disks.bsi`; do
220 j=`echo $i | cut -c6-`
221 cat >$cwd/format.$j <<END_OF_DATA
224 # Format all partitions on disk $i -- created by getdiskinfo
227 echo "This script will format all partitions on disk $i."
229 echo "IT WILL DESTROY ALL DATA ON DISK $i !!!!"
231 echo -n "Are you sure you want to continue? yes/no: "
233 if [ x\$a != xyes ] ; then
236 echo "Do you want to do a disk check for bad blocks?"
237 echo -n "It is recommended, but takes time. yes/no: "
239 if [ x\$a = xyes ] ; then
246 # Find swap partitions in output from sfdisk
247 k=`grep "^$i.*82 Linux swap" sfdisk.$j.bsi | cut -d ' ' -f 1`
249 echo "echo \"Formatting $disk -- swap partition\"" >>$cwd/format.$j
250 echo "mkswap $check $disk" >>$cwd/format.$j
251 echo "echo \"\"" >>$cwd/format.$j
253 # Find ext2 partitions in mount output
254 k=`grep "^$i" mount.ext2.bsi | cut -d ' ' -f 1`
256 echo "echo \"Formating $disk -- ext2 partition\"" >>$cwd/format.$j
257 label=`grep "^$disk" mount.ext2.bsi | cut -d ' ' -f 7 | cut -c2- | cut -d ] -f 1`
258 if [ x$label = x ] ; then
259 echo "mke2fs -v \$check $disk" >>$cwd/format.$j
261 echo "mke2fs -v \$check -L $label $disk" >>$cwd/format.$j
263 echo "echo \"\"" >>$cwd/format.$j
265 # Find ext3 partitions in mount output
266 k=`grep "^$i" mount.ext3.bsi | cut -d ' ' -f 1`
268 echo "echo \"Formating $disk -- ext3 partition\"" >>$cwd/format.$j
269 label=`grep "^$disk" mount.ext3.bsi | cut -d ' ' -f 7 | cut -c2- | cut -d ] -f 1`
270 if [ x$label = x ] ; then
271 echo "mke2fs -v -j \$check $disk" >>$cwd/format.$j
273 echo "mke2fs -v -j \$check -L $label $disk" >>$cwd/format.$j
275 echo "echo \"\"" >>$cwd/format.$j
277 # Find reiserfs partitions in mount output
278 k=`grep "^$i" mount.rei.bsi | cut -d ' ' -f 1`
280 echo "echo \"Formating $disk -- reiser partition\"" >>$cwd/format.$j
281 label=`grep "^$disk" mount.rei.bsi | cut -d ' ' -f 7 | cut -c2- | cut -d ] -f 1`
282 if [ x$label = x ] ; then
283 echo "mkereiserfs -V \$check $disk" >>$cwd/format.$j
285 echo "mkereiserfs -V \$check -L $label $disk" >>$cwd/format.$j
287 echo "echo \"\"" >>$cwd/format.$j
289 chmod 755 $cwd/format.$j
293 # Create LVM formatting scripts(s)
295 # Somebody more clever than I with bash scripting
296 # could probably factor a lot of this code out.
298 if [ $dolvm -eq 1 ] ; then
299 echo "Begin making LVM formatting script(s) ..."
300 for i in `cat lvm-vg.bsi | awk '{print $1}'`; do
301 cat >$cwd/format.$i <<END_OF_DATA
304 # Format all partitions on VG $i -- created by getdiskinfo
307 echo "This script will format all LVs on VG $i."
309 echo "IT WILL DESTROY ALL DATA ON DISK $i !!!!"
311 echo -n "Are you sure you want to continue? yes/no: "
313 if [ x\$a != xyes ] ; then
316 echo "Do you want to do a disk check for bad blocks?"
317 echo -n "It is recommended, but takes time. yes/no: "
319 if [ x\$a = xyes ] ; then
326 # Find swap partitions in output from sfdisk
327 mapper="/dev/mapper/${i}"
328 k=`grep "^$mapper" swaps.bsi | awk '{print $1}'`
330 echo "echo \"Formatting $disk -- swap partition\"" >>$cwd/format.$i
331 echo "mkswap $check $disk" >>$cwd/format.$i
332 echo "echo \"\"" >>$cwd/format.$i
334 # Find ext2 partitions in mount output
335 k=`grep "^$mapper" mount.ext2.bsi | cut -d ' ' -f 1`
337 echo "echo \"Formating $disk -- ext2 partition\"" >>$cwd/format.$i
338 label=`grep "^$disk on" mount.ext2.bsi | cut -d ' ' -f 7 | cut -c2- | cut -d ] -f 1`
339 if [ x$label = x ] ; then
340 echo "mke2fs -v \$check $disk" >>$cwd/format.$i
342 echo "mke2fs -v \$check -L $label $disk" >>$cwd/format.$i
344 echo "echo \"\"" >>$cwd/format.$i
346 # Find ext3 partitions in mount output
347 k=`grep "^$mapper" mount.ext3.bsi | cut -d ' ' -f 1`
349 echo "echo \"Formating $disk -- ext3 partition\"" >>$cwd/format.$i
350 label=`grep "^$disk on" mount.ext3.bsi | cut -d ' ' -f 7 | cut -c2- | cut -d ] -f 1`
351 if [ x$label = x ] ; then
352 echo "mke2fs -v -j \$check $disk" >>$cwd/format.$i
354 echo "mke2fs -v -j \$check -L $label $disk" >>$cwd/format.$i
356 echo "echo \"\"" >>$cwd/format.$i
358 # Find reiserfs partitions in mount output
359 k=`grep "^$mapper" mount.rei.bsi | cut -d ' ' -f 1`
361 echo "echo \"Formating $disk -- reiser partition\"" >>$cwd/format.$i
362 label=`grep "^$disk on" mount.rei.bsi | cut -d ' ' -f 7 | cut -c2- | cut -d ] -f 1`
363 if [ x$label = x ] ; then
364 echo "mkereiserfs -V \$check $disk" >>$cwd/format.$i
366 echo "mkereiserfs -V \$check -L $label $disk" >>$cwd/format.$i
368 echo "echo \"\"" >>$cwd/format.$i
370 chmod 755 $cwd/format.$i
377 # Create network start script
379 ifconfig eth0 2>/dev/null >/dev/null
381 ip=`ifconfig eth0 | grep inet | head -1 | sed -n 's/\ \+inet addr:\([0-9]\+\(\.[0-9]\+\)\{3\}\).*/\1/p'`
382 cat >start_network <<END_OF_DATA
385 # Start network -- created by getdiskinfo
390 ifconfig \$dev up \$ip
393 echo "Warning, no eth0 found."
394 echo "#!/bin/sh" >start_network
397 ifconfig eth1 2>/dev/null >/dev/null
399 ip=`ifconfig eth1 | grep inet | head -1 | sed -n 's/\ \+inet addr:\([0-9]\+\(\.[0-9]\+\)\{3\}\).*/\1/p'`
400 cat >>start_network <<END_OF_DATA
403 ifconfig \$dev1 up \$ip1
406 ip=`route -n | grep UG | head -1 | awk '{print $2}'`
407 dev=`route -n | grep UG | head -1 | awk '{print $8}'`
408 cat >>start_network <<END_OF_DATA
409 route add default gw $ip dev $dev
412 chmod 755 start_network
414 cat >mount_drives <<END_OF_DATA
417 # Mount disk drives -- created by getdiskinfo
419 PATH=$PATH:/mnt/disk/sbin:/mnt/disk/bin
421 TMP1=`mktemp gdi.1.XXXXXXXXXX`
422 TMP2=`mktemp gdi.2.XXXXXXXXXX`
423 if test x${TMP1} = x; then
425 if test -f ${TMP1}; then
426 echo "Temp file security problem on: ${TMP1}"
430 if test x${TMP2} = x; then
432 if test -f ${TMP2}; then
433 echo "Temp file security problem on: ${TMP2}"
438 sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/\1 \/mnt\/disk\2/p' $di/mount.ext2.bsi >${TMP1}
439 sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/\1 \/mnt\/disk\2/p' $di/mount.ext3.bsi >>${TMP1}
440 sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/\1 \/mnt\/disk\2/p' $di/mount.rei.bsi >>${TMP1}
441 # sort so that / is first
442 sort -k 2 <${TMP1} >${TMP2}
443 # output mkdir followed by its mount
444 sed -n 's/\(^.*\)\ \(.*$\)/mkdir -p \2\
445 mount \1 \2/p' ${TMP2} >>mount_drives
447 # Now build unmount_drives
448 cat >umount_drives <<END_OF_DATA
451 # Unmount disk drives -- created by getdiskinfo
454 sort -r -k 2 <${TMP1} >${TMP2}
455 sed -n 's/\(^.*\)\ \(.*$\)/umount \2/p' ${TMP2} >>umount_drives
456 echo "umount /mnt/cdrom 2>/dev/null >/dev/null" >>umount_drives
457 echo "sync" >>umount_drives
458 echo "sync" >>umount_drives
460 rm -f ${TMP1} ${TMP2}
462 chmod 755 mount_drives umount_drives
463 echo "Done building scripts."