How can I create a /dev/null-like "blackhole" directory?

I would like to create a “/dev/null” directory (or a “blackhole” directory) such that any files written to it are not really written, but just disappear.

I have an application that writes out large temporary files to a directory. I have no control over the name of the files and I don’t really care about the content of these files. I could write a script that periodically clobbers these files, but the files are written out very quickly and fill my disk. I’m looking for something cleverer. I want the application to “think” that it is writing out these files, when in fact, the writes are just being discarded at the other end.

Also see this old related thread.

Asked By: dogbane

||

If the program is so stupid to not let you switch those logs off, may be it also doesn’t check for errors after opening a log file? I’d try mounting some dummy read-only file system (e.g. using mount -o loop.)

Answered By: alex

Just symlink that directory to /dev/null

rm -rf ~/.logs
ln -s /dev/null ~/.logs

/dev/null, doesn’t have to be a directory. If the program tries writing to ~/.logs/log1.dump, it still goes right into /dev/null.
I do this for Google Chrome’s cache because after a while it gets so big that Chrome will take minutes to start.

Answered By: jonescb

You say that removing the files periodically with a script isn’t fast enough. Could you live with a trigger that deletes a temporary file any time your application finishes writing and closes it? If so, you might make use of the “inotify” API.

(See http://en.wikipedia.org/wiki/Inotify and https://github.com/rvoicilas/inotify-tools/wiki/)

Answered By: Elliot Nelson

This isn’t supported out-of-the-box on any unix I know, but you can do pretty much anything with FUSE. There’s at least one implementation of nullfs┬╣, a filesystem where every file exists and behaves like /dev/null (this isn’t the only implementation I’ve ever seen).

┬╣
Not to be confused with the *BSD nullfs, which is analogous to bindfs.

Another approach would be an LD_PRELOAD wrapper; basically a small shared library which is loaded before libc.so, and intercepts calls to open() with something that checks the prospective file path and substitutes /dev/null if it would be in the target directory.

This has the advantage of being
(a) entirely in user-space – no kernel hacking required; and
(b) only affecting the single errant application.

A simple example is at http://www.noah.org/wiki/LD_PRELOAD_notes, but in your case you will want to intercept the access(), open() & creat() system calls. For a more complete implementation you might also need to intercept chdir(), accessat(), openat(), and possibly others.

Answered By: Martin Kealey

i have created a kernel module based on the ramfs example in the linux kernel,
it is basically a blackhole filesystem called nullfsvfs. The FUSE system implementation needs to copy data from user to kernelspace and is quite slow, compared to a straight implementation as kernel module. See:

https://github.com/abbbi/nullfsvfs

Answered By: michael