logo

KVM + QEMU + libvirt

The stack:

  • KVM: Linux kernel module that allows a user space program access to the hardware virtualization features of various processors; allows the kernel to function as a hypervisor. KVM itself emulates very little hardware, instead deferring to a higher level client application such as QEMU.
  • QEMU: provides a set of different hardware and device models for the machine, supports the emulation of various architectures; can boot many guest operating systems. QEMU = Quick Emulator.
  • libvirt: the lib to manage KVM, Xen, VMware ESXi, QEMU.
  • virt-manager: using libvirt; included in many linux distros.
  • kubevirt: brining VMs to k8s. Using libvirt + QEMU + KVM.

From Commandline

List VMs

virsh is a CLI tool for libvirt.

$ virsh -c qemu:///system list --all

Each VM started with qemu-system-x86_64 corresponds to a process on the host machine. This means that a list of qemu-system-x86_64 processes corresponds to the list of VMs that are currently running on the host.

$ ps -ef | grep qemu-system-x86_64

This will list all the qemu-system-x86_64 processes, their pids and the parameters used to start the VM.

$ ps -ef | grep qemu-kvm

Create VMs

Emulates Intel x86 64-bit architecture.

$ qemu-system-x86_64

Use virt-install:

$ virt-install --virt-type kvm --name bookworm-amd64 \
--location https://deb.debian.org/debian/dists/bookworm/main/installer-amd64/ \
--os-variant debian12 \
--disk size=10 --memory 1024 \
--graphics none \
--console pty,target_type=serial \
--extra-args "console=ttyS0"

How to enable KVM?

$ kvm-ok

If you KVM is not running, make sure the virtualization features are enabled in BIOS:

  • SVM (Secure Virtual Machine) by AMD
  • Virtualization Technology by Intel
  • IOMMU: Input–output memory management unit

How to install required packages?

On Debian / Ubuntu:

# Install qemu related packages
$ sudo apt install qemu-utils qemu-system-x86 qemu-system-gui

# Install libvirt related packages
$ sudo apt install libvirt-clients libvirt-daemon-system

# Install virtinst (for `virt-install`)
$ sudo apt install virtinst

virsh Commands

# list virtual machines
$ virsh list
 Id   Name       State
--------------------------
 3    debian     running

# list networks
$ virsh net-list
 Name      State    Autostart   Persistent
--------------------------------------------
 default   active   no          yes

# get more info about the network
# (replace `default` with the actual network name)
$ virsh net-info default
Name:           default
UUID:           xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Active:         yes
Persistent:     yes
Autostart:      no
Bridge:         virbr0