Skip to content

Commit

Permalink
Merge pull request #115 from microsoft/user/sgolshani/remove_HID_devices
Browse files Browse the repository at this point in the history
Changes to support new VM Security Type
  • Loading branch information
shayan-msft authored Aug 13, 2024
2 parents b7dcec5 + 77aabd5 commit a3717c7
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 1 deletion.
8 changes: 8 additions & 0 deletions pkg/virtualization/constant/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@ const (
LinuxSecureBootTemplateGUID SecureBootTemplateGUID = "272e7447-90a4-4563-a4b9-8e4ab00526ce"
OSSSecureBootTemplateGUID SecureBootTemplateGUID = "4292ae2b-ee2c-42b5-a969-dd8f8689f6f3"
)

type ResourceSubType string

const (
SyntheticMouseSubtype ResourceSubType = "Microsoft:Hyper-V:Synthetic Mouse"
SyntheticKeyboardSubtype ResourceSubType = "Microsoft:Hyper-V:Synthetic Keyboard"
SyntheticDisplaySubtype ResourceSubType = "Microsoft:Hyper-V:Synthetic Display Controller"
)
39 changes: 39 additions & 0 deletions pkg/virtualization/core/service/virtualmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/microsoft/wmi/pkg/base/query"
"github.com/microsoft/wmi/pkg/constant"
"github.com/microsoft/wmi/pkg/errors"
virtconstant "github.com/microsoft/wmi/pkg/virtualization/constant"
"github.com/microsoft/wmi/pkg/virtualization/core/memory"
"github.com/microsoft/wmi/pkg/virtualization/core/processor"
"github.com/microsoft/wmi/pkg/virtualization/core/resource/resourceallocation"
Expand Down Expand Up @@ -272,3 +273,41 @@ func (vmms *VirtualSystemManagementService) SetMemoryMB(vm *virtualsystem.Virtua
err = vmms.ModifyVirtualSystemResourceEx(mem.WmiInstance, -1)
return
}

// Re-implementation of Disable-VMConsoleSupport Cmdlet
// Removes the Synthetic Mouse, Synthetic Keyboard, and Synthetic Display devices from a VM
func (vmms *VirtualSystemManagementService) RemoveHIDDevices(vm *virtualsystem.VirtualMachine) (err error) {
// Get the RASD objects representing the devices. If there are multiple RASD for a device type, will remove the first occurence
syntheticMouse, err1 := vm.GetResourceAllocationSettingDataBySubType(virtconstant.SyntheticMouseSubtype)
if err1 == nil { // Don't error if failing to get the device. Matches behavior of Disable-VMConsoleSupport
defer syntheticMouse.Close()
err1 = vmms.RemoveVirtualSystemResource(syntheticMouse, -1)
if err1 != nil {
log.Printf("Removing synthetic mouse failed with [%v]", err1)
}
} else {
log.Printf("Getting synthetic mouse failed with [%v]", err1)
}
syntheticKeyboard, err1 := vm.GetResourceAllocationSettingDataBySubType(virtconstant.SyntheticKeyboardSubtype)
if err1 == nil {
defer syntheticKeyboard.Close()
err1 = vmms.RemoveVirtualSystemResource(syntheticKeyboard, -1)
if err1 != nil {
log.Printf("Removing synthetic keyboard failed with [%v]", err1)
}
} else {
log.Printf("Getting synthetic keyboard failed with [%v]", err1)
}
syntheticDisplay, err1 := vm.GetResourceAllocationSettingDataBySubType(virtconstant.SyntheticDisplaySubtype)
if err1 == nil {
defer syntheticDisplay.Close()
err1 = vmms.RemoveVirtualSystemResource(syntheticDisplay, -1)
if err1 != nil {
log.Printf("Removing synthetic display failed with [%v]", err1)
}
} else {
log.Printf("Getting synthetic display failed with [%v]", err1)
}

return
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/google/uuid"
"github.com/microsoft/wmi/pkg/base/host"
_ "github.com/microsoft/wmi/pkg/base/session"
virtconstant "github.com/microsoft/wmi/pkg/virtualization/constant"
"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"
Expand Down Expand Up @@ -1368,3 +1369,52 @@ func TestModifyVirtualMachineSystemSettingsGen1(t *testing.T) {
}
t.Logf("Successfully changes vm setting from %v to %v", valBefore, valAfter)
}

func TestRemoveHIDDevices(t *testing.T) {
vmms, err := GetVirtualSystemManagementService(whost)
if err != nil {
t.Fatalf("Failed [%+v]", err)
}
vm, err := vmms.GetVirtualMachineByName("test")
if err != nil {
t.Fatalf("Failed [%+v]", err)
}
defer vm.Close()
t.Logf("Found [%s] VMs", "testGen1")

// Check that HID devices are present
_, err = vm.GetResourceAllocationSettingDataBySubType(virtconstant.SyntheticDisplaySubtype)
if err != nil {
t.Fatalf("Failed [%+v]", err)
}
_, err = vm.GetResourceAllocationSettingDataBySubType(virtconstant.SyntheticKeyboardSubtype)
if err != nil {
t.Fatalf("Failed [%+v]", err)
}
_, err = vm.GetResourceAllocationSettingDataBySubType(virtconstant.SyntheticMouseSubtype)
if err != nil {
t.Fatalf("Failed [%+v]", err)
}

// Remove the HID devices
err = vmms.RemoveHIDDevices(vm)
if err != nil {
t.Fatalf("Failed [%+v]", err)
}

// Check that devices were removed
_, err = vm.GetResourceAllocationSettingDataBySubType(virtconstant.SyntheticDisplaySubtype)
if err == nil {
t.Fatalf("Synthetic display device stil exists after RemoveHIDDevices")
}
_, err = vm.GetResourceAllocationSettingDataBySubType(virtconstant.SyntheticKeyboardSubtype)
if err == nil {
t.Fatalf("Synthetic keyboard device stil exists after RemoveHIDDevices")
}
_, err = vm.GetResourceAllocationSettingDataBySubType(virtconstant.SyntheticMouseSubtype)
if err == nil {
t.Fatalf("Synthetic mouse device stil exists after RemoveHIDDevices")
}

t.Logf("Successfully removed HID devices")
}
53 changes: 53 additions & 0 deletions pkg/virtualization/core/virtualsystem/virtualmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"reflect"

virtconstant "github.com/microsoft/wmi/pkg/virtualization/constant"
"github.com/microsoft/wmi/pkg/virtualization/core/gpu"
job "github.com/microsoft/wmi/pkg/virtualization/core/job"
"github.com/microsoft/wmi/pkg/virtualization/core/memory"
Expand Down Expand Up @@ -96,6 +97,14 @@ const (
HyperVGeneration_V2 = "Microsoft:Hyper-V:SubType:2"
)

type GuestStateIsolationMode uint16

const (
Default GuestStateIsolationMode = 0
NoPersistentSecrets GuestStateIsolationMode = 1
NoManagementVtl GuestStateIsolationMode = 2
)

// NewVirtualMachine
func NewVirtualMachine(instance *wmi.WmiInstance) (*VirtualMachine, error) {
wmivm, err := v2.NewMsvm_ComputerSystemEx1(instance)
Expand Down Expand Up @@ -883,6 +892,50 @@ func (vm *VirtualMachine) GetResourceAllocationSettingData(rtype v2.ResourcePool
return
}

func (vm *VirtualMachine) GetResourceAllocationSettingDataBySubType(resourceSubType virtconstant.ResourceSubType) (col *v2.CIM_ResourceAllocationSettingData, err error) {
settings, err := vm.GetVirtualSystemSettingData()
if err != nil {
return
}
defer settings.Close()

rasdcol, err := settings.GetAllRelated("CIM_ResourceAllocationSettingData")
if err != nil {
return
}
defer rasdcol.Close()

for _, ins := range rasdcol {
rasd, err1 := v2.NewCIM_ResourceAllocationSettingDataEx1(ins)
if err1 != nil {
err = err1
return
}

sourceResourceSubType, err1 := rasd.GetProperty("ResourceSubType")
if err1 != nil || sourceResourceSubType == nil {
continue
}

if string(resourceSubType) == sourceResourceSubType {
instance, err1 := rasd.Clone()
if err1 != nil {
err = err1
return
}
col, err1 = v2.NewCIM_ResourceAllocationSettingDataEx1(instance)
if err1 != nil {
instance.Close()
err = err1
return
}
return
}
}
err = errors.Wrapf(errors.NotFound, "GetResourceAllocationSettingDataBySubType [%s] ", resourceSubType)
return
}

func (vm *VirtualMachine) GetMemory() (vmmemory *memory.MemorySettingData, err error) {
settings, err := vm.GetVirtualSystemSettingData()
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"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"
"github.com/microsoft/wmi/server2019/root/virtualization/v2"
v2 "github.com/microsoft/wmi/server2019/root/virtualization/v2"
)

type VirtualSystemSettingData struct {
Expand Down

0 comments on commit a3717c7

Please sign in to comment.