#!/usr/bin/env dash # # Setup timesyncd to use NTP servers received by DHCP. # Time server addresses are added or removed for each connection. # set -e INTERFACE=$1 ACTION=$2 _CONF_DIR=/etc/systemd/timesyncd.conf.d # Do we have a connection ID? if [ -z "$CONNECTION_UUID" ]; then exit 0 fi # Is the timesyncd service currently active? _timesyncd_state="$( systemctl show --property "ActiveState" systemd-timesyncd.service )" # Do we have the overlay configuration directory for timesyncd if [ ! -d "$_CONF_DIR" ]; then # Create it mkdir -p "$_CONF_DIR" fi case $ACTION in # Network just went up or configuration received by DHCP has changed up | vpn-up | dhcp4-change | dhcp6-change) # Did we receive any NTP server addresses by DHCP? if [ -z "$DHCP4_NTP_SERVERS" ] && [ -z "$DHCP6_NTP_SERVERS" ]; then # Neither IPv6 nor IPv4 NTP server address received. exit 0 fi if [ -n "$DHCP4_NTP_SERVERS" ]; then _ntp_servers="$_ntp_servers $DHCP4_NTP_SERVERS" fi if [ -n "$DHCP6_NTP_SERVERS" ]; then _ntp_servers="$_ntp_servers $DHCP6_NTP_SERVERS" fi # Create a connection-specific timesyncd configuration file exec > "${_CONF_DIR}/${CONNECTION_UUID}.conf" echo "# Generated by $(basename "$0")for $INTERFACE" echo "[Time]" echo "NTP=${_ntp_servers}" if [ "$_timesyncd_state" = "ActiveState=active" ]; then # Restart the timesyncd service systemctl restart systemd-timesyncd fi ;; # Connection went down down | vpn-down) # Remove the timesyncd configuration for this connection rm -f "${_CONF_DIR}/${CONNECTION_UUID}.conf" if [ "$_timesyncd_state" = "ActiveState=active" ]; then # Restart the timesyncd service systemctl restart systemd-timesyncd fi ;; esac