Merge pull request #5183 from vvchik/feature/hyperv-memory-and-cpu-management

Added possibility to configure memory, cpus count, and vmname in vagrant...
This commit is contained in:
Seth Vargo 2015-01-14 11:53:44 -05:00
commit 507c0d65a5
5 changed files with 105 additions and 12 deletions

View File

@ -14,6 +14,15 @@ module VagrantPlugins
def call(env) def call(env)
vm_dir = env[:machine].box.directory.join("Virtual Machines") vm_dir = env[:machine].box.directory.join("Virtual Machines")
hd_dir = env[:machine].box.directory.join("Virtual Hard Disks") hd_dir = env[:machine].box.directory.join("Virtual Hard Disks")
memory = env[:machine].provider_config.memory
maxmemory = env[:machine].provider_config.maxmemory
cpus = env[:machine].provider_config.cpus
vmname = env[:machine].provider_config.vmname
env[:ui].output("Configured Dynamical memory allocation, maxmemory is #{maxmemory}") if maxmemory
env[:ui].output("Configured startup memory is #{memory}") if memory
env[:ui].output("Configured cpus number is #{cpus}") if cpus
env[:ui].output("Configured vmname is #{vmname}") if vmname
if !vm_dir.directory? || !hd_dir.directory? if !vm_dir.directory? || !hd_dir.directory?
raise Errors::BoxInvalid raise Errors::BoxInvalid
@ -78,6 +87,10 @@ module VagrantPlugins
image_path: image_path.to_s.gsub("/", "\\") image_path: image_path.to_s.gsub("/", "\\")
} }
options[:switchname] = switch if switch options[:switchname] = switch if switch
options[:memory] = memory if memory
options[:maxmemory] = maxmemory if maxmemory
options[:cpus] = cpus if cpus
options[:vmname] = vmname if vmname
env[:ui].detail("Creating and registering the VM...") env[:ui].detail("Creating and registering the VM...")
server = env[:machine].provider.driver.import(options) server = env[:machine].provider.driver.import(options)

View File

@ -8,15 +8,27 @@ module VagrantPlugins
# #
# @return [Integer] # @return [Integer]
attr_accessor :ip_address_timeout attr_accessor :ip_address_timeout
attr_accessor :memory
attr_accessor :maxmemory
attr_accessor :cpus
attr_accessor :vmname
def initialize def initialize
@ip_address_timeout = UNSET_VALUE @ip_address_timeout = UNSET_VALUE
@memory = UNSET_VALUE
@maxmemory = UNSET_VALUE
@cpus = UNSET_VALUE
@vmname = UNSET_VALUE
end end
def finalize! def finalize!
if @ip_address_timeout == UNSET_VALUE if @ip_address_timeout == UNSET_VALUE
@ip_address_timeout = 120 @ip_address_timeout = 120
end end
@memory = nil if @memory == UNSET_VALUE
@maxmemory = nil if @maxmemory == UNSET_VALUE
@cpus = nil if @cpus == UNSET_VALUE
@vmname = nil if @vmname == UNSET_VALUE
end end
def validate(machine) def validate(machine)

View File

@ -4,7 +4,11 @@ Param(
[Parameter(Mandatory=$true)] [Parameter(Mandatory=$true)]
[string]$image_path, [string]$image_path,
[string]$switchname=$null [string]$switchname=$null,
[string]$memory=$null,
[string]$maxmemory=$null,
[string]$cpus=$null,
[string]$vmname=$null
) )
# Include the following modules # Include the following modules
@ -13,10 +17,22 @@ $Dir = Split-Path $script:MyInvocation.MyCommand.Path
[xml]$vmconfig = Get-Content -Path $vm_xml_config [xml]$vmconfig = Get-Content -Path $vm_xml_config
$vm_name = $vmconfig.configuration.properties.name.'#text'
$processors = $vmconfig.configuration.settings.processors.count.'#text'
$generation = [int]($vmconfig.configuration.properties.subtype.'#text')+1 $generation = [int]($vmconfig.configuration.properties.subtype.'#text')+1
if (!$vmname) {
# Get the name of the vm
$vm_name = $vmconfig.configuration.properties.name.'#text'
}else {
$vm_name = $vmname
}
if (!$cpus) {
# Get the name of the vm
$processors = $vmconfig.configuration.settings.processors.count.'#text'
}else {
$processors = $cpus
}
function GetUniqueName($name) { function GetUniqueName($name) {
Get-VM | ForEach-Object -Process { Get-VM | ForEach-Object -Process {
if ($name -eq $_.Name) { if ($name -eq $_.Name) {
@ -31,18 +47,34 @@ do {
$vm_name = GetUniqueName $name $vm_name = GetUniqueName $name
} while ($vm_name -ne $name) } while ($vm_name -ne $name)
$memory = (Select-Xml -xml $vmconfig -XPath "//memory").node.Bank if (!$memory) {
if ($memory.dynamic_memory_enabled."#text" -eq "True") { $xmlmemory = (Select-Xml -xml $vmconfig -XPath "//memory").node.Bank
$dynamicmemory = $True if ($xmlmemory.dynamic_memory_enabled."#text" -eq "True") {
$dynamicmemory = $True
}
else {
$dynamicmemory = $False
}
# Memory values need to be in bytes
$MemoryMaximumBytes = ($xmlmemory.limit."#text" -as [int]) * 1MB
$MemoryStartupBytes = ($xmlmemory.size."#text" -as [int]) * 1MB
$MemoryMinimumBytes = ($xmlmemory.reservation."#text" -as [int]) * 1MB
} }
else { else {
$dynamicmemory = $False if (!$maxmemory){
$dynamicmemory = $False
$MemoryMaximumBytes = ($memory -as [int]) * 1MB
$MemoryStartupBytes = ($memory -as [int]) * 1MB
$MemoryMinimumBytes = ($memory -as [int]) * 1MB
}
else {
$dynamicmemory = $True
$MemoryMaximumBytes = ($maxmemory -as [int]) * 1MB
$MemoryStartupBytes = ($memory -as [int]) * 1MB
$MemoryMinimumBytes = ($memory -as [int]) * 1MB
}
} }
# Memory values need to be in bytes
$MemoryMaximumBytes = ($memory.limit."#text" -as [int]) * 1MB
$MemoryStartupBytes = ($memory.size."#text" -as [int]) * 1MB
$MemoryMinimumBytes = ($memory.reservation."#text" -as [int]) * 1MB
if (!$switchname) { if (!$switchname) {
# Get the name of the virtual switch # Get the name of the virtual switch

View File

@ -9,10 +9,37 @@ describe VagrantPlugins::HyperV::Config do
subject.finalize! subject.finalize!
expect(subject.ip_address_timeout).to eq(180) expect(subject.ip_address_timeout).to eq(180)
end end
it "defaults to a number" do it "defaults to a number" do
subject.finalize! subject.finalize!
expect(subject.ip_address_timeout).to eq(120) expect(subject.ip_address_timeout).to eq(120)
end end
end end
describe "#vmname" do
it "can be set" do
subject.vmname = "test"
subject.finalize!
expect(subject.vmname).to eq("test")
end
end
describe "#memory" do
it "can be set" do
subject.memory = 512
subject.finalize!
expect(subject.memory).to eq(512)
end
end
describe "#maxmemory" do
it "can be set" do
subject.maxmemory = 1024
subject.finalize!
expect(subject.maxmemory).to eq(1024)
end
end
describe "#cpus" do
it "can be set" do
subject.cpus = 2
subject.finalize!
expect(subject.cpus).to eq(2)
end
end
end end

View File

@ -8,6 +8,15 @@ sidebar_current: "hyperv-configuration"
The Hyper-V provider has some provider-specific configuration options The Hyper-V provider has some provider-specific configuration options
you may set. A complete reference is shown below: you may set. A complete reference is shown below:
* `vmname` (string) - Name of virtual mashine as shown in Hyper-V manager.
Defaults is taken from box image XML.
* `cpus` (integer) - Number of virtual CPU given to mashine.
Defaults is taken from box image XML.
* `memory` (integer) - Number of MegaBytes allocated to VM at startup.
Defaults is taken from box image XML.
* `maxmemory` (integer) - Number of MegaBytes maximal allowed to allocate for VM
This parameter is switch on Dynamic Allocation of memory.
Defaults is taken from box image XML.
* `ip_address_timeout` (integer) - The time in seconds to wait for the * `ip_address_timeout` (integer) - The time in seconds to wait for the
virtual machine to report an IP address. This defaults to 120 seconds. virtual machine to report an IP address. This defaults to 120 seconds.
This may have to be increased if your VM takes longer to boot. This may have to be increased if your VM takes longer to boot.