Files
podman/pkg/machine/provider/platform_unix.go
Brent Baude 5e1c2f8d7d Machine init --provider
Add the ability for users to override the default provider when creating mahcines.  The new flag is `--provider` and allows you to specifiy a valid vmtype for the platform.  This PR also removes the previous list test where we tested listing all providers.  I added a PR for testing --provider which includes a standard `machine ls` which defaults now to showing all providers.

Signed-off-by: Brent Baude <bbaude@redhat.com>
2025-10-29 07:59:34 -05:00

84 lines
2.2 KiB
Go

//go:build !windows && !darwin
package provider
import (
"errors"
"fmt"
"io/fs"
"os"
"github.com/containers/podman/v6/pkg/machine/define"
"github.com/containers/podman/v6/pkg/machine/qemu"
"github.com/containers/podman/v6/pkg/machine/vmconfigs"
"github.com/sirupsen/logrus"
"go.podman.io/common/pkg/config"
)
func Get() (vmconfigs.VMProvider, error) {
cfg, err := config.Default()
if err != nil {
return nil, err
}
provider := cfg.Machine.Provider
if providerOverride, found := os.LookupEnv("CONTAINERS_MACHINE_PROVIDER"); found {
provider = providerOverride
}
resolvedVMType, err := define.ParseVMType(provider, define.QemuVirt)
if err != nil {
return nil, err
}
logrus.Debugf("Using Podman machine with `%s` virtualization provider", resolvedVMType.String())
return GetByVMType(resolvedVMType)
}
func GetAll() []vmconfigs.VMProvider {
return []vmconfigs.VMProvider{new(qemu.QEMUStubber)}
}
// GetByVMType takes a VMType (presumably from ParseVMType) and returns the correlating
// VMProvider
func GetByVMType(resolvedVMType define.VMType) (vmconfigs.VMProvider, error) {
switch resolvedVMType {
case define.QemuVirt:
return qemu.NewStubber()
default:
}
return nil, fmt.Errorf("unsupported virtualization provider: `%s`", resolvedVMType.String())
}
// SupportedProviders returns the providers that are supported on the host operating system
func SupportedProviders() []define.VMType {
return []define.VMType{define.QemuVirt}
}
func IsInstalled(provider define.VMType) (bool, error) {
switch provider {
case define.QemuVirt:
cfg, err := config.Default()
if err != nil {
return false, err
}
if cfg == nil {
return false, fmt.Errorf("error fetching getting default config")
}
_, err = cfg.FindHelperBinary(qemu.QemuCommand, true)
if errors.Is(err, fs.ErrNotExist) {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
default:
return false, nil
}
}
// HasPermsForProvider returns whether the host operating system has the proper permissions to use the given provider
func HasPermsForProvider(provider define.VMType) bool {
// there are no permissions required for QEMU
return provider == define.QemuVirt
}