Skip to content

Commit 5fb7439

Browse files
committed
Add podman machine restart subcommand
1 parent 00d375f commit 5fb7439

3 files changed

Lines changed: 96 additions & 0 deletions

File tree

cmd/podman/machine/restart.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//go:build amd64 || arm64
2+
3+
package machine
4+
5+
import (
6+
"fmt"
7+
8+
"github.com/spf13/cobra"
9+
"go.podman.io/podman/v6/cmd/podman/registry"
10+
"go.podman.io/podman/v6/libpod/events"
11+
"go.podman.io/podman/v6/pkg/machine"
12+
"go.podman.io/podman/v6/pkg/machine/shim"
13+
)
14+
15+
var restartCmd = &cobra.Command{
16+
Use: "restart [MACHINE]",
17+
Short: "Restart an existing machine",
18+
Long: "Restart a managed virtual machine",
19+
PersistentPreRunE: machinePreRunE,
20+
RunE: restart,
21+
Args: cobra.MaximumNArgs(1),
22+
Example: `podman machine restart podman-machine-default`,
23+
ValidArgsFunction: AutocompleteMachine,
24+
}
25+
26+
func init() {
27+
registry.Commands = append(registry.Commands, registry.CliCommand{
28+
Command: restartCmd,
29+
Parent: machineCmd,
30+
})
31+
}
32+
33+
func restart(_ *cobra.Command, args []string) error {
34+
vmName := defaultMachineName
35+
if len(args) > 0 && len(args[0]) > 0 {
36+
vmName = args[0]
37+
}
38+
39+
mc, vmProvider, err := shim.VMExists(vmName)
40+
if err != nil {
41+
return err
42+
}
43+
44+
if err := shim.Stop(mc, vmProvider, false); err != nil {
45+
return err
46+
}
47+
48+
fmt.Printf("Machine %q stopped successfully\n", vmName)
49+
newMachineEvent(events.Stop, events.Event{Name: vmName})
50+
51+
if err := shim.Start(mc, vmProvider, machine.StartOptions{}, nil); err != nil {
52+
return err
53+
}
54+
fmt.Printf("Machine %q started successfully\n", vmName)
55+
newMachineEvent(events.Start, events.Event{Name: vmName})
56+
return nil
57+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
% podman-machine-restart 1
2+
3+
## NAME
4+
podman\-machine\-restart - Restart a virtual machine
5+
6+
## SYNOPSIS
7+
**podman machine restart** [*name*]
8+
9+
## DESCRIPTION
10+
11+
Restarts a virtual machine for Podman.
12+
13+
The default machine name is `podman-machine-default`. If a machine name is not specified as an argument,
14+
then `podman-machine-default` will be restarted.
15+
16+
Stopping an already stopped vm is not considered an error so running restart on a stopped vm just
17+
starts it from a stopped state.
18+
19+
**podman machine restart** stops and then starts a Linux virtual machine where containers are run.
20+
21+
## OPTIONS
22+
23+
#### **--help**
24+
25+
Print usage statement.
26+
27+
## EXAMPLES
28+
29+
Restart a podman machine named myvm.
30+
```
31+
$ podman machine restart myvm
32+
```
33+
34+
## SEE ALSO
35+
**[podman(1)](podman.1.md)**, **[podman-machine(1)](podman-machine.1.md)**
36+
37+
## HISTORY
38+
March 2021, Originally compiled by Ashley Cui <acui@redhat.com>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ supported by platform. The asterisk denotes the default provider for the platfo
4949
| ssh | [podman-machine-ssh(1)](podman-machine-ssh.1.md) | SSH into a virtual machine |
5050
| start | [podman-machine-start(1)](podman-machine-start.1.md) | Start a virtual machine |
5151
| stop | [podman-machine-stop(1)](podman-machine-stop.1.md) | Stop a virtual machine |
52+
| restart | [podman-machine-restart(1)](podman-machine-restart.1.md) | Restart a virtual machine |
5253

5354
## SEE ALSO
5455
**[podman(1)](podman.1.md)**, **[podman-machine-cp(1)](podman-machine-cp.1.md)**, **[podman-machine-info(1)](podman-machine-info.1.md)**, **[podman-machine-init(1)](podman-machine-init.1.md)**, **[podman-machine-list(1)](podman-machine-list.1.md)**, **[podman-machine-os(1)](podman-machine-os.1.md)**, **[podman-machine-rm(1)](podman-machine-rm.1.md)**, **[podman-machine-ssh(1)](podman-machine-ssh.1.md)**, **[podman-machine-start(1)](podman-machine-start.1.md)**, **[podman-machine-stop(1)](podman-machine-stop.1.md)**, **[podman-machine-inspect(1)](podman-machine-inspect.1.md)**, **[podman-machine-reset(1)](podman-machine-reset.1.md)**, **[containers.conf(5)](https://github.com/containers/container-libs/blob/main/common/docs/containers.conf.5.md)**

0 commit comments

Comments
 (0)