在Windows操作系統中,系統服務(Services)、計劃任務(Scheduled Tasks)以及很多系統調用都是以SYSTEM系統賬號進行操作的。如果在阿里雲ECS用 net use 掛載,或者在文件管理器上直接掛載,掛載卷是以 Administrator 身份進行掛載的,而 SYSTEM 賬號無法使用 Administrator 掛載的文件卷。
關於SYSTEM賬號的更多內容請參考 How the SYSTEM account is used in Windows[1] 以及 SYSTEM account in Windows[2]。
以下我們就以SQLServer使用NAS SMB文件卷為例,介紹如何以SYSTEM賬號掛載NAS SMB卷。
0. 安裝 SQL Server Express 2014
我們的實驗中使用的是 SQL Server Express 2014,下載地址:https://www.microsoft.com/en-in/download/details.aspx?id=42299
下載時請選擇 ExpressAndTools 64BIT\SQLEXPRWT_x64_ENU.exe。
1. 以SYSTEM身份掛載NAS SMB文件卷
1.1. 參考 Map network drive for the SYSTEM user in Windows[3] ,我們建立
c:\my_mount.bat 腳本,內容如下:
ECHO ON
ECHO This will map the drive, but is being run by task scheduler AS the user SYSTEM
ECHO which should make it accessible to the user SYSTEM
ECHO List the existing drives first.
net use >> c:\SystemNetUseOutput.txt
net use y: \\xxx.nas.aliyuncs.com\myshare
ECHO List the existing drives with the new mapping
net use >> c:\SystemNetUseOutput.txt
ECHO See what user this batch job ran under
whoami >> c:\SystemNetUseOutput.txt
ECHO need to exit to allow the job to finish
EXIT
將腳本中的xxx改為您的卷掛載點。以上腳本會掛載NAS SMB卷,並且會輸出是以何種身份掛載。
1.2. 可以在命令行運行以下命令生成 my_mount 任務並運行該任務
schtasks /create /tn "my_mount" /tr "c:\my_mount.bat" /sc onstart /RU SYSTEM /RL HIGHEST
schtasks /run /tn "my_mount"
1.3. 運行完之後的結果
在 C:\SystemNetUseOutput.txt 中應該能看到類似這樣的結果
New connections will be remembered.
There are no entries in the list.
New connections will be remembered.
Status Local Remote Network
OK y: \\xxx.nas.aliyuncs.com\myshare Microsoft Windows Network
The command completed successfully.
nt authority\system
掛載成功後文件管理器會顯示類似這樣的文件卷:
1.4. Windows Server 2016 及以上版本的注意事項
Windows Server 2016 及以上版本客戶端不允許匿名訪問。用戶可以使用workshop\administrator身份進行掛載。
將1.1.中的腳本改為:
ECHO ON
ECHO This will map the drive, but is being run by task scheduler AS the user SYSTEM
ECHO which should make it accessible to the user SYSTEM
ECHO List the existing drives first.
net use >> c:\SystemNetUseOutput.txt
net use y: \\xxx.nas.aliyuncs.com\myshare /user:workshop\administrator PASSWORD
ECHO List the existing drives with the new mapping
net use >> c:\SystemNetUseOutput.txt
ECHO See what user this batch job ran under
whoami >> c:\SystemNetUseOutput.txt
ECHO need to exit to allow the job to finish
EXIT
其他步驟相同。
1.5. 也可以使用圖形界面生成 my_mount 任務並運行該任務
1.5.1. 打開任務計劃程序
1.5.2. 點擊創建基本任務,寫入任務名 my_mount
1.5.3. 點擊下一步,選擇 計算機啟動時
1.5.4. 選擇 啟動程序
1.5.5. 程序或腳本寫入:C:\my_mount.bat
1.5.6. 點擊下一步,點擊完成
1.5.7. 在活動任務中找到 my_mount
1.5.8. 雙擊進入,選擇 運行
1.6. 運行完之後的結果
在 C:\SystemNetUseOutput.txt 中應該能看到類似這樣的結果
New connections will be remembered.
There are no entries in the list.
New connections will be remembered.
Status Local Remote Network
OK y: \\xxx.nas.aliyuncs.com\myshare Microsoft Windows Network
The command completed successfully.
nt authority\system
掛載成功後文件管理器會顯示類似這樣的文件卷:
1.7. Windows Server 2016 及以上版本的注意事項(同1.4.)
Windows Server 2016 及以上版本客戶端不允許匿名訪問。用戶可以在本地創建一個專門用來掛載的用戶,比如 mount_user。
將1.1.中的腳本改為:
ECHO ON
ECHO This will map the drive, but is being run by task scheduler AS the user SYSTEM
ECHO which should make it accessible to the user SYSTEM
ECHO List the existing drives first.
net use >> c:\SystemNetUseOutput.txt
net use y: \\xxx.nas.aliyuncs.com\myshare /user:workshop\administrator PASSWORD
ECHO the /P switch makes the drive remain after reboot
ECHO List the existing drives with the new mapping
net use >> c:\SystemNetUseOutput.txt
ECHO See what user this batch job ran under
whoami >> c:\SystemNetUseOutput.txt
ECHO need to exit to allow the job to finish
EXIT
其他步驟相同。
2. SQLServer使用NAS SMB卷存儲數據庫
在沒有以 SYSTEM 身份掛載文件卷之前,在SQLServer 2014中選擇 Databases->Attach ,無法看到 Administrator 掛載的文件卷:
在採用上述方法掛載之後,可以看到NAS SMB卷(Y:)
之後就可以將數據庫文件存放到文件捲上了。
3. 總結:SYSTEM 身份掛載文件卷可解的問題列表
SYSTEM身份掛載文件卷不僅可以解決SQLServer使用文件卷的問題,還可以解決很多跟 SYSTEM 身份相關的問題。以下列舉一下已知的問題:
3.1. SQLServer使用文件卷
段落2中有詳細描述。
3.2. 某些服務(Services)無法啟動
比如如果一個服務(Service)裡面調用了文件卷裡的執行程序,如果文件卷不是以 SYSTEM 身份掛載的,則服務無法執行,會返回程序無法加載。採用上述的 SYSTEM 身份掛載解決方案就可以讓服務啟動了。
在碰到依賴文件卷的系統程序無法執行,並且抓包沒有明顯錯誤時,問題很可能出在卷需要用 SYSTEM 賬號掛載,可以優先嚐試上述的解決方案。