UPDATE from 2019 (since someone upvoted and I noted it again)
Meanwhile the part of my answer that elaborates on dd
use is correct by itself, proper answer to the question how to let hypervisor know that there's some not-in-fact-occupied disk space inside its guest block storage is to leverage TRIM/Discard commands.
How can I fill the free space with zeroes in Ubuntu 12.04 (ext4)?
It's rather simple:
dd if=/dev/zero of=/some/path/to/zerofile bs=128M count=NumOf128MBlocksToFillTheDiskWith
You'd better have some space left untouched with that commands, say 1 %. By default EXT…
FSes have special amount of reserved space for root user (AFAIR, 5 %), so in case you'd run this command as ordinary user, you won't fill up the whole disk, and that's great, since another system software running as root
may malfunction in that case. But even running this dd
as ordinary user you'd better not use the rest available space (95 %), otherwise other programms like, say Firefox, wouldn't be happy to realize they can't write to disk anymore and you may end up with inconsistent data. So leave the space untouched. It'd be even better to run this in single mode, then you'd squeeze the maximum and won't hurt anything.
After that, needless to say, but just in case — simply remove /some/path/to/zerofile
.
P. S. Thanks to @Weijian for sharing info regarding zerofree
which does suit better for EXT2
/EXT3
/EXT4
.
fstrim
may do what you want. With a lot less I/O, and far faster.fstrim
work. Not sure if/when it went into the mainline kernel, but that's fairly recent. If it works for you (or you're willing to patch your kernel to make it work), that's a FAR better solution.