summaryrefslogtreecommitdiffstats
path: root/modules.d/slx-clock
diff options
context:
space:
mode:
authorSimon Rettberg2025-04-02 17:03:17 +0200
committerSimon Rettberg2025-04-02 17:03:17 +0200
commit0493ed7a9192b2f10e5acede5d319aa8a76adf87 (patch)
treea951876d8fa795e51960991dd00a8a8ac6462c92 /modules.d/slx-clock
parentAvoid loading dracut-lib.sh in several scripts (diff)
downloadsystemd-init-0493ed7a9192b2f10e5acede5d319aa8a76adf87.tar.gz
systemd-init-0493ed7a9192b2f10e5acede5d319aa8a76adf87.tar.xz
systemd-init-0493ed7a9192b2f10e5acede5d319aa8a76adf87.zip
[slx-clock] Try to guess RTC mode and write time back if no mode is specified
Diffstat (limited to 'modules.d/slx-clock')
-rwxr-xr-xmodules.d/slx-clock/hooks/s3-ntp-sync.sh31
1 files changed, 24 insertions, 7 deletions
diff --git a/modules.d/slx-clock/hooks/s3-ntp-sync.sh b/modules.d/slx-clock/hooks/s3-ntp-sync.sh
index 7bbcd14a..c4be6161 100755
--- a/modules.d/slx-clock/hooks/s3-ntp-sync.sh
+++ b/modules.d/slx-clock/hooks/s3-ntp-sync.sh
@@ -3,6 +3,28 @@
. /etc/openslx
+adjust_rtc() {
+ if [ "$SLX_BIOS_CLOCK" = "local" ]; then
+ usleep 100000
+ hwclock -l -w || echo "... but could not set BIOS clock to localtime"
+ elif [ "$SLX_BIOS_CLOCK" = "utc" ]; then
+ usleep 100000
+ hwclock -u -w || echo "... but could not set BIOS clock to UTC"
+ else
+ local rtc sys diff arg
+ for arg in "-l" "-u"; do
+ rtc=$( date -d "$( LC_ALL=C hwclock "$arg" | sed -r 's/ *[0-9.]+ seconds$//' )" +%s )
+ sys=$( date +%s )
+ diff=$(( sys - rtc ))
+ if [ "$diff" -gt -900 ] && [ "$diff" -lt 900 ]; then
+ # Less than 15 minutes off, assume it's the correct mode
+ hwclock "$arg" -w || echo "... but could not set BIOS clock ($arg)"
+ break
+ fi
+ done
+ fi
+}
+
# Try to merge with servers from DHCP. Prefer DHCP, skip duplicates
for SERVER in $SLX_NTP_SERVER; do
if ! echo "$SLX_DHCP_NTP" | grep -wq "$SERVER"; then
@@ -13,13 +35,7 @@ SUCCESS=
for SERVER in $SLX_DHCP_NTP; do
if timeout 3 ntpd -q -n -p "$SERVER"; then
echo "Successfully queried $SERVER for time."
- if [ "$SLX_BIOS_CLOCK" = "local" ]; then
- usleep 100000
- hwclock -l -w || echo "... but could not set BIOS clock to localtime"
- elif [ "$SLX_BIOS_CLOCK" = "utc" ]; then
- usleep 100000
- hwclock -u -w || echo "... but could not set BIOS clock to UTC"
- fi
+ adjust_rtc
SUCCESS=1
break
fi
@@ -40,6 +56,7 @@ if [ -z "$SUCCESS" ]; then
fi
echo "Setting time to SLX_NOW ($SLX_NOW, corrected $TTS)"
date -s "@$TTS"
+ adjust_rtc
else
echo "No fallback option for timesync available, relying on correct RTC setup"
if [ "$SLX_BIOS_CLOCK" = "local" ]; then