detached directories

June 18, 2007

So one thing I wanted for my Graphical Boot stuff was a standalone environment for it to work in. I wanted it to have its own /proc, device nodes, etc, because it gets run so early in the boot process that those things aren’t set up yet globally. If I make my program set them up globally, then other parts of the boot process get confused that things are set up already. What I really want is my own private directory that only my process can see and that goes away when my process exits.

This is something you can do with individual files pretty easily. Something like:

int fd;
char file[] ="/tmp/XXXXXX";
fd = mkstemp (file);
unlink (file);

It turns out you can do something similar with directories, too, although there are some caveats. If your program

  1. creates a temp directory
  2. mounts a ram filesystem in the directory
  3. opens the directory and stores the file descriptor somewhere
  4. lazily unmounts the ram filesystem
  5. remove the temp directory

Then the directory will no longer be visible from the filesystem, but will exist as long as the stored file descriptor is opened. The program can fchdir() to the directory using the saved file descriptor and work with it. Now some of the caveats are:

  1. the program needs to be root to mount the filesystem
  2. MNT_DETACH (the mount flag used to lazily unmount a filesystem) is unsupported api
  3. you can’t mount other filesystems in your detached directory

Overall, it’s kind of a neat concept, but those caveats make it fairly impractical to use.

Leave a Reply