Skip to content

Commit 1a8c715

Browse files
n1hilitymheon
authored andcommitted
Introduce podman machine init --root=t|f and podman machine set --root=t|f
Switch default to rootless for mac and windows Signed-off-by: Jason T. Greene <[email protected]>
1 parent f71dfcb commit 1a8c715

File tree

10 files changed

+318
-40
lines changed

10 files changed

+318
-40
lines changed

cmd/podman/machine/init.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var (
2626

2727
var (
2828
initOpts = machine.InitOptions{}
29-
defaultMachineName = "podman-machine-default"
29+
defaultMachineName = machine.DefaultMachineName
3030
now bool
3131
)
3232

@@ -99,6 +99,9 @@ func init() {
9999
IgnitionPathFlagName := "ignition-path"
100100
flags.StringVar(&initOpts.IgnitionPath, IgnitionPathFlagName, "", "Path to ignition file")
101101
_ = initCmd.RegisterFlagCompletionFunc(IgnitionPathFlagName, completion.AutocompleteDefault)
102+
103+
rootfulFlagName := "rootful"
104+
flags.BoolVar(&initOpts.Rootful, rootfulFlagName, false, "Whether this machine should prefer rootful container exectution")
102105
}
103106

104107
// TODO should we allow for a users to append to the qemu cmdline?

cmd/podman/machine/set.go

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// +build amd64 arm64
2+
3+
package machine
4+
5+
import (
6+
"github.com/containers/common/pkg/completion"
7+
"github.com/containers/podman/v4/cmd/podman/registry"
8+
"github.com/containers/podman/v4/pkg/machine"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
var (
13+
setCmd = &cobra.Command{
14+
Use: "set [options] [NAME]",
15+
Short: "Sets a virtual machine setting",
16+
Long: "Sets an updatable virtual machine setting",
17+
RunE: setMachine,
18+
Args: cobra.MaximumNArgs(1),
19+
Example: `podman machine set --root=false`,
20+
ValidArgsFunction: completion.AutocompleteNone,
21+
}
22+
)
23+
24+
var (
25+
setOpts = machine.SetOptions{}
26+
)
27+
28+
func init() {
29+
registry.Commands = append(registry.Commands, registry.CliCommand{
30+
Command: setCmd,
31+
Parent: machineCmd,
32+
})
33+
flags := setCmd.Flags()
34+
35+
rootfulFlagName := "rootful"
36+
flags.BoolVar(&setOpts.Rootful, rootfulFlagName, false, "Whether this machine should prefer rootful container execution")
37+
}
38+
39+
func setMachine(cmd *cobra.Command, args []string) error {
40+
var (
41+
vm machine.VM
42+
err error
43+
)
44+
45+
vmName := defaultMachineName
46+
if len(args) > 0 && len(args[0]) > 0 {
47+
vmName = args[0]
48+
}
49+
provider := getSystemDefaultProvider()
50+
vm, err = provider.LoadVMByName(vmName)
51+
if err != nil {
52+
return err
53+
}
54+
55+
return vm.Set(vmName, setOpts)
56+
}

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

+9
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ Memory (in MB).
5555

5656
Start the virtual machine immediately after it has been initialized.
5757

58+
#### **--rootful**=*true|false*
59+
60+
Whether this machine should prefer rootful (`true`) or rootless (`false`)
61+
container execution. This option will also determine the remote connection default
62+
if there is no existing remote connection configurations.
63+
64+
API forwarding, if available, will follow this setting.
65+
5866
#### **--timezone**
5967

6068
Set the timezone for the machine and containers. Valid values are `local` or
@@ -84,6 +92,7 @@ Print usage statement.
8492
```
8593
$ podman machine init
8694
$ podman machine init myvm
95+
$ podman machine init --rootful
8796
$ podman machine init --disk-size 50
8897
$ podman machine init --memory=1024 myvm
8998
$ podman machine init -v /Users:/mnt/Users
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
% podman-machine-set(1)
2+
3+
## NAME
4+
podman\-machine\-set - Sets a virtual machine setting
5+
6+
## SYNOPSIS
7+
**podman machine set** [*options*] [*name*]
8+
9+
## DESCRIPTION
10+
11+
Sets an updatable virtual machine setting.
12+
13+
Options mirror values passed to `podman machine init`. Only a limited
14+
subset can be changed after machine initialization.
15+
16+
## OPTIONS
17+
18+
#### **--rootful**=*true|false*
19+
20+
Whether this machine should prefer rootful (`true`) or rootless (`false`)
21+
container execution. This option will also update the current podman
22+
remote connection default if it is currently pointing at the specified
23+
machine name (or `podman-machine-default` if no name is specified).
24+
25+
API forwarding, if available, will follow this setting.
26+
27+
#### **--help**
28+
29+
Print usage statement.
30+
31+
## EXAMPLES
32+
33+
To switch the default VM `podman-machine-default` from rootless to rootful:
34+
35+
```
36+
$ podman machine set --rootful
37+
```
38+
39+
or more explicitly:
40+
41+
```
42+
$ podman machine set --rootful=true
43+
```
44+
45+
To switch the default VM `podman-machine-default` from rootful to rootless:
46+
```
47+
$ podman machine set --rootful=false
48+
```
49+
50+
To switch the VM `myvm` from rootless to rootful:
51+
```
52+
$ podman machine set --rootful myvm
53+
```
54+
55+
## SEE ALSO
56+
**[podman(1)](podman.1.md)**, **[podman-machine(1)](podman-machine.1.md)**
57+
58+
## HISTORY
59+
February 2022, Originally compiled by Jason Greene <[email protected]>

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

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ podman\-machine - Manage Podman's virtual machine
1616
| init | [podman-machine-init(1)](podman-machine-init.1.md) | Initialize a new virtual machine |
1717
| list | [podman-machine-list(1)](podman-machine-list.1.md) | List virtual machines |
1818
| rm | [podman-machine-rm(1)](podman-machine-rm.1.md) | Remove a virtual machine |
19+
| set | [podman-machine-set(1)](podman-machine-set.1.md) | Sets a virtual machine setting |
1920
| ssh | [podman-machine-ssh(1)](podman-machine-ssh.1.md) | SSH into a virtual machine |
2021
| start | [podman-machine-start(1)](podman-machine-start.1.md) | Start a virtual machine |
2122
| stop | [podman-machine-stop(1)](podman-machine-stop.1.md) | Stop a virtual machine |

pkg/machine/config.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type InitOptions struct {
2727
URI url.URL
2828
Username string
2929
ReExec bool
30+
Rootful bool
3031
}
3132

3233
type QemuMachineStatus = string
@@ -35,7 +36,8 @@ const (
3536
// Running indicates the qemu vm is running
3637
Running QemuMachineStatus = "running"
3738
// Stopped indicates the vm has stopped
38-
Stopped QemuMachineStatus = "stopped"
39+
Stopped QemuMachineStatus = "stopped"
40+
DefaultMachineName string = "podman-machine-default"
3941
)
4042

4143
type Provider interface {
@@ -89,6 +91,10 @@ type ListResponse struct {
8991
IdentityPath string
9092
}
9193

94+
type SetOptions struct {
95+
Rootful bool
96+
}
97+
9298
type SSHOptions struct {
9399
Username string
94100
Args []string
@@ -107,6 +113,7 @@ type RemoveOptions struct {
107113
type VM interface {
108114
Init(opts InitOptions) (bool, error)
109115
Remove(name string, opts RemoveOptions) (string, func() error, error)
116+
Set(name string, opts SetOptions) error
110117
SSH(name string, opts SSHOptions) error
111118
Start(name string, opts StartOptions) error
112119
Stop(name string, opts StopOptions) error

pkg/machine/connection.go

+25
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,31 @@ func AddConnection(uri fmt.Stringer, name, identity string, isDefault bool) erro
3939
return cfg.Write()
4040
}
4141

42+
func AnyConnectionDefault(name ...string) (bool, error) {
43+
cfg, err := config.ReadCustomConfig()
44+
if err != nil {
45+
return false, err
46+
}
47+
for _, n := range name {
48+
if n == cfg.Engine.ActiveService {
49+
return true, nil
50+
}
51+
}
52+
53+
return false, nil
54+
}
55+
56+
func ChangeDefault(name string) error {
57+
cfg, err := config.ReadCustomConfig()
58+
if err != nil {
59+
return err
60+
}
61+
62+
cfg.Engine.ActiveService = name
63+
64+
return cfg.Write()
65+
}
66+
4267
func RemoveConnection(name string) error {
4368
cfg, err := config.ReadCustomConfig()
4469
if err != nil {

pkg/machine/qemu/config.go

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ type MachineVM struct {
3333
QMPMonitor Monitor
3434
// RemoteUsername of the vm user
3535
RemoteUsername string
36+
// Whether this machine should run in a rootful or rootless manner
37+
Rootful bool
3638
}
3739

3840
type Mount struct {

0 commit comments

Comments
 (0)