A server is a machine with a processor, memory, and disk. Most of the time, one program uses a small fraction of it, and the rest sits idle. Virtualization is the trick that lets one physical machine pretend to be many machines, each with its own operating system, each unaware of the others. This lesson opens the machine and shows the layer in the middle.
Difficulty: Beginner · Know this first: F24 — APIs: Programs Talking to Programs
Every layer charges a little time and a little memory. The bar below is a worked example for this lesson's scenarios, not a measurement from a live system. The shape is honest: the guest OS is the biggest cost in a VM, containers skip it, and an overcommitted host swamps every other cost at once. Step through the lesson or switch the scenario and watch the bar change.
Extra diagram · from scratch to master
Start with bare hardware. Add one layer at a time until the full stack is on the map, full host included.
Extra diagram · from scratch to master
Start with one job. Add one question at a time until the choice between a VM and a container answers itself.
Size a host. Pick how many virtual machines and how many containers it must carry, and the simulator adds up the memory. The host has 64 GB in this teaching example. Each VM costs 4 GB for its work plus 1 GB for its guest operating system, and each container costs 256 MB plus 50 MB of runtime overhead. The numbers are exact for the rules in this simulator.
Each VM costs 4 GB for its work plus 1 GB for its own operating system: 5 GB in total.
Each container costs 256 MB for its work plus 50 MB of runtime overhead: 306 MB in total, about 0.3 GB.
Try this: (1) 4 VMs, 20 containers. VMs cost 20 GB, containers cost about 6.1 GB, total about 26.1 GB of 64 GB, with about 37.9 GB free. (2) 12 VMs, 0 containers. 60 GB used, 4 GB free, and the next VM will not fit. (3) 0 VMs, 100 containers. About 30.6 GB used, 33.4 GB free. The same memory carries far more containers than VMs, because there is no guest operating system in each one.
Three questions. No score is saved anywhere — this is just you against the material.
I take the same idea four times here. First I learn what it is and why it exists. Then I learn to use it and read what it tells me. Then I design with it so one failure cannot take the system down. Last I run it for the world.
Definition: Virtualization lets one physical machine pretend to be many machines. A hypervisor sits between the hardware and the guests and divides the machine. Each virtual machine runs its own operating system and believes it is alone. A container is lighter: it shares the host's kernel and carries only the app and its libraries.
Types:
Uses: I use this when a server is mostly idle. I pack more work onto it, and I keep the guests apart so one failure stays one failure.
Dimensions: At low counts everything fits and the overhead is small. What breaks first is memory, and I notice it when every guest slows down at once. It costs a little overhead per guest and a guest operating system per VM. The trade is simple: a VM buys a strong wall with memory, a container buys speed with a thinner fence.
Definition: A host is sized by adding up the promises: each VM costs its work plus its guest operating system, each container costs its work plus a small runtime overhead. The box is picked by four questions: does it need its own kernel, does it need a strong wall, must it start fast, must it scale to many copies.
Types:
Uses: I use this when I decide where a new service runs. I ask the four questions before I pick the box, and I add up the memory before I promise it.
Dimensions: As the host fills, the headroom shrinks, and a spike in one guest hurts the rest. What breaks is the promise that exceeded the hardware, and I notice it in swap activity before users notice the slowness. It costs a sizing habit: add it up, leave room, and alert before the host is full. The trade is that packing saves money and costs isolation, and the balance is set by the work, not by the budget.
Definition: At this level a virtual machine is a file: it can be snapshotted, copied, moved to another host while it runs, and restored from a backup. Hosts are emptied for maintenance without stopping the guests. A host that dies loses its guests, so the guests are spread across hosts and the important ones restart elsewhere on their own.
Types:
Uses: I use this when a host needs maintenance or dies. I move the guests first, fix the host, and move them back, and the users never know.
Dimensions: At ten times the guests the hosts are a fleet, and the fleet needs a manager that places guests, watches capacity, and moves them when a host fails. What breaks is the host nobody watched, and I notice it when its guests all fail together. It costs a control plane and the discipline to never put all copies of one service on one host. The trade is that movement buys uptime and costs complexity, and the complexity is the job.
Is this machine virtual. A machine may be a guest running inside another machine, and the guest often does not know it. I ask with systemd-detect-virt. It prints the kind of virtualisation, or none if the machine is real hardware.
$ systemd-detect-virt
systemd-nspawn
$ lscpu | grep -i virtual
Virtualization type: full
Every flag in the commands above, letter by letter.
systemd-detect-virt takes no flags here. It prints the kind of virtual machine or container, or none if the machine is bare metal. The answer here is systemd-nspawn, which means this machine is itself a container.lscpu takes no flags here. It prints CPU facts. The pipe into grep -i virtual keeps only the matching lines, case-insensitively.systemd-detect-virt -q is quiet mode. It prints nothing and answers with the exit code only. 0 means virtualised. 1 means not. Scripts use it to test am I virtualised without parsing text.Check it: run systemd-detect-virt, then systemd-detect-virt -q; echo $?. The first prints a word. The second prints 0 and nothing else.
Try it: run both commands on your own machine. If the first prints none, you are on real hardware. If it prints a name, you are a guest. A guest shares the CPU and the memory with other guests, and that sharing is the first thing to check when a guest is slow.
The CPU that can virtualise. A host needs CPU support to run guests well. The flags are vmx on Intel and svm on AMD. I count them in /proc/cpuinfo. Zero means this machine cannot host guests, and no setting will change that.
$ grep -c -E 'vmx|svm' /proc/cpuinfo
0
Every flag in the commands above, letter by letter.
grep -c: -c counts matching lines and prints only the number. Here it counts lines in /proc/cpuinfo with a virtualisation flag.grep -E: -E turns on extended regex, so | means or. The pattern matches vmx or svm. vmx is the Intel flag. svm is the AMD flag. On this machine the answer is 0, so it cannot host guests.grep -o prints only the matching part, not the whole line. Pipe it into wc -l to count matches instead of lines. -c counts lines, and one line can hold more than one match.Check it: run grep -c -E 'vmx|svm' /proc/cpuinfo. It prints 0 here. Then run grep -o -E 'vmx|svm' /proc/cpuinfo | wc -l. It also prints 0, because there are no matches at all.
Try it: run the count above. On this machine the answer is 0, so it cannot host guests. On a laptop with the flag, the answer is the core count. The flag is hardware. It is either there or it is not, and the count tells you which.
Definition: At this level I size hypervisor hosts for a whole fleet: the processor to memory ratio per workload, the storage speed per guest, the network bandwidth per host, and the failure budget that says how many hosts may die at once. I know the noisy neighbor problem by name and I isolate the guests that cannot share. I treat the host as a product with a capacity plan, not a box with room.
Types:
Uses: I use this when I run the platform others build on: I size the hosts, I set the limits, and I prove the failover works by failing a host on purpose.
Dimensions: At a hundred times the guests the hosts are spread across regions, and a region going down must not take the service with it. What breaks is the capacity plan that assumed steady growth, and I notice it in the queue of guests waiting for a host. It costs a planning cycle and the honesty to say no to a guest that does not fit. The trade is that spare capacity costs money every day and saves the service on the one day it matters.
The guest that starves. A guest shares the host with other guests, and one greedy guest can starve the rest. I compare the load average with the core count. If the load is higher than the cores for a long time, the guest is waiting for CPU it cannot get, and the fix is on the host, not in the guest.
$ uptime
00:12:30 up 17 min, 0 user, load average: 0.91, 0.89, 0.69
$ nproc
4
Every flag in the commands above, letter by letter.
uptime takes no flags here. It prints the load average for the last 1, 5, and 15 minutes.nproc takes no flags here. It prints the number of CPU cores. The load only makes sense next to this number. A load of 0.91 on 4 cores means the guest is mostly idle.uptime -p prints one short line, like up 55 minutes. And nproc --all counts every CPU the system could use, including cores taken offline.Check it: run uptime and nproc, then uptime -p and nproc --all. The short forms are for status lines. The plain forms are for me.
Try it: run both commands. A load of 0.91 on 4 cores means the guest is mostly idle. A load of 8 on 4 cores means the guest is starved. The guest cannot fix that by itself. The host has to give it more CPU, or take a noisy neighbour away.
A snapshot is the virtual machine's superpower. The hypervisor freezes the guest's disk and memory into files, and those files can be copied, stored, and restored. A broken guest can be rolled back to its snapshot in minutes. A physical machine cannot do that. That is the real reason servers live on VMs, more than packing or isolation.
The noisy neighbor is the cost of sharing that never goes away. Guests share the processor, the memory bus, and the disk. One guest working hard can slow the others, even when each one stays inside its own memory limit. The fix is isolation: pin the heavy guest to its own processors, or move it to a host where it cannot hurt anyone.
Overcommitment has two faces. Memory overcommitment hurts at once, because memory cannot be shared the way processor time can. Processor overcommitment hurts only when everyone is busy at the same time, because processors can be shared in slices. That is why a host can promise more processors than it has and get away with it, and why promising more memory than it has always ends badly.
Containers inside VMs is the common shape in production. The VM is the wall: strong isolation, its own kernel, its own patching. The containers are the rooms: fast starts, many copies, one kernel per VM. You get the wall and the speed, and you pay for both.
At 10x the traffic, the shape holds and the numbers grow. More guests, more hosts, same hypervisor in the middle. I watch host memory before guest memory, because the host is the floor everything stands on.
At 100x, the design changes. The hosts are a fleet, the fleet has a manager, and the manager places guests, balances them, and moves them when a host fails. The hypervisor is still the layer in the middle. It just has a boss now.
# list the virtual machines on this host (teaching example)
virsh list --all
# example output
# Id Name State
# 1 web-01 running
# 2 db-01 running
# - cache-01 shut off
# show one guest's memory promise
virsh dominfo web-01 | grep -i memory
Every flag in the commands above, letter by letter.
virsh list lists the guests. --all adds the shut off ones. Without it, only running guests show, and a stopped guest looks like it does not exist. The dash in the Id column means the guest has no runtime ID because it is not running.virsh dominfo web-01 prints the facts for one guest by name: state, memory, CPUs, and UUID. The pipe into grep -i memory keeps only the memory lines.virsh list --inactive shows only the shut off guests. --all shows both. --inactive shows the ones you can start.virsh is not installed here. I verified these flags against the libvirt manual, not by running them.Check it: on a host with libvirt, run virsh list, then virsh list --all. The first shows running guests only. The second adds the shut off ones, with a dash in the Id column.
What the snippet means: virsh talks to the hypervisor on a Linux host. list shows every guest and whether it is running. dominfo shows one guest's details, including the memory it was promised. If the promises add up to more than the host has, the host is overcommitted, and that is the first thing I check when every guest slows down at once.
Try it: are you a guest. Run systemd-detect-virt and the vmx count. Two commands tell you whether your machine is virtual and whether it could host guests itself. Most cloud machines are guests. Most laptops are not. Knowing which one you are on changes how you debug slowness.
$ systemd-detect-virt
systemd-nspawn
$ grep -c -E 'vmx|svm' /proc/cpuinfo
0
Every service on one host went slow at the same time, on a Tuesday afternoon. The web server, the database, the cache: all of them, all at once. Nothing had been deployed. No single service was broken. Everything was slow together, which meant the problem was below all of them.
Three things. First, the host now refuses a new guest when the promises would exceed its memory, and the refusal is loud, not silent. Second, an alert fires when host memory passes 85 percent, before swapping starts. Third, I wrote down the rule: when every guest on one host slows down at once, check the host first. A guest problem hits one guest. A host problem hits all of them, and the signature is the simultaneity. The services were never broken. The floor they stood on was full.
Design the hosting for a small company: a website, a database, a cache, and a background worker. The company has one physical server with 64 GB of memory and 16 processors. The website must stay up when the worker is busy.
Start with the count. Four parts on 64 GB. The database gets a VM with 16 GB, because it needs its own kernel tuning and a strong wall. The website gets a container with 4 GB, the cache a container with 8 GB, and the worker a container with 8 GB. That is 36 GB promised, with 28 GB free for spikes and for the hypervisor's own share. The worker is capped at its 8 GB, so a big job slows the worker, not the website.
One host is simple and is a single point of failure. A second host buys survival and costs the problem of keeping the database in sync across hosts. VMs buy strong walls and cost memory per guest. Containers buy speed and cost thinner isolation. And the worker's cap is a deliberate choice: a slow worker is a queue that drains, and a slow website is a user who leaves.