WIN1@Codename

Codename (http://codename.win1.jp/)

Hyper-Vを使ったクラウドサービスの作り方 Vol.4 -Hyper-V Provisioning メモリとプロセッサーの設定方法-

Hyper-V上の仮想マシンのメモリを512MBから任意の値に変更

デフォルトでは512MBのメモリが割り当てられています。こちらを任意の値に変更してみましょう。
はじめに、静的メモリの設定方法からご紹介します。(参照:コード01)

[ コード01 ]
   1:  FunctionSetStaticMemory( ByValobjManagementScope AsManagementScope,   ByValstrVMName AsString, ByValintMemory AsInteger) AsBoolean
   2:  DimobjComputerSystem AsManagementObject = Nothing
   3:  ForEachobjManagementObject AsManagementObject   InNewManagementObjectSearcher(objManagementScope, NewObjectQuery  ("SELECT * FROM Msvm_ComputerSystem WHERE ElementName = '"  & strVMName & "'")).Get
   4:  objComputerSystem = objManagementObject
   5:  Next
   6:   
   7:  DimobjVirtualSystemSettingData AsManagementObject = Nothing
   8:  ForEachobjManagementObject AsManagementObject   InNewManagementObjectSearcher(objManagementScope, NewObjectQuery  ("SELECT * FROM Msvm_VirtualSystemSettingData WHERE ElementName = '"  & strVMName & "'")).Get
   9:  objVirtualSystemSettingData = objManagementObject
  10:  Next
  11:   
  12:  DimstrMemorySettingData AsString= ""
  13:  DimobjMemorySettingDataCollection AsManagementObjectCollection =   objVirtualSystemSettingData.GetRelated("Msvm_MemorySettingData")
  14:  ForEachobjManagementObject AsManagementObject   InobjMemorySettingDataCollection
  15:  objManagementObject("VirtualQuantity") = intMemory
  16:  strMemorySettingData = objManagementObject.GetText(TextFormat.CimDtd20)
  17:  Next
  18:   
  19:  ForEachobjVirtualSystemManagementService AsManagementObject   InNewManagementObjectSearcher(objManagementScope, NewObjectQuery ("SELECT * FROM Msvm_VirtualSystemManagementService")).Get
  20:  DimobjParams AsManagementBaseObject =   objVirtualSystemManagementService.GetMethodParameters  "ModifyVirtualSystemResources")
  21:  objParams("ResourceSettingData") = NewString() {strMemorySettingData}
  22:  objParams("ComputerSystem") = objComputerSystem.Path.Path
  23:  DimobjManagementBaseObject AsManagementBaseObject =   objVirtualSystemManagementService.InvokeMethod  ("ModifyVirtualSystemResources", objParams, Nothing)
  24:  ReturnJobComplete(objManagementBaseObject, objManagementScope)
  25:  Next
  26:  EndFunction


    1行目:
    引数として、ManagementScope オブジェクト、仮想マシン名、メモリ容量を渡します。

    2〜5行目:
    仮想マシン名をキーにして、WMIクエリでメモリ設定を行う仮想マシンオブジェクトを取得します。

    7〜10行目:
    仮想マシンに対して、様々な設定を行うには、仮想マシンごとにある、Msvm_VirtualSystemSettingData クラスを介して設定していきます。

    13行目:
    取得したMsvm_VirtualSystemSettingDataオブジェクトを利用して、メモリの設定を行うためにMsvm_MemorySettingDataクラスから、メモリ設定の項目一覧を取得します。

    14〜17行目:
    項目一覧から今回設定するメモリ容量のプロパティーに設定値を入力。
    今回は1GBのメモリを設定するので、VirtualQuantityに1024を指定。
    設定情報をXML形式で保存します。

    19行目:
    仮想マシン作成時と同様に、Msvm_VirtualSystemManagementService クラスを使用して、
    メモリ容量を変更していきます。
    Msvm_VirtualSystemManagementServiceクラスのオブジェクトの一つをFor〜Next文で取り出します。

    20行目:
    仮想マシンの作成時とは異なり、メモリ容量などの変更には、ModifyVirtualSystemResourcesメソッドを使用します。事前にModifyVirtualSystemResourcesメソッドで、必要となるパラメーターオブジェクトをGetMethodParametersで取得し、各項目に設定値を入力しておきます。

    21行目:
    先ほど設定したメモリ容量をXML化した設定情報を、ResourceSettingDataパラメーターに入力します。

    22行目:
    ComputerSystemパラメーターには、4行目で取得したメモリ容量を変更する仮想マシンを格納したMsvm_ComputerSystemオブジェクトを指定します。

    23行目:
    ModifyVirtualSystemResourcesメソッドを、入力したパラメーターの内容で実行します。

    24行目:
    今回もJobCompleteを使用して、メソッドの実行を完了します。



以上で、エラーが無くコードが走れば、Hyper-V上の仮想マシンのメモリは512MBから1GB(1024MB)に変更されています。





DynamicMemoryの設定方法

さて、次は、SP1の売りの一つ、DynamicMemoryの設定方法です。基本的な流れはStaticMemoryと同様ですが、Msvm_MemorySettingDataで設定する項目に、DynamicMemory特有のものが増えています。(参照:コード02)

[ コード02 ]
   1:  FunctionSetDynamicMemory(ByValobjManagementScope AsManagementScope,   ByValstrVMName AsString, ByValintMemory AsInteger) AsBoolean
   2:  DimobjComputerSystem AsManagementObject = Nothing
   3:  ForEachobjManagementObject AsManagementObject   InNewManagementObjectSearcher(objManagementScope, NewObjectQuery  ("SELECT * FROM Msvm_ComputerSystem WHERE ElementName = '"  & strVMName & "'")).Get
   4:  objComputerSystem = objManagementObject
   5:  Next
   6:   
   7:  DimobjVirtualSystemSettingData AsManagementObject = Nothing
   8:  ForEachobjManagementObject AsManagementObject   InNewManagementObjectSearcher(objManagementScope, NewObjectQuery  ("SELECT * FROM Msvm_VirtualSystemSettingData WHERE ElementName = '"  & strVMName & "'")).Get
   9:  objVirtualSystemSettingData = objManagementObject
  10:  Next
  11:   
  12:  DimstrMemorySettingData AsString= ""
  13:  DimobjMemorySettingDataCollection AsManagementObjectCollection =   objVirtualSystemSettingData.GetRelated("Msvm_MemorySettingData")
  14:  ForEachobjManagementObject AsManagementObject   InobjMemorySettingDataCollection
  15:  objManagementObject("DynamicMemoryEnabled") = True
  16:  objManagementObject("VirtualQuantity") = intMemory
  17:  objManagementObject("Limit") = 4096
  18:  objManagementObject("TargetMemoryBuffer") = 20
  19:  objManagementObject("Weight") = 5000
  20:  objManagementObject("Reservation") = intMemory
  21:  strMemorySettingData = objManagementObject.GetText(TextFormat.CimDtd20)
  22:  Next
  23:   
  24:  ForEachobjVirtualSystemManagementService AsManagementObject   InNewManagementObjectSearcher(objManagementScope, NewObjectQuery   ("SELECT * FROM Msvm_VirtualSystemManagementService")).Get
  25:  DimobjParams AsManagementBaseObject =  objVirtualSystemManagementService.GetMethodParameters  ("ModifyVirtualSystemResources")
  26:  objParams("ResourceSettingData") = NewString() {strMemorySettingData}
  27:  objParams("ComputerSystem") = objComputerSystem.Path.Path
  28:  DimobjManagementBaseObject AsManagementBaseObject =   objVirtualSystemManagementService.InvokeMethod  ("ModifyVirtualSystemResources", objParams, Nothing)
  29:  ReturnJobComplete(objManagementBaseObject, objManagementScope)
  30:  Next
  31:  EndFunction


    1〜13行目:
    StaticMemoryの設定と同様です。

    15行目:
    DynamicMemoryEnabled このプロパティーをTrueにすることでDyanimcMemoryが使用可能となります。同様のコードで静的メモリを設定したい場合は、DynamicMemoryEnabled をFlaseにすることで、下述のVirtualQuantityの設定値が静的メモリ容量となります。

    16〜20行目:
    以前、DynamicMemoryの「Reservation」の設定方法の回でもふれましたが、各設定項目は以下のような意味となっています。

    VirtualQuantityスタートアップRAM
    Reservation最低RAM
    Limit最大RAM
    TargetMemoryBufferメモリバッファー(5 〜2000%)
    Weightメモリの優先度(0 〜10000)

    http://msdn.microsoft.com/en-us/library/cc136856(v=vs.85).aspx



また、Msvm_VirtualSystemSettingDataオブジェクトを取得した場合に、各プロパティーに対して値を設定する人も、現在設定中のプロパティー値を取得することができます。

例えば、Msgbox(objManagementObject("VirtualQuantity")) と、してみると現在設定されているメモリ容量が表示されます。

Reservationの詳細については、こちら(サービス開発者から見たWindows Server 2008 R2 Service Pack 1 - Vol.2 - Dynamic Memoryの効果)を参考にしていただければと思います。

今回のサンプルでは、最大メモリを4GB、最低メモリを1GBとして設定しています。このコードによってDynemicMemoryの設定が可能となります。





プロセッサーの設定

次に、プロセッサーの設定を行なってみましょう。(参照:コード03)

[ コード03 ]
   1:  FunctionSetProcessor(ByValobjManagementScope AsManagementScope,   ByValstrVMName AsString, ByValintProcessor AsInteger) AsBoolean
   2:  DimobjComputerSystem AsManagementObject = Nothing
   3:  ForEachobjManagementObject AsManagementObject   InNewManagementObjectSearcher(objManagementScope, NewObjectQuery  ("SELECT * FROM Msvm_ComputerSystem WHERE ElementName = '"  & strVMName & "'")).Get
   4:  objComputerSystem = objManagementObject
   5:  Next
   6:   
   7:  DimobjVirtualSystemsettingData AsManagementObject = Nothing
   8:  ForEachobjManagementObject AsManagementObject   InobjComputerSystem.GetRelated("Msvm_VirtualSystemsettingData")
   9:  objVirtualSystemsettingData = objManagementObject
  10:  Next
  11:   
  12:  DimobjProcessorSettingData AsManagementObject = Nothing
  13:  ForEachobjManagementObject AsManagementObject   InobjVirtualSystemsettingData.GetRelated("Msvm_ProcessorSettingData")
  14:  objProcessorSettingData = objManagementObject
  15:  objProcessorSettingData("VirtualQuantity") = intProcessor
  16:  Next
  17:   
  18:  ForEachobjVirtualSystemManagementService AsManagementObject   InNewManagementObjectSearcher(objManagementScope, NewObjectQuery  ("SELECT * FROM Msvm_VirtualSystemManagementService")).Get
  19:  DimobjParams AsManagementBaseObject =   objVirtualSystemManagementService.GetMethodParameters  ("ModifyVirtualSystemResources")
  20:  DimstrResourceSettingData AsString() = NewString(0) {}
  21:  strResourceSettingData(0) = objProcessorSettingData.GetText  (TextFormat.CimDtd20)
  22:  objParams("ResourceSettingData") = strResourceSettingData
  23:  objParams("ComputerSystem") = objComputerSystem.Path.Path
  24:  DimobjManagementBaseObject AsManagementBaseObject =   objVirtualSystemManagementService.InvokeMethod   ("ModifyVirtualSystemResources", objParams, Nothing)
  25:  ReturnJobComplete(objManagementBaseObject, objManagementScope)
  26:  Next
  27:  EndFunction


    1行目:
    引数として、ManagementScope オブジェクト、仮想マシン名、CPUの数を渡します。

    2〜10行目:
    ここまではいつもの通り、Msvm_ComputerSystemオブジェクトと、Msvm_VirtualSystemsettingDataオブジェクトを取得しておきます。

    13行目:
    CPUの設定を行うには、Msvm_ProcessorSettingDataクラスからオブジェクトを取得して行ないます。

    15行目:
    VirtualQuantityがCPUの数を設定するプロパティーとなります。
    今回は2個のCPUを搭載します。

    18〜26行目:
    メモリの設定同様、ModifyVirtualSystemResourcesメソッドを使用して変更し、JobCompleteを使用してメソッドの実行を完了します。CPUが2つ搭載されていれば成功です。



以上、今回はStaticMemory、DynamicMemory、CPUの設定方法についてご紹介しました。
次回も仮想マシンに対しての各パーツの設定方法についてご紹介する予定です。



コメント

コメントの投稿


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

トラックバック

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

FC2Ad

FC2ブログ