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
- Strong Isolation - Complete separation at hardware level
- Different OS - Run Windows on Linux host, or vice versa
- Mature Technology - Decades of development and optimization
- Live Migration - Move running VMs between hosts
- Hardware Compatibility - Legacy applications work
Disadvantages of VMs
- Resource Overhead - Each VM needs its own OS (GBs of RAM)
- Slow Startup - Boot time is minutes
- Large Images - VM images are tens of GBs
- 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.