0

I'm writing a program, which is managing a lot of instances of some unit templates. My plan was, to use the dbus service of systemd to do so. But apparently, the interface on org.freedesktop.systemd1.Manager can only manage either units, which are already loaded, or unitFiles.

The methods, working on unit files dont't work for enabling or starting template instances, because they apparently aren't designed to work with unit names which don't have a unit file with an exactly matching name, and don't find the template corresponding to the matching instance.

The methods working on loaded units also need a unit with that exact name to be already loaded, which isn't the case with units, which haven't been instantiated yet.

Also the documentation of the interface doesn't really talk about templates. Isn't this interface supposed to be equally powerful as the systemctl command?

Am I just missing something about the interface? Or is there another interface, specifically for templates, which I didn't find yet? or is there just not way, to do this stuff with systemd?

1 Answer 1

2

The systemd service on dbus is as well fully capable of managing the services started off a template unit file. So given you have a template unit file [email protected] and an instance with concrete name [email protected] followings should start the unit.

dbus-send --system --print-reply  --dest=org.freedesktop.systemd1  /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager.StartUnit  string:'[email protected]' string:'replace'

You can enable the unit as well given not already done via org.freedesktop.systemd1.Manager.EnableUnitFiles method.

Be aware that for other operations over the unit object path, you need to translate the name to a dbus valid name, so for instance [email protected] translates to /org/freedesktop/systemd1/unit/foo_40bar_2eservice where

  • @ translates to _40
  • . translates to _2e
  • _ translates to _5f
  • - translates to _2d

not sure where to get a full list of this translation though.

Then for instance you can interact with the unit to acquire properties

dbus-send --system --print-reply  --dest=org.freedesktop.systemd1  /org/freedesktop/systemd1/unit/foo_40bar_2eservice org.freedesktop.DBus.Properties.Get string:'org.freedesktop.systemd1.Service' string:'MainPID'
1
  • The translations are just a hexadecimal representation of the ascii encoded character, with a dash in front, I think. I think, it's worth checking, how this interacts with the systemd internal translation.
    – Niclas
    Aug 9, 2022 at 2:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .