| 1 | |
| 2 | |
| 3 | |
| 4 | package main |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "os" |
| 11 | "os/exec" |
| 12 | |
| 13 | "github.com/sirupsen/logrus" |
| 14 | "github.com/spf13/cobra" |
| 15 | |
| 16 | "github.com/lima-vm/lima/v2/pkg/autostart" |
| 17 | "github.com/lima-vm/lima/v2/pkg/autostart/launchd" |
| 18 | "github.com/lima-vm/lima/v2/pkg/limatype/dirnames" |
| 19 | "github.com/lima-vm/lima/v2/pkg/store" |
| 20 | "github.com/lima-vm/lima/v2/pkg/textutil" |
| 21 | ) |
| 22 | |
| 23 | func autostartEnableAction(cmd *cobra.Command, args []string) error { |
| 24 | flags := cmd.Flags() |
| 25 | condition, err := flags.GetString("condition") |
| 26 | if err != nil { |
| 27 | return err |
| 28 | } |
| 29 | keepAlive, err := flags.GetBool("keep-alive") |
| 30 | if err != nil { |
| 31 | return err |
| 32 | } |
| 33 | |
| 34 | ctx := cmd.Context() |
| 35 | inst, err := store.Inspect(ctx, args[0]) |
| 36 | if err != nil { |
| 37 | if errors.Is(err, os.ErrNotExist) { |
| 38 | return fmt.Errorf("instance %q not found", args[0]) |
| 39 | } |
| 40 | return err |
| 41 | } |
| 42 | |
| 43 | switch condition { |
| 44 | case "login": |
| 45 | mgr := autostart.ManagerWith(keepAlive) |
| 46 | if err := mgr.RegisterToStartAtLogin(ctx, inst); err != nil { |
| 47 | return fmt.Errorf("failed to register instance %#q to start at login: %w", inst.Name, err) |
| 48 | } |
| 49 | logrus.Infof("Instance %#q registered to start at login", inst.Name) |
| 50 | case "boot": |
| 51 | userName, err := flags.GetString("user") |
| 52 | if err != nil { |
| 53 | return err |
| 54 | } |
| 55 | if userName == "" { |
| 56 | userName = os.Getenv("USER") |
| 57 | } |
| 58 | if userName == "" { |
| 59 | return errors.New("could not determine user; pass --user") |
| 60 | } |
| 61 | return daemonInstall(ctx, inst.Name, inst.Dir, userName, keepAlive) |
| 62 | default: |
| 63 | return fmt.Errorf("unknown condition %q: must be \"login\" or \"boot\"", condition) |
| 64 | } |
| 65 | return nil |
| 66 | } |
| 67 | |
| 68 | func autostartDisableAction(cmd *cobra.Command, args []string) error { |
| 69 | ctx := cmd.Context() |
| 70 | inst, err := store.Inspect(ctx, args[0]) |
| 71 | if err != nil { |
| 72 | if errors.Is(err, os.ErrNotExist) { |
| 73 | return fmt.Errorf("instance %q not found", args[0]) |
| 74 | } |
| 75 | return err |
| 76 | } |
| 77 | |
| 78 | |
| 79 | if registered, err := autostart.IsRegistered(ctx, inst); err != nil { |
| 80 | return err |
| 81 | } else if registered { |
| 82 | if err := autostart.UnregisterFromStartAtLogin(ctx, inst); err != nil { |
| 83 | return fmt.Errorf("failed to unregister instance %#q from start at login: %w", inst.Name, err) |
| 84 | } |
| 85 | logrus.Infof("Instance %#q unregistered from start at login", inst.Name) |
| 86 | return nil |
| 87 | } |
| 88 | |
| 89 | |
| 90 | daemonPath := launchd.GetDaemonPlistPath(inst.Name) |
| 91 | if _, err := os.Stat(daemonPath); err == nil { |
| 92 | return daemonUninstall(ctx, inst.Name) |
| 93 | } |
| 94 | |
| 95 | logrus.Infof("Instance %#q is not registered for automatic startup", inst.Name) |
| 96 | return nil |
| 97 | } |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | func renderDaemonPlist(binary, instName, workDir, userName string, keepAlive bool) ([]byte, error) { |
| 103 | |
| 104 | |
| 105 | limaHome, err := dirnames.LimaDir() |
| 106 | if err != nil { |
| 107 | return nil, err |
| 108 | } |
| 109 | vars := map[string]string{ |
| 110 | "Binary": binary, |
| 111 | "Instance": instName, |
| 112 | "LimaHome": limaHome, |
| 113 | "WorkDir": workDir, |
| 114 | "UserName": userName, |
| 115 | } |
| 116 | if keepAlive { |
| 117 | vars["KeepAlive"] = "true" |
| 118 | } |
| 119 | return textutil.ExecuteTemplate(launchd.DaemonTemplate, vars) |
| 120 | } |
| 121 | |
| 122 | func daemonInstall(ctx context.Context, instName, workDir, userName string, keepAlive bool) error { |
| 123 | selfExe, err := os.Executable() |
| 124 | if err != nil { |
| 125 | return fmt.Errorf("could not determine limactl path: %w", err) |
| 126 | } |
| 127 | |
| 128 | content, err := renderDaemonPlist(selfExe, instName, workDir, userName, keepAlive) |
| 129 | if err != nil { |
| 130 | return fmt.Errorf("failed to render daemon plist: %w", err) |
| 131 | } |
| 132 | |
| 133 | tmp, err := os.CreateTemp("", "io.lima-vm.daemon.*.plist") |
| 134 | if err != nil { |
| 135 | return fmt.Errorf("failed to create temp file: %w", err) |
| 136 | } |
| 137 | defer os.Remove(tmp.Name()) |
| 138 | if _, err := tmp.Write(content); err != nil { |
| 139 | return fmt.Errorf("failed to write temp plist: %w", err) |
| 140 | } |
| 141 | tmp.Close() |
| 142 | |
| 143 | destPath := launchd.GetDaemonPlistPath(instName) |
| 144 | svcTarget := "system/" + launchd.DaemonServiceNameFrom(instName) |
| 145 | |
| 146 | _ = runSudo(ctx, "launchctl", "bootout", svcTarget) |
| 147 | if err := runSudo(ctx, "install", "-m", "644", tmp.Name(), destPath); err != nil { |
| 148 | return fmt.Errorf("failed to install plist to %s: %w", destPath, err) |
| 149 | } |
| 150 | if err := runSudo(ctx, "launchctl", "enable", svcTarget); err != nil { |
| 151 | return fmt.Errorf("failed to enable LaunchDaemon: %w", err) |
| 152 | } |
| 153 | if err := runSudo(ctx, "launchctl", "bootstrap", "system", destPath); err != nil { |
| 154 | return fmt.Errorf("failed to bootstrap LaunchDaemon: %w", err) |
| 155 | } |
| 156 | |
| 157 | logrus.Infof("LaunchDaemon installed for instance %q (runs as %q at boot)", instName, userName) |
| 158 | logrus.Infof("Plist: %s", destPath) |
| 159 | return nil |
| 160 | } |
| 161 | |
| 162 | func daemonUninstall(ctx context.Context, instName string) error { |
| 163 | svcTarget := "system/" + launchd.DaemonServiceNameFrom(instName) |
| 164 | destPath := launchd.GetDaemonPlistPath(instName) |
| 165 | |
| 166 | _ = runSudo(ctx, "launchctl", "bootout", svcTarget) |
| 167 | _ = runSudo(ctx, "launchctl", "disable", svcTarget) |
| 168 | if err := runSudo(ctx, "rm", "-f", destPath); err != nil { |
| 169 | return fmt.Errorf("failed to remove %s: %w", destPath, err) |
| 170 | } |
| 171 | logrus.Infof("LaunchDaemon uninstalled for instance %q", instName) |
| 172 | return nil |
| 173 | } |
| 174 | |
| 175 | |
| 176 | func runSudo(ctx context.Context, args ...string) error { |
| 177 | cmd := exec.CommandContext(ctx, "sudo", args...) |
| 178 | cmd.Stdout = os.Stdout |
| 179 | cmd.Stderr = os.Stderr |
| 180 | cmd.Stdin = os.Stdin |
| 181 | logrus.Infof("running: sudo %v", args) |
| 182 | return cmd.Run() |
| 183 | } |