5 Replies Latest reply: Jun 23, 2009 3:01 PM by BobHarris
Rick Anderson Level 2 Level 2 (165 points)
Is there a way, say within a shell script, to figure out if a given directory is a mount point? My first thought was to use ls but just casually looking over the output for ls -al, I don't see any difference between a mount point and a regular directory that I could use, so I'm wondering if maybe there is some Unix command that will tell me.

Mac OS X (10.5.7)
  • glsmith Level 3 Level 3 (875 points)
    Do you mean while a file system is mounted on the mount point? Otherwise, there is no difference between a directory and a mount point.

    If you mean when a file system is mounted, you could parse the output of "mount", which would contain the directory path if a mount exists...
  • BobHarris Level 6 Level 6 (15,690 points)
    Traditionally the root directory of a file system has inode number 2. You may run across exceptions, but maybe this will work for you:

    if [[ $(stat -f%i $dir) = 2 ]]; then
    echo "this is a mount point: $dir"
    fi

    Another, maybe more reliable test, would be to look at the device of the directory and its parent directory. If they are different, then you very likely have a mount point.

    if [[ $(stat -f%r $dir) != $(stat -f%r $dir/..) ]]; then
    echo "this is a mount point: $dir"
    fi

    Except if $dir is / where / and /.. are the same place, so that would mean you have to add exception code

    if [[ $(stat -f%r $dir) != $(stat -f%r $dir/..) || $dir = / ]]; then
    echo "this is a mount point"
    fi

    The first example, using the inode, is more efficient as you do not need to invoke stat twice for each directory, as long as there are no exceptions to the inode = 2 is a root file system and thus a mount point.
  • Rick Anderson Level 2 Level 2 (165 points)
  • glsmith Level 3 Level 3 (875 points)
    Hi BobHarris,

    Checking the inode will also be unreliable when the exported directory is not the root, e.g. in BSD systems when "-alldirs" is used, allowing clients to mount at any point in the filesystem. This is a common export option on the servers I deal with.

    Perhaps that's what you meant by "exceptions", but wanted to point it out to the OP as it's not that uncommon (again, at least in my world).
  • BobHarris Level 6 Level 6 (15,690 points)
    Checking the inode will also be unreliable when the exported directory is not the root, e.g. in BSD systems when "-alldirs" is used, allowing clients to mount at any point in the filesystem. This is a common export option on the servers I deal with.

    Perhaps that's what you meant by "exceptions", but wanted to point it out to the OP as it's not that uncommon (again, at least in my world).

    Yup. That is the kind of situation that could void the inode = 2 being the mount point.

    And in that situation checking to see if the parent directory has a different device, will most likely work.