Skip to content

Commit 2b6687a

Browse files
authored
Fix installer to gracefully handle systemd not running in containers (#46068)
## What does this PR do? Fixes the datadog-installer to gracefully handle environments where systemd is not running (e.g., containers, non-systemd init systems). ## Motivation The installer was failing when attempting to execute `systemctl` commands on systems where systemd is installed but not running, which is common in containers. ## Changes Added `IsRunning()` checks to systemd operations: - `StartUnit()` - skips start if systemd not running - `RestartUnit()` - skips restart if systemd not running - `EnableUnit()` - skips enable if systemd not running - `Reload()` - skips daemon-reload if systemd not running These functions now log at INFO level and return success (nil) when systemd is not running, allowing installation to continue normally. ## Testing - Compilation verified - Linters passed (0 issues) Co-authored-by: baptiste.foy <baptiste.foy@datadoghq.com>
1 parent bfbafe1 commit 2b6687a

File tree

1 file changed

+34
-2
lines changed
  • pkg/fleet/installer/packages/service/systemd

1 file changed

+34
-2
lines changed

pkg/fleet/installer/packages/service/systemd/systemd.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,44 @@ func StopUnit(ctx context.Context, unit string, args ...string) error {
7070

7171
// StartUnit starts a systemd unit
7272
func StartUnit(ctx context.Context, unit string, args ...string) error {
73+
running, err := IsRunning()
74+
if err != nil {
75+
return err
76+
}
77+
if !running {
78+
log.Infof("Installer: systemd not running, skipping start of %s", unit)
79+
return nil
80+
}
7381
args = append([]string{"start", unit}, args...)
74-
err := telemetry.CommandContext(ctx, "systemctl", args...).Run()
82+
err = telemetry.CommandContext(ctx, "systemctl", args...).Run()
7583
return handleSystemdSelfStops(err)
7684
}
7785

7886
// RestartUnit restarts a systemd unit
7987
func RestartUnit(ctx context.Context, unit string, args ...string) error {
88+
running, err := IsRunning()
89+
if err != nil {
90+
return err
91+
}
92+
if !running {
93+
log.Infof("Installer: systemd not running, skipping restart of %s", unit)
94+
return nil
95+
}
8096
args = append([]string{"restart", unit}, args...)
81-
err := telemetry.CommandContext(ctx, "systemctl", args...).Run()
97+
err = telemetry.CommandContext(ctx, "systemctl", args...).Run()
8298
return handleSystemdSelfStops(err)
8399
}
84100

85101
// EnableUnit enables a systemd unit
86102
func EnableUnit(ctx context.Context, unit string) error {
103+
running, err := IsRunning()
104+
if err != nil {
105+
return err
106+
}
107+
if !running {
108+
log.Infof("Installer: systemd not running, skipping enable of %s", unit)
109+
return nil
110+
}
87111
return telemetry.CommandContext(ctx, "systemctl", "enable", unit).Run()
88112
}
89113

@@ -133,6 +157,14 @@ func WriteUnitOverride(ctx context.Context, unit string, name string, content st
133157

134158
// Reload reloads the systemd daemon
135159
func Reload(ctx context.Context) (err error) {
160+
running, runningErr := IsRunning()
161+
if runningErr != nil {
162+
return runningErr
163+
}
164+
if !running {
165+
log.Infof("Installer: systemd not running, skipping daemon-reload")
166+
return nil
167+
}
136168
return telemetry.CommandContext(ctx, "systemctl", "daemon-reload").Run()
137169
}
138170

0 commit comments

Comments
 (0)