|
| 1 | +// Command rd-init is a minimal implementation of cloud-init to start lima. |
| 2 | +// Note that this only implements `cloud-init-local`. |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "log/slog" |
| 9 | + "os" |
| 10 | + "os/exec" |
| 11 | + |
| 12 | + "github.com/coreos/go-systemd/daemon" |
| 13 | + "github.com/coreos/go-systemd/v22/dbus" |
| 14 | +) |
| 15 | + |
| 16 | +func runCommand(ctx context.Context, name string, arg... string) error { |
| 17 | + slog.DebugContext(ctx, "Running command", "name", name, "arg", arg) |
| 18 | + cmd := exec.CommandContext(ctx, name, arg...) |
| 19 | + cmd.Stdout = os.Stdout |
| 20 | + cmd.Stderr = os.Stderr |
| 21 | + return cmd.Run() |
| 22 | +} |
| 23 | + |
| 24 | +func run(ctx context.Context) error { |
| 25 | + var units []string |
| 26 | + fns := []func(context.Context)([]string, error) { |
| 27 | + LoadMetadata, |
| 28 | + LoadUserData, |
| 29 | + LoadNetworkConfig, |
| 30 | + } |
| 31 | + for _, fn := range fns { |
| 32 | + if newUnits, err := fn(ctx); err != nil { |
| 33 | + return err |
| 34 | + } else { |
| 35 | + units = append(units, newUnits...) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + slog.InfoContext(ctx, "reloading systemd", "units", units) |
| 40 | + |
| 41 | + conn, err := dbus.NewSystemConnectionContext(ctx) |
| 42 | + if err != nil { |
| 43 | + return fmt.Errorf("failed to connect to systemd: %w", err) |
| 44 | + } |
| 45 | + defer conn.Close() |
| 46 | + |
| 47 | + if err := conn.ReloadContext(ctx); err != nil { |
| 48 | + return fmt.Errorf("failed to reload systemd: %w", err) |
| 49 | + } |
| 50 | + |
| 51 | + // Trigger udevadm to cause the network interface to change name. |
| 52 | + slog.InfoContext(ctx, "triggering udevadm to rename interfaces") |
| 53 | + if err := runCommand(ctx, "/usr/bin/udevadm", "control", "--reload"); err != nil { |
| 54 | + return fmt.Errorf("failed to reload udev: %w", err) |
| 55 | + } |
| 56 | + err = runCommand(ctx, "/usr/bin/udevadm", "trigger", "--type=devices", "--subsystem-match=net") |
| 57 | + if err != nil { |
| 58 | + return fmt.Errorf("failed to rename network devices: %w", err) |
| 59 | + } |
| 60 | + |
| 61 | + // Notify ready before we reload the other units; otherwise we end up |
| 62 | + // blocking startup due to a loop with systemd-networkd. |
| 63 | + if _, err := daemon.SdNotify(true, daemon.SdNotifyReady); err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + |
| 67 | + seenUnits := make(map[string]bool) |
| 68 | + for _, unit := range units { |
| 69 | + if seenUnits[unit] { |
| 70 | + continue |
| 71 | + } |
| 72 | + seenUnits[unit] = true |
| 73 | + ch := make(chan string) |
| 74 | + _, err = conn.RestartUnitContext(ctx, unit, "replace", ch) |
| 75 | + if err != nil { |
| 76 | + return fmt.Errorf("failed to start unit %s: %w", unit, err) |
| 77 | + } |
| 78 | + slog.InfoContext(ctx, "restarted systemd unit", "unit", unit, "result", <-ch) |
| 79 | + } |
| 80 | + |
| 81 | + return nil |
| 82 | +} |
| 83 | + |
| 84 | +func main () { |
| 85 | + ctx := context.Background() |
| 86 | + if err := run(ctx); err != nil { |
| 87 | + slog.ErrorContext(ctx, "rd-init failed", "error", err) |
| 88 | + os.Exit(1) |
| 89 | + } |
| 90 | +} |
0 commit comments