Skip to content

Commit 4f262ed

Browse files
Merge pull request #27386 from baude/initprovider
Machine init --provider
2 parents 386c8f3 + 5e1c2f8 commit 4f262ed

17 files changed

+161
-118
lines changed

cmd/podman/machine/info.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func hostInfo() (*entities.MachineHostInfo, error) {
100100
host.Arch = runtime.GOARCH
101101
host.OS = runtime.GOOS
102102

103-
dirs, err := env.GetMachineDirs(provider.VMType())
103+
dirs, err := env.GetMachineDirs(machineProvider.VMType())
104104
if err != nil {
105105
return nil, err
106106
}
@@ -126,7 +126,7 @@ func hostInfo() (*entities.MachineHostInfo, error) {
126126
host.DefaultMachine = vm.Name
127127
}
128128
// If machine is running or starting, it is automatically the current machine
129-
state, err := provider.State(vm, false)
129+
state, err := machineProvider.State(vm, false)
130130
if err != nil {
131131
return nil, err
132132
}
@@ -149,7 +149,7 @@ func hostInfo() (*entities.MachineHostInfo, error) {
149149
}
150150
}
151151

152-
host.VMType = provider.VMType().String()
152+
host.VMType = machineProvider.VMType().String()
153153

154154
host.MachineImageDir = dirs.DataDir.GetPath()
155155
host.MachineConfigDir = dirs.ConfigDir.GetPath()

cmd/podman/machine/init.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ import (
66
"errors"
77
"fmt"
88
"os"
9+
"slices"
910

1011
"github.com/containers/podman/v6/cmd/podman/registry"
1112
ldefine "github.com/containers/podman/v6/libpod/define"
1213
"github.com/containers/podman/v6/libpod/events"
1314
"github.com/containers/podman/v6/pkg/machine/define"
15+
"github.com/containers/podman/v6/pkg/machine/provider"
1416
"github.com/containers/podman/v6/pkg/machine/shim"
17+
"github.com/containers/podman/v6/pkg/machine/vmconfigs"
1518
"github.com/shirou/gopsutil/v4/mem"
1619
"github.com/sirupsen/logrus"
1720
"github.com/spf13/cobra"
@@ -36,6 +39,7 @@ var (
3639
initOptionalFlags = InitOptionalFlags{}
3740
defaultMachineName = define.DefaultMachineName
3841
now bool
42+
providerOverride string
3943
)
4044

4145
// Flags which have a meaning when unspecified that differs from the flag default
@@ -158,6 +162,10 @@ func init() {
158162

159163
flags.BoolVar(&initOptionalFlags.tlsVerify, "tls-verify", true,
160164
"Require HTTPS and verify certificates when contacting registries")
165+
166+
providerFlagName := "provider"
167+
flags.StringVar(&providerOverride, providerFlagName, "", "Override the default machine provider")
168+
_ = initCmd.RegisterFlagCompletionFunc(providerFlagName, autocompleteMachineProvider)
161169
}
162170

163171
func initMachine(cmd *cobra.Command, args []string) error {
@@ -173,6 +181,21 @@ func initMachine(cmd *cobra.Command, args []string) error {
173181
}
174182
}
175183

184+
// If the provider option was given, we need to override the
185+
// current provider
186+
if cmd.Flags().Changed("provider") {
187+
// var found bool
188+
indexFunc := func(s vmconfigs.VMProvider) bool {
189+
return s.VMType().String() == providerOverride
190+
}
191+
providers := provider.GetAll()
192+
indexVal := slices.IndexFunc(providers, indexFunc)
193+
if indexVal == -1 {
194+
return fmt.Errorf("unsupported provider %q", providerOverride)
195+
}
196+
machineProvider = providers[indexVal]
197+
}
198+
176199
// The vmtype names need to be reserved and cannot be used for podman machine names
177200
if _, err := define.ParseVMType(initOpts.Name, define.UnknownVirt); err == nil {
178201
return fmt.Errorf("cannot use %q for a machine name", initOpts.Name)
@@ -251,7 +274,7 @@ func initMachine(cmd *cobra.Command, args []string) error {
251274
// return err
252275
// }
253276

254-
err = shim.Init(initOpts, provider)
277+
err = shim.Init(initOpts, machineProvider)
255278
if err != nil {
256279
// The installation is partially complete and podman should
257280
// exit gracefully with no error and no success message.

cmd/podman/machine/inspect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func inspect(cmd *cobra.Command, args []string) error {
6060
continue
6161
}
6262

63-
dirs, err := env.GetMachineDirs(provider.VMType())
63+
dirs, err := env.GetMachineDirs(machineProvider.VMType())
6464
if err != nil {
6565
return err
6666
}

cmd/podman/machine/machine.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ var (
4141
}
4242
)
4343

44-
var (
45-
// TODO This needs to be deleted!
46-
provider vmconfigs.VMProvider
47-
)
44+
var machineProvider vmconfigs.VMProvider
4845

4946
func init() {
5047
registry.Commands = append(registry.Commands, registry.CliCommand{
@@ -54,7 +51,7 @@ func init() {
5451

5552
func machinePreRunE(c *cobra.Command, args []string) error {
5653
var err error
57-
provider, err = provider2.Get()
54+
machineProvider, err = provider2.Get()
5855
if err != nil {
5956
return err
6057
}
@@ -104,6 +101,14 @@ func autocompleteMachine(_ *cobra.Command, args []string, toComplete string) ([]
104101
return nil, cobra.ShellCompDirectiveNoFileComp
105102
}
106103

104+
func autocompleteMachineProvider(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
105+
suggestions := make([]string, 0)
106+
for _, p := range provider2.GetAll() {
107+
suggestions = append(suggestions, p.VMType().String())
108+
}
109+
return suggestions, cobra.ShellCompDirectiveNoFileComp
110+
}
111+
107112
func getMachines(toComplete string) ([]string, cobra.ShellCompDirective) {
108113
suggestions := []string{}
109114
provider, err := provider2.Get()

docs/source/markdown/podman-machine-init.1.md.in

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ Add the provided Ansible playbook to the machine and execute it after the first
9696
Note: The playbook will be executed with the same privileges given to the user in the virtual machine. The playbook provided cannot include other files from the host system, as they will not be copied.
9797
Use of the `--playbook` flag will require the image to include Ansible. The default image provided will have Ansible included.
9898

99+
100+
#### **--provider**
101+
102+
Specify the provider for the machine to be created. This allows users to override the default provider on platforms
103+
that have multiple providers.
104+
99105
#### **--rootful**
100106

101107
Whether this machine prefers rootful (`true`) or rootless (`false`)
@@ -237,6 +243,11 @@ Initialize the default Podman machine with a usb device passthrough with specifi
237243
$ podman machine init --usb bus=1,devnum=3
238244
```
239245

246+
Initialize a machine on different provider than the default
247+
```
248+
$ podman machine init --provider applehv
249+
```
250+
240251
## SEE ALSO
241252
**[podman(1)](podman.1.md)**, **[podman-machine(1)](podman-machine.1.md)**, **containers.conf(5)**
242253

docs/source/markdown/podman-machine.1.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ environment variable while the machines are running can lead to unexpected behav
2222

2323
Podman machine behaviour can be modified via the [machine] section in the containers.conf(5) file.
2424

25+
Podman is based on virtual machine providers. The following table describes which providers are
26+
supported by platform. The asterisk denotes the default provider for the platform.
27+
28+
| Platform | Provider |
29+
| -------- |----------|
30+
| Linux | qemu* |
31+
| MacOS | libkrun* |
32+
| MacOS | applehv |
33+
| Windows | wsl* |
34+
| Windows | hyperv |
35+
2536
## SUBCOMMANDS
2637

2738
| Command | Man Page | Description |
Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
11
package e2e_test
22

3-
import "github.com/containers/podman/v6/pkg/machine/define"
4-
53
const podmanBinary = "../../../bin/darwin/podman"
6-
7-
func getOtherProvider() string {
8-
if isVmtype(define.AppleHvVirt) {
9-
return "libkrun"
10-
} else if isVmtype(define.LibKrun) {
11-
return "applehv"
12-
}
13-
return ""
14-
}
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
package e2e_test
22

33
const podmanBinary = "../../../bin/podman-remote"
4-
5-
func getOtherProvider() string {
6-
return ""
7-
}

pkg/machine/e2e/config_init_test.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,8 @@ import (
1010
)
1111

1212
type initMachine struct {
13-
/*
14-
--cpus uint Number of CPUs (default 1)
15-
--disk-size uint Disk size in GiB (default 100)
16-
--ignition-path string Path to ignition file
17-
--username string Username of the remote user (default "core" for FCOS, "user" for Fedora)
18-
--image-path string Path to bootable image (default "testing")
19-
-m, --memory uint Memory in MiB (default 2048)
20-
--now Start machine now
21-
--rootful Whether this machine should prefer rootful container execution
22-
--playbook string Run an ansible playbook after first boot
23-
--tls-verify Require HTTPS and verify certificates when contacting registries
24-
--timezone string Set timezone (default "local")
25-
-v, --volume stringArray Volumes to mount, source:target
26-
--volume-driver string Optional volume driver
27-
*/
2813
playbook string
14+
provider string
2915
cpus *uint
3016
diskSize *uint
3117
swap *uint
@@ -80,6 +66,9 @@ func (i *initMachine) buildCmd(m *machineTestBuilder) []string {
8066
if l := len(i.playbook); l > 0 {
8167
cmd = append(cmd, "--playbook", i.playbook)
8268
}
69+
if l := len(i.provider); l > 0 {
70+
cmd = append(cmd, "--provider", i.provider)
71+
}
8372
if i.userModeNetworking {
8473
cmd = append(cmd, "--user-mode-networking")
8574
}
@@ -176,6 +165,11 @@ func (i *initMachine) withRunPlaybook(p string) *initMachine {
176165
return i
177166
}
178167

168+
func (i *initMachine) withProvider(p string) *initMachine {
169+
i.provider = p
170+
return i
171+
}
172+
179173
func (i *initMachine) withTlsVerify(tlsVerify *bool) *initMachine {
180174
i.tlsVerify = tlsVerify
181175
return i
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
package e2e_test
22

33
const podmanBinary = "../../../bin/podman-remote"
4-
5-
func getOtherProvider() string {
6-
return ""
7-
}

0 commit comments

Comments
 (0)