Mailinglist Archive: opensuse-virtual (12 mails)

< Previous Next >
[opensuse-virtual] QCOW2 resizing magic explained
  • From: Rob Verduijn <rob.verduijn@xxxxxxxxx>
  • Date: Fri, 7 Feb 2014 16:01:17 +0100
  • Message-id: <>
QCOW2 resizing magic explained

Hello all,

I don't know about you but Ipersonally have been rather irritated
about the fact that in the past you could not shrink a qcow2 image
online while the virtual machine was running.

I've found that this is already possible for some time and actually
rather simple to do.

Now don't get your hopes up to high there's still a few drawbacks that
you will have to deal and live with (for now).

The first drawback being that unless you already use a (virtio-)scsi
device you will need to reinstall your vm or migrate your vm-images to
(virtio-)scsi, depending on your config you should decide which is the
least hassle.

From now on to keep the virt-install commands readable I will use a
couple variables containing values that are always the same and of no
influence on the resizing of the images.

Another requirement is that you have a running kvm setup with a bridge.

STDOPTS="--name=testmachine --ram=1024 --vcpus=1 --os-type=linux
STDOPTS="$STDOPTS --network bridge=$BRIDGE --disk $CDISO,device=cdrom"

So the first requirement is to create a virtual machine with a scsi or
virtio-scsi controller.
Issue the following commandto create a vm using a scsi disk image

virt-install $STDOPTS \
--disk $DISKIMAGE,bus=scsi

Issue the following command to creat a vm with a virtio-scsi disk.
(add the Virtualization repo if you want this in opensuse)

virt-install $STDOPTS \
--disk $DISKIMAGE,format=qcow2,size=16,bus=scsi \
--controller scsi,model=virtio-scsi

Now choose the installation of your personal liking and wait for it to complete.

Once the machine is done you need to change a few entries in the fstab.

In my example I have created an installation using an lvm partition schema.
My opensuse installation produced the following fstab:

/dev/system/swap swap swap defaults 0 0
/dev/system/root / ext4 acl,user_xattr 1 1
/dev/disk/by-id/scsi-0QEMU_QEMU_HARDDISK_drive-scsi0-0-0-0-part1 /boot
ext4 acl,user_xattr 1 2
/dev/system/home /home ext4 acl,user_xattr 1 2

The discard option must be added to all filesystems that support it.

/dev/system/swap swap swap defaults 0 0
/dev/system/root / ext4 discard,acl,user_xattr 1 1
/dev/disk/by-id/scsi-0QEMU_QEMU_HARDDISK_drive-scsi0-0-0-0-part1 /boot
ext4 discard,acl,user_xattr 1 2
/dev/system/home /home ext4 discard,acl,user_xattr 1 2

Save and close the fstab

Now since I've used an lvm based partition schema I need to explain to
lvm to use the discard option.
Edit /etc/lvm/lvm.conf

find the line that says : issue_discards = 0
and change it to : issue_discards = 1

save and close the file

We have almost reached our goal.

Shut down the vm

Edit the virtual machine settings with virsh.

virsh edit testmachine
and find the entry for your harddisk.
Mine looks like this :

<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/var/lib/libvirt/images/disk.qcow2'/>
<target dev='sda' bus='scsi'/>
<address type='drive' controller='0' bus='0' target='0' unit='0'/>

add the discard=unmap option to line 2 :

<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' discard='unmap'/>
<source file='/var/lib/libvirt/images/disk.qcow2'/>
<target dev='sda' bus='scsi'/>
<address type='drive' controller='0' bus='0' target='0' unit='0'/>

save and close the file

check the file size of the disk image
ls -als /var/lib/libvirt/images/disk.qcow2
1222792 -rw------- 1 root root 1252261888 Feb 7 15:14

start the vm
log in as root on the vm and create a big file
dd if=/dev/urandom of=/bigfile count=1024k bs=1k

check the file size of the disk image
ls -als /var/lib/libvirt/images/disk.qcow2
2271560 -rw------- 1 qemu qemu 2327248896 Feb 7 15:24

delete the bigfile from the vm
rm /bigfile

and check the file of the disk image again
ls -als /var/lib/libvirt/images/disk.qcow2
1222984 -rw------- 1 qemu qemu 2327248896 Feb 7 15:27

Notice the first number has shrunk back to a smaller size, the second
is still big.
This is normal behaviour, the image is sparse therefore it claims to
be already 2.2G while it only consumes 1.2G on the hd.

You now have a virtual disk image that automatically shrinks and grows
depending on the usage.

But wait, I said there were multiple drawbacks and I've named only one yet.
So here are the others.

You might have already deduced that this causes fragmentation of the
images on your drive.
This is one of the drawbacks that you will have to live with.

Next there is the performance impact from doing this, but there is a
solution to this.
You can do the discarding manually, or in a cronjob at a time that pleases you.

To do the manual discards, remove the discard option from the /etc/fstab.
If you use lvm leave it in /etc/lvm/lvm.conf.

Now simply issue in the vm: fstrim <mountpoint>
In my example this would be

fstrim /
fstrim /home
fstrim /boot

And for the last catch, older kernels do not pass the discard option
properly when using qcow2 images.
rhel6.5 and lower don't support this, I haven't tested this in sles11 yet.

However they do support it on raw images (as do the newer kernels).
You will just have to live with the fact that your snapshots of those
systems do not support discarding.

Well that is all there is to it

Oh p.s. on a side note and completely unrelated to this subject.

Did you know there has been a new qcow release ?
Probably not since it has been done rather quietly.


But what would you care, and I would say performance.

So to add a nice finishing touch to the vm creation using qcow3...errr
I mean qcow2 compat=1.1 mode
(I'm so happy they took care not to confuse people)

issue the following two commands :

qemu-img create -f qcow2 -o
preallocation=metadata,lazy_refcounts=on,compat=1.1 \

virt-install $STDOPTS \
--disk $DISKIMAGE,bus=scsi --controller scsi,model=virtio-scsi

If you want to check what qcow version an image is, you will need a
hex editor to look at the first eight bytes of the qcow image.

Byte number eight will be a three when using qcow3 and a two for qcow2.
Glad to see they made it easy to tell the difference....<cough> <cough>

Enjoy your new fully dynamic qcow image resizing.

Rob Verduijn
To unsubscribe, e-mail: opensuse-virtual+unsubscribe@xxxxxxxxxxxx
To contact the owner, e-mail: opensuse-virtual+owner@xxxxxxxxxxxx

< Previous Next >
This Thread
  • No further messages