Practical 5: KVM Virtualization Setup
Aim
Install KVM (Kernel-based Virtual Machine) on Linux. Create a virtual machine with the required CPU (Central Processing Unit), memory and disk space. Start it, stop it and take a snapshot. Write down how it connects to the network.
A snapshot or checkpoint depends on the VM (Virtual Machine) disks. It is not an independent backup.
Theory
KVM helps Linux run virtual machines. A virtual machine (VM) is a computer created using software inside a physical computer.
KVM uses processor support called Intel VT-x (Virtualization Technology for x86) or AMD-V (AMD Virtualization). Linux shows this support with the vmx or svm flag. The physical computer running the VMs is called the host.
Your Linux computer is a building. KVM is the builder who divides it into flats (VMs). QEMU (Quick Emulator) gives each flat its fittings (disk, network card, screen). libvirt is the society office that keeps the records and handles requests. virsh is the register at the office where you write "start flat 1", "stop flat 1".
flowchart TB
subgraph Host["Linux host (KVM hypervisor)"]
K["Linux kernel + kvm / kvm_intel modules"]
Q1["qemu-system-x86_64<br/>VM: cc-vm1"] --> K
Q2["qemu-system-x86_64<br/>VM: …"] --> K
L["libvirtd (management daemon)"] -->|controls| Q1 & Q2
B["virbr0 bridge · NAT · DHCP (dnsmasq)"]
end
T["virsh / virt-install / virt-manager"] -->|libvirt API| L
Q1 -->|vnet0| B -->|NAT| N["Physical NIC → LAN / Internet"]
| Component | Role |
|---|---|
| kvm / kvm_intel / kvm_amd | Kernel modules that use VT-x/AMD-V; they create the special file /dev/kvm |
| QEMU | One program per VM; it imitates the devices (disk, network card, display) and uses KVM for the CPU |
| libvirt / libvirtd | A stable management library and background service that defines, starts, stops and snapshots VMs (called domains) |
| virsh | The command-line tool for libvirt |
| virt-install | Creates a new VM from an ISO, an image or a network install |
| qcow2 | QEMU's disk format: uses only as much real space as data written (thin provisioning), supports snapshots |
VM lifecycle with virsh
| Operation | Command | Notes |
|---|---|---|
| List | virsh list --all |
Shows running and switched-off VMs |
| Start | virsh start <vm> |
Turns the VM on |
| Polite stop | virsh shutdown <vm> |
Like pressing the power button – the OS (Operating System) closes properly |
| Force stop | virsh destroy <vm> |
Like pulling the plug – stops immediately (does not delete) |
| Snapshot | virsh snapshot-create-as <vm> <name> "<desc>" |
Saves the disk state (and memory if running) |
| Restore | virsh snapshot-revert <vm> <name> |
Goes back to that saved point |
Networking modes
| Mode | How it works | Who can reach whom |
|---|---|---|
NAT (Network Address Translation) (the default network, virbr0) |
libvirt runs a private network 192.168.122.0/24 with automatic addresses (DHCP (Dynamic Host Configuration Protocol)); VM traffic goes out using the host's IP (Internet Protocol) |
VM → Internet: yes · LAN (Local Area Network) → VM: no (unless you add port forwarding) |
Bridged (br0) |
The VM's network card joins a bridge with the host's real network card | The VM gets its own LAN address – reachable like a real computer |
| Isolated | A virtual network with no way out | Only VM ↔ VM and VM ↔ host |
KVM needs hardware support: egrep -c '(vmx|svm)' /proc/cpuinfo must be greater than 0. The default libvirt network is NAT on virbr0 (192.168.122.0/24).
Requirements
- A 64-bit Linux computer (Ubuntu 24.04) with VT-x/AMD-V enabled in the BIOS (Basic Input/Output System)
- Packages:
qemu-kvm,libvirt-daemon-system,libvirt-clients,bridge-utils,virtinst - An installation ISO file in
/var/lib/libvirt/images
Procedure
Step 1 – Check that the CPU supports virtualization
egrep -c '(vmx|svm)' /proc/cpuinfo # > 0 means supported
If it prints 0, turn on VT-x / AMD-V in the BIOS and try again.
Step 2 – Install KVM and its tools
sudo apt update
sudo apt install -y qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virtinst
sudo systemctl enable --now libvirtd
sudo usermod -aG libvirt,kvm $USER && newgrp libvirt
lsmod | grep kvm
apt install– installs KVM/QEMU, libvirt, bridge tools andvirt-install.systemctl enable --now libvirtd– starts the libvirt service now and at every boot.usermod … && newgrp– lets your user manage VMs withoutsudo.lsmod | grep kvm– you should seekvmandkvm_intel(orkvm_amd).
Step 3 – Create the VM (2 vCPUs, 2048 MiB RAM (Random Access Memory), 20 GiB qcow2 disk)
virt-install --name cc-vm1 --memory 2048 --vcpus 2 \
--disk size=20,format=qcow2 \
--cdrom /var/lib/libvirt/images/ubuntu-24.04-live-server-amd64.iso \
--os-variant ubuntu24.04 --network network=default --graphics vnc
This creates cc-vm1 with 2 CPUs, 2 GB RAM and a 20 GB disk, boots the Ubuntu installer from the ISO, connects it to the default (NAT) network and gives a VNC (Virtual Network Computing) screen.
Step 4 – Look at the VM
virsh list --all
virsh dominfo cc-vm1
virsh domifaddr cc-vm1
list shows it running, dominfo shows its CPUs and memory, domifaddr shows its IP address.
Step 5 – Lifecycle operations
virsh shutdown cc-vm1
virsh snapshot-create-as cc-vm1 base-install "fresh OS"
virsh snapshot-list cc-vm1
virsh start cc-vm1
Shut it down politely → take a snapshot called base-install → list snapshots → start it again.
Step 6 – Write down the networking mode
virsh net-info default # bridge virbr0, NAT
virsh domiflist cc-vm1 # vnet0 → network 'default'
net-info shows the default network uses bridge virbr0; domiflist shows the VM's card vnet0 is connected to default.
Virtual lab · KVM / oVirt · 35 min
Practical 5: Install and configure KVM, provision a VM and manage its lifecycle
Verify hardware virtualization on a Linux host, install KVM/QEMU/libvirt, create a VM with defined CPU, RAM and disk, run it through start/stop/snapshot, and document the NAT networking mode it uses.
Output / Observations
Example output for the settings in this lesson:
$ virsh dominfo cc-vm1
Name: cc-vm1
State: running
CPU(s): 2
Max memory: 2097152 KiB
Autostart: disable
$ virsh domiflist cc-vm1
Interface Type Source Model MAC
--------------------------------------------------------------
vnet0 network default virtio 52:54:00:3a:1c:7e
| Item | Value recorded |
|---|---|
| vCPUs / RAM / disk | 2 / 2048 MiB / 20 GiB qcow2 |
| Snapshot | base-install |
| Network mode | NAT on virbr0, DHCP address in 192.168.122.0/24 |
(2,097,152 KiB = 2,048 MiB = 2 GB.)
Conclusion
Check that KVM is available and the VM starts with the settings you chose. Use virsh to check its state and manage it. Record the snapshot and confirm that the VM uses the default NAT network on virbr0.
Viva questions
- Why does KVM need VT-x or AMD-V? The kvm module uses these CPU features to run guest code directly and catch privileged instructions; without them KVM cannot run hardware-accelerated guests.
- What is the difference between
virsh shutdownandvirsh destroy? shutdown asks the guest OS to power off cleanly (ACPI); destroy immediately stops the QEMU process, like pulling the plug. - What is the role of libvirt? A stable management API (Application Programming Interface) and service used by virsh, virt-manager, oVirt and OpenStack Nova to control hypervisors.
- Why is qcow2 preferred over raw disks? Thin provisioning, copy-on-write, compression and internal snapshots.
- Compare NAT and bridged networking. NAT hides VMs behind the host (outbound only); bridged gives each VM its own address on the physical LAN.
- Where are VM definitions and images stored? The XML (Extensible Markup Language) definitions in
/etc/libvirt/qemu/, disk images in/var/lib/libvirt/images/.
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.