WIN1@Codename

Codename

Hyper-V をプログラムでどうにかしてみる その10 (ハードディスクの設定)

ようやく仮想マシンの作成も最終ステップとなりました。
ハードディスクの設定が最後になります。
最後の最後が一番複雑なコードになっています。

Dim objConnectionOptions As New ConnectionOptions()
objConnectionOptions.Impersonation = ImpersonationLevel.Impersonate
objConnectionOptions.EnablePrivileges = True
objConnectionOptions.Username = strAccount
objConnectionOptions.Password = strPassword
Dim objManagementScope As New ManagementScope("\\" + strServer + "\root\virtualization", objConnectionOptions)
objManagementScope.Connect()

Dim objComputerSystem As ManagementObject = Nothing
For Each objManagementObject As ManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery("SELECT * FROM Msvm_ComputerSystem WHERE ElementName = 'TestVM'")).Get
    objComputerSystem = objManagementObject
Next

Dim objDiskDrive As ManagementObject = Nothing
For Each objResourcePool As ManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery("SELECT * FROM Msvm_ResourcePool WHERE ResourceType = 22 AND ResourceSubType = 'Microsoft Synthetic Disk Drive' AND OtherResourceType = null")).Get
    Dim objAllocationCapabilitiesCollection As ManagementObjectCollection = objResourcePool.GetRelated("Msvm_AllocationCapabilities")
    For Each objAllocationCapabilities As ManagementObject In objAllocationCapabilitiesCollection
Dim objSettingsDefineCapabilitiesCollection As ManagementObjectCollection = objAllocationCapabilities.GetRelationships("Msvm_SettingsDefineCapabilities")
For Each objSettingsDefineCapabilities As ManagementObject In objSettingsDefineCapabilitiesCollection
    If objSettingsDefineCapabilities("ValueRole") = 0 Then
objDiskDrive = New ManagementObject(objSettingsDefineCapabilities("PartComponent").ToString())
objDiskDrive.Scope = objManagementScope
    End If
Next
    Next
Next

Dim objIDEController As ManagementObject = Nothing
For Each objManagementObject As ManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery("SELECT * FROM Msvm_ResourceAllocationSettingData WHERE ResourceType = 5 AND ResourceSubType = 'Microsoft Emulated IDE Controller' AND Address = '0'")).Get
    objIDEController = objManagementObject
    objDiskDrive("Parent") = objIDEController.Path.Path
    objDiskDrive("Address") = 0
Next

For Each objVirtualSystemManagementService As ManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery("SELECT * FROM Msvm_VirtualSystemManagementService")).Get
    Dim objParams As ManagementBaseObject = objVirtualSystemManagementService.GetMethodParameters("AddVirtualSystemResources")
    Dim strResourcesettingData As String() = New String(0) {}
    strResourcesettingData(0) = objDiskDrive.GetText(TextFormat.CimDtd20)
    objParams("ResourceSettingData") = strResourcesettingData
    objParams("TargetSystem") = objComputerSystem.Path.Path
    Dim objManagementBaseObject As ManagementBaseObject = objVirtualSystemManagementService.InvokeMethod("AddVirtualSystemResources", objParams, Nothing)
Next

Dim objHardDisk As ManagementObject = Nothing
For Each objResourcePool As ManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery("SELECT * FROM Msvm_ResourcePool WHERE ResourceType = 21 AND ResourceSubType = 'Microsoft Virtual Hard Disk' AND OtherResourceType = null")).Get
    Dim objAllocationCapabilitiesCollection As ManagementObjectCollection = objResourcePool.GetRelated("Msvm_AllocationCapabilities")
    For Each objAllocationCapabilities As ManagementObject In objAllocationCapabilitiesCollection
Dim objSettingsDefineCapabilitiesCollection As ManagementObjectCollection = objAllocationCapabilities.GetRelationships("Msvm_SettingsDefineCapabilities")
For Each objSettingsDefineCapabilities As ManagementObject In objSettingsDefineCapabilitiesCollection
    If objSettingsDefineCapabilities("ValueRole") = 0 Then
objHardDisk = New ManagementObject(objSettingsDefineCapabilities("PartComponent").ToString())
objHardDisk.Scope = objManagementScope
    End If
Next
    Next
Next

Dim objIDEDrive As ManagementObject = Nothing
For Each objManagementObject As ManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery("SELECT * FROM Msvm_ResourceAllocationSettingData WHERE Parent = '" & objIDEController.Path.Path.Replace("\", "\\") & "' AND Address = '0'")).Get
    objIDEDrive = objManagementObject
    objHardDisk("Parent") = objIDEDrive.Path.Path
    objHardDisk("Connection") = New String(0) {"C:\Hyper-V\TestVM.vhd"}
Next

For Each objVirtualSystemManagementService As ManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery("SELECT * FROM Msvm_VirtualSystemManagementService")).Get
    Dim objParams As ManagementBaseObject = objVirtualSystemManagementService.GetMethodParameters("AddVirtualSystemResources")
    Dim strResourcesettingData As String() = New String(0) {}
    strResourcesettingData(0) = objHardDisk.GetText(TextFormat.CimDtd20)
    objParams("ResourcesettingData") = strResourcesettingData
    objParams("TargetSystem") = objComputerSystem.Path.Path
    Dim objManagementBaseObject As ManagementBaseObject = objVirtualSystemManagementService.InvokeMethod("AddVirtualSystemResources", objParams, Nothing)
    Console.WriteLine("SetHardDisk=" & objManagementBaseObject("ReturnValue"))
Next 

「TestVM」という名前の仮想マシンに、「TestVM.vhd」という仮想ハードディスクを接続しています。
大まかな流れとしては、まずIDEコントローラーの0番を取得します。
取得したコントローラーの0番にvhdを接続しています。
20100916100400

と、いうことでひとまず仮想マシンの一通りの設定が完了しました。
今回も細かい説明は省いていますが、詳細などなんなりとご質問ください。

コメント

コメントの投稿


管理者にだけ表示を許可する

トラックバック

トラックバック URL
http://codename2010.blog11.fc2.com/tb.php/131-fa68d768
この記事にトラックバックする(FC2ブログユーザー)

FC2Ad