0

System A (Ubuntu server 18.04 LTS) exports a directory for NFS mounting by systems B, C, or D. On A (the server) is there a way to tell whether any of the other systems currently has the directory mounted?

The objective is to avoid shutting down A if any of B, C, or D have mounted the exported directory.

An automated (shell scripted) means of doing so would be ideal, but manual is OK.

0

There is no direct NFS utils (provided by nfs-utils) to list the clients connected to NFS server (mounts exported directories).

However, if NFSv4 is used, on the NFS server side clients can easily be identified using ss or netstat because it uses only 1 port 2049 for both UDP and TCP:

Examples

netstat -naptule | grep :2049

root@n54l:~# exportfs -rav
exporting 192.168.1.0/24:/srv/oops

root@n54l:~# netstat -naptule | grep :2049
tcp        0      0 0.0.0.0:2049            0.0.0.0:*               LISTEN      0          32620      -                 
tcp        0      0 192.168.1.123:2049      192.168.1.150:730       ESTABLISHED 0          94689      -                 
tcp6       0      0 :::2049                 :::*                    LISTEN      0          32631      -                 

ss -tuna | grep :2049

root@n54l:~# ss -tuna | grep :2049
tcp   LISTEN     0      64                0.0.0.0:2049            0.0.0.0:*
tcp   ESTAB      0      0           192.168.1.123:2049      192.168.1.150:730
tcp   LISTEN     0      64                   [::]:2049               [::]:*

We can see the NFS server 192.168.1.123, there was 1 client 192.168.1.150 connected to it.

Combination of either command with text processing (grep, cut, awk, sed, etc.) can assemble a shell script to achieve what you want at ease.

NOTE: nfsstat and nfsiostat (client side) may provide some insight but the results/stats are not direct.

| improve this answer | |
0

From the No Good Deed Goes Unpunished Department, here is a simple script which accomplishes my mission. Thanks again to Terry Wang. The script which calls this enters a perpetual "do" loop which attempts shutown and sleeps for a few minutes.

    #!/bin/bash

    # Check for open Samba share.
    # All shares are named "share" something,
    # so grep for "share" is usable

    smbstatus | grep -i share > /dev/nul
    samba=$?

    # Check for open NFS mount.
    # Grep for port 2049, then
    # grep that for "ESTAB"

    netstat -naptule | grep :2049 | grep ESTAB > /dev/nul
    nfs=$?

    # If either came back zero, something is active.

    if [[ $samba != 0 && $nfs != 0 ]]; then
       shutdown -h now
    fi
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.