Linux – How is findmnt able to list bind mounts

bind-mountlinuxmount

A lot of people keep saying that Linux does not keep information about bind mounts, so there is no way to get a list of them and their sources. Here are some examples:

  • from one of the the comments here:

    IIRC this information is not kept anywhere: after mount --bind, the two copies are equivalent, there isn't one that's more “original” than the other. After all there could be no original if you'd already unmounted /mnt.

  • from an answer on this site:

    So the only way to remember what mounts were bind mounts is the log of mount commands left in /etc/mtab. A bind mount operation is indicated by the bind mount option (which causes the filesystem type to be ignored). But mount has no option to list only filesystems mounted with a particular set of sets of options.

  • from a Debian bug report:

    This is intentional. Both mount points are fully equal in all ways so
    the kernel does not keep any flags to differentiate them.

The above is nonsense though. The tool findmnt is able to list the source paths of bind mounts (in the form of device[source-path]; I'm also trying to get it to list just the source path and not the device). If the Linux kernel is to maintain a bind mount, then that information has to be stored somewhere, otherwise it couldn't know that /home is bound to /users. So where is this data? Is it stored in some obscure region in RAM? Does findmnt look in /proc somewhere?

Best Answer

You've misunderstood a little; the two mount points are equal in terms of permissions, flags, etc because the bind effectively redirects access from one path to another. But they are still distinct.

If you look at /proc/self/mountinfo you'll see the kernel view of the mount world for this process (namespaces make things more complicated; there's not just one view of the mount table).

man 5 proc will explain the format of this file, but you can see the tree hierarchy and where bind mounts have their "parent". This is the file that findmnt parses.

Related Question