1 # Sample dhcpcd hook script for NTP
2 # It will configure either one of NTP, OpenNTP or Chrony (in that order)
3 # and will default to NTP if no default config is found.
5 # Like our resolv.conf hook script, we store a database of ntp.conf files
6 # and merge into /etc/ntp.conf
8 # You can set the env var NTP_CONF to override the derived default on
9 # systems with >1 NTP client installed.
10 # Here is an example for OpenNTP
11 # dhcpcd -e NTP_CONF=/usr/pkg/etc/ntpd.conf
12 # or by adding this to /etc/dhcpcd.conf
13 # env NTP_CONF=/usr/pkg/etc/ntpd.conf
14 # or by adding this to /etc/dhcpcd.enter-hook
15 # NTP_CONF=/usr/pkg/etc/ntpd.conf
16 # To use Chrony instead, simply change ntpd.conf to chrony.conf in the
19 : ${ntp_confs:=ntp.conf ntpd.conf chrony.conf}
20 : ${ntp_conf_dirs=/etc /usr/pkg/etc /usr/local/etc}
21 ntp_conf_dir="$state_dir/ntp.conf"
23 # If NTP_CONF is not set, work out a good default
24 if [ -z "$NTP_CONF" ]; then
25 for d in ${ntp_conf_dirs}; do
26 for f in ${ntp_confs}; do
27 if [ -e "$d/$f" ]; then
33 [ -e "$NTP_CONF" ] || NTP_CONF=/etc/ntp.conf
36 # Derive service name from configuration
37 if [ -z "$ntp_service" ]; then
39 *chrony.conf) ntp_service=chronyd;;
44 # Debian has a seperate file for DHCP config to avoid stamping on
46 if [ "$ntp_service" = ntpd ] && type invoke-rc.d >/dev/null 2>&1; then
47 [ -e /var/lib/ntp ] || mkdir /var/lib/ntp
49 : ${NTP_DHCP_CONF:=/var/lib/ntp/ntp.conf.dhcp}
52 : ${ntp_restart_cmd:=service_condcommand $ntp_service restart}
60 cf="$state_dir/ntp.conf.$ifname"
62 # Build a list of interfaces
63 interfaces=$(list_interfaces "$ntp_conf_dir")
67 if [ -n "$interfaces" ]; then
69 for x in ${interfaces}; do
70 header="$header${header:+, }$x"
74 srvs=$(cd "$ntp_conf_dir";
75 key_get_value "server " $interfaces)
76 if [ -n "$srvs" ]; then
77 for x in $(uniqify $srvs); do
78 servers="${servers}server $x$NL"
83 # Merge our config into ntp.conf
84 [ -e "$cf" ] && rm -f "$cf"
85 [ -d "$ntp_conf_dir" ] || mkdir -p "$ntp_conf_dir"
87 if [ -n "$NTP_DHCP_CONF" ]; then
88 [ -e "$ntp_conf" ] && cp "$ntp_conf" "$cf"
89 ntp_conf="$NTP_DHCP_CONF"
90 elif [ -e "$ntp_conf" ]; then
91 remove_markers "$signature_base" "$signature_base_end" \
95 if [ -n "$servers" ]; then
96 echo "$signature_base${header:+ $from }$header" >> "$cf"
97 printf %s "$servers" >> "$cf"
98 echo "$signature_base_end${header:+ $from }$header" >> "$cf"
100 [ -e "$ntp_conf" ] && [ -e "$cf" ] || return
103 # If we changed anything, restart ntpd
104 if change_file "$ntp_conf" "$cf"; then
105 [ -n "$ntp_restart_cmd" ] && eval $ntp_restart_cmd
111 cf="$ntp_conf_dir/$ifname"
113 [ -e "$cf" ] && rm "$cf"
114 [ -d "$ntp_conf_dir" ] || mkdir -p "$ntp_conf_dir"
115 if [ -n "$new_ntp_servers" ]; then
116 for x in $new_ntp_servers; do
117 echo "server $x" >> "$cf"
125 if [ -e "$ntp_conf_dir/$ifname" ]; then
126 rm "$ntp_conf_dir/$ifname"
131 # For ease of use, map DHCP6 names onto our DHCP4 names
133 BOUND6|RENEW6|REBIND6|REBOOT6|INFORM6)
134 new_ntp_servers="$new_dhcp6_sntp_servers"