Skip to content

Commit 3d36eb1

Browse files
committed
guestagent: prioritize BIN.gz over BIN
When both `lima-guestagent.Linux-<ARCH>` and `lima-guestagent.Linux-<ARCH>.gz` are present, the compressed one is chosen with a warning. Preparation for issue 3325 Signed-off-by: Akihiro Suda <[email protected]>
1 parent d33a05d commit 3d36eb1

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

pkg/cidata/cidata.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -396,21 +396,24 @@ func GenerateISO9660(instDir, name string, instConfig *limayaml.LimaYAML, udpDNS
396396
return err
397397
}
398398
var guestAgent io.ReadCloser
399-
guestAgent, err = os.Open(guestAgentBinary)
400-
if err != nil {
401-
if !errors.Is(err, os.ErrNotExist) {
399+
if strings.HasSuffix(guestAgentBinary, ".gz") {
400+
logrus.Debugf("Decompressing %s", guestAgentBinary)
401+
guestAgentGz, err := os.Open(guestAgentBinary)
402+
if err != nil {
402403
return err
403404
}
404-
compressedGuestAgent, err := os.Open(guestAgentBinary + ".gz")
405+
defer guestAgentGz.Close()
406+
guestAgent, err = gzip.NewReader(guestAgentGz)
405407
if err != nil {
406408
return err
407409
}
408-
logrus.Debugf("Decompressing %s.gz", guestAgentBinary)
409-
guestAgent, err = gzip.NewReader(compressedGuestAgent)
410+
} else {
411+
guestAgent, err = os.Open(guestAgentBinary)
410412
if err != nil {
411413
return err
412414
}
413415
}
416+
414417
defer guestAgent.Close()
415418
layout = append(layout, iso9660util.Entry{
416419
Path: "lima-guestagent",

pkg/usrlocalsharelima/usrlocalsharelima.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ func Dir() (string, error) {
8080
ostype, arch, self, gaCandidates)
8181
}
8282

83+
// GuestAgentBinary returns the guest agent binary, possibly with ".gz" suffix.
8384
func GuestAgentBinary(ostype limayaml.OS, arch limayaml.Arch) (string, error) {
8485
if ostype == "" {
8586
return "", errors.New("os must be set")
@@ -91,5 +92,35 @@ func GuestAgentBinary(ostype limayaml.OS, arch limayaml.Arch) (string, error) {
9192
if err != nil {
9293
return "", err
9394
}
94-
return filepath.Join(dir, "lima-guestagent."+ostype+"-"+arch), nil
95+
uncomp := filepath.Join(dir, "lima-guestagent."+ostype+"-"+arch)
96+
comp := uncomp + ".gz"
97+
res, err := chooseGABinary([]string{comp, uncomp})
98+
if err != nil {
99+
logrus.Debug(err)
100+
return "", fmt.Errorf("guest agent binary could not be found for %s-%s (Hint: try installing `lima-additional-guestagents` package)", ostype, arch)
101+
}
102+
return res, nil
103+
}
104+
105+
func chooseGABinary(candidates []string) (string, error) {
106+
var entries []string
107+
for _, f := range candidates {
108+
if _, err := os.Stat(f); err != nil {
109+
if !errors.Is(err, fs.ErrNotExist) {
110+
logrus.WithError(err).Warnf("failed to stat %q", f)
111+
}
112+
continue
113+
}
114+
entries = append(entries, f)
115+
}
116+
switch len(entries) {
117+
case 0:
118+
return "", fmt.Errorf("%w: attempted %v", fs.ErrNotExist, candidates)
119+
case 1:
120+
return entries[0], nil
121+
default:
122+
logrus.Warnf("multiple files found, choosing %q from %v; consider removing the other ones",
123+
entries[0], candidates)
124+
return entries[0], nil
125+
}
95126
}

0 commit comments

Comments
 (0)