diff options
| -rwxr-xr-x | modules.d/slx-clock/hooks/s3-ntp-sync.sh | 31 |
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 |
