|
|

前回までのレポートで解説した骨組みまで出来上がっている仮想マシンに、今回はネットワークアダプタの設定を追加していきます。今回はかなり難易度の高いコードとなりますが、ある程度パターンがあると思ってもらえると良いと思います。 (参照:コード01)
- サンプルコードをこちらからダウンロードいただけます。 → GMOReport.zip(16KB)
[ コード01 ]
1: Function SetNetworkAdapter(ByVal objManagementScope As ManagementScope, ByVal strVMName As String, ByVal strNetworkName As String) As Boolean
2: Dim objComputerSystem As ManagementObject = Nothing
3: For Each objManagementObject AsManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery("SELECT * FROM Msvm_ComputerSystem WHERE ElementName = '" & strVMName & "'")).Get
4: objComputerSystem = objManagementObject
5: Next
6:
7: Dim objVirtualSwitchManagementService As ManagementObject = Nothing
8: For Each objManagementObject As ManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery("SELECT * FROM Msvm_VirtualSwitchManagementService")).Get
9: objVirtualSwitchManagementService = objManagementObject
10: Next
11:
12: Dim objVirtualSwitch As ManagementObject = Nothing
13: Dim objSwitchPort As ManagementObject = Nothing
14: For Each objManagementObject As ManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery("SELECT * FROM Msvm_VirtualSwitch WHERE ElementName = '" & strNetworkName & "'")).Get
15: objVirtualSwitch = objManagementObject
16: Dim objParams As ManagementBaseObject = objVirtualSwitchManagementService.GetMethodParameters("CreateSwitchPort")
17: objParams("Name") = DateTime.Now.ToString("yyymmddhhmmss")
18: objParams("FriendlyName") = objParams("Name").ToString
19: objParams("VirtualSwitch") = objVirtualSwitch.Path.Path
20: Dim objManagementBaseObject As ManagementBaseObject = objVirtualSwitchManagementService.InvokeMethod("CreateSwitchPort", objParams, Nothing)
21: objSwitchPort = New ManagementObject(objManagementScope, New ManagementPath(objManagementBaseObject("CreatedSwitchPort").ToString), Nothing)
22: Next
23:
24: Dim objDefaultNic As ManagementObject = Nothing
25: For Each objResourcePool As ManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery("SELECT * FROM Msvm_ResourcePool WHERE ResourceType = 10 AND ResourceSubType = 'Microsoft Synthetic Ethernet Port' AND OtherResourceType = null")).Get
26: Dim objAllocationCapabilitiesCollection As ManagementObjectCollection = objResourcePool.GetRelated("Msvm_AllocationCapabilities")
27: For Each objAllocationCapabilities As ManagementObject In objAllocationCapabilitiesCollection
28: Dim objSettingsDefineCapabilitiesCollection As ManagementObjectCollection = objAllocationCapabilities.GetRelationships ("Msvm_SettingsDefineCapabilities")
29:
30: For Each objSettingsDefineCapabilities As ManagementObject In objSettingsDefineCapabilitiesCollection
31: If objSettingsDefineCapabilities("ValueRole") = 0 Then
32: objDefaultNic = New ManagementObject(objSettingsDefineCapabilities ("PartComponent").ToString())
33: objDefaultNic.Scope = objManagementScope
34: End If
35: Next
36: Next
37: Next
38:
39: For Each objVirtualSystemManagementService As ManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery("SELECT * FROM Msvm_VirtualSystemManagementService")).Get
40: Dim objParams As ManagementBaseObject = objVirtualSystemManagementService.GetMethodParameters ("AddVirtualSystemResources")
41: objDefaultNic("Connection") = New String() {objSwitchPort.Path.Path}
42: objDefaultNic("StaticMacAddress") = False
43: objDefaultNic("ElementName") = "ネットワーク アダプター"
44: Dim strVirtualSystemIdentifiers As String() = New String(0) {}
45: strVirtualSystemIdentifiers(0) = String.Format("{{{0}}}", Guid.NewGuid())
46: objDefaultNic("VirtualSystemIdentifiers") = strVirtualSystemIdentifiers
47:
48: Dim strResourceSettingData As String() = New String(0) {}
49: strResourceSettingData(0) = objDefaultNic.GetText(TextFormat.CimDtd20)
50: objParams("ResourceSettingData") = strResourceSettingData
51: objParams("TargetSystem") = objComputerSystem.Path.Path
52:
53: Dim objManagementBaseObject As ManagementBaseObject = objVirtualSystemManagementService.InvokeMethod ("AddVirtualSystemResources", objParams, Nothing)
54: Return JobComplete(objManagementBaseObject, objManagementScope)
55: Next
56: End Function
1行目:
引数として、ManagementScope オブジェクト、仮想マシン名、仮想ネットワーク名を渡します。仮想ネットワーク名とは、事前にHyper-Vマネージャーの仮想ネットワークマネージャーで、作成した仮想ネットワークです。今回は仮に「LAN」という名前で作成しました。
2~5行目:
仮想マシン名をキーにして、WMIクエリにてメモリ設定を行なう仮想マシンオブジェクトを取得します。
ここまでは前回の『メモリとプロセッサーの設定』と同様のパターンになります。
さて、ここから本題のネットワーク関連のオブジェクトの取り扱いとなります。
7~10行目:
仮想マシンに対して、ネットワーク関連の様々な設定を行なうには、仮想マシンごとにある、Msvm_VirtualSwitchManagementServiceクラスを介して設定してゆくことになります。
手順としては、まず事前に「LAN」という名前で作成した仮想スイッチアダプタ(仮想ネットワーク)に、ネットワークアダプタを接続するためのスイッチポートを作成します。次に仮想マシンにネットワークアダプタを作成し、追加して先ほど作成したスイッチポートに接続する手順になります。
12行目:
仮想スイッチアダプタ(仮想ネットワーク)を格納するためのオブジェクトを作成しておきます。
13行目:
以下で作成するスイッチポートを格納するためのオブジェクトを作成しておきます。
14~15行目:
仮想スイッチアダプタの一覧から、事前に作成した「LAN」という名前をSQLで選択して、取得します。
16行目:
Msvm_VirtualSwitchManagementService クラスからGetMethodParametersメソッドを利用して、スイッチポートを作成するためのCreateSwitchPortメソッドに必要なパラメーターを取得し、パラメーターに入力してゆきます。
17行目:
Nameには他のポート名とは重複しない名前が必要となるので、今回は作成時間を数値で指定しています。
18行目:
FriendlyNameは、Nameと同様の名前を使用しています。
19行目:
VirtualSwitchには、仮想スイッチアダプタ「LAN」オブジェクトを指定します
20行目:
CreateSwitchPortメソッドを使用して、スイッチポートを作成します。
21行目:
作成したスイッチポートをオブジェクトとして取得し、格納します。
24行目:
次に、仮想マシンに追加するネットワークアダプタオブジェクトを作成します。作成したネットワークアダプタオブジェクトを格納するためのオブジェクトを用意しておきます。
25行目:
仮想マシンにネットワークアダプタを追加する場合は、Hyper-Vのリソース一覧にあるオブジェクトを呼び出し、各パラメーターを設定して作成してゆきます。Msvm_ResourcePoolクラスには仮想マシンに追加することができるリソースが格納されています。Msvm_ResourcePoolクラスから、ResourceType = 10(Ethernet Adapter)、ResourceSubType = Microsoft Synthetic Ethernet Port、OtherResourceType = null という内容のSQL文でフィルタします。
ResourceTypeについては、こちらに一覧があります。
26~37行目:
Msvm_AllocationCapabilities オブジェクト(Msvm_AllocationCapabilities)、Msvm_SettingsDefineCapabilitiesオブジェクトを利用してネットワークアダプタを実際に追加していきますが、この部分のコードは解説自体が非常に困難なものとなりますので、ほぼ決まり文句といった感じでこのまま利用することになります。
39行目:
仮想マシン作成時同様に、Msvm_VirtualSystemManagementService クラスを使用して、ネットワークアダプタを追加してゆきます。Msvm_VirtualSystemManagementServiceクラスのオブジェクトの一つをFor~Next文で取り出します。
40行目:
仮想マシン作成時とはちがい、ネットワークアダプタの追加にはAddVirtualSystemResourcesメソッドを使用します。各パラメータは以下のようになります。
41行目:
先ほど作成したネットワークアダプタを仮想ネットワークアダプタに接続します。
42行目:
MACアドレスは動的に割り当てます。
43行目:
ElementNameはネットワーク アダプターとなります。
44~46行目:
仮想ネットワークアダプタを特定するために、一意のIDをGUIDより作成し割りあてます。
49~50行目:
仮想ネットワークアダプタをXML化した設定情報を、ResourceSettingDataパラメーターに入力します。
51行目:
ComputerSystemパラメーターには、仮想ネットワークアダプタを追加する仮想マシンを格納したMsvm_ComputerSystemオブジェクトを指定します。
53行目:
AddVirtualSystemResourcesメソッドを、入力したパラメーターの内容で実行します。
54行目:
今回もJobCompleteを使用してメソッドの実行を完了します。
以上で、エラーが出ること無くコードが走れば、Hyper-V上の仮想マシンに仮想スイッチアダプタ「LAN」に接続されたネットワークアダプタが追加されたことになります。
以上、今回はネットワークアダプタの追加方法についてご紹介しました。
次回は、仮想マシンに対して各パーツの設定方法をご紹介していきます。
- サンプルコードをこちらからダウンロードいただけます。 → GMOReport.zip(16KB)
- 【第70回】Hyper-Vを使ったクラウドサービスの作り方 - Vol.4 - Hyper-V Provisioning メモリとプロセッサーの設定
- 【第68回】Hyper-Vを使ったクラウドサービスの作り方 - Vol.3 - Hyper-V Provisioning仮想マシン一覧の取得
- 【第65回】Hyper-Vを使ったクラウドサービスの作り方 - Vol.2 - Build to Orderな仮想マシンについて
- 【第61回】Hyper-Vを使ったクラウドサービスの作り方 - Vol.1 - Windows Server ライセンスの種類、使い方の紹介
| 執筆協力した本が出版されました | ||
当レポートを担当しているGMOインターネット株式会社 樋口 勝一が執筆協力をした書籍が出版されました。 |
||
![]() |
最新!Android徹底活用読本 出版社:宝島社 価格:880円(税込) ※価格は店舗によって異なります。 次々に発売されるAndroidケータイを「コミュニケーションしたい」「情報をブラウジングしたい」「ビジネス手帳の代わりにしたい」「音楽や動画を120%楽しみたい」など、やりたいこと別に章立てした活用書。 |
|
![]() |
GALAXY Sユーザーのためのアプリ245 出版社:三才ブックス 価格:1,260円(税込) ※価格は店舗によって異なります。 25万本以上あるといわれるAndroidアプリの中から、目的別・状況別に本当に使えるアプリだけを厳選。 |
|
![]() |
ソニー・エリクソン公式 Xperia arc ガイドブック 出版社:日経BP社 価格:1,260円(税込) ※価格は店舗によって異なります。 こだわりのデザインと薄さを追求しながら、Androidスマートフォンのフラッグシップモデルとして世界に先駆けて日本で発売されるXperia arcの公式ガイドブックです。 |
|
*本文中に記載されている会社名および商品名・サービス名は、各社の商標 または登録商標です。




























