PadhaikoAll subjects

Hypervisors and Virtual Systems

Operating System (ENCT 254)

Open in the Padhaiko app

7.1 Hypervisors: Type 1 and type 2

A virtual machine (VM) is a software-defined computer that emulates or partitions physical hardware—CPU, memory, disk, and network interfaces—so that an unmodified guest operating system runs in isolation just as if it owned real hardware. The software layer that creates, schedules, and isolates VMs is called a hypervisor (or Virtual Machine Monitor, VMM).

Definition

Type 1 vs. Type 2 Hypervisors

  1. Type 1 (Bare-Metal) Hypervisor: Installs and executes directly on the physical hardware without an underlying general-purpose host OS. It schedules guest VMs directly onto physical CPUs and hardware memory.
    • Examples: VMware ESXi, Microsoft Hyper-V, Xen, Linux KVM (turns the Linux kernel itself into a Type 1 hypervisor).
    • Use cases: Enterprise data centers, cloud infrastructure, high-availability server consolidation.
  2. Type 2 (Hosted) Hypervisor: Runs as a user-space application on top of a conventional host OS (Windows, Linux, or macOS) and relies on the host OS for device drivers, process scheduling, and memory management.
    • Examples: Oracle VirtualBox, VMware Workstation / Fusion, QEMU (emulation mode).
    • Use cases: Desktop software testing, cross-OS development, university lab environments.
AspectType 1 (Bare-Metal) HypervisorType 2 (Hosted) Hypervisor
Execution LayerDirectly on hardware (Ring / root mode)On top of a host OS
Performance & OverheadNear-native; minimal virtualization overheadHigher overhead due to extra host OS scheduling & I/O layers
Hardware CompatibilitySupports certified server hardware listsBroadest—inherits every driver supported by the host OS
Attack Surface & IsolationVery small trusted computing base; strong isolationLarger attack surface (depends on host OS security)
Typical ExamplesVMware ESXi, Xen, Hyper-V, KVMVirtualBox, VMware Workstation, QEMU (TCG mode)

7.2 Virtual machines: Creating virtual machine in Qemu/Virtual box/VMWare

Creating a virtual machine across QEMU/KVM, Oracle VirtualBox, or VMware follows a common workflow: verify hardware virtualization extensions (VT-x/AMD-V in UEFI/BIOS), create a virtual disk file, configure virtual hardware (vCPUs, RAM, virtual NIC), attach a guest OS ISO image, install the OS, and install guest integration drivers.

Virtual Machine Tools, Disk Formats, and Guest Integration
Hypervisor / ToolPrimary Disk FormatNetworking ModesGuest Integration Package
QEMU / KVMqcow2 (Copy-on-Write, snapshots, thin provisioning), rawUser-mode SLIRP (NAT), TAP/Bridgevirtio drivers + QEMU Guest Agent
Oracle VirtualBox.vdi (VirtualBox Disk Image), .vmdk, .vhdNAT, Bridged Adapter, Host-Only, InternalVirtualBox Guest Additions
VMware (Workstation / ESXi).vmdk (Virtual Machine Disk) + .vmx configNAT, Bridged, Host-OnlyVMware Tools / open-vm-tools

Creating a VM in QEMU / KVM (CLI Workflow)

Creating a Virtual Disk and Booting a Guest in QEMU/KVMText
# 1. Create a 20 GiB dynamically allocated (thin-provisioned) qcow2 disk
qemu-img create -f qcow2 ubuntu-lab.qcow2 20G

# 2. Boot the VM from an installation ISO with KVM acceleration and virtio devices
qemu-system-x86_64 \
  -enable-kvm -cpu host -smp 2 -m 4096 \
  -drive file=ubuntu-lab.qcow2,format=qcow2,if=virtio \
  -cdrom ubuntu-24.04-live-server-amd64.iso -boot d \
  -netdev user,id=n1,hostfwd=tcp::2222-:22 -device virtio-net-pci,netdev=n1

Creating a VM in VirtualBox and VMware


7.3 Container virtualization: Docker and Kubernetes

While VMs virtualize hardware and boot a full guest OS kernel per instance, containers virtualize at the operating system kernel level. Multiple isolated user-space environments share a single host Linux kernel.

AspectVirtual Machines (Hardware Virtualization)Containers (OS-Level Virtualization)
Isolation BoundaryVirtualized hardware + separate guest OS kernelHost OS kernel primitives (namespaces + cgroups)
Startup TimeTens of seconds to minutes (full OS boot)Milliseconds to seconds (starts a process)
Disk & RAM FootprintGigabytes (full OS image + guest kernel RAM)Tens of megabytes (app + minimal libraries only)
OS FlexibilityCan run Windows, Linux, BSD on the same hostShares host kernel (Linux containers require a Linux kernel)
Security IsolationVery strong (hardware/hypervisor boundary)Weaker than VMs if kernel is exploited (mitigated via hardening)

Linux Kernel Primitives Behind Containers

Docker

Docker packages an application together with its exact runtime libraries and configuration into a portable, immutable container image built in stacked read-only layers (via UnionFS / OverlayFS) plus a thin writable layer at runtime.

Minimal Dockerfile and Common Docker CommandsText
# Dockerfile
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["python", "app.py"]

# Build and run commands
# docker build -t myapp:v1 .
# docker run -d -p 8080:8000 --memory=512m --cpus=1.0 myapp:v1

Kubernetes (K8s)

When applications scale to dozens or thousands of containers across multiple hosts, Kubernetes automates deployment, scaling, self-healing, service discovery, and rolling updates.

Definition

Kubernetes Cluster Architecture

A Kubernetes cluster consists of a Control Plane and one or more Worker Nodes:


7.4 Power shell and windows subsystem for LINUX (WSL)

Modern Windows environments pair native object-oriented administration (PowerShell) with integrated Linux tooling (WSL).

PowerShell

PowerShell is a cross-platform task automation shell and scripting language built on .NET. Unlike traditional UNIX shells that pipe unstructured plain text between commands, PowerShell pipelines pass structured .NET objects with typed properties and methods.

Definition

PowerShell Cmdlets and Object Pipeline

PowerShell Object Pipeline ExamplesText
# List top 5 processes by working-set memory (no text slicing needed)
Get-Process | Sort-Object WorkingSet64 -Descending | Select-Object -First 5 Name, Id, WorkingSet64

# Find stopped services and inspect their startup type
Get-Service | Where-Object { $_.Status -eq 'Stopped' -and $_.StartType -eq 'Automatic' }

# Check WSL distributions and status
wsl --list --verbose

Windows Subsystem for Linux (WSL)

Windows Subsystem for Linux (WSL) enables developers and administrators to run a genuine Linux environment (Bash, awk, make, GCC, SSH, Docker) directly on Windows without the overhead of managing a traditional desktop VM or dual-booting.

AspectWSL 1 (Translation Layer)WSL 2 (Managed Lightweight VM)
ArchitectureTranslates Linux system calls directly onto the Windows NT kernelRuns a real Microsoft-built Linux kernel inside a lightweight Hyper-V utility VM
System Call CompatibilityPartial (some kernel features, Docker daemon, eBPF unsupported)Full Linux syscall compatibility (supports native Docker, systemd, FUSE)
Linux File I/O SpeedSlower on metadata-heavy operations (git status, npm install)Very fast inside its virtual ext4.vhdx disk
Cross-OS File AccessFast direct access to Windows NTFS files (/mnt/c/...)Slower across the 9P bridge when accessing /mnt/c/...
Memory AllocationShared directly with Windows processesDynamic VM memory allocation (reclaims unused RAM back to Windows)

7.5 Performance optimization and security in virtualized environments

Running multiple VMs or containers on shared hardware introduces virtualization overhead and multi-tenant security risks that must be systematically tuned and hardened.

Performance Optimization Techniques

Definition

Key Virtualization Performance Optimizations

  1. Hardware-Assisted CPU & Memory Virtualization: Enable Intel VT-x / AMD-V along with Second Level Address Translation (SLAT) (Intel EPT / AMD RVI), which lets hardware translate guest-virtual guest-physical host-physical addresses directly, eliminating costly shadow page-table VM-exits.
  2. Paravirtualized Drivers (virtio / PVSCSI / VMXNET3): Emulating legacy IDE disks or e1000 NICs forces thousands of register traps per second. Paravirtualized drivers use shared ring buffers in memory to batch I/O requests with minimal hypervisor exits.
  3. Memory Overcommitment Controls:
    • Memory Ballooning: A balloon driver inside the guest allocates pinned dummy pages and hands their physical frames back to the hypervisor when host RAM tightens.
    • Page Deduplication (KSM / TPS): Merges identical read-only memory pages (such as shared OS kernel/library code) across multiple guests into a single copy-on-write frame.
  4. CPU Pinning, HugePages & NUMA Alignment: Pinning latency-sensitive vCPUs to dedicated physical cores, backing guest RAM with / HugePages (reducing TLB misses), and keeping a VM's vCPUs and RAM inside the same NUMA node avoid cross-socket latency.
  5. SR-IOV (Single Root I/O Virtualization): Partitions a physical PCIe NIC into hardware Virtual Functions assigned directly to VMs, achieving near-line-rate network throughput by bypassing the hypervisor virtual switch.

Security in Virtualized Environments


Worked Out Examples

Example 7.1

7.1

A physical host running a Type 1 hypervisor has of RAM, of which is reserved by the hypervisor. Six identical Linux VMs are each configured with of virtual RAM, and each VM currently has an active working set of . Suppose Kernel Same-page Merging (KSM) deduplicates of identical kernel and library pages across the six guests. Calculate:

  1. The memory overcommitment ratio (configured vRAM to available host RAM).
  2. The net physical RAM consumed by the VMs and the remaining free host RAM.

Finding available host RAM and total configured vRAM:

Calculating the memory overcommitment ratio:

Computing net guest RAM usage and free host RAM: Without deduplication, the active working sets require . Subtracting saved by KSM:

Because , no host swapping occurs despite overcommitment.

Answer:

The memory overcommitment ratio is . Net physical RAM used by the six VMs is , leaving of host RAM free.

Example 7.2

7.2

You need to deploy an isolated Ubuntu Server test VM on a Linux workstation using QEMU/KVM with , RAM, a dynamically growing disk, paravirtualized I/O, and SSH access from host port 2222 to guest port 22. Write the commands and explain each key option.

Creating the virtual disk image (qcow2): Use qemu-img to allocate a thin-provisioned qcow2 disk (consumes host storage only as data is written and supports snapshots):

Step-free Disk CreationText
qemu-img create -f qcow2 srv-test.qcow2 40G

Launching the VM with KVM acceleration and virtio devices: Enable hardware virtualization (-enable-kvm), attach the disk and NIC using paravirtualized virtio drivers for low VM-exit overhead, and forward TCP port 2222 on the host to port 22 on the guest:

QEMU Launch CommandText
qemu-system-x86_64 \
  -enable-kvm -cpu host -smp 4 -m 8192 \
  -drive file=srv-test.qcow2,format=qcow2,if=virtio \
  -cdrom ubuntu-server.iso -boot once=d \
  -netdev user,id=net0,hostfwd=tcp::2222-:22 \
  -device virtio-net-pci,netdev=net0

Verifying SSH access after installation: Once the guest finishes installing OpenSSH, running ssh -p 2222 user@localhost on the host connects directly into the VM over the NAT port-forward without altering host bridge interfaces.

Answer:

Create srv-test.qcow2 () with qemu-img create -f qcow2, then boot qemu-system-x86_64 with -enable-kvm -smp 4 -m 8192, if=virtio storage, virtio-net-pci networking, and hostfwd=tcp::2222-:22 for SSH access.

Example 7.3

7.3

A Kubernetes cluster has identical worker nodes. Each worker node has and of RAM, of which and of RAM are reserved for the OS and kubelet. If each microservice Pod requests () and of RAM, what is the maximum number of Pods the kube-scheduler can place across the cluster?

Computing allocatable resources per worker node:

Finding the CPU and RAM Pod limits per node:

  • By CPU: .
  • By RAM: .

Computing total cluster capacity: Across all worker nodes:

Answer:

Each node can host at most Pods (RAM-limited), giving a cluster-wide maximum of Pods.


Exercises

Exercise — Hypervisors and Virtual Systems

Question 1

Explain the Virtual Machine structure and distinguish between Type 1 (bare-metal) and Type 2 (hosted) hypervisors with two examples of each. What are the primary benefits of virtual machines over traditional single-OS installations? [Adapted from past exam 2073/2074]

Virtual Machine structure and hypervisor types: A Virtual Machine Monitor (hypervisor) multiplexes physical CPU, memory, and I/O devices across isolated VMs so each guest OS perceives its own dedicated hardware:

  • Type 1 Hypervisor: Runs directly on bare hardware without a host OS (e.g., VMware ESXi, Microsoft Hyper-V, KVM, Xen), offering maximum performance and smallest trusted computing base.
  • Type 2 Hypervisor: Runs as an application inside a host OS (e.g., Oracle VirtualBox, VMware Workstation), trading some overhead for easy desktop installation and broad host driver support.

Benefits of the VM structure:

  • Fault and security isolation: Crashes or compromises remain contained inside a single VM.
  • Hardware consolidation: Multiple heterogeneous OSs (e.g., Linux + Windows) run simultaneously on one machine.
  • Snapshots, cloning, and live migration: Instant rollback before updates and zero-downtime host maintenance.
Answer:

Type 1 hypervisors (ESXi, Hyper-V, KVM) run directly on hardware; Type 2 hypervisors (VirtualBox, VMware Workstation) run on a host OS. VMs deliver strong isolation, server consolidation, multi-OS support, and snapshots/migration.

Question 2

Outline the procedure to create a virtual machine in VirtualBox or VMware, and compare the NAT, Bridged, and Host-Only virtual networking modes.

VM creation workflow:

  1. Enable hardware virtualization (VT-x / AMD-V) in host BIOS/UEFI.
  2. Create a new VM specifying OS family, vCPU count, and RAM allocation.
  3. Create a dynamically expanding virtual disk (.vdi in VirtualBox, .vmdk in VMware, or .qcow2 in QEMU).
  4. Attach the guest installer .iso, boot and install the OS, then install Guest Additions / VMware Tools / virtio drivers.

Comparing virtual network modes:

  • NAT: The hypervisor translates VM traffic behind the host's IP; the VM reaches the internet easily, while external hosts cannot initiate connections unless port-forwarded.
  • Bridged: The VM's virtual NIC connects directly to the physical LAN switch and obtains its own LAN IP via DHCP—appearing as a peer machine on the network.
  • Host-Only: Creates an isolated private network shared strictly between the host and its VMs with no external internet routing—ideal for malware analysis or isolated lab clusters.
Answer:

Creating a VM involves enabling VT-x/AMD-V, allocating vCPUs/RAM, creating a virtual disk (.vdi/.vmdk/.qcow2), booting from an .iso, and installing guest tools. NAT shares the host IP for outbound access, Bridged exposes the VM directly on the physical LAN, and Host-Only isolates communication to host-and-VMs only.

Question 3

Compare Virtual Machines and Containers (naming the two Linux kernel mechanisms that isolate and limit containers). Briefly explain the roles of Docker and Kubernetes (Control Plane vs. Worker Nodes).

VMs vs. Containers: VMs virtualize hardware and run a complete guest OS kernel per instance (gigabytes of overhead, seconds-to-minutes boot), whereas containers virtualize the OS layer, sharing the host Linux kernel (megabytes of overhead, millisecond startup). Containers isolate views using Linux namespaces (PID, NET, MNT, UTS, IPC, USER) and enforce CPU/RAM/I/O quotas using cgroups.

Docker and Kubernetes roles:

  • Docker builds layered immutable container images (Dockerfile) and runs individual containers on a single host.
  • Kubernetes orchestrates containers across a multi-node cluster: the Control Plane (kube-apiserver, etcd, kube-scheduler, kube-controller-manager) decides cluster state and schedules Pods, while Worker Nodes (kubelet, kube-proxy, container runtime) execute Pods and route Service traffic.
Answer:

Containers share the host kernel using namespaces (isolation) and cgroups (resource limits), making them far lighter than VMs. Docker packages and runs single-host containers, while Kubernetes orchestrates Pods across clusters using a Control Plane and Worker Nodes.

Question 4

How does PowerShell differ from traditional UNIX text shells in its pipeline model? Compare WSL 1 and WSL 2 with respect to architecture, syscall compatibility, and file-system performance.

PowerShell object pipeline: Traditional UNIX shells pipe unstructured plain-text lines (requiring grep/awk/cut parsing), whereas PowerShell Verb-Noun cmdlets emit and consume structured .NET objects, allowing downstream commands to filter, sort, and select fields directly by property name (e.g., Get-Process | Sort-Object WorkingSet64).

WSL 1 vs. WSL 2:

  • WSL 1 implements a syscall translation layer over the Windows NT kernel: fast access to Windows NTFS files (/mnt/c), but incomplete Linux syscall compatibility (no native Docker daemon) and slower Linux metadata I/O.
  • WSL 2 runs a real Microsoft-maintained Linux kernel inside a lightweight Hyper-V utility VM: full Linux syscall/Docker compatibility and high-speed Linux ext4 disk performance, at the cost of slower cross-OS NTFS access over the 9P bridge.
Answer:

PowerShell passes structured .NET objects through pipelines rather than raw text. WSL 1 translates Linux syscalls onto the NT kernel (fast Windows file access, partial syscall coverage), whereas WSL 2 runs a real Linux kernel inside a lightweight VM (full syscall/Docker support and fast Linux I/O).

Question 5

A bare-metal hypervisor runs on a host with physical CPU cores and of RAM ( reserved for the hypervisor). It hosts VMs, each configured with and of virtual RAM. Each VM actively uses of RAM, and transparent page sharing saves across the guests. Calculate:

  1. The vCPU-to-pCPU ratio and memory overcommitment ratio.
  2. The net physical RAM used by all VMs and the remaining free host RAM.

Computing CPU and memory overcommitment ratios:

Computing net guest RAM and remaining free RAM:

Answer:

The vCPU:pCPU ratio is and memory overcommitment ratio is (). Net VM RAM used is , leaving free.

Question 6

Describe four performance optimization techniques used in hypervisors (such as SLAT, paravirtualization, ballooning, and NUMA/CPU pinning) and discuss three key security threats in virtualized/containerized systems along with their mitigations.

Performance optimizations:

  • SLAT (Intel EPT / AMD RVI): Hardware nested page tables accelerate guest-to-host address translation without software shadow page-table traps.
  • Paravirtualized I/O (virtio): Replaces slow emulated hardware registers with shared memory ring buffers.
  • Memory Ballooning & KSM: Reclaims idle RAM from low-pressure VMs and merges duplicate read-only pages across guests.
  • CPU Pinning & HugePages: Binds vCPUs to dedicated physical cores in the same NUMA node and uses pages to cut TLB misses.

Security threats and mitigations:

  • VM Escape: Attackers exploit bugs in emulated virtual devices to compromise the host. Mitigation: Remove unused emulated hardware, prefer virtio, patch hypervisors, and sandbox hypervisor processes (sVirt/seccomp).
  • Cross-VM Side Channels: Shared L1/L3 caches leak secrets across co-resident VMs. Mitigation: Apply CPU microcode patches, flush caches on context switches, or isolate sensitive workloads on dedicated hosts.
  • Container Kernel Exposure: A shared kernel allows privilege escalation if containers run as root. Mitigation: Run non-root users, drop Linux capabilities, enable seccomp/AppArmor, and use read-only root filesystems.
Answer:

Performance relies on SLAT (EPT/RVI), virtio paravirtualization, memory ballooning/KSM, and CPU pinning/NUMA alignment. Security focuses on mitigating VM escape (minimal emulation + sandboxing), side-channel leaks (microcode/core isolation), and container kernel risks (non-root, dropped capabilities, seccomp).

Past exam questions for Operating System

← Security and System AdministrationOverview of Contemporary OS →