Virtme: The Kernel Developer’s Best Friend

1735

When working on the Linux Kernel, testing via QEMU is pretty common. Here’s a look at virtme, a QEMU wrapper that uses the host instead of a virtual disk, making working with QEMU extremely easy.

By Ezequiel Garcia, Senior Software Engineer at Collabora.

When working on the Linux Kernel, testing via QEMU is pretty common. Many virtual drivers have been recently merged, useful either to test the kernel core code, or your application. These virtual drivers make QEMU even more attractive.

However, QEMU can be hard to setup, which is discouraging for some developers: all you wanted was to run a test, and suddenly you are reading through QEMU man pages, trying to find the right combination of arguments. We have blogged about QEMU’s bonanzas and so this time we want to take a somewhat different path and explore virtme, which is basically a QEMU wrapper. Quoting virtme’s own readme:

“Virtme is a set of simple tools to run a virtualized Linux kernel that uses the host Linux distribution or a simple rootfs instead of a whole disk image. Virtme is tiny, easy to use, and makes testing kernel changes quite simple.”

The tool was written by Andy Lutomirski. See more details on the readme.

We really enjoy using this tool, and have found it’s not too well known. So, we’ve decided to spread the word, and put together a curated zero-to-kernel steps:

Installing virtme

Instead of using Andy Lutomirski’s upstream, we are going to use Ezequiel’s repo. This version of virtme, simply adds some extra sugar.

git clone https://github.com/ezequielgarcia/virtme.git
cd virtme
sudo ./setup.py install

Now get your favourite kernel tree

git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

Configure it for virtme

virtme comes with some handy tools to produce a suitable kernel configuration. This makes the config process much easier.

virtme-configkernel --defconfig

Enable the drivers you need

For instance, let’s enable the vim2m driver. This is a virtual video4linux memory2memory virtual driver.

CONFIG_MEDIA_SUPPORT=y
CONFIG_MEDIA_CAMERA_SUPPORT=y
CONFIG_VIDEO_DEV=y
CONFIG_VIDEO_V4L2=y
CONFIG_V4L2_MEM2MEM_DEV=y
CONFIG_V4L_TEST_DRIVERS=y
CONFIG_VIDEO_VIM2M=y

Build the kernel

make -j4

Run virtme

sudo virtme-run --kimg arch/x86_64/boot/bzImage

or just:

sudo virtme-run --kdir .

Extra sugar

Running scripts at boot

One of them is --script-dir, which allows to run some scripts at boot. This can be used to run all your tests at boot, providing a quick way to test kernel changes.

Continue reading on Collabora’s blog.