I had a really stupid problem. I needed to truncate a file in place and my distribution didn’t provide this. At least it didn’t provide this when the system was created. I’m using Gentoo and the machine I am working on was put in place almost five years ago.
The nitty gritty is that we’re using Bacula as our backup solution. Occasionally Bacula stops responding and we have to restart the service. When this happens any backup that wasn’t completed and committed to the catalogue results in a corrupted volume. Our volumes are file based and are a couple hundred gigabytes. The solution I came up with so long ago was to simply dd the amount of data the catalogue thought the volume should be and copy it back over the source. Kind of like traveling back through time to a place where the backup software was happy. The problem with this method is that the we end up copying basically most of the data, minus the amount of data added from the incomplete backup, and then copying it back again. The system must then guarantee that the maximum size of the volume must be available for the copy, ie there must always be a couple hundred gigs free to complete this repair.
The best solution would be to truncate the file in place. A proper truncate command shouldn’t have to copy any parts of the file and simply inform the file system that the file ends abruptly at x bytes and be relatively instant vs io intensive. The good guys at the Gentoo forums recommend back in 2005… to simply compile the FreeBSD version. That probably resolved everyone’s problems or confused people who don’t know how to simply compile files. The problem I have is that now I have a rogue executable on the machine in the system’s bin directory or whatever the heck I will end up putting it. You can’t really trust a systems admin with corners. Therefore I created an ebuild for this file so that I could put it in the package management system on Gentoo, called Portage. If a systems administrator who is not me needs to figure out where the file came from our where the packages files are located they can now use the Portage system to figure this information out.
The newest version of coreutils just has this included. I didn’t know that until after I tried to install my new ebuild on a system with a more recent update of Gentoo. Because this is an ugly hack since the correct answer is to just update the software on the machine I didn’t go back and grab a GNU version of truncate. The provides an answer to my problem but maybe someone else’s.
If you’re not running Gentoo then you can follow my easy simple compiling directions.
- Grab the source from FreeBSD or here
- Copy it to your *inx system that doesn’t ship with truncate
- Run the command gcc -o truncate truncate-9999.c (if you’re using something other than gcc you could try replacing the gcc command with your c compiling, e.g. gcc, cc, icc…
- Test truncate out to make sure that the BSD c library isn’t too divergent from the GNU library or whatever c library you compiled for
If you’re running Gentoo then just create an over lay with my ebuild from here.