When Linux is booting, the current time must be fetched from a RTC chip and then set to Linux system date/time. I can do this manually by running hwclock --hctosys. But where exactly does this happen automatically at boot time? Is there a process taking care of this? Does the kernel do this job?
1 Answer
Method 1: Linux Kernel loads time from RTC chip
One can set CONFIG_RTC_HCTOSYS in kernel config to enable this feature. When enabled, the Linux Kernel will take care of reading stored time in rtc0 and setting it to system time. With CONFIG_RTC_HCTOSYS_DEVICE="rtc0" the device can be altered.
For this to work the according RTC driver must be compiled as built-in module. This can be determined as follows:
$ cat /boot/config-$(uname -r) | grep -i RTC_HCTOSYS
Method 2: udev calls hwclock as soon as /dev/rtcX is created
The package util-linux provides a udev rule file at /usr/lib/udev/rules.d/85-hwclock.rules which can be copied to /etc/udev/rules.d/ (as illustrated below). The udev daemon will then call /usr/lib/udev/hwclock-set automatically as soon as /dev/rtc0 is created. You can change rtc0 in 85-hwclock.rules if needed.
$ sudo cp /usr/lib/udev/rules.d/85-hwclock.rules /etc/udev/rules.d/85-hwclock.rules
Note: Both methods accommodate the warnings in man hwclock against using the --hctosys option with hwclock on a running system. Method 2 is probably easiest for most.
-
hwclock-setscript has a condition for ".../system" folder. When it does exist, the script exits, leaving the clock intact.Daniel– Daniel2023-07-10 14:34:52 +00:00Commented Jul 10, 2023 at 14:34 -
That folder is being created early (much earlier than udev starts) so that script is useless. Moreover, commenting out that condition, will cause access denied errors on "hwclock" calls (when the script is called by udev). So it seems totally useless.Daniel– Daniel2023-07-10 14:35:38 +00:00Commented Jul 10, 2023 at 14:35
-
@Daniel: Have you actually tried Method 2? I ask b/c it seems to be a solution to a problem I've been having with an "embedded/SBC" system. And the
hwclock-setscript makes sense to me as it includes:/sbin/hwclock --rtc=$dev --hctosys. Theifcondition you mention is done early in thebootcycle & thesystemfile has not yet been created. This (AIUI) is necessary for adjusting the system clock from the hardware clock IAW warnings inman hwclock.Seamus– Seamus2025-02-11 01:56:38 +00:00Commented Feb 11 at 1:56
/dev/rtcshows up: github.com/systemd/systemd/blob/…/dev/rtcsymlink.