You are not logged in.

#26 2016-06-04 12:10:53

ukhippo
Member
From: Non-paged pool
Registered: 2014-02-21
Posts: 366

Re: [SOLVED] Where to put environment vars regarding SystemD

Caldazar wrote:

Also given that I can't think of one of said theoretical fringe-cases, even "WantedBy=basic.target" should be more than enough, maybe with a "Before=multi-user.target".(?)

You're looking the wrong way; you need "After=sysinit.target".
In order to reach a target, all its dependencies must be satisfied. So when systemd has reached basic.target your service will have been started. basic.target is a dependency of multi-user.target, so "Before=multi-user.target" is superfluous.
Without any After dependencies (explicit or implicitly from Requires) your service could be started anytime from startup. That's why services get the default dependency "After=basic.target" (unless UseDefaultDependencies=no is given).

Offline

#27 2016-06-05 12:28:50

Caldazar
Member
Registered: 2012-06-21
Posts: 38

Re: [SOLVED] Where to put environment vars regarding SystemD

I was completely wrong on that premise, thinking "WantedBy" means "spawned by" rather than making my service a pre-requisite to reach the WantedBy checkpoint.

Well, into the bin my plan went.

Now I got this:

[Unit]
Description=Import of /etc/environment into systemd's environment
DefaultDependencies=no
After=sysinit.target

[Service]
ExecStart=/usr/bin/bash -c "systemctl set-environment $(egrep -v '^\s*(#|$)' /etc/environment)"

[Install]
WantedBy=basic.target

In the first test run, it worked as intended.

I'll have to look into the other available options for the fine tuning. Dependencies and conditions and such.
For example I'm not sure whether I also should 'require' sysinit.target as I've seen it in a couple of units.
Don't I need timers, paths and socket targets up, that are now parallel to my service?
Also I have to check on other, potentially missing dependencies, now that I use 'DefaultDependencies=no'.

But as a rough foundation this should work more reliably, than with  "WantedBy=multi-user.target". (?)

Last edited by Caldazar (2016-06-05 12:43:06)


4.4.11-1-lts #1 SMP Thu May 19 21:03:24 CEST 2016 x86_64 GNU/Linux
KDE
AMD Phenom II X4 960T - AMD Radeon HD 6670 with Catalyst - 24GB RAM

Offline

#28 2016-06-05 20:43:26

ukhippo
Member
From: Non-paged pool
Registered: 2014-02-21
Posts: 366

Re: [SOLVED] Where to put environment vars regarding SystemD

Units use "Require" to ensure they are not run should the required unit(s) fail. Should sysinit.target fail there's no point your service running, so you may as well add a Require for that.

Offline

#29 2016-06-05 22:52:59

Caldazar
Member
Registered: 2012-06-21
Posts: 38

Re: [SOLVED] Where to put environment vars regarding SystemD

I thought "Require" meant "needs to be active". In any case, it doesn't look like it really matters for this service.

I had an interesting error on further inspection (sudo journalctl -b | grep dbus).

[...]
Jun 05 22:11:29 alboran dbus[467]: [system] Rejected send message, 2 matched rules; type="method_call", sender=":1.12" (uid=1000 pid=612 comm="systemctl set-environment PYTHONPATH=/usr/local/li")
interface="org.freedesktop.systemd1.Manager" member="SetEnvironment" error name="(unset)" requested_reply="0" destination="org.freedesktop.systemd1" (uid=0 pid=1 comm="/sbin/init ")
[...]

One thing that's interesting is that the process actually gets launched by uid=1000. I didn't know that, I thought it was root.

However, DBus here blocks the message from Systemd member="SetEnvironment".
That's ok, DBus apparently acts as a firewall for messages that are not whitelisted  in /etc/dbus-1/system.d/.
I just had to edit /etc/dbus-1/system.d/org.freedesktop.systemd1.conf and add

                <allow send_destination="org.freedesktop.systemd1"
                       send_interface="org.freedesktop.systemd1.Manager"
                       send_member="SetEnvironment"/>

to the 'policy context="default section"'.

I might revert that later on, as it doesn't bother me, but for now I have nice messages showing me when exactly my service gets started (sudo journalctl -b | grep systemd):

EDIT: I made the dumb mistake of not disenabling - re-enabling the service before reboot, so the list below obviously still is what happens if "WantedBy=multi-user.target"

[...]
Jun 05 23:39:17 alboran systemd[1]: Reached target Basic System.
Jun 05 23:39:17 alboran systemd[1]: Started Entropy Harvesting Daemon.
Jun 05 23:39:17 alboran systemd[1]: Starting Catalyst's fglrx kernel' module builder...
Jun 05 23:39:17 alboran systemd[1]: Starting Permit User Sessions...
Jun 05 23:39:17 alboran systemd[1]: Starting Login Service...
Jun 05 23:39:17 alboran systemd[1]: Starting Packet Filtering Framework...
Jun 05 23:39:17 alboran systemd[1]: Started Import of /etc/environment into systemd's environment.        <<<<<<<<
Jun 05 23:39:17 alboran systemd[1]: Starting Initialize hardware monitoring sensors...
Jun 05 23:39:17 alboran systemd[1]: Started Daily Cleanup of Temporary Directories.
Jun 05 23:39:17 alboran systemd[1]: Started Timer for clean-cache.
Jun 05 23:39:17 alboran systemd[1]: Started Daily rotation of log files.
Jun 05 23:39:17 alboran systemd[1]: Reached target Timers.
Jun 05 23:39:17 alboran systemd[1]: Starting Save/Restore Sound Card State...
Jun 05 23:39:17 alboran systemd[1]: Started D-Bus System Message Bus.
Jun 05 23:39:17 alboran systemd[1]: Started Catalyst's fglrx kernel' module builder.
Jun 05 23:39:17 alboran systemd[1]: Started Permit User Sessions.      
Jun 05 23:39:17 alboran systemd[1]: Started Packet Filtering Framework.
Jun 05 23:39:17 alboran systemd[1]: Started Save/Restore Sound Card State.
Jun 05 23:39:17 alboran systemd[1]: Started Initialize hardware monitoring sensors.
Jun 05 23:39:17 alboran systemd[1]: Reached target Network (Pre).
Jun 05 23:39:17 alboran systemd[1]: Starting Network Manager...
Jun 05 23:39:17 alboran systemd[1]: Started Simple Desktop Display Manager.
Jun 05 23:39:17 alboran systemd-logind[454]: New seat seat0.
Jun 05 23:39:17 alboran systemd[1]: Started Login Service.
Jun 05 23:39:17 alboran systemd-logind[454]: Watching system buttons on /dev/input/event3 (Power Button)
Jun 05 23:39:17 alboran systemd-logind[454]: Watching system buttons on /dev/input/event2 (Power Button)
Jun 05 23:39:17 alboran systemd[1]: Started Network Manager.
Jun 05 23:39:17 alboran systemd[1]: Reached target Network.
Jun 05 23:39:17 alboran dbus[463]: [system] Activating via systemd: service name='org.freedesktop.nm_dispatcher' unit='dbus-org.freedesktop.nm-dispatcher.service'
Jun 05 23:39:17 alboran systemd[1]: Started Apache Web Server.
Jun 05 23:39:17 alboran systemd[1]: Started OpenSSH Daemon.
Jun 05 23:39:17 alboran systemd[1]: Reached target Multi-User System.
Jun 05 23:39:17 alboran systemd[1]: Reached target Graphical Interface.
[...]

It gets started quite early, second only to "Entropy Harvesting Daemon". If there's no dependency issue now, then there is none.
We seem to be fine.

EDIT: vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
The list with After=sysinit.target & WantedBy=basic.target" is

[...]Jun 06 11:10:54 alboran systemd[1]: Reached target System Initialization.
Jun 06 11:10:54 alboran systemd[1]: Started Daily Cleanup of Temporary Directories.
Jun 06 11:10:54 alboran systemd[1]: Started Timer for clean-cache.
Jun 06 11:10:54 alboran systemd[1]: Listening on D-Bus System Message Bus Socket.
Jun 06 11:10:54 alboran systemd[1]: Reached target Sockets.
Jun 06 11:10:54 alboran systemd[1]: Started Daily rotation of log files.
Jun 06 11:10:54 alboran systemd[1]: Started Daily verification of password and group files.
Jun 06 11:10:54 alboran systemd[1]: Started Backup.
Jun 06 11:10:54 alboran systemd[1]: Started Import of /etc/environment into systemd's environment.
Jun 06 11:10:54 alboran systemd[1]: Reached target Basic System.
[...]

There it's actually the last one. I've yet to see if this is always the case or if I was just lucky.
In any case, "After=sockets.target" wouldn't hurt, I think
EDIT: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

If you have further critique, advice or suggestions, I'd love to read it.

So far, this was really fun and I now know a lot more about Systemd and the boot process than I did at the beginning of the week.
Thank you guys for this great learning experience!

Last edited by Caldazar (2016-06-06 09:29:22)


4.4.11-1-lts #1 SMP Thu May 19 21:03:24 CEST 2016 x86_64 GNU/Linux
KDE
AMD Phenom II X4 960T - AMD Radeon HD 6670 with Catalyst - 24GB RAM

Offline

Board footer

Powered by FluxBB