I'm trying to create a virtual disk with grub2 on it. I'm doing this because I'm going to be writing a multi-boot compliant loader. I want to test this on a VM, so I need a virtual disk with grub2 for testing. It seems like having a virtual disk with grub2 on it would be a common thing to do, but this is very difficult. On a side note: is there an easier way to do this?
I think part of the problem is the grub-install command (in Step 7 below). This command was written to work with a real disk, not a virtual disk. I'm essentially trying to trick the grub-install command into thinking that my virtual disk is a real disk. Maybe this isn't a good idea?
I noticed strange behaviour when running Qemu after grub-install (Step 7 and 8). Sometimes when I run Qemu, Grub boots successfully, but the text would be messed up (show strange characters). Then, if I were to do grub-install again, the Grub would not boot and go into the grub rescue prompt. Other times, Grub would boot but it will give errors when trying to list the contents of a disk partition. When I do the same behaviour (running grub-install), I get different results every time. This seems counter-intuitive to the whole concept of programming (if you do the same thing, you will get the same results). What's going on?
I got almost everything from this tutorial: http://blog.worldturner.com/worldturner/entry/creating_a_bootable_virtual_appliance
This is everything I did from the beginning. I'm using Ubuntu 11.10.
Step 1: create the disk
# touch ~/Desktop/vdisk.img # dd if=/dev/zero of=~/Desktop/vdisk.img bs=1 count=0 seek=2G
Step 2: partition the disk
# parted -s disk.img mklabel msdos # parted -s --align=none disk.img mkpart primary 64s 100%
Step 3: set vdisk to loop device and map it to a device. Then create device for each partition
# sudo losetup --find --show vdisk.img /dev/loop3 # echo "0 4194304 linear /dev/loop3 0" | dmsetup create sdb # kpartx -a /dev/mapper/sdb
Step 4: copy each partition device to /dev/
# cp -R -L /dev/mapper/sdb /dev/msdb # cp -R -L /dev/mapper/sdb1 /dev/msdb1
Step 5: make the filesystem
# mkfs -t ext4 /dev/msdb1
Step 6: mount partition
# mkdir -p /mnt/vdisk # mount /dev/msdb1 /mnt/vdisk
Step 7: install grub onto virtual disk
# mkdir /mnt/vdisk/boot # grub-install --boot-directory=/mnt/vdisk/boot /dev/msdb Installation finished. No error reported.
Step 8: run the disk in qemu. This fails.
# sudo qemu /dev/msdb Booting from Hard Disk... error: no such device: <long string appears here (i think it's a UUID)> grub rescue> _
Here's the output to
fdisk -l /dev/msdb:
Device Boot Start End Blocks Id System /deb/msdb1 64 4194303 2097120 83 Linux
I'm a complete noob with this stuff so I feel like I might be making an obvious mistake. Let me know if you see anything that's incorrect. Thanks!