chrony: improve hotplug script

- Use the chronyc onoffline command to update state of all sources
  per current routing configuration
- Don't ignore the "ifupdate" action
- Add NTP servers from DHCP for the interface that went up instead of
  the wan4+wan6 interfaces
- Save the servers to files loaded by the sourcedir directive to not
  lose them when chronyd is restarted, and remove them when the
  interface goes down

Signed-off-by: Miroslav Lichvar <mlichvar0@gmail.com>
This commit is contained in:
Miroslav Lichvar 2020-10-30 20:02:48 +01:00
parent 1dc8c501a6
commit b9d6d6cdd0
2 changed files with 21 additions and 17 deletions

View File

@ -1,6 +1,9 @@
# Load UCI configuration
confdir /var/etc/chrony.d
# Load NTP servers from DHCP if enabled in UCI
sourcedir /var/run/chrony-dhcp
# Log clock errors above 0.5 seconds
logchange 0.5

View File

@ -1,20 +1,18 @@
#!/bin/sh
# Set chronyd online/offline status, allow NTP access and add servers from DHCP
[ "$ACTION" = ifup -o "$ACTION" = ifdown ] || exit 0
SOURCEFILE="/var/run/chrony-dhcp/$INTERFACE.sources"
run_command() {
/usr/bin/chronyc -n "$*" > /dev/null 2>&1
}
run_command tracking || exit 0
run_command onoffline
. /lib/functions/network.sh
network_find_wan iface4
network_find_wan6 iface6
run_command $([ -n "$iface4" ] && echo online || echo offline) 0.0.0.0/0.0.0.0
run_command $([ -n "$iface6" ] && echo online || echo offline) ::/0
if [ "$ACTION" = ifdown ] && [ -f "$SOURCEFILE" ]; then
rm -f "$SOURCEFILE"
run_command reload sources
fi
[ "$ACTION" = ifup ] || exit 0
@ -32,13 +30,16 @@ done
. /usr/share/libubox/jshn.sh
for iface in $iface4 $iface6; do
json_load "$(ifstatus $iface)"
json_select data
json_get_var dhcp_ntp_servers ntpserver
json_load "$(ifstatus "$INTERFACE")"
json_select data
json_get_var dhcp_ntp_servers ntpserver
for server in $dhcp_ntp_servers; do
run_command add $(NTP_SOURCE_HOSTNAME=$server config_foreach \
handle_source dhcp_ntp_server server)
done
done
[ -z "$dhcp_ntp_servers" ] && exit 0
mkdir -p "$(dirname "$SOURCEFILE")"
for NTP_SOURCE_HOSTNAME in $dhcp_ntp_servers; do
config_foreach handle_source dhcp_ntp_server server
done > "$SOURCEFILE"
run_command reload sources