Is it possible to re-shrink the .qcow2 image files?

yes. for your whonix images, you can use a tool called zerofree free to work, since the disks are ext4 partitions. it’s a little bit involved. but this is how i’ve gotten the best results:

  1. install the package “zerofree” in the virtual machine you want to shrink.
  2. erase what you need in the vm, run apt-get purge, apt-get autoremove, apt-get clean, etc. as needed.
  3. reboot your virtual machine in recovery mode.
  4. at the command prompt, type “systemctl stop systemd-journald.socket && systemctl stop systemd-journald.service && mount -o remount,ro / && zerofree -v /dev/vda1 && shutdown now”
  5. when the vm shuts down, open a terminal on your host.
  6. switch to root. type “sudo -s”
  7. type “cd /var/lib/libvirt/images”
  8. create a back up of the disk you want to shrink. type “mv YourVirtualMachineDisk.qcow2 YourVirtualMachineDisk.qcow2.backup”
  9. shrink the disk. type “qemu-img convert -o qcow2 -p YourVirtualMachineDisk.qcow2.backup YourVirtualMachineDisk.qcow2”
  10. boot up the vm and see if it is working. if it is, you can delete the backup of the qcow file.

there are ways you can get more creative with this using thin provisioned images that are based on a static qcow2 backing file. that’s the method i prefer. but, the above is a basic method for getting a shrunken qcow2 disk file.

1 Like