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