| 1 | |
| 2 | |
| 3 | |
| 4 | package usrlocal |
| 5 | |
| 6 | import ( |
| 7 | "errors" |
| 8 | "fmt" |
| 9 | "io/fs" |
| 10 | "os" |
| 11 | "os/exec" |
| 12 | "path/filepath" |
| 13 | "strings" |
| 14 | "sync" |
| 15 | |
| 16 | "github.com/sirupsen/logrus" |
| 17 | |
| 18 | "github.com/lima-vm/lima/v2/pkg/debugutil" |
| 19 | "github.com/lima-vm/lima/v2/pkg/limatype" |
| 20 | ) |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | var ExecutableViaArgs0 = sync.OnceValues(func() (string, error) { |
| 30 | if os.Args[0] == "" { |
| 31 | return "", errors.New("os.Args[0] has not been set") |
| 32 | } |
| 33 | executable, err := exec.LookPath(os.Args[0]) |
| 34 | if err == nil { |
| 35 | executable, err = filepath.Abs(executable) |
| 36 | } |
| 37 | if err != nil { |
| 38 | return "", fmt.Errorf("os.Args[0] is invalid: %w", err) |
| 39 | } |
| 40 | |
| 41 | return executable, nil |
| 42 | }) |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | func SelfDirs() []string { |
| 48 | var selfPaths []string |
| 49 | |
| 50 | selfViaArgs0, err := ExecutableViaArgs0() |
| 51 | if err != nil { |
| 52 | logrus.WithError(err).Warn("failed to find executable from os.Args[0]") |
| 53 | } else { |
| 54 | selfPaths = append(selfPaths, filepath.Dir(selfViaArgs0)) |
| 55 | } |
| 56 | |
| 57 | selfViaOS, err := os.Executable() |
| 58 | if err != nil { |
| 59 | logrus.WithError(err).Warn("failed to find os.Executable()") |
| 60 | } else { |
| 61 | selfFinalPathViaOS, err := filepath.EvalSymlinks(selfViaOS) |
| 62 | if err != nil { |
| 63 | logrus.WithError(err).Warn("failed to resolve symlinks") |
| 64 | selfFinalPathViaOS = selfViaOS |
| 65 | } |
| 66 | |
| 67 | selfDir := filepath.Dir(selfFinalPathViaOS) |
| 68 | if len(selfPaths) == 0 || selfDir != selfPaths[0] { |
| 69 | selfPaths = append(selfPaths, selfDir) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | return selfPaths |
| 74 | } |
| 75 | |
| 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 | } |
| 87 | |
| 88 | func delveWorkspace() string { |
| 89 | self := delveDebugExe() |
| 90 | if self == "" { |
| 91 | return "" |
| 92 | } |
| 93 | |
| 94 | |
| 95 | |
| 96 | return filepath.Dir(filepath.Dir(filepath.Dir(self))) |
| 97 | } |
| 98 | |
| 99 | |
| 100 | func ShareLima() ([]string, error) { |
| 101 | var candidates []string |
| 102 | selfDirs := SelfDirs() |
| 103 | for _, selfDir := range selfDirs { |
| 104 | |
| 105 | |
| 106 | |
| 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 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 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 | } |
| 124 | } |
| 125 | } |
| 126 | return candidates, nil |
| 127 | } |
| 128 | |
| 129 | |
| 130 | func GuestAgentBinary(ostype limatype.OS, arch limatype.Arch) (string, error) { |
| 131 | if ostype == "" { |
| 132 | return "", errors.New("os must be set") |
| 133 | } |
| 134 | if arch == "" { |
| 135 | return "", errors.New("arch must be set") |
| 136 | } |
| 137 | shareLimaDirs, err := ShareLima() |
| 138 | if err != nil { |
| 139 | return "", err |
| 140 | } |
| 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 | |
| 154 | err = fs.ErrNotExist |
| 155 | } |
| 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) |
| 157 | } |
| 158 | |
| 159 | func chooseGABinary(candidates []string) (string, error) { |
| 160 | var entries []string |
| 161 | for _, f := range candidates { |
| 162 | if _, err := os.Stat(f); err != nil { |
| 163 | if !errors.Is(err, fs.ErrNotExist) { |
| 164 | logrus.WithError(err).Warnf("failed to stat %#q", f) |
| 165 | } |
| 166 | continue |
| 167 | } |
| 168 | entries = append(entries, f) |
| 169 | } |
| 170 | switch len(entries) { |
| 171 | case 0: |
| 172 | return "", fmt.Errorf("%w: attempted %v", fs.ErrNotExist, candidates) |
| 173 | case 1: |
| 174 | return entries[0], nil |
| 175 | default: |
| 176 | logrus.Warnf("multiple files found, choosing %#q from %v; consider removing the other ones", |
| 177 | entries[0], candidates) |
| 178 | return entries[0], nil |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | |
| 183 | |
| 184 | func LibexecLima() ([]string, error) { |
| 185 | var candidates []string |
| 186 | selfDirs := SelfDirs() |
| 187 | for _, selfDir := range selfDirs { |
| 188 | |
| 189 | |
| 190 | |
| 191 | prefix := filepath.Dir(selfDir) |
| 192 | candidate := filepath.Join(prefix, "libexec", "lima") |
| 193 | if ents, err := os.ReadDir(candidate); err == nil && len(ents) > 0 { |
| 194 | candidates = append(candidates, candidate) |
| 195 | } |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | candidate = filepath.Join(prefix, "lib", "lima") |
| 204 | if ents, err := os.ReadDir(candidate); err == nil && len(ents) > 0 { |
| 205 | candidates = append(candidates, candidate) |
| 206 | } |
| 207 | } |
| 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 | } |
| 216 | return candidates, nil |
| 217 | } |