From 159d1ecd2239a514930d90baccc13020105c7351 Mon Sep 17 00:00:00 2001 From: Nir Soffer Date: Sat, 7 Sep 2024 01:29:10 +0300 Subject: [PATCH] Run usernet process in background Similar to the how we run the hostagent process[1], we want to run the usernet process in the background. Now a program using killpg to cleanup child processes will not terminate the usernet process. Example run with this change: % ps -o pid,pgid,ppid,command PID PGID PPID COMMAND 55768 55768 55767 -zsh 56126 56126 55768 limactl start userv2.yaml --tty=0 56128 56128 56126 /Users/nsoffer/src/lima/_output/bin/limactl usernet ... 56131 56131 56126 /Users/nsoffer/src/lima/_output/bin/limactl hostagent ... % ps -o pid,pgid,ppid,command PID PGID PPID COMMAND 55768 55768 55767 -zsh 56128 56128 1 /Users/nsoffer/src/lima/_output/bin/limactl usernet ... 56131 56131 1 /Users/nsoffer/src/lima/_output/bin/limactl hostagent ... [1] https://github.com/lima-vm/lima/pull/2574 Signed-off-by: Nir Soffer --- .../ha_cmd_opts_others.go => executil/opts_others.go} | 2 +- .../ha_cmd_opts_windows.go => executil/opts_windows.go} | 2 +- pkg/instance/start.go | 5 +++-- pkg/networks/usernet/recoincile.go | 2 ++ 4 files changed, 7 insertions(+), 4 deletions(-) rename pkg/{instance/ha_cmd_opts_others.go => executil/opts_others.go} (90%) rename pkg/{instance/ha_cmd_opts_windows.go => executil/opts_windows.go} (91%) diff --git a/pkg/instance/ha_cmd_opts_others.go b/pkg/executil/opts_others.go similarity index 90% rename from pkg/instance/ha_cmd_opts_others.go rename to pkg/executil/opts_others.go index 56ec240b159..41c5479aaeb 100644 --- a/pkg/instance/ha_cmd_opts_others.go +++ b/pkg/executil/opts_others.go @@ -1,6 +1,6 @@ //go:build !windows -package instance +package executil import ( "syscall" diff --git a/pkg/instance/ha_cmd_opts_windows.go b/pkg/executil/opts_windows.go similarity index 91% rename from pkg/instance/ha_cmd_opts_windows.go rename to pkg/executil/opts_windows.go index 6b7c97adbfb..99f62901221 100644 --- a/pkg/instance/ha_cmd_opts_windows.go +++ b/pkg/executil/opts_windows.go @@ -1,4 +1,4 @@ -package instance +package executil import ( "syscall" diff --git a/pkg/instance/start.go b/pkg/instance/start.go index 00dd9ede04e..f3ec3064b4b 100644 --- a/pkg/instance/start.go +++ b/pkg/instance/start.go @@ -17,6 +17,7 @@ import ( "github.com/coreos/go-semver/semver" "github.com/lima-vm/lima/pkg/driver" "github.com/lima-vm/lima/pkg/driverutil" + "github.com/lima-vm/lima/pkg/executil" "github.com/lima-vm/lima/pkg/osutil" "github.com/lima-vm/lima/pkg/qemu" "github.com/lima-vm/lima/pkg/qemu/entitlementutil" @@ -191,9 +192,9 @@ func Start(ctx context.Context, inst *store.Instance, launchHostAgentForeground haCmd := exec.CommandContext(ctx, self, args...) if launchHostAgentForeground { - haCmd.SysProcAttr = ForegroundSysProcAttr + haCmd.SysProcAttr = executil.ForegroundSysProcAttr } else { - haCmd.SysProcAttr = BackgroundSysProcAttr + haCmd.SysProcAttr = executil.BackgroundSysProcAttr } haCmd.Stdout = haStdoutW diff --git a/pkg/networks/usernet/recoincile.go b/pkg/networks/usernet/recoincile.go index 8828dc344d0..d0026654f58 100644 --- a/pkg/networks/usernet/recoincile.go +++ b/pkg/networks/usernet/recoincile.go @@ -13,6 +13,7 @@ import ( "strings" "time" + "github.com/lima-vm/lima/pkg/executil" "github.com/lima-vm/lima/pkg/lockutil" "github.com/lima-vm/lima/pkg/store" "github.com/lima-vm/lima/pkg/store/dirnames" @@ -81,6 +82,7 @@ func Start(ctx context.Context, name string) error { args = append(args, "--leases", leasesString) } cmd := exec.CommandContext(ctx, self, args...) + cmd.SysProcAttr = executil.BackgroundSysProcAttr stdoutPath := filepath.Join(usernetDir, fmt.Sprintf("%s.%s.%s.log", "usernet", name, "stdout")) stderrPath := filepath.Join(usernetDir, fmt.Sprintf("%s.%s.%s.log", "usernet", name, "stderr"))