Working With qcow2 Images
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
- Create the
/root/vmdirectory and create/root/vm/base.qcow2as a qcow2 of 2G. - 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. - Create
/root/vm/plain.rawas a 2G raw-format image, and write the actual usage ofqcow2andrawas the two linesqcow2=<바이트>/raw=<바이트>(each placeholder is a number of bytes) in/root/vm/formats.txt. - Convert
plain.rawto qcow2 to create/root/vm/converted.qcow2.qemu-img infomust show the format asqcow2. - Create an overlay
/root/vm/vm01.qcow2that usesbase.qcow2as its backing. You must specify the backing format explicitly. - Create
/root/vm/big.qcow2of 4G withcluster_sizeset to 1M, and save the line of theqemu-img infooutput where cluster_size appears to/root/vm/cluster.txt. - Move
base.qcow2to/root/vm/golden/base.qcow2, and fix the backing reference ofvm01.qcow2to the new path. After fixing,qemu-img info --backing-chain /root/vm/vm01.qcow2must show both images without errors, and you save that output to/root/vm/chain.txt. - Create
/root/vm/inventory.csv. The first line is the headerfile,format,virtual_size,backing, and write one line each for the three imagesvm01.qcow2,converted.qcow2, andbig.qcow2.backingis the backing file path ornone.
Notes
- The order is
qemu-img create -f qcow2 <파일> 2G,qemu-img info --output=json <파일>,qemu-img convert -f raw -O qcow2 <입력> <출력>(the placeholders are the file, the input, and the output). - An overlay is
qemu-img create -f qcow2 -b <백킹> -F qcow2 <오버레이>(the placeholders are the backing file and the overlay). - Fixing the backing path is
qemu-img rebase -u -F qcow2 -b <새경로> <오버레이>(the placeholders are the new path and the overlay). - Common mistake 1: leaving out
-Fin step 5. Recent qemu-img versions give a warning or reject it. - Common mistake 2: if you only move the file in step 7 and do not rebase, you get
Could not open backing file.
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.