![]() |
Getting systemd to do what I want
We have a problem with systemd not starting things in the right order, but the configuration seems accurate, so I thought maybe there would be an systemd expert around here that could provide some insight.
Basically we have an embedded Linux box (running an offshoot of Debian), and two of the things that need to happen during the systemd-controlled startup are: 1) set the system clock with "hwclock -s", and 2) bring up the Ethernet interface with "ifup". It's important that hwclock be fully completed before ifup runs. Right now, that ordering is not ensured by the systemd configuration and I don't understand why. Instead what's happening is that hwclock and ifup are launched and run simultaneously, and the system clock gets set just as dhclient is waiting for a DHCP offer. Since the system clock changes, dhclient gets confused on its timeout and instantly and gives up waiting for the DHCP offer, and then 5 minutes passes before trying again. The ifup command is being run by a systemd service called "networking.service". The networking.service has "ExecStart=/sbin/ifup". Then ifup spawns dhclient because the network interface is configured to use DHCP. The hwclock command is being run by a systemd service called "hwclock.service". The hwclock.service has ExecStart=/sbin/hwclock -s. In the [Unit] section of the hwclock.service, there is a directive specifying "Before=networking.service", which should guarantee that the hwclock command completely finishes *before* networking.service is launched. But that is not what ends up happening in reality. I grabbed a snippet of the syslog to show the sequence of events: Nov 3 17:16:45 sdc daemon.info dhclient[314]: Nov 3 17:16:45 sdc daemon.info dhclient[314]: Listening on LPF/eth0/b8:27:eb:1e:87:94 Nov 3 17:16:45 sdc daemon.info dhclient[314]: Sending on LPF/eth0/b8:27:eb:1e:87:94 Nov 3 17:16:45 sdc daemon.info dhclient[314]: Sending on Socket/fallback Nov 3 17:16:45 sdc daemon.info dhclient[314]: DHCPDISCOVER on eth0 to 255.255.255.255 port 67 interval 4 Dec 13 16:03:14 sdc daemon.info dhclient[314]: No DHCPOFFERS received. Dec 13 16:03:14 sdc daemon.info dhclient[314]: No working leases in persistent database - sleeping. Note above how dhclient starts running on the date "Nov 3" (this is incorrect value of the system clock before hwclock is run). Then after dhclient does a DHCP discover (still on Nov 3), the clock suddenly changes to Dec 13 (the correct date) due to hwclock being run. Of course this causes dhclient to instantly timeout waiting for DHCP offers. The current configuration for hwclock.service is as follows (in a file named "hwclock.service"): [Unit] Description=Real-time Clock Service Before=networking.service [Service] Type=oneshot ExecStartPre=/usr/local/bin/pcf8523 ExecStart=/sbin/hwclock -s RemainAfterExit=yes [Install] WantedBy=multi-user.target What is the best way to configure the hwclock.service so that the hwclock command "completely finishes and the system time is fully set" *before* the networking.service is started? |
The "Before=networking.service" only means that the network will start after your this process is called doesn't matter if it has finished or not. My guess is the clock setting program doesn't have time to finish before the the network is being called.
In your networking.service under [Unit] add Code:
Requires=hwclock.service |
Quote:
There's another service called "ifup@.service" that also does an "ifup". I guess this is a template service with the @ symbol representing the interface name. But I don't see any references to the "ifup@.service" in any other service. What I'd really like is a dump of all the services that are running, along with some information about what caused each service to be activated. "systemctl list-dependencies" doesn't really give much information other than what's in the systemd files. |
Code:
systemctl | grep running Code:
systemctl --help Edit: maybe Code:
systemctl -r list-dependencies |
Quote:
I don't know what is causing the ifup@.service to activate. It's not referenced anywhere by any other service. Nothing "Requires=ifup@.service" and ifup@.service doesn't have a "RequiredBy=". So how do I figure out what is causing this ifup@.service to get activated? Basically I just want to set the damn clock very early in the boot process, before a clock change can interfere with processes that depending on the clock for their function. Surely this cannot be that difficult. systemd seems to have gone out of its way to make this as convoluted as possible, requiring you to reverse-engineer and fully comprehend the entire architecture of 100 service inter-dependencies before you can safely make a single sane modification. To make matters worse, setting the clock sometimes screws up systemd, because systemd depends on the clock to know what it's doing, and changing it mid-process sometimes causes it to do strange things. Ok rant over, open to any suggestions. Thanks! |
If the ifup@.service is enabled, it's going to start at boot. In other words, it's "activated" by being there.
Did you try also adding Code:
Requires=hwclock.service Now you've introduced another service? ifup@eth0.service ?? Why would eth0 not being present affect the service waiting for ifup? ifup happens and eth0 then fails? So what? Aside: May I ask why you're concerned about the hardware clock? Are you not running ntpd to keep the system time correct? |
Quote:
Quote:
Quote:
Quote:
|
It seems everyone is moving towards NetworkManager.service to run the networking part. Do you have this?
|
Quote:
|
What flavor of Linux is it using? Could be a badly trimmed down version of something.
|
Quote:
I appreciate the thoughts and ideas, but if the best-practice solution to the problem of "How do I get systemd to set my clock early" is: "Oh if you want to set your clock early then first you have to switch to NetworkManager and rewrite all your network config code," then I guess I'll just figure something else out. Surely there's a better answer? |
No, I'm not saying that at all. I gave you what I believe is need to allow things to boot in the order you want them to.
|
Quote:
|
I'm certainly no expert, but isn't what the OP is asking for done with After?
In the ifup and/or networking service(s) Code:
After=hwclock.service |
Here's a page I found that might help you track down your issues.
How to Manage ‘Systemd’ Services and Units Using ‘Systemctl’ in Linux |
All times are GMT -5. The time now is 11:20 AM. |