From 05aa46571fa655ab72a4b673de4b28a69423481e Mon Sep 17 00:00:00 2001 From: Ivan Romanov Date: Sat, 17 Oct 2026 20:11:58 +0500 Subject: [PATCH] chrony: add UCI options for drift file and time restoration chronyd can restore the system time at startup from the drift file's modification time. This helps routers without a real-time clock recover a more recent time after reboot when the drift file is stored on persistent storage. Add a UCI section to configure the drift file path, its minimum update interval, and optional time restoration using chronyd's -s option. Keep time restoration disabled by default and preserve the existing drift file path. New section created by uci-defaults. ```uci config driftfile 'driftfile' # Set to 1 to restore time from the drift file at startup option restore_time '0' # Use a path on persistent storage to retain the file across reboots option file '/var/run/chrony/drift' # Minimum update interval in seconds; empty uses chronyd's default option interval '3600' ``` Signed-off-by: Ivan Romanov --- net/chrony/Makefile | 4 +++- net/chrony/files/chrony.conf | 3 --- net/chrony/files/chrony.config | 5 +++++ net/chrony/files/chrony.defaults | 11 +++++++++++ net/chrony/files/chronyd.init | 28 +++++++++++++++++++++++++++- 5 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 net/chrony/files/chrony.defaults diff --git a/net/chrony/Makefile b/net/chrony/Makefile index 7225d09a36ca95..518372d1b3b278 100644 --- a/net/chrony/Makefile +++ b/net/chrony/Makefile @@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=chrony PKG_VERSION:=4.9 -PKG_RELEASE:=2 +PKG_RELEASE:=3 PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz PKG_SOURCE_URL:=https://chrony-project.org/releases/ @@ -97,11 +97,13 @@ define Package/chrony/install $(INSTALL_DIR) $(1)/etc/init.d $(INSTALL_DIR) $(1)/etc/config $(INSTALL_DIR) $(1)/etc/chrony + $(INSTALL_DIR) $(1)/etc/uci-defaults $(INSTALL_DIR) $(1)/etc/hotplug.d/iface $(INSTALL_BIN) ./files/chrony.hotplug $(1)/etc/hotplug.d/iface/20-chrony $(INSTALL_BIN) ./files/chronyd.init $(1)/etc/init.d/chronyd $(INSTALL_CONF) ./files/chrony.config $(1)/etc/config/chrony $(INSTALL_CONF) ./files/chrony.conf $(1)/etc/chrony/chrony.conf + $(INSTALL_BIN) ./files/chrony.defaults $(1)/etc/uci-defaults/chrony endef Package/chrony-nts/install= $(Package/chrony/install) diff --git a/net/chrony/files/chrony.conf b/net/chrony/files/chrony.conf index a4d24a7c6f5786..2c9d37d0712bb5 100644 --- a/net/chrony/files/chrony.conf +++ b/net/chrony/files/chrony.conf @@ -13,8 +13,5 @@ noclientlog # Mark the system clock as synchronized rtcsync -# Record the clock's drift -driftfile /var/run/chrony/drift - # Save NTS keys and cookies ntsdumpdir /var/run/chrony diff --git a/net/chrony/files/chrony.config b/net/chrony/files/chrony.config index f9d838c2a487c4..b66046f4b7738f 100644 --- a/net/chrony/files/chrony.config +++ b/net/chrony/files/chrony.config @@ -21,6 +21,11 @@ config nts 'nts' option rtccheck '1' option systemcerts '1' +config driftfile 'driftfile' + option restore_time '0' + option file '/var/run/chrony/drift' + option interval '3600' + #config smoothtime 'smoothtime' # option maxppm '400' # option maxwander '0.01' diff --git a/net/chrony/files/chrony.defaults b/net/chrony/files/chrony.defaults new file mode 100644 index 00000000000000..0573d7560a26fd --- /dev/null +++ b/net/chrony/files/chrony.defaults @@ -0,0 +1,11 @@ +#!/bin/sh + +uci -q get chrony.driftfile >/dev/null && exit 0 + +uci -q batch <<-EOF + set chrony.driftfile=driftfile + set chrony.driftfile.restore_time='0' + set chrony.driftfile.file='/var/run/chrony/drift' + set chrony.driftfile.interval='3600' + commit chrony +EOF diff --git a/net/chrony/files/chronyd.init b/net/chrony/files/chronyd.init index aa535b1989d699..e75bcd8ff1bdfd 100644 --- a/net/chrony/files/chronyd.init +++ b/net/chrony/files/chronyd.init @@ -8,6 +8,7 @@ HOTPLUG=/usr/sbin/chrony-hotplug CONFIGFILE=/etc/chrony/chrony.conf INCLUDEFILE=/var/etc/chrony.d/10-uci.conf RTCDEVICE=/dev/rtc0 +DRIFTFILE=/var/run/chrony/drift handle_source() { local cfg=$1 sourcetype=$2 disabled hostname minpoll maxpoll iburst nts @@ -117,11 +118,36 @@ handle_logging() { [ -n "$logchange" ] && echo logchange "$logchange" } +handle_driftfile() { + local cfg=$1 file interval + + config_get file "$cfg" file $DRIFTFILE + config_get interval "$cfg" interval + + local dir=$(dirname "$file") + if [ ! -d "$dir" ]; then + mkdir -p "$dir" + chown chrony:chrony "$dir" + fi + + if [ -n "$interval" ]; then + echo driftfile $file interval $interval + else + echo driftfile $file + fi +} + start_service() { . /lib/functions/network.sh + config_load chrony + # Restoring time requires the -s command-line option; no configuration directive exists. + local restore_time + config_get_bool restore_time driftfile restore_time 0 + procd_open_instance procd_set_param command $PROG -n + [ "$restore_time" = "1" ] && procd_append_param command -s procd_set_param file $CONFIGFILE procd_set_param file $INCLUDEFILE procd_close_instance @@ -130,7 +156,6 @@ start_service() { procd_set_param command $HOTPLUG procd_close_instance - config_load chrony mkdir -p $(dirname $INCLUDEFILE) ( @@ -143,5 +168,6 @@ start_service() { config_foreach handle_systemclock systemclock config_foreach handle_nts nts config_foreach handle_logging logging + config_foreach handle_driftfile driftfile ) > $INCLUDEFILE }