@@ -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