Chapter 2: Virtual Machines

Virtual Machines Deep Dive

Understanding how Virtual Machines work - hypervisors, hardware virtualization, and the architecture that enables running multiple operating systems on a single machine.

Virtual Machines Deep Dive

Now let's understand how Virtual Machines actually work under the hood. This knowledge will help you make better decisions about when to use VMs.

What is a Hypervisor?

A hypervisor (also called Virtual Machine Monitor or VMM) is the software layer that creates and manages virtual machines. It sits between the hardware and the VMs, allocating resources and providing isolation.

Type 1 Hypervisors (Bare Metal)

These run directly on the physical hardware, without a host operating system.

┌─────────┐ ┌─────────┐ ┌─────────┐
│   VM1   │ │   VM2   │ │   VM3   │
└────┬────┘ └────┬────┘ └────┬────┘
     └───────────┼───────────┘
          ┌──────┴──────┐
          │  Hypervisor │  (Type 1)
          └──────┬──────┘
          ┌──────┴──────┐
          │  Hardware   │
          └─────────────┘

Examples:

  • VMware ESXi
  • Microsoft Hyper-V
  • Xen
  • KVM (technically Type 1.5)

Used in: Data centers, cloud providers (AWS, Google Cloud, Azure)

Type 2 Hypervisors (Hosted)

These run on top of a conventional operating system.

┌─────────┐ ┌─────────┐
│   VM1   │ │   VM2   │
└────┬────┘ └────┬────┘
     └─────┬─────┘
    ┌──────┴──────┐
    │  Hypervisor │  (Type 2)
    └──────┬──────┘
    ┌──────┴──────┐
    │   Host OS   │
    └──────┬──────┘
    ┌──────┴──────┐
    │  Hardware   │
    └─────────────┘

Examples:

  • VMware Workstation/Fusion
  • VirtualBox
  • Parallels Desktop

Used in: Development, testing, desktop virtualization

Hardware Virtualization

Modern CPUs have built-in support for virtualization:

  • Intel VT-x (Virtualization Technology)
  • AMD-V (AMD Virtualization)

These features allow:

  • Direct execution of guest code on the CPU
  • Hardware-assisted memory virtualization
  • Better performance than software-only virtualization

How VMs Work

1. CPU Virtualization

The hypervisor intercepts privileged CPU instructions from the guest OS and handles them safely.

Guest OS tries to access hardware
        ↓
Hypervisor intercepts (VM Exit)
        ↓
Hypervisor handles the request
        ↓
Control returns to guest (VM Entry)

2. Memory Virtualization

Each VM thinks it has its own physical memory, but the hypervisor manages the actual mapping.

Guest Physical Address → Host Physical Address
     (what VM sees)          (actual RAM)

Extended Page Tables (EPT) and Nested Page Tables (NPT) handle this in hardware.

3. I/O Virtualization

Devices like network cards and storage are virtualized:

  • Emulation: Software pretends to be hardware (slow)
  • Paravirtualization: Guest knows it's virtualized, uses efficient APIs
  • Passthrough: Direct hardware access (fastest, but less flexible)

Resource Allocation

VMs are allocated resources at creation time:

VM Configuration:
  vCPUs: 4
  Memory: 16GB
  Disk: 100GB
  Network: 1 Gbps

These resources are:

  • Reserved - Guaranteed minimum
  • Limit - Maximum allowed
  • Shares - Priority when competing for resources

VM Lifecycle

Create → Power On → Running → Suspend/Snapshot → Power Off → Delete
                      ↑           ↓
                      └───────────┘
                        (Resume)

Snapshots capture the entire VM state, enabling:

  • Point-in-time recovery
  • Easy cloning
  • Safe testing

Advantages of VMs

  1. Strong Isolation - Complete separation at hardware level
  2. Different OS - Run Windows on Linux host, or vice versa
  3. Mature Technology - Decades of development and optimization
  4. Live Migration - Move running VMs between hosts
  5. Hardware Compatibility - Legacy applications work

Disadvantages of VMs

  1. Resource Overhead - Each VM needs its own OS (GBs of RAM)
  2. Slow Startup - Boot time is minutes
  3. Large Images - VM images are tens of GBs
  4. Licensing - Each OS instance may need a license

VMs in the Cloud

Cloud providers use VMs extensively:

Provider VM Service
AWS EC2
Google Cloud Compute Engine
Azure Virtual Machines
Float16 GPU Instances

When you rent a "server" from a cloud provider, you're typically getting a VM.

What's Next?

In the next chapter, we'll explore Containers in the same depth - understanding Docker, container images, and the container runtime.