Skip navigation
This discussion is archived

Differentiating directories and mount points

1223 Views 5 Replies Latest reply: Jun 23, 2009 3:01 PM by BobHarris RSS
Rick Anderson Calculating status...
Currently Being Moderated
Jun 22, 2009 2:31 PM
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)
    Currently Being Moderated
    Jun 22, 2009 2:40 PM (in response to Rick Anderson)
    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...
    Mac OS X (10.5.7)
  • BobHarris Level 6 Level 6 (12,505 points)
    Currently Being Moderated
    Jun 22, 2009 5:57 PM (in response to Rick Anderson)
    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.
    MacBook/2GHz, PowerMac G5/2.5GHz, Mac mini/1.83Ghz, iPod Touch/32GB, Mac OS X (10.5.5), I also manage: 12" iBook G4/1GHz, MacBook Pro/2.2GHz, iMac G4/1.25GHz
  • glsmith Level 3 Level 3 (875 points)
    Currently Being Moderated
    Jun 23, 2009 8:40 AM (in response to BobHarris)
    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).
    Mac OS X (10.5.7)
  • BobHarris Level 6 Level 6 (12,505 points)
    Currently Being Moderated
    Jun 23, 2009 3:01 PM (in response to glsmith)
    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.
    MacBook/2GHz, PowerMac G5/2.5GHz, Mac mini/1.83Ghz, iPod Touch/32GB, Mac OS X (10.5.5), I also manage: 12" iBook G4/1GHz, MacBook Pro/2.2GHz, iMac G4/1.25GHz

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.