2

我想创建一个具有确定数量的 RAM、网卡、处理器内核数量的新 Hyper-V VM,并将 VHD 文件附加到 IDE 控制器。

问题是它Msvm_ResourceAllocationSettingData不是很容易使用。我使用的代码不起作用(这是将 VHD 附加到现有 VM 的代码,但我也想在创建新 VHD 时使用它)。

public void AttachVhd(IdeChannel ideChannel, String vhdPath) {

        // Get VirtualSystemSettingData
        ManagementObject vssd = this.GetVirtualSystemSettingData();

        // Get the IDE Controller
        ManagementObject ideController = this.GetResourceAllocationSettingData(ResourceType.IdeController, ResourceSubType.IdeController);

        // Create synthetic disk:
        ManagementObject syntheticDiskRasd = this.GetResourceAllocationSettingData(ResourceType.Disk, ResourceSubType.DiskSynthetic);
        syntheticDiskRasd["Parent"]  = ideController.Path;
        syntheticDiskRasd["Address"] = ideChannel == IdeChannel.Primary ? "0" : "1";

        this.AddVirtualSystemResources(syntheticDiskRasd);

        // Attach it
        ManagementObject vhdRasd = this.GetResourceAllocationSettingData(ResourceType.StorageExtent, ResourceSubType.Vhd); ;
        vhdRasd["Parent"]     = syntheticDiskRasd.Path;
        vhdRasd["Connection"] = new String[] { vhdPath };

        this.AddVirtualSystemResources( vhdRasd );

        // Cleanup
        vhdRasd.Dispose();
        syntheticDiskRasd.Dispose();
        ideController.Dispose();
        vssd.Dispose();
    }

    private ManagementObject GetResourceAllocationSettingData(ResourceType resourceType, ResourceSubType resourceSubType)
    {
        String desiredSubType = ResourceSubTypeStrings.GetString(resourceSubType); // Scout.Common.Extensions.GetDescription( resourceType );

        using(ManagementObjectCollection settingsDatas = _vm.GetRelated("Msvm_VirtualSystemSettingData"))
        foreach(ManagementObject settingData in settingsDatas)
        {
            using(ManagementObjectCollection rasds = settingData.GetRelated("Msvm_ResourceAllocationSettingData"))
            foreach(ManagementObject rasd in rasds)
            {
                ResourceType rasdResourceType    = (ResourceType)(UInt16)rasd["ResourceType"];
                String       rasdResourceSubType =               (String)rasd["ResourceSubType"];
                String       rasdOtherType       =               (String)rasd["OtherResourceType"];

                if( rasdResourceType == resourceType && rasdResourceSubType == desiredSubType )
                {
                    return rasd;
                }
            }
        }

        return null;
    }

    private void AddVirtualSystemResources(ManagementObject rasd)
    {
        using (ManagementObject vmService = HyperV.GetManagementService())
        {
            ManagementBaseObject inParams = vmService.GetMethodParameters("AddVirtualSystemResources");
            inParams["TargetSystem"]         = _vm;
            inParams["ResourceSettingsData"] = rasd.GetText(TextFormat.CimDtd20);

            ManagementBaseObject outParams = vmService.InvokeMethod("AddVirtualSystemResources", inParams, options: null);

            String[]            addedResources = (String[])outParams["NewResources"];
            OperationReturnCode returnValue    = (OperationReturnCode)(UInt32)outParams["ReturnValue"];

            if (returnValue == OperationReturnCode.JobStarted)
            {
                String jobPath = (String)outParams["Job"];
                HyperV.MonitorJob(jobPath);
            }
            else if (returnValue == OperationReturnCode.Completed)
            {
            }
            else
            {
                throw new ApplicationException( returnValue.ToString() );
            }
        }
    }
4

1 回答 1

-1

我可以为您指出一个有效的示例,而不是找到您的问题吗?

WmiCalls.DeployVirtualMachine我的 Apache CloudStack Hyper-V 插件中查看

如果您需要更多详细信息,请发表评论,我会更新答案。

于 2013-05-11T20:50:14.817 回答