Correct VM existance check on WSL

Replaces GetHyperVisorVMs() with Exists() to better abstract the underlying
use-case and slightly imrpove efficiency.

Signed-off-by: Jason T. Greene <jason.greene@redhat.com>
This commit is contained in:
Jason T. Greene 2024-02-13 21:37:56 -06:00
parent 07779e09f6
commit d23dd35dc1
6 changed files with 12 additions and 30 deletions

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -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)
}
}

View File

@ -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)

View File

@ -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 {