diff options
author | Josh Rahm <joshuarahm@gmail.com> | 2023-02-17 13:40:49 -0700 |
---|---|---|
committer | Josh Rahm <joshuarahm@gmail.com> | 2023-02-17 13:40:49 -0700 |
commit | cc05bb2fc537ca0667730d8435e537e0ff2048cc (patch) | |
tree | 11fa7891a063e2c1517708b3651e65bca5bfdb45 | |
download | ntp-module-main.tar.gz ntp-module-main.tar.bz2 ntp-module-main.zip |
-rwxr-xr-x | module-setup.sh | 17 | ||||
-rwxr-xr-x | poll-ntpd.sh | 59 | ||||
-rwxr-xr-x | start-ntpd.sh | 3 |
3 files changed, 79 insertions, 0 deletions
diff --git a/module-setup.sh b/module-setup.sh new file mode 100755 index 0000000..2309caf --- /dev/null +++ b/module-setup.sh @@ -0,0 +1,17 @@ +#!/bin/bash +# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*- +# ex: ts=8 sw=4 sts=4 et filetype=sh + +check() { + return 255 +} + +depends() { + echo 'network busybox bash' +} + +install() { + inst_hook pre-udev 99 "$moddir/start-ntpd.sh" + inst_script "$moddir/poll-ntpd.sh" "/bin/poll-ntpd.sh" + inst_simple "/etc/ntp.conf" +} diff --git a/poll-ntpd.sh b/poll-ntpd.sh new file mode 100755 index 0000000..4fc8ece --- /dev/null +++ b/poll-ntpd.sh @@ -0,0 +1,59 @@ +#!/bin/bash + +# This script waits for the network to come online and then will set the system time. +# +# This is useful because my system motherboard does not save the system time +# for some reason. + +timeout=600 +count=0 + +# Make sure we can resolve DNS addresses +echo "nameserver 8.8.8.8" > /etc/resolv.conf + +# Wait for the given interface to come up and for a route to +# be added. +interface="$1" +echo -n "Waiting for interface $interface ... " +while ! ( ip route list dev "$interface" &>/dev/null ) ; do + if [[ "$count" -eq "$timeout" ]] ; then + echo "Timedout waiting for $interface. Exiting." + exit 1 + fi + + echo -n "." + sleep 0.1 + count=$((count + 1)) +done +echo -e "\ndone" + +# Even though a route has been added, it still takes time for DNS +# to be come resolvable. Wait till we can ping one of the NTP +# servers. +echo -n "Waiting for 1.gentoo.pool.ntp.org to become reachable ... " +timeout=10 +count=0 + +while ! ( ping -c 1 1.gentoo.pool.ntp.org ) ; do + if [[ "$count" -eq "$timeout" ]] ; then + echo "Timedout waiting for 1.gentoo.pool.ntp.org. Exiting." + exit 1 + fi + + echo -n "." + sleep 1 + count=$((count + 1)) +done +echo -e "\ndone" + +# Finally, set the time. +echo -n "Setting time ... " +ntpd -I "$interface" -n -q + +if [[ "$?" -ne 0 ]] ; then + exit 1 +fi +echo "done" + +# Touch a file to notify other scripts that the time has been set. +touch /var/ntpd-time-set.target diff --git a/start-ntpd.sh b/start-ntpd.sh new file mode 100755 index 0000000..9077d30 --- /dev/null +++ b/start-ntpd.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +nohup /bin/poll-ntpd.sh "eno1" &>/var/log/ntpd.log & |