Let's say I have
linux-vm-image.raw with size of 10GB. Inside there is one primary ("classical" MS-DOS-like partition table used, not GPT)
etx4 partition (no LVM used), extending to the entire volume. And real data allocated on that partition is, for example, 3GB.
This image is prepared to use for some virtualization environment (kvm+OpenStack in my case), and any VM created from this image would resize itself to target virtual disk size, depending on user's choice. So there are no reasons to keep any free diskspace inside of that disk image.
How could I shrink size of this raw disk image to minimal (from 10GB to ~3GB)?
There are some instructions for
qcow2 disk images, but I have to use
raw images only - not
OVF and not others.
My guess is somethink like
- mount this image somewhere as a block device
- do some maintenance to have all files inside of ext4 partition moved to "beginning" of it, another words - do a freespace defragmentation
- unmount image
- minify size of filesystem into image (to match the size of data inside of it)
- minify size of image (to match the size of filesystem inside of it)
- update partition table into image to match new partition size
But don't I try to invent a bicycle? Is there some one-line command or special software to do what I want?