在Windows操作系统中,大家通常会用Administrator 用户挂载卷。但是windows系统服务、计划任务和很多系统调用都是用SYSTEM系统账号操作的。所以会导致SYSTEM 账号无法使用Administrator 挂载的文件存储。下面我们以SQLServer使用NAS SMB文件存储为例,介绍如何以SYSTEM账号挂载NAS SMB文件存储。
1. 以SYSTEM身份挂载NAS SMB文件卷
1.1. 我们建立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 账号挂载,可以优先尝试上述的解决方案。