以编程方式将 VHD 附加到远程 Hyper-V VM

发布于 2024-08-20 02:15:12 字数 960 浏览 11 评论 0 原文

使用 Hyper-V 管理器,我可以连接到远程 VM 主机,转到 VM 的设置,并将现有 .VHD 文件添加为新硬盘。如果 VM 主机运行 Server 2008 R2,并且磁盘连接到 SCSI 控制器,我什至可以在 VM 运行时执行此操作(请参阅 Hyper-V R2 中的新增功能)。

手动执行此操作,一切都很好。问题是,现在我想将其自动化,这样我就可以在一些自动化测试期间即时附加不同的 VHD。

我已经有了通过 WMI 连接到远程 VM 主机并通过调用 RequestStateChange,我想将其扩展为能够说“这是 VHD 的路径,将其作为 SCSI 驱动器附加到此虚拟机”。但是查看 WMI 虚拟化类列表 ,我不知道该怎么做。

我发现的最接近的是 Mount Msvm_ImageManagementService 的方法,但这似乎挂载当前操作系统内的 VHD,这不是我想要的。

Using Hyper-V Manager, I can connect to a remote VM host, go to the settings of a VM, and add an existing .VHD file as a new hard disk. If the VM host is running Server 2008 R2, and the disk is being attached to a SCSI controller, I can even do this while the VM is running (see What's new in Hyper-V R2).

Doing this manually, everything works great. The trouble is, now I want to automate it so I can attach different VHDs on-the-fly during some automated tests.

I already have C# code that connects to the remote VM host over WMI and starts/stops VMs by calling RequestStateChange, and I'd like to extend it to be able to say "here's the path to a VHD, attach it as a SCSI drive to this VM". But looking at the list of WMI virtualization classes, I can't figure out how to do this.

The closest I've found is the Mount method of Msvm_ImageManagementService, but this appears to mount a VHD inside the current OS, which isn't what I want.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沙与沫 2024-08-27 02:15:12

需要使用 Msvm_VirtualSystemManagementService.AddVirtualSystemResources 添加合成磁盘(ResourceType.Disk、ResourceSubType.DiskSynthetic)。父级 = SCSI 控制器的 WMI 路径。

ManagementObject synthetic = Utilities.GetResourceAllocationSettingData(scope,
    ResourceType.Disk, ResourceSubType.DiskSynthetic);
synthetic["Parent"] = <ideControllerPath>; //or SCSI controller path (WMI path)
synthetic["Address"] = <diskDriveAddress>; //0 or 1 for IDE
string[] RASDs = new string[1];
RASDs[0] = synthetic.GetText(TextFormat.CimDtd20);

然后使用 Msvm_VirtualSystemManagementService.AddVirtualSystemResources 附加虚拟硬盘(ResourceType.StorageExtent、ResourceSubType.VHD)。父级 = 合成磁盘的 WMI 路径,连接 = *.vhd 文件路径。

ManagementObject hardDisk = Utilities.GetResourceAllocationSettingData(scope,  
    ResourceType.StorageExtent, ResourceSubType.VHD);
hardDisk["Parent"] = <syntheticPath>; //WMI path
string[] connection = { <vhdPath> }; //Path to *.vhd file
hardDisk["Connection"] = connection;
string[] RASDs = new string[1];
RASDs[0] = hardDisk.GetText(TextFormat.CimDtd20);

使用虚拟化示例的常用实用程序和< a href="http://www.ks-soft.net/hostmon.eng/wmi/index.htm" rel="nofollow noreferrer">WMI 资源管理器。

It is necessary to add synthetic disk (ResourceType.Disk, ResourceSubType.DiskSynthetic) using Msvm_VirtualSystemManagementService.AddVirtualSystemResources. Parent = SCSI controller's WMI path.

ManagementObject synthetic = Utilities.GetResourceAllocationSettingData(scope,
    ResourceType.Disk, ResourceSubType.DiskSynthetic);
synthetic["Parent"] = <ideControllerPath>; //or SCSI controller path (WMI path)
synthetic["Address"] = <diskDriveAddress>; //0 or 1 for IDE
string[] RASDs = new string[1];
RASDs[0] = synthetic.GetText(TextFormat.CimDtd20);

Then to attach virtual hard disk (ResourceType.StorageExtent, ResourceSubType.VHD) using Msvm_VirtualSystemManagementService.AddVirtualSystemResources. Parent = Synthetic disk's WMI path, Connection = *.vhd file path.

ManagementObject hardDisk = Utilities.GetResourceAllocationSettingData(scope,  
    ResourceType.StorageExtent, ResourceSubType.VHD);
hardDisk["Parent"] = <syntheticPath>; //WMI path
string[] connection = { <vhdPath> }; //Path to *.vhd file
hardDisk["Connection"] = connection;
string[] RASDs = new string[1];
RASDs[0] = hardDisk.GetText(TextFormat.CimDtd20);

Use Common Utilities for the Virtualization Samples and WMI Explorer.

清风不识月 2024-08-27 02:15:12

另请查看 http://hypervlib.codeplex.com 示例。

Also take a look at http://hypervlib.codeplex.com for an example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文