Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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)?

share|improve this question
    
I'm fairly sure no. A mount point isn't a symlink. –  Shadur Sep 5 '12 at 9:55
add comment

3 Answers

up vote 3 down vote accepted

I think you're looking for mount -l.

share|improve this answer
    
Thanks! That is what I'm looking for. –  Vincent Sep 5 '12 at 10:08
add comment

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
share|improve this answer
add comment

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);
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.