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 need a platform-independent and lightweight way of detecting the mount type of my CWD. Specifically, I need to determine if my CWD is in an SSHFS, NFS, CIFS, SMB, or local block store mount.

I need to do this on Mac OSX 10.6, 10.7, and RHEL5 (at minimum).

There's a possibility of using df, but df behaves quite differently on these two platforms. Plus, I'd have to CLI scrape its output with awk, which while workable is... fragile.

This info must be available on some level. Is there a tool that I'm unaware of that could provide this information to me?

share|improve this question

3 Answers 3

The filesystem type isn't something you can easily access in an OS-independent way. Filesystem type names are OS-dependent, so there hasn't been any incentive to establish a standard method.

On non-embedded Linux systems, stat -f -c %T . shows the filesystem type of the current directory. OSX's stat command doesn't have anything like this.

Since all you want to know is whether the filesystem type is in a finite list, under Linux, you can limit both the set of paths to print information for and the set of filesystem types. You'll get an error (“df: no file systems processed”) if the filesystem type of the current directory isn't in the list.

if df -t sshfs -t nfs -t cifs . >/dev/null 2>/dev/null; then …

The same syntax may or may not work on OSX; if it doesn't, try df -T sshfs,nfs,cifs ..

share|improve this answer

Haven't tested it, but df -P should behave the same on most platforms.

This works on Linux.

$ mount | grep "^$(df -Pk . | head -n 2 | tail -n 1 | cut -f 1 -d ' ') " | cut -f 5 -d ' '

Or breaking it into reusable functions:

# get_mount <directory>
get_mount() {
    df -Pk "$1" | head -n 2 | tail -n 1 | cut -f 1 -d ' '
}

# get_fs <mountpoint>
get_fs() {
    mount | grep "^$1 " | cut -f 5 -d ' '
}

And calling the function:

get_fs $(get_mount .)

Could be re-written to be slightly faster using sed or awk, but this way's probably easier to read.

If this doesn't work, you could try something similar, but using /etc/mtab rather than the output of mount.

share|improve this answer
    
The P in df -P is for POSIX, so it should behave in the same way. However, getting from the filesystem path to the mount point is more fiddly. –  Gilles Oct 2 '11 at 1:45
    
Your solution does not seem to be safe if directory is mounted with -o loop. At least on my system df shows the used loop device but mount shows the used image file. I guess that there could be even more cases where df does not show the same device like mount. Note this quotation from mount's man page "The listing mode is maintained for backward compatibility only. For more robust and definable output use findmnt(8), especially in your scripts." –  rudimeier Mar 11 at 13:40

On Linux there is findmnt available since util-linux 2.18.

findmnt -n -o FSTYPE -T /home
nfs4
share|improve this answer

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.