* Home :: Get a random password :: Get a multi-word password :: What is my IP? (v4 | v6)
* My projects

How to find files in use by the NFS server

Tags: howto,nfs,linux

Date: 20220519

Sometimes we need to figure out - on a Linux based NFS server - which files are in actual use by NFS clients.

In my particular case I needed to figure whether certain files (qcow2, vmdk) were still in use by virtual machines, without having access to said VMs or their hypervisors.
Alas, the likes of lsof or fuser are not of use here. Lsof has a "-N" (for NFS) switch, but it's useless on NFS servers, perhaps it's useful on the clients.
However there is a workaround, this being Linux and all. We know that the NFS server is locking the files while they are in use, so we just need to find the files under the exported path that are also locked.

This can be done in 2 very easy steps:
1 - Find the file's inode (as this is what Linux uses internally for locks), this can be done easily with ls, stat or find:

find /export/path/file.qcow2 -printf '%i\n'

2 - Check whether the file is in use (ie locked). If the inode shows up in /proc/locks then it is in use.. No output means it's not currently in use:

grep -w $inodeFromStep1 /proc/locks

Voila! This is the best I could do.
Based on this it should be trivial to write a script to automate similar operations.
Please watch out for corner cases and so on, the above is helpful if the file is in _active_ use. A stopped VM will likely cause this backing file to not be locked on the server, but this doesn't necesarilly mean it's useless.
Apply common sense, know your environment etc.
(c)The Nux(tm) ;-)

No CSS, no Javascript, no some HTML.
No ads, no tracking, no cookies.