Question

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

Was it helpful?

Solution

virt-clone will clone your disk image to the same file format as the original.

If your original disk is an LVM partition then you have to clone to a disk raw format image cause the lvm partition is a raw device

eg:

# virt-clone -d -o winxp -n winxpclone -f winxpclone.img

# qemu-img info winxpclone.img 
image: winxpclone.img
file format: raw
virtual size: 5.0G (5368709120 bytes)
disk size: 3.1G

You can convert the cloned disk image to qcow2:

# qemu-img convert -f raw winxpclone.img -O qcow2 winxpclone.qcow2

# qemu-img info !$ 
qemu-img info winxpclone.qcow2 
image: winxpclone.qcow2
file format: qcow2
virtual size: 5.0G (5368709120 bytes)
disk size: 3.1G
cluster_size: 65536

If your VM has a qcow2 file format disk image then virt-clone will clone your disk image to a qcow2 file format

# virt-clone -d -o winxpclone -n winxpcloneclone -f winxpcloneclone.img

# qemu-img info winxpcloneclone.img 
image: winxpcloneclone.img
file format: qcow2
virtual size: 5.0G (5368709120 bytes)
disk size: 3.1G
cluster_size: 65536

to resume:

virt-clone will keep the same file format to the cloned disk as the original.

An good solution is to use virt-sparsify

http://libguestfs.org/virt-sparsify.1.html

to reduce the exported disk image size

OTHER TIPS

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.

virt-clone now has --reflink which achieves something similar with the help of cp --reflink. This needs copy on write support in the file system, but could also be implemented via libvirt its backing chain concept. But volume cloning with --reflink for disk images in libvirt is not yet implemented in either way when the format is qcow2 even though it works for raw images stored in btrfs.

# virt-clone --reflink --auto-clone --original test
ERROR    Couldn't create storage volume 'test-clone.qcow2': 'unsupported  flags (0x2) in function virStorageBackendCreateQemuImg'`

# virsh
virsh # vol-clone --pool testpool test.qcow2 test-clone.qcow2 --reflink
error: Failed to clone vol from test.qcow2
error: unsupported flags (0x2) in function virStorageBackendCreateQemuImg

However the backing chain concept (or backing store) of libvirt works with qcow2:

virsh # vol-create-as testpool test-clone.qcow2 20G --backing-vol-format qcow2 --format qcow2 --backing-vol test.qcow2

virsh # vol-dumpxml --pool testpool test-clone.qcow2
...
<backingStore>
  <path>/testpool/test.qcow2</path>
  <format type='qcow2'/>
...

virsh # domstats --backing testvm
block.0.name=vda
block.0.path=/testpool/test-clone.qcow2
...
block.1.name=vda
block.1.path=/testpool/test.qcow2
block.1.backingIndex=1

This also works while creating a new volume with virt-manager by specifying a backing store image.

Such a clone created with qemu-imq independently of libvirt:

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

And then used in libvirt will be represented the same as above where it was created with virsh vol-create-as.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top