Skip to content

Commit

Permalink
Merge pull request #34 from madhanrm/memproc
Browse files Browse the repository at this point in the history
* Expose Memory/Processor changes to Virtual Machine
  • Loading branch information
madhanrm authored Jul 25, 2020
2 parents a786c0a + 886496a commit 5ca6d57
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 2 deletions.
26 changes: 26 additions & 0 deletions pkg/virtualization/core/memory/memorysettingdata.go
Original file line number Diff line number Diff line change
@@ -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)
}
26 changes: 26 additions & 0 deletions pkg/virtualization/core/processor/processorsettingdata.go
Original file line number Diff line number Diff line change
@@ -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)
}
32 changes: 32 additions & 0 deletions pkg/virtualization/core/service/virtualmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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())
Expand Down
23 changes: 23 additions & 0 deletions pkg/virtualization/core/virtualsystem/virtualmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

}
20 changes: 20 additions & 0 deletions pkg/virtualization/core/virtualsystem/virtualsystemsettingdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

0 comments on commit 5ca6d57

Please sign in to comment.