List only bind mounts
Bind mounts are not a filesystem type, nor a parameter of a mounted filesystem; they're parameters of a mount operation. As far as I know, the following sequences of commands lead to essentially identical system states as far as the kernel is concerned:
mount /dev/foo /mnt/one; mount --bind /mnt/one /mnt/two
mount /dev/foo /mnt/two; mount --bind /mnt/two /mnt/one
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. Therefore you need to do your own filtering.
mount | grep -E '[,(]bind[,)]'
</etc/mtab awk '$4 ~ /(^|,)bind(,|$)/'
Note that /etc/mtab
is only useful here if it's a text file maintained by mount
. Some distributions set up /etc/mtab
as a symbolic link to /proc/mounts
instead; /proc/mounts
is mostly equivalent to /etc/mtab
but does have a few differences, one of which is not tracking bind mounts.
One piece of information that is retained by the kernel, but not shown in /proc/mounts
, is when a mount point only shows a part of the directory tree on the mounted filesystem. In practice this mostly happens with bind mounts:
mount --bind /mnt/one/sub /mnt/partial
In /proc/mounts
, the entries for /mnt/one
and /mnt/partial
have the same device, the same filesystem type and the same options. The information that /mnt/partial
only shows the part of the filesystem that's rooted at /sub
is visible in the per-process mount point information in /proc/$pid/mountinfo
(column 4). Entries there look like this:
12 34 56:78 / /mnt/one rw,relatime - ext3 /dev/foo rw,errors=remount-ro,data=ordered
12 34 56:78 /sub /mnt/partial rw,relatime - ext3 /dev/foo rw,errors=remount-ro,data=ordered
Maybe this could do the trick:
findmnt | grep "\["
Example:
$ mkdir /tmp/foo
$ sudo mount --bind /media/ /tmp/foo
$ findmnt | grep "\["
│ └─/tmp/foo /dev/sda2[/media] ext4 rw,relatime,data=ordered
The kernel doesn't handle bind mounts different from normal mounts after the fact. The only differ in what happens while mount
runs.
When you mount a filesystem (eg. with mount -t ext4 /dev/sda1 /mnt
) the kernel (a bit simplified) performs three steps:
- The kernel looks for a filesystem driver for the specified filesystem type (if you omit
-t
or use-t auto
mount
guesses the type for you and provides the guessed type to the kernel) - The kernel instructs the filesystem driver to access the filesystem using the source path and any provided options. At this point the filesystem is only identified by a major:minor number pair.
- The filesystem is bound to a path (the mountpoint). The kernel also uses some of the mount options here. (
nodev
for example is an option on the mountpoint, not on the filesystem. You can have a bind mount withnodev
and one without)
If you perform a bind mount (eg. with mount --bind /a /b
) the following happens:
- The kernel resolves which filesystem contains the source path and the relative path from the mountpoint to the directory.
- The filesystem is bound to the new mountpoint using the options and the relative path.
(I'll skip mount --move
, because it's not relevant to the question.)
This quite is similar to how files are created on Linux:
- The kernel resolves which filesystem is responsible for the directory in which the file should be created.
- A new file in the filesystem is created. At this point the file only has an inode number.
- The new file is linked to a filename in the directory.
If you make a hard link the following happens:
- The kernel resolves the inode number of the source file.
- The file is linked to the destination filename.
As you can see, the created file and the hard link are indistinguishable:
$ touch first
$ ln first second
$ ls -li
1184243 -rw-rw-r-- 2 cg909 cg909 0 Feb 20 23:56 /tmp/first
1184243 -rw-rw-r-- 2 cg909 cg909 0 Feb 20 23:56 /tmp/second
But, as you can identify all hardlinks to a file by comparing the inode numbers, you can identify all mounts to a filesystem by comparing the major:minor numbers of mounts.
You can do this with findmnt -o TARGET,MAJ:MIN
or by directly looking at /proc/self/mountinfo
(see the Linux kernel documentation for more information).
The following Python script lists all bind mounts. It assumes that the oldest mount point with the shortest relative path to the root of the mounted file system is the original mount.
#!/usr/bin/python3
import os.path, re
from collections import namedtuple
MountInfo = namedtuple('MountInfo', ['mountid', 'parentid', 'devid', 'root', 'mountpoint', 'mountoptions', 'extra', 'fstype', 'source', 'fsoptions'])
mounts = {}
def unescape(string):
return re.sub(r'\\([0-7]{3})', (lambda m: chr(int(m.group(1), 8))), string)
with open('/proc/self/mountinfo', 'r') as f:
for line in f:
# Parse line
mid, pid, devid, root, mp, mopt, *tail = line.rstrip().split(' ')
extra = []
for item in tail:
if item != '-':
extra.append(item)
else:
break
fstype, src, fsopt = tail[len(extra)+1:]
# Save mount info
mount = MountInfo(int(mid), int(pid), devid, unescape(root), unescape(mp), mopt, extra, fstype, unescape(src), fsopt)
mounts.setdefault(devid, []).append(mount)
for devid, mnts in mounts.items():
# Skip single mounts
if len(mnts) <= 1:
continue
# Sort list to get the first mount of the device's root dir (if still mounted)
mnts.sort(key=lambda x: x.root)
src, *binds = mnts
# Print bind mounts
for bindmount in binds:
if src.root == bindmount.root:
srcstring = src.mountpoint
else:
srcstring = src.mountpoint+':/'+os.path.relpath(bindmount.root, src.root)
print('{0} -> {1.mountpoint} ({1.mountoptions})'.format(srcstring, bindmount))