Skip to content

Commit

Permalink
Introduce podman machine init --root=t|f and podman machine set --roo…
Browse files Browse the repository at this point in the history
…t=t|f

Switch default to rootless for mac and windows

Signed-off-by: Jason T. Greene <[email protected]>
  • Loading branch information
n1hility committed Feb 16, 2022
1 parent 8f5ba05 commit c74f8f0
Show file tree
Hide file tree
Showing 10 changed files with 318 additions and 40 deletions.
5 changes: 4 additions & 1 deletion cmd/podman/machine/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var (

var (
initOpts = machine.InitOptions{}
defaultMachineName = "podman-machine-default"
defaultMachineName = machine.DefaultMachineName
now bool
)

Expand Down Expand Up @@ -99,6 +99,9 @@ func init() {
IgnitionPathFlagName := "ignition-path"
flags.StringVar(&initOpts.IgnitionPath, IgnitionPathFlagName, "", "Path to ignition file")
_ = initCmd.RegisterFlagCompletionFunc(IgnitionPathFlagName, completion.AutocompleteDefault)

rootfulFlagName := "rootful"
flags.BoolVar(&initOpts.Rootful, rootfulFlagName, false, "Whether this machine should prefer rootful container exectution")
}

// TODO should we allow for a users to append to the qemu cmdline?
Expand Down
56 changes: 56 additions & 0 deletions cmd/podman/machine/set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// +build amd64 arm64

package machine

import (
"github.com/containers/common/pkg/completion"
"github.com/containers/podman/v4/cmd/podman/registry"
"github.com/containers/podman/v4/pkg/machine"
"github.com/spf13/cobra"
)

var (
setCmd = &cobra.Command{
Use: "set [options] [NAME]",
Short: "Sets a virtual machine setting",
Long: "Sets an updatable virtual machine setting",
RunE: setMachine,
Args: cobra.MaximumNArgs(1),
Example: `podman machine set --root=false`,
ValidArgsFunction: completion.AutocompleteNone,
}
)

var (
setOpts = machine.SetOptions{}
)

func init() {
registry.Commands = append(registry.Commands, registry.CliCommand{
Command: setCmd,
Parent: machineCmd,
})
flags := setCmd.Flags()

rootfulFlagName := "rootful"
flags.BoolVar(&setOpts.Rootful, rootfulFlagName, false, "Whether this machine should prefer rootful container execution")
}

func setMachine(cmd *cobra.Command, args []string) error {
var (
vm machine.VM
err error
)

vmName := defaultMachineName
if len(args) > 0 && len(args[0]) > 0 {
vmName = args[0]
}
provider := getSystemDefaultProvider()
vm, err = provider.LoadVMByName(vmName)
if err != nil {
return err
}

return vm.Set(vmName, setOpts)
}
9 changes: 9 additions & 0 deletions docs/source/markdown/podman-machine-init.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ Memory (in MB).

Start the virtual machine immediately after it has been initialized.

#### **--rootful**=*true|false*

Whether this machine should prefer rootful (`true`) or rootless (`false`)
container execution. This option will also determine the remote connection default
if there is no existing remote connection configurations.

API forwarding, if available, will follow this setting.

#### **--timezone**

Set the timezone for the machine and containers. Valid values are `local` or
Expand Down Expand Up @@ -84,6 +92,7 @@ Print usage statement.
```
$ podman machine init
$ podman machine init myvm
$ podman machine init --rootful
$ podman machine init --disk-size 50
$ podman machine init --memory=1024 myvm
$ podman machine init -v /Users:/mnt/Users
Expand Down
59 changes: 59 additions & 0 deletions docs/source/markdown/podman-machine-set.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
% podman-machine-set(1)

## NAME
podman\-machine\-set - Sets a virtual machine setting

## SYNOPSIS
**podman machine set** [*options*] [*name*]

## DESCRIPTION

Sets an updatable virtual machine setting.

Options mirror values passed to `podman machine init`. Only a limited
subset can be changed after machine initialization.

## OPTIONS

#### **--rootful**=*true|false*

Whether this machine should prefer rootful (`true`) or rootless (`false`)
container execution. This option will also update the current podman
remote connection default if it is currently pointing at the specified
machine name (or `podman-machine-default` if no name is specified).

API forwarding, if available, will follow this setting.

#### **--help**

Print usage statement.

## EXAMPLES

To switch the default VM `podman-machine-default` from rootless to rootful:

```
$ podman machine set --rootful
```

or more explicitly:

```
$ podman machine set --rootful=true
```

To switch the default VM `podman-machine-default` from rootful to rootless:
```
$ podman machine set --rootful=false
```

To switch the VM `myvm` from rootless to rootful:
```
$ podman machine set --rootful myvm
```

## SEE ALSO
**[podman(1)](podman.1.md)**, **[podman-machine(1)](podman-machine.1.md)**

## HISTORY
February 2022, Originally compiled by Jason Greene <[email protected]>
1 change: 1 addition & 0 deletions docs/source/markdown/podman-machine.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ podman\-machine - Manage Podman's virtual machine
| init | [podman-machine-init(1)](podman-machine-init.1.md) | Initialize a new virtual machine |
| list | [podman-machine-list(1)](podman-machine-list.1.md) | List virtual machines |
| rm | [podman-machine-rm(1)](podman-machine-rm.1.md) | Remove a virtual machine |
| set | [podman-machine-set(1)](podman-machine-set.1.md) | Sets a virtual machine setting |
| ssh | [podman-machine-ssh(1)](podman-machine-ssh.1.md) | SSH into a virtual machine |
| start | [podman-machine-start(1)](podman-machine-start.1.md) | Start a virtual machine |
| stop | [podman-machine-stop(1)](podman-machine-stop.1.md) | Stop a virtual machine |
Expand Down
9 changes: 8 additions & 1 deletion pkg/machine/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type InitOptions struct {
URI url.URL
Username string
ReExec bool
Rootful bool
}

type QemuMachineStatus = string
Expand All @@ -35,7 +36,8 @@ const (
// Running indicates the qemu vm is running
Running QemuMachineStatus = "running"
// Stopped indicates the vm has stopped
Stopped QemuMachineStatus = "stopped"
Stopped QemuMachineStatus = "stopped"
DefaultMachineName string = "podman-machine-default"
)

type Provider interface {
Expand Down Expand Up @@ -89,6 +91,10 @@ type ListResponse struct {
IdentityPath string
}

type SetOptions struct {
Rootful bool
}

type SSHOptions struct {
Username string
Args []string
Expand All @@ -107,6 +113,7 @@ type RemoveOptions struct {
type VM interface {
Init(opts InitOptions) (bool, error)
Remove(name string, opts RemoveOptions) (string, func() error, error)
Set(name string, opts SetOptions) error
SSH(name string, opts SSHOptions) error
Start(name string, opts StartOptions) error
Stop(name string, opts StopOptions) error
Expand Down
25 changes: 25 additions & 0 deletions pkg/machine/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,31 @@ func AddConnection(uri fmt.Stringer, name, identity string, isDefault bool) erro
return cfg.Write()
}

func AnyConnectionDefault(name ...string) (bool, error) {
cfg, err := config.ReadCustomConfig()
if err != nil {
return false, err
}
for _, n := range name {
if n == cfg.Engine.ActiveService {
return true, nil
}
}

return false, nil
}

func ChangeDefault(name string) error {
cfg, err := config.ReadCustomConfig()
if err != nil {
return err
}

cfg.Engine.ActiveService = name

return cfg.Write()
}

func RemoveConnection(name string) error {
cfg, err := config.ReadCustomConfig()
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/machine/qemu/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ type MachineVM struct {
QMPMonitor Monitor
// RemoteUsername of the vm user
RemoteUsername string
// Whether this machine should run in a rootful or rootless manner
Rootful bool
}

type Mount struct {
Expand Down
Loading

0 comments on commit c74f8f0

Please sign in to comment.