Sync system clock from RTC on boot

folks!

I have a single board computer (HardKernel Odroid C2) that has no builtin RTC and need an external time source on each system boot. While I have a local NTP server, I also need RTC just in case of rare power fail, that will force NTP server to reboot and loose its time as well. As time is vital for the system functioning, I built RTC using PCF8563 I2C RTC. The problem is that it needs special kernel modules to make a system usable RTC and I have to load this modules during boot process. So the system starts with incorrect time, loads modules needed and then sets the system time from RTC with a help of /etc/rc.local:

#Set system datetime from RTC
logger "Starting system clock synchronization with RTC"
if [ -e /dev/rtc ]; then /sbin/hwclock --hctosys
fi
logger "System clock synchronized to RTC"

The problem is that systemd seems to treat the period in between early boot process and clock setup as kind of "incorrect" writing no logs. I see no logs at all after system goes down and system clock setup:

Jan 5 23:20:39 tank systemd-journald[206]: Journal stopped
Jan 5 23:20:55 tank root[394]: System clock synchronized to RTC <- this is the second logger message
Jan 5 23:20:55 tank systemd[1]: Started /etc/rc.local Compatibility.
...
Jan 5 23:21:07 tank chronyd[384]: Forward time jump detected!

Synchronizing system clock from RTC by running hwhlock --hctosys from /etc/rc.local is the suggested method by the HardKernel. But that seems totally wrong as system clearly work for some period with predefined time before it runs rc.local. So, is there are any ways to set system clock from RTC early?

4

1 Answer

I spent 2 days and finally found the exact solution. The problem is the rc.local is executed way to late after system boots up. This leads to several problems: 1) part (including valuable kernel and early system start up) or all logs dropped dew to time jump, that makes journal discard all logs prior new date 2) if dew to some reason system clock will be updated after system logger (syslogd or syslog-ng in my case) is started, there will be no logs after last shut down at all. While system is still functional, this is a major failure as there is no way to investigate anything on a running system. After some exhausting experimenting with no logs, I found the combination (pretty obvious, actually) to set system clock at the most yearly time possible:

/lib/systemd/system/hwrtc.service

[Unit]
Description=Synchronise System clock to hardware RTC
DefaultDependencies=no
After=systemd-modules-load.service
Before=systemd-journald.service time-sync.target sysinit.target shutdown.target
Conflicts=shutdown.target
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/sbin/hwclock --hctosys --utc --noadjfile
RestrictRealtime=yes
[Install]
WantedBy=sysinit.target

And enable this service. This will make system start hwclock right after mandatory modules are loaded and before journal is started, so all logs will be marked with actual time restored from RTC saving all messages including the very first kernel ones.

5

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like