Sub topic 2
Virtual machines: a whole computer in software
This chapter assumes you can use a Linux or macOS command line, know what an operating system kernel does, and know what a process is. It covers virtual machines (VMs) as the baseline that containers are later compared against.
What a VM is
A virtual machine is a program that behaves like a complete computer. It presents a virtual CPU, virtual memory, a virtual disk, and a virtual network card. You install an operating system on those virtual parts just as you would on real hardware.
That operating system is called the guest. The physical machine and the software that runs the VM are the host. The guest does not know it is virtual. It boots, loads its own kernel, starts its own services, and manages its own processes.
Software called a hypervisor creates and runs VMs. The next chapter covers hypervisors in detail. For now, treat the hypervisor as the layer that hands each VM a slice of the real hardware.
What a VM contains
Every VM carries a full software stack:
- A guest kernel, separate from the host kernel.
- Device drivers for the virtual hardware.
- System libraries and services such as an init system, logging, and a network stack.
- Your application and its dependencies.
The VM's disk is usually a single large file on the host, called a disk image. The VM sees that file as a physical drive with partitions and a filesystem.
What this gives you
Each VM has its own kernel, so VMs are strongly separated from one another. A crash or a kernel panic in one guest does not stop the others. A guest can also run a different operating system from the host. A Linux host can run a Windows guest, and the reverse.
Because the disk is a file, you can copy it, back it up, or move it to another host. Many hypervisors also take snapshots, which record the state of a VM so you can roll back later.
What it costs
The same design that gives strong separation also adds overhead:
- Memory. Each guest kernel and its services use RAM, even when the application is idle.
- Disk. A disk image often takes several gigabytes, because it holds a whole operating system.
- Startup time. A VM must go through a full boot sequence. That commonly takes tens of seconds, though lightweight configurations can be faster.
- CPU. Most instructions run directly on the real CPU, but privileged operations and I/O pass through the hypervisor, which adds some cost.
| Property | Virtual machine |
|---|---|
| Kernel | Own guest kernel |
| Typical image size | Gigabytes |
| Typical start time | Seconds to minutes |
| Guest OS can differ from host | Yes |
| Isolation | Strong, at the hardware level |
Common mistakes
A frequent error is treating a VM as just a "bigger process." A VM is an entire operating system, so it needs its own patches, user accounts, and monitoring. Ten VMs means ten kernels to update.
Another error is assuming the guest sees all the host's resources. The hypervisor assigns a fixed share of CPUs and memory. If you give a VM 4 GB of RAM, the guest sees 4 GB, whatever the host has.
Looking ahead
Containers take a different approach. Instead of virtualizing hardware and running a second kernel, they run as isolated processes on the host's kernel. To see why that is lighter, you first need to know how the hypervisor shares hardware, which is the subject of the next chapter.
Exercise
A team runs 10 small web services, each in its own VM, and finds that most of the host's memory is used even though traffic is low. What in the VM design explains this, and what is one benefit the team still gets from it?
Show answer
Each VM runs its own guest kernel and operating system services, and these consume RAM even when the application is idle. Ten VMs means ten copies of that overhead. The benefit is strong isolation: each service has its own kernel, so a crash or compromise in one guest does not directly affect the others, and a guest could even run a different operating system.