在用戶端 VM 上設定 NFS 通訊埠

本頁說明如何在用戶端 VM 上設定 statd 和 nlockmgr 精靈的通訊埠,簡化防火牆設定。

Filestore 會使用 nlockmgr 和 statd Daemon 啟用檔案鎖定功能。您必須透過防火牆規則,在用戶端 VM 上正確公開這些服務的通訊埠,用戶端才能正常使用鎖定。建議您設定 statd 和 nlockmgr 通訊埠,確保所有用戶端 VM 的設定一致,方便設定輸入防火牆規則。

如要進一步瞭解如何判定您是否需要為虛擬私有雲網路設定防火牆規則,請參閱「設定防火牆規則」。

檢查連接埠設定

如要查看 statd 和 nlockmgr 通訊埠目前設定的值,請在用戶端 VM 執行個體上執行下列指令。如果檔案不存在,或選項沒有值,系統就不會設定連接埠。在這種情況下,會為 Daemon 動態指派任意可用通訊埠。

Debian/Ubuntu

  1. 如要判斷 statd 通訊埠,請執行下列指令並查看 STATDOPTS 值:

    cat /etc/default/nfs-common
    
  2. 如要判斷 nlockmgr 連接埠,請執行下列指令並查看 nlm_tcpport 和 nlm_udpport 值:

    cat /etc/modprobe.d/lock.conf
    

RHEL/CentOS

  1. 如要判斷 statd 通訊埠,請執行下列指令並查看 STATD_PORT 值:

    cat /etc/sysconfig/nfs
    
  2. 如要判斷 nlockmgr 連接埠,請執行下列指令並查看 nlm_tcpport 和 nlm_udpport 值:

    cat /etc/modprobe.d/lock.conf
    

SUSE

執行下列指令:

cat /etc/sysconfig/nfs

statd 通訊埠會列在 STATD_PORT 下方,nlockmgr 通訊埠則會列在 LOCKD_TCPPORT 和 LOCKD_UDPPORT 下方。

Windows

在 Windows 上不需要設定 NFS 通訊埠。

設定通訊埠

如要設定 statd 和 nlockmgr 連接埠,請在用戶端 VM 執行個體上執行下列指令。這些範例使用 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_tcpport 和 nlm_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_tcpport 和 nlm_udpport 選項:

      options lockd nlm_tcpport=4045
      options lockd nlm_udpport=4045
      
    3. 儲存檔案並結束。

SUSE

設定 statd 和 nlockmgr 連接埠:

  1. 開啟 /etc/sysconfig/nfs 檔案進行編輯:

    sudo nano /etc/sysconfig/nfs
    
  2. 設定 STATD_PORT、LOCKD_TCPPORT 和 LOCKD_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:指令碼檔案所在的路徑。這項指令應以根層級執行,否則請在指令開頭加入 sudo。
  4. 輸入下列指令:

    SCRIPT_PATH INSTANCE_IP SHARE_NAME
    

    更改下列內容:

    • SCRIPT_PATH:指令碼檔案所在的路徑。這項指令應以根層級執行,否則請在指令開頭加入 sudo。
    • INSTANCE_IP:Filestore 執行個體的 IP 位址
    • SHARE_NAME:檔案共用名稱

    如果通訊埠已開啟,指令碼會傳回下列回應:

    The NLM GRANT message is working properly!
    

    如果通訊埠未開啟,指令碼會傳回下列錯誤:

    The NLM GRANT message is not working properly!
    

後續步驟