We're hiring!
*

Virtme: The kernel developers' best friend

Ezequiel Garcia avatar

Ezequiel Garcia
September 18, 2018

Share this post:

Reading time:

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

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

or just:

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.

The following command will run all the .sh scripts in the test-dir/ directory.

virtme-run --kdir . --script-dir test-dir/

Continuing with our previous example, we could use the script-dir parameter to run the v4l2-compliance test on the vim2m driver. Something like this would do the job:

#!/bin/sh

v4l2-compliance -d /dev/video0 -s

Specifying a module directory

Another virtme added parameter is --mdir. This one allows you to specify a module directory. It is useful if you want to load modules on the guest, as otherwise the guest would try to load the host modules, which is of course not what we want.

This is how it works. Let's first configure the kernel to build some modules.

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

And install them to a ./tmp directory:

make INSTALL_MOD_PATH=./tmp modules_install
  INSTALL crypto/crypto_engine.ko
  INSTALL drivers/crypto/virtio/virtio_crypto.ko
  INSTALL drivers/media/common/videobuf2/videobuf2-common.ko
  INSTALL drivers/media/common/videobuf2/videobuf2-memops.ko
  INSTALL drivers/media/common/videobuf2/videobuf2-v4l2.ko
  INSTALL drivers/media/common/videobuf2/videobuf2-vmalloc.ko
[..]

We are now ready to use to run virtme and load some modules:

virtme-run --kimg arch/x86_64/boot/bzImage --mdir tmp/lib/modules/4.19.0-rc3
Decompressing Linux... Parsing ELF... Performing relocations... done.
Booting the kernel.
[..]
root@(none):/# modprobe vim2m
[   66.071041] videodev: Linux video capture interface: v2.00
[   66.083405] vim2m vim2m.0: Device registered as /dev/video0

Coming soon...

In the next blog post we'll see an example of virtme in action, testing bleeding-edge gstreamer builds on bleeding-edge kernels. Stay tuned!

 

Comments (4)

  1. Guilherme Gallo:
    Sep 25, 2018 at 01:33 AM

    Hey Ezequiel. Great article!

    I have one question for you.
    Do you know how to access the virtme's virtualized Linux kernel via SSH?

    Thanks in advance.

    Reply to this comment

    Reply to this comment

    1. Ezequiel:
      Feb 07, 2019 at 08:22 PM

      Hello Guilherme:

      To be honest, I haven't tried sshing to the guest. Never needed that! :-)

      Reply to this comment

      Reply to this comment

  2. Andy Lutomirski:
    Feb 07, 2019 at 12:02 AM

    Why sudo? Virtme only requires permission to open /dev/kvm, which many distros give to normal users.

    Reply to this comment

    Reply to this comment

    1. Ezequiel:
      Feb 07, 2019 at 10:19 PM

      Hey Andy,

      Absolutely right. Thanks for the correction.

      Reply to this comment

      Reply to this comment


Add a Comment






Allowed tags: <b><i><br>Add a new comment:


Search the newsroom

Latest Blog Posts

Automatic regression handling and reporting for the Linux Kernel

14/03/2024

In continuation with our series about Kernel Integration we'll go into more detail about how regression detection, processing, and tracking…

Almost a fully open-source boot chain for Rockchip's RK3588!

21/02/2024

Now included in our Debian images & available via our GitLab, you can build a complete, working BL31 (Boot Loader stage 3.1), and replace…

What's the latest with WirePlumber?

19/02/2024

Back in 2022, after a series of issues were found in its design, I made the call to rework some of WirePlumber's fundamentals in order to…

DRM-CI: A GitLab-CI pipeline for Linux kernel testing

08/02/2024

Continuing our Kernel Integration series, we're excited to introduce DRM-CI, a groundbreaking solution that enables developers to test their…

Persian Rug, Part 4 - The limitations of proxies

23/01/2024

This is the fourth and final part in a series on persian-rug, a Rust crate for interconnected objects. We've touched on the two big limitations:…

How to share code between Vulkan and Gallium

16/01/2024

One of the key high-level challenges of building Mesa drivers these days is figuring out how to best share code between a Vulkan driver…

Open Since 2005 logo

We use cookies on this website to ensure that you get the best experience. By continuing to use this website you are consenting to the use of these cookies. To find out more please follow this link.

Collabora Ltd © 2005-2024. All rights reserved. Privacy Notice. Sitemap.