Building a KVM Guest for Kernel Testing

Making mistakes and correcting them is part of the software development process. Trial and error come with the territory, no matter how well a developer plans and executes her code. Thus, running and testing a kernel that you’ve just hacked on yourself can be risky. Even without kernel panics, it can be a big PITR because you can’t just stop and start a kernel like you can any other program; you have to reboot the machine. This is why kernel development is often done inside a virtual machine. A guest system running inside a VM can be stopped and started much the same way as any other program, and it’s definitely a lot faster than rebooting.

My mentor advised me to use KVM because, with Linux rather than a separate hypervisor in charge, it’s much lighter and faster than other virtualization solutions. I chose to use LVM logical volumes to store the guest images, because I thought that would offer me the most flexibility in the long run, without having yet another folder full of hacking junk in my home directory. Here’s what my final setup looks like, and how you can replicate it on a full featured Linux distro (Mint and Ubuntu, in my case).

First, make sure that virtualization technology is enabled in your machine’s BIOS. Unless your machine is quite old, your processor is almost certainly built with it; to test, run egrep -c '(vmx|svm)' /proc/cpuinfo and if the output is nonzero, you’re good to go. Once you enable it, be sure to completely power down your machine–don’t just reboot–before you run lsmod | grep kvm and look for the needed modules. There should be two; one is kvm and the other, in my case, is kvm_intel.

Of course, you’re going to need to install a bunch of packages. I can’t list every one because I let Synaptic pull a bunch of them in for me and, to be honest, I fumbled around awhile before I had them all. Among the key packages and searches were qemu-kvm (but not qemu itself), qemu-system, and system-config-lvm for the logical volume manager (I selected the GUI tool and let it pull in the base packages). A suggestion from my mentor, virt-manager, turned out to be centrally important. The commands for creating and managing VMs is confusing (forms of it are currently being deprecated and replaced, for instance), and the first time I ran the tool, it suggested and fetched the qemu-system-* packages.

While you’re fetching bits, grab the OS that you want to run in the guest, if you don’t already have it. I used Ubuntu Saucy, the same OS as my main computer (the host). The installation will go a lot faster if you have it stored as an iso in your file system rather than on a disk.

If you choose to use LVM volumes for the guests (instead of creating disk image files), create them first. The version of system-config-lvm that ships with current Debian based distributions (and perhaps others) has a bug described here, and will not start if called from the menu. That link contains a workaround, which I’ve wrapped in a shell script here. Just make one logical volume for each guest, and make it big (my main one is 75G). You will partition it into drives and swap areas through the guest’s installation program.

Now it’s a simple matter of calling Virtual Machine Manager from the menu and filling in the blanks on its five screens. If you’ve created logical volumes, you’ll also need to tell Virtual Machine Manager where to find them. Click Edit->Connection Details and in the Storage tab, click the blue plus sign at the bottom left (great description of the process here). Once the guest’s installation program starts, it will be just like installing Linux anywhere else. Once the installation is done and tested, set it up for kernel hacking as described in the OPW First Patch tutorial, and you’re good to go! Happy hacking!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s