Skip to content

Commit caced74

Browse files
authored
Merge pull request #2438 from afbjorklund/guest-gzip
Allow compressing lima-guestagent with gzip
2 parents fc41244 + 1b54ed4 commit caced74

File tree

5 files changed

+57
-22
lines changed

5 files changed

+57
-22
lines changed

Kconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,9 @@ config GUESTAGENT_ARCH_RISCV64
3030
help
3131
Build lima-guestagent for "riscv64" Arch
3232
default y
33+
34+
config GUESTAGENT_COMPRESS
35+
bool "guestagent compress"
36+
help
37+
Compress lima-guestagent
38+
default n

Makefile

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,26 @@ HELPERS = \
8686
_output/bin/podman.lima \
8787
_output/bin/kubectl.lima
8888

89+
ifeq ($(CONFIG_GUESTAGENT_COMPRESS),y)
90+
gz = .gz
91+
endif
92+
8993
ifeq ($(CONFIG_GUESTAGENT_OS_LINUX),y)
9094
ifeq ($(CONFIG_GUESTAGENT_ARCH_X8664),y)
9195
GUESTAGENT += \
92-
_output/share/lima/lima-guestagent.Linux-x86_64
96+
_output/share/lima/lima-guestagent.Linux-x86_64$(gz)
9397
endif
9498
ifeq ($(CONFIG_GUESTAGENT_ARCH_AARCH64),y)
9599
GUESTAGENT += \
96-
_output/share/lima/lima-guestagent.Linux-aarch64
100+
_output/share/lima/lima-guestagent.Linux-aarch64$(gz)
97101
endif
98102
ifeq ($(CONFIG_GUESTAGENT_ARCH_ARMV7L),y)
99103
GUESTAGENT += \
100-
_output/share/lima/lima-guestagent.Linux-armv7l
104+
_output/share/lima/lima-guestagent.Linux-armv7l$(gz)
101105
endif
102106
ifeq ($(CONFIG_GUESTAGENT_ARCH_RISCV64),y)
103107
GUESTAGENT += \
104-
_output/share/lima/lima-guestagent.Linux-riscv64
108+
_output/share/lima/lima-guestagent.Linux-riscv64$(gz)
105109
endif
106110
endif
107111

@@ -188,6 +192,9 @@ _output/share/lima/lima-guestagent.Linux-riscv64:
188192
GOOS=linux GOARCH=riscv64 CGO_ENABLED=0 $(GO_BUILD) -o $@ ./cmd/lima-guestagent
189193
chmod 644 $@
190194

195+
_output/share/lima/lima-guestagent.%.gz: _output/share/lima/lima-guestagent.%
196+
gzip $<
197+
191198
.PHONY: manpages
192199
manpages: _output/bin/limactl$(exe)
193200
@mkdir -p _output/share/man/man1

config.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ CONFIG_GUESTAGENT_ARCH_X8664=y
33
CONFIG_GUESTAGENT_ARCH_AARCH64=y
44
CONFIG_GUESTAGENT_ARCH_ARMV7L=y
55
CONFIG_GUESTAGENT_ARCH_RISCV64=y
6+
CONFIG_GUESTAGENT_COMPRESS=n

pkg/cidata/cidata.go

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cidata
22

33
import (
4+
"compress/gzip"
45
"errors"
56
"fmt"
67
"io"
@@ -341,14 +342,30 @@ func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort
341342
}
342343
}
343344

344-
guestAgentBinary, err := GuestAgentBinary(*y.OS, *y.Arch)
345+
guestAgentBinary, err := usrlocalsharelima.GuestAgentBinary(*y.OS, *y.Arch)
345346
if err != nil {
346347
return err
347348
}
348-
defer guestAgentBinary.Close()
349+
var guestAgent io.ReadCloser
350+
guestAgent, err = os.Open(guestAgentBinary)
351+
if err != nil {
352+
if !errors.Is(err, os.ErrNotExist) {
353+
return err
354+
}
355+
compressedGuestAgent, err := os.Open(guestAgentBinary + ".gz")
356+
if err != nil {
357+
return err
358+
}
359+
logrus.Debugf("Decompressing %s.gz", guestAgentBinary)
360+
guestAgent, err = gzip.NewReader(compressedGuestAgent)
361+
if err != nil {
362+
return err
363+
}
364+
}
365+
defer guestAgent.Close()
349366
layout = append(layout, iso9660util.Entry{
350367
Path: "lima-guestagent",
351-
Reader: guestAgentBinary,
368+
Reader: guestAgent,
352369
})
353370

354371
if nerdctlArchive != "" {
@@ -375,21 +392,6 @@ func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort
375392
return iso9660util.Write(filepath.Join(instDir, filenames.CIDataISO), "cidata", layout)
376393
}
377394

378-
func GuestAgentBinary(ostype limayaml.OS, arch limayaml.Arch) (io.ReadCloser, error) {
379-
if ostype == "" {
380-
return nil, errors.New("os must be set")
381-
}
382-
if arch == "" {
383-
return nil, errors.New("arch must be set")
384-
}
385-
dir, err := usrlocalsharelima.Dir()
386-
if err != nil {
387-
return nil, err
388-
}
389-
gaPath := filepath.Join(dir, "lima-guestagent."+ostype+"-"+arch)
390-
return os.Open(gaPath)
391-
}
392-
393395
func getCert(content string) Cert {
394396
lines := []string{}
395397
for _, line := range strings.Split(content, "\n") {

pkg/usrlocalsharelima/usrlocalsharelima.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,27 @@ func Dir() (string, error) {
5555
} else if !errors.Is(err, os.ErrNotExist) {
5656
return "", err
5757
}
58+
if _, err := os.Stat(gaCandidate + ".gz"); err == nil {
59+
return filepath.Dir(gaCandidate), nil
60+
} else if !errors.Is(err, os.ErrNotExist) {
61+
return "", err
62+
}
5863
}
5964

6065
return "", fmt.Errorf("failed to find \"lima-guestagent.%s-%s\" binary for %q, attempted %v",
6166
ostype, arch, self, gaCandidates)
6267
}
68+
69+
func GuestAgentBinary(ostype limayaml.OS, arch limayaml.Arch) (string, error) {
70+
if ostype == "" {
71+
return "", errors.New("os must be set")
72+
}
73+
if arch == "" {
74+
return "", errors.New("arch must be set")
75+
}
76+
dir, err := Dir()
77+
if err != nil {
78+
return "", err
79+
}
80+
return filepath.Join(dir, "lima-guestagent."+ostype+"-"+arch), nil
81+
}

0 commit comments

Comments
 (0)