What is the best way (reliable, portable, etc.) to check if a given folder is on a mounted remote (nfs) filesystem within a shell script?
I am looking for a command that would look like:
chk-remote-mountpoint /my/path/to/folder
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
As Stephane says “there is no universal Unix answer to that”.
The best solution I have found to my question:
df -P -T /my/path/to/folder | tail -n +2 | awk '{print $2}'
will return the filesystem type, for example: nfs or ext3.
The -T option is not standard, so it may not work on other Unix/Linux systems…
According to Gilles‘ comment below: “This works on any non-embedded Linux, but not on BusyBox, *BSD, etc.”
Method 2
You could use GNU stat.
%m to find out the mountpoint.
$ stat --format=%m /usr/src/linux /usr/src
%T (in file-system mode) to find out the name of the file system.
$ stat --file-system --format=%T /usr/src/linux reiserfs
Thus you know that /usr/src/linux, on my system, is stored in a filesystem that is mounted on /usr/src and has the filesystem type reiserfs.
Also refer to man stat for further reference. It’s a very versatile command, useful almost always when you need info about files and don’t want to fall back to grep | awkwardness.
Method 3
mount -l and use grep, sed, or awk to find the line that refers to the directory in question.
Method 4
Unfortunately, there is no universal Unix answer to that.
One thing you can do, for a given file /a/b/c/d is walk up the path:
- /a/b/c/.
- /a/b/c/..
- /a/b/c/../..
- …
… and do a stat(2) at each level, until the st_dev changes. Then you’ll know where the mount point is. Then you can look up the canonical path of that mount point in /etc/mtab or in the output of mount to find out the file system type. Then finding out what is remote and what is not is going to be tricky especially for fuse-type ones. For instance, nfs, cifs, fuse.sshfs, fuse.davfs are obvious, but what about for instance fuse.gvfs-fuse-daemon or fuse.avfsd that can have both network and non-network files?
Method 5
The “-l” to df(1) will fail with an error on non-local filesystems, so you can use this behavior to know if the filesystem is remote:
df -l /path 2> /dev/null | grep -q "File" rc=$? if [ "$rc" = "0" ] then echo "local mount, do stuff" fi
However, the -l option is not standard.
Method 6
df /path will tell you that /path is a mount point if it says that the mount point is not /.
Method 7
Do:
df /me/path/to/folder
If the first field (the Filesystem) is in the format host:/path
then you know it is NFS
So:
df /my/path/to/folder | awk 'NR==1{next};$1~/:[/]/{print "yes";exit(0)};{print "No";exit(1)}'
Now, put it in a bash file named “ifchk-remote-mountpoint”.
Usage:
if ifchk-remote-mountpoint /my/path/to/folder >/dev/null; then
...do something...
fi
or:
ISNFS=$(ifchk-remote-mountpoint /my/path/to/folder)
Method 8
You can use the df -t on Linux and df -T on BSD systems.
From man df:
-t, –type=TYPE
limit listing to file systems of type TYPE
if df -t nfs | grep -q /path/to/folder$; then # it is an NFS fi
Method 9
The following works fine on CentOS 7.
- Find the mount point of the required path (
/mnt/users/bbharatiin my case) with the below command:
df -hk /mnt/users/bbharati
Filesystem 1K-blocks Used Available Use% Mounted on svm-dev-01-2048-lif:/expo_dev_users/users 6764573504 3540678720 3223894784 53% /mnt/svmdev01expodevusers
- From the output of the above command, pick the value for the
Mounted onfield, and use that in the below command:
mount -l | grep /mnt/svmdev01expodevusers
svm-dev-01-2048-lif:/expo_dev_users/users on /mnt/svmdev01expodevusers type nfs (rw,relatime,vers=3,rsize=65536,wsize=65536,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,mountaddr=1.......)
You can see that in the output of the second command, type is nfs.
Method 10
I would personally use mountpoint (very portable on Linux!):
NAME
mountpoint - see if a directory is a mountpoint
SYNOPSIS
mountpoint [-d|-q] directory
or showmount which is pretty much required to be installed on any system that actually mount NFS shares (part of nfs-common package):
NAME
showmount - show mount information for an NFS server
SYNOPSIS
showmount [ -adehv ] [ --all ] [ --directories ] [ --exports ] [ --help ] [ --version ] [ host ]
Another option would be someting like:
$ mount -l -t nfs | grep 'my mount point'
Method 11
find . -type d -name nfs -exec mountpoint {} ; | grep not
will check all folders named ‘nfs’ if they’re mounted
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0