Virtualization in Void Linux using KVM + QEMU + libvirt

Last edited on 2023-05-07 Tagged under  #virtualization   #void   #linux 

Setup a stack of virtualization tools on a Void Linux host for creating and managing virtual machines (VMs).

Let's go!

KVM (Kernel-based Virtual Machine) is built into the Linux kernel and handles the CPU and memory details. QEMU (Quick EMUlator) emulates the various hardware components of a physical machine. Finally, libvirt provides the tools for creating and managing VMs. I use virt-manager and virsh as graphical and console interfaces respectively.

First, check whether the host computer's CPU supports virtualization ...

$ egrep -c '(vmx|svm)' /proc/cpuinfo

A result of 1 or more means the CPU supports virtualization extensions. A result of 0 means it does not, though double-check the BIOS and see if the extensions are available and just need to be enabled.

1. Install

Install packages ...

$ sudo xbps-install libvirt virt-manager qemu polkit

Add user to the kvm and libvirt groups ...

$ sudo usermod -a -G libvirt,kvm <my_username>

Log out and back in.

2. Directories

Default directory to hold VM images is /var/lib/libvirt/images.

Since I have root and home on separate partitions - and I have much more storage space in home - I create an images directory there, plus an isos directory to hold Linux installer images ...

$ mkdir ~/libvirt/{images,isos}
$ chown :kvm ~/libvirt/images
$ sudo rmdir /var/lib/libvirt/images

Create symbolic links to these new directories in /var/lib/libvirt ...

$ sudo ln -s /home/<my_username>/libvirt/images /var/lib/libvirt/images
$ sudo ln -s /home/<my_username/libvirt/isos /var/lib/libvirt/isos

3. Permissions

3.1 libvirt.conf

Create ~/.config/libvirt/libvirt.conf ...

$ mkdir ~/.config/libvirt
$ sudo cp -rv /etc/libvirt/libvirt.conf ~/.config/libvirt/
$ sudo chown foo: ~/.config/libvirt/libvirt.conf

Open the file and set ...

uri_default = "qemu:///system"

3.2 qemu.conf

For storage file permissions, open /etc/libvirt/qemu.conf and set user to <my_username> and group to libvirt ...

user = "<my_username>"
group = "libvirt"

4. Services

Enable necessary services ...

$ sudo ln -s /etc/sv/dbus /var/service/
$ sudo ln -s /etc/sv/polkitd /var/service
$ sudo ln -s /etc/sv/libvirtd /var/service/
$ sudo ln -s /etc/sv/virtlockd /var/service/
$ sudo ln -s /etc/sv/virtlogd /var/service/

5. Virtual machines (VM)

Create a VM using virt-manager. Click the icon to add a new VM, and work through the series of dialog boxes to configure.

Here I've created two: (Debian) bullseye and (Linux) mint ...

virt-manager

Note during the creation of a VM, the network selection option defaults to Virtual network 'default': NAT. If the default network is not active, virt-manager will prompt to start it.

Otherwise, start manually with ...

$ virsh net-start default

Each VM (in default network) will be a member of 192.168.122.0/24, with an IP address in the range of 192.168.122.2 to 192.168.122.254, and are accessible via SSH from the host.

6. Resize VM guest window

In the virt-manager console window, navigate to Edit->Preferences->Console and set:

  • Graphical console scaling to Always
    • Resize guest with window to On

Inside a Debian/Mint/Ubuntu VM, install a spice agent ...

$ sudo apt install spice-vdagent

Reboot the VM.

In the VM guest window, navigate to View->Scale display and check Always and Auto resize VM with window.

Now click-and-drag the window edge to resize display, or use xrandr to set display size.

7. Video acceleration

A Linux Mint VM in virt-manager complained about lack of video acceleration and was very slow.

Using VirtIO its possible to create a virtual 3D accelerated GPU and pass through the hardware capabilities of the VM host's graphic card (which, in my case, is integrated Intel graphics).

With the VM shutdown, open the VM hardware details window. Click on Video QXL and set Model: Virtio, check the box for 3D acceleration, and Apply the modifications.

virtio

Click on Display Spice, set Type: Spice Server and Listen type: None, check the box for OpenGL, and Apply the modifications.

opengl

Start the VM. Much improved!

8. Virsh and virt-clone

Some useful commands:

  • start network - virsh net-start <network_name>
  • list networks - virsh net-list
  • list of VMs - virsh list --all
  • start, reboot, and shutdown VM - virsh start <VM>, virsh reboot <VM>, and virsh shutdown <VM>
  • show IP addresses - virsh net-dhcp-leases <network_name>
  • clone VM and create storage image - virt-clone -o <VM> -n <new_VM> -f /var/lib/libvirt/images/<new_VM>.qcow2

9. Helpful

Thanks for reading! Read other posts?

» Next: Keep LMDE updated and tidy

« Previous: VirtualBox on Linux Mint 21.1