These last few days I wrote a few articles about using KVM/libvirt under Rocky Linux 8. In my last article I describe in detail how to increase the size of a virtual image with an ext4 filesystem. The article is in french, but the listings are universal:
I never figured out how to do the reverse, e. g. reduce the size of a virtual machine with an ext4 filesystem on it.
If anyone here in this forum has figured it out, I’d be glad to know the answer. I’m no lamer for RTFM, so a curt pointer to some documentation will do.
If you want to reduce the size of the image file, but by leaving the disk/partition data/sizes intact, then this post will help:
It will shrink your image size, thus making it like thin-provisioned. So in the future when you start to use the VM image, it will increase in size as data is added to the VM. It will still show as having a physical disk size of 40GB, or whatever was chosen during install when you boot and use the VM.
Decreasing the size of a filesystem and partition can be destructive especially when done wrong. If you have a 10GB partition, and we resize the filesystem to 2GB, and then the disk partition to 2GB, data could be lost, or even worse. Articles years ago suggested having the file system 3% smaller than the partition size itself, so in which case reducing from 10GB to 2GB for the filesystem, and then reduce the partition to 2GB + 3% to ensure it doesn’t cut off the end of the filesystem. I found that a bit close for comfort, so just don’t risk it.
When I make myself OpenStack images, I install the VM with 10GB partition, and shrink it later and end up with a 1GB qcow2 image for uploading to OpenStack. When I create an instance, it needs a flavor that has at least 10GB of space since the partition still shows 10GB, even though I shrunk the qcow2 from all the empty space on that disk.