Gentoo minimal boot rescue on Qemu

From Tom's notes
Jump to navigation Jump to search

Putting a rescue image on the harddisk in case normal boot doesn't work anymore.

Where to put files and configure grub

Files can be put in /boot, but in that case it's not possible from within the rescue boot to do anything with the boot partition as it will have the iso image mounted. To avoid this, an additional partition was created and mounted as /mnt/rescue (location doesn't matter). In theory, only the iso image is needed. Copy install-amd64-minimal-20170907.iso to /mnt/rescue.

As it's running on Qemu there's an additional problem that the virtio modules are not present in the initrd. To add these, the initrd has to be unpacked, the virtio modules added and repacked. First mount the iso image:

mount -o loop /mnt/rescue/install-amd64-minimal-20170907.iso /mnt/tmp

Extract the initrd:

mkdir /tmp/initrd
cd /tmp/initrd
xzcat /mnt/tmp/isolinux/gentoo.igz | cpio -ivmud

Mount the squashfs image and copy the modules, to add disk support at least virtio_blk.ko and virtio_pci.ko have to be added:

mount -o loop /mnt/tmp/image.squashfs /mnt/tmp2
cp /mnt/tmp2/lib/modules/4.12.5-gentoo/kernel/drivers/block/virtio_blk.ko /tmp/initrd/lib/modules/4.12.5-gentoo/kernel/drivers/block/
cp /mnt/tmp2/lib/modules/4.12.5-gentoo/kernel/drivers/virtio/virtio* /tmp/initrd/lib/modules/4.12.5-gentoo/kernel/drivers/virtio/

Repack the initrd:

find . -print | cpio -o -H newc | xz -9 -C none > /mnt/rescue/gentoo-minimal-20170907.igz

It's important to use -C none which omits checksums from the archive. Without the option the kernel seems to have problems decompressing the archive.

Create the grub entry (in /etc/grub.d/40_custom):

menuentry "Gentoo minimal 20170907" {
	insmod gzio
	insmod part_gpt
	insmod ext2
	search --no-floppy --fs-uuid --set=boot cddcff7f-81ef-45e9-9329-66056308b7bb
	search --no-floppy --fs-uuid --set=rescue 80eb8826-6e3e-4b7a-aa82-bfc59e4e9eb7
	loopback loop ($rescue)/"install-amd64-minimal-20170907.iso"
	linux (loop)/isolinux/gentoo root=/dev/ram0 init=/linuxrc dokeymap looptype=squashfs loop=/image.squashfs cdroot initrd=($boot)/gentoo-minimal-20170907.igz doload=virtio_pci,virtio_blk vga=791 isoboot=/install-amd64-minimal-20170907.iso
#	initrd (loop)/isolinux/gentoo.igz
	initrd ($rescue)/gentoo-minimal-20170907.igz
}

menuentry "Gentoo minimal 20170907 nofb" {
	insmod gzio
	insmod part_gpt
	insmod ext2
	search --no-floppy --fs-uuid --set=boot cddcff7f-81ef-45e9-9329-66056308b7bb
	search --no-floppy --fs-uuid --set=rescue 80eb8826-6e3e-4b7a-aa82-bfc59e4e9eb7
	loopback loop ($rescue)/"install-amd64-minimal-20170907.iso"
	linux (loop)/isolinux/gentoo root=/dev/ram0 init=/linuxrc dokeymap looptype=squashfs loop=/image.squashfs cdroot initrd=($boot)/gentoo-minimal-20170907.igz doload=virtio_pci,virtio_blk isoboot=/install-amd64-minimal-20170907.iso
#	initrd (loop)/isolinux/gentoo.igz
	initrd ($rescue)/gentoo-minimal-20170907.igz
}

In the above block, the UUIDs are for the boot and rescue partition respectively. Also required is the option doload=virtio_pci,virtio_blk which will tell the initrd to actually load these modules. The option isoboot=/install-amd64-minimal-20170907.iso is also required to tell the initrd that it should first look for the iso file, before looking for the image.squashfs which is inside the iso file. The commented initrd line could be used if the initrd image already contained the virtio drivers (maybe in the future).

To actually activate the new grub settings:

grub-mkconfig -o /boot/grub/grub.cfg