重新启动后“陈旧的 NFS 文件句柄”

mah*_*ood 18 nfs mount linux-networking

在服务器节点上,可以访问导出的文件夹。但是,在重新启动(服务器和客户端)后,客户端将无法再访问该文件夹。

在服务器上

# ls /data
Folder1
Forlder2
Run Code Online (Sandbox Code Playgroud)

并且 /etc/exports 文件包含

/data 192.168.1.0/24(rw,no_subtree_check,async,no_root_squash)
Run Code Online (Sandbox Code Playgroud)

在客户端

# ls /data
ls: cannot access /data: Stale NFS file handle
Run Code Online (Sandbox Code Playgroud)

我不得不说客户端的共享文件夹没有问题,但是在重新启动(服务器和客户端)后,我看到了这条消息。

有什么办法可以解决吗?

Bil*_*hor 26

重新启动的顺序很重要。在客户端之后重新启动服务器可能会导致这种情况。陈旧的 NFS 句柄表示客户端打开了一个文件,但服务器不再识别该文件句柄。在某些情况下,NFS 会在超时后清理其数据结构。在其他情况下,您需要自己清理 NFS 数据结构,然后重新启动 NFS。这些结构所在的位置在某种程度上取决于 O/S。

尝试首先在服务器上重新启动 NFS,然后在客户端上。这可能会清除文件句柄。

不建议使用从其他服务器打开的文件重新启动 NFS 服务器。如果已在服务器上删除打开的文件,这尤其成问题。服务器可能会保持文件打开直到重新启动,但重新启动将删除服务器端的内存文件句柄。然后客户端将无法再打开该文件。

从服务器确定使用了哪些挂载既困难又不可靠。该showmount -a选项可能会显示一些活动的挂载,但可能不会报告所有这些挂载。锁定的文件更容易识别,但需要启用锁定并依赖客户端软件来锁定文件。

您可以lsof在客户端上使用来识别在装载上打开文件的进程。

我在 NFS 安装上使用hardintr安装选项。该hard选项会导致无限期重试 IO。该intr选项允许进程在等待 NFS IO 完成时被杀死。


小智 5

试试我写的这个脚本:

#!/bin/bash
# Purpose:
# Detect Stale File handle and remove it
# Script created: July 29, 2015 by Birgit Ducarroz
# Last modification: --
#

# Detect Stale file handle and write output into a variable and then into a file
mounts=`df 2>&1 | grep 'Stale file handle' |awk '{print ""$2"" }' > NFS_stales.txt`
# Remove : ‘ and ’ characters from the output
sed -r -i 's/://' NFS_stales.txt && sed -r -i 's/‘//' NFS_stales.txt && sed -r -i 's/’//' NFS_stales.txt

# Not used: replace space by a new line
# stales=`cat NFS_stales.txt && sed -r -i ':a;N;$!ba;s/ /\n /g' NFS_stales.txt`

# read NFS_stales.txt output file line by line then unmount stale by stale.
#    IFS='' (or IFS=) prevents leading/trailing whitespace from being trimmed.
#    -r prevents backslash escapes from being interpreted.
#    || [[ -n $line ]] prevents the last line from being ignored if it doesn't end with a \n (since read returns a non-zero exit code when it encounters EOF).

while IFS='' read -r line || [[ -n "$line" ]]; do
    echo "Unmounting due to NFS Stale file handle: $line"
    umount -fl $line
done < "NFS_stales.txt"
#EOF
Run Code Online (Sandbox Code Playgroud)

同时,上述脚本不适用于所有服务器。这是一个更新:

#!/bin/bash
# Purpose:
# Detect Stale File handle and remove it
# Script created: July 29, 2015 by Birgit Ducarroz
# Last modification: 23.12.2020  /bdu
#

MYMAIL="my.mail@something.com"
THIS_HOST=`hostname`

# Detect Stale file handle and write output into a variable and then into a file
mounts=`df 2>&1 | grep 'Stale' |awk '{print ""$2"" }' > NFS_stales.txt`
sleep 8

# Remove : special characters from the output

sed -r -i 's/://' NFS_stales.txt && sed -r -i 's/‘//' NFS_stales.txt && sed -r -i 's/’//' NFS_stales.txt && sed -r -i 's/`//' NFS_stales.txt  && sed -r -i "s/'//" NFS_stales.txt 


# Not used: replace space by a new line
# stales=`cat NFS_stales.txt && sed -r -i ':a;N;$!ba;s/ /\n /g' NFS_stales.txt`

# read NFS_stales.txt output file line by line then unmount stale by stale.
#    IFS='' (or IFS=) prevents leading/trailing whitespace from being trimmed.
#    -r prevents backslash escapes from being interpreted.
#    || [[ -n $line ]] prevents the last line from being ignored if it doesn't end with a \n (since read returns a non-zero exit code when it encounters EOF).

while IFS='' read -r line || [[ -n "$line" ]]; do
    message=`echo "Unmounting due to NFS Stale file handle: $line"`
    echo echo | mail -s "$THIS_HOST: NFS Stale Handle unmounted" $MYMAIL <<< $message
    umount -f -l $line
done < "NFS_stales.txt"
mount -a

#EOF
Run Code Online (Sandbox Code Playgroud)