-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Add podman machine restart subcommand
#28687
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
30bd1b5
33ae62d
1d7d182
a317420
47a7d4b
df76d65
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| //go:build amd64 || arm64 | ||
|
|
||
| package machine | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "go.podman.io/podman/v6/cmd/podman/registry" | ||
| "go.podman.io/podman/v6/libpod/events" | ||
| "go.podman.io/podman/v6/pkg/machine" | ||
| "go.podman.io/podman/v6/pkg/machine/shim" | ||
| ) | ||
|
|
||
| var ( | ||
| restartCmd = &cobra.Command{ | ||
| Use: "restart [options] [MACHINE]", | ||
| Short: "Restart an existing machine", | ||
| Long: "Restart a managed virtual machine", | ||
| PersistentPreRunE: machinePreRunE, | ||
| RunE: restart, | ||
| Args: cobra.MaximumNArgs(1), | ||
| Example: `podman machine restart podman-machine-default`, | ||
| ValidArgsFunction: AutocompleteMachine, | ||
| } | ||
| restartOpts = machine.StartOptions{} | ||
| ) | ||
|
|
||
| func init() { | ||
| registry.Commands = append(registry.Commands, registry.CliCommand{ | ||
| Command: restartCmd, | ||
| Parent: machineCmd, | ||
| }) | ||
|
|
||
| flags := restartCmd.Flags() | ||
| noInfoFlagName := "no-info" | ||
| flags.BoolVar(&restartOpts.NoInfo, noInfoFlagName, false, "Suppress informational tips") | ||
|
|
||
| quietFlagName := "quiet" | ||
| flags.BoolVarP(&restartOpts.Quiet, quietFlagName, "q", false, "Suppress machine restarting status output") | ||
| } | ||
|
|
||
| func restart(_ *cobra.Command, args []string) error { | ||
| restartOpts.NoInfo = restartOpts.Quiet || restartOpts.NoInfo | ||
| vmName := defaultMachineName | ||
| if len(args) > 0 && len(args[0]) > 0 { | ||
| vmName = args[0] | ||
| } | ||
|
|
||
| mc, vmProvider, err := shim.VMExists(vmName) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if !restartOpts.Quiet { | ||
| fmt.Printf("Restarting machine %q\n", vmName) | ||
| } | ||
|
|
||
| updateConnection := false | ||
| if err := shim.StopThenStart(mc, vmProvider, false, restartOpts, &updateConnection); err != nil { | ||
| return err | ||
| } | ||
| fmt.Printf("Machine %q restarted successfully\n", vmName) | ||
| newMachineEvent(events.Stop, events.Event{Name: vmName}) | ||
| newMachineEvent(events.Start, events.Event{Name: vmName}) | ||
|
Comment on lines
+64
to
+65
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non-blocking: Maybe create a new event.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @baude I want to double check if I can address this Nit with the following change. I did read your comment that
and see this event,
|
||
| return nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| % podman-machine-restart 1 | ||
|
|
||
| ## NAME | ||
| podman\-machine\-restart - Restart a virtual machine | ||
|
|
||
| ## SYNOPSIS | ||
| **podman machine restart** [*options*] [*name*] | ||
|
|
||
| ## DESCRIPTION | ||
|
|
||
| Restarts a virtual machine for Podman. | ||
|
|
||
| The default machine name is `podman-machine-default`. If a machine name is not specified as an argument, | ||
| then `podman-machine-default` will be restarted. | ||
|
|
||
| Stopping an already stopped virtual machine is not considered an error so running restart on a stopped | ||
| virtual machine just starts it from a stopped state. | ||
|
|
||
| **podman machine restart** stops and then starts a Linux virtual machine where containers are run. | ||
|
|
||
| ## OPTIONS | ||
|
|
||
| #### **--help** | ||
|
|
||
| Print usage statement. | ||
|
|
||
| #### **--no-info** | ||
|
|
||
| Suppress informational tips. | ||
|
|
||
| #### **--quiet**, **-q** | ||
|
|
||
| Suppress machine restarting status output. | ||
|
|
||
| ## EXAMPLES | ||
|
|
||
| Restart a podman machine named myvm. | ||
| ``` | ||
| $ podman machine restart myvm | ||
| ``` | ||
|
|
||
| ## SEE ALSO | ||
| **[podman(1)](podman.1.md)**, **[podman-machine(1)](podman-machine.1.md)**, **[podman-machine-start(1)](podman-machine-start.1.md)**, **[podman-machine-stop(1)](podman-machine-stop.1.md)** | ||
|
|
||
| ## HISTORY | ||
| May 2026, Originally compiled by Jait Jacob <jai8.jacob@gmail.com> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package e2e_test | ||
|
|
||
| type restartMachine struct{} | ||
|
|
||
| func (r restartMachine) buildCmd(m *machineTestBuilder) []string { | ||
| cmd := []string{"machine", "restart"} | ||
| if len(m.name) > 0 { | ||
| cmd = append(cmd, m.name) | ||
| } | ||
| return cmd | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package e2e_test | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "go.podman.io/podman/v6/pkg/machine/define" | ||
|
|
||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| . "github.com/onsi/gomega/gexec" | ||
| ) | ||
|
|
||
| var _ = Describe("podman machine restart", func() { | ||
| It("should tell you when trying to restart a machine that doesn't exist", func() { | ||
| r := restartMachine{} | ||
| name := "aVMThatDoesntExist" | ||
| session, err := mb.setName(name).setCmd(r).run() | ||
| Expect(err).ToNot(HaveOccurred()) | ||
| Expect(session).To(Exit(125)) | ||
| Expect(session.errorToString()).To(ContainSubstring("VM does not exist")) | ||
| }) | ||
|
|
||
| It("should restart a running machine", func() { | ||
| name := randomString() | ||
| i := new(initMachine) | ||
| session, err := mb.setName(name).setCmd(i.withImage(mb.imagePath).withNow().withVolume("")).run() | ||
| Expect(err).ToNot(HaveOccurred()) | ||
| Expect(session).To(Exit(0)) | ||
|
|
||
| r := restartMachine{} | ||
| restartSession, err := mb.setName(name).setCmd(r).run() | ||
| Expect(err).ToNot(HaveOccurred()) | ||
| Expect(restartSession).To(Exit(0)) | ||
| Expect(restartSession.outputToString()).To(ContainSubstring(fmt.Sprintf("Machine %q restarted successfully", name))) | ||
|
|
||
| inspect := new(inspectMachine) | ||
| inspectSession, err := mb.setName(name).setCmd(inspect.withFormat("{{.State}}")).run() | ||
| Expect(err).ToNot(HaveOccurred()) | ||
| Expect(inspectSession).To(Exit(0)) | ||
| Expect(inspectSession.outputToString()).To(Equal(define.Running)) | ||
| }) | ||
|
|
||
| It("should start a stopped machine", func() { | ||
| name := randomString() | ||
| i := new(initMachine) | ||
| session, err := mb.setName(name).setCmd(i.withImage(mb.imagePath).withVolume("")).run() | ||
| Expect(err).ToNot(HaveOccurred()) | ||
| Expect(session).To(Exit(0)) | ||
|
|
||
| inspect := new(inspectMachine) | ||
| inspectSession, err := mb.setName(name).setCmd(inspect.withFormat("{{.State}}")).run() | ||
| Expect(err).ToNot(HaveOccurred()) | ||
| Expect(inspectSession).To(Exit(0)) | ||
| Expect(inspectSession.outputToString()).To(Equal(define.Stopped)) | ||
|
|
||
| r := restartMachine{} | ||
| restartSession, err := mb.setName(name).setCmd(r).run() | ||
| Expect(err).ToNot(HaveOccurred()) | ||
| Expect(restartSession).To(Exit(0)) | ||
|
|
||
| inspectSession, err = mb.setName(name).setCmd(inspect.withFormat("{{.State}}")).run() | ||
| Expect(err).ToNot(HaveOccurred()) | ||
| Expect(inspectSession).To(Exit(0)) | ||
| Expect(inspectSession.outputToString()).To(Equal(define.Running)) | ||
| }) | ||
| }) |
Uh oh!
There was an error while loading. Please reload this page.