Here is the source code for the scripts mentioned above.
Code:
#! /bin/bash
#
# mbrinstall --- install grub from this partition on to the MBR of the
# current disk. When the BIOS subsequently boots this disk, it will run
# grub from this partition.
if [[ $(pwd) != */scripts || $0 != ./* ]] ; then
echo error you must cd to the scripts directory and invoke this script with "./name"
exit 1
fi
#
# Extract device file name and mount point from the output of "df" command
set $(df -k . | awk 'NR == 2 { print $0}')
devicefile=$1
mountpoint=$6
name=${mountpoint#/}
#
# Build grub style device name
devicefile=$(df -k . | awk 'NR == 2 { print $1}')
string=${devicefile#/dev/}
partition=${string#???}
drive=${string%$partition}
case "$drive" in
hda) grubdrive="hd0" ;;
hdb) grubdrive="hd1" ;;
hdc) grubdrive="hd2" ;;
hdd) grubdrive="hd3" ;;
*)
echo localinstall cannot handle device $drive
exit 1
;;
esac
((partition=partition-1))
grubdevice="(${grubdrive},${partition})"
echo devicefile=$devicefile
echo mountpoint=$mountpoint
echo name=$name
echo partition = $partition
echo drive = $drive
echo grubdrive = $grubdrive
echo grubdevice = $grubdevice
#
# rewrite grub.conf to reflect the local partition
cp ../grub/grub.conf ../grub/grub.conf.old
sed < ../grub/grub.conf.old > ../grub/grub.conf \
's/.*root (.*/ root '${grubdevice}'/
s/.*title Boot Octave from.*/ title Boot Octave from '${name}'/'
#
# Use grub to install itself on the drive
set -x
${mountpoint}/bin/grub --batch --verbose --device-map=${mountpoint}/grub/device.map << End
root $grubdevice
setup (${grubdrive})
End
set +x
exit 0
Code:
#! /bin/bash
#
# localinstall -- install grub in the first sector of the current partition.
# This will make the current partition bootable.
dir=$(pwd)
if [[ $dir != */scripts || $0 != ./* ]] ; then
echo error you must cd to the scripts directory and invoke this script with "./name"
exit 1
fi
exit 0
#
# Extract device file name and mount point from the output of "df" command
set $(df -k . | awk 'NR == 2 { print $0}')
devicefile=$1
mountpoint=$6
name=${mountpoint#/}
#
# Build grub style device name
string=${devicefile#/dev/}
partition=${string#???}
drive=${string%$partition}
case "$drive" in
hda) grubdrive="hd0" ;;
hdb) grubdrive="hd1" ;;
hdc) grubdrive="hd2" ;;
hdd) grubdrive="hd3" ;;
*)
echo localinstall cannot handle device $drive
exit 1
;;
esac
((partition=partition-1))
grubdevice="(${grubdrive},${partition})"
echo devicefile=$devicefile
echo mountpoint=$mountpoint
echo name=$name
echo partition = $partition
echo drive = $drive
echo grubdrive = $grubdrive
echo grubdevice = $grubdevice
#
# rewrite grub.conf to reflect the local partition
cp ../grub/grub.conf ../grub/grub.conf.old
sed < ../grub/grub.conf.old > ../grub/grub.conf \
's/.*root (.*/ root '${grubdevice}'/
s/.*title Boot Octave from.*/ title Boot Octave from '${name}'/'
set -x
#
# We are going to write on the raw device so
# make sure that the filesystem does not also
# do any writing.
mount -o remount -o ro $mountpoint
sync
#
# Use grub to install itself on the device
${mountpoint}/bin/grub --batch --verbose --device-map=${mountpoint}/grub/device.map << End
root $grubdevice
setup $grubdevice
End
#
# Now we need a new copy of bootsector to give to XP
[[ ! -d /driveE/bootsectors ]] && mkdir /driveE/bootsectors
dd if=${fulldevice} bs=512 count=1 of=/driveE/bootsectors/bootsector.$name
set +x
#
# Restore read-write access to mountpoint
mount -o remount -o rw $mountpoint
exit 0
Code:
#! /bin/bash
#
# floppyinstall -- install the grub software including the configuration file
# from this partition on to a floppy. This will create a bootable floppy that
# will boot the kernels from this partition.
#
if [[ $(pwd) != */scripts || $0 != ./* ]] ; then
echo error you must cd to the scripts directory and invoke this script with "./name"
exit 1
fi
#
# Extract device file name and mount point from the output of "df" command
set $(df -k . | awk 'NR == 2 { print $0}')
devicefile=$1
mountpoint=$6
name=${mountpoint#/}
#
# Build grub style device name
string=${devicefile#/dev/}
partition=${string#???}
drive=${string%$partition}
case "$drive" in
hda) grubdrive="hd0" ;;
hdb) grubdrive="hd1" ;;
hdc) grubdrive="hd2" ;;
hdd) grubdrive="hd3" ;;
*)
echo localinstall cannot handle device $drive
exit 1
;;
esac
((partition=partition-1))
grubdevice="(${grubdrive},${partition})"
echo devicefile=$devicefile
echo mountpoint=$mountpoint
echo name=$name
echo partition = $partition
echo drive = $drive
echo grubdrive = $grubdrive
echo grubdevice = $grubdevice
#
# set up the floppy
set -x
mkdosfs -F 32 -c /dev/fd0
[[ ! -d /media/floppy ]] && mkdir -p /media/floppy
mount -t vfat -o shortname=winnt /dev/fd0 /media/floppy
mkdir -p /media/floppy/boot/grub
cd /media/floppy/boot/grub
cp ${mountpoint}/grub/stage1 .
cp ${mountpoint}/grub/stage2 .
cd ${mountpoint}
#
# rewrite grub.conf to reflect the local partition
sed < ${mountpoint}/grub/grub.conf > /media/floppy/boot/grub/grub.conf \
's/.*root (.*/ root '${grubdevice}'/
s/.*title Boot Octave from.*/ title Boot Octave from '${name}' via floppy built '"$(date +'%B %e, %Y')"'/'
umount /dev/fd0
#
# Use grub to install itself on floppy
set -x
${mountpoint}/bin/grub --batch --verbose --device-map=${mountpoint}/grub/device.map << End
root (fd0)
setup (fd0)
End
exit 0
Code:
#! /bin/bash
#
# cdinstall -- install the grub software including the configuration file
# from this partition on to a cd. This will create a bootable cd that
# can boot the kernels from the cd itself.
#
if [[ $(pwd) != */scripts || $0 != ./* ]] ; then
echo error you must cd to the scripts directory and invoke this script with "./name"
exit 1
fi
#
# Extract device file name and mount point from the output of "df" command
set $(df -k . | awk 'NR == 2 { print $0}')
devicefile=$1
mountpoint=$6
name=${mountpoint#/}
#
# Build grub style device name
string=${devicefile#/dev/}
partition=${string#???}
drive=${string%$partition}
case "$drive" in
hda) grubdrive="hd0" ;;
hdb) grubdrive="hd1" ;;
hdc) grubdrive="hd2" ;;
hdd) grubdrive="hd3" ;;
*)
echo localinstall cannot handle device $drive
exit 1
;;
esac
((partition=partition-1))
grubdevice="(${grubdrive},${partition})"
grubdevice="(cd)"
echo devicefile=$devicefile
echo mountpoint=$mountpoint
echo name=$name
echo partition = $partition
echo drive = $drive
echo grubdrive = $grubdrive
echo grubdevice = $grubdevice
set -x
mkdir /tmp/iso
mkdir /tmp/iso/backup
cp -R $mountpoint /tmp/iso/backup
mkdir -p /tmp/iso/boot/grub
cp /mastergrub/src/grub-0.97/stage2/stage2_eltorito /tmp/iso/boot/grub
#cp /mastergrub/grub/grub.conf /tmp/iso/boot/grub
# rewrite grub.conf to reflect the local partition
sed < ${mountpoint}/grub/grub.conf > /tmp/iso/boot/grub/menu.lst \
's/.*root (.*/ root '${grubdevice}'/
s/.*title Boot Octave from.*/ title Boot Octave from cd built '"$(date +'%B %e, %Y')"'/'
#cp /mastergrub/grub/grub.conf /tmp/iso/boot/grub/menu.lst
cp -R /mastergrub/Redhat /tmp/iso
cp -R /mastergrub/Fedora /tmp/iso
cp -R /mastergrub/SuSE /tmp/iso
cp -R /mastergrub/Debian /tmp/iso
cp -R /mastergrub/Scientific /tmp/iso
mkisofs -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -boot-info-table -o /driveE/grub.iso /tmp/iso
exit 0
Code:
#! /bin/bash
#
# supermbrbackup -- save disk infrastructure
# This script backs up the partition structure on hd0. It also scans the hidden sectors and backs up any
# non-zero data. In my case, this is grub stage 1.5. And naturally, the boot code in the mbr is included.
# Without this stuff, the disk won't be bootable. The data is saved on a floppy disk. Also a shell script
# to restore the data is included on the floppy. That script can be run from a bootable cd such as the
# System Rescue CD. So this is intended as the first step of a bare metal restore.
#
#
# We need some info about the disk (psuedo) geometry
SECT_NUM=63 #### Sectors per cylinder
SECT_LNG=512 ### Bytes per sector
#
# This was originally a ksh script which was converted to bash. The original ksh code is still here,
# prefixed with "##".
##integer bytes
##set -A bytes abytes
declare -a bytes abytes
typeset -i bytes
#
# Build a filesystem on the floppy
mkfs -t ext2 -b 1024 -i 1024 -O sparse_super /dev/fd0 2>/dev/null
mkdir -p /media/fd0
mount -t ext2 /dev/fd0 /media/fd0
#
# The variable called "restore" will contain the entire restore script. The script will be written out
# once it is complete. I wanted to prevent excess i/o on the floppy drive and I expect the script to
# be short... so I decided to build it in-core.
total_saved=0
restore="#! /bin/bash"
#
# Read a specified sector and set up the arrays abytes and bytes with the contents of each byte
function read_sector
{
skip=$1
## dd if=/dev/hda bs=$SECT_LNG skip=$skip count=1 2>/dev/null| od -An -t x1 -w32 -v
abytes=( $(dd if=/dev/hda bs=$SECT_LNG skip=$skip count=1 2>/dev/null| od -An -t x1 -w32 -v ))
for((ibyte=0; ibyte < ${#abytes[*]}; ibyte++)) ; do
bytes[ibyte]="16#${abytes[ibyte]}"
done
return 0
}
#
# Write the sector onto the floppy disk (also update the restore script)
function save_sector
{
((total_saved++))
dd if=/dev/hda bs=$SECT_LNG skip=$1 count=1 2>/dev/null of=/media/fd0/sec${1}
restore="$restore
dd if=sec${1} bs=$SECT_LNG seek=$1 count=1 of=/dev/hda"
return 0
}
#
# Nothing to check.... sector zero is always saved.
echo sector 0 is the MBR
save_sector 0
#
# Now scan the hidden sectors. Back them up only if the sectors contains some non-zero data
for((sec=1; sec < $SECT_NUM; sec++)) ; do
read_sector $sec
nonzero=0
for((ibyte=0; ibyte < ${#bytes[*]}; ibyte++)) ; do
((${bytes[ibyte]})) && nonzero=1
done
if ((nonzero)) ; then
echo sector $sec has non-zero data
save_sector $sec
fi
done
##typeset -R2 partition
##typeset -i bytes
##typeset -R11 next
read_sector 0
exstart=0
#
# Scan the partition table looking for an extended partition.
for ((partition=1; partition < 5; partition++)) ; do
((s=430 + (partition * 16)))
status=${abytes[s]}
type=${abytes[s+4]}
(( fval=(( (${bytes[s+11]}*256+${bytes[s+10]})*256 + ${bytes[s+9]})*256)+${bytes[s+8]} ))
(( length=(((${bytes[s+15]}*256+${bytes[s+14]})*256+${bytes[s+13]})*256)+${bytes[s+12]} ))
((lval=fval+length-1))
((lval == -1)) && ((lval=0))
if [[ $type = 0f ]] ; then
exstart=$fval
fi
done
#
# If we found an extended partition, step though the logical's and back up each MBR of each logical
# drive.
if ((exstart)) ; then
next=$exstart
while ((next)) ; do
read_sector $next
((s=430 + (1 * 16)))
type=${abytes[s+4]}
(( fval=(( (${bytes[s+11]}*256+${bytes[s+10]})*256 + ${bytes[s+9]})*256)+${bytes[s+8]} ))
(( length=(((${bytes[s+15]}*256+${bytes[s+14]})*256+${bytes[s+13]})*256)+${bytes[s+12]} ))
((lval=fval+length-1))
((lval == -1)) && ((lval=0))
((pstart=fval + next))
((pstop=lval + next))
((s=430 + (2 * 16)))
type=${abytes[s+4]}
(( fval=(( (${bytes[s+11]}*256+${bytes[s+10]})*256 + ${bytes[s+9]})*256)+${bytes[s+8]} ))
(( length=(((${bytes[s+15]}*256+${bytes[s+14]})*256+${bytes[s+13]})*256)+${bytes[s+12]} ))
((lval=fval+length-1))
((lval == -1)) && ((lval=0))
if ((length)) ; then
((nextnext=fval+exstart))
else
((nextnext=0))
fi
echo "sector $next describes logical partition $partition ($pstart - $pstop)"
save_sector $next
((partition=partition+1))
((next=nextnext))
done
fi
#
# Write out the restore script and finish up
echo "$restore" > /media/fd0/restore
chmod 755 /media/fd0/restore
umount /dev/fd0
exit 0