diff --git a/pkg/machine/applehv/stubber.go b/pkg/machine/applehv/stubber.go index 3ebaf1eaa4..fd34507c11 100644 --- a/pkg/machine/applehv/stubber.go +++ b/pkg/machine/applehv/stubber.go @@ -74,9 +74,9 @@ func (a AppleHVStubber) CreateVM(opts define.CreateVMOpts, mc *vmconfigs.Machine return resizeDisk(mc, strongunits.GiB(mc.Resources.DiskSize)) } -func (a AppleHVStubber) GetHyperVisorVMs() ([]string, error) { +func (a AppleHVStubber) Exists(name string) (bool, error) { // not applicable for applehv - return nil, nil + return false, nil } func (a AppleHVStubber) MountType() vmconfigs.VolumeMountType { diff --git a/pkg/machine/hyperv/stubber.go b/pkg/machine/hyperv/stubber.go index 080d78c039..506a416b6d 100644 --- a/pkg/machine/hyperv/stubber.go +++ b/pkg/machine/hyperv/stubber.go @@ -123,19 +123,10 @@ func (h HyperVStubber) CreateVM(opts define.CreateVMOpts, mc *vmconfigs.MachineC return err } -func (h HyperVStubber) GetHyperVisorVMs() ([]string, error) { - var ( - vmNames []string - ) +func (h HyperVStubber) Exists(name string) (bool, error) { vmm := hypervctl.NewVirtualMachineManager() - vms, err := vmm.GetAll() - if err != nil { - return nil, err - } - for _, vm := range vms { - vmNames = append(vmNames, vm.ElementName) // Note: elementname is human-readable name - } - return vmNames, nil + exists, _, err := vmm.GetMachineExists(name) + return exists, err } func (h HyperVStubber) MountType() vmconfigs.VolumeMountType { diff --git a/pkg/machine/qemu/stubber.go b/pkg/machine/qemu/stubber.go index 28614246bb..d5b02cdcf9 100644 --- a/pkg/machine/qemu/stubber.go +++ b/pkg/machine/qemu/stubber.go @@ -218,8 +218,8 @@ func waitForReady(readySocket *define.VMFile, pid int, stdErrBuffer *bytes.Buffe return err } -func (q *QEMUStubber) GetHyperVisorVMs() ([]string, error) { - return nil, nil +func (q *QEMUStubber) Exists(name string) (bool, error) { + return false, nil } func (q *QEMUStubber) VMType() define.VMType { diff --git a/pkg/machine/shim/host.go b/pkg/machine/shim/host.go index 7c4d470416..ea62c5d90b 100644 --- a/pkg/machine/shim/host.go +++ b/pkg/machine/shim/host.go @@ -7,7 +7,6 @@ import ( "runtime" "time" - "github.com/containers/common/pkg/util" "github.com/containers/podman/v5/pkg/machine" "github.com/containers/podman/v5/pkg/machine/connection" machineDefine "github.com/containers/podman/v5/pkg/machine/define" @@ -243,11 +242,11 @@ func VMExists(name string, vmstubbers []vmconfigs.VMProvider) (*vmconfigs.Machin } // Check with the provider hypervisor for _, vmstubber := range vmstubbers { - vms, err := vmstubber.GetHyperVisorVMs() + exists, err := vmstubber.Exists(name) if err != nil { return nil, false, err } - if util.StringInSlice(name, vms) { //nolint:staticcheck + if exists { return nil, true, fmt.Errorf("vm %q already exists on hypervisor", name) } } diff --git a/pkg/machine/vmconfigs/config.go b/pkg/machine/vmconfigs/config.go index 149ec43958..0de2f34cdb 100644 --- a/pkg/machine/vmconfigs/config.go +++ b/pkg/machine/vmconfigs/config.go @@ -114,7 +114,7 @@ type VMProvider interface { //nolint:interfacebloat // Let's deprecate this ASAP GetDisk(userInputPath string, dirs *define.MachineDirs, mc *MachineConfig) error PrepareIgnition(mc *MachineConfig, ignBuilder *ignition.IgnitionBuilder) (*ignition.ReadyUnitOpts, error) - GetHyperVisorVMs() ([]string, error) + Exists(name string) (bool, error) MountType() VolumeMountType MountVolumesToVM(mc *MachineConfig, quiet bool) error Remove(mc *MachineConfig) ([]string, func() error, error) diff --git a/pkg/machine/wsl/stubber.go b/pkg/machine/wsl/stubber.go index a3693d9c99..92fb7c94ea 100644 --- a/pkg/machine/wsl/stubber.go +++ b/pkg/machine/wsl/stubber.go @@ -94,16 +94,8 @@ func (w WSLStubber) PrepareIgnition(_ *vmconfigs.MachineConfig, _ *ignition.Igni return nil, nil } -func (w WSLStubber) GetHyperVisorVMs() ([]string, error) { - vms, err := getAllWSLDistros(false) - if err != nil { - return nil, err - } - wslVMs := make([]string, 0) - for name := range vms { - wslVMs = append(wslVMs, name) - } - return wslVMs, nil +func (w WSLStubber) Exists(name string) (bool, error) { + return isWSLExist(machine.ToDist(name)) } func (w WSLStubber) MountType() vmconfigs.VolumeMountType {