| 1 | Subject: [PATCH] Open independent Windows SSH connections for resource probes |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | @@ -8,12 +8,14 @@ import ( |
| 8 | "errors" |
| 9 | "io" |
| 10 | "os/exec" |
| 11 | + "runtime" |
| 12 | "strconv" |
| 13 | "sync" |
| 14 | "time" |
| 15 | |
| 16 | "github.com/lima-vm/lima/v2/pkg/driver" |
| 17 | "github.com/lima-vm/lima/v2/pkg/limatype" |
| 18 | + "github.com/lima-vm/lima/v2/pkg/sshutil" |
| 19 | ) |
| 20 | |
| 21 | // ManageResources is an optional embedding hook. It starts after the driver and |
| 22 | @@ -33,11 +35,7 @@ func (a *HostAgent) startResourceManager(ctx context.Context) func() { |
| 23 | return func() {} |
| 24 | } |
| 25 | read := func(ctx context.Context, script string) ([]byte, error) { |
| 26 | - args := append([]string{}, a.sshConfig.Args()...) |
| 27 | - args = append(args, "-o", "ConnectTimeout=2", "-o", "ClearAllForwardings=yes", "-T", "-p", strconv.Itoa(a.sshLocalPort), a.instSSHAddress, "--", script) |
| 28 | - cmd := exec.CommandContext(ctx, a.sshConfig.Binary(), args...) |
| 29 | - hideResourceProbe(cmd) |
| 30 | - cmd.WaitDelay = time.Second |
| 31 | + cmd := a.resourceProbeCommand(ctx, script) |
| 32 | pipe, err := cmd.StdoutPipe() |
| 33 | if err != nil { |
| 34 | return nil, err |
| 35 | @@ -57,3 +55,16 @@ func (a *HostAgent) startResourceManager(ctx context.Context) func() { |
| 36 | var once sync.Once |
| 37 | return func() { once.Do(stop) } |
| 38 | } |
| 39 | + |
| 40 | +func (a *HostAgent) resourceProbeCommand(ctx context.Context, script string) *exec.Cmd { |
| 41 | + args := a.sshConfig.Args() |
| 42 | + if runtime.GOOS == "windows" { |
| 43 | + // The bundled Windows SSH client cannot run the probe over Lima's mux socket. |
| 44 | + args = sshutil.DisableControlMasterOptsFromSSHArgs(args) |
| 45 | + } |
| 46 | + args = append(args, "-o", "ConnectTimeout=2", "-o", "ClearAllForwardings=yes", "-T", "-p", strconv.Itoa(a.sshLocalPort), a.instSSHAddress, "--", script) |
| 47 | + cmd := exec.CommandContext(ctx, a.sshConfig.Binary(), args...) |
| 48 | + hideResourceProbe(cmd) |
| 49 | + cmd.WaitDelay = time.Second |
| 50 | + return cmd |
| 51 | +} |
| 52 | |
| 53 | new file mode 100644 |
| 54 | |
| 55 | |
| 56 | |
| 57 | @@ -0,0 +1,40 @@ |
| 58 | +// SPDX-FileCopyrightText: Copyright The Lima Authors |
| 59 | +// SPDX-License-Identifier: Apache-2.0 |
| 60 | + |
| 61 | +package hostagent |
| 62 | + |
| 63 | +import ( |
| 64 | + "context" |
| 65 | + "runtime" |
| 66 | + "slices" |
| 67 | + "testing" |
| 68 | + |
| 69 | + "github.com/lima-vm/sshocker/pkg/ssh" |
| 70 | +) |
| 71 | + |
| 72 | +func TestResourceProbeCommand(t *testing.T) { |
| 73 | + agent := HostAgent{ |
| 74 | + sshLocalPort: 60022, instSSHAddress: "127.0.0.1", |
| 75 | + sshConfig: &ssh.SSHConfig{AdditionalArgs: []string{ |
| 76 | + "-o", "IdentityFile=private-instance-key", "-o", "ControlMaster=auto", |
| 77 | + "-o", "ControlPath=instance/ssh.sock", "-o", "ControlPersist=5m", |
| 78 | + }}, |
| 79 | + } |
| 80 | + configured := slices.Clone(agent.sshConfig.Args()) |
| 81 | + const probe = "cat /proc/meminfo" |
| 82 | + cmd := agent.resourceProbeCommand(context.Background(), probe) |
| 83 | + want := slices.Clone(configured) |
| 84 | + if runtime.GOOS == "windows" { |
| 85 | + want = []string{ |
| 86 | + "-o", "ControlMaster=no", "-o", "ControlPath=none", "-o", "ControlPersist=no", |
| 87 | + "-o", "IdentityFile=private-instance-key", |
| 88 | + } |
| 89 | + } |
| 90 | + want = append(want, "-o", "ConnectTimeout=2", "-o", "ClearAllForwardings=yes", "-T", "-p", "60022", "127.0.0.1", "--", probe) |
| 91 | + if !slices.Equal(cmd.Args[1:], want) { |
| 92 | + t.Fatalf("probe arguments: got %q, want %q", cmd.Args[1:], want) |
| 93 | + } |
| 94 | + if !slices.Equal(configured, agent.sshConfig.Args()) { |
| 95 | + t.Fatal("resource probe changed the SSH configuration used by other host-agent tasks") |
| 96 | + } |
| 97 | +} |