Files
moby/daemon/command/daemon_linux.go
Cory Snider 40f06bf418 Reapply: daemon/command: add support for sd_notify "reload" notifications
This reapplies the af18206 commit which was reverted by ddb636c.

commit f74b856e1a added an [ExecReload] to
the systemd unit, which allows users to signal the daemon to reload its
config through systemd (`systemctl reload docker.service`).

While reloading works, systemd expects the `ExecReload` command to be
synchronous, so that it knows when the reload completes, and can account
for this when managing dependent services.

> Note however that reloading a daemon by enqueuing a signal (...) is usually
> not a good choice, because this is an asynchronous operation and hence not
> suitable when ordering reloads of multiple services against each other.

Systemd 253 introduced a new Type (Type=notify-reload, see [systemd#25916]),
which allows setting a "ReloadSignal" instead, if the service supports
it by including a [MONOTONIC_USEC] value in its "RELOADING=1"
notifications.

This patch:

- adds reload notifications to the daemon to notify when the daemon got
  signaled to reload its configuration see [sd_notify(3)].
- appends a [MONOTONIC_USEC] value to the reload notification to support
  systemd 253's notify-reload protocol. This is backwards-compatible
  with older systemd releases as the systemd service manager ignores
  unknown assignments in notifications.
- adds a `notifyReady` callback to notify when the reload finished,
  which we currently send regardless if the reload was successful or
  failed (which could be due to an invalid config).

[ExecReload]: https://www.freedesktop.org/software/systemd/man/latest/systemd.service.html#ExecReload=
[systemd#25916]: https://github.com/systemd/systemd/pull/25916
[MONOTONIC_USEC]: https://www.freedesktop.org/software/systemd/man/latest/sd_notify.html#MONOTONIC_USEC=…
[sd_notify(3)]: https://www.freedesktop.org/software/systemd/man/latest/sd_notify.html#RELOADING=1

Signed-off-by: Cory Snider <csnider@mirantis.com>
Co-authored-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
2026-02-26 12:08:46 +01:00

92 lines
3.3 KiB
Go

package command
import (
"context"
cdcgroups "github.com/containerd/cgroups/v3"
"github.com/containerd/log"
systemdDaemon "github.com/coreos/go-systemd/v22/daemon"
"github.com/moby/moby/v2/daemon"
"github.com/moby/moby/v2/daemon/config"
"github.com/moby/moby/v2/pkg/sysinfo"
"github.com/pkg/errors"
)
// setPlatformOptions applies platform-specific CLI configuration options.
func setPlatformOptions(conf *config.Config) error {
if conf.RemappedRoot == "" {
return nil
}
containerdNamespace, containerdPluginNamespace, err := daemon.RemapContainerdNamespaces(conf)
if err != nil {
return err
}
conf.ContainerdNamespace = containerdNamespace
conf.ContainerdPluginNamespace = containerdPluginNamespace
// Buildkit breaks when userns remapping is enabled and containerd snapshotter is used. As a temporary workaround,
// if containerd snapshotter is explicitly enabled, and userns remapping is enabled too, return an error. If userns
// remapping is enabled, but containerd-snapshotter is enabled by default, disable it. See https://github.com/moby/moby/issues/47377.
enabled := conf.Features["containerd-snapshotter"]
if enabled {
return errors.New("containerd-snapshotter is explicitly enabled, but is not compatible with userns remapping. Please disable userns remapping or containerd-snapshotter")
}
log.G(context.TODO()).Warn("userns remapping enabled, disabling containerd snapshotter")
if conf.Features == nil {
conf.Features = make(map[string]bool)
}
conf.Features["containerd-snapshotter"] = false
return nil
}
// preNotifyReady sends a message to the host when the API is active, but before the daemon is
func preNotifyReady() error {
return nil
}
// notifyReady sends a message to the host when the server is ready to be used
func notifyReady() {
// Tell the init daemon we are accepting requests
go systemdDaemon.SdNotify(false, systemdDaemon.SdNotifyReady)
}
// notifyStopping sends a message to the host when the server is shutting down
func notifyStopping() {
go systemdDaemon.SdNotify(false, systemdDaemon.SdNotifyStopping)
}
// notifyReloading sends a message to the host when the server got signaled to
// reloading its configuration, see [sd_notify(3)]. The server should be running
// as a systemd unit with "Type=notify" or "Type=notify-reload" (see
// [systemd.service(5)]).
//
// notifyReloading returns a callback that must be called after reloading completes
// (either successfully or unsuccessfully) to send [notifyReady].
//
// [sd_notify(3)]: https://www.freedesktop.org/software/systemd/man/latest/sd_notify.html#RELOADING=1
// [systemd.service(5)]: https://www.freedesktop.org/software/systemd/man/latest/systemd.service.html#Type=
func notifyReloading() (done func()) {
sent, _ := systemdDaemon.SdNotify(false, systemdDaemon.SdNotifyReloading+"\n"+systemdDaemon.SdNotifyMonotonicUsec())
if !sent {
// Nothing to do if no reloading event was sent.
return func() {}
}
return notifyReady
}
func validateCPURealtimeOptions(cfg *config.Config) error {
if cfg.CPURealtimePeriod == 0 && cfg.CPURealtimeRuntime == 0 {
return nil
}
if cdcgroups.Mode() == cdcgroups.Unified {
return errors.New("daemon-scoped cpu-rt-period and cpu-rt-runtime are not implemented for cgroup v2")
}
if !sysinfo.New().CPURealtime {
return errors.New("daemon-scoped cpu-rt-period and cpu-rt-runtime are not supported by the kernel")
}
return nil
}