QEMU / KVM: Using the Copy-On-Write mode

The mode “Copy-On-Write”, often referred by the acronym COW, is available on some formats of virtual machine disk as QCOW2.
Specifically, when using the COW mode, no changes are applied to the disk image. All changes are recorded in a separate file preserving the original image. Several COW files can point to the same image to test several configurations simultaneously without jeopardizing the basic system.
QEMU / KVM allows to incorporate changes from a COW file to the original image.

Kvmbanner-logo2_1

Unlike the snapshot, the copy-on-write uses multiple files and allows to simultaneously run multiple instances of the basic machine.

Setting up the COW

We assume that you have a virtual machine disk.

Passage of the disk image to the size QCOW2

We will start by converting the disk of the virtual machine on which we will workto the QCOW2 format which supports the COW mode.

~/vbox/kvm $ file ../vm/vm.vmdk 
../vm/vm.vmdk: VMware4 disk image
~/vbox/kvm $ ls -lsa ~/vbox/vm/vm.vmdk
-rw------- 1 anis users 1.1G May 28 10:58 /users/anis/vbox/vm/vm.vmdk
~/vbox/kvm $ qemu-img convert -c -O qcow2 ~/vbox/vm/vm.vmdk vm01.qcow2
~/vbox/kvm $ ls -lsa
total 486M
-rw------- 1 anis users 486M Jun  9 14:29 vm01.qcow2
~/vbox/kvm $ file vm01.qcow2 
vm01.qcow2: QEMU QCOW Image (v2), 8589934592 bytes

Note:The activation of compression about the transition from vmdk format to qcow2 format via the “-c” of “qemu-img” which, in this case reduces by more than 50% the size of the virtual machine .

Generating COW pictures

The generations of COW images are simple created images. So they go through the utility “qemu-img” in “create” which passed on the disc format (“-f Format”) and the reference image (option “-b reference image”):

~/vbox/kvm $ qemu-img create -f qcow2 -b vm01.qcow2 img01.qcow2
Formatting 'img01.qcow2', fmt=qcow2 size=8589934592 backing_file='vm01.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off 
~/vbox/kvm $ ls -lsa
total 486M
-rw------- 1 anis users 193K Jun  9 14:59 img01.qcow2
-rw------- 1 anis users 486M Jun  9 14:29 vm01.qcow2

The created image is smaller than the reference image (193K for a reference about 486M) because during the creation there is no difference between the original and the COW.
The needed image COW can be generated with the same reference.

~/vbox/kvm $ qemu-img create -f qcow2 -b vm01.qcow2 img02.qcow2
Formatting 'img02.qcow2', fmt=qcow2 size=8589934592 backing_file='vm01.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off 
~/vbox/kvm $ qemu-img create -f qcow2 -b vm01.qcow2 img03.qcow2
Formatting 'img03.qcow2', fmt=qcow2 size=8589934592 backing_file='vm01.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off 
~/vbox/kvm $ ls -lsa
total 486M
-rw------- 1 anis users 193K Jun  9 14:59 img01.qcow2
-rw------- 1 anis users 193K Jun  9 15:08 img02.qcow2
-rw------- 1 anis users 193K Jun  9 15:08 img03.qcow2
-rw------- 1 anis users 486M Jun  9 14:29 vm01.qcow2

Note: To ensure the preservation of the original image, it can be protected on written by a “chmod aw vm01.qcow2”.

Using disk images

The “differential” images can be used simultaneously:

~/vbox/kvm $ qemu-system-x86_64 img01.qcow2 &
[1] 3279
~/vbox/kvm $ qemu-system-x86_64 img02.qcow2 &
[2] 3283
~/vbox/kvm $ qemu-system-x86_64 img03.qcow2 &
[3] 3287

but their volumes represent just the difference between the original disk and the current status:

~/vbox/kvm $ ls -lsa
total 883M
-rw------- 1 anis users  75M Jun  9 15:21 img01.qcow2
-rw------- 1 anis users 7.4M Jun  9 15:30 img02.qcow2
-rw------- 1 anis users 236M Jun  9 15:30 img03.qcow2
-rw------- 1 anis users 486M Jun  9 14:29 vm01.qcow2

Information about COW pictures

The information about a COW image and its reference are obtained through “info” of the image utility “qemu-img” that is passed to the “backing-chain”:

~/vbox/kvm $ qemu-img info --backing-chain img01.qcow2 
image: img01.qcow2
file format: qcow2
virtual size: 8.0G (8589934592 bytes)
disk size: 75M
cluster_size: 65536
backing file: vm01.qcow2
 
image: vm01.qcow2
file format: qcow2
virtual size: 8.0G (8589934592 bytes)
disk size: 485M
cluster_size: 65536

In this example, the image “img01.qcow2” on the format “qcow2”, with a virtual size of 8G, actually occupies 75M space on the hard disk, it is related to “vm01.qcow2” also in the format “qcow2” occupying 485M space on the hard disk.

Change the reference image

The mode “rebase” of the disk image manager allows you to change the reference image. The new reference must be equivalent to the old one:

~/vbox/kvm $ cp vm01.qcow2 vm02.qcow2
~/vbox/kvm $ chmod a-w vm02.qcow2 
~/vbox/kvm $ qemu-img info img02.qcow2 
image: img02.qcow2
file format: qcow2
virtual size: 8.0G (8589934592 bytes)
disk size: 8.6M
cluster_size: 65536
backing file: vm01.qcow2
~/vbox/kvm $ qemu-img rebase -b vm02.qcow2 img02.qcow2
~/vbox/kvm $ qemu-img info img02.qcow2
image: img02.qcow2
file format: qcow2
virtual size: 8.0G (8589934592 bytes)
disk size: 8.6M
cluster_size: 65536
backing file: vm02.qcow2

Note: Virtual machines can be running while copying the reference disk. I tested machine references disk changes stopped and started without any trouble, but it seems logical / preferable that the VM is stopped for the reference change.

Merging the COW file and the reference

The command “commit” from the “qemu-img” applies the differences to the original image:

~/vbox/kvm $ ls -lsa
total 1.7G
-rw------- 1 anis users  76M Jun  9 16:37 img01.qcow2
-rw------- 1 anis users 9.4M Jun  9 16:36 img02.qcow2
-rw------- 1 anis users 621M Jun  9 16:35 img03.qcow2
-rw------- 1 anis users 486M Jun  9 14:29 vm01.qcow2
-rw------- 1 anis users 486M Jun  9 16:38 vm02.qcow2
~/vbox/kvm $ qemu-img rebase -b vm02.qcow2 img01.qcow2
 ~/vbox/kvm $ qemu-img commit img01.qcow2
Image committed.
~/vbox/kvm $ ls -lsa
total 1.8G
-rw------- 1 anis users  76M Jun  9 16:37 img01.qcow2
-rw------- 1 anis users 9.4M Jun  9 16:36 img02.qcow2
-rw------- 1 anis users 621M Jun  9 16:35 img03.qcow2
-rw------- 1 anis users 486M Jun  9 14:29 vm01.qcow2
-rw------- 1 anis users 559M Jun  9 16:41 vm02.qcow2

Conclusion

In this article, we change the reference of “img01.qcow2” then apply its contents to the referent disk (“commit”). The result is “vm02.qcow2” which is extending approximately to the size of the COW file.