简体   繁体   English

如何以编程方式在Windows中挂载原始iscsi卷,并准备使用它?

[英]How do I programatically mount a raw iscsi volume in windows, and get it ready to use?

The API for creating a new volume on our SAN is pretty straight forward. 在我们的SAN上创建新卷的API非常简单。 What I have not been able to figure out is how to programatically connect the iSCSI initiator to it, initialize the space (in the windows disk manager sense) and finally format it and mount it is a drive letter. 我还不能弄清楚如何以编程方式将iSCSI启动器连接到它,初始化空间(就Windows磁盘管理器而言),最后格式化并安装它是一个驱动器号。

I currently use win2k3, however moving to win2k8 is an option if it would simplify implementing this. 我目前使用的是win2k3,但是如果可以简化实现,可以选择使用win2k8。

I had to implement disk initialization, partitioning, and formatting for one of my company's products. 我必须为公司的一种产品实施磁盘初始化,分区和格式化。 I can't share the code but I can point you in the right direction. 我无法共享代码,但可以为您指明正确的方向。

The API you want is called VDS - Virtual Disk Service . 所需的API称为VDS- 虚拟磁盘服务 It's a COM API, but I've used it successfully from C++ (with ATL) and C# (COM interop). 这是一个COM API,但是我已经在C ++(带有ATL)和C#(COM互操作)中成功使用了它。

Sadly the documentation is quite lacking; 遗憾的是,该文件非常缺乏。 you just have to immerse yourself in the object model, write some code, and gradually you get a feel for it. 您只需要将自己沉浸在对象模型中,编写一些代码,然后逐渐对它有所了解。

Windows Server 2008 ships with an undocumented but quite usable C# wrapper around VDS. Windows Server 2008附带了一个围绕VDS的未记录但相当可用的C#包装器。 Look for a DLL called Microsoft.Storage.Vds.dll. 查找一个名为Microsoft.Storage.Vds.dll的DLL。 You can use Reflector to discover its various classes and methods. 您可以使用Reflector来发现其各种类和方法。 I found out about this when I read this blog post , in which the author is trying to initialize a disk from PowerShell using the aforementioned DLL. 当我阅读此博客文章时 ,我发现了有关此内容,作者在其中尝试使用上述DLL从PowerShell初始化磁盘。

VDS includes APIs that could be implemented by SAN vendors to provision a LUN and do other SAN things; VDS包含可由SAN供应商实施的API,以配置LUN并执行其他SAN任务; suggest you avoid those and focus on the basic software provider, which will create basic (as opposed to dynamic) partitions on either an MBR or GPT disk. 建议您避免使用这些软件,而应将重点放在基本的软件提供程序上,后者将在MBR或GPT磁盘上创建基本(相对于动态)分区。 Note that the Microsoft wrapper I mentioned is a bit light on GPT support; 请注意,我提到的Microsoft包装器对GPT的支持略有不同。 I had to modify it a bit to get GPT disks working. 我必须对其进行一些修改才能使GPT磁盘正常工作。

VDS is a complex and finicky API, but if you're just looking to initialize a disk, create a partition, format it, and mount it to a drive letter, most of what you need is there and fairly easy to do. VDS是一个复杂而挑剔的API,但是,如果您只是想初始化磁盘,创建分区,对其进行格式化并将其装入驱动器号,那么您所需的大部分就已经存在并且相当容易实现。 Good luck. 祝好运。

I have done this from Powershell using diskpart with a script. 我已经从Powershell使用diskpart和脚本完成了此操作。

Something like: 就像是:

$target    = ''    #Desired target IQN
$partition = 1     #Desired partition
$drvLetter = ''    #Desired drive letter

#get the disk device number from the iscsi session class (diskpart needs it)
$iscsiSsn = gwmi -namespace "root\wmi" -class MSiSCSIInitiator_SessionClass
$diskNum = ($($iscsiSsn | where { $_.targetname -eq $target}).devices).deviceNumber

#create the diskpart script on-the-fly then call the utility
$dskPrtScr = "$($env:temp)\diskpart.scr"
"sel disk $diskNum`nsel par 1`nassign letter=$drvLetter`nexit" | out-file $dskPrtScr -encoding ascii
diskpart /s $dskPrtScr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM