How to deflate/shrink .qcow2 KVM virtual disk image ?

  • Hi,

    What is the equivalent of this in KVM Centos world:

    wipe free space from inside the guest os prior:

    Windows --> SDelete –z 😄
    Linux --> cat /dev/zero > zero;sync;sleep 1;rm -r -f zero


    vmkfstools -K /vmfs/volumes/2nd_Disk_1TB_Purple/Win7_x64_SP1_Client.vmdk

    Which is punching zeros of an thin VM to get back the space in ESXi

    in Centos KVM world, the only thing I got close to working is cloning the whole VM, but that changes some details like MAC address, I tried

    virt-sparsify --in-place Windows_7x64.qcow2

    But that didn't work

  • Never tried to do this. You should be able to set the MAC as the same if you clone it.

  • @stacksofplates said in How to deflate/shrink .qcow2 KVM virtual disk image ?:

    Never tried to do this. You should be able to set the MAC as the same if you clone it.

    True, but only after you remove the original VM.

    I found solution btw, I used

    qemu-image clone -c -f qcow input.qcow2 -O qcow output.qcow2

    However it produced 6 GB qco2w image, Working 100% like the original, but the original is occupying 13 GB of disk space, perhaps it also did compressing, however when you clone you will get 13 GB new VM, which is correct.

    My scenario is the following my base windows 7 VM used space is 12-13 GB

    I download big files worth of 5 GB then delete them, thus space is counted in the Hypervisor but not in the VM cause I delete the big files as soon as the download finishes (I am simulating ESXi stuff in KVM)

    So you can say I got my answer but I wonder if compressing the images affected the performance.

  • @msff-amman-Itofficer @stacksofplates

    Got more usefull info, it seems it is cause of my -c switch:


    The convert option is used to convert one recognized image format to another image format.
    Command format:

    qemu-img convert [-c] [-f format] [-o options] [-O output_format] filename output_filename

    Convert the disk image filename to disk image output_filename using format output_format. The disk image can be optionally compressed with the -c option, or encrypted with the -o option by setting -o encryption. Note that the options available with the -o parameter differ with the selected format.

    Only the qcow2 format supports encryption or compression. qcow2 encryption uses the AES format with secure 128-bit keys. qcow2 compression is read-only, so if a compressed sector is converted from qcow2 format, it is written to the new format as uncompressed data.

    Image conversion is also useful to get a smaller image when using a format which can grow, such as qcow or cow. The empty sectors are detected and suppressed from the destination image.

Log in to reply