Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/limactl/guest-install.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/lima-vm/lima/v2/pkg/limatype"
"github.com/lima-vm/lima/v2/pkg/limatype/filenames"
"github.com/lima-vm/lima/v2/pkg/store"
"github.com/lima-vm/lima/v2/pkg/usrlocalsharelima"
"github.com/lima-vm/lima/v2/pkg/usrlocal"
)

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

// lima-guestagent
guestAgentBinary, err := usrlocalsharelima.GuestAgentBinary(*inst.Config.OS, *inst.Config.Arch)
guestAgentBinary, err := usrlocal.GuestAgentBinary(*inst.Config.OS, *inst.Config.Arch)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/instance/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"github.com/lima-vm/lima/v2/pkg/limatype/filenames"
"github.com/lima-vm/lima/v2/pkg/registry"
"github.com/lima-vm/lima/v2/pkg/store"
"github.com/lima-vm/lima/v2/pkg/usrlocalsharelima"
"github.com/lima-vm/lima/v2/pkg/usrlocal"
)

// DefaultWatchHostAgentEventsTimeout is the duration to wait for the instance
Expand All @@ -48,7 +48,7 @@ type Prepared struct {
func Prepare(ctx context.Context, inst *limatype.Instance, guestAgent string) (*Prepared, error) {
if !*inst.Config.Plain && guestAgent == "" {
var err error
guestAgent, err = usrlocalsharelima.GuestAgentBinary(*inst.Config.OS, *inst.Config.Arch)
guestAgent, err = usrlocal.GuestAgentBinary(*inst.Config.OS, *inst.Config.Arch)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/limainfo/limainfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/lima-vm/lima/v2/pkg/plugins"
"github.com/lima-vm/lima/v2/pkg/registry"
"github.com/lima-vm/lima/v2/pkg/templatestore"
"github.com/lima-vm/lima/v2/pkg/usrlocalsharelima"
"github.com/lima-vm/lima/v2/pkg/usrlocal"
"github.com/lima-vm/lima/v2/pkg/version"
)

Expand Down Expand Up @@ -97,7 +97,7 @@ func New(ctx context.Context) (*LimaInfo, error) {
}
info.IdentityFile = filepath.Join(configDir, filenames.UserPrivateKey)
for _, arch := range limatype.ArchTypes {
bin, err := usrlocalsharelima.GuestAgentBinary(limatype.LINUX, arch)
bin, err := usrlocal.GuestAgentBinary(limatype.LINUX, arch)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
logrus.WithError(err).Debugf("Failed to resolve the guest agent binary for %q", arch)
Expand Down
6 changes: 3 additions & 3 deletions pkg/plugins/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/sirupsen/logrus"

"github.com/lima-vm/lima/v2/pkg/osutil"
"github.com/lima-vm/lima/v2/pkg/usrlocalsharelima"
"github.com/lima-vm/lima/v2/pkg/usrlocal"
)

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

var getPluginDirectories = sync.OnceValue(func() []string {
dirs := usrlocalsharelima.SelfDirs()
dirs := usrlocal.SelfDirs()

pathEnv := os.Getenv("PATH")
if pathEnv != "" {
pathDirs := filepath.SplitList(pathEnv)
dirs = append(dirs, pathDirs...)
}

libexecDirs, err := usrlocalsharelima.LibexecLima()
libexecDirs, err := usrlocal.LibexecLima()
if err == nil {
dirs = append(dirs, libexecDirs...)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

"github.com/lima-vm/lima/v2/pkg/driver"
"github.com/lima-vm/lima/v2/pkg/driver/external/client"
"github.com/lima-vm/lima/v2/pkg/usrlocalsharelima"
"github.com/lima-vm/lima/v2/pkg/usrlocal"
)

const (
Expand Down Expand Up @@ -124,7 +124,7 @@ func discoverDrivers() error {
}
}

stdDriverDirs, err := usrlocalsharelima.LibexecLima()
stdDriverDirs, err := usrlocal.LibexecLima()
if err != nil {
return err
}
Expand Down
13 changes: 7 additions & 6 deletions pkg/templatestore/templatestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"unicode"

"github.com/lima-vm/lima/v2/pkg/limatype/dirnames"
"github.com/lima-vm/lima/v2/pkg/usrlocalsharelima"
"github.com/lima-vm/lima/v2/pkg/usrlocal"
)

type Template struct {
Expand All @@ -31,14 +31,15 @@ func templatesPaths() ([]string, error) {
if err != nil {
return nil, err
}
shareDir, err := usrlocalsharelima.Dir()
shareLimaDirs, err := usrlocal.ShareLima()
if err != nil {
return nil, err
}
return []string{
limaTemplatesDir,
filepath.Join(shareDir, "templates"),
}, nil
res := []string{limaTemplatesDir}
for _, shareLimaDir := range shareLimaDirs {
res = append(res, filepath.Join(shareLimaDir, "templates"))
}
return res, nil
}

// Read searches for template `name` in all template directories and returns the
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-FileCopyrightText: Copyright The Lima Authors
// SPDX-License-Identifier: Apache-2.0

package usrlocalsharelima
package usrlocal

import (
"errors"
Expand All @@ -10,7 +10,7 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -73,61 +73,57 @@ func SelfDirs() []string {
return selfPaths
}

// Dir returns the location of the <PREFIX>/lima/share directory, relative to the location
// of the current executable. It checks for multiple possible filesystem layouts and returns
// the first candidate that contains the native guest agent binary.
func Dir() (string, error) {
selfDirs := SelfDirs()
func delveDebugExe() string {
exe, err := os.Executable()
if err != nil {
return ""
}
exeBase := filepath.Base(exe)
if strings.HasPrefix(exeBase, "__debug_bin") {
return exe
}
return ""
}

ostype := limatype.NewOS("linux")
arch := limatype.NewArch(runtime.GOARCH)
if arch == "" {
return "", fmt.Errorf("failed to get arch for %q", runtime.GOARCH)
func delveWorkspace() string {
self := delveDebugExe()
if self == "" {
return ""
}
// https://github.com/lima-vm/lima/pull/2651/commits/644c11373cb79aaebd8520706f7d51bd3ee5fbe4
// launched by `~/go/bin/dlv dap`
// - self: ${workspaceFolder}/cmd/limactl/__debug_bin_XXXXXX
return filepath.Dir(filepath.Dir(filepath.Dir(self)))
}

gaCandidates := []string{}
// ShareLima returns the <PREFIX>/share/lima directories.
func ShareLima() ([]string, error) {
var candidates []string
selfDirs := SelfDirs()
for _, selfDir := range selfDirs {
// selfDir: /usr/local/bin
selfDirDir := filepath.Dir(selfDir)
gaCandidates = append(gaCandidates,
// candidate 0:
// - self: /Applications/Lima.app/Contents/MacOS/limactl
// - agent: /Applications/Lima.app/Contents/MacOS/lima-guestagent.Linux-x86_64
// - dir: /Applications/Lima.app/Contents/MacOS
filepath.Join(selfDir, "lima-guestagent."+ostype+"-"+arch),
// candidate 1:
// - self: /usr/local/bin/limactl
// - agent: /usr/local/share/lima/lima-guestagent.Linux-x86_64
// - dir: /usr/local/share/lima
filepath.Join(selfDirDir, "share/lima/lima-guestagent."+ostype+"-"+arch),
// TODO: support custom path
)
if debugutil.Debug {
// candidate 2: launched by `~/go/bin/dlv dap`
// prefix: /usr/local
// candidate: /usr/local/share/lima
prefix := filepath.Dir(selfDir)
candidate := filepath.Join(prefix, "share", "lima")
if ents, err := os.ReadDir(candidate); err == nil && len(ents) > 0 {
candidates = append(candidates, candidate)
}
}
if debugutil.Debug {
if workspace := delveWorkspace(); workspace != "" {
// https://github.com/lima-vm/lima/pull/2651/commits/644c11373cb79aaebd8520706f7d51bd3ee5fbe4
// launched by `~/go/bin/dlv dap`
// - self: ${workspaceFolder}/cmd/limactl/__debug_bin_XXXXXX
// - agent: ${workspaceFolder}/_output/share/lima/lima-guestagent.Linux-x86_64
// - dir: ${workspaceFolder}/_output/share/lima
candidateForDebugBuild := filepath.Join(filepath.Dir(selfDirDir), "_output/share/lima/lima-guestagent."+ostype+"-"+arch)
gaCandidates = append(gaCandidates, candidateForDebugBuild)
logrus.Infof("debug mode detected, adding more guest agent candidates: %v", candidateForDebugBuild)
}
}

for _, gaCandidate := range gaCandidates {
if _, err := os.Stat(gaCandidate); err == nil {
return filepath.Dir(gaCandidate), nil
} else if !errors.Is(err, os.ErrNotExist) {
return "", err
}
if _, err := os.Stat(gaCandidate + ".gz"); err == nil {
return filepath.Dir(gaCandidate), nil
} else if !errors.Is(err, os.ErrNotExist) {
return "", err
candidate := filepath.Join(workspace, "_output", "share", "lima")
if ents, err := os.ReadDir(candidate); err == nil && len(ents) > 0 {
candidates = append(candidates, candidate)
}
}
}

return "", fmt.Errorf("failed to find \"lima-guestagent.%s-%s\" binary for %v, attempted %v",
ostype, arch, selfDirs, gaCandidates)
return candidates, nil
}

// GuestAgentBinary returns the absolute path of the guest agent binary, possibly with ".gz" suffix.
Expand All @@ -138,18 +134,26 @@ func GuestAgentBinary(ostype limatype.OS, arch limatype.Arch) (string, error) {
if arch == "" {
return "", errors.New("arch must be set")
}
dir, err := Dir()
shareLimaDirs, err := ShareLima()
if err != nil {
return "", err
}
uncomp := filepath.Join(dir, "lima-guestagent."+ostype+"-"+arch)
comp := uncomp + ".gz"
res, err := chooseGABinary([]string{comp, uncomp})
if err != nil {
logrus.Debug(err)
return "", fmt.Errorf("guest agent binary could not be found for %s-%s: %w (Hint: try installing `lima-additional-guestagents` package)", ostype, arch, err)
for _, dir := range shareLimaDirs {
uncomp := filepath.Join(dir, "lima-guestagent."+ostype+"-"+arch)
comp := uncomp + ".gz"
var res string
res, err = chooseGABinary([]string{comp, uncomp})
if err != nil {
logrus.Debug(err)
continue
}
return res, nil
}
if err == nil {
// caller expects err to be comparable to fs.ErrNotExist
err = fs.ErrNotExist
}
return res, nil
return "", fmt.Errorf("guest agent binary could not be found for %s-%s: %w (Hint: try installing `lima-additional-guestagents` package)", ostype, arch, err)
}

func chooseGABinary(candidates []string) (string, error) {
Expand Down Expand Up @@ -194,12 +198,20 @@ func LibexecLima() ([]string, error) {
// candidate: /opt/homebrew/lib/lima
//
// Note that there is no /opt/homebrew/libexec directory,
// as Homebrew preserves libexec for private use.
// as Homebrew reserves libexec for private use.
// https://github.com/lima-vm/lima/issues/4295#issuecomment-3490680651
candidate = filepath.Join(prefix, "lib", "lima")
if ents, err := os.ReadDir(candidate); err == nil && len(ents) > 0 {
candidates = append(candidates, candidate)
}
}
if debugutil.Debug {
if workspace := delveWorkspace(); workspace != "" {
candidate := filepath.Join(workspace, "_output", "libexec", "lima")
if ents, err := os.ReadDir(candidate); err == nil && len(ents) > 0 {
candidates = append(candidates, candidate)
}
}
}
return candidates, nil
}
Loading