Day 1: QEMU by Hand
Goal: Start an Ubuntu VM with nothing but
qemu-system-x86_64, look inside via console and SSH, and see what KVM makes possible. · Duration: approx. 60 min · Prerequisite: Day 0
Briefing
- Without a management layer, a VM is a single command:
qemu-system-x86_64with options for CPU, RAM, disks and network. When the process ends, the VM is off. - As the disk you use a cloud image: a ready-installed Ubuntu without a user and password. The guest gets both on first boot from cloud-init, out of a small ISO file, the seed.
-enable-kvmturns on acceleration. Without it, QEMU emulates every CPU instruction in software (TCG, Tiny Code Generator), and that is ten times slower or more.
kvm1
└─ qemu-system-x86_64 (your user)
├─ vda erste.qcow2 copy of the cloud image
├─ vdb seed.iso user-data for cloud-init
├─ network: user-mode (10.0.2.15), port 2222 → 22
└─ console: your terminal (-nographic)
Drill 1: Install QEMU
sudo apt-get install -y qemu-system-x86 qemu-utils cloud-image-utils
qemu-system-x86_64 --versionExpected:
QEMU emulator version 8.2.2 (Debian 1:8.2.2+ds-0ubuntu1.18)
Copyright (c) 2003-2023 Fabrice Bellard and the QEMU Project developers
qemu-system-x86brings the emulator for x86 guests.qemu-utilscontainsqemu-imgfor disk files (Day 2).cloud-image-utilscontainscloud-localds, which builds the seed.
Save your progress? Sign up for free
Drill 2: The cloud image
cd ~/kvm-lab/images
wget -q https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img
qemu-img info noble-server-cloudimg-amd64.imgExpected (shortened, your sizes will differ):
image: noble-server-cloudimg-amd64.img
file format: qcow2
virtual size: 3.5 GiB (3758096384 bytes)
disk size: 593 MiB
…
The file is 593 MiB, but the guest sees a 3.5 GiB disk. The qcow2 format only stores allocated blocks. More on that on Day 2.
Save your progress? Sign up for free
Drill 3: The seed for cloud-init
mkdir -p ~/kvm-lab/tag01 && cd ~/kvm-lab/tag01
cat > user-data <<EOF
#cloud-config
hostname: erste
password: lab-console-pw-123
chpasswd: { expire: false }
ssh_authorized_keys:
- $(cat ~/.ssh/id_ed25519.pub)
EOF
cat user-dataExpected (your key will differ):
#cloud-config
hostname: erste
password: lab-console-pw-123
chpasswd: { expire: false }
ssh_authorized_keys:
- ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKBb5JErN+8f… ubuntu@kvm1
passwordapplies to the default userubuntuand is only there for the console.- The shell substitutes
$(cat …)while writing, which is whyEOFhas no quotes here.
cloud-localds seed.iso user-data
ls -lh seed.isoExpected:
-rw-rw-r-- 1 ubuntu ubuntu 366K Nov 13 12:48 seed.iso
cloud-localds builds a small ISO with the volume label
cidata. cloud-init in the guest looks for exactly such a
drive at boot. This source is called NoCloud.
Save your progress? Sign up for free
Drill 4: The VM’s disk
The downloaded image stays untouched. The VM gets a copy that you grow to 10 GB:
cp ../images/noble-server-cloudimg-amd64.img erste.qcow2
qemu-img resize erste.qcow2 10GExpected:
Image resized.
The partition in the guest grows along automatically on first boot; cloud-init takes care of that.
Save your progress? Sign up for free
Drill 5: The start
qemu-system-x86_64 -enable-kvm -cpu host -m 1024 -smp 2 \
-drive file=erste.qcow2,if=virtio \
-drive file=seed.iso,if=virtio,format=raw \
-nic user,hostfwd=tcp::2222-:22 \
-nographic| Option | Meaning |
|---|---|
-enable-kvm -cpu host |
use KVM and show the guest the real CPU |
-m 1024 -smp 2 |
1 GB RAM, 2 vCPUs |
-drive …,if=virtio |
disk as a fast virtio device, /dev/vda in the
guest |
-nic user,hostfwd=… |
network inside the QEMU process, port 2222 on kvm1 leads to port 22 in the guest |
-nographic |
no graphics, the serial console ends up in your terminal |
The boot messages scroll by. After about a minute you see (shortened, your times will differ):
erste login: [ 46.445286] cloud-init[1047]: Cloud-init v. 26.1-0ubuntu1~24.04.1
running 'modules:final' at Fri, 13 Nov 2026 12:50:08 +0000. Up 46.34 seconds.
[ 47.118406] cloud-init[1047]: Cloud-init v. 26.1-0ubuntu1~24.04.1 finished at …
Datasource DataSourceNoCloud [seed=/dev/vdb]. Up 47.08 seconds
Datasource DataSourceNoCloud [seed=/dev/vdb] means:
cloud-init found your seed. Press Enter and log in as
ubuntu with lab-console-pw-123.
Save your progress? Sign up for free
Drill 6: Inside the guest and next to it
Leave the console open and open a second SSH session to kvm1:
# second session on kvm1
ssh -p 2222 ubuntu@localhostConfirm the key with yes. In the guest:
# in the guest erste
systemd-detect-virt
lsblkExpected:
kvm
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
fd0 2:0 1 4K 0 disk
sr0 11:0 1 1024M 1 rom
vda 253:0 0 10G 0 disk
├─vda1 253:1 0 9G 0 part /
├─vda14 253:14 0 4M 0 part
├─vda15 253:15 0 106M 0 part /boot/efi
└─vda16 259:0 0 913M 0 part /boot
vdb 253:16 0 366K 0 disk
vda has 10 GB, and / has grown to 9 GB.
vdb is the seed.
# in the guest erste
ip -br a
exitExpected:
lo UNKNOWN 127.0.0.1/8 ::1/128
ens3 UP 10.0.2.15/24 metric 100 …
In the user-mode network, every guest gets
10.0.2.15. It can reach the internet, but from outside you
can only reach it through forwarded ports. You build real networks on
Day 6.
On kvm1 the VM is a completely ordinary process:
# on kvm1
ps -o pid,rss,etime,cmd -C qemu-system-x86_64Expected (shortened):
PID RSS ELAPSED CMD
30414 672344 01:20 qemu-system-x86_64 -enable-kvm -cpu host -m 1024 -smp 2 …
RSS is the real memory the process occupies, here a good
650 MB.
Save your progress? Sign up for free
Drill 7: The QEMU monitor
Back to the first session with the console. Press
Ctrl+A, release, then c. You land in QEMU’s
monitor, the control center of the process:
(qemu) info kvm
kvm support: enabled
(qemu) info cpus
* CPU #0: thread_id=31419
CPU #1: thread_id=31420
kvm support: enabledconfirms the acceleration.- Each vCPU is a separate thread of the QEMU process. The Linux scheduler distributes them across the real cores like any other thread (Day 10).
(qemu) info block
virtio0 (#block176): erste.qcow2 (qcow2)
Attached to: /machine/peripheral-anon/device[0]/virtio-backend
Cache mode: writethrough
…
Ctrl+A, then c takes you back to the
console. Shut the guest down cleanly there:
# in the console of erste
sudo poweroffOnce the guest is off, the QEMU process ends as well, and your prompt
on kvm1 returns. In an emergency, Ctrl+A, then
x ends QEMU immediately, like pulling the plug.
Save your progress? Sign up for free
Drill 8: Without KVM
What does KVM bring? Start a second copy with pure emulation
(-accel tcg) and measure the time until login:
cp ../images/noble-server-cloudimg-amd64.img tcg.qcow2
qemu-system-x86_64 -accel tcg -m 1024 -smp 2 \
-drive file=tcg.qcow2,if=virtio \
-drive file=seed.iso,if=virtio,format=raw \
-nographicIn the monitor (Ctrl+A, c),
info kvm now shows kvm support: disabled. And
then you wait:
| Acceleration | Login prompt after |
|---|---|
| KVM | approx. 46 seconds |
| TCG | approx. 600 seconds |
Tried it: Under TCG the guest was so slow that systemd lost patience:
Timed out waiting for device … /dev/disk/by-label/BOOT, thenDependency failed for boot.mountand emergency mode with(or press Control-D to continue). The boot still continued on its own, and the login prompt appeared after about 600 seconds. The VM ran, just ten times slower. If a VM on a new host runs into timeouts for no apparent reason, first check whether KVM is really active.
End the TCG VM with Ctrl+A, then x, and
clean up:
rm tcg.qcow2Save your progress? Sign up for free
Checkpoint
ls ~/kvm-lab/tag01Expected: the files erste.qcow2, seed.iso
and user-data.
qemu-img info ~/kvm-lab/tag01/erste.qcow2Expected: virtual size: 10 GiB. The
disk size line is much smaller, because only allocated
blocks take up space.
pgrep -a qemu-systemExpected: no output. All VMs are off.
Challenge
Start erste once more, this time with 2 GB RAM, 4 vCPUs
and without the seed. Check via SSH whether your key still works and how
many vCPUs the guest sees.
Hint: cloud-init only runs fully on the very first boot. Its results are stored on the disk, not in the seed.
Solution
cd ~/kvm-lab/tag01
qemu-system-x86_64 -enable-kvm -cpu host -m 2048 -smp 4 \
-drive file=erste.qcow2,if=virtio \
-nic user,hostfwd=tcp::2222-:22 \
-nographicIn a second session:
ssh -p 2222 ubuntu@localhost nprocExpected:
4
Logging in with your key still works, because the user and key now
live on the disk. RAM and vCPUs are not a property of the disk but of
the QEMU invocation. This exact separation is what libvirt manages for
you from Day 3 on. Shut the guest down with
sudo poweroff.
Save your progress? Sign up for free
Enterprise Note
- Nobody types QEMU invocations: In production,
libvirt, Proxmox (
qm showcmd) or OpenStack generate these command lines. Being able to read them helps with debugging. - User-mode network only for testing: It is slow and not reachable from outside. Production VMs are attached to bridges (Day 6).
- Cloud images instead of installation: All major distributions offer them. With cloud-init, a VM is up in a minute, without an installer and without clicking.
- Secure the monitor: The monitor can swap disks and terminate the VM. In production only libvirt talks to it, via the QMP protocol (QEMU Machine Protocol) on a socket with fixed permissions.