I also had a look at https://github.com/mattwire/upspico/tree/master/systemd . 
But I think it is not needed to write the system clock to rtc on shutdown, 
because systemd-timesyncd or timedatectl (dont know which of those is actually 
getting ntp sync and write to rtc) are updating the rtc after each ntp sync 
automatically. Also the sync from rtc to system time should be earlier in the 
boot process than defined in this systemd service files.
Updating the time on the rtc is not an issue the user has to care about if the 
rtc is reachable at/as /etc/rtc0 and therefore recognized by 
timedatectl/systemd-timesyncd.
The problem is reading the rtc at the correct point in the boot process. Early 
enough to have as early as possible the correct time, but not too early, 
otherwise the rtc modules are not loaded and therefore, it would fail to read 
the rtc.

After investigating this issue two weeks I think the best approach is to create 
a systemd service. Creating a systemd service avoids messing around with the 
/lib/udev/hwclock-set which gets overwritten by an update, and also a systemd 
service is doing a similar thing which the fake-hwclock.service is doing, just 
later in the boot process, because the modules have to be loaded before.

I took the fake-hwclock.service as a blueprint and wrote a service which is 
doing the same if systemd is present on the system.

The systemd service file looks like (/etc/systemd/system/hwclock-start.service) 
this:

'
[Unit]
Description=read rtc and write to system clock
After=sysinit.target

[Service]
Type=oneshot
ExecStart=/sbin/hwclock --hctosys --utc

[Install]
WantedBy=basic.target

'

After=sysinit.target is needed, so that the modules are safely loaded before, 
and WantedBy=basic.target so that it is done as early as possible in the boot 
process to get as early as possible the correct time. 
https://www.freedesktop.org/software/systemd/man/bootup.html#

A "sudo journalctl -b | grep rtc' print out something like this:

"
Nov 03 18:16:44 raspberrypi kernel: rtc-ds1307 1-0068: registered as rtc0
Aug 27 09:04:30 raspberrypi systemd[1]: Starting read rtc and write to system 
clock...
Aug 27 14:34:20 raspberrypi systemd[1]: Started read rtc and write to system 
clock.
"

First, the kernel does not know which time it is and took some old point in 
time, maybe when it was compiled or so. Then systemd-timesyncd corrects the 
time to the last known ntp sync, and afterwards the hwclock-start.service will 
sync with the rtc to get the correct time.

The hwclock-start.service is all which is needed to get the correct time from 
the rtc and getting it functional at least in raspbian.

So just two modifications in /boot/config.txt are needed:

Uncomment the following to get i2c working:
'
dtparam=i2c_arm=on
'

and add as the last line (empty line at the end is important!!!):

'
# enable RTC
dtoverlay=i2c-rtc,ds3231

'

to load the rtc modules meanwhile the boot process.

Then create the service file and activate it. 
Then ntp is working, ntp updates the rtc and the rtc is read in meanwhile the 
boot process without messing around in any system file or setting.

Please let me/us know, if this approach makes sense, or if still something is 
missing.

Reply via email to