DEVOPS, SRE AND CLOUD · PART 1 · LINUX, FROM THE INSIDE · VIRTUALIZATION · 25 OF 187

Virtual Machines and Hypervisors: KVM, QEMU, Vagrant

How one physical machine runs many: hypervisors (type 1 and type 2), CPU virtualisation support, KVM and QEMU, virtual disks, networks and images, Vagrant and Multipass for repeatable lab VMs, how cloud instances are VMs, and a measured comparison: a Linux VM reached user space in 2.5 to 2.8 s with an emulated CPU, while a process in its own namespaces started in 9 to 10 ms.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
1 / 12

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Hypervisor type 1 and 2
  • VT-x + KVM + QEMU
  • VM boot ~2.5 s (emulated) vs namespaces ~10 ms
  • Vagrant
  • cloud instances

Notes

The idea

A hypervisor shares one physical machine between several virtual machines. It gives each VM virtual CPUs, memory, disks and network cards, and each VM boots its own kernel, believing it owns real hardware. So one host can run Ubuntu, Red Hat and Windows side by side, and a crash or a break-in in one VM stays inside it. That strong isolation is why clouds use VMs to separate customers.

A hypervisor
  • splits CPU, memory, disk and network between VMs
  • each VM gets virtual hardware and boots its own kernel
  • VMs are isolated: one crash or hack stays inside
  • different OSes side by side

Two types

There are two kinds. Type one hypervisors run directly on the hardware: VMware ESXi, Microsoft Hyper-V, Xen, AWS's Nitro, and KVM, which turns the Linux kernel itself into a hypervisor. Type two hypervisors run as an app on your normal operating system: VirtualBox, VMware Workstation, UTM or Parallels on a Mac. Servers and clouds use type one; laptops usually use type two.

Type 1 and type 2
  • type 1, bare metal: runs on the hardware: ESXi, Hyper-V, Xen, KVM (inside Linux), AWS Nitro
  • type 2, hosted: an app on your OS: VirtualBox, VMware Workstation, UTM, Parallels
  • servers and clouds: type 1

Hardware help

Modern CPUs have virtualisation features, Intel's V T x and AMD's A M D V, which let guest code run directly on the CPU at nearly full speed. On Linux, the KVM module uses them and appears as slash dev slash kvm. QEMU provides the rest of the virtual machine: the BIOS, disks and network cards. With KVM it is fast. Without it, QEMU must emulate every CPU instruction in software, which is slow. Our test machine had no V T x flags and no KVM, so we saw the slow path.

KVM and QEMU
  • Intel VT-x / AMD-V: the CPU runs guest code directly (vmx or svm flag)
  • KVM: the kernel module using it: /dev/kvm
  • QEMU: emulates disks, network, BIOS; with KVM it is fast, without it, CPU is emulated
  • our machine: 0 vmx/svm, no KVM

Measured: a VM boot

We booted a real Ubuntu kernel in QEMU, the same test as episode two. With five hundred and twelve megabytes and an emulated CPU, user space started after two and a half to two point eight seconds, and the whole boot and power-off took four to four point three seconds of wall time. Real servers take longer, because they start many services and real disks. A VM is a whole computer, so it starts like one.

QEMU, emulated CPU (ours)
kernel 6.8.0-151, 512 MB RAM
firmware: SeaBIOS
user space started at:
  2.76 s, 2.67 s, 2.49 s
boot + power off, wall time:
  4.3 s, 4.1 s, 4.0 s
61 processes at start
real servers start many more
  services, so take longer

An isolated process

Now compare. unshare starts a process in its own namespaces, a private hostname and a private list of process IDs, which is the core trick behind containers. It set the hostname to box, and inside, ps saw only a handful of lines, not the whole machine. It took nine to ten milliseconds, hundreds of times faster than our VM, because no kernel boots: it is just a process on the same kernel with a restricted view. Part six builds a full container this way.

unshare (our runs)
$ unshare --fork --pid
    --mount-proc --uts sh -c
    'hostname box; hostname;
    ps -e | wc -l'
box   4
10 ms, 10 ms, 9 ms
own hostname, own PID list,
  SAME kernel

VM or container?

So which is better? A VM has its own kernel, strong isolation and any operating system, but takes seconds or more to start and uses gigabytes. A container shares the host kernel, starts in milliseconds and uses megabytes, but its isolation is thinner and it must match the host's kernel. In practice you use both: containers running inside VMs. Every Kubernetes node in the cloud is a VM full of containers.

Choose
  • VM: own kernel, strong isolation, any OS, seconds to minutes to start, GBs
  • container: shared kernel, lighter isolation, Linux on Linux, ms to start, MBs
  • usual answer: containers running inside VMs (Kubernetes nodes are VMs)

Lab VMs as code

For labs, write VMs as code. Vagrant reads a small file: which base image, which network, and a script to set it up. vagrant up builds it, vagrant ssh logs in, and vagrant destroy throws it away, so anyone gets an identical lab. Multipass, from episode seven of part zero, does the same with less config. This idea, describing machines in files, grows into infrastructure as code in part nine.

Vagrantfile
Vagrant.configure("2") do |c|
  c.vm.box = "bento/ubuntu-24.04"
  c.vm.network "private_network",
    ip: "192.168.56.10"
  c.vm.provision "shell",
    path: "setup.sh"
end
$ vagrant up / ssh / destroy

Cloud instances

In the cloud, an instance is a VM. The instance type sets its virtual CPUs and memory, the image, an AMI on AWS, is its disk, and cloud init configures users, keys and packages on first boot. The hypervisor, Nitro on AWS, shares the host, which is why steal time from episode thirteen can appear. A few bare-metal instance types skip the hypervisor entirely, for special workloads.

In the cloud
  • instance type = vCPU + RAM (e.g. 2 vCPU, 4 GB)
  • image (AMI) = the VM's disk
  • boots with cloud-init: users, keys, packages on first boot
  • steal time: neighbours on the same host (episode 13)
  • bare-metal types: no hypervisor

Common mistakes

Mistakes. Running VMs inside VMs without checking the CPU supports it, and getting the slow emulated path, as we did. Treating containers as if they were VMs, forgetting they share a kernel. Snowflake VMs set up by hand that nobody can rebuild. Oversized VMs that sit idle. And forgetting to destroy lab and cloud VMs, which keeps costing money.

Avoid these
  • nested virtualisation without checking support (slow)
  • treating containers as VMs (shared kernel)
  • snowflake VMs set up by hand
  • oversized VMs nobody uses
  • forgetting to destroy lab and cloud VMs (cost)

Say it like this

In the interview: a VM virtualises hardware, so it runs its own kernel on a hypervisor such as KVM or AWS Nitro, with strong isolation and any OS, but it boots like a computer. A container is a process on the host kernel, isolated with namespaces and limited with cgroups, so it starts in milliseconds but shares the kernel. Production usually runs containers inside VMs, for density and isolation together.

In the interview
  • "A VM virtualises hardware and runs its own kernel on a hypervisor like KVM or Nitro: strong isolation, slower start.
  • A container is a process on the host kernel with namespaces and cgroups: milliseconds to start, thinner isolation. We run both."

Interview questions

Q1What is a hypervisor?

Software that shares a physical machine between virtual machines, giving each virtual CPUs, memory, disks and network, and isolating them.

Q2Type 1 vs type 2 hypervisors?

Type 1 runs on the hardware (ESXi, Hyper-V, Xen, KVM, AWS Nitro); type 2 runs as an application on a host OS (VirtualBox, VMware Workstation, UTM).

Q3What are KVM and QEMU?

KVM is the Linux kernel module that uses CPU virtualisation extensions (/dev/kvm); QEMU emulates the rest of the machine (firmware, disks, NICs) and uses KVM for speed.

Q4VM vs container?

A VM has its own kernel and strong isolation but starts slower and uses more resources; a container is an isolated process on the host kernel (namespaces, cgroups) that starts in milliseconds.

Q5Why do clouds use VMs?

Strong isolation between customers on shared hardware, any OS, and flexible sizing; containers then run inside those VMs.

Q6What is Vagrant?

A tool that builds and manages VMs from a Vagrantfile (box, network, provisioning), giving reproducible development and lab environments.

Q7How do you check if a CPU supports hardware virtualisation?

Look for vmx (Intel) or svm (AMD) in /proc/cpuinfo, and /dev/kvm on Linux.

Q8What is cloud-init?

The tool that configures a cloud VM on first boot: users, SSH keys, packages, files and scripts from user data.