[systemd-devel] How does systemd discover template instances?

Michael Chapman mike at very.puzzling.org
Thu Oct 15 02:23:21 PDT 2015


On Thu, 15 Oct 2015, Stuart Longland wrote:
> Assuming I have a few files distributed in the base package:
> 	/lib/systemd/system/comms-drivers.service
> 	/lib/systemd/system/comms-drivers at .service
>
> Ordinarily, one would tell systemd about template instances by creating
> symbolic links.
>
> Suppose however I wanted to not do this, but instead, provide some
> automatic discovery mechanism for systemd, so it could run a script that
> would tell it what instances exist.
>
> Is there a mechanism for doing this in systemd?

Hi Stuart,

One approach you might want to consider is using a systemd generator [1] 
to create the instance symlinks automatically at boot, e.g.:

   ARGV[1]/comms-driver at some-instance.service
      -> /lib/systemd/system/comms-drivers at .service

The ARGV[1] argument passed to your generator is likely to be 
"/run/systemd/generator".

Your generator can use any mechanism it likes to decide which instances 
should be created. Note, however, the generator is run very early on in 
boot (and whenever systemd's configuration is reloaded), so it should be 
lightweight and not need any other system services to be operational.

If you have particular instances you want started at boot, you can link 
them in to the appropriate target's .wants directory, e.g.:

   ARGV[1]/multi-user.target.wants/comms-driver at some-instance.service
      -> /lib/systemd/system/comms-drivers at .service

Hope this is of assistance.

Regards,
Michael


[1] http://www.freedesktop.org/software/systemd/man/systemd.generator.html


More information about the systemd-devel mailing list