Sample Header Ad - 728x90

Configure GRUB to load in QEMU

2 votes
1 answer
5136 views
I have a a debootstrapped Debian image, which is created using the following commands:
qemu-img create -f qcow2 disk.qcow2 16G

su

export PATH=$PATH:/sbin:/usr/sbin
export TERM=xterm

modprobe nbd
qemu-nbd --connect=/dev/nbd0 disk.qcow2 --cache=unsafe --discard=unmap
parted --align optimal --script --machine --fix /dev/nbd0 -- \
       mklabel gpt \
       mkpart efi fat32 '0%' 500MiB \
       set 1 boot on \
       mkpart swap linux-swap 500MiB 1GiB \
       mkpart root ext4 1GiB '100%'
mkfs.vfat -F 32 /dev/nbd0p1
mkswap /dev/nbd0p2
mkfs.ext4 /dev/nbd0p3

mkdir -p disk
mount /dev/nbd0p3 disk

debootstrap --arch amd64 bullseye ./disk
mkdir -p ./disk/boot/efi
mount /dev/nbd0p1 ./disk/boot/efi
mount --bind /dev ./disk/dev
mount -t devpts /dev/pts ./disk/dev/pts
mount -t proc proc ./disk/proc
mount -t sysfs sysfs ./disk/sys
mount -t tmpfs tmpfs ./disk/tmp

chroot ./disk

echo -e 'password\npassword' | passwd

echo debian > /etc/hostname

UUID1=$(blkid /dev/nbd0p1 | grep -Eo ' UUID="[^ ]*"' | sed 's/[" ]//g')
UUID2=$(blkid /dev/nbd0p2 | grep -Eo ' UUID="[^ ]*"' | sed 's/[" ]//g')
UUID3=$(blkid /dev/nbd0p3 | grep -Eo ' UUID="[^ ]*"' | sed 's/[" ]//g')
echo "none /tmp tmpfs defaults 0 0" > /etc/fstab
echo "$UUID1 /boot/efi vfat defaults 0 2" >> /etc/fstab
echo "$UUID2 none swap sw 0 0" >> /etc/fstab
echo "$UUID3 / ext4 errors=remount-ro 0 1" >> /etc/fstab

cat > /etc/apt/sources.list  /etc/systemd/network/80-dhcp.network > /etc/default/grub
update-grub2

exit

for dev in tmp sys proc dev/pts dev boot/efi; do
    umount ./disk/$dev
done

umount disk
rmdir disk
qemu-nbd --disconnect /dev/nbd0

exit
I can successfully run this image when I copy kernel and initrd images from the drive and start them directly using the command
qemu-system-x86_64 \
    -drive format=qcow2,file=disk.qcow2 \
    -kernel vmlinuz \
    -initrd initrd.img \
    -cpu host -M q35  \
    -enable-kvm \
    -m 2G,maxmem=4G \
    -device rtl8139,netdev=network0 \
    -netdev user,id=network0 \
    -append "root=/dev/sda3"
But when I try to start this disk using GRUB with [SeaBIOS](https://www.coreboot.org/SeaBIOS) (default QEMU BIOS) with command
qemu-system-x86_64 \
    -drive format=qcow2,file=disk.qcow2 \
    -boot c  \
    -cpu host -M q35  \
    -enable-kvm \
    -m 2G,maxmem=4G \
    -device rtl8139,netdev=network0 \
    -netdev user,id=network0
it gets stuck at Booting from Hard Disk... line. When using [OVMF EFI](https://packages.debian.org/bullseye/ovmf) with command
qemu-system-x86_64 \
    -drive format=qcow2,file=disk.qcow2 \
    -boot c  \
    -bios /usr/share/ovmf/OVMF.fd \
    -cpu host -M q35  \
    -enable-kvm \
    -m 2G,maxmem=4G \
    -device rtl8139,netdev=network0 \
    -netdev user,id=network0
I get error lines
BdsDxe: failed to load Boot0001 "UEFI QEMU DVD-ROM QM00005 " from PciRoot(0x0)/Pci(0x1F,0x2)/Sata(0x2,0xFFFF,0x0): Not Found
BdsDxe: failed to load Boot0002 "UEFI QEMU HARDDISK QM00001 " from PciRoot(0x0)/Pci(0x1F,0x2)/Sata(0x0,0xFFFF,0x0): Not Found
How do I make my disk to load with GRUB?
Asked by Sergey (383 rep)
Oct 1, 2022, 07:13 AM
Last activity: Feb 22, 2025, 01:17 PM