From 886496af4f1916fc9d90a7841bfe2f6954f8c8a9 Mon Sep 17 00:00:00 2001 From: Madhan Raj Mookkandy Date: Fri, 24 Jul 2020 20:20:20 -0700 Subject: [PATCH] * Expose Memory/Processor changes to Virtual Machine --- .../core/memory/memorysettingdata.go | 26 +++++++++++ .../core/processor/processorsettingdata.go | 26 +++++++++++ .../core/service/virtualmachine.go | 32 ++++++++++++++ .../virtualmachinemanagementservice_test.go | 43 ++++++++++++++++++- .../core/virtualsystem/virtualmachine.go | 23 ++++++++++ .../virtualsystem/virtualsystemsettingdata.go | 20 +++++++++ 6 files changed, 168 insertions(+), 2 deletions(-) create mode 100644 pkg/virtualization/core/memory/memorysettingdata.go create mode 100644 pkg/virtualization/core/processor/processorsettingdata.go diff --git a/pkg/virtualization/core/memory/memorysettingdata.go b/pkg/virtualization/core/memory/memorysettingdata.go new file mode 100644 index 0000000000..2ecf75e64f --- /dev/null +++ b/pkg/virtualization/core/memory/memorysettingdata.go @@ -0,0 +1,26 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +package memory + +import ( + wmi "github.com/microsoft/wmi/pkg/wmiinstance" + "github.com/microsoft/wmi/server2019/root/virtualization/v2" +) + +type MemorySettingData struct { + *v2.Msvm_MemorySettingData +} + +// NewMemorySettingData +func NewMemorySettingData(instance *wmi.WmiInstance) (*MemorySettingData, error) { + wmivm, err := v2.NewMsvm_MemorySettingDataEx1(instance) + if err != nil { + return nil, err + } + return &MemorySettingData{wmivm}, nil +} + +func (msd *MemorySettingData) SetSizeMB(size uint64) (err error) { + return msd.SetPropertyVirtualQuantity(size) +} diff --git a/pkg/virtualization/core/processor/processorsettingdata.go b/pkg/virtualization/core/processor/processorsettingdata.go new file mode 100644 index 0000000000..5b0bee35b4 --- /dev/null +++ b/pkg/virtualization/core/processor/processorsettingdata.go @@ -0,0 +1,26 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +package processor + +import ( + wmi "github.com/microsoft/wmi/pkg/wmiinstance" + "github.com/microsoft/wmi/server2019/root/virtualization/v2" +) + +type ProcessorSettingData struct { + *v2.Msvm_ProcessorSettingData +} + +// NewProcessorSettingData +func NewProcessorSettingData(instance *wmi.WmiInstance) (*ProcessorSettingData, error) { + wmivm, err := v2.NewMsvm_ProcessorSettingDataEx1(instance) + if err != nil { + return nil, err + } + return &ProcessorSettingData{wmivm}, nil +} + +func (msd *ProcessorSettingData) SetCPUCount(count uint64) (err error) { + return msd.SetPropertyVirtualQuantity(count) +} diff --git a/pkg/virtualization/core/service/virtualmachine.go b/pkg/virtualization/core/service/virtualmachine.go index 72e66c9c2d..b510967f2c 100644 --- a/pkg/virtualization/core/service/virtualmachine.go +++ b/pkg/virtualization/core/service/virtualmachine.go @@ -211,3 +211,35 @@ func (vmms *VirtualSystemManagementService) RemoveTPM(resource *resourceallocati err = vmms.RemoveVirtualSystemResource(resource.CIM_ResourceAllocationSettingData) return } + +func (vmms *VirtualSystemManagementService) SetProcessorCount(vm *virtualsystem.VirtualMachine, count uint64) (err error) { + proc, err := vm.GetProcessor() + if err != nil { + return + } + defer proc.Close() + + err = proc.SetCPUCount(count) + if err != nil { + return + } + + err = vmms.ModifyVirtualSystemResourceEx(proc.WmiInstance) + return +} + +func (vmms *VirtualSystemManagementService) SetMemoryMB(vm *virtualsystem.VirtualMachine, sizeMB uint64) (err error) { + mem, err := vm.GetMemory() + if err != nil { + return + } + defer mem.Close() + + err = mem.SetSizeMB(sizeMB) + if err != nil { + return + } + + err = vmms.ModifyVirtualSystemResourceEx(mem.WmiInstance) + return +} diff --git a/pkg/virtualization/core/service/virtualmachinemanagementservice_test.go b/pkg/virtualization/core/service/virtualmachinemanagementservice_test.go index 95a394e57f..a1b972d484 100644 --- a/pkg/virtualization/core/service/virtualmachinemanagementservice_test.go +++ b/pkg/virtualization/core/service/virtualmachinemanagementservice_test.go @@ -43,12 +43,42 @@ func TestCreateVirtualMachines(t *testing.T) { defer setting.Close() t.Logf("Create VMSettings") - _, err = vmms.CreateVirtualMachine(setting) + vm, err := vmms.CreateVirtualMachine(setting) if err != nil { t.Fatal("Failed " + err.Error()) } t.Logf("Created VM [%s]", "test") - //defer vm.Close() + defer func() { + if vm != nil { + vm.Close() + } + }() + return +} + +func TestModifyVirtualMachine(t *testing.T) { + vmms, err := GetVirtualSystemManagementService(whost) + if err != nil { + t.Fatal("Failed " + err.Error()) + } + + vm, err := vmms.GetVirtualMachineByName("test") + if err != nil { + t.Fatal("Failed " + err.Error()) + } + t.Logf("Found [%s] VMs", "test") + defer vm.Close() + + t.Logf("Setting Memory [%d] [%s] VMs", 2048, "test") + err = vmms.SetMemoryMB(vm, 2048) + if err != nil { + t.Fatalf("Failed [%+v]", err) + } + t.Logf("Setting Processor [%d] [%s] VMs", 4, "test") + err = vmms.SetProcessorCount(vm, 4) + if err != nil { + t.Fatalf("Failed [%+v]", err) + } } func TestGetVirtualMachines(t *testing.T) { @@ -312,6 +342,15 @@ func TestVirtualMachineDelete(t *testing.T) { } defer vm.Close() + err = vm.Start() + if err != nil { + t.Fatalf("Failed [%+v]", err) + } + + err = vm.Stop(true) + if err != nil { + t.Fatalf("Failed [%+v]", err) + } err = vmms.DeleteVirtualMachine(vm) if err != nil { t.Fatal("Failed " + err.Error()) diff --git a/pkg/virtualization/core/virtualsystem/virtualmachine.go b/pkg/virtualization/core/virtualsystem/virtualmachine.go index 3dc643556e..99198c83bf 100644 --- a/pkg/virtualization/core/virtualsystem/virtualmachine.go +++ b/pkg/virtualization/core/virtualsystem/virtualmachine.go @@ -14,6 +14,8 @@ import ( "github.com/microsoft/wmi/pkg/errors" "github.com/microsoft/wmi/pkg/virtualization/core/job" + "github.com/microsoft/wmi/pkg/virtualization/core/memory" + "github.com/microsoft/wmi/pkg/virtualization/core/processor" "github.com/microsoft/wmi/pkg/virtualization/core/resource" "github.com/microsoft/wmi/pkg/virtualization/core/resource/resourceallocation" "github.com/microsoft/wmi/pkg/virtualization/core/resource/resourcepool" @@ -512,3 +514,24 @@ func (vm *VirtualMachine) GetResourceAllocationSettingData(rtype v2.ResourcePool } return } + +func (vm *VirtualMachine) GetMemory() (vmmemory *memory.MemorySettingData, err error) { + settings, err := vm.GetVirtualSystemSettingData() + if err != nil { + return + } + defer settings.Close() + vmmemory, err = settings.GetMemorySetting() + return +} + +func (vm *VirtualMachine) GetProcessor() (vmprocessor *processor.ProcessorSettingData, err error) { + settings, err := vm.GetVirtualSystemSettingData() + if err != nil { + return + } + defer settings.Close() + vmprocessor, err = settings.GetProcessorSetting() + return + +} diff --git a/pkg/virtualization/core/virtualsystem/virtualsystemsettingdata.go b/pkg/virtualization/core/virtualsystem/virtualsystemsettingdata.go index 3ad3df5fc5..ac61d55e3b 100644 --- a/pkg/virtualization/core/virtualsystem/virtualsystemsettingdata.go +++ b/pkg/virtualization/core/virtualsystem/virtualsystemsettingdata.go @@ -12,6 +12,8 @@ import ( "github.com/microsoft/wmi/pkg/base/query" "github.com/microsoft/wmi/pkg/constant" "github.com/microsoft/wmi/pkg/errors" + "github.com/microsoft/wmi/pkg/virtualization/core/memory" + "github.com/microsoft/wmi/pkg/virtualization/core/processor" "github.com/microsoft/wmi/pkg/virtualization/core/storage/disk" na "github.com/microsoft/wmi/pkg/virtualization/network/virtualnetworkadapter" wmi "github.com/microsoft/wmi/pkg/wmiinstance" @@ -181,3 +183,21 @@ func (vm *VirtualSystemSettingData) getResourceAllocationSettingData(rtype v2.Re col, err = vm.GetAllRelatedWithQuery(query) return } + +func (vm *VirtualSystemSettingData) GetMemorySetting() (out *memory.MemorySettingData, err error) { + msd, err := vm.GetRelated("Msvm_MemorySettingData") + if err != nil { + return nil, err + } + + return memory.NewMemorySettingData(msd) +} + +func (vm *VirtualSystemSettingData) GetProcessorSetting() (out *processor.ProcessorSettingData, err error) { + psd, err := vm.GetRelated("Msvm_ProcessorSettingData") + if err != nil { + return nil, err + } + + return processor.NewProcessorSettingData(psd) +}