nfslock
- test getting an NFS lock on a filehandle
nfslock
[-hlTv
] [-H
hertz]
nfslock
sends an NLM version 4 NLM_TEST RPC request to an NFS server for each filehandle passed to it on stdin
. NLM version 4 is used by NFS version 3 clients. The program outputs whether the request was "granted" or "failed" to stderr
and prints the response time in Graphite format to stdout
.
nfslock
does not actually take out file locks on the server, it only tests whether a lock call for a specific filehandle would succeed.
Input filehandles are specified as JSON objects (one per line) with the keys "host", "ip", "path", and "filehandle", where the value of the "filehandle" key is the hex representation of the target file's NFS filehandle.
If the NLM server requires "secure" ports (<1024), nfslock
will have to be run as root. (On Linux this is controlled by the insecure_locks
option in exports(5)).
-h
Display a help message and exit.
-H
hertzThe polling frequency in Hertz. This is the number of requests sent to each target per second. Default = 1.
-l
Loop forever. Exit with Ctrl-c.
-T
Use TCP to connect to server. Default = UDP.
-v
Display debug output on stderr
.
nfsls
uses filehandles returned by nfsmount
and nfsls
, possibly along with other filters like grep
or jq
:
sudo sh -c "nfsmount dumpy | nfsls | grep testfile | nfslock"
Since these commands typically need root privileges (to bind to a "secure" port) they can be run with a single subshell under sudo
.
If the check is being run repeatedly, there is no need to query the fileserver to look up filehandles every time. Since the filehandles shouldn't change, the output can be stored in a file:
`sudo sh -c "nfsmount dumpy | nfsls | grep testfile" > testfile.nfs`
`sudo nfslock < testfile.nfs`
nfslock
will return 0
if all requests to all targets received successful responses. Nonzero exit codes indicate a failure and correspond to the NLM error in nlm4_stats. 10
is a name resolution or RPC failure, 3
is an initialisation failure (typically bad arguments).
http://pubs.opengroup.org/onlinepubs/9629799/NLM_TEST.htm
http://pubs.opengroup.org/onlinepubs/9629799/chap14.htm
Matt Provost, mprovost@termcap.net
Copyright 2017 Matt Provost
RPC files Copyright Sun Microsystems
NFSv4 files Copyright IETF
nfsmount(8), nfsls(8)