From a7954a7d0300c57755c377bc95ae4c799e8b8c82 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Thu, 1 Feb 2024 09:46:14 +0100 Subject: [slx-clock] Order ntp sync after fetch config --- modules.d/slx-clock/hooks/s3-ntp-sync.sh | 84 ++++++++++++------------ modules.d/slx-clock/module-setup.sh | 4 +- modules.d/slx-clock/services/s3-ntp-sync.service | 2 + 3 files changed, 46 insertions(+), 44 deletions(-) diff --git a/modules.d/slx-clock/hooks/s3-ntp-sync.sh b/modules.d/slx-clock/hooks/s3-ntp-sync.sh index 96e72c04..7bbcd14a 100755 --- a/modules.d/slx-clock/hooks/s3-ntp-sync.sh +++ b/modules.d/slx-clock/hooks/s3-ntp-sync.sh @@ -3,51 +3,51 @@ . /etc/openslx -ntp_sync() { - # 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 - SLX_DHCP_NTP="$SLX_DHCP_NTP $SERVER" +# 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 + SLX_DHCP_NTP="$SLX_DHCP_NTP $SERVER" + fi +done +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 - done - local SUCCESS= - for SERVER in $SLX_DHCP_NTP; do - if timeout 3 ntpd -q -n -p "$SERVER"; then - echo "Successfully queried $SERVER for time." - if [ "x$SLX_BIOS_CLOCK" = "xlocal" ]; then - usleep 100000 - hwclock -l -w || echo "... but could not set BIOS clock to localtime" - elif [ "x$SLX_BIOS_CLOCK" = "xutc" ]; then - usleep 100000 - hwclock -u -w || echo "... but could not set BIOS clock to UTC" + SUCCESS=1 + break + fi + echo "Error querying $SERVER for current time." +done +if [ -z "$SUCCESS" ]; then + echo "No NTP server reachable" + # See if we have a timestamp in our server-config - should only be a few seconds off by now + if [ -n "$SLX_NOW" ] && [ "$SLX_NOW" -gt 1234567890 ]; then + TTS="$SLX_NOW" + CONFIG_DOWNLOAD_TIME=$( cat /tmp/config-download-time 2> /dev/null ) + if [ -n "$CONFIG_DOWNLOAD_TIME" ]; then + # times are in centiseconds + NOW_TIME=$(sed -r 's/^([0-9]+)\.([0-9][0-9]).*$/\1\2/' /proc/uptime) + if [ "$CONFIG_DOWNLOAD_TIME" -gt 0 ] && [ "$NOW_TIME" -gt 0 ]; then + TTS=$(( TTS + ( NOW_TIME - CONFIG_DOWNLOAD_TIME ) / 100 )) fi - SUCCESS=1 - break fi - echo "Error querying $SERVER for current time." - done - if [ -z "$SUCCESS" ]; then - echo "No NTP server reachable" - # See if we have a timestamp in our server-config - should only be a few seconds off by now - if [ -n "$SLX_NOW" ] && [ "$SLX_NOW" -gt 1234567890 ]; then - TTS="$SLX_NOW" - if [ -n "$CONFIG_DOWNLOAD_TIME" ]; then - NOW_TIME=$(sed -r 's/^([0-9]+)\.([0-9]+).*$/\1\2/' /proc/uptime) - if [ "$CONFIG_DOWNLOAD_TIME" -gt 0 ] && [ "$NOW_TIME" -gt 0 ]; then - TTS=$(( TTS + ( NOW_TIME - CONFIG_DOWNLOAD_TIME ) / 100 )) - fi - fi - echo "Setting time to SLX_NOW ($SLX_NOW, corrected $TTS)" - date -s "@$TTS" - else - echo "No fallback option for timesync available, relying on correct RTC setup" - if [ "x$SLX_BIOS_CLOCK" = "xlocal" ]; then - # Linux defaults to RTC = UTC, so read again in this case - hwclock -l -s - fi + echo "Setting time to SLX_NOW ($SLX_NOW, corrected $TTS)" + date -s "@$TTS" + else + echo "No fallback option for timesync available, relying on correct RTC setup" + if [ "$SLX_BIOS_CLOCK" = "local" ]; then + # Linux defaults to RTC = UTC, so read again in this case + hwclock -l -s fi fi - echo "Timesync finished" -} +fi -ntp_sync +echo "Timesync finished" +exit 0 diff --git a/modules.d/slx-clock/module-setup.sh b/modules.d/slx-clock/module-setup.sh index b99f7640..0977522e 100755 --- a/modules.d/slx-clock/module-setup.sh +++ b/modules.d/slx-clock/module-setup.sh @@ -16,8 +16,8 @@ install() { inst_simple "${moddir}/services/${_name}.service" \ "${systemdsystemunitdir}/${_name}.service" mkdir --parents \ - "${initdir}/${systemdsystemunitdir}/dracut-pre-pivot.service.requires" + "${initdir}/${systemdsystemunitdir}/initrd.target.wants" ln_r "${systemdsystemunitdir}/${_name}.service" \ - "${systemdsystemunitdir}/dracut-pre-pivot.service.requires/${_name}.service" + "${systemdsystemunitdir}/initrd.target.wants/${_name}.service" done } diff --git a/modules.d/slx-clock/services/s3-ntp-sync.service b/modules.d/slx-clock/services/s3-ntp-sync.service index 77208d0f..6cb9eccd 100644 --- a/modules.d/slx-clock/services/s3-ntp-sync.service +++ b/modules.d/slx-clock/services/s3-ntp-sync.service @@ -2,6 +2,8 @@ Description=Sync time via NTP Requires=s3-setup-bootif-network.service After=s3-setup-bootif-network.service +Requires=s3-fetch-config.service +After=s3-fetch-config.service DefaultDependencies=no IgnoreOnIsolate=true -- cgit v1.2.3-55-g7522