packages/net/chrony/files/chrony.hotplug
Miroslav Lichvar b9d6d6cdd0 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>
2020-10-30 20:02:48 +01:00

45 lines
1,006 B
Bash

#!/bin/sh
# Set chronyd online/offline status, allow NTP access and add servers from DHCP
SOURCEFILE="/var/run/chrony-dhcp/$INTERFACE.sources"
run_command() {
/usr/bin/chronyc -n "$*" > /dev/null 2>&1
}
run_command onoffline
if [ "$ACTION" = ifdown ] && [ -f "$SOURCEFILE" ]; then
rm -f "$SOURCEFILE"
run_command reload sources
fi
[ "$ACTION" = ifup ] || exit 0
. /lib/functions.sh
. /etc/init.d/chronyd
config_load chrony
config_foreach handle_allow allow | while read command; do
run_command "$command"
done
# Add servers from DHCP only if the config has a dhcp_ntp_server section
[ -z "$(config_foreach echo dhcp_ntp_server)" ] && exit 0
. /usr/share/libubox/jshn.sh
json_load "$(ifstatus "$INTERFACE")"
json_select data
json_get_var dhcp_ntp_servers ntpserver
[ -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