| 1 | |
| 2 | |
| 3 | |
| 4 | package main |
| 5 | |
| 6 | import ( |
| 7 | "errors" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | |
| 12 | "github.com/sirupsen/logrus" |
| 13 | "github.com/spf13/cobra" |
| 14 | |
| 15 | "github.com/lima-vm/lima/v2/pkg/cidata" |
| 16 | "github.com/lima-vm/lima/v2/pkg/driver/external/server" |
| 17 | "github.com/lima-vm/lima/v2/pkg/driverutil" |
| 18 | "github.com/lima-vm/lima/v2/pkg/instance" |
| 19 | "github.com/lima-vm/lima/v2/pkg/limatype/filenames" |
| 20 | "github.com/lima-vm/lima/v2/pkg/store" |
| 21 | ) |
| 22 | |
| 23 | func newFactoryResetCommand() *cobra.Command { |
| 24 | resetCommand := &cobra.Command{ |
| 25 | Use: "factory-reset INSTANCE", |
| 26 | Short: "Factory reset an instance of Lima", |
| 27 | Args: WrapArgsError(cobra.MaximumNArgs(1)), |
| 28 | RunE: factoryResetAction, |
| 29 | ValidArgsFunction: factoryResetBashComplete, |
| 30 | GroupID: advancedCommand, |
| 31 | } |
| 32 | return resetCommand |
| 33 | } |
| 34 | |
| 35 | func factoryResetAction(cmd *cobra.Command, args []string) error { |
| 36 | ctx := cmd.Context() |
| 37 | instName := DefaultInstanceName |
| 38 | if len(args) > 0 { |
| 39 | instName = args[0] |
| 40 | } |
| 41 | |
| 42 | inst, err := store.Inspect(ctx, instName) |
| 43 | if err != nil { |
| 44 | if errors.Is(err, os.ErrNotExist) { |
| 45 | logrus.Infof("Instance %#q not found", instName) |
| 46 | return nil |
| 47 | } |
| 48 | return err |
| 49 | } |
| 50 | if inst.Protected { |
| 51 | return errors.New("instance is protected to prohibit accidental factory-reset (Hint: use `limactl unprotect`)") |
| 52 | } |
| 53 | |
| 54 | instance.StopForcibly(inst) |
| 55 | |
| 56 | fi, err := os.ReadDir(inst.Dir) |
| 57 | if err != nil { |
| 58 | return err |
| 59 | } |
| 60 | retain := map[string]struct{}{ |
| 61 | filenames.LimaVersion: {}, |
| 62 | filenames.Protected: {}, |
| 63 | filenames.VzIdentifier: {}, |
| 64 | } |
| 65 | for _, f := range fi { |
| 66 | path := filepath.Join(inst.Dir, f.Name()) |
| 67 | if _, ok := retain[f.Name()]; !ok && !strings.HasSuffix(path, ".yaml") && !strings.HasSuffix(path, ".yml") { |
| 68 | logrus.Infof("Removing %#q", path) |
| 69 | if err := os.Remove(path); err != nil { |
| 70 | logrus.Error(err) |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | if _, err := driverutil.CreateConfiguredDriver(ctx, inst, 0); err != nil { |
| 75 | logrus.WithError(err).Error("failed to configure driver for regenerating cloud-config") |
| 76 | } |
| 77 | |
| 78 | if err := cidata.GenerateCloudConfig(ctx, inst.Dir, instName, inst.Config); err != nil { |
| 79 | logrus.Error(err) |
| 80 | } |
| 81 | defer func() { |
| 82 | server.Stop(inst.Dir, true) |
| 83 | }() |
| 84 | |
| 85 | logrus.Infof("Instance %#q has been factory reset", instName) |
| 86 | return nil |
| 87 | } |
| 88 | |
| 89 | func factoryResetBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { |
| 90 | return bashCompleteInstanceNames(cmd) |
| 91 | } |