Hypervisors and containers
Clear, exam-focused notes on hypervisors (VMs) and containers: definitions, architectures, isolation mechanisms, trade-offs, commands/syntax to remember, worked examples, common mistakes and exam tips.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Virtualization and containers are core tools for deploying, testing and isolating software in cloud, data-centre and embedded systems. An engineer uses hypervisors (virtual machines) when full OS isolation and different kernels are required, and uses containers when lightweight, fast deployment and high density on a single host kernel are needed.
Key ideas
Start from the physical machine: CPU, memory, storage and devices. To run multiple isolated environments on one physical machine we must provide each environment with the illusion of dedicated hardware while sharing the real hardware.
- Virtual Machine (VM) — an environment that imitates a real machine (CPU, memory, devices) so that an unmodified guest operating system can run. A VM requires a hypervisor to manage access to real hardware.
- Hypervisor (also called Virtual Machine Monitor, VMM) — software that creates and runs virtual machines. Exact types:
- Type 1 (bare-metal) hypervisor — runs directly on hardware; guests run above the hypervisor. Example drawing: draw a vertical stack labelled from bottom: Hardware; above it a box labelled "Type 1 hypervisor (VMM)"; above it several guest OS boxes each with applications. Label arrows for CPU, memory, and I/O managed by hypervisor. Examples: VMware ESXi, Xen, Microsoft Hyper-V, and KVM (Linux kernel as the hypervisor).
- Type 2 (hosted) hypervisor — runs as an application on a host OS; guests are processes on the host. Draw: Hardware → Host OS → Hosted hypervisor process → guest OSes. Examples: VMware Workstation/Fusion, Oracle VirtualBox, Parallels Desktop.
- Trap-and-emulate — classical software virtualization method: when a guest executes a privileged instruction it causes a trap (an exception) to the hypervisor; the hypervisor emulates the instruction's effect safely. Write exact rule: "For trap-and-emulate to work, the set of sensitive instructions must be a subset of the set of privileged instructions, so that any sensitive instruction executed in non-privileged mode causes a trap to the VMM" (Popek and Goldberg virtualization requirement; early x86 violated this for some sensitive but non-privileged instructions such as POPF/SGDT).
- Paravirtualization — modify the guest OS to replace sensitive instructions with explicit calls (hypercalls) to the hypervisor. Definition: "Paravirtualization requires guest source or binary modification so the guest cooperates with the VMM for privileged operations." Mention where used (Xen early approach).
- Hardware-assisted virtualization — modern CPUs (Intel VT-x, AMD-V) provide VMX/VMRUN, VMEXIT, VMRESUME primitives to implement hypervisors without modifying guest OS. Definition: "Hardware support provides a new CPU mode where privileged guest actions cause VM exit to monitor, allowing correct virtualization without binary modification." (List VT-x/AMD-V names only; avoid internal microarchitecture claims.) Hardware memory virtualization commonly uses extended/nested page tables (Intel EPT, AMD NPT) to reduce translation overhead.
- Container — an OS-level virtualization unit that isolates processes but shares the host kernel. Definition: "A container is a group of processes with restricted view of system resources implemented by kernel isolation features (namespaces) and resource controllers (cgroups)." Draw: Host Kernel at center, multiple container boxes around with processes inside; label namespaces and cgroups as kernel features separating containers.
- Namespaces (Linux) — kernel feature that provides isolation of global resources. Common namespaces include PID, NET, IPC, MNT, UTS, USER, CGROUP, and TIME (each isolates process IDs, network interfaces, inter-process communication keys, mount points, hostname/domainname, user IDs and ID mappings, cgroup membership views, and per-namespace clocks/time offsets respectively).
- Control groups (cgroups) — kernel feature to limit and account for resources (CPU, memory, block I/O) for a group of processes. Definition: "cgroups organize processes into hierarchies with controllers that enforce limits and accounting." Note: cgroups v2 provides a unified hierarchy and revised controller semantics; many modern distributions default to cgroups v2.
- Image — filesystem snapshot used to instantiate containers or VMs; container images are layered (overlay/union) to share common read-only layers and save space.
- Security & isolation trade-offs: VMs give stronger isolation (separate kernels) but higher overhead; containers are lightweight but share kernel attack surface. Mention device passthrough and SR-IOV as ways to give near-native I/O to VMs (describe at high level only). Also mention paravirtualized I/O (virtio) as the common high-performance device model in KVM/QEMU. Containers are commonly hardened by dropping Linux capabilities, using seccomp filters, and enabling LSMs such as SELinux or AppArmor.
- Scheduling & resource management: Hypervisor schedules virtual CPUs onto physical CPUs; containers rely on kernel scheduler but can be constrained by cgroups. Explain memory overcommit and memory ballooning concept: a hypervisor may reclaim guest memory using a balloon driver in the guest — the balloon driver allocates and pins guest pages so the hypervisor can reclaim the host physical pages backing them.
Diagrams to draw for exams (describe exactly):
- Hypervisor stack (Type 1): draw hardware, hypervisor box, then multiple guest OS boxes each containing apps; label "VM kernel" and "Guest apps" in each guest.
- Hosted hypervisor: draw hardware → Host OS → hypervisor process → guest OS boxes.
- Container architecture: draw Host Kernel in center; around it draw container boxes with processes; draw labels "Namespaces" and "cgroups" between kernel and containers.
- Trap-and-emulate flow: draw Guest CPU executing privileged instruction → trap (arrow) → hypervisor (emulate) → resume guest. Label this with "VM Exit" and "VM Entry" for hardware-assisted case.
Formulas
overhead_% = (T_virtual - T_native) / T_native × 100 — where T_virtual is execution time in seconds for the workload inside VM/container, T_native is time on bare metal, applies when measuring CPU/runtime overhead; units seconds for T, percent for overhead_%.
max_containers = floor(Host_mem / Mem_per_container) — Host_mem and Mem_per_container in bytes (B), applies when containers have strict memory limits and memory is not shared.
docker run -it --name NAME IMAGE COMMAND — syntax to start a container: NAME is chosen container name; IMAGE is image name:tag; COMMAND is command to run inside the container; flags: -i interactive, -t allocate tty.
docker build -t NAME:TAG -f PATH . — build image syntax: NAME:TAG is image name and tag; PATH is Dockerfile path; . is build context.
lxc-create -n NAME -t TEMPLATE — create LXC container command (NAME container name, TEMPLATE distribution template).
virsh start VMNAME; virsh shutdown VMNAME — basic libvirt VM control syntax (VMNAME is domain name).
mem_overcommit_ratio = sum_requested_memory / physical_memory — ratio (unitless) showing memory overcommit; >1 means overcommit.
Worked examples
Numerical resource packing (memory): Given: Host has 128 GiB RAM available. Each container is limited to 2 GiB. How many containers can you run? Show steps. Step 1: Convert units: 128 GiB = 128 × 2^30 B, but counting containers we can use GiB units directly. Step 2:
max_containers = floor(128 / 2) = floor(64) = 64. Answer: 64 containers.Command trace: create and run a minimal Ubuntu container and inspect namespaces. Step 1: Pull and run:
docker run -it --rm --name exam ubuntu:20.04 bash. Step 2: Inside container runps -efandip ato see PID and network; from host runlsnsorsudo ls -l /proc/<pid>/nsto show namespace differences. You can get the container's init PID on the host with:docker inspect -f '{{.State.Pid}}' exam. Step 3: Explain what each command shows:psinside shows PIDs starting at 1 in its PID namespace;ip ashows network interfaces isolated by NET namespace. Model answer outlines commands and explains what is observed; list expected outputs qualitatively.Theory/model answer: Explain trap-and-emulate vs. hardware-assisted virtualization (short essay expected in exam). Model answer: Define trap-and-emulate (how traps occur when guest runs privileged instructions in non-privileged mode). Show the limitation: on some architectures privileged instructions do not trap; explain paravirtualization as an alternative (guest modified to use hypercalls). Then describe hardware-assisted features (VM-exit/VM-entry) that allow hypervisor to control sensitive events without guest modification. Conclude with trade-offs: paravirtualization reduces traps but requires guest changes; hardware-assist simplifies guest compatibility; type-1 hypervisors reduce OS noise.
Common mistakes
- Confusing namespaces and cgroups — namespaces isolate views (IDs, mounts, network) while cgroups limit and account for resources.
- Claiming hardware VT-x or AMD-V are hypervisors — they are CPU features used by hypervisors.
- Forgetting to state assumptions in numerical packing problems (e.g., whether memory is overcommitted or images share memory layers).
- Mixing up Type 1 and Type 2 hypervisors stack diagrams.
For your exam
- Short notes: differences between Type 1 and Type 2, trap-and-emulate, paravirtualization, hardware-assisted virtualization; include a small labelled diagram for each.
- Derivations/explanations: explain why trap-and-emulate needs all sensitive instructions to trap; explain ballooning concept with sequence of events.
- Numericals: packing containers/VMs by CPU and memory; be explicit with unit conversions and floor/ceil decisions.
- Commands/programming: write Docker run/build command, libvirt/virsh basics, LXC create/start commands. Include flags and meaning.
- Diagrams: draw hypervisor stacks and container architecture; label every box and arrow. A full-marks answer must draw the stack, label layers, and annotate where isolation and scheduling occur.
Quick check
- What kernel feature provides per-process filesystem and network isolation for containers?
- Name two main advantages of containers over VMs.
- In trap-and-emulate, what must happen when a guest executes a privileged instruction? Answers: Namespaces (e.g., MNT, NET); faster startup and higher density (lighter weight, less overhead); the instruction must cause a trap (exception) to the hypervisor which then emulates it.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?