在客户端虚拟机上配置 NFS 端口

本页面介绍了如何在客户端虚拟机上设置 statdnlockmgr 守护程序的端口,以便更轻松地配置防火墙。

Filestore 使用 nlockmgrstatd 守护程序来启用文件锁定。这些服务的端口需要通过防火墙规则正确地在客户端虚拟机上公开,以便客户端可以正确使用锁定。我们建议设置 statdnlockmgr 端口,使其在所有客户端虚拟机上保持一致,从而更轻松地配置入站防火墙规则。

如需详细了解如何确定您是否需要为 VPC 网络配置防火墙规则,请参阅配置防火墙规则

检查端口设置

如需查看 statdnlockmgr 端口当前设置的值,请在客户端虚拟机实例上运行以下命令。如果文件不存在,或者选项没有值,则端口未设置。在这种情况下,系统会向守护程序动态分配任意可用端口。

Debian/Ubuntu

  1. 要确定 statd 端口,请运行以下命令并查看 STATDOPTS 值:

    cat /etc/default/nfs-common
    
  2. 要确定 nlockmgr 端口,请运行以下命令并查看 nlm_tcpportnlm_udpport 值:

    cat /etc/modprobe.d/lock.conf
    

RHEL/CentOS

  1. 要确定 statd 端口,请运行以下命令并查看 STATD_PORT 值:

    cat /etc/sysconfig/nfs
    
  2. 要确定 nlockmgr 端口,请运行以下命令并查看 nlm_tcpportnlm_udpport 值:

    cat /etc/modprobe.d/lock.conf
    

SUSE

运行以下命令:

cat /etc/sysconfig/nfs

statd 端口列在 STATD_PORT 下,nlockmgr 端口列在 LOCKD_TCPPORTLOCKD_UDPPORT 下。

Windows

Windows 上不需要设置 NFS 端口。

设置端口

如需设置 statdnlockmgr 端口,请在客户端虚拟机实例上运行以下命令。这些示例使用的是 nano 文本编辑器,但您可以使用任何文本编辑器。这些示例还使用 2046 作为 statd 的端口,使用 4045 作为 nlockmgr 的端口,因为这些值是常用选项。您可以根据网络配置使用不同的端口。在这种情况下,入站防火墙规则必须允许流量进入您使用的特定端口。

Debian/Ubuntu

  • 设置 statd 端口:

    1. 打开 /etc/default/nfs-common 文件进行修改:

      sudo nano /etc/default/nfs-common
      
    2. 设置 STATDOPTS 选项:

      STATDOPTS="-p 2046"
      
    3. 保存文件并退出。

  • 设置 nlockmgr 端口:

    1. 创建 /etc/modprobe.d/lock.conf 文件:

      sudo nano /etc/modprobe.d/lock.conf
      
    2. 设置 nlm_tcpportnlm_udpport 选项:

      options lockd nlm_tcpport=4045
      options lockd nlm_udpport=4045
      
    3. 保存文件并退出。

RHEL/CentOS

  • 设置 statd 端口:

    1. 打开 /etc/sysconfig/nfs 文件进行修改:

      sudo nano /etc/sysconfig/nfs
      
    2. 设置 STATD_PORT 选项:

      STATD_PORT=2046
      
    3. 保存文件并退出。

  • 设置 nlockmgr 端口:

    1. 创建 /etc/modprobe.d/lock.conf 文件:

      sudo nano /etc/modprobe.d/lock.conf
      
    2. 设置 nlm_tcpportnlm_udpport 选项:

      options lockd nlm_tcpport=4045
      options lockd nlm_udpport=4045
      
    3. 保存文件并退出。

SUSE

设置 statdnlockmgr 端口:

  1. 打开 /etc/sysconfig/nfs 文件进行修改:

    sudo nano /etc/sysconfig/nfs
    
  2. 设置 STATD_PORTLOCKD_TCPPORTLOCKD_UDPPORT 选项:

    STATD_PORT=2046
    LOCKD_TCPPORT=4045
    LOCKD_UDPPORT=4045
    
  3. 保存文件并退出。

Windows

Windows 上不需要设置 NFS 端口。

验证端口是否已打开

如需验证 NFS 端口是否已正确打开,请完成以下步骤。

  1. 安装以下依赖项。

    Debian/Ubuntu

    从命令行中,输入以下命令:

    sudo apt install nfs-common tcpdump tshark
    

    RHEL/CentOS

    从命令行中,输入以下命令:

    sudo yum install nfs-utils tcpdump wireshark
    

    SUSE

    从命令行中,输入以下命令:

    sudo zypper install nfs-client tcpdump wireshark
    

    Windows

    Windows 不支持此验证流程。

  2. 创建一个名为 verify-nfs-port-script.sh 的脚本文件,将以下脚本复制并粘贴到该文件中,然后将其本地保存到您的机器。请记下该文件的位置并保存,以便用于下一步。

    #!/bin/bash
    
    # This script is intended to run on client machines to verify that the ports
    # are properly open to allow the reception of NLM GRANT messages from the server.
    
    set -eu
    
    function kill_descendants() {
       for pid in $(ps -o pid= --ppid "$1")
       do
       kill_descendants "$pid"
       done
       if [[ $1 -ne $$ ]]; then
       kill "$1" 2>/dev/null | true
       fi
    }
    
    function cleanup {
       set +eu
    
       # Kill all background jobs and wait for it to end, makes sure locks are released
       kill_descendants $$
    
       # Wait for jobs to die and locks to be released, so mount is not busy
       sleep 2
    
       umount -f "$MNT1"
       umount -f "$MNT2"
    
       rmdir "$MNT1" 2&> /dev/null || true
       rmdir "$MNT2" 2&> /dev/null || true
    }
    
    function print_help {
       echo "$0 [server_ip] [mount_path]"
       echo -e "\t For example, if you mount a server using:"
       echo -e "\t\t \"mount 10.0.0.1:share /mnt/mount_point\""
       echo -e "\t Run the script: "
       echo -e "\t\t \"$0 10.0.0.1 share\""
    }
    
    if [ $# -ne 2 ]; then
       print_help
       exit 1
    fi
    
    if [ $(id -u) -ne 0 ]; then
       echo "Failure! This script needs to run as root, use \"sudo $@\""
       exit 1
    fi
    
    if ! [ -x "$(command -v tshark)" ]; then
       echo "The 'tshark' command does not exist and is needed for the script. Please install it"
       exit 1
    fi
    
    if ! [ -x "$(command -v tcpdump)" ]; then
       echo "The 'tcpdump' command does not exist and is needed for the script. Please install it"
       exit 1
    fi
    
    SERVER_IP=$1
    MOUNT_PATH=$2
    
    MNT1=$(mktemp -d)
    MNT2=$(mktemp -d)
    
    trap cleanup EXIT
    
    echo "Mounting..."
    mount -o nosharecache "$SERVER_IP":"$MOUNT_PATH" "$MNT1"
    mount -o nosharecache "$SERVER_IP":"$MOUNT_PATH" "$MNT2"
    
    REC_FILE=$(mktemp /tmp/nlm_recording_XXXXXXXX.pcap)
    tcpdump -i any -s0 -w "$REC_FILE" "host $SERVER_IP" &
    TCPDUMP_PID=$!
    echo "Recording TCP dump to $REC_FILE"
    
    sleep 5 # wait for tcpdump to start running
    
    echo "Running test..."
    flock "$MNT1"/lock_file -c "echo -n \"Got first lock: \" && date && sleep 5 && echo -n \"Releasing first lock: \" && date" &
    sleep 2 # Wait for the first lock to actually be taken
    
    echo "Waiting for second lock: $(date)"
    flock "$MNT2"/lock_file -c "echo -n \"Got second lock: \" && date"
    
    sleep 2 # Wait for tcpdump to record everything
    kill $TCPDUMP_PID
    
    # For quick analysis inspect recording with tshark, if you don't have it just inspect with Wireshark
    echo "Inspecting results in $REC_FILE with TShark"
    tshark -r "$REC_FILE" -Y nlm # First, print the output
    
    tshark -r "$REC_FILE" -Y nlm 2>/dev/null | grep -q GRANTED
    EXIT_CODE=0
    if [ $? -eq 0 ]; then
       echo "The NLM GRANT message is working properly!"
       EXIT_CODE=0
    else
    echo "The NLM GRANT message is not working properly!"
    EXIT_CODE=1
    fi
    echo "For debugging, please provide the printed output of the script, and $REC_FILE"
    exit ${EXIT_CODE}
    
  3. 输入以下命令:

    chmod +x SCRIPT_PATH
    

    替换以下内容:

    • SCRIPT_PATH:您的脚本文件的路径。该命令应以 root 用户身份运行,否则请在命令开头添加 sudo
  4. 输入以下命令:

    SCRIPT_PATH INSTANCE_IP SHARE_NAME
    

    替换以下内容:

    • SCRIPT_PATH:您的脚本文件的路径。该命令应以 root 用户身份运行,否则请在命令开头添加 sudo
    • INSTANCE_IP:Filestore 实例的 IP 地址
    • SHARE_NAME:文件共享的名称

    如果端口已打开,脚本将返回以下响应:

    The NLM GRANT message is working properly!
    

    如果端口未开放,脚本会返回以下错误:

    The NLM GRANT message is not working properly!
    

后续步骤