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

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


Handle NTP servers given by DHCP through NetworkManager in a
similar way to how distributions have done in 11-dhclient, e.g. [1].
New NTP servers are written as entries to a file per-interface in
CHRONY_SERVER_DIR, which is read by chrony.helper when updating
the chrony service.

This provides a way for NTP server configuration to be carried over
from NetworkManager DHCP events to chrony, for DHCP clients other
than dhclient. Part of fixing integration where the NetworkManager
internal client is used, e.g [2].

Since the marker @CHRONY_SERVER_DIR@ requires replacing, rename the
file with `.in` suffixed.

[1] https://src.fedoraproject.org/rpms/dhcp/blob/master/f/11-dhclient
[2] https://bugzilla.redhat.com/show_bug.cgi?id=1800901
---
 examples/chrony.nm-dispatcher    | 15 ----------
 examples/chrony.nm-dispatcher.in | 49 ++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 15 deletions(-)
 delete mode 100644 examples/chrony.nm-dispatcher
 create mode 100644 examples/chrony.nm-dispatcher.in

diff --git a/examples/chrony.nm-dispatcher b/examples/chrony.nm-dispatcher
deleted file mode 100644
index 0b0c3e7..0000000
--- a/examples/chrony.nm-dispatcher
+++ /dev/null
@@ -1,15 +0,0 @@
-#!/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
-
-export LC_ALL=C
-
-# For NetworkManager consider only up/down events
-[ $# -ge 2 ] && [ "$2" != "up" ] && [ "$2" != "down" ] && exit 0
-
-# Note: for networkd-dispatcher routable.d ~= on and off.d ~= off
-
-chronyc onoffline > /dev/null 2>&1
-
-exit 0
diff --git a/examples/chrony.nm-dispatcher.in b/examples/chrony.nm-dispatcher.in
new file mode 100644
index 0000000..2acf97f
--- /dev/null
+++ b/examples/chrony.nm-dispatcher.in
@@ -0,0 +1,49 @@
+#!/bin/sh
+# This is a NetworkManager dispatcher / networkd-dispatcher script for
+# 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
+
+# Make sure the two action interface and action arguments are passed by
+# NetworkManager before continuing.
+[ $# -lt 2 ] && exit 0
+
+interface=$1
+action=$2
+
+server_dir=@CHRONY_SERVER_DIR@
+dhcp_server_file=$server_dir/chrony.servers.$interface
+
+mkdir -p $server_dir
+
+add_servers_from_dhcp() {
+    rm -f "$dhcp_server_file"
+    # DHCP4_NTP_SERVERS is passed from DHCP options by
+    # NetworkManager.
+    for server in $DHCP4_NTP_SERVERS; do
+        echo "$server iburst" >> "$dhcp_server_file"
+    done
+    /usr/libexec/chrony-helper update-daemon || :
+}
+
+clear_servers_from_dhcp() {
+    if [ -f "$dhcp_server_file" ]; then
+        rm -f "$dhcp_server_file"
+        /usr/libexec/chrony-helper update-daemon || :
+    fi
+}
+
+if [ "$action" = "up" ] || [ "$action" = "dhcp4-change" ]; then
+    add_servers_from_dhcp
+elif [ "$action" = "down" ]; then
+    clear_servers_from_dhcp
+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/