summaryrefslogtreecommitdiffstats
path: root/examples/chrony.nm-dispatcher.dhcp
blob: 6ea4c3705c93e04ac2465be2d6e128bc537a242d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/bin/sh
# This is a NetworkManager dispatcher script for chronyd to update
# its NTP sources passed from DHCP options. Note that this script is
# specific to NetworkManager-dispatcher due to use of the
# DHCP4_NTP_SERVERS environment variable.

export LC_ALL=C

interface=$1
action=$2

chronyc=/usr/bin/chronyc
default_server_options=iburst
server_dir=/var/run/chrony-dhcp

dhcp_server_file=$server_dir/$interface.sources
# DHCP4_NTP_SERVERS is passed from DHCP options by NetworkManager.
nm_dhcp_servers=$DHCP4_NTP_SERVERS

add_servers_from_dhcp() {
    rm -f "$dhcp_server_file"
    for server in $nm_dhcp_servers; do
        echo "server $server $default_server_options" >> "$dhcp_server_file"
    done
    $chronyc reload sources > /dev/null 2>&1 || :
}

clear_servers_from_dhcp() {
    if [ -f "$dhcp_server_file" ]; then
        rm -f "$dhcp_server_file"
        $chronyc reload sources > /dev/null 2>&1 || :
    fi
}

mkdir -p $server_dir

if [ "$action" = "up" ] || [ "$action" = "dhcp4-change" ]; then
    add_servers_from_dhcp
elif [ "$action" = "down" ]; then
    clear_servers_from_dhcp
fi

exit 0