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.
- 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, 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.
- 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.
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 --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.
- 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.
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.
- 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.
- 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.
- "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."