For reducing the logical size (what a simple ls -al gives) you need to use virt-spasify or qemu-img (as described in the other answers). However, please note that this can be CPU-intensive for the host (which had to "parse" any guest write searching for repeating zeroes), so it should be only enabled in specific cases.īoth methods will reduce the physical allocated size (what du -hs shows). Therefore, in order to make it ESXi compatible I have to use the vmkfstools with the following command. This command gives me a vmdk image which is only VMWare Workstation compatible. qemu-img convert -f qcow2 myImage.qcow2 -O vmdk myNewImage.vmdk. This can be checked with du -hs on host side (note: ls -al will not show the real allocated size, just the logical one).įor even newer libvirt/qemu instances (ie: RHEL8+), guest file size can be reduced even if the guest OS does not support trim: by enabling both discard='unmap' and discard_zeroes='unmap' (and using the virtio-scsi driver), writing a sequential stream of zero on guest side (ie: via dd if=/dev/zero of=/zero.img bs=1M count=1024) will trigger host-size trimming of the affected LBA ranges. I used the image converter tool qemu with the following command. I was thinking using qemu-img or detecting the file format by looking for the magic value in the header. If the virtual machine uses virtio-scsi and the libvirt definition include discard='unmap', a simple fstrim on the guest filesystem will release assigned-but-unused free space on the host. A solution would be to query the image format with qemu-img info -outputjson, attribute format.
![qemu img create qcow2 qemu img create qcow2](https://user-images.githubusercontent.com/40992472/61710912-bb8bb500-ad8d-11e9-93cb-f39f963f3366.png)
First, we make a backup copy of the disk as shown below.
#Qemu img create qcow2 driver#
In addition to the accepted answer (which describe the more common, general method to shrink a qcow2 file), modern version (ie: RHEL7+) of the libvirt/qemu/qcow2 stack supports the virtio-scsi driver which supports the discard='unmap' option. Now we need to resize the underlying filesystems using virt-resize.Let us make a backup copy and use the backup copy of the qcow as input and use the original qcow as output.