is there a good way to shrink vm images?

hanscees

New Member
Mar 21, 2024
10
3
3
Hi,

I notice vm qcow2 images grow pretty fast. Also in the backup they are not " deflated".

I have deviced a crude way to deflate the images, but it there a supported way to do this perhaps?

Code:
#to use virt-sparsify below you must install libguestfs-tools
#apt install libguestfs-tools  (200 mb)

qm list | egrep -v VMID | awk '{print $1}' > id.list
for id in `cat id.list`; do
echo
echo now processing vm with ID $id
bootstatus=0
file_name=/mnt/pve/backup1tb/images/$id/vm-$id-disk-0.qcow2

if test -e "$file_name"; then
echo  file exists : $file_name
### uncomment what you want below
#qm stop $id
#virt-sparsify --in-place $file_name  #uncomment to use this: will smallify cow disk
#egrep "onboot: 1"  /etc/pve/qemu-server/103.conf
bootstatus=`egrep "onboot: 1"  /etc/pve/qemu-server/$id.conf`
#echo bootstatus is $bootstatus  #debug
if test -z "$bootstatus"; then
    echo "The boot variable is empty, dont boot VM."
else
        echo bootstatus is 1, start VM
        qm start $id

fi
echo end
else
echo no vm qcow2 file found for this ID
echo aborting
echo
fi
#ls -allah /mnt/pve/backup1tb/images/$id/vm-$id-disk-0.qcow2
done
 
Have you reviewed : https://pve.proxmox.com/wiki/Shrink_Qcow2_Disk_Files


Blockbridge : Ultra low latency all-NVME shared storage for Proxmox - https://www.blockbridge.com/proxmox
Hi, thank you for pointing to that howto.

The howto is not entirely clear to me however and didnt work for me: perhaps I did not follow it correctly because of that.

What does this sentence mean:
"In order to shrink the *.qcow2 files you've two options, enable TRIM support or zero out all free space of the partitions contained within the guest and then reconvert the image with qemu-img."

Do you still have to reconvert the image or not?

Also, the reconvert image steps are under the heading " manual" which states:
"This solution is no longer necessary and is only needed on Windows XP and earlier and older Linux distributions that don't support fstrim."

So is the reconvert step still needed? To me its confusing.


Also is it enough to have an qcow2 disk for it to work? Or does it also need to be on lvm-thin?
 
OK, to answer the questions above after testing:

If you have a qcow2 disk
and harddisk scsi0 has discard on
and you use fstrim inside the vm

the disks will shrink! (backup just shrunk form 1.7G to 670MB with a debian bookworm minimal system)



also, on debian fstrim -av works

on alpine its sometimes fstrim -v / (if you have one big disk), or so it seems.
 
Last edited:

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!