Skip to content

Commit fe4eeb7

Browse files
committed
Add support for systemd-less systems
Add timeout for systemd connection Autodetect systemd availability Signed-off-by: Alessandro Vinciguerra <alessandro.vinciguerra@postfinance.ch>
1 parent 125d1db commit fe4eeb7

3 files changed

Lines changed: 47 additions & 5 deletions

File tree

cmd/nvidia-mig-manager/main.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ const (
5858
DefaultDriverRootCtrPath = "/run/nvidia/driver"
5959
DefaultNvidiaCDIHookPath = "/usr/local/nvidia/toolkit/nvidia-cdi-hook"
6060
DefaultGeneratedConfigFile = "/etc/nvidia-mig-manager/generated-config.yaml"
61+
DefaultSystemdTimeout = 1.0
6162
)
6263

6364
var (
@@ -80,6 +81,7 @@ var (
8081
devRoot string
8182
devRootCtrPath string
8283
nvidiaCDIHookPath string
84+
systemdTimeout float64
8385
)
8486

8587
type GPUClients struct {
@@ -266,6 +268,13 @@ func main() {
266268
Destination: &nvidiaCDIHookPath,
267269
Sources: cli.EnvVars("NVIDIA_CDI_HOOK_PATH"),
268270
},
271+
&cli.Float64Flag{
272+
Name: "systemd-timeout",
273+
Value: DefaultSystemdTimeout,
274+
Usage: "Timeout for systemd connection (in seconds)",
275+
Destination: &systemdTimeout,
276+
Sources: cli.EnvVars("SYSTEMD_CONNECTION_TIMEOUT"),
277+
},
269278
}
270279

271280
err := c.Run(context.Background(), os.Args)
@@ -522,6 +531,7 @@ func migReconfigure(ctx context.Context, migConfigValue string, clientset *kuber
522531
DefaultGPUClientsNamespace: defaultGPUClientsNamespaceFlag,
523532
WithReboot: withRebootFlag,
524533
WithShutdownHostGPUClients: withShutdownHostGPUClientsFlag,
534+
SystemdTimeout: systemdTimeout,
525535
}
526536

527537
if cdiEnabledFlag {

internal/systemd/systemd.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,22 @@ type Manager struct {
3535
}
3636

3737
// NewManager creates a new Manager instance
38-
func NewManager(ctx context.Context) (*Manager, error) {
39-
conn, err := dbus.NewSystemConnectionContext(ctx)
38+
func NewManager(ctx context.Context, timeout time.Duration) (*Manager, error) {
39+
deadline, cancel := context.WithTimeout(ctx, timeout)
40+
defer cancel()
41+
42+
conn, err := dbus.NewSystemConnectionContext(deadline)
43+
if err != nil {
44+
err = fmt.Errorf("failed to connect to systemd D-Bus: %w", err)
45+
if timeout := deadline.Err(); timeout == context.DeadlineExceeded {
46+
err = errors.Join(err, timeout)
47+
}
48+
return nil, err
49+
}
50+
51+
conn, err = dbus.NewSystemConnectionContext(ctx)
4052
if err != nil {
41-
return nil, fmt.Errorf("failed to connect to systemd D-Bus: %w", err)
53+
return nil, fmt.Errorf("failed to recreate connection with no timeout")
4254
}
4355

4456
return &Manager{

pkg/mig/reconfigure/reconfigure.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ type Options struct {
7878
DriverLibraryPath string
7979
NvidiaSMIPath string
8080
NvidiaCDIHookPath string
81+
SystemdTimeout float64
8182
}
8283

8384
// Reconfigure handles the MIG reconfiguration process
@@ -107,9 +108,14 @@ func New(ctx context.Context, clientset *kubernetes.Clientset, migPartedBinary [
107108
_ = os.Setenv("DBUS_SYSTEM_BUS_ADDRESS", hostSystemBusAddress)
108109
}
109110

110-
systemdManager, err := systemd.NewManager(ctx)
111+
systemdManager, err := systemd.NewManager(ctx, time.Duration(opts.SystemdTimeout)*time.Second)
111112
if err != nil {
112-
return nil, fmt.Errorf("failed to initialize systemd manager: %w", err)
113+
if errors.Is(err, context.DeadlineExceeded) {
114+
log.Warn("connection to systemd timed out; skipping systemd-related tasks for this reconfiguration")
115+
systemdManager = nil
116+
} else {
117+
return nil, fmt.Errorf("failed to initialize systemd manager: %w", err)
118+
}
113119
}
114120

115121
return &Reconfigure{
@@ -331,6 +337,10 @@ Environment="MIG_PARTED_SELECTED_CONFIG=%s"
331337
return fmt.Errorf("failed to write config to state file: %w", err)
332338
}
333339

340+
if r.systemdManager == nil {
341+
return nil
342+
}
343+
334344
return r.systemdManager.ReloadDaemon()
335345
}
336346

@@ -420,6 +430,11 @@ func (r *Reconfigure) waitForPodsToBeDeleted() error {
420430

421431
// shutdownHostGPUClients shuts down host GPU clients
422432
func (r *Reconfigure) shutdownHostGPUClients() error {
433+
if r.systemdManager == nil {
434+
log.Info("No systemd manager available, not shutting down GPU clients on the host")
435+
return nil
436+
}
437+
423438
log.Info("Shutting down all GPU clients on the host by stopping their systemd services")
424439

425440
services := strings.Split(r.opts.HostGPUClientServices, ",")
@@ -705,6 +720,11 @@ func (r *Reconfigure) createCDISpec() error {
705720
}
706721

707722
func (r *Reconfigure) hostStartSystemdServices() error {
723+
if r.systemdManager == nil {
724+
log.Info("No systemd manager available, not starting anything via systemd")
725+
return nil
726+
}
727+
708728
services := r.stoppedServices
709729
var restartServices []string
710730
if len(services) == 0 {

0 commit comments

Comments
 (0)