Tips and tricks: setting up QEMU for kernel development
by Anne Macedo
This is just a post documenting what I did to set up a QEMU VM for doing kernel development. It also highlights the pain points for people that want to do a headless installation (i.e. without having a GTK display available).
Why? Because I like to code on my MacBook while it’s connected to my Linux box. That’s why I want to avoid VNC, displays and other stuff.
I will not cover how to set up KVM (which is a painful process :P).
This post is based on a post by FLUSP [1].
Create a qcow2 image
This step is straightforward.
qemu-img create -f qcow2 kernel-dev.qcow 20G
Install your distro
I’m using debian.iso as my boot disk.
I decided to use -curses here for a curses-based GUI.
qemu-system-x86_64 -enable-kvm -cdrom debian.iso -boot order=d \
-drive file=kernel-dev.qcow,format=qcow2 -m 2G -curses
After that, nothing happens.
But fear not: if you see a blank screen, hit Esc and then type:
install vga=normal fb=false
This is to disable the framebuffer and to start in vga mode. [2]
Hopefully, you’ll see the installation prompt for debian. Follow the steps through completion.
Boot your VM
This is how I booted my VM:
qemu-system-x86_64 -enable-kvm \
-nic user,hostfwd=tcp::2222-:22 \
-m 16G \
-smp cores=8,cpus=8 \
-nographic \
kernel-dev.qcow
I’m using a handful of cores and RAM, but you can use whichever values your hardware supports.
Troubleshooting: what if you need to change a grub setting but you can’t access your VM?
I had this problem where the VM wouldn’t boot and I couldn’t see anything. No logs, no nothing.
So, I had to mount my qcow2 image to update grub.
To achieve that, you can enable qemu-nbd [3].
sudo modprobe nbd max_part=8
Then connect the nbd.
sudo qemu-nbd --connect=/dev/nbd0 kernel-dev.qcow
Mount it and mount procfs, sysfs and /dev
sudo mkdir /mnt/qcow
sudo mount /dev/nbd0p1 /mnt/qcow
sudo mount --rbind /dev /mnt/qcow/dev
sudo mount --rbind /sys /mnt/qcow/sys
sudo mount --rbind /proc /mnt/qcow/proc
You can now chroot to your mountpoint
sudo chroot /mnt/qcow
Make your changes under /etc/default/grub, then run update-grub.
To disconnect the nbd, run:
sudo qemu-nbd --disconnect=/dev/nbd0
References
[1] Use QEMU to Play with Linux Kernel [2] qemu-kvm install iso falls in blank screen with “640 480 graphic mode” [3] QEMU-NBD(8)
tags: