| 1 | |
| 2 | |
| 3 | |
| 4 | package hostagent |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "io" |
| 11 | "os/exec" |
| 12 | "runtime" |
| 13 | "strconv" |
| 14 | "sync" |
| 15 | "time" |
| 16 | |
| 17 | "github.com/lima-vm/lima/v2/pkg/driver" |
| 18 | "github.com/lima-vm/lima/v2/pkg/limatype" |
| 19 | "github.com/lima-vm/lima/v2/pkg/sshutil" |
| 20 | ) |
| 21 | |
| 22 | |
| 23 | |
| 24 | var ManageResources func(context.Context, string, *limatype.LimaYAML, driver.MemoryBalloon, func(context.Context, string) ([]byte, error)) func() |
| 25 | |
| 26 | func (a *HostAgent) startResourceManager(ctx context.Context) func() { |
| 27 | if ManageResources == nil { |
| 28 | return func() {} |
| 29 | } |
| 30 | d := a.driver |
| 31 | if configured, ok := d.(*driver.ConfiguredDriver); ok { |
| 32 | d = configured.Driver |
| 33 | } |
| 34 | balloon, ok := d.(driver.MemoryBalloon) |
| 35 | if !ok { |
| 36 | return func() {} |
| 37 | } |
| 38 | read := func(ctx context.Context, script string) ([]byte, error) { |
| 39 | return runResourceProbe(ctx, a.resourceProbeCommand(ctx, script)) |
| 40 | } |
| 41 | stop := ManageResources(ctx, a.instDir, a.instConfig, balloon, read) |
| 42 | var once sync.Once |
| 43 | return func() { once.Do(stop) } |
| 44 | } |
| 45 | |
| 46 | |
| 47 | |
| 48 | type resourceProbeStderr struct{ data []byte } |
| 49 | |
| 50 | func (b *resourceProbeStderr) Write(p []byte) (int, error) { |
| 51 | n := len(p) |
| 52 | if remaining := 4096 - len(b.data); remaining > 0 { |
| 53 | b.data = append(b.data, p[:min(remaining, n)]...) |
| 54 | } |
| 55 | return n, nil |
| 56 | } |
| 57 | |
| 58 | func runResourceProbe(ctx context.Context, cmd *exec.Cmd) ([]byte, error) { |
| 59 | var stderr resourceProbeStderr |
| 60 | cmd.Stderr = &stderr |
| 61 | pipe, err := cmd.StdoutPipe() |
| 62 | if err != nil { |
| 63 | return nil, fmt.Errorf("resource probe stdout: %w", err) |
| 64 | } |
| 65 | if err = cmd.Start(); err != nil { |
| 66 | return nil, fmt.Errorf("resource probe start: %w", err) |
| 67 | } |
| 68 | out, readErr := io.ReadAll(io.LimitReader(pipe, 16385)) |
| 69 | if readErr != nil || len(out) > 16384 { |
| 70 | _ = cmd.Process.Kill() |
| 71 | _ = cmd.Wait() |
| 72 | if len(out) > 16384 { |
| 73 | return nil, errors.New("resource observation exceeded its limit") |
| 74 | } |
| 75 | return nil, fmt.Errorf("resource probe read: %w; stderr=%s", errors.Join(ctx.Err(), readErr), stderr.data) |
| 76 | } |
| 77 | if err = cmd.Wait(); err != nil { |
| 78 | return nil, fmt.Errorf("resource probe execution: %w; stderr=%s", errors.Join(ctx.Err(), err), stderr.data) |
| 79 | } |
| 80 | return out, nil |
| 81 | } |
| 82 | |
| 83 | func (a *HostAgent) resourceProbeCommand(ctx context.Context, script string) *exec.Cmd { |
| 84 | args := a.sshConfig.Args() |
| 85 | if runtime.GOOS == "windows" { |
| 86 | |
| 87 | args = sshutil.DisableControlMasterOptsFromSSHArgs(args) |
| 88 | } |
| 89 | args = append(args, "-o", "ConnectTimeout=2", "-o", "ClearAllForwardings=yes", "-T", "-p", strconv.Itoa(a.sshLocalPort), a.instSSHAddress, "--", script) |
| 90 | cmd := exec.CommandContext(ctx, a.sshConfig.Binary(), args...) |
| 91 | hideResourceProbe(cmd) |
| 92 | cmd.WaitDelay = time.Second |
| 93 | return cmd |
| 94 | } |