Linux – Place grub on virtual disk

bootgrub2linuxqemu

To learn about Linux startup process I placed grub in a disk image file and tried to boot using qemu. I currently has not put any kernel image into the drive just grub. I installed grub using

kpartx -av mydrive.img
losetup /dev/loop1 /dev/mapper/loop0p1
mount /dev/loop1 mnt/mydrive/
cd mnt/mydrive/boot
grub-install --no-floppy --boot-directory=. -v /dev/loop0

I wanted to go through the boot sequence step by step, so I expected grub to claim that the kernel is missing, then I want to fix that by installing the kernel, and continue adding stuff all the way up to X.

Now I get

error: no such device

But expected

error: no configuration file

form rescue prompt, ls gives

(hd0) (fd0)

Questions:
* What device is grub looking for? Is that something that refers to the host system? [Partially solved, the UUID is the same as the UUID for the virtual file system]
* Why cannot Grub find the device?

All works fine if I install grub (and nothing but grub) from a live-cd (I chose Bodhi-linux since this is a small binary distro) inside the VM. Will qemu give the boot partition a different uuid, not used outside?

Here is the partition table for the virtual drive:

Disk mydrive.img: 264 MB, 264241152 byte
32 huvuden, 63 sektorer/spår, 256 cylindrar, totalt 516096 sektorer
Enheter = sektorer av 1 · 512 = 512 byte
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Diskidentifierare: 0xebe6ebdb

       Enhet Start     Början        Slut     Block    Id  System
mydrive.img1            2048      516095      257024   83  Linux

Best Answer

It seems the module for the kind of partition (I assume a DOS/MBR partition label) is not installed by grub by default into core.img.

Use the following to install the required module as well:

grub-install --modules part_msdos --root-directory=. /dev/loop0

Then grub should be able to read the partition table, the filesystem and therefore the installation contained in mnt/mydrive/boot.

As a result the grub command ls should output something like:

(hd0) (hd0,msdos1) (hd0)
Related Question