4

I have mounted a directory to another directory using mount --bind so that an sftp user of my server can access this directory.

mount --bind /path/share /path/home/user/stuff

I have put this in /etc/fstab and it is working great.

If I cd into the mounted directory and do pwd, it seems as if the directory is actually, physically there:

/path/home/user/stuff/share

Is there a command that shows where the 'original' directory is located (just as symlinks: when you do ls -fsl you see the original path)?

1

5 Answers 5

3

I think you're looking for mount -l.

0
2

Yes, the directory is there, because you mounted it there. mount --bind tells the kernel to use an existing mount and attach it to another mount point. As far as the kernel is concerned,

mount /dev/something /path/share
mount --bind /path/share /path/home/user/stuff

and

mount /dev/something /path/home/user/stuff
mount --bind /path/share /path/home/user/stuff

are the same thing. There are two ways to distinguish them:

  • If /path/share is not a mount point (i.e. it's part of a larger filesystem mounted at /path or /), you can tell the bind mount because it's not at the root of the filesystem.
  • The mount program records its actions in /etc/mtab.

The df command reads /etc/mtab, so if a directory is on a filesystem that's been mounted with the bind option, you can see what the original path was with

df -P /path/to/file | awk 'NR==2 {print $1}'

(first field of the second line). Note that this information isn't always reliable, for example it's possible that the original filesystem has been unmounted and nothing, or something else, is now mounted at this location, as in

mount /dev/something /path/share
mount --bind /path/share /path/home/user/stuff
unmount /dev/something
mount /dev/somethingelse /path/share
2

On Linux, you can now use findmnt (from util-linux) for that:

~$ sudo mount --bind /usr/local mnt
~$ cd mnt/bin
~/mnt/bin$ pwd
/home/stephane/mnt/bin
~/mnt/bin$ findmnt -T .
TARGET             SOURCE                             FSTYPE OPTIONS
/home/stephane/mnt /dev/mapper/VG-LV[/usr/local]      ext4   rw,noatime,discard,errors=remount-ro,data=ordered
1
  • This should be the accepted answer as it actually finds it for you whereas mount -l just gives you a list of mounts and it's still up to you to figure out where it is.
    – mae
    Commented Sep 3, 2021 at 3:07
1

I checked source code of df, and made a similar script that will find the original path,

e.g you bind /tmp/a to /tmp/b,

And you launch my script with perl script.pl /tmp/b/what/ever/deep/inside, you will find /tmp/a

#!/usr/bin/perl

use strict;
use warnings;
use File::Basename;

sub findmnt
{
    my $path = shift or die;

    open my $fh, 'mount|' or die;
    while (<$fh>)
    {
        return $1 if ($_ =~ qq{(.*) on $path type })
    }
};

if ($#ARGV lt 0)
{
    die 'Usage: ', $0, ' /path/to/dir', "\n";
}

my $mntpath = $ARGV[0];
while ($mntpath ne '/')
{
    my $origpath = findmnt $mntpath;
    if ($origpath ne '')
    {
        print 'Original path: ', $origpath, "\n";
        last;
    }

    $mntpath = dirname ($mntpath);
}
0

Based on daisy's response, how about...

use strict;
use warnings;
use File::Basename;

my $mntpath = $ARGV[0] or die "Missing required parameter <pathname>\n";
$mntpath = dirname ($mntpath) while (! grep / on $mntpath type /, `mount`);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .