[systemd-devel] Generators, Targets and Stater Units - I am holding it wrong?
Lennart Poettering
lennart at poettering.net
Mon Jun 23 11:27:24 UTC 2025
On So, 22.06.25 22:30, Marc Haber (mh+systemd-devel at zugschlus.de) wrote:
> Hi,
>
> the following works, but I doubt that it is a nice way to do it. I have a
> generator that generates a number of service, and I want all of those
> services automatically started at boot. I have:
>
> $ sudo systemctl cat console-log-8.service
You made a typo there. The command is spelled "run0", not "sudo". ;-)
> [sudo] password for mh on hover:
> # /run/systemd/generator/console-log-8.service
> # generated by console-log.generator
>
> [Unit]
> Description=page tty8
> After=console-log.target
>
> [Service]
> ...
>
> [Install]
> WantedBy=console-log.target
> $ sudo systemctl cat console-log-9.service
> # /run/systemd/generator/console-log-9.service
> # generated by console-log.generator
>
> [Unit]
> Description=page tty9
> After=console-log.target
>
> [Service]
> ...
>
> [Install]
> WantedBy=console-log.target
> $ sudo systemctl cat console-log.target
> # /run/systemd/generator/console-log.target
> # generated by console-log.generator
>
> [Unit]
> Description=Pull in all console-log units
> After=multi-user.target
> Wants=console-log-9-var-log-syslog-syslog.service
> Wants=console-log-8-var-log-exim4-mainlog.service
>
> [Install]
> WantedBy=multi-user.target
> $ sudo systemctl cat console-log-starter.service
> # /etc/systemd/system/console-log-starter.service
> [Unit]
> Description=Start console-log.target at boot
> After=multi-user.target
>
> [Service]
> Type=oneshot
> ExecStart=/bin/systemctl start console-log.target
> RemainAfterExit=yes
>
> [Install]
> WantedBy=multi-user.target
>
> $
>
> The console-log-starter.service is the only persistent thing, all others are
> generated (including the target since the target lists all generated
> services). So I cannot enable them. The console-log-starter.service is the
> first unit that I can actually enable.
I am not sure I follow. What are you trying to do? Do you want that
the service units you generated are automatically activated at boot
without any further admin step? Or do you want that there's an
explicit enablement step the user has to do via "systemctl enable"?
If you do not want that extra step, then just do create a symlink from
the service units you generate (which you generate into one of the
thre params that a generator gets passed via argv[], let's call the
one you picked $TARGET) to the multi-user.target.wants/ subdir.
i.e. add something like this to your generator code:
mkdir -p $TARGET/multi-user.target.wants
ln -s ../console-log-8.service $TARGET/multi-user.target.wants/console-log-8.service
ln -s ../console-log-9.service $TARGET/multi-user.target.wants/console-log-9.service
If you instead want an explicit enablement step for the admin, you
could do that via an extra target. if you call that target
"foobar.target", then you would do
mkdir -p $TARGET/foobar.target.wants
ln -s ../console-log-8.service $TARGET/foobar.target.wants/console-log-8.service
ln -s ../console-log-9.service $TARGET/foobar.target.wants/console-log-9.service
And then foobar.target would basically just consist of
WantedBy=multi-user.target in its [Install] section (and would be
shipped in /usr/, not generated by the generator). And you could
then hook the generated units into the boot via "systemctl enable
foobar.target".
But then again I really don't grok what you are trying to do, maybe I
misread your intentions entirely.
Lennart
--
Lennart Poettering, Berlin
More information about the systemd-devel
mailing list