Virtualization Sneak Peek
Putting It All Together
I assume you have cloned the kvmtool Git repository already, so let's go straight to the build process. Alternatively, you can install kvmtool from the distribution's repositories with sudo apt-get install kvmtool
or something similar. Kvmtool has minimum dependencies. If you need a GUI, as I do in this example, you'd want SDL or GTK+ 3.x development headers. I opted for the former:
sudo apt-get install libsdl-dev
Building kvmtool is as simple as typing make
. There are no configuration steps as, again, build dependencies are minimal. Compilation takes a few seconds, and you get a command named lkvm
as the result. Supposedly, "l" stands for lightweight. The initial name was just kvm, but Qemu had the same, so kvmtool was renamed. Alternatively, you can run vm
, which is just an alias.
For usage summary, call ./lkvm --help
. You see it implements various sub-commands, one per builtin-*.c
file we just dissected. To start a VM, you need ./lkvm run
. This is already a working command that opens a simple shell, with some caveats. It runs the guest on the same kernel as the host, so the image (/boot/vmlinuz-$version
) must be readable. On my Ubuntu 16.04, this requires root privileges (hence, you need sudo
). Moreover, kvmtool needs the guest kernel to support certain features (notably, virtio), which are often shipped as modules. So, you'd need to tell lkvm
where the initial RAM drive (initrd) is.
Last but not least, lkvm
needs a disk image to run. The default is a small rootfs, which lkvm
builds in the ~/.lkvm/default
directory in the host and exports as a 9P filesystem (see the "What on Earth Is 9P?" box).
What on Earth Is 9P?
Kvmtool understands two types of disk images. It could be a file storing a bytewise copy of a real hard drive or SSD. Or, it could be a directory in the host filesystem exported via 9P.
9P, also known as Plan 9 Filesystem Protocol is a remote resource access protocol. As the name suggests, it originates from the Plan 9 operating system. The latter is sometimes loosely described as "Unix done right." In Unix, everything is a file. Plan 9 takes this concept to a whole new level: Kernel interfaces are files. Windows are files. And files are files, too. No wonder Plan 9 needs a good protocol to access files over the network. 9P is just that.
9P itself is network-agnostic. The only thing it needs is a reliable, in-order transport. This means messages shouldn't disappear or arrive out of the order in which they were sent. TCP/IP is okay for 9P, as well as shared memory or virtio channels. Linux has implemented 9P for a while, so it seems a natural choice for remote file access in virtio-enabled VMs.
If you ever used Shared Folders in VirtualBox, 9P serves similar purposes, yet it comes with some pedigree.
Another option is to use one of the testing images Qemu provides [5]. I'd go this route and download linux-0.2.img
. With all these bits in place, a complete command to run a VM could be as follows:
./lkvm run --kernel /boot/vmlinuz-4.8.0-46-generic --initrd /boot/initrd.img-4.8.0-46-generic --disk /boot/linux-0.2.img --cpus 2 --sdl
Note that lkvm
itself doesn't need superuser privileges, but you'd want to prefix this command with sudo
if your kernel image or initrd requires privileges to read, as explained previously. Here, --cpus 2
sets the number of vCPUs your guests will have, and --sdl
tells kvmtool to open it in an SDL window (Figure 3).

The command will also print a line on a terminal where you started it, like this:
# lkvm run -k /boot/vmlinuz-4.8.0-46-generic -m 320 -c 2 --name guest-4361
Take note of --name
: You'll need it to manage the VM. For example, ./lkvm stop -n guest-4361
will shut down the guest gracefully. Here, the name was autogenerated, but as in this example,
lkvm run --name linux-0.2 <other arguments follow>
you can also assign a VM something more descriptive.
Command of the Month: lkvm debug
Although kvmtool is great for learning, I still tend to run Qemu/KVM in production. Reports are though that kvmtool addresses one specific use-case particularly well. I'm talking now about early boot-time debugging.
You can already specify some debugging switches to lkvm run. Say, you want to enable single-step mode. Just add --debug-single-step, and kvmtool will dump the system state (Figure 4) after every machine code instruction. Naturally, that would make a guest really slow.
Then there is a dedicated debugging sub-command named (you guessed it) debug. Typically, you supply it a guest name (either the one you assigned with --name or autogenerated) and an action to do, like this:
./lkvm debug --name guest-4849 --dump
This makes kvmtool dump the guest system state (again, see Figure 4). Internally, this command sends a SIGUSR1 signal to the vCPU thread, which causes it to request the guest state (such as registers) from the kernel-side KVM via an ioctl.
It is also possible to signal a non-maskable interrupt (NMI) to the guest:
./lkvm debug --name guest-4849 --nmi 0
The integer argument is a vCPU number to send an NMI to. Non-maskable interrupts are a serious weapon, and Linux would complain if it came unexpectedly:
[ 45.364335] Uhhuh. NMI received for unknown reason 20 on CPU 0. [ 45.364968] Do you have a strange power saving mode enabled? [ 45.365467] Dazed and confused, but trying to continue
Otherwise, they can be an only option to solicit a feedback from an otherwise irresponsible kernel.
Infos
- Virtio v1.0: http://docs.oasis-open.org/virtio/virtio/v1.0/virtio-v1.0.html
- Kvmtool homepage: https://git.kernel.org/pub/scm/linux/kernel/git/will/kvmtool.git/about/
- Running rkt with KVM stage1: https://coreos.com/rkt/docs/latest/running-kvm-stage1.html
- 7 ways we harden our KVM hypervisor at Google Cloud: https://cloudplatform.googleblog.com/2017/01/7-ways-we-harden-our-KVM-hypervisor-at-Google-Cloud-security-in-plaintext.html
- Testing/System Images at Qemu wiki: http://wiki.qemu-project.org/Testing/System_Images
« Previous 1 2
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.
News
-
elementary OS 7.1 Now Available for Download
The team behind elementary OS has released the latest version of its operating system with a focus on personalization, inclusivity, accessibility, and privacy.
-
The GNU Project Celebrates Its 40th Birthday
September 27 marks the 40th anniversary of the GNU Project, and it was celebrated with a hacker meeting in Biel/Bienne, Switzerland.
-
Linux Kernel Reducing Long-Term Support
LTS support for the Linux kernel is about to undergo some serious changes that will have a considerable impact on the future.
-
Fedora 39 Beta Now Available for Testing
For fans and users of Fedora Linux, the first beta of release 39 is now available, which is a minor upgrade but does include GNOME 45.
-
Fedora Linux 40 to Drop X11 for KDE Plasma
When Fedora 40 arrives in 2024, there will be a few big changes coming, especially for the KDE Plasma option.
-
Real-Time Ubuntu Available in AWS Marketplace
Anyone looking for a Linux distribution for real-time processing could do a whole lot worse than Real-Time Ubuntu.
-
KSMBD Finally Reaches a Stable State
For those who've been looking forward to the first release of KSMBD, after two years it's no longer considered experimental.
-
Nitrux 3.0.0 Has Been Released
The latest version of Nitrux brings plenty of innovation and fresh apps to the table.
-
Linux From Scratch 12.0 Now Available
If you're looking to roll your own Linux distribution, the latest version of Linux From Scratch is now available with plenty of updates.
-
Linux Kernel 6.5 Has Been Released
The newest Linux kernel, version 6.5, now includes initial support for two very exciting features.