Sub topic 3
Hypervisors and how they share hardware
This chapter assumes you know what a kernel does, what a process is, and how to use a Linux or macOS command line. The previous chapter introduced a virtual machine (VM) as a whole computer in software. Here you will see what makes that possible: the hypervisor.
What a hypervisor does
A hypervisor is software that creates and runs virtual machines. It sits between the physical hardware and the guest operating systems. Each guest believes it owns a CPU, memory, disks, and network cards. In fact, the hypervisor hands out slices of the real hardware and keeps the guests apart.
The hypervisor has three main jobs:
- CPU sharing. It schedules each guest's virtual CPUs (vCPUs) onto physical cores, much as an operating system schedules processes.
- Memory isolation. It maps each guest's "physical" memory onto different regions of real RAM. A guest cannot read another guest's pages.
- Device virtualization. It presents virtual disks and network adapters, either by emulating real devices or by offering faster paravirtual devices such as virtio.
Hardware support
A guest kernel expects to run privileged instructions, such as changing page tables. Letting it do so directly would break isolation. Early hypervisors rewrote or trapped these instructions in software, which was slow.
Modern CPUs add virtualization extensions: Intel VT-x and AMD-V. They give the CPU a special mode for guests. Most guest code runs directly on the hardware at full speed. Only sensitive operations cause a "VM exit", which hands control back to the hypervisor. Extended page tables (Intel EPT, AMD NPT) let the CPU translate guest addresses in hardware as well.
On Linux you can check whether your CPU offers these extensions:
grep -E -c '(vmx|svm)' /proc/cpuinfoA result above zero means the CPU advertises VT-x (vmx) or AMD-V (svm). A result of zero is common inside a VM unless nested virtualization is turned on.
Type 1 and type 2 hypervisors
Hypervisors are grouped by where they run.
| Property | Type 1 (bare metal) | Type 2 (hosted) |
|---|---|---|
| Runs on | Directly on hardware | As an application on a host OS |
| Examples | VMware ESXi, Xen, Microsoft Hyper-V, KVM | VirtualBox, VMware Workstation, Parallels |
| Typical use | Servers, cloud platforms | Developer laptops, testing |
| Overhead | Lower, with fewer layers | Higher, because the host OS sits in the path |
The line between the types is blurry. KVM is a module inside the Linux kernel, so Linux itself acts as the hypervisor. It is usually classed as type 1 because the guests run on the kernel with hardware support, not through an ordinary application layer.
Common errors and misunderstandings
- Overcommitting without watching it. Hypervisors let you assign more vCPUs or RAM in total than the host has. This works until all guests are busy at once. Then guests wait for CPU time or the host starts swapping.
- Assuming a vCPU is a core. A vCPU is a thread of execution that the hypervisor schedules. Giving a VM more vCPUs than it needs can make it slower, because they must all find free cores.
- Expecting virtualization to be available everywhere. VT-x or AMD-V can be disabled in firmware. Cloud instances may not allow nested virtualization.
- Confusing a hypervisor with a container runtime. A hypervisor runs a separate guest kernel for each VM. Later chapters show that containers share one kernel.
Exercise
A server has 8 physical cores and 32 GB of RAM. An administrator creates 10 VMs, each with 4 vCPUs and 8 GB of RAM, and all of them start a heavy workload at once. Name two problems that can happen, and say which hypervisor job each one relates to.
Show answer
CPU: 40 vCPUs compete for 8 cores, so guests wait for scheduled time and run slowly. This relates to CPU sharing. Memory: the VMs are assigned 80 GB against 32 GB of real RAM, so the host must swap or reclaim memory and performance drops sharply. This relates to memory isolation and allocation.
Exercise
Why do VT-x and AMD-V make virtualization faster than trapping privileged instructions in software?
Show answer
They give the CPU a guest mode, so most guest instructions run directly on the hardware. Only sensitive operations trigger a VM exit to the hypervisor, so far fewer instructions need software handling.