Michael Tokarev
2010-11-04 20:45:30 UTC
Hello.
There are quite some talks on the 'net - questions, not
answers - about detecting bind mounts - be it a directory
or a file.
There are 2 (mostly) different kinds of applications. One
is cp/tar/find with --same-filesystem option (or equivalent),
that should not cross mountpoints. And one more, apps like
mountpoint(1) from sysvinit - a utility to determine if a
given path is a mountpoint.
Neither of the two work when two directores on the same
filesystem are bind-mounted.
The usual idiom is to compare st_dev of current directory and
the parent - if they're different that's a mount point. But
in this case, two st_devs will be the same, so such a mount
point will not be detected.
It is even worse for bind-mounted files (as opposed to dirs):
there's no path/file/.. entry to stat(2), and cutting the
last component from the pathname does not work reliable due
to symlinks (it may be a symlink from different filesystem).
So far I know only one way to detect a bind mount like this,
and it is unreliable anyway. It is to parse /proc/mounts
and try to find the object(s) in question. Unreliable because
of, again, symlinks, and possible complex mounts and bind-
mounts. And this is also very slow - imagine using this way
for find/tar/cp --one-file-system.
Is there some simpler and more reliable way? Maybe use mount
syscall, like we use kill($pid, 0) to check existance of a
process?
And as far as I understand, the same applies to multiple
mounts of the same filesystem.
Thanks!
/mjt
There are quite some talks on the 'net - questions, not
answers - about detecting bind mounts - be it a directory
or a file.
There are 2 (mostly) different kinds of applications. One
is cp/tar/find with --same-filesystem option (or equivalent),
that should not cross mountpoints. And one more, apps like
mountpoint(1) from sysvinit - a utility to determine if a
given path is a mountpoint.
Neither of the two work when two directores on the same
filesystem are bind-mounted.
The usual idiom is to compare st_dev of current directory and
the parent - if they're different that's a mount point. But
in this case, two st_devs will be the same, so such a mount
point will not be detected.
It is even worse for bind-mounted files (as opposed to dirs):
there's no path/file/.. entry to stat(2), and cutting the
last component from the pathname does not work reliable due
to symlinks (it may be a symlink from different filesystem).
So far I know only one way to detect a bind mount like this,
and it is unreliable anyway. It is to parse /proc/mounts
and try to find the object(s) in question. Unreliable because
of, again, symlinks, and possible complex mounts and bind-
mounts. And this is also very slow - imagine using this way
for find/tar/cp --one-file-system.
Is there some simpler and more reliable way? Maybe use mount
syscall, like we use kill($pid, 0) to check existance of a
process?
And as far as I understand, the same applies to multiple
mounts of the same filesystem.
Thanks!
/mjt