當您需要跨作業系統共用資料時,可以通過Windows系統掛載NAS NFS檔案系統實現資料上傳與下載。本文為您介紹如何在Windows作業系統中掛載NFS檔案系統。
前提條件
在建立NAS檔案系統的節點,已有可用的ENS執行個體(Windows系統)。
已建立NAS NFS檔案系統,並擷取到檔案系統的掛載點地址,且與ENS執行個體屬於同一VPC。
操作步驟
遠端連線ENS計算執行個體。
安裝NFS用戶端。
開啟伺服器管理員。
選擇管理 > 添加角色和功能。
根據添加角色和功能嚮導提示安裝NFS用戶端。
在伺服器角色選項卡下,選擇檔案和儲存服務 > 檔案和iSCSI服務下的NFS伺服器。
在功能頁簽,選擇NFS用戶端。
重啟ENS執行個體。
啟動命令提示字元,執行mount命令。如果返回以下資訊,說明NFS用戶端安裝成功。
修改註冊表,目的是添加匿名使用者的預設UID和GID,防止mount nas後可能會出現沒有許可權開啟檔案的問題。
修改路徑:HKEY_LOCAL_MACHINE > SOFTWARE > Microsoft > ClientForNFS > CurrentVersion > Users > Default > Mount,建立Locking、AnonymousGID、AnonymousUID這三個登錄機碼,注意其中 Locking值必須為1 。
修改路徑:HKEY_LOCAL_MACHINE > SOFTWARE > Microsoft > ClientForNFS > CurrentVersion > Default,建立以下登錄機碼設定GID和UID。
重啟系統。
必須重啟系統,否則只有讀許可權。
添加路由。
訪問NAS掛載點前,需要在Windows執行個體上先執行添加路由的命令
route add 100.64.128.0/18 執行個體內網IP
。掛載NFS系統。
在Windows用戶端,執行以下命令掛載NFS檔案系統mount 掛載點地址 <掛載盤符>。
Windows用戶端僅支援NFS v3協議
您可在目標檔案系統的詳情頁擷取到NFS v3協議掛載點地址,如詳情頁展示的NFS v3掛載點地址為
100.64.XXX.XXX:/source_path
,則在Windows用戶端掛載時,掛載點地址為\\100.64.128.X\source_path
。驗證掛載結果。
掛載成功後,您可以雙擊這台電腦表徵圖,在介面查看新的共用檔案系統。
指令碼掛載
指令碼能力
NFS用戶端安裝。
註冊表修改。
NFS讀寫掛載。
開機自動掛載。
指令碼未包含重啟,對於新機器安裝了NFS用戶端以及修改註冊表需要手動進行一次重啟,否則對於掛載的目錄只有讀許可權。
使用方式
指令碼放置指定目錄下,例如C盤,命名為:nfs_mount.ps1。
powershell執行命令。
執行方式一:
powershell -file C:\nfs_mount.ps1
執行方式二:
powershell -file C:\nfs_mount.ps1 -url 掛載點 -drive z # 備忘:該方式會覆蓋指令碼中預設的url(掛載點)和drive(盤符)
NFS讀寫掛載
[CmdletBinding()]
Param(
#掛載點
[string]$url="100.125.255.100\nas_test",
#掛載Windows盤符
[string]$drive="z"
)
#擷取當前執行時間
$now = Get-Date
#指定log檔案的場所 當前執行目錄
$logPath="C:\"
#設定log檔案名稱
$logName = "executLog"
$logFileName= $logName + "_" + $now.ToString("yyyy-MM-dd")+".log"
$logInfo = $now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " "
#log 全路徑設定
$logPathFile = Join-Path $logPath $logFileName
Write-Output ($logInfo+"掛載路徑:"+$url+" 掛載盤符:"+$drive)
Write-Output ($logInfo+"掛載路徑:"+$url+" 掛載盤符:"+$drive) | Out-File -FilePath $logPathFile -Encoding default -Append
$nfs=get-WindowsFeature -Name "NFS-Client"
if($nfs.installed){
$nas_start = (Get-Item HKCU:\Software\Microsoft\Windows\CurrentVersion\Run).GetValueNames().Contains("nas_run")
Write-Output ($logInfo+":開機自啟動>>"+$nas_start)
Write-Output ($logInfo+":開機自啟動>>"+$nas_start) | Out-File -FilePath $logPathFile -Encoding default -Append
if(!$nas_start){
Write-Output ($logInfo+":設定開機自啟動")
Write-Output ($logInfo+":設定開機自啟動") | Out-File -FilePath $logPathFile -Encoding default -Append
$nas_script_path = $myinvocation.mycommand.definition
$run_script = "powershell -file "+ $nas_script_path +" -url "+ $url +" -drive "+ $drive
$nas_run = "nas_run"
$nas_run_result = New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run" -Name $nas_run -PropertyType String -Value $run_script -ErrorAction Continue -ErrorVariable run_err_msg
if($err_msg){
Write-Output ($logInfo +"設定開機自啟動返回結果失敗:" +$run_err_msg)
Write-Output ($logInfo +"設定開機自啟動返回結果失敗:" +$run_err_msg) | Out-File -FilePath $logPathFile -Encoding default -Append
}else{
Write-Output ($logInfo +"設定開機自啟動完成:" +$nas_run_result)
Write-Output ($logInfo +"設定開機自啟動完成:" +$nas_run_result) | Out-File -FilePath $logPathFile -Encoding default -Append
}
}
Write-Output ($logInfo+"已存在nfs-client,掛載nfs掛載點")
Write-Output ($logInfo+"已存在nfs-client,掛載nfs掛載點") | Out-File -FilePath $logPathFile -Encoding default -Append
$nfs_url = "mount -o nolock -o mtype=hard -o timeout=60 \\$url ${drive}:"
Write-Output ($logInfo + ":開始掛載nas "+$nfs_url)
Write-Output ($logInfo + ":開始掛載nas "+$nfs_url) | Out-File -FilePath $logPathFile -Encoding default -Append
$reslut = cmd /c $nfs_url
Write-Output ($logInfo+$reslut)
Write-Output ($logInfo+$reslut) | Out-File -FilePath $logPathFile -Encoding default -Append
}else{
Write-Output ($logInfo +":沒有安裝nfs-client,進行安裝&註冊表配置 運行完成請重啟")
Write-Output ($logInfo +":沒有安裝nfs-client,進行安裝&註冊表配置 運行完成請重啟") | Out-File -FilePath $logPathFile -Encoding default -Append
Add-WindowsFeature -Name "NFS-Client"
$lock_path = Test-Path HKLM:\SOFTWARE\Microsoft\ClientForNFS\CurrentVersion\Users\Default\Mount
if($lock_path){
$lock = (Get-Item HKLM:\SOFTWARE\Microsoft\ClientForNFS\CurrentVersion\Users\Default\Mount).GetValueNames().Contains("Locking")
if(!$lock){
$lock_result = New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\ClientForNFS\CurrentVersion\Users\Default\Mount" -Name "Locking" -PropertyType DWord -Value 1 -ErrorAction Continue -ErrorVariable err_msg
if($err_msg){
Write-Output ($logInfo +"註冊表配置完成返回結果失敗:" +$err_msg)
Write-Output ($logInfo +"註冊表配置完成返回結果失敗:" +$err_msg) | Out-File -FilePath $logPathFile -Encoding default -Append
}else{
Write-Output ($logInfo +"註冊表配置完成返回結果為:" +$lock_result)
Write-Output ($logInfo +"註冊表配置完成返回結果為:" +$lock_result) | Out-File -FilePath $logPathFile -Encoding default -Append
}
}
}else{
Write-Output "不存在註冊表HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ClientForNFS\CurrentVersion\Users\Default\Mount項請自行產生"
Write-Output "不存在註冊表HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ClientForNFS\CurrentVersion\Users\Default\Mount項請自行產生" | Out-File -FilePath $logPathFile -Encoding default -Append
}
$id_path = Test-Path HKLM:\SOFTWARE\Microsoft\ClientForNFS\CurrentVersion\Default
if ($id_path){
$gid = (Get-Item HKLM:\SOFTWARE\Microsoft\ClientForNFS\CurrentVersion\Default).GetValueNames().Contains("AnonymousGID")
$uid = (Get-Item HKLM:\SOFTWARE\Microsoft\ClientForNFS\CurrentVersion\Default).GetValueNames().Contains("AnonymousUID")
if (!$gid){
$gid_result = New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\ClientForNFS\CurrentVersion\Default" -Name "AnonymousGID" -PropertyType DWord -Value 0 -ErrorAction Continue -ErrorVariable gid_err_msg
if($gid_err_msg){
Write-Output ($logInfo +"註冊表配置完成返回結果失敗:" +$gid_err_msg)
Write-Output ($logInfo +"註冊表配置完成返回結果失敗:" +$gid_err_msg) | Out-File -FilePath $logPathFile -Encoding default -Append
}else{
Write-Output ($logInfo +"註冊表配置完成返回結果為:" +$gid_result)
Write-Output ($logInfo +"註冊表配置完成返回結果為:" +$gid_result) | Out-File -FilePath $logPathFile -Encoding default -Append
}
}
if (!$uid){
$uid_result = New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\ClientForNFS\CurrentVersion\Default" -Name "AnonymousUID" -PropertyType DWord -Value 0 -ErrorAction Continue -ErrorVariable uid_err_msg
if($uid_err_msg){
Write-Output ($logInfo +"註冊表配置完成返回結果失敗:" +$uid_err_msg)
Write-Output ($logInfo +"註冊表配置完成返回結果失敗:" +$uid_err_msg) | Out-File -FilePath $logPathFile -Encoding default -Append
}else{
Write-Output ($logInfo +"註冊表配置完成返回結果為:" +$uid_result)
Write-Output ($logInfo +"註冊表配置完成返回結果為:" +$uid_result) | Out-File -FilePath $logPathFile -Encoding default -Append
}
}
}else{
Write-Output ($logInfo +"不存在註冊表HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ClientForNFS\CurrentVersion\Default項請自行產生")
Write-Output ($logInfo +"不存在註冊表HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ClientForNFS\CurrentVersion\Default項請自行產生") | Out-File -FilePath $logPathFile -Encoding default -Append
}
$nas_start = (Get-Item HKCU:\Software\Microsoft\Windows\CurrentVersion\Run).GetValueNames().Contains("nas_run")
Write-Output ($logInfo+":開機自啟動>>"+$nas_start)
Write-Output ($logInfo+":開機自啟動>>"+$nas_start) | Out-File -FilePath $logPathFile -Encoding default -Append
if(!$nas_start){
Write-Output ($logInfo+":設定開機自啟動")
Write-Output ($logInfo+":設定開機自啟動") | Out-File -FilePath $logPathFile -Encoding default -Append
$nas_script_path = $myinvocation.mycommand.definition
$run_script = "powershell -file "+ $nas_script_path +" -url "+ $url +" -drive "+ $drive
$nas_run = "nas_run"
$nas_run_result = New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run" -Name $nas_run -PropertyType String -Value $run_script -ErrorAction Continue -ErrorVariable run_err_msg
if($err_msg){
Write-Output ($logInfo +"設定開機自啟動返回結果失敗:" +$run_err_msg)
Write-Output ($logInfo +"設定開機自啟動返回結果失敗:" +$run_err_msg) | Out-File -FilePath $logPathFile -Encoding default -Append
}else{
Write-Output ($logInfo +"設定開機自啟動完成:" +$nas_run_result)
Write-Output ($logInfo +"設定開機自啟動完成:" +$nas_run_result) | Out-File -FilePath $logPathFile -Encoding default -Append
}
}
$nfs_url = "mount -o nolock -o mtype=hard -o timeout=60 \\$url ${drive}:"
Write-Output ($logInfo + ":開始掛載nas "+$nfs_url)
Write-Output ($logInfo + ":開始掛載nas "+$nfs_url) | Out-File -FilePath $logPathFile -Encoding default -Append
$result = cmd /c $nfs_url
Write-Output ($logInfo +":nas掛載結束" +$result)
Write-Output ($logInfo +":nas掛載結束" +$result) | Out-File -FilePath $logPathFile -Encoding default -Append
Write-Output ($logInfo +":設定結束")
Write-Output ($logInfo +":設定結束") | Out-File -FilePath $logPathFile -Encoding default -Append
}
唯讀掛載&開機自動掛載
[CmdletBinding()]
Param(
#掛載點
[string]$url="100.125.255.100\nas_test",
#掛載Windows盤符
[string]$drive="z"
)
#擷取當前執行時間
$now = Get-Date
#指定log檔案的場所 當前執行目錄
$logPath="C:\"
#設定log檔案名稱
$logName = "executLog"
$logFileName= $logName + "_" + $now.ToString("yyyy-MM-dd")+".log"
$logInfo = $now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " "
#log 全路徑設定
$logPathFile = Join-Path $logPath $logFileName
Write-Output ($logInfo+"掛載路徑:"+$url+" 掛載盤符:"+$drive)
Write-Output ($logInfo+"掛載路徑:"+$url+" 掛載盤符:"+$drive) | Out-File -FilePath $logPathFile -Encoding default -Append
$nfs=get-WindowsFeature -Name "NFS-Client"
if($nfs.installed){
$nas_start = (Get-Item HKCU:\Software\Microsoft\Windows\CurrentVersion\Run).GetValueNames().Contains("nas_run")
Write-Output ($logInfo+":開機自啟動>>"+$nas_start)
Write-Output ($logInfo+":開機自啟動>>"+$nas_start) | Out-File -FilePath $logPathFile -Encoding default -Append
if(!$nas_start){
Write-Output ($logInfo+":設定開機自啟動")
Write-Output ($logInfo+":設定開機自啟動") | Out-File -FilePath $logPathFile -Encoding default -Append
$nas_script_path = $myinvocation.mycommand.definition
$run_script = "powershell -file "+ $nas_script_path +" -url "+ $url +" -drive "+ $drive
$nas_run = "nas_run"
$nas_run_result = New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run" -Name $nas_run -PropertyType String -Value $run_script -ErrorAction Continue -ErrorVariable run_err_msg
if($err_msg){
Write-Output ($logInfo +"設定開機自啟動返回結果失敗:" +$run_err_msg)
Write-Output ($logInfo +"設定開機自啟動返回結果失敗:" +$run_err_msg) | Out-File -FilePath $logPathFile -Encoding default -Append
}else{
Write-Output ($logInfo +"設定開機自啟動完成:" +$nas_run_result)
Write-Output ($logInfo +"設定開機自啟動完成:" +$nas_run_result) | Out-File -FilePath $logPathFile -Encoding default -Append
}
}
Write-Output ($logInfo+"已存在nfs-client,掛載nfs掛載點")
Write-Output ($logInfo+"已存在nfs-client,掛載nfs掛載點") | Out-File -FilePath $logPathFile -Encoding default -Append
$nfs_url = "mount -o nolock -o mtype=hard -o timeout=60 \\$url ${drive}:"
Write-Output ($logInfo + ":開始掛載nas "+$nfs_url)
Write-Output ($logInfo + ":開始掛載nas "+$nfs_url) | Out-File -FilePath $logPathFile -Encoding default -Append
$reslut = cmd /c $nfs_url
Write-Output ($logInfo+$reslut)
Write-Output ($logInfo+$reslut) | Out-File -FilePath $logPathFile -Encoding default -Append
}else{
Write-Output ($logInfo +":沒有安裝nfs-client,進行安裝&註冊表配置 運行完成請重啟")
Write-Output ($logInfo +":沒有安裝nfs-client,進行安裝&註冊表配置 運行完成請重啟") | Out-File -FilePath $logPathFile -Encoding default -Append
Add-WindowsFeature -Name "NFS-Client"
$lock_path = Test-Path HKLM:\SOFTWARE\Microsoft\ClientForNFS\CurrentVersion\Users\Default\Mount
if($lock_path){
$lock = (Get-Item HKLM:\SOFTWARE\Microsoft\ClientForNFS\CurrentVersion\Users\Default\Mount).GetValueNames().Contains("Locking")
if(!$lock){
$lock_result = New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\ClientForNFS\CurrentVersion\Users\Default\Mount" -Name "Locking" -PropertyType DWord -Value 1 -ErrorAction Continue -ErrorVariable err_msg
if($err_msg){
Write-Output ($logInfo +"註冊表配置完成返回結果失敗:" +$err_msg)
Write-Output ($logInfo +"註冊表配置完成返回結果失敗:" +$err_msg) | Out-File -FilePath $logPathFile -Encoding default -Append
}else{
Write-Output ($logInfo +"註冊表配置完成返回結果為:" +$lock_result)
Write-Output ($logInfo +"註冊表配置完成返回結果為:" +$lock_result) | Out-File -FilePath $logPathFile -Encoding default -Append
}
}
}else{
Write-Output "不存在註冊表HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ClientForNFS\CurrentVersion\Users\Default\Mount項請自行產生"
Write-Output "不存在註冊表HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ClientForNFS\CurrentVersion\Users\Default\Mount項請自行產生" | Out-File -FilePath $logPathFile -Encoding default -Append
}
$nas_start = (Get-Item HKCU:\Software\Microsoft\Windows\CurrentVersion\Run).GetValueNames().Contains("nas_run")
Write-Output ($logInfo+":開機自啟動>>"+$nas_start)
Write-Output ($logInfo+":開機自啟動>>"+$nas_start) | Out-File -FilePath $logPathFile -Encoding default -Append
if(!$nas_start){
Write-Output ($logInfo+":設定開機自啟動")
Write-Output ($logInfo+":設定開機自啟動") | Out-File -FilePath $logPathFile -Encoding default -Append
$nas_script_path = $myinvocation.mycommand.definition
$run_script = "powershell -file "+ $nas_script_path +" -url "+ $url +" -drive "+ $drive
$nas_run = "nas_run"
$nas_run_result = New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run" -Name $nas_run -PropertyType String -Value $run_script -ErrorAction Continue -ErrorVariable run_err_msg
if($err_msg){
Write-Output ($logInfo +"設定開機自啟動返回結果失敗:" +$run_err_msg)
Write-Output ($logInfo +"設定開機自啟動返回結果失敗:" +$run_err_msg) | Out-File -FilePath $logPathFile -Encoding default -Append
}else{
Write-Output ($logInfo +"設定開機自啟動完成:" +$nas_run_result)
Write-Output ($logInfo +"設定開機自啟動完成:" +$nas_run_result) | Out-File -FilePath $logPathFile -Encoding default -Append
}
}
$nfs_url = "mount -o nolock -o mtype=hard -o timeout=60 \\$url ${drive}:"
Write-Output ($logInfo+":開始掛載nas "+$nfs_url)
Write-Output ($logInfo+":開始掛載nas "+$nfs_url) | Out-File -FilePath $logPathFile -Encoding default -Append
$result = cmd /c $nfs_url
Write-Output ($logInfo +":nas掛載結束" +$result)
Write-Output ($logInfo +":nas掛載結束" +$result) | Out-File -FilePath $logPathFile -Encoding default -Append
Write-Output ($logInfo +":設定結束")
Write-Output ($logInfo +":設定結束") | Out-File -FilePath $logPathFile -Encoding default -Append
}