Shrink qcow2 virtual disk

IT May 5, 2024

As mentioned in my post about Looking glass I run a Windows VM on my Ubuntu machine. This works perfectly fine, but the virtual disk tends to grow slowly over time, taking up space on my SSD. I'm pretty neurotic about cleaning up my storage spaces, see it as my personal "clean deisk policy", so how can I retrieve this disk space?

Shrink Qcow2 Disk Files - Proxmox VE

Following the steps of the "Manual Solution" described in the Proxmox guide linked above, you can free up unused space taken up by the VM. A summarized version is copied next:

Prepare VM

  • Cleanup your VM
  • Defragment your Windows guest's drives
  • Download SDelete and save it in eg. C:\windows\system32\
  • Run SDelete and zero free space:
sdelete -z c:

Shrink the disk

  • Shut down VM
  • Make a backup of the virtual disk
cd /var/lib/libvirt/images
cp image.qcow2 image.qcow2_backup
    • Either shrink the disk
qemu-img convert -O qcow2 image.qcow2_backup image.qcow2
    • Or shrink and compress the disk
qemu-img convert -O qcow2 -c image.qcow2_backup image.qcow2

Results

There are two ways of shrinking the disk, with and without compression, but how do they compare? What is meant by "smaller disk size" and "takes longer to shrink"?

My CPU is a Xeon W-1250 @ 3.3 GHz (4.7 GHz turbo). My SSD is a Samsung PM9A1 NVMe PCIe 4.0 x4 (my CPU only supports PCIe 3.0). In my specific case these are the numbers.

Size:

  • before shrinking: 219424948224 bytes or ~219.4 Gigabytes.
  • after shrinking: 83322601472 bytes or ~83.3 Gigabytes
  • after shrinking with compression: 46313895424 bytes or ~46.3 Gigabytes

Shrinking time:

  • shrinking: 2m23,532s
  • shrinking with compression: 44m22,687s

Tags