Linux › Section Q · Virtualization › Topic Q.1

Virtual machines

A virtual machine is a whole computer built in software: processor, memory, disks and network, with its own operating system and kernel. Virtual machines power the cloud, make safe test labs, and let Windows and Linux share one PC.

What virtualisation means

Virtual means an intermediary that makes something complicated or shared look simple and private. Virtual memory, from "The kernel: processes, memory, drivers, and system calls", gives each process what looks like its own memory. A system virtual machine applies the same trick to an entire computer, so a complete operating system, kernel included, runs inside it unaware that it is sharing hardware. IBM mainframes have worked this way since the 1970s. A machine can be built purely in software, called an emulator, which can even imitate a different processor, but that is slow. Practical virtual machines run most instructions directly on the real processor.

Hypervisors, hosts and guests

The software that creates and manages virtual machines is the hypervisor, or virtual machine monitor. A type 1 hypervisor runs directly on the hardware as a small dedicated operating system; Xen, VMware ESXi and Microsoft Hyper-V are examples. A type 2 hypervisor runs as a program on an ordinary operating system, as VirtualBox does. Linux's own KVM blurs the line: it is a kernel module that turns Linux itself into a hypervisor, usually paired with QEMU for device emulation and libvirt for management. Each virtual machine with its operating system is a guest; whatever runs the hypervisor is the host.

Help from the processor

A guest kernel expects kernel mode, but the hypervisor cannot simply hand it real control of the machine. Early x86 hypervisors ran the guest's kernel without full privilege and trapped each restricted instruction, emulating it in software; most instructions ran untouched, so this worked surprisingly well. Intel and AMD then added hardware support, called VT-x and AMD-V, which removes the need for those tricks. KVM requires it, and firmware settings sometimes switch it off.

Worked example: is this host ready for KVM?

Before creating virtual machines on a Linux host, check each layer in turn. 1. Processor: grep -E -c 'vmx|svm' /proc/cpuinfo counts lines with the Intel (vmx) or AMD (svm) flag; 0 means no support or support disabled in the firmware. 2. Kernel: lsmod | grep kvm should list kvm and either kvm_intel or kvm_amd; if not, load one with sudo modprobe kvm_intel or kvm_amd. 3. Device: ls -l /dev/kvm should show the device node, usually owned by the kvm group. 4. Permission: id should list kvm or libvirt among your groups. 5. libvirt's virt-host-validate runs similar checks and reports PASS or FAIL for each.

Virtual hardware and paravirtualisation

The simplest virtual disk is a large file on the host presented to the guest as a disk, with the hypervisor imitating a real controller. Imitating real hardware precisely is slow, so most virtual machines use paravirtualisation: the guest runs drivers that know they are virtual and talk to the hypervisor directly. On KVM these are the virtio devices. A Linux guest sees virtio disks as /dev/vda, /dev/vdb and so on, and a virtio network card; Xen guests see /dev/xvda. Guest additions, such as those for VirtualBox, add conveniences like shared folders and smooth mouse movement. To the guest's own tools, a virtual disk is still a block device to partition and format.

Common uses

Testing is the classic use: try a new distribution, a risky upgrade or untrusted software in a disposable machine, and restore a snapshot when it goes wrong. Compatibility is another: run an operating system different from your own, such as Windows software on a Linux host. Servers are the largest: every major cloud sells virtual machines, called instances, on type 1 hypervisors. Amazon Web Services began on Xen and has moved most instance types to its KVM-based Nitro system, which shows that the choice of hypervisor matters less than the model itself. A web service you use today is very likely to be running inside a virtual machine.

Linux on Windows: WSL

Windows Subsystem for Linux lets Linux distributions run on Windows 10 and 11. The original WSL 1 translated Linux system calls into Windows ones. WSL 2, the default, instead runs a real Linux kernel in a lightweight virtual machine on Hyper-V, so almost all Linux software works, including Docker. wsl --install sets it up with Ubuntu by default, wsl -l -v lists installed distributions with their WSL version, and wsl --version shows component versions, including the Microsoft-built Linux kernel, which Windows Update keeps current. wsl --set-version followed by a distribution name and 2 converts a WSL 1 distribution. Graphical Linux applications also work, through WSLg.

Worked example: installing from an ISO

Create a test Debian virtual machine on a Linux host with KVM. 1. Download the installer ISO and the project's SHA256SUMS file; run sha256sum -c SHA256SUMS --ignore-missing and expect OK beside the ISO name. 2. Confirm KVM is ready: /dev/kvm exists. 3. Create the machine with libvirt's virt-install: --name lab, --memory 2048 (MiB), --vcpus 2, --disk size=20 (GiB, a new image file), --cdrom with the ISO's path, and --osinfo naming the guest OS. 4. Work through the installer in the console window; the disk appears as /dev/vda. 5. Remove the ISO and boot from the virtual disk. The graphical virt-manager or GNOME Boxes do the same.

Cloud images and writing an ISO to USB

Installing from an ISO is slow when you need many machines. Distributions therefore publish cloud images: ready-installed disk images, often in qcow2 format, configured at first boot by cloud-init, which reads a host name, users and SSH keys supplied by the cloud or by you. A new instance is ready in under a minute. To install on real hardware instead, write the ISO to a USB stick with a tool such as dd if=debian.iso of=/dev/sdX bs=4M status=progress conv=fsync. Warning: dd overwrites the target without asking. Check the device name with lsblk first, because naming your system disk destroys everything on it.

Drawbacks of virtual machines

Virtual machines are easy to create but carry the weight of complete systems. Each needs installing and configuring, which tools such as Ansible or cloud-init can automate but not eliminate. Each boots a full kernel and init system, so starting takes seconds to minutes. Each must be patched and monitored like any server, with its own systemd, sshd and libraries. Applications can clash with the software on the image, or break when an upgrade changes a library. And the safe practice of one service per machine multiplies the cost, especially in clouds that charge per instance. These pressures led to containers, the subject of "Containers: namespaces and cgroups".

Common mistakes to avoid

Calling every hypervisor type 2 because it is a program you install; KVM and Hyper-V do not fit that picture. Expecting KVM to work when VT-x or AMD-V is disabled in the firmware. Confusing a system virtual machine with a language runtime such as the Java virtual machine. Skipping the checksum on a downloaded ISO. Running dd with the wrong of= device. Forgetting that every guest needs its own security updates. Assuming a WSL 1 distribution behaves like real Linux: only WSL 2 runs a Linux kernel.

Key takeaways

A system virtual machine imitates a whole computer and runs a full operating system as a guest under a hypervisor. Type 1 hypervisors (Xen, ESXi, Hyper-V) run on hardware; type 2 (VirtualBox) on a host OS; KVM makes Linux itself a hypervisor, with QEMU and libvirt. VT-x and AMD-V provide hardware support; virtio devices such as /dev/vda are paravirtualised. Uses: testing, compatibility, cloud servers. WSL 2 runs a real Linux kernel on Windows. Verify ISOs, use virt-install or cloud images, and check device names before dd.

Flashcards

Flashcard

System virtual machine

A complete computer imitated in software, running its own OS

Kernel included.

Test your understanding

Question 1 of 5

Which statement about KVM is correct?

Question 2 of 5

A Linux guest on KVM shows its disk as /dev/vda rather than /dev/sda. Why?

Question 3 of 5

What distinguishes WSL 2 from WSL 1?

Question 4 of 5

Before writing an installer ISO to a USB stick with dd, which check matters most?

Question 5 of 5

Why do clouds use cloud images instead of installing each instance from an ISO?

← Previous topicNext topic →