Skip to content
Hands-on IT IT Drills

Day 2: Understanding Disks

Goal: Tell raw and qcow2 apart, create VMs as thin overlays on a shared base image, and check, repair and flatten backing chains. · Duration: approx. 60 min · Prerequisite: Day 1

Briefing

  • raw is the disk byte for byte as a file. It is fast and simple, but it cannot do snapshots and cannot be an overlay itself (it works fine as a base).
  • qcow2 (QEMU Copy-on-Write, version 2) only stores allocated blocks and supports internal snapshots, compression and backing files.
  • An overlay is a qcow2 file that only contains the changes relative to a base. Reads go to the overlay first, then to the base. This way ten VMs share one base image.
  vm-a.qcow2 (changes of A) ─┐
                             ├──► basis/basis.qcow2 (read-only, 593 MiB)
  vm-b.qcow2 (changes of B) ─┘

Drill 1: raw and qcow2

mkdir -p ~/kvm-lab/tag02 && cd ~/kvm-lab/tag02
qemu-img create -f raw platte.raw 10G
qemu-img create -f qcow2 platte.qcow2 10G

Expected (shortened):

Formatting 'platte.raw', fmt=raw size=10737418240
Formatting 'platte.qcow2', fmt=qcow2 cluster_size=65536 … size=10737418240 …
ls -lh platte.*
du -h platte.*

Expected:

-rw-r--r-- 1 ubuntu ubuntu 193K Nov 13 13:03 platte.qcow2
-rw-r--r-- 1 ubuntu ubuntu  10G Nov 13 13:03 platte.raw
196K    platte.qcow2
4.0K    platte.raw
  • For raw, ls shows 10 GB, du only 4 KB. The file is sparse: the file system does not reserve blocks for zeros. If you copy it carelessly, for example over a network drive, it turns into a real 10 GB.
  • qcow2 is small even for ls. The file only grows when the guest writes.
rm platte.*

Save your progress? Sign up for free

Drill 2: Convert and compress

qemu-img convert -f qcow2 -O raw ../images/noble-server-cloudimg-amd64.img noble.raw
du -h noble.raw

Expected:

1.8G    noble.raw

As qcow2, the cloud image was only 593 MB. Ubuntu ships it compressed. Unpacked it takes 1.8 GB. Back again with compression:

qemu-img convert -c -f raw -O qcow2 noble.raw komprimiert.qcow2
du -h komprimiert.qcow2

Expected:

592M    komprimiert.qcow2

QEMU reads compressed blocks more slowly, and the guest writes everything new uncompressed. -c pays off for images you distribute, not for running VMs.

rm noble.raw komprimiert.qcow2

Save your progress? Sign up for free

Drill 3: Base and overlays

mkdir basis
cp ../images/noble-server-cloudimg-amd64.img basis/basis.qcow2
chmod a-w basis/basis.qcow2
qemu-img create -f qcow2 -b basis/basis.qcow2 -F qcow2 vm-a.qcow2 10G
qemu-img create -f qcow2 -b basis/basis.qcow2 -F qcow2 vm-b.qcow2 10G
  • -b names the backing file, -F its format. Without -F, newer QEMU versions refuse to work, because a guessed format is a security hole.
  • chmod a-w protects the base. It must never change again, or the overlays will no longer match it.
  • The path to the base is stored in the overlay exactly as you specify it. A relative path is relative to the overlay.
ls -lh basis/*.qcow2 vm-*.qcow2
qemu-img info --backing-chain vm-a.qcow2

Expected (shortened):

-r--r--r-- 1 ubuntu ubuntu 593M Nov 13 13:07 basis/basis.qcow2
-rw-r--r-- 1 ubuntu ubuntu 193K Nov 13 13:07 vm-a.qcow2
-rw-r--r-- 1 ubuntu ubuntu 193K Nov 13 13:07 vm-b.qcow2
image: vm-a.qcow2
file format: qcow2
virtual size: 10 GiB (10737418240 bytes)
disk size: 196 KiB
backing file: basis/basis.qcow2
backing file format: qcow2
…
image: basis/basis.qcow2
file format: qcow2
virtual size: 3.5 GiB (3758096384 bytes)
disk size: 593 MiB
…

So far, two VMs with 10 GB each cost 593 MiB plus twice 196 KiB.

Save your progress? Sign up for free

Drill 4: An overlay in use

Start vm-a with the seed from Day 1, this time in the background:

qemu-system-x86_64 -enable-kvm -cpu host -m 1024 -smp 2 \
  -drive file=vm-a.qcow2,if=virtio \
  -drive file=../tag01/seed.iso,if=virtio,format=raw \
  -nic user,hostfwd=tcp::2222-:22 \
  -display none -daemonize

-display none -daemonize starts the VM without a console as a background process. Wait about a minute, then:

ssh -o StrictHostKeyChecking=no -p 2222 ubuntu@localhost \
  'echo hallo | sudo tee /root/nur-in-a.txt'
ssh -o StrictHostKeyChecking=no -p 2222 ubuntu@localhost sudo poweroff

Expected:

hallo

You need StrictHostKeyChecking=no because the guest has new host keys but listens on the same address as erste from Day 1. Lab only.

The guest still needs a few seconds after poweroff. Wait until no QEMU is running anymore:

pgrep -a qemu-system

Expected: no output.

Tried it: If you carry on immediately, every qemu-img command gives you Failed to get "write" lock and Is another process using the image [vm-a.qcow2]?. QEMU locks its disks so that two processes cannot write into them at the same time. This also protects you from accidentally starting one disk in two VMs.

ls -lh vm-*.qcow2

Expected:

-rw-r--r-- 1 ubuntu ubuntu  56M Nov 13 13:09 vm-a.qcow2
-rw-r--r-- 1 ubuntu ubuntu 193K Nov 13 13:07 vm-b.qcow2

The first boot with cloud-init, logs and your file wrote 56 MB, and only into vm-a. The base and vm-b are unchanged.

Save your progress? Sign up for free

Drill 5: Checks and internal snapshots

qemu-img check vm-a.qcow2

Expected:

No errors were found on the image.
869/163840 = 0.53% allocated, 16.57% fragmented, 0.00% compressed clusters
Image end offset: 57999360

check finds leaks and errors after a host crash. With -r all it repairs them. Both only with the VM powered off.

An internal snapshot lives in the same file:

qemu-img snapshot -c vor-update vm-a.qcow2
qemu-img snapshot -l vm-a.qcow2

Expected:

Snapshot list:
ID        TAG               VM SIZE                DATE     VM CLOCK     ICOUNT
1         vor-update            0 B 2026-11-13 13:09:05 00:00:00.000          0

VM SIZE 0 B means: only the disk is saved, not the RAM. Revert and delete:

qemu-img snapshot -a vor-update vm-a.qcow2
qemu-img snapshot -d vor-update vm-a.qcow2

Expected: no output. libvirt uses the same internal snapshots, but it can also save the RAM of a running VM (Day 8).

Save your progress? Sign up for free

Drill 6: A broken chain

What happens when someone moves the base?

mkdir neu
mv basis/basis.qcow2 neu/
qemu-img info --backing-chain vm-b.qcow2

Expected:

qemu-img: Could not open 'basis/basis.qcow2': Could not open 'basis/basis.qcow2': No such
file or directory

The VM would not start either. rebase -u only writes the new path into the overlay and does not touch any data (-u for “unsafe”):

qemu-img rebase -u -b neu/basis.qcow2 -F qcow2 vm-a.qcow2
qemu-img rebase -u -b neu/basis.qcow2 -F qcow2 vm-b.qcow2
qemu-img info --backing-chain vm-b.qcow2 | grep -E '^image|^backing file:'

Expected:

image: vm-b.qcow2
backing file: neu/basis.qcow2
image: neu/basis.qcow2

Warning: -u is only correct if the base is the same file, just in a different place. If you attach an overlay to a different base, the guest reads a mix of two disks, and the file system is broken. Without -u, rebase compares the contents; that takes time, but it is safe.

Save your progress? Sign up for free

Drill 7: Flatten instead of merging

You want to make vm-a independent of the base, for example to copy it to another host. The obvious command would be commit, which writes the changes into the base:

qemu-img commit vm-a.qcow2

Expected:

qemu-img: Could not open 'neu/basis.qcow2': Permission denied

Good. Had commit changed the base, vm-b would now be broken, because its changes only fit the old base. The right way is a new, flat file:

qemu-img convert -O qcow2 vm-a.qcow2 vm-a-flach.qcow2
qemu-img info vm-a-flach.qcow2 | head -4

Expected:

image: vm-a-flach.qcow2
file format: qcow2
virtual size: 10 GiB (10737418240 bytes)
disk size: 1.79 GiB

No more backing file line. At 1.8 GB the file is larger than base and overlay combined, because convert without -c writes uncompressed.

Save your progress? Sign up for free

Drill 8: Shrinking

You know growing from Day 1. Shrinking is dangerous, and QEMU says so clearly:

qemu-img resize vm-b.qcow2 5G

Expected:

qemu-img: Use the --shrink option to perform a shrink operation.
qemu-img: warning: Shrinking an image will delete all data beyond the shrunken image's
end. Before performing such an operation, make sure there is no important data there.

First the file system and partition in the guest must get smaller, then the file. In practice you would rather create a new, smaller disk and copy the data.

Save your progress? Sign up for free

Checkpoint

qemu-img info --backing-chain ~/kvm-lab/tag02/vm-b.qcow2 | grep '^image'

Expected: two lines, vm-b.qcow2 and neu/basis.qcow2. The chain is intact.

ls -l ~/kvm-lab/tag02/neu/basis.qcow2

Expected: The permissions start with -r--r--r--, the base is write-protected.

qemu-img check ~/kvm-lab/tag02/vm-a.qcow2

Expected: No errors were found on the image.

Challenge

Create an overlay vm-a-test.qcow2 on top of vm-a.qcow2, so that you get a chain of three files. Display the chain and then write the test overlay back into vm-a with commit.

Hint: This time commit is right, because only vm-a-test depends on vm-a.

Solution

cd ~/kvm-lab/tag02
qemu-img create -f qcow2 -b vm-a.qcow2 -F qcow2 vm-a-test.qcow2
qemu-img info --backing-chain vm-a-test.qcow2 | grep -E '^image|^backing file:'

Expected:

image: vm-a-test.qcow2
backing file: vm-a.qcow2
image: vm-a.qcow2
backing file: neu/basis.qcow2
image: neu/basis.qcow2
qemu-img commit vm-a-test.qcow2
rm vm-a-test.qcow2

Expected:

Image committed.

commit writes the changes one level down, that is, into vm-a. After that the test overlay is no longer needed. This is exactly how external snapshots work in libvirt (Day 8).

Save your progress? Sign up for free

Enterprise Note

  • Version base images: In production the base is called something like noble-2026-11.qcow2 and is never overwritten. New version, new name.
  • raw on block storage: On LVM, Ceph RBD or a SAN you often use raw, because the storage system handles snapshots and thin provisioning itself.
  • Long chains cost performance: Every level is one more lookup. Keep chains short and merge them when they are no longer needed.
  • Never copy backups from running files: A cp of the qcow2 file of a running VM produces an inconsistent image. That is what virsh backup-begin is for (Day 8).