Installing KVM/QEMU on Manjaro and Arch Linux

KVM, or Kernel-based Virtual Machine, is a virtualization technology integrated within the Linux kernel module. As an open-source solution, it operates on Linux x86 systems, leveraging hardware virtualization features such as Intel VT and AMD-V. Since its inclusion in Linux kernel version 2.6.20, KVM has also been adapted to other operating systems like FreeBSD and Illumos.

Utilizing KVM transforms a Linux machine into a hypervisor, often referred to as the host. This allows for the creation of multiple isolated virtual machines (VMs), each with its own system environments, which could be Linux, Windows, or BSD, and virtualized hardware such as memory, CPUs, network cards, disks, and graphics.

What is QEMU?

QEMU, standing for Quick Emulator, is an open-source system emulator and virtualizer employed in hardware virtualization. Commonly, QEMU is coupled with the KVM kernel module to execute virtual machines. It maximizes performance by utilizing hardware virtualization extensions such as Intel VT and AMD-V. This QEMU/KVM combination is primarily utilized as a hypervisor in data centers.

This guide outlines the setup process for KVM/QEMU virtualization on a Manjaro/ArchLinux system, and how to create a virtual machine using “virt-manager”, a GUI for managing VMs.

Prerequisites

  • A Manjaro/ArchLinux system with x86 or 64-bit architecture.
  • A CPU/processor supporting virtualization (Intel VT or AMD-V).
  • A non-root user with sudo privileges.

Checking System Architecture and CPU Virtualization Support

First, determine the system architecture and check for hardware virtualization support (Intel VT for Intel CPUs and AMD-V for AMD CPUs).

1. Run this command to verify your system architecture:

uname -m

You will receive output similar to the following:

Linux machine1 5.4.134-1 #1 SMP PREEMPT Tue Jul 06 08:10:03 UTC 2021 x86_64 GNU/Linux

This confirms you are using a Linux system with x86_64 or 64-bit architecture and kernel version 5.4.

2. Next, verify hardware virtualization support with this command:

sudo lscpu | grep Virtualization

If you have an Intel processor, you should see:

Virtualization: VT-x

For AMD processors, the output will be:

Virtualization: AMD-V

3. Optionally, enable nested virtualization with these commands:

sudo modprobe -r kvm_intel
sudo modprobe kvm_intel nested=1

Check if nested virtualization is enabled:

cat /sys/module/kvm_intel/parameters/nested

If the output is “Y” or “1”, nested virtualization is enabled. Otherwise, you may encounter an error like “No such file or directory”.

Installing QEMU and Virt-Manager Packages

1. Install qemu and virt-manager packages using this command:

sudo pacman -S qemu virt-manager libvirt virt-viewer dnsmasq vde2 bridge-utils openbsd-netcat ebtables libguestfs

Manjaro systems may prompt about a conflict between “iptables” and “ebtables“. Press “y” to substitute the default iptables package with “ebtables” and “nftables“.

Key packages include:

  • qemu: An open-source machine emulator and virtualizer.
  • virt-manager: A GUI for managing virtual machines.
  • libvirt: An API for controlling virtualization engines.
  • dnsmasq: A lightweight DNS forwarder and DHCP server.
  • bridge-utils: Tools for Linux ethernet bridge configuration.
  • libguestfs: Tools for modifying VM disk images.

2. Start and enable the libvirtd service with:

sudo systemctl enable --now libvirtd

Expected output:

Created symlink /etc/systemd/system/multi-user.target.wants/libvirtd.service → /usr/lib/systemd/system/libvirtd.service.
Created symlink /etc/systemd/system/sockets.target.wants/virtlockd.socket → /usr/lib/systemd/system/virtlockd.socket.
Created symlink /etc/systemd/system/sockets.target.wants/virtlogd.socket → /usr/lib/systemd/system/virtlogd.socket.
Created symlink /etc/systemd/system/sockets.target.wants/libvirtd.socket → /usr/lib/systemd/system/libvirtd.socket.
Created symlink /etc/systemd/system/sockets.target.wants/libvirtd-ro.socket → /usr/lib/systemd/system/libvirtd-ro.socket.

3. Check libvirtd service status:

sudo systemctl status libvirtd

The output should indicate that the service is active and running:

? libvirtd.service - Virtualization daemon
  Loaded: loaded (/usr/lib/systemd/system/libvirtd.service; disabled; vendor preset: disabled)
  Active: active (running) since Fri 2021-07-23 10:33:25 UTC; 6s ago
TriggeredBy: ? libvirtd-ro.socket
            ? libvirtd.socket
            ? libvirtd-admin.socket
     Docs: man:libvirtd(8)
           https://libvirt.org
   Main PID: 16828 (libvirtd)
      Tasks: 19 (limit: 32768)
     Memory: 16.4M
        CPU: 226ms
     CGroup: /system.slice/libvirtd.service
             ??16828 /usr/bin/libvirtd --timeout 120

Allow Non-root User to Use KVM/QEMU Virtualization

Typically, only the root user can manage virtual machines. To enable non-root users to create and configure VMs, adjust the libvirtd configuration as follows:

1. Open the libvirtd configuration file:

sudo nano /etc/libvirt/libvirtd.conf

Uncomment “unix_sock_group” and set the group to “libvirt“:

# Set the UNIX domain socket group ownership. This can be used to
# allow a 'trusted' set of users access to management capabilities
# without becoming root.
#
# This setting is not required or honoured if using systemd socket
# activation.
#
# This is restricted to 'root' by default.
unix_sock_group = "libvirt"

Next, uncomment “unix_sock_rw_perms” and keep the default permission “0770“:

# Set the UNIX socket permissions for the R/W socket. This is used
# for full management of VMs
#
# This setting is not required or honoured if using systemd socket
# activation.
#
# Default allows only root. If PolicyKit is enabled on the socket,
# the default will change to allow everyone (eg, 0777)
#
# If not using PolicyKit and setting group ownership for access
# control, then you may want to relax this too.
unix_sock_rw_perms = "0770"

Save your changes with Ctrl+x, then y, followed by Enter.

2. Add your user to the “libvirt” group:

sudo usermod -a -G libvirt username

3. Restart the libvirtd service:

sudo systemctl restart libvirtd

All users in the “libvirt” group can now create and manage virtual machines.

Verify QEMU/KVM Installation with Virt-Manager

Launch “virt-manager” from your application menu.

1. Access “Edit -> Connection Details”:

Check Connection details of QEMU/KVM

2. The Overview tab will indicate an automatic connection to “qemu:///system“:

virt-manager will automatically connect to the QEMU/KVM within the system

3. Check the Virtual Networks tab for “default” network settings:

  • Interface: virbr0
  • Auto start at boot: yes
  • IP address: 192.168.122.0/24
  • Range DHCP IP address: 192.168.122.2 – 192.168.122.254
  • Network Type: NAT

Default network configuration for QEMU/KVM with NAT

4. Go to Storage and check the “default” storage pool:

  • Type: Filesystem directory
  • Size: Varies per disk
  • Location: /var/lib/libvirt/images
  • Auto start at boot: yes

VM images are stored in “/var/lib/libvirt/images“:

Default QEMU/KVM Storage

5. Click “+” to create new ISO storage:

  • Name: ISO
  • Type: dir: Filesystem Directory
  • Target Path: /path/directory/to/your/iso/

Create new storage for ISO files operating systems

Finalize with “Finish“. You’ll now be ready to create VMs.

Create New Virtual Machine using Virt-Manager

1. In virt-manager, click “+” to create a VM:

Create new virtual machine with virt-manager

2. Select “Local install media“, then “Forward“:

Select local media ISO type installation

3. Use “Browse” to find the ISO file:

Browse ISO operating system you want to install

Select “ISO” storage pool, choose your ISO (e.g., “Debian 10“), then “Choose Volume“:

Select OS ISO file for installation example Debian 10

Uncheck “Automatically detect from installation media/source“, specify the OS (e.g., “Debian 10“), and proceed with “Forward“:

Choose type of operating system you want to install

4. Allocate memory and CPUs, then “Forward“:

memory and CPU configuration for virtual machine

5. Define the disk space, then “Forward“:

Choose size disk for virtual machine

6. Review your configuration and click “Finish” to install:

Confirm to create and install virtual machine

7. Your VM should now be operational and ready for OS installation:

Virtual Machine is up and running with QEMU/KVM virtualization

Conclusion

Congratulations! You have successfully configured QEMU/KVM virtualization on a Manjaro/ArchLinux system and learned how to create virtual machines using the virt-manager application. With this setup, you can now explore different operating systems, such as other Linux distributions, Windows, or BSD derivatives, on your virtual environment.

FAQ

What is the difference between KVM and QEMU?

KVM is a Linux kernel module that enables hardware-accelerated virtualization. QEMU is a hardware emulator and virtualizer which, when paired with KVM, uses the hardware acceleration capabilities to improve performance.

Can I use this guide on other Linux distributions?

While this guide is tailored for Manjaro/ArchLinux, the overall process applies to most Linux distributions with slight variations in package management and dependencies.

Do I need a high-performance CPU to run KVM/QEMU?

A CPU with virtualization support (Intel VT or AMD-V) is recommended. However, the host system’s overall performance may vary depending on the virtual machines’ workload demands.

Is it safe to uninstall iptables in favor of ebtables and nftables?

For Manjaro systems encountering package conflicts, iptables can be replaced with ebtables/nftables to ensure compatibility with the virtualization setup. Ensure proper firewall configurations are maintained post-installation.

How can I manage remote virtual machines with virt-manager?

Virt-manager supports remote connection to different hosts. Use the “File -> Add Connection” menu option and configure the details of the remote host for management.