With these changes:
* NTP is run at boot time, once, to set the system clock.
This allows the rest of the boot to continue as soon as time is set,
which should happen as soon as there is networking. This is
necessary because there's parts of the system that need to wait
for the system clock to be set.
* NTP is then also started as a daemon, which runs continuously,
until stopped by the user, to keep system clock synchronised.
The two systemd units need to be separate, otherwise we can't have
network.target depend on a unit that finishes quickly: the daemon
unit doesn't exit until there's an error.
---
busybox.morph | 17 +++++++++++++----
scripts/ntpd-set.sh | 31 -------------------------------
scripts/run-ntpd-with-config | 23 +++++++++++++++++++++++
systemd-units/ntpd-boot.service.in | 10 ++++++++++
systemd-units/ntpd.service.in | 10 ++++++----
5 files changed, 52 insertions(+), 39 deletions(-)
delete mode 100755 scripts/ntpd-set.sh
create mode 100755 scripts/run-ntpd-with-config
create mode 100644 systemd-units/ntpd-boot.service.in
diff --git a/busybox.morph b/busybox.morph
index 188edf4..f92f4ff 100644
--- a/busybox.morph
+++ b/busybox.morph
@@ -57,8 +57,10 @@ configure-commands:
build-commands:
- make
- |
- for f in ifup@.service ntpd.service crond.service; do
- sed -e 's|@rootprefix@||g' "systemd-units/$f.in"
>"systemd-units/$f";
+ for fin in systemd-units/*.in
+ do
+ f=$(echo "$fin" | sed 's/\.in$//')
+ sed -e 's|@rootprefix@||g' "$fin" >"$f";
done
install-commands:
@@ -91,5 +93,12 @@ install-commands:
- cp examples/udhcp/simple.script
"$DESTDIR$PREFIX"/share/udhcpc/default.script
# Set up NTP
- - cp scripts/ntpd-set.sh "$DESTDIR$PREFIX"/bin/ntpd-set.sh
- - chmod 6755 "$DESTDIR$PREFIX"/bin/ntpd-set.sh
+ - install scripts/run-ntpd-with-config "$DESTDIR$PREFIX"/sbin/.
+ - install -d "$DESTDIR/etc"
+ - |
+ cat << EOF > "$DESTDIR/etc/ntpd.conf"
+ server
0.pool.ntp.org
+ server
1.pool.ntp.org
+ server
2.pool.ntp.org
+ server
3.pool.ntp.org
+ EOF
diff --git a/scripts/ntpd-set.sh b/scripts/ntpd-set.sh
deleted file mode 100755
index d6fd8f2..0000000
--- a/scripts/ntpd-set.sh
+++ /dev/null
@@ -1,31 +0,0 @@
-#!/bin/sh
-max_attempts=4
-
-# This script takes a list of ntp servers and passes them to ntpd to set the
-# system time. If a /etc/ntpd.conf file exists, the servers there are used,
-# if not, some default values are passed
-set_time() {
- # -q flag makes ntpd exit after setting the time once
- ntpd -q -n -p "$1"
-}
-
-check_time() {
- for attempt in $(seq "$max_attempts"); do
- for arg ; do
- echo $arg
- if set_time "$arg" ; then
- return 0
- fi
- done
- sleep 2
- done
- return 1
-}
-
-if [ -f /etc/ntpd.conf ]; then
- server_list=`cat /etc/ntpd.conf | sed ':a;N;$!ba;s/\n/ /g;s/server//g'`
- check_time $server_list
-else
- # Use a default list if there's no config
- check_time
0.pool.ntp.org 1.pool.ntp.org 2.pool.ntp.org 3.pool.ntp.org
-fi
diff --git a/scripts/run-ntpd-with-config b/scripts/run-ntpd-with-config
new file mode 100755
index 0000000..24d9cc1
--- /dev/null
+++ b/scripts/run-ntpd-with-config
@@ -0,0 +1,23 @@
+#!/bin/sh
+#
+# Invoke the Busybox ntpd with servers listed in /etc/ntpd.conf, if it
+# exists. The servers should be listed one per line, with the first
+# word in the line being "server". Any lines that don't start with
+# "server" as the first word are ignored. The server name should be
+# the second word. Anything else on the line is ignored.
+#
+# If the config file does not exist, no default servers are used.
+
+set -eu
+
+parse_servers()
+{
+ awk '$1 == "server" { for (i=2; i <= NF; ++i) print "-p",
$i }' "$1"
+}
+
+if [ -e /etc/ntpd.conf ]
+then
+ exec ntpd $(parse_servers /etc/ntpd.conf) "$@"
+else
+ exec ntpd "$@"
+fi
diff --git a/systemd-units/ntpd-boot.service.in b/systemd-units/ntpd-boot.service.in
new file mode 100644
index 0000000..17318c6
--- /dev/null
+++ b/systemd-units/ntpd-boot.service.in
@@ -0,0 +1,10 @@
+[Unit]
+Description=Set clock at boot with NTP
+Before=network.target
+Before=ntpd.service
+
+[Service]
+Type=oneshot
+ExecStart=/usr/sbin/run-ntpd-with-config -n -q
+Restart=on-failure
+RestartSec=15
diff --git a/systemd-units/ntpd.service.in b/systemd-units/ntpd.service.in
index 96ce5ac..7226c87 100644
--- a/systemd-units/ntpd.service.in
+++ b/systemd-units/ntpd.service.in
@@ -1,8 +1,10 @@
[Unit]
-Description=Network Time Protocol client
+Description=Keep clock synchronised with NTP
Before=network.target
+After=ntpd-boot.service
[Service]
-Type=oneshot
-ExecStart=/usr/bin/ntpd-set.sh
-RemainAfterExit=true
+Type=simple
+ExecStart=/usr/sbin/run-ntpd-with-config -n
+Restart=on-failure
+RestartSec=15
--
1.7.10.4