hyperv: lookup machine on local filesystem first

when looking for a machine, look it up locally first to prevent
accidental collision with non-podman machine vms.  in the cast of
`podman machine ls`, only list podman machines found by json files

Enabled remove with force.

[NO NEW TESTS NEEDED]

Signed-off-by: Brent Baude <bbaude@redhat.com>
This commit is contained in:
Brent Baude
2023-03-22 15:29:24 -05:00
parent 67c98ec5de
commit 518eafaa34
15 changed files with 232 additions and 54 deletions

2
go.mod
View File

@ -15,7 +15,7 @@ require (
github.com/containers/common v0.51.1-0.20230316131336-0be880eaeb02
github.com/containers/conmon v2.0.20+incompatible
github.com/containers/image/v5 v5.24.3-0.20230314083015-0c6d07e02a9a
github.com/containers/libhvee v0.0.1
github.com/containers/libhvee v0.0.2
github.com/containers/ocicrypt v1.1.7
github.com/containers/psgo v1.8.0
github.com/containers/storage v1.45.5-0.20230315220505-1c6287eea927

4
go.sum
View File

@ -253,8 +253,8 @@ github.com/containers/conmon v2.0.20+incompatible h1:YbCVSFSCqFjjVwHTPINGdMX1F6J
github.com/containers/conmon v2.0.20+incompatible/go.mod h1:hgwZ2mtuDrppv78a/cOBNiCm6O0UMWGx1mu7P00nu5I=
github.com/containers/image/v5 v5.24.3-0.20230314083015-0c6d07e02a9a h1:2xIif78r5x2nmdb5uhjXBZuexiDAt1c/XIXFxFhfKSk=
github.com/containers/image/v5 v5.24.3-0.20230314083015-0c6d07e02a9a/go.mod h1:9PM/hiCVTh6dt8Swi7eYKXKHIaPabHn8gtFV2YD44Mk=
github.com/containers/libhvee v0.0.1 h1:QpknIQ2VG54HLNoXb0ZXdmah8lw7EC2atD5pX7543pI=
github.com/containers/libhvee v0.0.1/go.mod h1:bV1MfbuXk/ZLWHiWZpm8aePOR6iJGD1q55guYhH4CnA=
github.com/containers/libhvee v0.0.2 h1:eWtbOvpT8bD9jvksMES2yXUmEpcE0zENWkci+bbP7U8=
github.com/containers/libhvee v0.0.2/go.mod h1:bV1MfbuXk/ZLWHiWZpm8aePOR6iJGD1q55guYhH4CnA=
github.com/containers/libtrust v0.0.0-20230121012942-c1716e8a8d01 h1:Qzk5C6cYglewc+UyGf6lc8Mj2UaPTHy/iF2De0/77CA=
github.com/containers/libtrust v0.0.0-20230121012942-c1716e8a8d01/go.mod h1:9rfv8iPl1ZP7aqh9YA68wnZv2NUDbXdcdPHVz0pFbPY=
github.com/containers/ocicrypt v1.0.1/go.mod h1:MeJDzk1RJHv89LjsH0Sp5KTY3ZYkjXO/C+bKAeWFIrc=

View File

@ -70,16 +70,16 @@ func (v Virtualization) IsValidVMName(name string) (bool, error) {
}
func (v Virtualization) List(opts machine.ListOptions) ([]*machine.ListResponse, error) {
var response []*machine.ListResponse
vmm := hypervctl.NewVirtualMachineManager()
vms, err := vmm.GetAll()
mms, err := v.loadFromLocalJson()
if err != nil {
return nil, err
}
for _, vm := range vms {
m := &HyperVMachine{Name: vm.ElementName}
mm, err := m.loadFromFile()
var response []*machine.ListResponse
vmm := hypervctl.NewVirtualMachineManager()
for _, mm := range mms {
vm, err := vmm.GetMachine(mm.Name)
if err != nil {
return nil, err
}
@ -159,8 +159,7 @@ func (v Virtualization) NewMachine(opts machine.InitOptions) (machine.VM, error)
config := hypervctl.HardwareConfig{
CPUs: uint16(opts.CPUS),
DiskPath: imagePath.GetPath(),
// TODO remove this and make real
DiskSize: 100,
DiskSize: opts.DiskSize,
Memory: int32(opts.Memory),
}
@ -251,7 +250,7 @@ func (v Virtualization) loadFromLocalJson() ([]*HyperVMachine, error) {
if e != nil {
return e
}
if filepath.Ext(d.Name()) == "json" {
if filepath.Ext(d.Name()) == ".json" {
jsonFiles = append(jsonFiles, input)
}
return nil

View File

@ -8,7 +8,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/docker/go-units"
"io/fs"
"net/url"
"os"
"path/filepath"
@ -18,6 +18,7 @@ import (
"github.com/containers/libhvee/pkg/hypervctl"
"github.com/containers/podman/v4/pkg/machine"
"github.com/containers/storage/pkg/homedir"
"github.com/docker/go-units"
"github.com/sirupsen/logrus"
)
@ -237,12 +238,13 @@ func (m *HyperVMachine) Remove(_ string, opts machine.RemoveOptions) (string, fu
}
// In hyperv, they call running 'enabled'
if vm.State() == hypervctl.Enabled {
// TODO Add for force
// Right now, the ole response from hyperv is segv'ing
// and until it comes back clean, force cannot be implemented
// because it will always fail.
if !opts.Force {
return "", nil, hypervctl.ErrMachineStateInvalid
}
if err := vm.Stop(); err != nil {
return "", nil, err
}
}
// Collect all the files that need to be destroyed
if !opts.SaveKeys {
@ -330,25 +332,34 @@ func (m *HyperVMachine) Stop(name string, opts machine.StopOptions) error {
return vm.Stop()
}
func (m *HyperVMachine) jsonConfigPath() (string, error) {
configDir, err := machine.GetConfDir(machine.HyperVVirt)
if err != nil {
return "", err
}
return getVMConfigPath(configDir, m.Name), nil
}
func (m *HyperVMachine) loadFromFile() (*HyperVMachine, error) {
if len(m.Name) < 1 {
return nil, errors.New("encountered machine with no name")
}
jsonPath, err := m.jsonConfigPath()
if err != nil {
return nil, err
}
mm := HyperVMachine{}
if err := loadMacMachineFromJSON(jsonPath, &mm); err != nil {
return nil, err
}
vmm := hypervctl.NewVirtualMachineManager()
vm, err := vmm.GetMachine(m.Name)
if err != nil {
return nil, err
}
configDir, err := machine.GetConfDir(machine.HyperVVirt)
if err != nil {
return nil, err
}
jsonPath := getVMConfigPath(configDir, m.Name)
mm := HyperVMachine{}
if err := loadMacMachineFromJSON(jsonPath, &mm); err != nil {
return nil, err
}
cfg, err := vm.GetConfig(mm.ImagePath.GetPath())
if err != nil {
return nil, err
@ -378,6 +389,9 @@ func getVMConfigPath(configDir, vmName string) string {
func loadMacMachineFromJSON(fqConfigPath string, macMachine *HyperVMachine) error {
b, err := os.ReadFile(fqConfigPath)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("%q: %w", fqConfigPath, machine.ErrNoSuchVM)
}
return err
}
return json.Unmarshal(b, macMachine)

View File

@ -68,7 +68,6 @@ const (
ErrShutdownInProgress = 32782
)
type shutdownCompError struct {
errorCode int
message string
@ -115,3 +114,44 @@ func translateShutdownError(code int) error {
return &shutdownCompError{code, message}
}
// Modify resource errors
const (
ErrModifyResourceNotSupported = 1
ErrModifyResourceFailed = 2
ErrModifyResourceTimeout = 3
ErrModifyResourceInvalidParameter = 4
ErrModifyResourceInvalidState = 5
ErrModifyResourceIncompatParam = 6
)
type modifyResourceError struct {
errorCode int
message string
}
func (m *modifyResourceError) Error() string {
return fmt.Sprintf("%s (%d)", m.message, m.errorCode)
}
func translateModifyError(code int) error {
var message string
switch code {
case ErrModifyResourceNotSupported:
message = "virtual machine does not support modification operations"
case ErrModifyResourceFailed:
message = "resource modification failed"
case ErrModifyResourceTimeout:
message = "timeout modifying resource"
case ErrModifyResourceInvalidParameter:
message = "a modify resource operation was passed an invalid parameter"
case ErrModifyResourceInvalidState:
message = "the requested modification could not be applied due to an invalid state"
case ErrModifyResourceIncompatParam:
message = "an incompatible parameter was passed to a modify resource operation"
default:
message = "unknown error"
}
return &modifyResourceError{code, message}
}

View File

@ -3,6 +3,8 @@
package hypervctl
import "fmt"
const MemoryResourceType = "Microsoft:Hyper-V:Memory"
type MemorySettings struct {
@ -41,7 +43,11 @@ type MemorySettings struct {
}
func createMemorySettings(settings *MemorySettings) (string, error) {
return createResourceSettingGeneric(settings, MemoryResourceType)
str, err := createResourceSettingGeneric(settings, MemoryResourceType)
if err != nil {
err = fmt.Errorf("could not create memory settings: %w", err)
}
return str, err
}
func fetchDefaultMemorySettings() (*MemorySettings, error) {

View File

@ -3,6 +3,8 @@
package hypervctl
import "fmt"
const ProcessorResourceType = "Microsoft:Hyper-V:Processor"
/*
@ -88,5 +90,9 @@ func fetchDefaultProcessorSettings() (*ProcessorSettings, error) {
}
func createProcessorSettings(settings *ProcessorSettings) (string, error) {
return createResourceSettingGeneric(settings, ProcessorResourceType)
str, err := createResourceSettingGeneric(settings, ProcessorResourceType)
if err != nil {
err = fmt.Errorf("could not create processor settings: %w", err)
}
return str, err
}

View File

@ -46,7 +46,6 @@ const (
SummaryRequestOtherEnabledState = 132
)
type SummaryRequestSet []uint
var (

View File

@ -174,7 +174,7 @@ func addResource(service *wmiext.Service, systemSettingPath string, resourceSett
return "", fmt.Errorf("AddResourceSettings failed: %w", err)
}
err = waitVMResult(res, service, job)
err = waitVMResult(res, service, job, "failed to add resource", nil)
if len(resultingSettings) > 0 {
return resultingSettings[0], err

View File

@ -139,9 +139,9 @@ func (builder *SystemSettingsBuilder) Build() (*SystemSettings, error) {
return nil, fmt.Errorf("failed to define system: %w", err)
}
err = waitVMResult(res, service, job)
err = waitVMResult(res, service, job, "failed to define system", nil)
if err != nil {
return nil, fmt.Errorf("failed to define system: %w", err)
return nil, err
}
newSettings, err := service.FindFirstRelatedInstance(resultingSystem, "Msvm_VirtualSystemSettingData")

View File

@ -183,18 +183,27 @@ func (vm *VirtualMachine) kvpOperation(op string, key string, value string, ille
return err
}
func waitVMResult(res int32, service *wmiext.Service, job *wmiext.Instance) error {
func waitVMResult(res int32, service *wmiext.Service, job *wmiext.Instance, errorMsg string, translate func(int) error) error {
var err error
if res == 4096 {
switch res {
case 0:
return nil
case 4096:
err = wmiext.WaitJob(service, job)
defer job.Close()
default:
if translate != nil {
return translate(int(res))
}
return fmt.Errorf("%s (result code %d)", errorMsg, res)
}
if err != nil {
desc, _ := job.GetAsString("ErrorDescription")
desc = strings.Replace(desc, "\n", " ", -1)
return fmt.Errorf("failed to define system: %w (%s)", err, desc)
return fmt.Errorf("%s: %w (%s)", errorMsg, err, desc)
}
return err
@ -270,7 +279,7 @@ func (vm *VirtualMachine) Start() error {
Out("ReturnValue", &res).End(); err != nil {
return err
}
return waitVMResult(res, srv, job)
return waitVMResult(res, srv, job, "failed to start vm", nil)
}
func getService(_ *wmiext.Service) (*wmiext.Service, error) {
@ -328,7 +337,7 @@ func (vm *VirtualMachine) GetSummaryInformation(requestedFields SummaryRequestSe
}
defer service.Close()
instance, err := service.FindFirstRelatedInstance(vm.Path(), "Msvm_VirtualSystemSettingData")
instance, err := vm.fetchSystemSettingsInstance(service)
if err != nil {
return nil, err
}
@ -418,6 +427,98 @@ func (vmm *VirtualMachineManager) NewVirtualMachine(name string, config *Hardwar
return nil
}
func (vm *VirtualMachine) fetchSystemSettingsInstance(service *wmiext.Service) (*wmiext.Instance, error) {
// When a settings snapshot is taken there are multiple associations, use only the realized/active version
return service.FindFirstRelatedInstanceThrough(vm.Path(), "Msvm_VirtualSystemSettingData", "Msvm_SettingsDefineState")
}
func (vm *VirtualMachine) fetchExistingResourceSettings(service *wmiext.Service, resourceType string, resourceSettings interface{}) error {
const errFmt = "could not fetch resource settings (%s): %w"
// When a settings snapshot is taken there are multiple associations, use only the realized/active version
instance, err := vm.fetchSystemSettingsInstance(service)
if err != nil {
return fmt.Errorf(errFmt, resourceType, err)
}
defer instance.Close()
path, err := instance.Path()
if err != nil {
return fmt.Errorf(errFmt, resourceType, err)
}
return service.FindFirstRelatedObject(path, resourceType, resourceSettings)
}
// Update processor and/or mem
func (vm *VirtualMachine) UpdateProcessorMemSettings(updateProcessor func(*ProcessorSettings), updateMemory func(*MemorySettings)) error {
service, err := wmiext.NewLocalService(HyperVNamespace)
if err != nil {
return err
}
defer service.Close()
proc := &ProcessorSettings{}
mem := &MemorySettings{}
var settings []string
if updateProcessor != nil {
err = vm.fetchExistingResourceSettings(service, "Msvm_ProcessorSettingData", proc)
if err != nil {
return err
}
updateProcessor(proc)
processorStr, err := createProcessorSettings(proc)
if err != nil {
return err
}
settings = append(settings, processorStr)
}
if updateMemory != nil {
err = vm.fetchExistingResourceSettings(service, "Msvm_MemorySettingData", mem)
if err != nil {
return err
}
updateMemory(mem)
memStr, err := createMemorySettings(mem)
if err != nil {
return err
}
settings = append(settings, memStr)
}
if len(settings) < 1 {
return nil
}
vsms, err := service.GetSingletonInstance("Msvm_VirtualSystemManagementService")
if err != nil {
return err
}
defer vsms.Close()
var job *wmiext.Instance
var res int32
err = vsms.BeginInvoke("ModifyResourceSettings").
In("ResourceSettings", settings).
Execute().
Out("Job", &job).
Out("ReturnValue", &res).
End()
if err != nil {
return fmt.Errorf("failed to modify resource settings: %w", err)
}
return waitVMResult(res, service, job, "failed to modify resource settings", translateModifyError)
}
func (vm *VirtualMachine) remove() (int32, error) {
var (
err error
@ -433,7 +534,7 @@ func (vm *VirtualMachine) remove() (int32, error) {
return -1, err
}
wmiInst, err := srv.FindFirstRelatedInstance(vm.Path(), "Msvm_VirtualSystemSettingData")
wmiInst, err := vm.fetchSystemSettingsInstance(srv)
if err != nil {
return -1, err
}
@ -448,7 +549,7 @@ func (vm *VirtualMachine) remove() (int32, error) {
if err != nil {
return -1, err
}
defer wmiInst.Close()
defer vsms.Close()
var (
job *wmiext.Instance
@ -465,7 +566,7 @@ func (vm *VirtualMachine) remove() (int32, error) {
}
// do i have this correct? you can get an error without a result?
if err := waitVMResult(res, srv, job); err != nil {
if err := waitVMResult(res, srv, job, "failed to remove vm", nil); err != nil {
return -1, err
}
return res, nil

View File

@ -138,7 +138,7 @@ func (*VirtualMachineManager) CreateVhdxFile(path string, maxSize uint64) error
return fmt.Errorf("failed to create vhdx: %w", err)
}
return waitVMResult(ret, service, job)
return waitVMResult(ret, service, job, "failed to create vhdx", nil)
}
// GetSummaryInformation returns the live VM summary information for all virtual machines.

View File

@ -17,7 +17,7 @@ import (
)
var (
unixEpoch = time.Unix(0,0)
unixEpoch = time.Unix(0, 0)
zeroTime = time.Time{}
)
@ -367,7 +367,7 @@ func convertDurationToDateTime(duration time.Duration) ole.VARIANT {
micros := duration / time.Microsecond
s:=fmt.Sprintf("%08d%02d%02d%02d.%06d:000", days, hours, mins, seconds, micros)
s := fmt.Sprintf("%08d%02d%02d%02d.%06d:000", days, hours, mins, seconds, micros)
return ole.NewVariant(ole.VT_BSTR, int64(uintptr(unsafe.Pointer(ole.SysAllocStringLen(s)))))
}
@ -440,7 +440,7 @@ func parseIntervalTime(interval string) (time.Time, error) {
stamp += hours * 3600
stamp += mins * 60
return time.Unix(int64(stamp), int64(micros * 1000)), nil
return time.Unix(int64(stamp), int64(micros*1000)), nil
}
func convertIntervalToDuration(variant *ole.VARIANT) (time.Duration, error) {
@ -465,7 +465,6 @@ func parseUintChain(str string, err error) (uint64, error) {
return strconv.ParseUint(str, 10, 0)
}
func abs(num int) int {
if num < 0 {
return -num

View File

@ -306,6 +306,20 @@ func (s *Service) FindFirstRelatedInstance(objPath string, className string) (*I
return s.FindFirstInstance(wql)
}
// FindFirstRelatedInstanceThrough finds and returns a related associator of the specified WMI object path of the
// expected className type, and only through the expected association type.
func (s *Service) FindFirstRelatedInstanceThrough(objPath string, resultClass string, assocClass string) (*Instance, error) {
wql := fmt.Sprintf("ASSOCIATORS OF {%s} WHERE AssocClass = %s ResultClass = %s ", objPath, assocClass, resultClass)
return s.FindFirstInstance(wql)
}
// FindFirstRelatedObject finds and returns a related associator of the specified WMI object path of the
// expected className type, and populates the passed in struct with its fields
func (s *Service) FindFirstRelatedObject(objPath string, className string, target interface{}) error {
wql := fmt.Sprintf("ASSOCIATORS OF {%s} WHERE ResultClass = %s", objPath, className)
return s.FindFirstObject(wql, target)
}
// FindFirstObject finds and returns the first WMI Instance in the result set for a WSL query, and
// populates the specified struct pointer passed in through the target parameter.
func (s *Service) FindFirstObject(wql string, target interface{}) error {

2
vendor/modules.txt vendored
View File

@ -249,7 +249,7 @@ github.com/containers/image/v5/transports
github.com/containers/image/v5/transports/alltransports
github.com/containers/image/v5/types
github.com/containers/image/v5/version
# github.com/containers/libhvee v0.0.1
# github.com/containers/libhvee v0.0.2
## explicit; go 1.18
github.com/containers/libhvee/pkg/hypervctl
github.com/containers/libhvee/pkg/kvp/ginsu