WIN1@Codename

Codename

スポンサーサイト

上記の広告は1ヶ月以上更新のないブログに表示されています。
新しい記事を書く事で広告が消せます。

Hyper-V をプログラムでどうにかしてみる その15 (SCSIディスクの追加)

前回SCSIコントローラを仮想マシンに追加しましたので、こちらにvhdディスクをマウントしてみます。

Dim objComputerSystem As ManagementObject = Nothing
For Each objManagementObject As ManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery("SELECT * FROM Msvm_ComputerSystem WHERE ElementName = '" & strVMName & "'")).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 objSCSIController As ManagementObject = Nothing
For Each objManagementObject As ManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery("SELECT * FROM Msvm_ResourceAllocationSettingData WHERE ResourceType = 6 AND ResourceSubType = 'Microsoft Synthetic SCSI Controller' AND OtherResourceType = null")).Get
    objSCSIController = objManagementObject
    objDiskDrive("Parent") = objSCSIController.Path.Path
    objDiskDrive("Address") = 0
    If objSCSIController("InstanceID").ToString.EndsWith("0") Then Exit For
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 objSCSIDrive As ManagementObject = Nothing
For Each objManagementObject As ManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery("SELECT * FROM Msvm_ResourceAllocationSettingData WHERE Parent = '" & objSCSIController.Path.Path.Replace("\", "\\") & "' AND Address = '0'")).Get
    objSCSIDrive = objManagementObject
    objHardDisk("Parent") = objSCSIDrive.Path.Path
    objHardDisk("Connection") = New String(0) {strDiskPath}
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)
Next 

SCSIコントローラーさえ出来ていればIDEのハードディスクをマウントする方法と同じです。
SCSI接続であれば、ホットスワップができるので、使い方次第ではvhdファイルを使ってのファイルの受け渡しなどいろいろできそうです。

コメント

コメントの投稿


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

トラックバック

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

FC2Ad

上記広告は1ヶ月以上更新のないブログに表示されています。新しい記事を書くことで広告を消せます。