次の方法で共有


Memory Configuration Utilizing The Hyper-V WMI v2 Namespace

I have gotten a few questions specifically regarding how to configure dynamic memory utilizing the root\virtualization\v2 namespace.  At face value it’s pretty simple you retrieve the Msvm_MemorySettingsData set the DynamicMemoryEnabled property to true, configure the startup (virtual quantity), minimum (reservation) and maximum (limit) properties and execute the ModifyResourceSettings method.  However if you just do those steps it will almost always fail – looking at the error it generally we be “Dynamic memory and virtual NUMA cannot be enabled on the virtual machine '<vm name>' because the features are mutually exclusive.”.  In the Hyper-V UI as well as PowerShell we take care of disabling and enabling virtual NUMA automatically when we disable or enable dynamic memory – your code should do the same.  Below is an example:

 $vmName = "Template"

 #Retrieve the Hyper-V Management Service, ComputerSystem class for the VM and the VM’s SettingData class. 
$Msvm_VirtualSystemManagementService = Get-WmiObject -Namespace root\virtualization\v2 `
      -Class Msvm_VirtualSystemManagementService 

$Msvm_ComputerSystem = Get-WmiObject -Namespace root\virtualization\v2 `
      -Class Msvm_ComputerSystem -Filter "ElementName='$vmName'" 

$Msvm_VirtualSystemSettingData = ($Msvm_ComputerSystem.GetRelated("Msvm_VirtualSystemSettingData", `
     "Msvm_SettingsDefineState", `
      $null, `
      $null, ` 
     "SettingData", `
     "ManagedElement", `
      $false, $null) | % {$_}) 

#Retrieve the Msvm_MemorySettingData Associated to the VM. 
$Msvm_MemorySettingData = ($Msvm_VirtualSystemSettingData.GetRelated( "Msvm_MemorySettingData") | % {$_})

#Enable Dynamic Memory
$Msvm_MemorySettingData.DynamicMemoryEnabled = $true

#Dynamic Memory and Virtual NUMA can not be enabled at the same time
if ($Msvm_MemorySettingData.DynamicMemoryEnabled)
{
    $Msvm_VirtualSystemSettingData.VirtualNumaEnabled = $false

    #Reservation is equavilant to minimum memory
    $Msvm_MemorySettingData.Reservation = 1024

    #VirtualQuantity is equavilant to startup memory
    $Msvm_MemorySettingData.VirtualQuantity = 2048

    #Limit is equivalnt to Maximum Memory
    $Msvm_MemorySettingData.Limit = 8096
}
else
{
    $Msvm_VirtualSystemSettingData.VirtualNumaEnabled = $true
    
    #VirtualQuantity is the static allocation amount
    $Msvm_MemorySettingData.VirtualQuantity = 4096
}

#First apply the Virtual NUMA changes then Apply The Memory Changes
$Msvm_VirtualSystemManagementService.ModifySystemSettings($Msvm_VirtualSystemSettingData.GetText(2))
$Msvm_VirtualSystemManagementService.ModifyResourceSettings($Msvm_MemorySettingData.GetText(2))

-Taylor Brown

-Program Manager, Hyper-V

Comments

  • Anonymous
    March 03, 2014
    Hi Taylon, you have sample using C#?

  • Anonymous
    March 03, 2014
    Bruno, We have official samples code using c# at the following link: code.msdn.microsoft.com/.../Hyper-V-dynamic-memory-9b0b1d05 -Taylor

  • Anonymous
    November 26, 2015
    Those samples mentioned above have nothing to do with allocating dynamic memory to virtual machines. The above samples modify Swap and Page files.