| 1 | |
| 2 | |
| 3 | |
| 4 | package main |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "maps" |
| 11 | "os" |
| 12 | |
| 13 | "github.com/sirupsen/logrus" |
| 14 | "github.com/spf13/cobra" |
| 15 | |
| 16 | "github.com/lima-vm/lima/v2/pkg/downloader" |
| 17 | "github.com/lima-vm/lima/v2/pkg/driverutil" |
| 18 | "github.com/lima-vm/lima/v2/pkg/limatmpl" |
| 19 | "github.com/lima-vm/lima/v2/pkg/limatype" |
| 20 | "github.com/lima-vm/lima/v2/pkg/limayaml" |
| 21 | "github.com/lima-vm/lima/v2/pkg/store" |
| 22 | "github.com/lima-vm/lima/v2/pkg/templatestore" |
| 23 | ) |
| 24 | |
| 25 | func newPruneCommand() *cobra.Command { |
| 26 | pruneCommand := &cobra.Command{ |
| 27 | Use: "prune", |
| 28 | Short: "Prune garbage objects", |
| 29 | Args: WrapArgsError(cobra.NoArgs), |
| 30 | RunE: pruneAction, |
| 31 | ValidArgsFunction: cobra.NoFileCompletions, |
| 32 | GroupID: advancedCommand, |
| 33 | } |
| 34 | pruneCommand.Flags().Bool("keep-referred", false, "Keep objects that are referred by some instances or templates") |
| 35 | return pruneCommand |
| 36 | } |
| 37 | |
| 38 | func pruneAction(cmd *cobra.Command, _ []string) error { |
| 39 | ctx := cmd.Context() |
| 40 | keepReferred, err := cmd.Flags().GetBool("keep-referred") |
| 41 | if err != nil { |
| 42 | return err |
| 43 | } |
| 44 | opt := downloader.WithCache() |
| 45 | if !keepReferred { |
| 46 | return downloader.RemoveAllCacheDir(opt) |
| 47 | } |
| 48 | |
| 49 | |
| 50 | cacheEntries, err := downloader.CacheEntries(opt) |
| 51 | if err != nil { |
| 52 | return err |
| 53 | } |
| 54 | knownLocations, err := knownLocations(ctx) |
| 55 | if err != nil { |
| 56 | return err |
| 57 | } |
| 58 | for cacheKey, cachePath := range cacheEntries { |
| 59 | if file, exists := knownLocations[cacheKey]; exists { |
| 60 | logrus.Debugf("Keep %#q caching %#q", cacheKey, file.Location) |
| 61 | } else { |
| 62 | logrus.Debug("Deleting ", cacheKey) |
| 63 | if err := os.RemoveAll(cachePath); err != nil { |
| 64 | logrus.Warnf("Failed to delete %#q: %v", cacheKey, err) |
| 65 | return err |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | |
| 71 | return downloader.RemoveStaleTempFiles(opt) |
| 72 | } |
| 73 | |
| 74 | func knownLocations(ctx context.Context) (map[string]limatype.File, error) { |
| 75 | locations := make(map[string]limatype.File) |
| 76 | |
| 77 | |
| 78 | instances, err := store.Instances() |
| 79 | if err != nil { |
| 80 | return nil, err |
| 81 | } |
| 82 | for _, instanceName := range instances { |
| 83 | instance, err := store.Inspect(ctx, instanceName) |
| 84 | if err != nil { |
| 85 | if errors.Is(err, os.ErrNotExist) { |
| 86 | return nil, fmt.Errorf("cannot safely prune while instance %#q is being used: %w", instanceName, err) |
| 87 | } |
| 88 | return nil, err |
| 89 | } |
| 90 | if instance.Errors != nil { |
| 91 | logrus.Warnf("skipping instance %#q because it has errors: %v", instanceName, instance.Errors) |
| 92 | continue |
| 93 | } |
| 94 | maps.Copy(locations, locationsFromLimaYAML(instance.Config)) |
| 95 | } |
| 96 | |
| 97 | |
| 98 | templates, err := templatestore.Templates() |
| 99 | if err != nil { |
| 100 | return nil, err |
| 101 | } |
| 102 | for _, t := range templates { |
| 103 | tmpl, err := limatmpl.Read(ctx, "", t.Location) |
| 104 | if err != nil { |
| 105 | return nil, err |
| 106 | } |
| 107 | if err := tmpl.Embed(ctx, true, true); err != nil { |
| 108 | return nil, err |
| 109 | } |
| 110 | y, err := limayaml.Load(ctx, tmpl.Bytes, tmpl.Name) |
| 111 | if err != nil { |
| 112 | return nil, err |
| 113 | } |
| 114 | |
| 115 | if err := driverutil.ResolveVMType(y); err != nil { |
| 116 | return nil, err |
| 117 | } |
| 118 | maps.Copy(locations, locationsFromLimaYAML(y)) |
| 119 | } |
| 120 | return locations, nil |
| 121 | } |
| 122 | |
| 123 | func locationsFromLimaYAML(y *limatype.LimaYAML) map[string]limatype.File { |
| 124 | locations := make(map[string]limatype.File) |
| 125 | for _, f := range y.Images { |
| 126 | locations[downloader.CacheKey(f.Location)] = f.File |
| 127 | if f.Kernel != nil { |
| 128 | locations[downloader.CacheKey(f.Kernel.Location)] = f.Kernel.File |
| 129 | } |
| 130 | if f.Initrd != nil { |
| 131 | locations[downloader.CacheKey(f.Initrd.Location)] = *f.Initrd |
| 132 | } |
| 133 | } |
| 134 | for _, f := range y.Containerd.Archives { |
| 135 | locations[downloader.CacheKey(f.Location)] = f |
| 136 | } |
| 137 | for _, f := range y.Firmware.Images { |
| 138 | locations[downloader.CacheKey(f.Location)] = f.File |
| 139 | } |
| 140 | return locations |
| 141 | } |