Skip to content

Commit 4efb676

Browse files
committed
Don't download files that are already cached
The nerdctl archive has already been downloaded to the cache, so don't log downloading it again. Signed-off-by: Anders F Björklund <[email protected]>
1 parent beaa734 commit 4efb676

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

pkg/fileutils/download.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ func DownloadFile(dest string, f limayaml.File, decompress bool, description str
4141
return res.CachePath, nil
4242
}
4343

44+
// CachedFile checks if a file is in the cache, validating the digest if it is available. Returns path in cache.
45+
func CachedFile(f limayaml.File) (string, error) {
46+
res, err := downloader.Cached(f.Location,
47+
downloader.WithCache(),
48+
downloader.WithExpectedDigest(f.Digest))
49+
if err != nil {
50+
return "", fmt.Errorf("cache did not contain %q: %w", f.Location, err)
51+
}
52+
return res.CachePath, nil
53+
}
54+
4455
// Errors compose multiple into a single error.
4556
// Errors filters out ErrSkipped.
4657
func Errors(errs []error) error {

pkg/start/start.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,21 @@ const DefaultWatchHostAgentEventsTimeout = 10 * time.Minute
3434
// ensureNerdctlArchiveCache prefetches the nerdctl-full-VERSION-GOOS-GOARCH.tar.gz archive
3535
// into the cache before launching the hostagent process, so that we can show the progress in tty.
3636
// https://github.com/lima-vm/lima/issues/326
37-
func ensureNerdctlArchiveCache(y *limayaml.LimaYAML) (string, error) {
37+
func ensureNerdctlArchiveCache(y *limayaml.LimaYAML, created bool) (string, error) {
3838
if !*y.Containerd.System && !*y.Containerd.User {
3939
// nerdctl archive is not needed
4040
return "", nil
4141
}
4242

4343
errs := make([]error, len(y.Containerd.Archives))
4444
for i, f := range y.Containerd.Archives {
45+
// Skip downloading again if the file is already in the cache
46+
if created && f.Arch == *y.Arch && !downloader.IsLocal(f.Location) {
47+
path, err := fileutils.CachedFile(f)
48+
if err == nil {
49+
return path, nil
50+
}
51+
}
4552
path, err := fileutils.DownloadFile("", f, false, "the nerdctl archive", *y.Arch)
4653
if err != nil {
4754
errs[i] = err
@@ -80,10 +87,16 @@ func Prepare(_ context.Context, inst *store.Instance) (*Prepared, error) {
8087
return nil, err
8188
}
8289

90+
// Check if the instance has been created (the base disk already exists)
91+
created := false
92+
baseDisk := filepath.Join(inst.Dir, filenames.BaseDisk)
93+
if _, err := os.Stat(baseDisk); err == nil {
94+
created = true
95+
}
8396
if err := limaDriver.CreateDisk(); err != nil {
8497
return nil, err
8598
}
86-
nerdctlArchiveCache, err := ensureNerdctlArchiveCache(y)
99+
nerdctlArchiveCache, err := ensureNerdctlArchiveCache(y, created)
87100
if err != nil {
88101
return nil, err
89102
}

0 commit comments

Comments
 (0)