Find the answer to your Linux question:
Results 1 to 1 of 1
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2009
    Paris suburb

    Question Detecting bind mounts on directory subtrees


    Does anyone know if there's any way to detect a bind mount in a C program ?
    I explicit my question :
    with Linux you can mount any directory on another with the command :
    $ mount -o bind /etc /tmp
    or in C
    mount("/etc", "/tmp", NULL, MS_BIND, NULL);

    /etc and /tmp have not to be special directories, or already mountpoints, you could do it with any other directories.
    This seems great but is a real trap in fact.
    Once you did it, there seems to be no easy way to determine your destination, (in the example /tmp) is a bind mount.

    Let's imagine you have a directory toto, in it only 2 dirs : d1 and d2, d1 has a content of 1 MB and d2 is empty, you mount d1 on d2 and then run :
    $ du -sh toto
    2M toto

    even with the -x option that excludes other filesystems, du doesn't detect the bind.

    I have thought of several solutions :
    - reading in /proc/mounts or /etc/mtab, but it is not reliable enough and too complicated in my case
    - comparing inodes, becuase the mountpoint takes the same inode as the source (try the command stat), but it can be horribly consuming to keep a list of visited inodes and compare any new inode with the whole list,
    - performing a mount("dest", "dest", NULL, "MS_REMOUNT", NULL) to test if it returns EINVAL or not, but this clears the mount options if it encounters a mountpoint so it is not a harmless solution

    Does a system-call, ioctl or something exist to determine such a thing ?

    Thank you

    Last edited by pascaltaf; 10-28-2010 at 08:30 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts