Hi Miroslav, Following discussion in https://bugzilla.redhat.com/show_bug.cgi?id=1800901, this patch carries over NTP config for DHCP client backends in NetworkManagerother than dhclient. I have tested this with a Fedora machine using the NetworkManager internal client connecting to a DHCP server where the NTP servers option was specified. NTP config is written to/var/lib/chrony-servers, and ends up shown with `chronyc_sources`.
Requires the patch in https://src.fedoraproject.org/rpms/chrony/pull-request/3 Thanks, Robert Fairley >From a7a9efe6fcbf35747ac407514cdd3a18bea8acf3 Mon Sep 17 00:00:00 2001 From: Robert Fairley <rfair...@redhat.com> Date: Wed, 20 May 2020 14:36:38 -0400 Subject: [PATCH] nm-dispatcher: handle NTP servers from DHCP 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-requ...@chrony.tuxfamily.org with "unsubscribe" in the subject. For help email chrony-dev-requ...@chrony.tuxfamily.org with "help" in the subject. Trouble? Email listmas...@chrony.tuxfamily.org.