Skip to content

Commit

Permalink
Do not prune template-referenced downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
jay7x committed Mar 9, 2023
1 parent b20b8af commit 58d8ef6
Showing 1 changed file with 47 additions and 2 deletions.
49 changes: 47 additions & 2 deletions cmd/limactl/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"path/filepath"
"strings"

"github.com/lima-vm/lima/pkg/limayaml"
"github.com/lima-vm/lima/pkg/store"
"github.com/lima-vm/lima/pkg/templatestore"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -75,11 +77,21 @@ func pruneDownloadsAction(cmd *cobra.Command, args []string) error {

logrus.Infof("Pruning unreferenced downloads in %q", downloadsDir)

refrd, err := getReferencedDigests()
refrd, err := getVMReferencedDigests()
if err != nil {
return err
}

tmplRefrd, err := getTemplateReferencedDigests()
if err != nil {
return err
}

// merge template-referenced digests into the VM-referenced
for k, v := range tmplRefrd {
refrd[k] = v
}

cached, err := getCachedDigests(downloadsDir)
if err != nil {
return err
Expand Down Expand Up @@ -115,7 +127,7 @@ func makeDigest(s string) string {
}

// Collect all downloads referenced in VM definitions
func getReferencedDigests() (map[string]string, error) {
func getVMReferencedDigests() (map[string]string, error) {
digests := make(map[string]string)
instanceNames, err := store.Instances()
if err != nil {
Expand All @@ -142,6 +154,39 @@ func getReferencedDigests() (map[string]string, error) {
return digests, nil
}

// Collect all downloads referenced in bundled templates
func getTemplateReferencedDigests() (map[string]string, error) {
digests := make(map[string]string)

if templates, err := templatestore.Templates(); err == nil {
for _, t := range templates {
yBytes, err := templatestore.Read(t.Name)
if err != nil {
logrus.WithError(err).Errorf("Cannot find template %q. Skipping...", t.Name)
continue
}
config, err := limayaml.Load(yBytes, t.Location)
if err != nil {
logrus.WithError(err).Errorf("Cannot parse template %q. Skipping...", t.Name)
continue
}
for _, img := range config.Images {
digests[makeDigest(img.File.Location)] = img.File.Location
if img.Kernel != nil {
digests[makeDigest(img.Kernel.File.Location)] = img.Kernel.File.Location
}
if img.Initrd != nil {
digests[makeDigest((*img.Initrd).Location)] = (*img.Initrd).Location
}
}
for _, archive := range config.Containerd.Archives {
digests[makeDigest(archive.Location)] = archive.Location
}
}
}
return digests, nil
}

// Collect all cached downloads
func getCachedDigests(downloadsDir string) (map[string]string, error) {
digests := make(map[string]string)
Expand Down

0 comments on commit 58d8ef6

Please sign in to comment.