11// SPDX-FileCopyrightText: Copyright The Lima Authors
22// SPDX-License-Identifier: Apache-2.0
33
4- package usrlocalsharelima
4+ package usrlocal
55
66import (
77 "errors"
@@ -10,12 +10,10 @@ import (
1010 "os"
1111 "os/exec"
1212 "path/filepath"
13- "runtime"
1413 "sync"
1514
1615 "github.com/sirupsen/logrus"
1716
18- "github.com/lima-vm/lima/v2/pkg/debugutil"
1917 "github.com/lima-vm/lima/v2/pkg/limatype"
2018)
2119
@@ -73,61 +71,21 @@ func SelfDirs() []string {
7371 return selfPaths
7472}
7573
76- // Dir returns the location of the <PREFIX>/lima/share directory, relative to the location
77- // of the current executable. It checks for multiple possible filesystem layouts and returns
78- // the first candidate that contains the native guest agent binary.
79- func Dir () (string , error ) {
74+ // ShareLima returns the <PREFIX>/share/lima directories.
75+ func ShareLima () ([]string , error ) {
76+ var candidates []string
8077 selfDirs := SelfDirs ()
81-
82- ostype := limatype .NewOS ("linux" )
83- arch := limatype .NewArch (runtime .GOARCH )
84- if arch == "" {
85- return "" , fmt .Errorf ("failed to get arch for %q" , runtime .GOARCH )
86- }
87-
88- gaCandidates := []string {}
8978 for _ , selfDir := range selfDirs {
9079 // selfDir: /usr/local/bin
91- selfDirDir := filepath .Dir (selfDir )
92- gaCandidates = append (gaCandidates ,
93- // candidate 0:
94- // - self: /Applications/Lima.app/Contents/MacOS/limactl
95- // - agent: /Applications/Lima.app/Contents/MacOS/lima-guestagent.Linux-x86_64
96- // - dir: /Applications/Lima.app/Contents/MacOS
97- filepath .Join (selfDir , "lima-guestagent." + ostype + "-" + arch ),
98- // candidate 1:
99- // - self: /usr/local/bin/limactl
100- // - agent: /usr/local/share/lima/lima-guestagent.Linux-x86_64
101- // - dir: /usr/local/share/lima
102- filepath .Join (selfDirDir , "share/lima/lima-guestagent." + ostype + "-" + arch ),
103- // TODO: support custom path
104- )
105- if debugutil .Debug {
106- // candidate 2: launched by `~/go/bin/dlv dap`
107- // - self: ${workspaceFolder}/cmd/limactl/__debug_bin_XXXXXX
108- // - agent: ${workspaceFolder}/_output/share/lima/lima-guestagent.Linux-x86_64
109- // - dir: ${workspaceFolder}/_output/share/lima
110- candidateForDebugBuild := filepath .Join (filepath .Dir (selfDirDir ), "_output/share/lima/lima-guestagent." + ostype + "-" + arch )
111- gaCandidates = append (gaCandidates , candidateForDebugBuild )
112- logrus .Infof ("debug mode detected, adding more guest agent candidates: %v" , candidateForDebugBuild )
113- }
114- }
115-
116- for _ , gaCandidate := range gaCandidates {
117- if _ , err := os .Stat (gaCandidate ); err == nil {
118- return filepath .Dir (gaCandidate ), nil
119- } else if ! errors .Is (err , os .ErrNotExist ) {
120- return "" , err
121- }
122- if _ , err := os .Stat (gaCandidate + ".gz" ); err == nil {
123- return filepath .Dir (gaCandidate ), nil
124- } else if ! errors .Is (err , os .ErrNotExist ) {
125- return "" , err
80+ // prefix: /usr/local
81+ // candidate: /usr/local/share/lima
82+ prefix := filepath .Dir (selfDir )
83+ candidate := filepath .Join (prefix , "share" , "lima" )
84+ if ents , err := os .ReadDir (candidate ); err == nil && len (ents ) > 0 {
85+ candidates = append (candidates , candidate )
12686 }
12787 }
128-
129- return "" , fmt .Errorf ("failed to find \" lima-guestagent.%s-%s\" binary for %v, attempted %v" ,
130- ostype , arch , selfDirs , gaCandidates )
88+ return candidates , nil
13189}
13290
13391// GuestAgentBinary returns the absolute path of the guest agent binary, possibly with ".gz" suffix.
@@ -138,18 +96,21 @@ func GuestAgentBinary(ostype limatype.OS, arch limatype.Arch) (string, error) {
13896 if arch == "" {
13997 return "" , errors .New ("arch must be set" )
14098 }
141- dir , err := Dir ()
99+ shareLimaDirs , err := ShareLima ()
142100 if err != nil {
143101 return "" , err
144102 }
145- uncomp := filepath .Join (dir , "lima-guestagent." + ostype + "-" + arch )
146- comp := uncomp + ".gz"
147- res , err := chooseGABinary ([]string {comp , uncomp })
148- if err != nil {
149- logrus .Debug (err )
150- return "" , fmt .Errorf ("guest agent binary could not be found for %s-%s: %w (Hint: try installing `lima-additional-guestagents` package)" , ostype , arch , err )
103+ for _ , dir := range shareLimaDirs {
104+ uncomp := filepath .Join (dir , "lima-guestagent." + ostype + "-" + arch )
105+ comp := uncomp + ".gz"
106+ res , err := chooseGABinary ([]string {comp , uncomp })
107+ if err != nil {
108+ logrus .Debug (err )
109+ continue
110+ }
111+ return res , nil
151112 }
152- return res , nil
113+ return "" , fmt . Errorf ( "guest agent binary could not be found for %s-%s (Hint: try installing `lima-additional-guestagents` package)" , ostype , arch )
153114}
154115
155116func chooseGABinary (candidates []string ) (string , error ) {
0 commit comments