Here's a modified and much expanded repost of my answer to your previous question:
sfill
from the secure-delete
package can do what you want.
Unlike zerofree (which works only with ext2, ext3, and ext4 filesystems), sfill will work with any file system.
e.g.
sfill -l -l -z /mnt/X
The sfill/secure-delete home page is now seems to have vanished, but it is packaged for debian and ubuntu. probably other distros too. if you need source code, that can be found in the debian archives if you can't find it anywhere else.
NOTE: sfill only works on a mounted filesystem. If you're zero-filling the image files from the host system rather than from within a VM, you'll need to mount the fs image on the host. The exact method varies depending on what kind of image file it is (e.g. qcow2 or raw).
Mounting 'raw' images is easy and straight-forward. Just use kpartx
on the image file to create a loopback device and /dev/mapper/ entries for each partition can then be mounted individually.
here's a partial script modified from a fragment of the script I use to mount net-bootable freedos hard-disk images (used for bios updates on machines where flashrom won't work):
image="myrawimagefile.img"
# use kpartx to build /dev/mapper device nodes
KP=$(kpartx -a -v "$image")
# now mount each partition under ./hd/
for p in $(echo "$KP" | awk '/^add map/ {print $3}') ; do
dm="/dev/mapper/$p"
fp=$(echo "$p" | sed -r -e 's/^loop[0-9]+//')
mkdir -p "./hd/$fp"
mount "$dm" "./hd/$fp"
sfill -l -l -v "./hd/$fp"
umount "$dm"
done
# now remove the loopback device and /dev/mapper entries
kpartx -d "$image"
NOTE: it assumes every partition in the image is mountable. true in my use-case (a freedos hd image with one partition). not true if one of the partitions is, say, swap space. Detecting such partitions and using dd to zero the swap partition is left as an exercise for the reader :)
This method, or a variant of it, should work for LVM volumes too.
If your images are qcow then you can use the qemu-nbd
tool from the qemu-utils
package, which will present the image file and its partitions as network block devices - e.g. /dev/nbd0 /dev/ndb0p1 - which can be used in a similar fashion to the /dev/mapper devices above.
It's possibly easier and less hassle (but more time-consuming) to use qemu-img
to convert from qcow to raw, use the method for raw above, and then convert the modified raw image back to compressed qcow2. This will probably result in slightly smaller images than using qemu-nbd as you'll be compressing the new qcow2 image as it is being created.
sfill
suggestion? It looks like it doesn't require extX. – Kevin Aug 03 '12 at 23:53truncate
and/orfallocate
– mikeserv Dec 14 '14 at 01:20