Skip to content
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

cmd: add support for restarting a running instance #3323

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

unsuman
Copy link
Contributor

@unsuman unsuman commented Mar 9, 2025

This PR adds a new command limactl restart INSTANCE which gracefully terminates and restarts Lima instances.

}

launchHostAgentForeground := false
time.Sleep(500 * time.Millisecond)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needing a sleep is always a red flag; it means there is a race, and there is still a chance that the sleep is not long enough.

I suspect the sleep exists because we need to wait for the PID files to be deleted before we can start the instance again. I wonder if it isn't possible to make sure StopGracefully will not return until the files are deleted, and return an error when the files still exist after a somewhat longish timeout.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect the sleep exists because we need to wait for the PID files to be deleted before we can start the instance again.

It is indeed the case, the StopGracefully() function is not waiting for the host agent and driver processes to shut down, let me see if I can fix this in this PR itself.

@unsuman unsuman force-pushed the fix/exit-ssh-session branch from 34ac574 to c09421d Compare March 10, 2025 12:27
restartCmd := &cobra.Command{
Use: "restart INSTANCE",
Short: "Restart a running instance",
Args: WrapArgsError(cobra.MinimumNArgs(1)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation seems ignoring args[1:]

@AkihiroSuda AkihiroSuda added this to the v1.1.0 milestone Mar 10, 2025
@AkihiroSuda AkihiroSuda added the area/cli limactl CLI user experience label Mar 10, 2025
@unsuman unsuman force-pushed the fix/exit-ssh-session branch from c09421d to 07f181e Compare March 10, 2025 13:07
return nil
}

func waitForInstanceShutdown(ctx context.Context, inst *store.Instance) error {
Copy link
Member

@jandubois jandubois Mar 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be part of waitForHostAgentTermination in pkg/instance/stop.go, or at least called from StopGracefully. That makes it available to an external caller and protects limactl stop && limactl start from failing too.

Is there a reason we would ever want StopGracefully to return before the processes have shut down?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I added this function is because I was facing this issue while restarting a VM:

FATA[0001] instance "default" seems running (hint: remove "/Users/ansumansahoo/.lima/default/ha.pid" if the instance is not running) 

But we don't need this function anymore because networks.Reconcile() ensures everything is shutdown before a restart.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we don't need this function anymore because networks.Reconcile() ensures everything is shutdown before a restart.

How does it ensure that? It is not immediately obvious to me. Does it just take sufficient time on your machine, so the problem no longer appears (in which case this would be a variant of the sleep mechanism, and not a guarantee)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct; I didn't think of it that way. Thanks! To be on the safer side, I will add the function that ensures the instance has been shutdown before starting it again.

@unsuman unsuman force-pushed the fix/exit-ssh-session branch from 07f181e to 2b4b6c9 Compare March 11, 2025 07:49
Copy link
Member

@jandubois jandubois left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Needs checking for maximum number of args
  • I don't understand why we don't need to wait for the instance to be stopped
  • commits should be squashed

return nil
}

func waitForInstanceShutdown(ctx context.Context, inst *store.Instance) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we don't need this function anymore because networks.Reconcile() ensures everything is shutdown before a restart.

How does it ensure that? It is not immediately obvious to me. Does it just take sufficient time on your machine, so the problem no longer appears (in which case this would be a variant of the sleep mechanism, and not a guarantee)?

@jandubois
Copy link
Member

I just tested restarting a stopped instance:

limactl restart
FATA[0000] expected status "Running", got "Stopped" (maybe use `limactl stop -f`?)

I think this is correct, you need to specify limactl restart -f.

But I wonder if the error message is correct. The user obviously expects the instance to be running when the command returns. So the suggested command should probably be limactl start (or limactl restart -f).

@lima-vm/maintainers WDYT?

@AkihiroSuda
Copy link
Member

I just tested restarting a stopped instance:

It probably should just start the instance automatically, with a warning

@AkihiroSuda AkihiroSuda linked an issue Mar 12, 2025 that may be closed by this pull request
@unsuman unsuman force-pushed the fix/exit-ssh-session branch 2 times, most recently from ec0a9fb to 93a8909 Compare March 12, 2025 10:29
if inst.Status != store.StatusRunning {
if isRestart, ok := ctx.Value("restart").(bool); ok && isRestart {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the correct way of doing?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't seem so? Is there any reason to use ctx here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tested restarting a stopped instance:

It probably should just start the instance automatically, with a warning

I used ctx here to find out who is calling StopGracefully. If the instance is being restarted and is already shut down, it won’t cause an error and will continue with starting the instance.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just add an argument to the function?

Signed-off-by: Ansuman Sahoo <anshumansahoo500@gmail.com>
@unsuman unsuman force-pushed the fix/exit-ssh-session branch from 93a8909 to ea40c79 Compare March 19, 2025 08:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/cli limactl CLI user experience
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ability to perform instance reboot using limactl cli
5 participants