Skip to content
VirtualLP Virtual Learning Platform

Virtualization using KVM and Creating Virtual Machines

16 min read Topic 8 of 34 1 hands-on lab

KVM (Kernel-based Virtual Machine) helps Linux run virtual machines. Each VM can have its own operating system, memory, disk and network settings.

The physical Linux computer is the host. The system inside a VM is the guest. This lesson shows how to check the host, install the tools and create a guest.

team

Think of building a VM as a team job:

  • KVM (in the Linux kernel) is the engine – it runs the VM's instructions directly on the real CPU (Central Processing Unit), very fast.
  • QEMU (Quick Emulator) is the body of the car – it gives the VM a virtual disk, network card, screen and BIOS (Basic Input/Output System).
  • libvirt is the dashboard and steering wheel – one easy way to start, stop and set up VMs.
  • virsh / virt-install / virt-manager are the buttons you press.
KVM

KVM is part of the Linux kernel that helps run virtual machines. It uses processor support such as Intel VT-x (Virtualization Technology for x86) or AMD-V (AMD Virtualization). Each VM is managed as a Linux process.

KVM architecture

flowchart TB
    subgraph User[User space]
        VM1[VM 1<br/>QEMU process]:::vm
        VM2[VM 2<br/>QEMU process]:::vm
        LV[libvirtd<br/>management daemon]
        T[virsh · virt-install · virt-manager · OpenStack Nova · oVirt]
        T --> LV --> VM1 & VM2
    end
    subgraph Kernel[Linux kernel]
        K[kvm.ko + kvm-intel.ko / kvm-amd.ko]
        S[Scheduler · memory manager · drivers]
    end
    VM1 & VM2 -->|/dev/kvm ioctl| K
    K --> HW[Hardware with VT-x / AMD-V]
    classDef vm fill:#eef3ff,stroke:#2747d4
Part What it does
kvm.ko The main kernel module that provides virtualization.
kvm-intel.ko / kvm-amd.ko The CPU-specific module – uses Intel VT-x or AMD-V.
/dev/kvm A special file; programs use it to create and run VMs.
QEMU Gives each VM its virtual hardware – disks, network cards, display, BIOS/UEFI (Unified Extensible Firmware Interface). With KVM, the CPU work runs directly on the real CPU; QEMU only handles the devices.
libvirt A management library + background service (libvirtd). It controls KVM (and also Xen, LXC and more) in one common way, and stores each VM's settings as XML (Extensible Markup Language).
virsh / virt-install / virt-manager The command-line and graphical tools built on libvirt.
virtio Special fast drivers for disk (virtio-blk), network (virtio-net) and memory (ballooning) – much faster than pretending to be old real hardware.
Why is KVM "type 1"?

Linux is a normal operating system, but once KVM is loaded, the kernel itself is the hypervisor running directly on the hardware. Each VM is a normal Linux process, so it gets Linux's scheduling, memory management, security (SELinux/sVirt) and drivers for free.

Features of KVM

  • Uses the CPU's virtualization support – almost the same speed as real hardware.
  • Live migration – move a running VM to another server with no downtime.
  • Overcommitment – give VMs more total CPU/RAM (Random Access Memory) than physically exists (because they rarely use it all at once). KSM (Kernel Same-page Merging) stores identical memory pages only once to save RAM.
  • Thin-provisioned disks with the qcow2 format – a 20 GB disk only uses as much real space as data written; supports snapshots.
  • Security with SELinux / sVirt, which keeps each VM process locked in.
  • Runs Linux, Windows and BSD guests.
  • Open source and free.

A snapshot or checkpoint depends on the VM disks. It is not an independent backup.

Step 1 – check hardware support

The CPU must support virtualization. Intel calls it vmx, AMD calls it svm.

# Count CPU flags for Intel (vmx) or AMD (svm) virtualization
egrep -c '(vmx|svm)' /proc/cpuinfo
# 0  → not supported or disabled in BIOS
# >0 → supported (number of logical CPUs)

# Another way (Ubuntu)
sudo apt install cpu-checker
kvm-ok
# INFO: /dev/kvm exists
# KVM acceleration can be used

If the result is 0, turn on Intel VT-x / AMD-V (SVM (Secure Virtual Machine)) in the computer's BIOS/UEFI settings.

Step 2 – install KVM (Ubuntu / Debian)

sudo apt update
sudo apt install -y qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virtinst virt-manager

# Allow the current user to manage VMs without sudo
sudo usermod -aG libvirt,kvm $USER

# Start the libvirt service now and on every boot
sudo systemctl enable --now libvirtd
systemctl status libvirtd

# Check that the kernel modules are loaded
lsmod | grep kvm
# kvm_intel   372736  0
# kvm        1036288  1 kvm_intel

What each step does:

  1. apt install – installs KVM/QEMU, libvirt, network bridge tools, virt-install and the virt-manager GUI (Graphical User Interface).
  2. usermod – adds you to the libvirt and kvm groups so you can manage VMs (log out and in again after this).
  3. systemctl enable --now – starts the libvirt service and makes it start automatically.
  4. lsmod | grep kvm – confirms the KVM modules are running.

On RHEL / CentOS / Rocky Linux: sudo dnf install @virtualization then sudo systemctl enable --now libvirtd.

Step 3 – networking

By default, libvirt creates a network named default using NAT (Network Address Translation). Its virtual bridge is virbr0, usually on 192.168.122.0/24. VMs can make outgoing Internet connections. Other computers cannot start connections to them through NAT unless extra access is set up.

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

If VMs must appear directly on the physical LAN (Local Area Network) (with their own LAN address), create a bridge (for example br0) connected to the host's network card.

Step 4 – create a virtual machine with virt-install

# Download an installation ISO first, then:
sudo virt-install \
  --name ubuntu-vm1 \
  --memory 2048 \
  --vcpus 2 \
  --disk path=/var/lib/libvirt/images/ubuntu-vm1.qcow2,size=20,format=qcow2,bus=virtio \
  --cdrom /var/lib/libvirt/images/ubuntu-24.04-live-server-amd64.iso \
  --os-variant ubuntu24.04 \
  --network network=default,model=virtio \
  --graphics vnc,listen=0.0.0.0 \
  --noautoconsole

This creates a VM named ubuntu-vm1 with 2 GB RAM, 2 CPUs and a 20 GB disk, and boots the Ubuntu installer from the ISO.

Option Meaning
--name Name of the VM (libvirt calls VMs domains)
--memory RAM in MiB (2048 = 2 GB)
--vcpus Number of virtual CPUs
--disk Virtual disk: path, size (GiB), format and bus
--cdrom / --location Where to install from (ISO file or URL (Uniform Resource Locator))
--os-variant Tells libvirt the guest OS (Operating System) so it picks good defaults (osinfo-query os lists the values)
--network Which virtual network or bridge, and the network-card model
--graphics Console type – VNC (Virtual Network Computing), SPICE (Simple Protocol for Independent Computing Environments) or none (text/serial)
--import Boot an existing disk image instead of installing

Creating a disk separately:

qemu-img create -f qcow2 /var/lib/libvirt/images/data.qcow2 10G
qemu-img info /var/lib/libvirt/images/data.qcow2

Step 5 – manage VMs with virsh

Command Purpose
virsh list --all List running and stopped VMs
virsh start vm1 Turn on a VM
virsh shutdown vm1 Polite shutdown (like pressing the power button once)
virsh destroy vm1 Force power-off (like pulling the plug) – it does not delete the VM
virsh reboot vm1 Restart
virsh suspend vm1 / virsh resume vm1 Pause / continue (stays in memory)
virsh autostart vm1 Start automatically when the host boots
virsh dominfo vm1 Show CPU, memory, state, autostart
virsh dumpxml vm1 Show the full XML definition
virsh edit vm1 Edit the XML definition
virsh console vm1 Connect to the text console (press Ctrl+] to leave)
virsh domifaddr vm1 Show the VM's IP (Internet Protocol) address
virsh setvcpus vm1 4 --config Change the number of vCPUs
virsh setmaxmem vm1 4G --config / setmem Change memory
virsh snapshot-create-as vm1 snap1 "before update" Take a snapshot
virsh snapshot-list vm1 / snapshot-revert vm1 snap1 List / go back to a snapshot
virsh undefine vm1 --remove-all-storage Delete the VM and its disks
virt-clone --original vm1 --name vm2 --auto-clone Make a copy (clone) of a VM
$ virsh list --all
 Id   Name         State
-----------------------------
 1    ubuntu-vm1   running
 -    centos-vm2   shut off
shutdown vs destroy

virsh shutdown asks the guest OS to shut down gracefully (saves work, closes programs). virsh destroy cuts the power immediately. Neither deletes the VM – virsh undefine deletes it.

The domain XML

Every libvirt VM is described by an XML file – the same kind of file OpenStack and oVirt create behind the scenes:

<domain type='kvm'>
  <name>ubuntu-vm1</name>
  <memory unit='MiB'>2048</memory>
  <vcpu>2</vcpu>
  <os><type arch='x86_64' machine='q35'>hvm</type><boot dev='hd'/></os>
  <devices>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/var/lib/libvirt/images/ubuntu-vm1.qcow2'/>
      <target dev='vda' bus='virtio'/>
    </disk>
    <interface type='network'>
      <source network='default'/>
      <model type='virtio'/>
    </interface>
    <graphics type='vnc' port='-1'/>
  </devices>
</domain>

Read the XML settings as a form: VM name, memory, CPU count, boot device, disk and network card. The example uses 2048 MiB of memory, 2 vCPUs and a qcow2 disk.

Using virt-manager (GUI)

virt-manager does the same things with a wizard:

  1. File → New Virtual Machine
  2. Local install media → choose the ISO
  3. Set memory and CPUs
  4. Set the disk size
  5. Give a name and choose the network
  6. Finish – a graphical console opens so you can complete the OS installation.

Virtual lab · KVM / oVirt · 35 min

Install KVM and create virtual machines with virt-install and virsh

On an Ubuntu server, verify hardware virtualization, install the KVM/libvirt stack, create a VM from an ISO with virt-install and manage its full lifecycle with virsh – exactly as you would on a real cloud compute node.

Open lab

Live migration

virsh migrate --live ubuntu-vm1 qemu+ssh://host2.example.com/system

The VM's memory is copied to the other server while the VM keeps running. Only at the end is there a tiny pause to copy the last changed memory pages. If both servers use shared storage (NFS (Network File System), Ceph), the disk does not need to be copied at all.

Summary

  • KVM = Linux kernel modules (kvm.ko, kvm-intel/amd.ko) + QEMU (virtual devices) + libvirt (management).
  • Check support with egrep -c '(vmx|svm)' /proc/cpuinfo or kvm-ok.
  • Install qemu-kvm libvirt virtinst, start libvirtd, add your user to the libvirt/kvm groups.
  • Create VMs with virt-install; manage them with virsh (list, start, shutdown, destroy, snapshot, undefine).
  • VMs are libvirt domains described in XML; virtio drivers give near-native speed.

Practice questions

  1. What is KVM? Explain its architecture with a diagram.
  2. Explain the roles of QEMU and libvirt in a KVM environment.
  3. How do you check whether a processor supports hardware virtualization?
  4. Write the commands to install KVM and create a VM using virt-install. Explain each option.
  5. List and explain ten virsh commands.
  6. What is the difference between virsh shutdown and virsh destroy?
  7. What is live migration? How is it performed in KVM?

Practise this topic

Hands-on virtual lab

Apply what you just read in a realistic, guided simulation. Free with a verified account.

Create a free account to track your progress and unlock virtual labs.