From: "Pádraig Brady" <P@draigBrady.com>
To: Michael Tokarev <mjt@tls.msk.ru>
Cc: Linux-kernel <linux-kernel@vger.kernel.org>
Subject: Re: Detecting bind-mounts
Date: Fri, 05 Nov 2010 10:24:29 +0000 [thread overview]
Message-ID: <4CD3DB5D.5040808@draigBrady.com> (raw)
In-Reply-To: <4CD31B6A.7040902@msgid.tls.msk.ru>
On 04/11/10 20:45, Michael Tokarev wrote:
> 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.
The `stat` command recently got support for
printing the mount point for a file:
http://git.savannah.gnu.org/gitweb/?p=coreutils.git;a=commit;h=ddf6fb86
`stat` will output the alias for a bind mounted file
while `df` will output the initial mount point of its backing device
So you could do something like:
file=.
df_mnt=$(df -P "$file" | sed -n '2s/.* \([^ ]*$\)/\1/p')
stat_mnt=$(stat -c%m "$file")
test "$df_mnt" = "$stat_mnt" || echo "bind mount"
cheers,
Pádraig.
next prev parent reply other threads:[~2010-11-05 10:31 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-11-04 20:45 Detecting bind-mounts Michael Tokarev
2010-11-05 10:24 ` Pádraig Brady [this message]
2010-11-05 18:30 ` Michael Tokarev
2010-11-05 20:30 ` Michael Tokarev
2010-11-06 0:32 ` Pádraig Brady
2010-11-06 0:47 ` Michael Tokarev
2010-11-06 10:57 ` Pádraig Brady
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4CD3DB5D.5040808@draigBrady.com \
--to=p@draigbrady.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mjt@tls.msk.ru \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox