Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

detecting bind mounts on linux

Tags:

c++

python

c

linux

I am looking for a way to determine if a given path is a bind mount point (on linux). The standard techniques for detecting regular mount points don't seem to work. Even the mountpoint command fails to detect bind mounts.

like image 702
Shamless Avatar asked Jan 03 '12 10:01

Shamless


People also ask

How check mount bind in Linux?

Once you know it's a mountpoint, then if its device id is commont to multiple entries in /proc/mounts , you can assume that one of them is a bind mount.

How do I know what bind my mount is?

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.

What are bind mounts in Linux?

“ - [Instructor] A bind mount allows us to mount a file system or a subset of a file system in two places at once. They can be used for various reasons when parts of the file system need to be made available in different places.


2 Answers

I'm not sure there is supposed to be a way to do that (except perhaps thru /etc/mtab or /etc/fstab) because I understand that bind mounts are sort-of "hard links" in the mount space (not the file hierarchy), and there is no way (once the bind mount happened) to distinguish the source and the target mount points.

And why are asking that? Bind mounts are (IMHO) mostly useful to hide such things from the application's point of view (otherwise you would use symlinks -or even hard links, in the rare cases they are possible- for directories)

And mountpoint which I just discovered thanks to your question seems to see something:

% grep /home /etc/fstab
UUID=000008-0003-000c-9ecd-0f1a /home           ext3    defaults        0       2
% grep /usr/src /etc/fstab
/home/Src /usr/src none bind 0 0
% mountpoint /usr/src
/usr/src is a mountpoint
% mountpoint /home/Src
/home/Src is not a mountpoint

By strace-ing mountpoint I find that it is doing lstat,stat and fstat syscalls on directories like /usr/src & /usr/src/..


(added in november 2016:)

See also /proc/mounts e.g. proc(5), and nftw(3)

like image 140
Basile Starynkevitch Avatar answered Sep 28 '22 15:09

Basile Starynkevitch


You can detect if a path is a mountpoint, by examining the device id of the path and of its parent (provided that the mounted filesystem is different to that of the parent directory - I've never tried bind-mounting a directory onto itself!).

Here's a quick command-line demonstration:

$ cut -d ' ' -f2 /proc/mounts | xargs stat -c '%d %n'
18 /sys
4 /proc
6 /dev
19 /dev/pts
20 /run
2049 /
7 /sys/kernel/security
21 /dev/shm
22 /run/lock
23 /sys/fs/cgroup
24 /sys/fs/cgroup/unified
25 /sys/fs/cgroup/systemd
26 /sys/fs/pstore
27 /sys/fs/cgroup/perf_event
28 /sys/fs/cgroup/cpu,cpuacct
29 /sys/fs/cgroup/pids
30 /sys/fs/cgroup/blkio
31 /sys/fs/cgroup/memory
32 /sys/fs/cgroup/cpuset
33 /sys/fs/cgroup/net_cls,net_prio
34 /sys/fs/cgroup/devices
35 /sys/fs/cgroup/freezer
39 /proc/sys/fs/binfmt_misc
17 /dev/mqueue
8 /sys/kernel/debug
37 /dev/hugepages
2066 /home
39 /proc/sys/fs/binfmt_misc
44 /run/user/1000
45 /sys/fs/fuse/connections
2049 /run/schroot/mount/my-chroot-cb43935e-0812-45ea-af4f-965a1e2de91d
4 /run/schroot/mount/my-chroot-cb43935e-0812-45ea-af4f-965a1e2de91d/proc
18 /run/schroot/mount/my-chroot-cb43935e-0812-45ea-af4f-965a1e2de91d/sys
6 /run/schroot/mount/my-chroot-cb43935e-0812-45ea-af4f-965a1e2de91d/dev
19 /run/schroot/mount/my-chroot-cb43935e-0812-45ea-af4f-965a1e2de91d/dev/pts
2066 /run/schroot/mount/my-chroot-cb43935e-0812-45ea-af4f-965a1e2de91d/home
2049 /run/schroot/mount/my-chroot-cb43935e-0812-45ea-af4f-965a1e2de91d/tmp

Once you know it's a mountpoint, then if its device id is commont to multiple entries in /proc/mounts, you can assume that one of them is a bind mount. Finding out which one is the bind and which is the bound-to is still a missing piece from this answer.

like image 20
Toby Speight Avatar answered Sep 28 '22 15:09

Toby Speight