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 qcow
, 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?
Best Answer
Raw images are not really resize-friendly. You can however try
zerofree
on (unmounted) filesystem and then compress the resulting image file.Also, check if your target environment supports OVF container format, which allows for compressed disk images. (zerofree still applies).