ext2 Diskfile for grub2

Grub playground with grub-diskfile

Foreward Play with grub on normal distro_grub_installed -say ubuntu-11.04 natty- is Risky ! even destroying the filesystem then unbootable !
The idea is to play grub in virtual world ; without using another "big distro" , the only reasonable solution is to create a bootable grub_diskfile and have fun with "qemu grub_diskfile".
Unfortunately inside grub_src there exists only grub-mkrescue creating a diskfile in iso9660 readonly_format ,so that without remastering the diskfile.iso one cannot include there more files to experiment with !
That's why the following script -extract just essentials needed for i386-pc 's ext2_diskfile creation - inspired from orginal_grub-mkrescue


Example: try with localGrub_installed_notouch_current_distroGrub !

bzr branch http://bzr.savannah.gnu.org/r/grub/trunk/grub
cd grub
./configure --prefix=($PWD)LocalGrub && make && make install
cd LocalGrub

(some_path)/LocalGrubSetupRescueDisk testdisk 10 hd

qemu testdisk.img

If you don't want to compile local grub just download in attach_script_below mkGrubRescueExt2Disk.sh
It uses ubuntu-natty installed grub tools to produce bootable diskfile.

That's it !



cat LocalGrubSetupRescueDisk

#/bin/sh
set -e
PATH=$PWD/bin:$PWD/sbin:$PATH

#any configure for your needs
#ex: configure --enable-diskless --enable-rtl8139 --prefix=($PWD)LocalGrub
# In LocalGrub_dir : (path)/LocalGrubSetupRescueDisk test 2 cd
# check with "qemu test.img"

if [ $# -lt 3 ]
then
echo "usage: $0 diskfile_name size_in_Mb type_hd/cd "
echo " ex: GrubSetupRescueDisk myrescue 2 cd
will produce myrescue.iso size 2Mb
type_hd will produce myrescue.img 2Mb ext2"
exit 1
fi

## creation diskfile and partition it a la msdos
cnt=$((32 * $2))
cyl=$((2 * $2))

dd if=/dev/zero of=$1 bs=32256 count=$cnt

fdisk -C $cyl -H 16 -S 63 $1 >/dev/null 2>&1 <\<EOF
n
p
1



a
1

w

EOF


## use sfdisk instead of fdisk ;shown for pedagogy
# /sbin/sfdisk -L -uS -C 4 -H 16 -S 63 $1 <
# ,,L,*
# ;
# ;
# ;
# y
# EOF


##format ext2 fs diskfile

loopdevice=`losetup -f`
echo $loopdevice
losetup $loopdevice $1
nextloop=`losetup -f`
echo $nextloop
losetup -o32256 $nextloop $1
mke2fs $nextloop
[ -d mnt ] || mkdir mnt
mount $nextloop mnt
mkdir -p mnt/boot/grub/


##inform grub devices with pseudo_pc_device a.k.a loopback

cat > mnt/boot/grub/device.map <
(hd0) /dev/loop0
(hd0,1) /dev/loop1
EOF


##prepare SOURCE_dir for grub 's modules,img,lst files
##remark filesystem_module to be in core.img which by default contains kernel.img built dynamically during grub-mkimage

cp lib/grub/i386-pc/* mnt/boot/grub

case "$3" in
hd)

grub-mkimage -d lib/grub/i386-pc -O i386-pc --output=mnt/boot/grub/core.img --prefix=/boot/grub/ biosdisk ext2 part_msdos
grub-setup --directory=mnt/boot/grub --device-map=mnt/boot/grub/device.map $loopdevice
;;

cd)

grub-mkimage -d lib/grub/i386-pc -O i386-pc --output=mnt/boot/grub/core.img --prefix=/boot/grub/ iso9660 biosdisk
cat "mnt/boot/grub/cdboot.img" "mnt/boot/grub/core.img" > "mnt/boot/grub/eltorito.img"

/usr/bin/genisoimage -b boot/grub/eltorito.img -R -no-emul-boot -boot-info-table -o $1.iso -r mnt/
;;

esac


##The following two lines allow experimenting diskfile_grub_bootable using script grub-install !
# grub-mkconfig -o mnt/boot/grub/grub.cfg
# grub-install --grub-mkdevicemap=device.map --root-directory=mnt $loopdevice


##cleanup
umount mnt
losetup -d $nextloop
losetup -d $loopdevice
rm -fr mnt
if [ "$3" = "hd" ]; then mv "$1" "$1.img";fi
rm -f "$1"



ċ
LocalGrubSetupRescueDisk
(2k)
hoan nn,
Oct 24, 2011, 5:48 AM
ċ
mkGrubRescueExt2Disk.sh
(2k)
hoan nn,
Oct 25, 2011, 2:11 AM
Comments