Mercurial > hg > dhcpcd
annotate hooks/50-ntp.conf @ 5535:a0d828e25482 draft
Add --noconfigure option
With this set dhcpcd will not configure anything on the host.
The expectation is that a 3rd party script will instead.
| author | Roy Marples <roy@marples.name> |
|---|---|
| date | Wed, 04 Nov 2020 14:18:48 +0000 |
| parents | 4539ffcdd656 |
| children |
| rev | line source |
|---|---|
| 3936 | 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. | |
| 4 | |
| 5 # Like our resolv.conf hook script, we store a database of ntp.conf files | |
| 6 # and merge into /etc/ntp.conf | |
| 7 | |
| 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 | |
| 17 # above examples. | |
| 18 | |
| 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" | |
| 22 | |
| 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 | |
| 28 NTP_CONF="$d/$f" | |
| 29 break 2 | |
| 30 fi | |
| 31 done | |
| 32 done | |
| 33 [ -e "$NTP_CONF" ] || NTP_CONF=/etc/ntp.conf | |
| 34 fi | |
| 35 | |
| 36 # Derive service name from configuration | |
| 37 if [ -z "$ntp_service" ]; then | |
| 38 case "$NTP_CONF" in | |
| 39 *chrony.conf) ntp_service=chronyd;; | |
| 40 *) ntp_service=ntpd;; | |
| 41 esac | |
| 42 fi | |
| 43 | |
|
5060
4539ffcdd656
spelling: Correct both privilege and separation
Roy Marples <roy@marples.name>
parents:
4552
diff
changeset
|
44 # Debian has a separate file for DHCP config to avoid stamping on |
| 3936 | 45 # the master. |
| 46 if [ "$ntp_service" = ntpd ] && type invoke-rc.d >/dev/null 2>&1; then | |
| 47 [ -e /var/lib/ntp ] || mkdir /var/lib/ntp | |
| 48 : ${ntp_service:=ntp} | |
| 49 : ${NTP_DHCP_CONF:=/var/lib/ntp/ntp.conf.dhcp} | |
| 50 fi | |
| 51 | |
| 52 : ${ntp_restart_cmd:=service_condcommand $ntp_service restart} | |
| 53 | |
| 54 ntp_conf=${NTP_CONF} | |
| 55 NL=" | |
| 56 " | |
| 57 | |
| 58 build_ntp_conf() | |
| 59 { | |
|
4215
e323d30f279e
dhcpcd-run-hooks: POSIX shell does not require the local builtin
Roy Marples <roy@marples.name>
parents:
3936
diff
changeset
|
60 cf="$state_dir/ntp.conf.$ifname" |
| 3936 | 61 |
| 62 # Build a list of interfaces | |
| 63 interfaces=$(list_interfaces "$ntp_conf_dir") | |
| 64 | |
|
4552
d62e5b96b66e
hooks: Ensure header is empty before populating it.
Roy Marples <roy@marples.name>
parents:
4322
diff
changeset
|
65 header= |
|
4215
e323d30f279e
dhcpcd-run-hooks: POSIX shell does not require the local builtin
Roy Marples <roy@marples.name>
parents:
3936
diff
changeset
|
66 servers= |
| 3936 | 67 if [ -n "$interfaces" ]; then |
| 68 # Build the header | |
| 69 for x in ${interfaces}; do | |
| 70 header="$header${header:+, }$x" | |
| 71 done | |
| 72 | |
| 73 # Build a server list | |
| 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" | |
| 79 done | |
| 80 fi | |
| 81 fi | |
| 82 | |
| 83 # Merge our config into ntp.conf | |
| 84 [ -e "$cf" ] && rm -f "$cf" | |
| 85 [ -d "$ntp_conf_dir" ] || mkdir -p "$ntp_conf_dir" | |
| 86 | |
| 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" \ | |
| 92 "$ntp_conf" > "$cf" | |
| 93 fi | |
| 94 | |
| 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" | |
| 99 else | |
|
4322
0b2abe4e9d2e
hooks: shell [ ] only supports 4 parameters
Roy Marples <roy@marples.name>
parents:
4215
diff
changeset
|
100 [ -e "$ntp_conf" ] && [ -e "$cf" ] || return |
| 3936 | 101 fi |
| 102 | |
| 103 # If we changed anything, restart ntpd | |
| 104 if change_file "$ntp_conf" "$cf"; then | |
| 105 [ -n "$ntp_restart_cmd" ] && eval $ntp_restart_cmd | |
| 106 fi | |
| 107 } | |
| 108 | |
| 109 add_ntp_conf() | |
| 110 { | |
|
4215
e323d30f279e
dhcpcd-run-hooks: POSIX shell does not require the local builtin
Roy Marples <roy@marples.name>
parents:
3936
diff
changeset
|
111 cf="$ntp_conf_dir/$ifname" |
| 3936 | 112 |
| 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" | |
| 118 done | |
| 119 fi | |
| 120 build_ntp_conf | |
| 121 } | |
| 122 | |
| 123 remove_ntp_conf() | |
| 124 { | |
| 125 if [ -e "$ntp_conf_dir/$ifname" ]; then | |
| 126 rm "$ntp_conf_dir/$ifname" | |
| 127 fi | |
| 128 build_ntp_conf | |
| 129 } | |
| 130 | |
| 131 # For ease of use, map DHCP6 names onto our DHCP4 names | |
| 132 case "$reason" in | |
| 133 BOUND6|RENEW6|REBIND6|REBOOT6|INFORM6) | |
| 134 new_ntp_servers="$new_dhcp6_sntp_servers" | |
| 135 ;; | |
| 136 esac | |
| 137 | |
| 5535 | 138 if $if_configured; then |
| 139 if $if_up; then | |
| 140 add_ntp_conf | |
| 141 elif $if_down; then | |
| 142 remove_ntp_conf | |
| 143 fi | |
| 3936 | 144 fi |
