Skip to content

Commit 8ff7aa2

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 c7d4c8a commit 8ff7aa2

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

cmd/limactl/start.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ func createAction(cmd *cobra.Command, args []string) error {
468468
if len(inst.Errors) > 0 {
469469
return fmt.Errorf("errors inspecting instance: %+v", inst.Errors)
470470
}
471-
if _, err = start.Prepare(cmd.Context(), inst); err != nil {
471+
if _, err = start.Prepare(cmd.Context(), inst, false); err != nil {
472472
return err
473473
}
474474
logrus.Infof("Run `limactl start %s` to start the instance.", inst.Name)

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 returns path in cache.
45+
func CachedFile(f limayaml.File) (string, error) {
46+
path, 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 path, 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: 11 additions & 4 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, start 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 start && 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
@@ -65,7 +72,7 @@ type Prepared struct {
6572
}
6673

6774
// Prepare ensures the disk, the nerdctl archive, etc.
68-
func Prepare(_ context.Context, inst *store.Instance) (*Prepared, error) {
75+
func Prepare(_ context.Context, inst *store.Instance, start bool) (*Prepared, error) {
6976
y, err := inst.LoadYAML()
7077
if err != nil {
7178
return nil, err
@@ -83,7 +90,7 @@ func Prepare(_ context.Context, inst *store.Instance) (*Prepared, error) {
8390
if err := limaDriver.CreateDisk(); err != nil {
8491
return nil, err
8592
}
86-
nerdctlArchiveCache, err := ensureNerdctlArchiveCache(y)
93+
nerdctlArchiveCache, err := ensureNerdctlArchiveCache(y, start)
8794
if err != nil {
8895
return nil, err
8996
}
@@ -116,7 +123,7 @@ func Start(ctx context.Context, inst *store.Instance) error {
116123
}
117124
}
118125

119-
prepared, err := Prepare(ctx, inst)
126+
prepared, err := Prepare(ctx, inst, true)
120127
if err != nil {
121128
return err
122129
}

0 commit comments

Comments
 (0)