Skip to content
systemcoding.dev
Virtualization

Check the KVM API version with KVM_GET_API_VERSION

Open /dev/kvm and call ioctl(fd, KVM_GET_API_VERSION, 0). It returns 12, and the docs tell programs to refuse any other value.

1 min read
Summarize in AI

Every KVM program starts the same way: open /dev/kvm, then check that the kernel speaks the API you were written for. The first call is KVM_GET_API_VERSION.

c
#include <fcntl.h>
#include <linux/kvm.h>
#include <stdio.h>
#include <sys/ioctl.h>

int main(void)
{
    int kvm = open("/dev/kvm", O_RDWR | O_CLOEXEC);
    if (kvm < 0) {
        perror("open /dev/kvm");
        return 1;
    }
    int version = ioctl(kvm, KVM_GET_API_VERSION, 0);
    if (version != KVM_API_VERSION) {
        fprintf(stderr, "unsupported KVM API %d\n", version);
        return 1;
    }
    printf("KVM API version %d\n", version);
    return 0;
}
Text
KVM API version 12

The value has been 12 since the interface was declared stable, in Linux 2.6.22, and the kernel's KVM API documentation says an application should refuse to run on any other version. New features arrive as capabilities, not by bumping this number, so a check for 12 is a sanity test, not a feature test.

The usual failure is not the version. open returns ENOENT when the kvm module is not loaded or virtualisation is off in firmware, and EACCES when your user is not in the kvm group. Both appear before the version call is ever reached.

Also inKernels
Taggedckvm
Share

More snippets

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.