Skip to content

Commit 3057806

Browse files
authored
Merge pull request #2146 from norio-nomura/limactl-start-foreground
limactl start: add `--foreground` flag
2 parents 62b1842 + 599712b commit 3057806

File tree

5 files changed

+48
-4
lines changed

5 files changed

+48
-4
lines changed

cmd/limactl/edit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func editAction(cmd *cobra.Command, args []string) error {
122122
if err != nil {
123123
return err
124124
}
125-
return start.Start(ctx, inst)
125+
return start.Start(ctx, inst, false)
126126
}
127127

128128
func askWhetherToStart() (bool, error) {

cmd/limactl/start.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http"
99
"os"
1010
"path/filepath"
11+
"runtime"
1112
"strings"
1213

1314
"github.com/containerd/containerd/identifiers"
@@ -92,6 +93,9 @@ See the examples in 'limactl create --help'.
9293
RunE: startAction,
9394
}
9495
registerCreateFlags(startCommand, "[limactl create] ")
96+
if runtime.GOOS != "windows" {
97+
startCommand.Flags().Bool("foreground", false, "run the hostagent in the foreground")
98+
}
9599
startCommand.Flags().Duration("timeout", start.DefaultWatchHostAgentEventsTimeout, "duration to wait for the instance to be running before timing out")
96100
return startCommand
97101
}
@@ -510,6 +514,14 @@ func startAction(cmd *cobra.Command, args []string) error {
510514
return err
511515
}
512516

517+
launchHostAgentForeground := false
518+
if runtime.GOOS != "windows" {
519+
foreground, err := cmd.Flags().GetBool("foreground")
520+
if err != nil {
521+
return err
522+
}
523+
launchHostAgentForeground = foreground
524+
}
513525
timeout, err := cmd.Flags().GetDuration("timeout")
514526
if err != nil {
515527
return err
@@ -518,7 +530,7 @@ func startAction(cmd *cobra.Command, args []string) error {
518530
ctx = start.WithWatchHostAgentTimeout(ctx, timeout)
519531
}
520532

521-
return start.Start(ctx, inst)
533+
return start.Start(ctx, inst, launchHostAgentForeground)
522534
}
523535

524536
func createBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {

pkg/osutil/osutil_unix.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import (
99
"golang.org/x/sys/unix"
1010
)
1111

12+
func Dup2(oldfd, newfd int) (err error) {
13+
return unix.Dup2(oldfd, newfd)
14+
}
15+
1216
func Ftruncate(fd int, length int64) (err error) {
1317
return unix.Ftruncate(fd, length)
1418
}

pkg/osutil/osutil_windows.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ func SysKill(pid int, _ Signal) error {
3434
return windows.GenerateConsoleCtrlEvent(syscall.CTRL_BREAK_EVENT, uint32(pid))
3535
}
3636

37+
func Dup2(oldfd int, newfd syscall.Handle) (err error) {
38+
return fmt.Errorf("unimplemented")
39+
}
40+
3741
func Ftruncate(_ int, _ int64) (err error) {
3842
return fmt.Errorf("unimplemented")
3943
}

pkg/start/start.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os/exec"
1111
"path/filepath"
1212
"runtime"
13+
"syscall"
1314
"text/template"
1415
"time"
1516

@@ -19,6 +20,7 @@ import (
1920
"github.com/lima-vm/lima/pkg/osutil"
2021
"github.com/lima-vm/lima/pkg/qemu"
2122
"github.com/lima-vm/lima/pkg/qemu/entitlementutil"
23+
"github.com/mattn/go-isatty"
2224

2325
"github.com/lima-vm/lima/pkg/downloader"
2426
"github.com/lima-vm/lima/pkg/fileutils"
@@ -115,7 +117,7 @@ func Prepare(ctx context.Context, inst *store.Instance) (*Prepared, error) {
115117

116118
// Start starts the instance.
117119
// Start calls Prepare by itself, so you do not need to call Prepare manually before calling Start.
118-
func Start(ctx context.Context, inst *store.Instance) error {
120+
func Start(ctx context.Context, inst *store.Instance, launchHostAgentForeground bool) error {
119121
haPIDPath := filepath.Join(inst.Dir, filenames.HostAgentPID)
120122
if _, err := os.Stat(haPIDPath); !errors.Is(err, os.ErrNotExist) {
121123
return fmt.Errorf("instance %q seems running (hint: remove %q if the instance is not actually running)", inst.Name, haPIDPath)
@@ -194,7 +196,29 @@ func Start(ctx context.Context, inst *store.Instance) error {
194196

195197
begin := time.Now() // used for logrus propagation
196198

197-
if err := haCmd.Start(); err != nil {
199+
if launchHostAgentForeground {
200+
logrus.Info("Running the host agent in the foreground")
201+
if isatty.IsTerminal(os.Stdin.Fd()) || isatty.IsCygwinTerminal(os.Stdin.Fd()) {
202+
// Write message to standard log files to avoid confusing users
203+
message := "This log file is not used because `limactl start` was launched in the terminal with the `--foreground` option."
204+
if _, err := haStdoutW.WriteString(message); err != nil {
205+
return err
206+
}
207+
if _, err := haStderrW.WriteString(message); err != nil {
208+
return err
209+
}
210+
} else {
211+
if err := osutil.Dup2(int(haStdoutW.Fd()), syscall.Stdout); err != nil {
212+
return err
213+
}
214+
if err := osutil.Dup2(int(haStderrW.Fd()), syscall.Stderr); err != nil {
215+
return err
216+
}
217+
}
218+
if err := syscall.Exec(self, haCmd.Args, haCmd.Environ()); err != nil {
219+
return err
220+
}
221+
} else if err := haCmd.Start(); err != nil {
198222
return err
199223
}
200224

0 commit comments

Comments
 (0)