TT Lab
Get started
Learn Learning paths Courses

Virtualisation with QEMU/KVM

Working With qcow2 Images

Continue in TT Lab

Goal

Create a qcow2 image, read its information, convert it, and set up a backing chain to reproduce the golden image pattern by hand.

Why it matters

The real power of qcow2 is the backing file. Reads come from the backing and writes go only to the overlay, so you can create dozens of VMs from a single golden image. Two rules come with this. You must never modify a backing file (the overlays quietly break), and because the backing path is recorded inside the image, if you move the file you have to fix the reference with rebase. And if you cannot tell apart the virtual size and the disk size in qemu-img info, you miss thin provisioning overcommit.

Steps

  1. Create the /root/vm directory and create /root/vm/base.qcow2 as a qcow2 of 2G.
  2. Write that image's virtual size (in bytes) and actual usage (in bytes), one per line (virtual size first), in /root/vm/size.txt. The actual usage must be much smaller than the virtual size.
  3. Create /root/vm/plain.raw as a 2G raw-format image, and write the actual usage of qcow2 and raw as the two lines qcow2=<바이트> / raw=<바이트> (each placeholder is a number of bytes) in /root/vm/formats.txt.
  4. Convert plain.raw to qcow2 to create /root/vm/converted.qcow2. qemu-img info must show the format as qcow2.
  5. Create an overlay /root/vm/vm01.qcow2 that uses base.qcow2 as its backing. You must specify the backing format explicitly.
  6. Create /root/vm/big.qcow2 of 4G with cluster_size set to 1M, and save the line of the qemu-img info output where cluster_size appears to /root/vm/cluster.txt.
  7. Move base.qcow2 to /root/vm/golden/base.qcow2, and fix the backing reference of vm01.qcow2 to the new path. After fixing, qemu-img info --backing-chain /root/vm/vm01.qcow2 must show both images without errors, and you save that output to /root/vm/chain.txt.
  8. Create /root/vm/inventory.csv. The first line is the header file,format,virtual_size,backing, and write one line each for the three images vm01.qcow2, converted.qcow2, and big.qcow2. backing is the backing file path or none.

Notes

Create a qcow2 image

Create the /root/vm directory and create /root/vm/base.qcow2 as a qcow2 of size 2G.

qemu-img create takes the format, the file name, and the size. Use a suffix such as G for the size unit.

Virtual size and actual size

Write that image's virtual size (in bytes) and actual usage (in bytes), one per line (virtual size first), in /root/vm/size.txt. The actual usage must be much smaller than the virtual size.

Using the JSON output makes parsing easy. Look for the two values virtual-size and actual-size.

Compare with raw

Create /root/vm/plain.raw as a 2G raw-format image, and write the actual usage of qcow2 and raw as the two lines qcow2=<바이트> / raw=<바이트> (each placeholder is a number of bytes) in /root/vm/formats.txt.

A raw image is also created as a sparse file, but it has no metadata. Compare the actual usage of the two formats.

Format conversion

Convert plain.raw to qcow2 to create /root/vm/converted.qcow2. qemu-img info must show the format as qcow2.

convert specifies the input format with -f and the output format with -O. There is a compression option too.

Create an overlay with a backing file

Create an overlay /root/vm/vm01.qcow2 that uses base.qcow2 as its backing. You must specify the backing format explicitly.

Specify the backing file with -b and its format with -F. If you leave out -F, you get a warning or an error.

Specify cluster_size

Create /root/vm/big.qcow2 of 4G with cluster_size set to 1M, and save the line of the qemu-img info output where cluster_size appears to /root/vm/cluster.txt.

You give format-specific options with -o. You can confirm it in the Format specific information section of the info output.

Change the backing path

Move base.qcow2 to /root/vm/golden/base.qcow2, and fix the backing reference of vm01.qcow2 to the new path. After fixing, qemu-img info --backing-chain /root/vm/vm01.qcow2 must show both images without errors, and you save that output to /root/vm/chain.txt.

The -u of rebase fixes only the reference without moving data. Move the file first and then fix the reference to match.

Image inventory

Create /root/vm/inventory.csv. The first line is the header file,format,virtual_size,backing, and write one line each for the three images vm01.qcow2, converted.qcow2, and big.qcow2. backing is the backing file path or none.

Gather the format and virtual size of each image. You have to distinguish the ones that have a backing file from the ones that do not.