Install grub on mounted kvm image?
I am trying to mount a KVM image on the host machine in order to fix a grub issue however seem to be going wrong somewhere.
I have tried the following steps:
mount -o loop,offset=32256 /var/lib/libvirt/images/vm1.img /mnt/vm1
mount -obind /dev/ /mnt/vm1/dev/
mount -obind /proc/ /mnt/vm1/proc/
mount -obind /sys/ /mnt/vm1/sys/
cd /mnt/vm1/
chroot .
grub-install
I do the above steps but when i install grub I see the following:
root@host:/# grub-install /dev/sda
grub-probe: error: Cannot find a GRUB drive for /dev/loop0. Check your device.map.
Auto-detection of a filesystem module failed.
Please specify the module with the option `--modules' explicitly.
Does anyone have any idea what I am missing or what the next step is? I am going around in circles on this one.
Solution 1:
The problem is that grub-install
install a bios-based (i.e. in real-mode) chunk into the boot sector. Yes, this is that old "real mode" from the PC/XT/AT era, most *86 machines are booting in this mode until now.
The BIOS doesn't know anything from your devices. In the bios, the block devices have a sinlge hexa byte identifier: floppy disks had 0x00, 0x01, ..., and hard disks have 0x80, 0x81, 0x82, ... (CDROM, pendrives are unknown for the bios, but in most cases there is some type of emulation which gives them look as if they were hard disks).
When grub-install
is called, it needs to say to this real mode boot code, from which devices should he load the next stages of the boot loader. This is the grub, which you soon know.
In the devices.map
, you says the grub installer, how should he install the bios boot loader, thus you give a mapping between your block devices, and their bios numbers (soon in the boot time!). It must be somewhere in /boot/grub
or in /boot/grub2
, or similar.
After you installed grub into your loopback device, it is highly suggested to revert your devices.map
to its original state.
If you know, that the loopback device on which you now the grub install, will be the first hard disk during the first stage of the boot, you could give it simply the bios identifier 0x80.
P.s.: It is the only way to say the bios, what to know with your loopback device. In the era of the bios development, the loopback devices were very, very esoteric things known only for big machines.
P.s.2.: There is a tool named kpartx
which can find the partitions of any file or device, and map them into device mapper block devices. I played a lot with losetup .. ---offset
solutions as you, at least a short googling for that I suggest to strong.