Operating Systems Module 8: Modern systems Lesson 8.1 15 min

Virtual machines and containers

A hypervisor fakes a whole computer for each guest OS. A container fakes nothing: it is ordinary processes with a limited view (namespaces) and a limited budget (cgroups). See both, and watch a memory limit kill a process in one container only.

You run docker run and a new “machine” appears in under a second. You run a virtual machine and it takes half a minute to boot, shows a BIOS screen and has its own kernel. Both feel like “a separate computer”. Are they the same trick?

They are not. One fakes the hardware, so a whole operating system can run on top. The other fakes nothing at all: it only changes what some ordinary processes are allowed to see and use. This lesson takes both apart.

Fake the hardware: virtual machines

An operating system expects to own the machine: to run in kernel mode, to program the page tables (Lesson 6.3) and to talk to the disk (Lesson 7.1). To run two operating systems at once, something has to sit below them and keep each one from taking the real machine. That something is a hypervisor.

The hypervisor gives each guest its own virtual CPU, its own slice of RAM and virtual devices, such as a disk that is really a big file on the host. The guest kernel runs as if it were in charge. Whenever it does something that would hurt the others, such as touching a device, the CPU traps out of the guest to the hypervisor, which does a safe version of the job and resumes the guest. Early hypervisors had to catch and rewrite many instructions in software (trap-and-emulate). Modern CPUs (Intel VT-x, AMD-V) have a guest mode built in, so most guest code runs directly on the CPU at full speed and only the sensitive operations trap.

Follow a guest program reading a file. Watch which layer is busy.

A guest reads a file, and the hypervisor steps in

  1. The guest program asks for a file Inside the guest, a program calls read, exactly as it would on a real machine.
    Running
    guest program
  2. The guest kernel handles the system call The guest kernel runs in its own kernel mode. It finds that the data is not in its cache and must ask its disk. To the guest kernel, that disk is real.
    Running
    guest kernel
  3. The guest touches its virtual disk The disk controller the guest is writing to does not exist. The access traps out of the guest to the hypervisor, which turns "read block 7 of the guest's disk" into "read part of a file on the host".
    Running
    hypervisor
  4. The real disk is read The hypervisor asks the host's real driver, and the real disk does the work. Other guests keep running meanwhile.
    Running
    real hardware
  5. The hypervisor resumes the guest The hypervisor copies the data into the guest's memory, makes it look like the virtual disk finished, and resumes the guest.
    Running
    hypervisor
  6. The guest carries on The guest kernel finishes the system call and the guest program gets its data. Nothing in the guest knows a hypervisor was involved.
    Running
    guest program

Every guest has a complete kernel of its own, so a guest can run a different operating system from the host (Windows on a Linux host, say) and a crash in one guest does not touch the others. The price is weight: a kernel to boot, memory reserved for each guest and extra work on every trap.

Fake nothing: containers

Now suppose every program already runs on the same operating system, and you only want them to stop seeing and disturbing each other. You do not need a second kernel for that. You need the one kernel to be a little less honest with some processes. Linux has two features for it.

  • Namespaces change what a process can see. A PID namespace gives its processes their own numbering, starting at 1 (Lesson 1.4 explained why PID 1 is special). Other namespaces do the same for mounted file systems, network interfaces, hostnames and users.
  • Control groups (cgroups) limit what a process can use: CPU time (the scheduler of Lesson 3.5 enforces it), memory, and disk and network bandwidth.

A container is nothing more than processes started inside a new set of namespaces and placed in a cgroup. There is no container object in the kernel. All containers and the host run on one shared kernel, which is why they start in milliseconds and why they cost almost nothing in memory.

Two views of the same processes

Here is a host with a container running a small web server. Look at the host’s list first, then press Container above the stage to see the same processes from inside.

One process tree, two views

  1. The host's process tree The host's kernel lists every process, with its real PID. systemd is PID 1 and everything descends from it, including the container's processes, which are the tinted rows.
    Processes on the host
    9
    Processes in the container
    3
  2. The container starts its own numbering containerd-shim started the web server in a new PID namespace. The namespace gives it a private numbering, so the kernel calls the process 4182 and the process itself calls it PID 1. Press Container above the stage to see it from inside.
    Processes on the host
    9
    Processes in the container
    3
  3. The two workers get small numbers too The web server started two workers. On the host they are 4190 and 4191. Inside they are 7 and 8, because the container's counter started at 1 when its first process was created.
    Processes on the host
    9
    Processes in the container
    3
  4. What the container cannot see Switch to the Container view. Only three processes are listed, and the first is PID 1. The host's systemd, sshd and your own bash are not there: they are not hidden by a trick in the list, they have no number in this namespace at all.
    Processes on the host
    9
    Processes in the container
    3

That was a PID namespace. The mount namespace does the same for files: the container sees a private root directory built from an image, not the host’s disk. The network namespace gives it its own interfaces and ports. None of it is a copy: it is the same kernel with a filtered view.

A budget the kernel enforces

Namespaces stop a container from seeing its neighbours. They do not stop it from eating the machine. For that the container’s processes are put in a cgroup with a memory limit, and the kernel counts every page they use (Lesson 6.5). Watch what happens when the limit is 256 MB and the container’s processes want more. This time the host view is the interesting one.

A memory limit that only the container feels

  1. Starting inside a 256 MB limit The container was started with a 256 MB memory limit. Its three processes use well under that between them, so nothing happens.
    Processes on the host
    9
    Processes in the container
    3
    Container memory
    160 of 256 MB used
  2. Worker 7 grows A worker loads a big file and now uses 110 MB. The container as a whole is at 210 MB, still under the limit.
    Processes on the host
    9
    Processes in the container
    3
    Container memory
    210 of 256 MB used
  3. Worker 8 asks for more The other worker asks for more memory and reaches 150 MB. The cgroup total would be 300 MB, over the limit of 256 MB. The kernel first tries to reclaim memory from the container, and here there is nothing it can free, so it runs the **OOM killer** for this cgroup. It ends the process using the most memory, worker 8, and it chooses only among the container's own processes.
    Processes on the host
    8
    Processes in the container
    2
    Container memory
    300 MB asked, 150 of 256 MB used
  4. Everything else is untouched The container is back under its limit and keeps running with two processes. On the host, systemd, sshd, your bash and every other process never noticed. The limit made one container fail, not the machine.
    Processes on the host
    8
    Processes in the container
    2
    Container memory
    150 of 256 MB used

Read the fields on step 3: 300 MB asked for, 150 MB left after the kill. The OOM killer is blunt, and being a victim is a normal way for a container to die when its limit is too small. Containers also have a CPU limit, which works the way the scheduler’s fair shares did in Lesson 3.5: a container may be allowed half a core, however many threads it starts.

VM or container?

Virtual machine Container
What is faked The whole computer Only the view of some processes
Kernel One per guest Shared with the host
Start-up Boots a kernel Starts a process
Isolation Strong: the guest cannot reach the host’s kernel except through the hypervisor Weaker: a kernel bug is a bug in the one kernel everyone shares
Guest OS Any The host’s kernel (Linux containers need Linux)

The two are often stacked. Cloud providers run your containers inside virtual machines, so you get the density of containers and the stronger wall of a VM between customers.

In the real world

Docker and Podman are tools that call the kernel’s namespace and cgroup features for you. The memory limit in the simulator is what --memory sets:

Terminal
$ docker run --rm -it --memory 256m alpine sh
/ # ps
PID   USER     TIME  COMMAND
    1 root      0:00 sh
  ...
/ # cat /sys/fs/cgroup/memory.max

Inside, ps lists only the container’s processes and the shell is PID 1. The file memory.max is the cgroup’s limit, in bytes. On the host, in another terminal:

On the host
$ ps -ef | grep alpine
$ docker stats --no-stream
$ dmesg | grep -i "killed process"

On the host the same shell appears under a large PID, next to everything else. docker stats shows each container’s memory use against its limit, and after an OOM kill the kernel log carries a line starting with “Memory cgroup out of memory”. The limit is stored in bytes, so 256 MB is this many:

limit.c
#include <stdio.h>

int main(void) {
    unsigned long mb = 256;
    printf("256 MB = %lu bytes\n", mb * 1024 * 1024);   /* 1 MB = 1024 * 1024 bytes */
    return 0;
}

Output (one run)

256 MB = 268435456 bytes

Windows has the same two ideas under other names: Windows containers use job objects and silos, and Hyper-V runs full virtual machines. macOS and Windows machines that run Linux containers start a small Linux virtual machine first, because a Linux container needs a Linux kernel to share. Kubernetes, which schedules containers over many machines, hands each one a CPU and memory limit in exactly this way. :::

Quick check

Quick check

  1. What is the main difference between a virtual machine and a container?
  2. A process is PID 1 inside a container. What does that tell you about its PID on the host?
  3. What does a namespace limit, compared with a cgroup?
  4. A container hits its memory limit and the kernel kills one of its processes. What happens to the host?

Recap

  • A hypervisor gives each guest a virtual computer. Each guest runs its own kernel, and sensitive operations trap to the hypervisor.
  • A container is processes in new namespaces (what they can see) and a cgroup (what they can use). All containers share the host’s one kernel.
  • A process has one PID per PID namespace: PID 1 in the container was 4182 on the host.
  • A cgroup memory limit is enforced by the OOM killer, which ends a process inside the container and leaves the host alone.
  • VMs are heavier and isolated more strongly; containers are light and share a kernel. Clouds often use both.

Next: the capstone. A real server has slowed down, and you decide which commands to run to find out why, using what you have learned in all eight modules.

Your progress is saved in this browser. Sign in to keep it on every device. Free, no password.

Join the discussion