Raspberry Pi as a KVM hypervisor



Introduction

The Raspberry Pi has long transcended its original role as a simple board for enthusiasts: today it is a fully functional mini-computer capable of performing complex tasks, including hardware virtualization. This article provides a step-by-step guide to installing Raspberry Pi OS Lite, configuring SSH access, updating the system, installing KVM support, setting up the Cockpit web interface, and creating and running a guest operating system using KVM. This approach allows for transforming the Raspberry Pi into a convenient platform for testing and learning in the field of virtualization.

Installing Raspberry Pi OS Lite

Using the Raspberry Pi Imager utility, we install the Raspberry Pi OS Lite (64) operating system on a micro SD card. During the installation phase, you need to change the configuration so that you can then work with the Raspberry Pi remotely by connecting via SSH.

  • Hostname
  • Username and password
  • Wi-Fi settings
  • Enable SSH

We boot the Raspberry Pi from this memory card and check if it is connected to the network, if not, then we initialize the process of configuring the network connection by calling the system configurator:

sudo raspi-config

Where we select General settingswireless settings and enter the name of the access point and password. Make sure that the connection has taken place and proceed to the next step – updating the system.

System Update

Connecting to the Raspberry Pi via SSH

ssh [email protected]

We update the OS to the latest version and restart the Raspberry Pi

sudo apt update && sudo apt full-upgrade -y
sudo reboot

Installing KVM + libvirt

Starting with the Linux kernel 5.10+ (and now Raspberry Pi OS uses 6.x), support for KVM hardware virtualization for ARM64 is included directly in the kernel. On Raspberry Pi OS (64-bit, desktop, or lite), KVM is activated automatically as soon as the device supports hardware virtualization.

This was made possible by the fact that the Raspberry Pi Foundation integrated KVM support directly into the upstream kernel for its ARM64 platforms. They focus on the fact that the user can immediately experiment with virtualization – without additional configuration.

However, it is better to check this by running the following commands:

if the file /dev/kvm exists – support is active

ls /dev/kvm
/dev/kvm

will show the KVM startup log

dmesg | grep -i kvm
[    1.078559] kvm [1]: nv: 554 coarse grained trap handlers
[    1.084177] kvm [1]: IPA Size Limit: 40 bits
[    1.088486] kvm [1]: GICV region size/alignment is unsafe, using trapping (reduced performance)
[    1.097272] kvm [1]: vgic interrupt IRQ9
[    1.101231] kvm [1]: VHE mode initialized successfully
[    3.526966] systemd[1]: Hostname set to <kvm>

Since kvm works by default, you need to install libvirt

sudo apt install libvirt-daemon-system libvirt-clients bridge-utils virtinst

Checking the operation of the service:

systemctl status libvirtd
● libvirtd.service - Virtualization daemon
     Loaded: loaded (/lib/systemd/system/libvirtd.service; enabled; preset: enabled)
     Active: active (running) since Thu 2025-05-08 10:42:46 BST; 25s ago
TriggeredBy: ● libvirtd-admin.socket
             ● libvirtd-ro.socket
             ● libvirtd.socket
       Docs: man:libvirtd(8)
             https://libvirt.org
   Main PID: 4708 (libvirtd)
      Tasks: 19 (limit: 32768)
        CPU: 138ms
     CGroup: /system.slice/libvirtd.service
             └─4708 /usr/sbin/libvirtd --timeout 120

May 08 10:42:46 kvm systemd[1]: Starting libvirtd.service - Virtualization daemon...
May 08 10:42:46 kvm systemd[1]: Started libvirtd.service - Virtualization daemon.

Change user permissions

You need to add a user to the libvirt and kvm groups. Since we created the kvm user in the first step, there is no need to add it to the kvm group, but if you created another user in the initial step, you need to add it to the kvm group.

sudo usermod -aG libvirt kvm

After adding a user to the libvirt group, in order not to reboot the system, you can activate the new permissions with the command:

newgrp libvirt

Installing the Cockpit

Cockpit – це веб-інтерфейс для керування Linux-серверами, і один із його плагінів — Cockpit Machines — є надбудовою саме для KVM

sudo apt install cockpit cockpit-machines

Checking the operation of the service:

sudo systemctl status cockpit.socket
● cockpit.socket - Cockpit Web Service Socket
     Loaded: loaded (/lib/systemd/system/cockpit.socket; enabled; preset: enabled)
     Active: active (listening) since Thu 2025-05-08 10:59:18 BST; 46s ago
   Triggers: ● cockpit.service
       Docs: man:cockpit-ws(8)
     Listen: [::]:9090 (Stream)
      Tasks: 0 (limit: 9561)
        CPU: 12ms
     CGroup: /system.slice/cockpit.socket

May 08 10:59:18 kvm systemd[1]: Starting cockpit.socket - Cockpit Web Service Socket...
May 08 10:59:18 kvm systemd[1]: Listening on cockpit.socket - Cockpit Web Service Socket.

The service is active, but in listening mode, you need to activate it

sudo systemctl status cockpit.socket
● cockpit.socket - Cockpit Web Service Socket
     Loaded: loaded (/lib/systemd/system/cockpit.socket; enabled; preset: enabled)
     Active: active (running) since Thu 2025-05-08 10:59:18 BST; 3min 24s ago
   Triggers: ● cockpit.service
       Docs: man:cockpit-ws(8)
     Listen: [::]:9090 (Stream)
      Tasks: 0 (limit: 9561)
        CPU: 12ms
     CGroup: /system.slice/cockpit.socket

May 08 10:59:18 kvm systemd[1]: Starting cockpit.socket - Cockpit Web Service Socket...
May 08 10:59:18 kvm systemd[1]: Listening on cockpit.socket - Cockpit Web Service Socket.

Now you can follow the link to port 9090 in your browser: https://192.168.99.181:9090/

Guest OS Test

You need to log in through the Cockpit web interface using the username and password of the server user. The first thing the user will see is a dashboard with general information about the server

For versatility, I switched the interface to English, I like to work that way more. So, to create an OS, you need to go to the Virtual machines menu and click on the Create VM button, the Create new virtual machine window will open, where you need to select the desired OS parameters

  • Name: Test_OS
  • Installation type: Download an OS
  • Operating system: select AlmaLinux 9
  • Storage: Create new volume
  • Storage limit: 10 Gib
  • Memory: 1 Gib

To start the build, click create and run, but after a moment you will get an error:

Creation of VM Test_OS failed
ERROR Requested operation is not valid: network ‘default’ is not active Domain installation does not appear to have been successful. If it was, you can restart your domain by running: virsh –connect qemu:///system start Test_OS otherwise, please restart your installation.

This error means that the virtual machine cannot be created because the default network is inactive. Libvirt creates a virtual network named default by default, but it may be inactive or not automatically started at startup. To fix this, you need to enable the network and add it to autostart:

sudo virsh net-start default
Network default started

sudo virsh net-autostart default
Network default marked as autostarted

Let’s check the status of the default network:

sudo virsh net-list --all
 Name      State    Autostart   Persistent
--------------------------------------------
 default   active   yes         yes

Now let’s try to re-run the creation of the AlmaLinux 9 guest OS

Click create and run again, and the image is mounted successfully, which indicates that the service is working correctly. The corresponding entry appears in the list of virtual machines.

To define and change the settings of a virtual OS, or to access OS management, at least for the logical completion of the installation, you must select the required OS from the list and then the user will be taken to the dashboard of this OS with its detailed characteristics.

Of course, in order for all images to work correctly, it is better to download them separately for the arm architecture to avoid conflicts and errors. After receiving the image, you can install it by selecting the Local install media (ISO image or distro install tree) option.

Conclusion

Using Raspberry Pi with Raspberry Pi OS Lite (64-bit), you can deploy a full-fledged virtualization environment thanks to KVM and Cockpit. The setup process is relatively simple, and the result is a stable system that can be managed via a web interface. Pay attention to the architecture of the guest OS images, download ARM versions, and as a result, you will have an efficient and convenient solution for running virtual machines directly on the Raspberry Pi.