Skip to content

Commit 74ff62d

Browse files
authored
Merge pull request #4298 from AkihiroSuda/pkg-usrlocalsharelima-simplify
pkg/usrlocalsharelima: simplify and allow multiple paths, etc.
2 parents 6c1d2f7 + 608928d commit 74ff62d

File tree

7 files changed

+86
-73
lines changed

7 files changed

+86
-73
lines changed

cmd/limactl/guest-install.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"github.com/lima-vm/lima/v2/pkg/limatype"
2222
"github.com/lima-vm/lima/v2/pkg/limatype/filenames"
2323
"github.com/lima-vm/lima/v2/pkg/store"
24-
"github.com/lima-vm/lima/v2/pkg/usrlocalsharelima"
24+
"github.com/lima-vm/lima/v2/pkg/usrlocal"
2525
)
2626

2727
func newGuestInstallCommand() *cobra.Command {
@@ -81,7 +81,7 @@ func guestInstallAction(cmd *cobra.Command, args []string) error {
8181
prefix := *inst.Config.GuestInstallPrefix
8282

8383
// lima-guestagent
84-
guestAgentBinary, err := usrlocalsharelima.GuestAgentBinary(*inst.Config.OS, *inst.Config.Arch)
84+
guestAgentBinary, err := usrlocal.GuestAgentBinary(*inst.Config.OS, *inst.Config.Arch)
8585
if err != nil {
8686
return err
8787
}

pkg/instance/start.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
"github.com/lima-vm/lima/v2/pkg/limatype/filenames"
3232
"github.com/lima-vm/lima/v2/pkg/registry"
3333
"github.com/lima-vm/lima/v2/pkg/store"
34-
"github.com/lima-vm/lima/v2/pkg/usrlocalsharelima"
34+
"github.com/lima-vm/lima/v2/pkg/usrlocal"
3535
)
3636

3737
// DefaultWatchHostAgentEventsTimeout is the duration to wait for the instance
@@ -48,7 +48,7 @@ type Prepared struct {
4848
func Prepare(ctx context.Context, inst *limatype.Instance, guestAgent string) (*Prepared, error) {
4949
if !*inst.Config.Plain && guestAgent == "" {
5050
var err error
51-
guestAgent, err = usrlocalsharelima.GuestAgentBinary(*inst.Config.OS, *inst.Config.Arch)
51+
guestAgent, err = usrlocal.GuestAgentBinary(*inst.Config.OS, *inst.Config.Arch)
5252
if err != nil {
5353
return nil, err
5454
}

pkg/limainfo/limainfo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"github.com/lima-vm/lima/v2/pkg/plugins"
2121
"github.com/lima-vm/lima/v2/pkg/registry"
2222
"github.com/lima-vm/lima/v2/pkg/templatestore"
23-
"github.com/lima-vm/lima/v2/pkg/usrlocalsharelima"
23+
"github.com/lima-vm/lima/v2/pkg/usrlocal"
2424
"github.com/lima-vm/lima/v2/pkg/version"
2525
)
2626

@@ -97,7 +97,7 @@ func New(ctx context.Context) (*LimaInfo, error) {
9797
}
9898
info.IdentityFile = filepath.Join(configDir, filenames.UserPrivateKey)
9999
for _, arch := range limatype.ArchTypes {
100-
bin, err := usrlocalsharelima.GuestAgentBinary(limatype.LINUX, arch)
100+
bin, err := usrlocal.GuestAgentBinary(limatype.LINUX, arch)
101101
if err != nil {
102102
if errors.Is(err, fs.ErrNotExist) {
103103
logrus.WithError(err).Debugf("Failed to resolve the guest agent binary for %q", arch)

pkg/plugins/plugins.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"github.com/sirupsen/logrus"
1919

2020
"github.com/lima-vm/lima/v2/pkg/osutil"
21-
"github.com/lima-vm/lima/v2/pkg/usrlocalsharelima"
21+
"github.com/lima-vm/lima/v2/pkg/usrlocal"
2222
)
2323

2424
const defaultPathExt = ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL"
@@ -51,15 +51,15 @@ var Discover = sync.OnceValues(func() ([]Plugin, error) {
5151
})
5252

5353
var getPluginDirectories = sync.OnceValue(func() []string {
54-
dirs := usrlocalsharelima.SelfDirs()
54+
dirs := usrlocal.SelfDirs()
5555

5656
pathEnv := os.Getenv("PATH")
5757
if pathEnv != "" {
5858
pathDirs := filepath.SplitList(pathEnv)
5959
dirs = append(dirs, pathDirs...)
6060
}
6161

62-
libexecDirs, err := usrlocalsharelima.LibexecLima()
62+
libexecDirs, err := usrlocal.LibexecLima()
6363
if err == nil {
6464
dirs = append(dirs, libexecDirs...)
6565
}

pkg/registry/registry.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616

1717
"github.com/lima-vm/lima/v2/pkg/driver"
1818
"github.com/lima-vm/lima/v2/pkg/driver/external/client"
19-
"github.com/lima-vm/lima/v2/pkg/usrlocalsharelima"
19+
"github.com/lima-vm/lima/v2/pkg/usrlocal"
2020
)
2121

2222
const (
@@ -124,7 +124,7 @@ func discoverDrivers() error {
124124
}
125125
}
126126

127-
stdDriverDirs, err := usrlocalsharelima.LibexecLima()
127+
stdDriverDirs, err := usrlocal.LibexecLima()
128128
if err != nil {
129129
return err
130130
}

pkg/templatestore/templatestore.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"unicode"
1616

1717
"github.com/lima-vm/lima/v2/pkg/limatype/dirnames"
18-
"github.com/lima-vm/lima/v2/pkg/usrlocalsharelima"
18+
"github.com/lima-vm/lima/v2/pkg/usrlocal"
1919
)
2020

2121
type Template struct {
@@ -31,14 +31,15 @@ func templatesPaths() ([]string, error) {
3131
if err != nil {
3232
return nil, err
3333
}
34-
shareDir, err := usrlocalsharelima.Dir()
34+
shareLimaDirs, err := usrlocal.ShareLima()
3535
if err != nil {
3636
return nil, err
3737
}
38-
return []string{
39-
limaTemplatesDir,
40-
filepath.Join(shareDir, "templates"),
41-
}, nil
38+
res := []string{limaTemplatesDir}
39+
for _, shareLimaDir := range shareLimaDirs {
40+
res = append(res, filepath.Join(shareLimaDir, "templates"))
41+
}
42+
return res, nil
4243
}
4344

4445
// Read searches for template `name` in all template directories and returns the

pkg/usrlocalsharelima/usrlocalsharelima.go renamed to pkg/usrlocal/usrlocal.go

Lines changed: 68 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-FileCopyrightText: Copyright The Lima Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package usrlocalsharelima
4+
package usrlocal
55

66
import (
77
"errors"
@@ -10,7 +10,7 @@ import (
1010
"os"
1111
"os/exec"
1212
"path/filepath"
13-
"runtime"
13+
"strings"
1414
"sync"
1515

1616
"github.com/sirupsen/logrus"
@@ -73,61 +73,57 @@ func SelfDirs() []string {
7373
return selfPaths
7474
}
7575

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) {
80-
selfDirs := SelfDirs()
76+
func delveDebugExe() string {
77+
exe, err := os.Executable()
78+
if err != nil {
79+
return ""
80+
}
81+
exeBase := filepath.Base(exe)
82+
if strings.HasPrefix(exeBase, "__debug_bin") {
83+
return exe
84+
}
85+
return ""
86+
}
8187

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)
88+
func delveWorkspace() string {
89+
self := delveDebugExe()
90+
if self == "" {
91+
return ""
8692
}
93+
// https://github.com/lima-vm/lima/pull/2651/commits/644c11373cb79aaebd8520706f7d51bd3ee5fbe4
94+
// launched by `~/go/bin/dlv dap`
95+
// - self: ${workspaceFolder}/cmd/limactl/__debug_bin_XXXXXX
96+
return filepath.Dir(filepath.Dir(filepath.Dir(self)))
97+
}
8798

88-
gaCandidates := []string{}
99+
// ShareLima returns the <PREFIX>/share/lima directories.
100+
func ShareLima() ([]string, error) {
101+
var candidates []string
102+
selfDirs := SelfDirs()
89103
for _, selfDir := range selfDirs {
90104
// 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`
105+
// prefix: /usr/local
106+
// candidate: /usr/local/share/lima
107+
prefix := filepath.Dir(selfDir)
108+
candidate := filepath.Join(prefix, "share", "lima")
109+
if ents, err := os.ReadDir(candidate); err == nil && len(ents) > 0 {
110+
candidates = append(candidates, candidate)
111+
}
112+
}
113+
if debugutil.Debug {
114+
if workspace := delveWorkspace(); workspace != "" {
115+
// https://github.com/lima-vm/lima/pull/2651/commits/644c11373cb79aaebd8520706f7d51bd3ee5fbe4
116+
// launched by `~/go/bin/dlv dap`
107117
// - self: ${workspaceFolder}/cmd/limactl/__debug_bin_XXXXXX
108118
// - agent: ${workspaceFolder}/_output/share/lima/lima-guestagent.Linux-x86_64
109119
// - 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
120+
candidate := filepath.Join(workspace, "_output", "share", "lima")
121+
if ents, err := os.ReadDir(candidate); err == nil && len(ents) > 0 {
122+
candidates = append(candidates, candidate)
123+
}
126124
}
127125
}
128-
129-
return "", fmt.Errorf("failed to find \"lima-guestagent.%s-%s\" binary for %v, attempted %v",
130-
ostype, arch, selfDirs, gaCandidates)
126+
return candidates, nil
131127
}
132128

133129
// GuestAgentBinary returns the absolute path of the guest agent binary, possibly with ".gz" suffix.
@@ -138,18 +134,26 @@ func GuestAgentBinary(ostype limatype.OS, arch limatype.Arch) (string, error) {
138134
if arch == "" {
139135
return "", errors.New("arch must be set")
140136
}
141-
dir, err := Dir()
137+
shareLimaDirs, err := ShareLima()
142138
if err != nil {
143139
return "", err
144140
}
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)
141+
for _, dir := range shareLimaDirs {
142+
uncomp := filepath.Join(dir, "lima-guestagent."+ostype+"-"+arch)
143+
comp := uncomp + ".gz"
144+
var res string
145+
res, err = chooseGABinary([]string{comp, uncomp})
146+
if err != nil {
147+
logrus.Debug(err)
148+
continue
149+
}
150+
return res, nil
151+
}
152+
if err == nil {
153+
// caller expects err to be comparable to fs.ErrNotExist
154+
err = fs.ErrNotExist
151155
}
152-
return res, nil
156+
return "", fmt.Errorf("guest agent binary could not be found for %s-%s: %w (Hint: try installing `lima-additional-guestagents` package)", ostype, arch, err)
153157
}
154158

155159
func chooseGABinary(candidates []string) (string, error) {
@@ -194,12 +198,20 @@ func LibexecLima() ([]string, error) {
194198
// candidate: /opt/homebrew/lib/lima
195199
//
196200
// Note that there is no /opt/homebrew/libexec directory,
197-
// as Homebrew preserves libexec for private use.
201+
// as Homebrew reserves libexec for private use.
198202
// https://github.com/lima-vm/lima/issues/4295#issuecomment-3490680651
199203
candidate = filepath.Join(prefix, "lib", "lima")
200204
if ents, err := os.ReadDir(candidate); err == nil && len(ents) > 0 {
201205
candidates = append(candidates, candidate)
202206
}
203207
}
208+
if debugutil.Debug {
209+
if workspace := delveWorkspace(); workspace != "" {
210+
candidate := filepath.Join(workspace, "_output", "libexec", "lima")
211+
if ents, err := os.ReadDir(candidate); err == nil && len(ents) > 0 {
212+
candidates = append(candidates, candidate)
213+
}
214+
}
215+
}
204216
return candidates, nil
205217
}

0 commit comments

Comments
 (0)