Are 'systemctl daemon-reload' calls before enabling a service needed?

Usually, when creating a new systemd unit, snapd makes two calls to systemctl:

  1. systemctl daemon-reload
  2. systemctl --root / enable <service>

(see [1] for instance). But, when reading the “enable UNIT” section in the systemctl man page [2], calling daemon-reload should not be really needed. Quoting:

After the symlinks have been created, the system manager configuration is reloaded (in a way equivalent to daemon-reload), in order to ensure the changes are taken into account immediately.

It also says that calling daemon-reload might be needed when creating manually symbolic links, which is not the case here. I have done some tests removing the daemon-reload call from snapd, and everything has worked as usual when installing a snap, without side effects. Also, I have captured the DBus signals produced by the two calls (with sudo dbus-monitor --system), and for both enable and daemon-reload I get a Reloading signal and multiple UnitRemoved/UnitNew signals, so it all indicates that calling daemon-reload is redundant.

Note that the cost of this additional call is high: doing a reload in a UC system with 5 snaps installed provokes more than 800 UnitRemoved/UnitNew signals (a pair for each unit present in the system). This takes some seconds in slow SoCs. Therefore, removing the additional call would have a nice boost in snap installation times.

[1] https://github.com/snapcore/snapd/blob/master/overlord/snapstate/backend/mountunit.go#L45
[2] https://www.freedesktop.org/software/systemd/man/systemctl.html

2 Likes

what happens if you have a snap installed, and refresh it, with the refreshed version having a slightly different metadata (say it adds a StopExec or changes the daemon type?

@chipaca I have done a quick test with a service I have manually created. It had originally type ‘oneshot’:

  1. I disabled the service
  2. Edited and changed type to ‘simple’
  3. Enabled the service
  4. Checked the type with systemctl show, which showed ‘simple’, as expected

So no daemon-reload was needed. Also, in case that daemon-reload is needed for a specific case, we should isolate that case and do the reload only for it.