-
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
Open
jaitjacob
wants to merge
6
commits into
containers:main
Choose a base branch
from
jaitjacob:add-podman-machine-restart-command
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
30bd1b5
Add `podman machine restart` subcommand
jaitjacob 33ae62d
Address review comments
jaitjacob 1d7d182
Add tests
jaitjacob a317420
Add --quiet and --no-info flags too
jaitjacob 47a7d4b
Add `shim.StopThenStart`
jaitjacob df76d65
Fix failing CI cause no '[options]' in synopsis
jaitjacob File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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}) | ||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.