Does libvirt support clone qcow2 with copy on write

hellolwq picture hellolwq · Sep 12, 2012 · Viewed 14.5k times · Source

The format qcow2 support copy-on-write.detail about qcow2 is here. Does libvirt suport clone qcow2 VM Image with copy-on-write. I find none options about:

virt-clone

Answer

cbehanna picture cbehanna · Nov 14, 2014

At least as of the libvirt that ships with RHEL 7, NO, neither the virt-manager GUI nor the virsh command-line tool do the copy-on-write magic that you seek with qcow2. They will copy the backing file to a new, completely-independent backing file with identical size and contents.

What you can do instead is:

qemu-img create -f qcow2 -o backing_file=master.qcow2 clone.qcow2

And then create a new VM in virt-manager, importing clone.qcow2 as its backing file. This works really well at preserving limited storage space, as well as at very quickly provisioning new VMs, once you have the master image the way you want it.

CAVEAT: the master image MUST be treated as read-only once you have clones that are using it for a copy-on-write backing store. If you spin up a VM and alter the master image, you will corrupt all of the the clones that refer to it. If you want to make a change, what you do instead is to clone the master image to a new, independent image (call it new-master.qcow2 for the sake of discussion), then re-point your clones at the new image:

qemu-img rebase -f qcow2 -b new-master.qcow2 clone.qcow2

qemu-img will copy the differences between master.qcow2 and new-master.qcow2 into clone.qcow2, after which you can safely delete master.qcow2 once all of the copy-on-write clones have been updated.