使用 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.
发布评论
评论(2)
需要使用 Msvm_VirtualSystemManagementService.AddVirtualSystemResources 添加合成磁盘(ResourceType.Disk、ResourceSubType.DiskSynthetic)。父级 = SCSI 控制器的 WMI 路径。
然后使用 Msvm_VirtualSystemManagementService.AddVirtualSystemResources 附加虚拟硬盘(ResourceType.StorageExtent、ResourceSubType.VHD)。父级 = 合成磁盘的 WMI 路径,连接 = *.vhd 文件路径。
使用虚拟化示例的常用实用程序和< 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.
Then to attach virtual hard disk (ResourceType.StorageExtent, ResourceSubType.VHD) using Msvm_VirtualSystemManagementService.AddVirtualSystemResources. Parent = Synthetic disk's WMI path, Connection = *.vhd file path.
Use Common Utilities for the Virtualization Samples and WMI Explorer.
另请查看 http://hypervlib.codeplex.com 示例。
Also take a look at http://hypervlib.codeplex.com for an example.