How to shrink qcow2 file?

krato

New Member
Sep 18, 2024
4
0
1
Hello. I have a qcow2 VM with freebsd installed. The freebsd is installed on ZFS inside VM.

In the past I shrinked qcow2 file in this situation by setting a quota on the ZFS inside VM and then doing qemu-img convert. Unfortunately this time it does not work. I have no more than 40GB data on this VM and I set quota at 150GB but the resulting qcow2 file after convert is ~500GB. Can someone help me solve this mystery?

This is the process that I used:

On VM:

zfs set quota=150G zroot
zfs set refquota=150G zroot

Then I edited qemu-server config file to reflect that change (after shutting down VM of course)

On proxmox host:
pct rescan
mv vm-100-disk-0.qcow2 vm-100-disk-0.qcow2.bkp
qemu-img convert -O qcow2 vm-100-disk-0.qcow2.bkp vm-100-disk-0.qcow2

once again, there is only 40GB of data on this VM and I set quota at 150GB, here is the resulting file after convert
qemu-img info vm-100-disk-0.qcow2
image: vm-100-disk-0.qcow2
file format: qcow2
virtual size: 1 TiB (1099511627776 bytes)
disk size: 494 GiB
cluster_size: 65536
Format specific information:
compat: 1.1
compression type: zlib
lazy refcounts: false
refcount bits: 16
corrupt: false
extended l2: false
Child node '/file':
filename: vm-100-disk-0.qcow2
protocol type: file
file length: 494 GiB (530053660672 bytes)
disk size: 494 GiB


What is virtual size 1TB? the vm-100-disk-0.qcow2 file in fact takes 494GB (so this is not like a thin provisioning issue)
ls -als vm-100-disk-0.qcow2
517634948 -rw-r--r-- 1 root root 530053660672 Oct 13 02:06 vm-100-disk-0.qcow2

I am running out of space on the promox host and desperately need to convert this VM into reasonable size
 
In the past I shrinked qcow2 file in this situation by setting a quota on the ZFS inside VM and then doing qemu-img convert.
I cannot imagine how that should work. You need to change the size of the virtual disk in addition to the filesystem/ZFS inside of the disk.

Shrinking is not as easy as expanding and in the most cases you need to convert the filesystem inside from the inside (or with tools on the hypervisor). Just setting a quota in ZFS will not move data, especially if the data was written at the end. With ZFS, it's best to
  • make a backup
  • add the smaller disk to the VM
  • bootup a live system
  • import the old pool with a new name
  • snapshot the pool
  • create pool on the new disk
  • copy over the boot stuff (depends on the guest OS)
  • send/receive snapshotted data to the new pool
  • reboot and hope everything worked

If you only want to reclaim space, running zpool trim <pool> may help and then convert the image, which will removes sparse space with
qemu-img convert -O qcow2 old.qcow2 new.qcow2
 
  • Like
Reactions: UdoB

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!