diff --git a/cmd/limactl/shell.go b/cmd/limactl/shell.go index ff771248e2b..2c6371f605c 100644 --- a/cmd/limactl/shell.go +++ b/cmd/limactl/shell.go @@ -18,6 +18,7 @@ import ( "github.com/lima-vm/lima/pkg/ioutilx" "github.com/lima-vm/lima/pkg/sshutil" "github.com/lima-vm/lima/pkg/store" + "github.com/lima-vm/sshocker/pkg/ssh" "github.com/mattn/go-isatty" "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -49,6 +50,7 @@ func newShellCommand() *cobra.Command { shellCmd.Flags().String("shell", "", "shell interpreter, e.g. /bin/bash") shellCmd.Flags().String("workdir", "", "working directory") + shellCmd.Flags().Bool("reconnect", false, "reconnect to the SSH session") return shellCmd } @@ -81,6 +83,24 @@ func shellAction(cmd *cobra.Command, args []string) error { return fmt.Errorf("instance %q is stopped, run `limactl start %s` to start the instance", instName, instName) } + restart, err := cmd.Flags().GetBool("reconnect") + if err != nil { + return err + } + if restart { + logrus.Infof("Exiting ssh session for the instance %q", instName) + + sshConfig := &ssh.SSHConfig{ + ConfigFile: inst.SSHConfigFile, + Persist: false, + AdditionalArgs: []string{}, + } + + if err := ssh.ExitMaster(inst.Hostname, inst.SSHLocalPort, sshConfig); err != nil { + return err + } + } + // When workDir is explicitly set, the shell MUST have workDir as the cwd, or exit with an error. // // changeDirCmd := "cd workDir || exit 1" if workDir != ""