Hypervisors and Virtual Systems
Open in the Padhaiko app7.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).
Type 1 vs. Type 2 Hypervisors
- 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.
- 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.
| Aspect | Type 1 (Bare-Metal) Hypervisor | Type 2 (Hosted) Hypervisor |
|---|---|---|
| Execution Layer | Directly on hardware (Ring / root mode) | On top of a host OS |
| Performance & Overhead | Near-native; minimal virtualization overhead | Higher overhead due to extra host OS scheduling & I/O layers |
| Hardware Compatibility | Supports certified server hardware lists | Broadest—inherits every driver supported by the host OS |
| Attack Surface & Isolation | Very small trusted computing base; strong isolation | Larger attack surface (depends on host OS security) |
| Typical Examples | VMware ESXi, Xen, Hyper-V, KVM | VirtualBox, 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.
| Hypervisor / Tool | Primary Disk Format | Networking Modes | Guest Integration Package |
|---|---|---|---|
| QEMU / KVM | qcow2 (Copy-on-Write, snapshots, thin provisioning), raw | User-mode SLIRP (NAT), TAP/Bridge | virtio drivers + QEMU Guest Agent |
| Oracle VirtualBox | .vdi (VirtualBox Disk Image), .vmdk, .vhd | NAT, Bridged Adapter, Host-Only, Internal | VirtualBox Guest Additions |
| VMware (Workstation / ESXi) | .vmdk (Virtual Machine Disk) + .vmx config | NAT, Bridged, Host-Only | VMware Tools / open-vm-tools |
Creating a VM in QEMU / KVM (CLI Workflow)
# 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=n1Creating a VM in VirtualBox and VMware
- Oracle VirtualBox (GUI or
VBoxManage): Click New, specify name and OS type, allocate RAM and vCPUs, create a dynamically allocated.vdivirtual hard disk, mount the OS.isounder Settings Storage Optical Drive, choose NAT (outbound internet via host) or Bridged (VM gets its own LAN IP), and install Guest Additions for shared clipboard, dynamic resolution, and seamless mouse integration. - VMware Workstation / Player: Select Create a New Virtual Machine, point to the installer
.iso(Easy Install or Custom), assign CPU cores, RAM, and a split/single.vmdkdisk, configure the virtual network adapter (NAT/Bridged/Host-only), power on, and install VMware Tools (open-vm-toolson Linux guests).
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.
| Aspect | Virtual Machines (Hardware Virtualization) | Containers (OS-Level Virtualization) |
|---|---|---|
| Isolation Boundary | Virtualized hardware + separate guest OS kernel | Host OS kernel primitives (namespaces + cgroups) |
| Startup Time | Tens of seconds to minutes (full OS boot) | Milliseconds to seconds (starts a process) |
| Disk & RAM Footprint | Gigabytes (full OS image + guest kernel RAM) | Tens of megabytes (app + minimal libraries only) |
| OS Flexibility | Can run Windows, Linux, BSD on the same host | Shares host kernel (Linux containers require a Linux kernel) |
| Security Isolation | Very strong (hardware/hypervisor boundary) | Weaker than VMs if kernel is exploited (mitigated via hardening) |
Linux Kernel Primitives Behind Containers
- Namespaces: Partition global kernel resources so each container sees its own isolated
PIDtree, network stack (NET), filesystem mount points (MNT), hostname (UTS), IPC queues, and user IDs (USER). - Control Groups (
cgroups): Limit, account for, and prioritize physical CPU shares, memory ceilings, and disk/network I/O per container so one container cannot starve others.
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.
# 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:v1Kubernetes (K8s)
When applications scale to dozens or thousands of containers across multiple hosts, Kubernetes automates deployment, scaling, self-healing, service discovery, and rolling updates.
Kubernetes Cluster Architecture
A Kubernetes cluster consists of a Control Plane and one or more Worker Nodes:
- Control Plane Components:
kube-apiserver: Central REST front-end validating and persisting cluster state.etcd: Consistent distributed key-value store holding all cluster configuration and state.kube-scheduler: Selects the best worker node for newly created Pods based on CPU/RAM requests and constraints.kube-controller-manager: Runs reconciliation loops (e.g., ensuring the desired number of replicas stay alive).
- Worker Node Components:
kubelet: Node agent ensuring containers in assigned Pods are healthy and running.kube-proxy: Maintains node network rules so Services route traffic to Pods.- Container Runtime (
containerd/ CRI-O): Pulls images and runs containers.
- Core Workloads: A Pod is the smallest deployable unit (one or more tightly coupled containers sharing IP and storage); a Deployment manages Pod replicas and rolling updates; a Service provides a stable virtual IP/DNS endpoint across ephemeral Pods.
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.
PowerShell Cmdlets and Object Pipeline
- Cmdlets (
Verb-Noun): Standardized built-in commands such asGet-Process,Stop-Service,Get-ChildItem,New-Item, andWhere-Object. - Object Pipeline (
|): Eliminates fragile text parsing (awk/grepcolumn slicing); downstream commands filter or format properties directly by name.
# 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 --verboseWindows 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.
| Aspect | WSL 1 (Translation Layer) | WSL 2 (Managed Lightweight VM) |
|---|---|---|
| Architecture | Translates Linux system calls directly onto the Windows NT kernel | Runs a real Microsoft-built Linux kernel inside a lightweight Hyper-V utility VM |
| System Call Compatibility | Partial (some kernel features, Docker daemon, eBPF unsupported) | Full Linux syscall compatibility (supports native Docker, systemd, FUSE) |
| Linux File I/O Speed | Slower on metadata-heavy operations (git status, npm install) | Very fast inside its virtual ext4.vhdx disk |
| Cross-OS File Access | Fast direct access to Windows NTFS files (/mnt/c/...) | Slower across the 9P bridge when accessing /mnt/c/... |
| Memory Allocation | Shared directly with Windows processes | Dynamic 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
Key Virtualization Performance Optimizations
- 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.
- 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. - 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.
- 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.
- 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
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:
- The memory overcommitment ratio (configured vRAM to available host RAM).
- 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.
The memory overcommitment ratio is . Net physical RAM used by the six VMs is , leaving of host RAM free.
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):
qemu-img create -f qcow2 srv-test.qcow2 40GLaunching 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-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=net0Verifying 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.
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.
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:
Each node can host at most Pods (RAM-limited), giving a cluster-wide maximum of Pods.
Exercises
Exercise — Hypervisors and Virtual Systems
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.
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.
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:
- Enable hardware virtualization (
VT-x/AMD-V) in host BIOS/UEFI. - Create a new VM specifying OS family, vCPU count, and RAM allocation.
- Create a dynamically expanding virtual disk (
.vdiin VirtualBox,.vmdkin VMware, or.qcow2in QEMU). - Attach the guest installer
.iso, boot and install the OS, then install Guest Additions / VMware Tools /virtiodrivers.
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.
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.
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.
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.
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
ext4disk performance, at the cost of slower cross-OS NTFS access over the 9P bridge.
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).
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:
- The vCPU-to-pCPU ratio and memory overcommitment ratio.
- 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:
The vCPU:pCPU ratio is and memory overcommitment ratio is (). Net VM RAM used is , leaving free.
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, enableseccomp/AppArmor, and use read-only root filesystems.
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).