4

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?

2
  • 2
    This is done by udev when /dev/rtc shows up: github.com/systemd/systemd/blob/… Commented Oct 13, 2021 at 15:01
  • 1
    @HenriMenke that just creates the /dev/rtc symlink. Commented Oct 13, 2021 at 15:24

1 Answer 1

3

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.

3
  • hwclock-set script has a condition for ".../system" folder. When it does exist, the script exits, leaving the clock intact. Commented 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. Commented 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-set script makes sense to me as it includes: /sbin/hwclock --rtc=$dev --hctosys. The if condition you mention is done early in the boot cycle & the system file has not yet been created. This (AIUI) is necessary for adjusting the system clock from the hardware clock IAW warnings in man hwclock. Commented Feb 11 at 1:56

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.