Your first VM with virt-manager¶
Practical 2.2 first-vm-virt-manager-annex.pdf · PDF
To try a new operating system on a real PC you need a spare PC, a USB stick, an hour, and the courage to wipe a disk. You have none of those. You have a window.
On the Virtualisation page you checked that the class server can run virtual machines. Now you build one. In about 25 minutes there will be a second computer inside your computer — and you will catch it believing things about itself that are not quite true.
What you will learn
- Connect virt-manager to the class server (today's host, not your PC) and create a virtual machine, choosing vCPUs, RAM, a virtual disk and an ISO image
- Boot a live system from the ISO and say why nothing is saved when it restarts
- Read from inside the guest what hardware it believes it has, and compare it with the host
- Explain why a 10 GB qcow2 disk takes almost no space on the host
- Explain what the NAT network gives the guest, and what it does not
- Change the hardware of a VM without a screwdriver, and check the change from inside
A computer made of settings and files¶
Picture an empty PC case. Before it can start, you must put parts inside — you know the list from Unit 1. A virtual machine needs the same list, except every part is a setting or a file:
The shopping list (we keep it all page)
Building a VM is filling in a shopping list. Each real part you would buy for a PC has a virtual twin that costs nothing but a slice of the host.
| Real PC | Virtual machine | You will choose |
|---|---|---|
| CPU cores | vCPU: a turn on a real core, lent by the host | 2 |
| RAM sticks | A slice of the host's RAM | 4096 MiB |
| Disk | One file on the host, in the qcow2 format | 10 GiB |
| Installation USB or DVD | An ISO image: a file that is an exact copy of a disc | Debian live |
| Network cable | A virtual cable to a virtual router inside the host (NAT) | virbr0 |
An ISO image is a single file holding an exact copy of a CD, DVD or USB installer. The VM "inserts" it and boots from it, as a real PC would boot from a disc.
A live system is a complete operating system that starts straight from the disc, without installing anything. It runs in RAM.
Live systems are great for trying a system, for rescuing a broken PC — and for today. Before you start, think about one question: if it runs in RAM, what happens to your files when you switch off? Task 5 answers it.
Careful: a VM is not a simulator, like a game
With KVM the guest's instructions run on the real CPU. Only the devices around it — disk, network card, screen — are pretend. That is why it is fast.
Where is today's host?¶
On the Virtualisation page the host was your PC. Today it is not. Picture three boxes:
flowchart LR
PC["<b>Your PC</b><br>the remote control:<br>shows the VM's screen"] -->|"mouse, keyboard"| NET(["Internet"])
NET --> G
subgraph SRV ["Class server: the host, in a data centre"]
G["<b>Your guest</b><br>live-test"]
end
G -.->|"its screen"| PC
The VM runs on the class server, a real machine in a data centre. Your PC only shows the VM's screen and sends it your mouse and keyboard, like a remote control. The host is the computer that lends its hardware — today that is the server, not the PC in front of you.
Every student is a Linux user on the server, with their own libvirt, and only sees their own VMs. Your teacher gives you a slip with three things: the server address, your user name and your password.
A remote connection is virt-manager talking to libvirt on another machine instead of on your own PC. The road it travels is SSH, a secure way to log in to a computer from another one (you will meet it properly with Ubuntu Server).
Build it: connect, then the five steps of the wizard¶
First, one line that saves you a lot of typing. virt-manager opens a separate SSH road for every part of the VM's window — picture, keyboard, mouse, sound — and each one would ask for your password. This line tells SSH to open one road and let the others share it, so you type the password once. Open a terminal on your PC (Ctrl+Alt+T), paste it, press Enter. You only do it once: it stays in your account.
mkdir -p ~/.ssh && printf 'Host *\n ControlMaster auto\n ControlPath ~/.ssh/cm-%%C\n ControlPersist 10m\n' >> ~/.ssh/config
Now the connection. Open Virtual Machine Manager:
- File → Add Connection. Hypervisor: Custom URI... and type
qemu+ssh://<user>@<server>/session(your user and the server address from the slip) → Connect. - Type the password from the slip. Wait: connecting takes up to a minute. The new connection appears in the list: select it. It is empty — you have no VMs there yet.
Now the wizard. Click File → New Virtual Machine and check that Connection shows the server:
- Choose Local install media (ISO image or CDROM) → Forward.
- Type the path
/srv/isos/debian-live-13.7.0-amd64-gnome.isoin the box → Forward. Do not use Browse Local: it browses your PC, and the ISO is on the server. Leave Automatically detect ticked. If it finds nothing, or something that is not Debian, untick it and typeDebian 13(orDebian 12if the list has no 13). - Memory
4096, CPUs2→ Forward. - Create a disk image for the virtual machine:
10GiB → Forward. - Name:
live-test. Network selection: Bridge device..., Device name:virbr0→ Finish.
A new window opens with the Debian boot menu. Click inside it, choose Live system (amd64) and press Enter. In about a minute the GNOME desktop appears, logged in as the user user (password live, if it ever asks).
flowchart LR
ISO["<b>ISO image</b><br>the installer disc,<br>read-only"] -->|"boots"| VM["<b>VM live-test</b><br>2 vCPU · 4096 MiB"]
VM -->|"runs in"| RAM["<b>RAM</b><br>the live system<br>lives here"]
VM -.->|"not used today"| DISK["<b>qcow2 disk</b><br>10 GiB, empty"]
When it goes wrong¶
| Symptom | Fix |
|---|---|
| No password window; the connection hangs or fails | The program ssh-askpass is missing on your PC. Test the login in a terminal: ssh <user>@<server>, and tell your teacher |
| The password is asked again and again | The SSH line at the start of this section is missing. Close virt-manager, run the line, open it again |
| "Connecting..." for a long time | Normal for up to a minute. Do not close it |
| "Permission denied" when connecting | A typo in the password or the user name. User names look like marc_santome, not marc. Check slowly |
| The ISO is "not found" | You used Browse Local, or the path has a typo. Type it again by hand |
An error mentioning qemu-bridge-helper or virbr0 |
The server's virtual network is down. Tell your teacher |
| The mouse is trapped in the VM window | Press Ctrl+Alt (the left ones) to release it |
| The VM window is slow | The screen travels over the internet. Normal; close other VM windows |
Inside the guest: what does it believe?¶
In the guest, press the Super key (the one with the logo), type terminal and press Enter. Every command is given whole; type it and write the answer on the Practical sheet.
nproc # how many CPUs do I have?
free -h # how much RAM? (column "total", line "Mem:")
lsblk # which disks?
ip -br a # my network addresses
systemd-detect-virt # am I a virtual machine?
ping -c 3 debian.org # can I reach the internet? (3 packets, then it stops)
What you should see:
| Command | Typical answer | What it means |
|---|---|---|
nproc |
2 |
The two vCPUs you gave it |
free -h |
About 3.8Gi total |
4096 MiB minus what the firmware and kernel keep |
lsblk |
A 10G disk called vda or sda, plus sr0 and loop0 |
The qcow2 disk; sr0 is the ISO in the virtual drive; loop0 is the live system |
ip -br a |
An address 192.168.122.x/24 |
A private address from the virtual router |
systemd-detect-virt |
kvm |
The guest can find out it is virtual — if it asks |
ping -c 3 debian.org |
Three replies, 0% packet loss |
It reaches the internet |
The guest says it has a 10 GB disk. Does it know that disk is a file? No. That is the trick: the guest cannot tell.
What you just built: qcow2 and NAT¶
The disk that grows¶
qcow2 (QEMU Copy-On-Write, version 2) is the disk-file format used by QEMU. The file starts tiny and grows only as the guest writes data.
The guest sees a full 10 GB disk. The host only stores the parts the guest has actually written. This is called thin provisioning. Since the live system runs in RAM and writes nothing to the disk, the file right now is almost empty.
The network: NAT¶
Inside the host — the class server — there is a small virtual router. It gives the guest a private address (192.168.122.x) and has the address 192.168.122.1 itself. On the host it shows up as a network interface called virbr0.
When the guest talks to the internet, the host replaces the guest's address with its own. That is NAT (Network Address Translation) — the same trick the router at home does for your phone.
And you are not alone in there: every classmate's VM hangs from the same virtual router, each with its own 192.168.122.x.
flowchart LR
subgraph HOST ["Host: the class server"]
G["<b>Your guest</b><br>live-test<br>192.168.122.x"] --> VR["<b>Virtual router</b><br>virbr0<br>192.168.122.1"]
G2["<b>Your neighbour's guest</b><br>192.168.122.y"] --> VR
VR -->|"NAT: swaps the guest's<br>address for the host's"| NIC["<b>Real network card</b><br>the server's public address"]
end
NIC --> NET(["Internet"])
The consequence: the guest can go out; nobody outside can come in. Addresses and NAT are studied properly in Unit 3 — for now, the drawing is enough.
Careful: “my VM has the same IP as my PC”
Compare the guest's ip -br a with the host's: two different addresses. The guest lives in a private network that exists only inside the host — and the host is not even your PC.
On the host: the truth¶
Today the host is the class server, so that is where the truth lives. Open a terminal on your PC, not in the VM (Ctrl+Alt+T), and log in to the server with the same user and password as in virt-manager. Then run the same commands, plus one question for libvirt about the disk file:
ssh <user>@<server> # the same login as virt-manager
nproc
free -h
ip -br a
virsh -c qemu:///session vol-info --pool default live-test.qcow2
exit # back to your PC
After ssh, the prompt changes to <user>@si: you are typing on the server now. Read the virsh answer carefully:
Capacity: 10.00 GiBis what the guest sees.Allocation:is what the file really takes on the host — a couple of MiB, because the live system wrote nothing.
Fill in the comparison on the Practical sheet. It will look something like this:
Guest (live-test) |
Host | |
|---|---|---|
nproc |
2 | The server's real threads: dozens, shared by the whole class |
free -h total |
About 3.8 GiB | The server's real RAM: tens of GiB |
| Address | 192.168.122.x |
The server's public address, plus virbr0 = 192.168.122.1 |
| Disk | 10 G, looks real | A file: capacity 10 GiB, allocation of a couple of MiB |
Find the line virbr0 in the server's ip -br a: that is the virtual router from the drawing. Why does the server have so many more CPUs and so much more RAM than your PC? Because the whole class's guests live on it at once.
If virsh answers "Storage volume not found", refresh the list of disk files first: virsh -c qemu:///session pool-refresh default.
Change the hardware¶
On a real PC, adding RAM means a screwdriver. On a VM it means two clicks — but only while it is shut down.
- In the guest, open Text Editor, write your name and save the file in Documents.
- Menu Virtual Machine → Shut Down → Shut Down. A black screen is not enough — that may be the screensaver. If after 30 seconds the VM is still running, use Virtual Machine → Shut Down → Force Off: a live system has nothing to lose.
- View → Details (the light-bulb button). After the first boot virt-manager ejected the ISO and set the VM to start from its disk — which is empty. Put the disc back: SATA CDROM 1 → Source path
/srv/isos/debian-live-13.7.0-amd64-gnome.iso→ Apply. Then Boot Options → tick SATA CDROM 1 and move it to the top → Apply. - Memory:
2048→ Apply. CPUs:1→ Apply. - View → Console, press ▶, boot the live system again and open the terminal.
- Run
nprocandfree -hagain, and look for your file in Documents.
nproc now says 1, free -h about 1.9Gi — and your file is gone. The live system lives in RAM, RAM is volatile (Unit 1), and the 10 GB disk was never used. Installing — next week — is exactly the step that copies the system onto that disk.
When you finish, shut the VM down again. Do not delete it: you will use it again.
Careful: closing the window does not stop the VM
If you close the virt-manager console window, the VM keeps running on the server, using its RAM. Shut it down from the menu.
The report¶
Write practical-2.2-surname.md with the lab report template: the VM's specification as a table, the guest/host comparison, the hardware change, and your answers, in your own words, to three questions:
- What is the ISO file for?
- The guest sees a 10 GB disk. Why does the file take so little space on the host?
- Why did your file disappear after the restart?
Hand it in on Google Classroom before the deadline on the Practical sheet. The sheet shows how it is marked.
Key takeaways¶
- Today the host is the class server: virt-manager reaches it through a remote connection over SSH, and your PC only shows the VM's screen.
- A VM is a shopping list of settings and files: vCPUs, a slice of RAM, a qcow2 disk file, an ISO image and a NAT network.
- A live system boots from the ISO and runs in RAM: nothing you do survives a restart, because nothing was installed.
- The guest believes its hardware is real; commands on the host show the truth.
systemd-detect-virtanswerskvminside. - A qcow2 disk uses thin provisioning: capacity 10 GiB, allocation only what the guest has written.
- With NAT, the guest gets a private
192.168.122.xaddress from a virtual router (virbr0,192.168.122.1): it can go out, nobody can come in. - Change a VM's hardware only when it is shut down, in View → Details.
Quick check¶
1. In the new-VM wizard, the ISO file plays the role of...
- the installation DVD or USB stick
- the virtual machine's hard disk
- the virtual machine's RAM
- the virtual network cable
An ISO image is an exact copy of an installation disc, read-only. The VM's disk is a separate qcow2 file.
2. You create a 10 GiB qcow2 disk and install nothing. On the host the file takes...
- a couple of MiB, and it grows as the guest writes
- exactly 10 GiB from the first second
- 5 GiB, because half is reserved in advance
- nothing, because it only exists in RAM
qcow2 uses thin provisioning: virsh vol-info shows Capacity 10 GiB (what the guest sees) and a tiny Allocation (what is really stored).
3. With the NAT network (virbr0), where does the guest's 192.168.122.x address come from?
- A virtual router inside the host
- The school's router
- The guest's ISO file
- The internet provider
The host — today the class server — runs a small virtual router (virbr0, 192.168.122.1) that hands out private addresses and translates them with NAT when the guest goes out.
4. You saved a file in the live system, restarted, and it is gone. Why?
- The live system runs in RAM and nothing was installed on the disk
- The qcow2 disk was already full
- The NAT network blocked the file
- The ISO deleted it for safety
A live system lives in RAM, and RAM is volatile. The qcow2 disk was never touched. Installing is what copies the system onto the disk.
5. You want the VM to have 2 CPUs instead of 1. What do you do?
- Shut it down, change it in Details, start it again
- Run nproc 2 inside the guest
- Buy a second CPU for the host
- Download a bigger ISO image
Virtual hardware is a setting. Change it in View → Details while the VM is shut down, then check from inside with nproc.
6. Inside the guest, systemd-detect-virt prints kvm. What does that tell you?
- The system is running in a virtual machine on the KVM hypervisor
- KVM must be installed inside the guest before it works
- The guest has no access to the real CPU at all
- The guest is a live system and not an installed one
The guest can discover it is virtual if it asks. kvm names the hypervisor underneath. Being live or installed is a different question.
7. Your VM has 2 vCPUs and 4096 MiB; the class server has 48 threads and 64 GiB. Pick the two that are true.
- Logged in to the server, nproc still shows 48
- Inside the guest, free -h shows a little under 4 GiB total
- Two of the server's cores are now reserved and invisible to it
- The guest's ip -br a shows the same address as the server
A vCPU is a turn on a real core, so the server still sees all its threads. The guest's RAM is a bit under 4096 MiB because firmware and kernel keep a part. The guest has its own private address behind NAT.
8. You run nproc in a terminal on your lab PC and on the class server, and they disagree. Where does live-test really run?
- On the class server; the lab PC only shows its screen
- On the lab PC; the server only stores its disk file
- Half on the lab PC and half on the server
- On the internet provider's router, between both
Today the host is the class server: it lends the CPU, RAM and disk. virt-manager on your PC is only a remote connection over SSH that shows the VM's screen and sends your mouse and keyboard.
Next: today the VM's numbers were given to you. Next time you decide them yourself — Planning an installation.