QEMU virtual disks: clearing unused blocks
How do I get Proxmox VM backups to back up actual data and not disk blocks that were written once but no longer used?
Update: the answer for me was to enable the “Discard” option in the hardware options for the VM disk. That’s documented here in the Proxmox manual and for some unknown reason the default is off. After enabling this, trim in the guest OS propagates up to the storage; running fstrim -va in the guest resulted in the ZFS volume in the hypervisor dropping from 167G used to 64G used. This works with VirtIO block devices although next time I set up a VM I’ll still prefer using VirtIO SCSI. (On the other machine which I created with a Clonezilla clone it dropped from 69G to 24G. I wonder if Clonezilla cloned all the blocks even when they weren’t being used.)
Update 2: I also had to add the discard option in fstab for my ext4 filesystem. It’s not the default. I think it may be the default for btrfs and other file systems, not positive.
I ran into a funny problem today. The Proxmox backups of my main Linux VM were taking a tidy 56GB, about as much data (compressed) as there actually is on the 2TB virtual disk. Then I accidentally created a 100GB file (all of Wikipedia in ZIM format). I removed it, df shows its gone. But now a VM backup is taking 156GB. Presumably it’s backing up all the unreferenced blocks that were deleted but still have data on them. I suspect the QEMU clones I started with weren’t copying unused blocks.
You can see how much space ZFS has reserved for a virtual disk like this
root@sfpve:/home/nelson# zfs get used rpool/data/vm-100-disk-1
NAME PROPERTY VALUE SOURCE
rpool/data/vm-100-disk-1 used 167G -
I want that 167GB to be more like 67GB.
I tried using trimfs in the VM guest but it didn’t do much. It’s not clear the trim command ever makes it to the hypervisor host OS. Anyway, fwiw: /: 35 GiB (37617963008 bytes) trimmed. Right after a reboot I ran it again and got /: 139.2 GiB (149456355328 bytes) trimmed, but the second time it was back to 35 GiB. I have no idea what any of this means.
The Proxmox docs have details on how to make trim work in VMs get through to the host OS. They require the virtual disc be of type virtio-scsi and unfortunately I created the VM with a virtio-block. Apparently the SCSI storage type is the more modern and capable one. Also note this wiki page is 4 years old and I can’t quite tell if it’s really relevant now.
virt-sparsify is a QEMU tool for removing unused blocks from an unmounted virtual disk image, see here for more details. This looks perfect for what I need. But it’s not installed by default and installing it includes bringing in all of X11 in Debian, so I’m reticent. Also it requires operating on a file and Proxmox stores VM disks in block storage as a ZFS volume.
That same old Proxmox wiki page also has another solution; just use qemu-img convert to copy the virtual disk and you’re done. The qemu tools seem to be filesystem aware and smart about avoiding unreferenced blocks, I suspect I benefitted from that when I first created the VM disk by cloning my physical disk. But again this requires operating on files and Proxmox has them the disk image as a volume.
This discussion has some notes on using qm disk instead of qemu-img to operate on disk images; it’s able to work with volumes as well as files and presumably is just calling qemu-img to do the real work. I think it’d be as simple as using import to create a new disk that’s a clone of the existing one.
My fear with any of these methods is that they might change the block IDs or device names on my Linux guest and then it won’t boot. Updating /etc/fstab is easy enough but updating Grub is harder. (See my notes here, section “Fix the block IDs”).
The current state is not awful; backups are taking 3x as long and 3x the space as they should, but they all still work. But the problem will only keep getting worse. I think I’ll let it go for now but if I come back to this, ideas to try:
- Switch to virtio-scsi for the VM and enable the trim command. See if that fixes it.
- Make a
qm disk importclone of the VM disk image and see if the new one is smaller. I can test that entirely withzfs get. - Clone the VM disk image to a file and use
qemu-imgorvirt-sparsifyto fix it.