]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
hooks: Support timesyncd
authorRoy Marples <roy@marples.name>
Tue, 9 Aug 2022 16:26:37 +0000 (17:26 +0100)
committerRoy Marples <roy@marples.name>
Tue, 9 Aug 2022 16:26:37 +0000 (17:26 +0100)
Fixes #106

configure
hooks/50-timesyncd.conf [new file with mode: 0644]

index 9b8e44035833046af8d9fe1fc06abee86086f361..8ff66a1a35a4a16926b546117d5cd39184f5f133 100755 (executable)
--- a/configure
+++ b/configure
@@ -1707,6 +1707,21 @@ if ! $HOOKSET; then
                echo "NTP will default to $NTPD"
        fi
 
+       printf "Checking for timesyncd ... "
+       TIMESYNCD=
+       for x in /usr/lib/systemd/systemd-timesyncd; do
+               if [ -x "$x" ]; then
+                       TIMESYNCD=$x
+                       break
+               fi
+       done
+       if [ -n "$TIMESYNCD" ]; then
+               echo "$TIMESYNCD"
+               HOOKS="$HOOKS${HOOKS:+ }50-timesyncd.conf"
+       else
+               echo "not found"
+       fi
+
        printf "Checking for ypbind ... "
        YPBIND=$(_which ypbind)
        if [ -n "$YPBIND" ]; then
diff --git a/hooks/50-timesyncd.conf b/hooks/50-timesyncd.conf
new file mode 100644 (file)
index 0000000..064e0c0
--- /dev/null
@@ -0,0 +1,39 @@
+: ${timesyncd_conf_d:=/run/systemd/timesyncd.conf.d}
+timesyncd_conf="${timesyncd_conf_d}/dhcpcd-$ifname.conf"
+timesyncd_tmp_d="$state_dir/timesyncd"
+timesyncd_tmp="$timesyncd_tmp_d/$ifname"
+
+NL="
+"
+
+add_timesyncd_conf()
+{
+       [ -n "$new_ntp_servers" ] || return
+
+       mkdir -p "$timesyncd_tmp_d" "$timesyncd_conf_d"
+
+       conf="$signature$NL"
+       conf="${conf}[Time]$NL"
+       conf="${conf}NTP=$new_ntp_servers$NL"
+
+       printf %s "$conf" > "$timesyncd_tmp"
+       if change_file "$timesyncd_conf" "$timesyncd_tmp"; then
+               systemctl try-reload-or-restart systemd-timesyncd
+       fi
+}
+
+remove_timesyncd_conf()
+{
+       if [ -e "$timesyncd_conf" ]; then
+               rm "$timesyncd_conf"
+               systemctl try-reload-or-restart systemd-timesyncd
+       fi
+}
+
+if $if_configured; then
+       if $if_up; then
+               add_timesyncd_conf
+       elif $if_down; then
+               remove_timesyncd_conf
+       fi
+fi