How can I determine if an NFS remote is exporting a directory?

70,125

Solution 1

For 3) you probably want to use showmount -e remote_nfs_server which shows if remote_nfs_server has exported anything.

And for 2) if you don't want to use a shubshell and know if the remote server runs NFSv3 or NFSv4 and if TCP or UDP, you could query for that specifically with rpcinfo:
rpcinfo -u remote_nfs_server nfs 3 for NFSv3 via UDP and
rpcinfo -t remote_nfs_server nfs 4 for NFSv4 via TCP

For 4) you may want to look at Check if folder is a mounted remote filesystem


Further information:

Solution 2

You can use showmount -e <server> for some aspects of #3. When the exports are simple, it would at least suggest that a directory (or perhaps a parent of such a directory) is exported.

# showmount -e server
/export                               (everyone)

For #2, I'm not sure what you're looking for. If you're using NFSv3, you'll be looking for at least mountd and nfs (and in some cases may want to confirm lock managers). You could check for any explicitly with rpcinfo as you say.

# rpcinfo -t server mountd 3
program 100005 version 3 ready and waiting
Share:
70,125

Related videos on Youtube

Sean Allred
Author by

Sean Allred

Updated on September 18, 2022

Comments

  • Sean Allred
    Sean Allred almost 2 years

    In my script, I have several layers of statusing:

    1. remote is available (ping)
    2. remote NFS service is active
    3. remote NFS is exporting a certain directory
    4. remote NFS is mounted (mount)

    For (2) and (3), I believe rcpinfo is the best bet. For (2) though, I cannot figure out how to narrow my query to the NFS service without starting a subshell (which is not acceptable for this application). For (3), I'm not sure this information is even available remotely (without sshing in, of course).

    I'm working on RHEL 6 and have no access to programs that are not included in the standard distribution.

  • Sean Allred
    Sean Allred over 9 years
    showmount -r remote will tell me if remote is exporting anything, but is there any way for me to determine if remote:/mnt/dir is being exported without starting a subshell (i.e. a pipeline)?
  • Sean Allred
    Sean Allred over 9 years
    showmount -r remote will tell me if remote is exporting anything, but is there any way for me to determine if remote:/mnt/dir is being exported without starting a subshell (i.e. a pipeline)?
  • BowlOfRed
    BowlOfRed over 9 years
    That depends on the facilities at your disposal. What is your script written in? Why is starting a separate executable (showmount) less of a problem than starting another shell?
  • BowlOfRed
    BowlOfRed over 9 years
    Write a separate script that does what you need (like calls showmount, but takes an argument for directory and returns success/failure, and includes a timeout that is reasonable for your purposes), then call that script from your python.
  • Sean Allred
    Sean Allred over 9 years
    I can promise you it's not as easy as you'd immediately think – when it's locked up so, rpcinfo does not respond to anything but SIGKILL (which is a misattribution – the OS obviously just kills the process). At any rate, that would still start a shell to start rpcinfo – the underlying problem remains.
  • BowlOfRed
    BowlOfRed over 9 years
    I think this is really a separate question from the NFS issue, it's about aspects of what you want to achieve from a python script. Perhaps you should ask that separately. But what is the purpose of getting this info? Is it just for troubleshooting? You could simply attempt the mount and handle failure. Is it a big advantage to detect that it's not exported before issuing the mount? For a down host, that could take some time. But if it's just unexported, I'd expect immediate failure.
  • Sean Allred
    Sean Allred over 9 years
    It truly is a separate issue – one that I've asked separately before (somewhere) to no great success or revelation. This script is part of a vast and (over-)complicated statusing network; I could create the mount and handle failure, but it would end up being an expensive step (since it would need to mount, unmount, and usually mount again). It's why I have so many levels of statusing – to ensure as little work as possible is done twice.
  • doktor5000
    doktor5000 over 9 years
    showmount -e remote will tell you if remote has exported anything. showmount -r will only give you an error message as that option doesn't exist. You could try to use shell substring matching to basically grep for a regular expression in the showmount output. Maybe have a look at e.g. stackoverflow.com/questions/12619720/…
  • Mark Plotnick
    Mark Plotnick over 9 years
    @SeanAllred Luckily, you don't need to create a subshell or a pipeline to look at the output of a process. Use output=subprocess.check_output(["showmount","-e",server]) and look through the output string for a line that starts with the directory or parent of a directory you want to know about.
  • doktor5000
    doktor5000 over 9 years
    Or put the other way around, I don't know of any standalone command which can specifically query a remote NFS server for one particular mount, and I don't think that exists. Only other two viable ways would be to query the remote server locally via ssh (although even then there's no reliable way to determine if a given directory is currently exported due to the stateless nature of NFS). Other approach could be to try to mount the NFS export from the client and check for the exit code and then the local mount and its contents.
  • Sean Allred
    Sean Allred over 9 years
    My mistake with the -r option versus -e – that's what I meant :) The 'attempt approach' has been suggested before. I don't particularly like it, but it's looking like a real option.
  • Sean Allred
    Sean Allred over 9 years
    I'm very familiar with the XY problem; we get them all the time over at TeX.SX. It may not look like it from my rep on this particular site, but I've been on SE for quite a while :). I do not have control over the NFS servers -- this is an extremely large and complicated system with multiple contracts for it -- I don't like it, but there is defensive code everywhere. And actually -- yes that kind of granulated status is part of the design. More accurately, it can (and should) return exit codes based on the mount status.
  • Ale
    Ale over 4 years
    For a /net mount to work, the /etc/auto.net script must establish that the server has a corresponding export. This uses the showmount command. If the server only has NFSv4, it wont work (showmount displays RPC: Program not registered, and /net/server will just report File not found)