Howto resize a qcow2 disk image

November 8th, 2008

Resizing of the file-based qcow2 format for handling disk images is possible by performing the resize on a different format and use the conversion capabilities of qemu-img.

Having created a 1GB disk image in qcow2 format like this:

quikit:/var/kvm# qemu-img create -f qcow2 mydisk.qcow2 1G
Formatting 'mydisk.qcow2', fmt=qcow2, size=1048576 kB
quikit:/var/kvm# qemu-img info mydisk.qcow2
image: mydisk.qcow2
file format: qcow2
virtual size: 1.0G (1073741824 bytes)
disk size: 16K
cluster_size: 4096

The resize existing images the following steps are required:

  1. Convert the qcow2 disk image to raw format

    qemu-img convert -f qcow2 mydisk.qcow2 -O raw mydisk.raw
  2. Resize the raw image using dd (the file contents is not touched)

    dd if=/dev/zero of=mydisk.raw bs=1M count=0 seek=4096
  3. Convert back to the qcow2 format (only used blocks will take up diskspace)

    qemu-img convert -f raw mydisk.raw -O qcow2 newmydisk.qcow2

After conversion the qcow2 info now shows:

quikit:/var/kvm# qemu-img info newmydisk.qcow2
image: newmydisk.qcow2
file format: qcow2
virtual size: 4.0G (4294967296 bytes)
disk size: 28K
cluster_size: 4096

So resizing a partition can be as simple as that. Now try to boot the resized image, if it is not working you may have to reinstall the bootloader.

If you are using a NTFS partition you have to take special care. See the qemu forum for details.

Comments are closed.

primary