Proxmox KVM: Trim Raw Disk

I want to move a cPanel server running on KVM to a new node. Disk type is raw:

Host max disk size: 300GB
Host actual disk size: 260GB
Real file usage on guest: 100GB

Transferring the whole backup over network takes around 3 hours and I don’t want to wait that long.

So, how to shrink the VM backup to the real file usage? I read Shrink Qcow2 Disk Files - Proxmox VE but this is not qcow2.

It’s harder than you would think…

  1. shrink guest disk size
  2. qemu-img resize on host
  3. manually edit VM config file

I would test the process out on a junk VM first to make sure you have the process down. Doing a step wrong will result in data loss because it’s a reduction operation.

More details: resize VM HDD to 800GB and my real HDD has only 80GB | Proxmox Support Forum

1 Like

Thank you!

But I think I didn’t explain correctly. I want the backup to have a lower size.

I have other machines set up with 32GB space but the backups/snapshots are only 8GB.

Edited the OP.

qemu-img -c
should work for raw as well I think.

1 Like

depends on the filesystem of your VM and if you use SCSI+discard etc.
f.i. ext4 doesn’t reuse/discard blocks of removed files, so you’ll end up with a growing disk image file even if it is/was sparse.

you can simply overwrite the unused disk-space with zeros within the guest. a simple dd if=/dev/zero into a temporary file can do that :wink:

after that backups already become smaller and a lot faster.

if you still want to resparse the image, that’s possible for RAW images too, but need the zero’d image anyways to work properly…

shutdown your VM, rename the disk image to something like .bak and run this to resparse

qemu-img convert -O raw ./vm-123-disk-1.raw.bak ./vm-123-disk-1.raw

might take some time!! be careful :wink:


Falzo has got it!

You need to convert the image, then you can move it over.

In the future, you can use qcow2… add the discard option and run fstrim on the regular. It will automatically shrink your images… however it does have a performance cost.

I cleaned stuff in the guest and I run dd. The backup size is 35GB :smiley:

Then I run qemu-img and now the disk size is 50GB :smile:

Thanks @Falzo!

I’m now using ZFS RAID1, I don’t see the option to choose disk format.

1 Like