|
| 1 | +// SPDX-FileCopyrightText: Copyright The Lima Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 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/launchd" |
| 17 | + "github.com/lima-vm/lima/v2/pkg/store" |
| 18 | + "github.com/lima-vm/lima/v2/pkg/textutil" |
| 19 | +) |
| 20 | + |
| 21 | +func daemonInstallAction(cmd *cobra.Command, args []string) error { |
| 22 | + userName, err := cmd.Flags().GetString("user") |
| 23 | + if err != nil { |
| 24 | + return err |
| 25 | + } |
| 26 | + if userName == "" { |
| 27 | + userName = os.Getenv("USER") |
| 28 | + } |
| 29 | + if userName == "" { |
| 30 | + return errors.New("could not determine user; pass --user") |
| 31 | + } |
| 32 | + |
| 33 | + ctx := cmd.Context() |
| 34 | + inst, err := store.Inspect(ctx, args[0]) |
| 35 | + if err != nil { |
| 36 | + if errors.Is(err, os.ErrNotExist) { |
| 37 | + return fmt.Errorf("instance %q not found", args[0]) |
| 38 | + } |
| 39 | + return err |
| 40 | + } |
| 41 | + |
| 42 | + selfExe, err := os.Executable() |
| 43 | + if err != nil { |
| 44 | + return fmt.Errorf("could not determine limactl path: %w", err) |
| 45 | + } |
| 46 | + |
| 47 | + content, err := textutil.ExecuteTemplate(launchd.DaemonTemplate, map[string]string{ |
| 48 | + "Binary": selfExe, |
| 49 | + "Instance": inst.Name, |
| 50 | + "WorkDir": inst.Dir, |
| 51 | + "UserName": userName, |
| 52 | + }) |
| 53 | + if err != nil { |
| 54 | + return fmt.Errorf("failed to render daemon plist: %w", err) |
| 55 | + } |
| 56 | + |
| 57 | + tmp, err := os.CreateTemp("", "io.lima-vm.daemon.*.plist") |
| 58 | + if err != nil { |
| 59 | + return fmt.Errorf("failed to create temp file: %w", err) |
| 60 | + } |
| 61 | + defer os.Remove(tmp.Name()) |
| 62 | + if _, err := tmp.Write(content); err != nil { |
| 63 | + return fmt.Errorf("failed to write temp plist: %w", err) |
| 64 | + } |
| 65 | + tmp.Close() |
| 66 | + |
| 67 | + destPath := launchd.GetDaemonPlistPath(inst.Name) |
| 68 | + svcTarget := "system/" + launchd.DaemonServiceNameFrom(inst.Name) |
| 69 | + |
| 70 | + // Bootout first in case a previous install is still loaded (ignore error). |
| 71 | + _ = runSudo(ctx, "launchctl", "bootout", svcTarget) |
| 72 | + |
| 73 | + if err := runSudo(ctx, "install", "-m", "644", tmp.Name(), destPath); err != nil { |
| 74 | + return fmt.Errorf("failed to install plist to %s: %w", destPath, err) |
| 75 | + } |
| 76 | + if err := runSudo(ctx, "launchctl", "enable", svcTarget); err != nil { |
| 77 | + return fmt.Errorf("failed to enable LaunchDaemon: %w", err) |
| 78 | + } |
| 79 | + if err := runSudo(ctx, "launchctl", "bootstrap", "system", destPath); err != nil { |
| 80 | + return fmt.Errorf("failed to bootstrap LaunchDaemon: %w", err) |
| 81 | + } |
| 82 | + |
| 83 | + logrus.Infof("LaunchDaemon installed for instance %q (runs as %q at boot)", inst.Name, userName) |
| 84 | + logrus.Infof("Plist: %s", destPath) |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +func daemonUninstallAction(cmd *cobra.Command, args []string) error { |
| 89 | + ctx := cmd.Context() |
| 90 | + inst, err := store.Inspect(ctx, args[0]) |
| 91 | + if err != nil { |
| 92 | + if errors.Is(err, os.ErrNotExist) { |
| 93 | + return fmt.Errorf("instance %q not found", args[0]) |
| 94 | + } |
| 95 | + return err |
| 96 | + } |
| 97 | + |
| 98 | + svcTarget := "system/" + launchd.DaemonServiceNameFrom(inst.Name) |
| 99 | + destPath := launchd.GetDaemonPlistPath(inst.Name) |
| 100 | + |
| 101 | + _ = runSudo(ctx, "launchctl", "bootout", svcTarget) |
| 102 | + _ = runSudo(ctx, "launchctl", "disable", svcTarget) |
| 103 | + if err := runSudo(ctx, "rm", "-f", destPath); err != nil { |
| 104 | + return fmt.Errorf("failed to remove %s: %w", destPath, err) |
| 105 | + } |
| 106 | + logrus.Infof("LaunchDaemon uninstalled for instance %q", inst.Name) |
| 107 | + return nil |
| 108 | +} |
| 109 | + |
| 110 | +// runSudo executes a command under sudo, inheriting the terminal so password prompts work. |
| 111 | +func runSudo(ctx context.Context, args ...string) error { |
| 112 | + cmd := exec.CommandContext(ctx, "sudo", args...) //nolint:gosec // args are constructed internally, not from user input |
| 113 | + cmd.Stdout = os.Stdout |
| 114 | + cmd.Stderr = os.Stderr |
| 115 | + cmd.Stdin = os.Stdin |
| 116 | + logrus.Debugf("running: sudo %v", args) |
| 117 | + return cmd.Run() |
| 118 | +} |
0 commit comments