[PATCH] nm-dispatcher: handle NTP servers from DHCP

[ Thread Index | Date Index | More chrony.tuxfamily.org/chrony-dev Archives ]


Currently, 11-dhclient [1] in Fedora writes NTP servers and their
chrony config into `/var/lib/dhclient`. This adds functionality to
do this if dhclient is not installed. A new directory
`/var/lib/chrony-servers` is introduced and set through an
environment variable $CHRONY_SERVER_DIR to override the default
location in `chrony-helper`. Once chrony-helper is called, it will
read the NTP servers from $CHRONY_SERVER_DIR and update chrony
sources in the same way. Required to add chrony/NM integration to
fix [2].

Requires patching `chrony-helper` in downstream packages to read the
$CHRONY_SERVER_DIR environment variable and override the default
$dhclient_servers_files variable, e.g.: [3]

[1] https://src.fedoraproject.org/rpms/dhcp/blob/master/f/11-dhclient
[2] https://bugzilla.redhat.com/show_bug.cgi?id=1800901
[3] https://src.fedoraproject.org/rpms/chrony/pull-request/3
---
 examples/chrony.nm-dispatcher | 59 +++++++++++++++++++++++++++++++----
 1 file changed, 53 insertions(+), 6 deletions(-)

diff --git a/examples/chrony.nm-dispatcher b/examples/chrony.nm-dispatcher
index 0b0c3e7..b59705f 100644
--- a/examples/chrony.nm-dispatcher
+++ b/examples/chrony.nm-dispatcher
@@ -1,15 +1,62 @@
 #!/bin/sh
 # This is a NetworkManager dispatcher / networkd-dispatcher script for
-# chronyd to set its NTP sources online or offline when a network interface
-# is configured or removed
+# chronyd to handle its NTP sources. It sets the NTP sources online or
+# offline when a network interface is configured or removed. On DHCP
+# change, chrony will update its NTP sources passed from DHCP options.

 export LC_ALL=C

-# For NetworkManager consider only up/down events
-[ $# -ge 2 ] && [ "$2" != "up" ] && [ "$2" != "down" ] && exit 0
+# Make sure the two action interface and action arguments are passed by
+# NetworkManager before continuing.
+[ $# -lt 2 ] && exit 0

-# Note: for networkd-dispatcher routable.d ~= on and off.d ~= off
+interface=$1
+action=$2
+# $CHRONY_SERVER_DIR is read by `chrony-helper`.
+export CHRONY_SERVER_DIR=/var/lib/chrony-servers
+SERVERFILE=$CHRONY_SERVER_DIR/chrony.servers.$interface

-chronyc onoffline > /dev/null 2>&1
+mkdir -p "$CHRONY_SERVER_DIR"
+
+# Respect network config including $PEERNTP and $NTPSERVERARGS.
+[ -f /etc/sysconfig/network ] && . /etc/sysconfig/network
+[ -f /etc/sysconfig/network-scripts/ifcfg-"${interface}" ] && \
+    . /etc/sysconfig/network-scripts/ifcfg-"${interface}"
+
+add_servers_from_dhcp() {
+    rm -f "$SERVERFILE"
+    if [ "$PEERNTP" != "no" ]; then
+        # $DHCP4_NTP_SERVERS is passed from DHCP options by
+        # NetworkManager.
+        for server in $DHCP4_NTP_SERVERS; do
+            echo "$server ${NTPSERVERARGS:-iburst}" >> "$SERVERFILE"
+        done
+        /usr/libexec/chrony-helper update-daemon || :
+    fi
+}
+
+clear_servers() {
+    if [ -f "$SERVERFILE" ]; then
+        rm -f "$SERVERFILE"
+        /usr/libexec/chrony-helper update-daemon || :
+    fi
+}
+
+# Check that a dhclient installation is not present, to avoid a
+# redundant operation with dhclient which handles NTP server
+# config through its own NetworkManager dispatcher script
+# 11-dhclient.
+if [ ! -e /usr/sbin/dhclient ]; then
+    if [ "$action" = "up" ] || [ "$action" = "dhcp4-change" ]; then
+        add_servers_from_dhcp
+    elif [ "$action" = "down" ]; then
+        clear_servers
+    fi
+fi
+
+if [ "$action" = "up" ] || [ "$action" = "down" ]; then
+    # Note: for networkd-dispatcher routable.d ~= on and off.d ~= off
+    chronyc onoffline > /dev/null 2>&1
+fi

 exit 0
--
2.26.2


-- 
To unsubscribe email chrony-dev-request@xxxxxxxxxxxxxxxxxxxx with "unsubscribe" in the subject.
For help email chrony-dev-request@xxxxxxxxxxxxxxxxxxxx with "help" in the subject.
Trouble?  Email listmaster@xxxxxxxxxxxxxxxxxxxx.


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/