10-22-2006
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
7 More Discussions You Might Find Interesting
1. UNIX for Dummies Questions & Answers
I have an x86 Sun server, with no CDROM drive and no OS currently loaded on it. I need to install CentOS on it... trying to figure out how to do that.
I've heard there are ways to connect to the serial management port, and mount a laptop's CDROM drive as a drive the server will recognize to... (0 Replies)
Discussion started by: Mariognarly
0 Replies
2. Ubuntu
Hi to all,
I have the problem that a laptops with windows XP cannot startup even in safe mode nor using last good known configuration. I have a Ubuntu 10.10 Live CD and booting from it I can read the Hard Drive.
I need to do a backup the Hard Drive from XP laptop and I want to connect this... (5 Replies)
Discussion started by: cgkmal
5 Replies
3. UNIX and Linux Applications
Hej guys,
I'm doing some simple matrix operations with octave and don't know where and how it stores it's variables. Is there any file somewhere that could be accessed?
I was using MATLAB before and now Octave is new for me.
Or maybe I should use some commands to save and load it.
but... (0 Replies)
Discussion started by: Johanni
0 Replies
4. Shell Programming and Scripting
Hej all,
I have an script which I run it with Octave command in Linux, I want to know how could I put bash commands like grep and sed and use them together with octave?
My Octave script:
Script.sh:
m = load ("file", "-ascii")
for i=1:10
g(i)= sin(m)
end
and then I use this... (2 Replies)
Discussion started by: Johanni
2 Replies
5. Red Hat
Hi all,
Good day. I just installed RHEL6.3 into my laptop to learn RH.
There is an existing Windows 7 in there.
After installing the RHEL, the laptop will just boot to Windows 7.
I tried to use this BCDedit to add Linux entry to the boot menu, BUT each time i pick the redhat selection,... (5 Replies)
Discussion started by: wingcross
5 Replies
6. Programming
This is probably something really simple, but I don't get it. I downloaded a copy of Octave and extracted it to octave-2.1.73-sol10-sparc-local. Now what do I do with it? I chmod'ed it to 777 and tried running it which just gave me an endless stream of errors. Here's the beginning of the file... (4 Replies)
Discussion started by: Michele31416
4 Replies
7. Ubuntu
Dear all,
I would like to transfer my old laptop documents/files etc to the new laptop without using any external hard disk.
Please let me know if its possible via any way.
Thank in advance,
emily (3 Replies)
Discussion started by: emily
3 Replies