I’m attempting to identify the blocks that are tied to a specific file and write zeros to them. I’ve found several methods that do this to the free space on a disk, but so far I haven’t found any slid suggestions for doing the following:
- identify the blocks for a file
- Write zeros to those blocks.
The purpose of this is for a virtualized system. This system has the ability to dedupe blocks that are identified as being the same. This is used to reduce space used by the guest OSes on the drive.
Currently this is being done using dd to write zeros to the free space on the drive. However this has the side effect on VMWare systems to cause the guest OS drive to use the entire disk space it has been allocated as from that point on the system things all the bytes have been written to.
Writing code that can safely modify even an unmounted filesystem will require significant effort. It is to be avoided unless there is no other option.
You basically have two choices to make modifying the filesystem easy:
This way you can use the (guest or host) OS’s filesystem code instead of having to roll your own. 🙂 If you can use one of those options, the code fragment listed below will fill a file with zeroes: