Detecting linux bindings on linux

I am looking for a way to determine if a given path is a binding junction point (on linux). Standard methods for detecting ordinary mount points do not seem to work. Even the mountpoint command cannot detect binding bindings.

+4
source share
3 answers

I'm not sure there should be a way to do this (except perhaps through /etc/mtab or /etc/fstab ), because I understand that bindings are mounted as "hard links" in the mount space (not a file hierarchy), and there is no way (after the binding has been established) to distinguish between the source and target mount points.

And why does it ask? Bind mounts (IMHO) are mostly useful for hiding such things from the point of view of the application (otherwise you would use symbolic links or even hard links, in rare cases they are possible for directories)

And the mountpoint 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 

In strace -ing mountpoint I found that it runs lstat , stat and fstat syscalls in directories like /usr/src and /usr/src/..


(added in November 2016 :)

See also /proc/mounts , for example. proc (5) and nftw (3)

+6
source

You can determine if the path is a mount point by examining the device identifier of the path and its parent (provided that the mounted file system is different from the one installed in the parent directory - I never tried to bind-set the directory to myself!).

Here's a quick command line demo:

 $ 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 that this is a mount point, then if its device identifier is used for multiple entries in /proc/mounts , you can assume that one of them is a binding binding. Finding out which one is binding and which is required is still the missing element in this answer.

0
source
 $ mount | grep bind 

I don’t quite understand what you want, does this team fit your needs?

-3
source

Source: https://habr.com/ru/post/1389053/


All Articles