From b8266fa7ecab87a4ae015139e8f8b8a7cd5c714e Mon Sep 17 00:00:00 2001 From: Nicholas Randon Date: Wed, 7 Jan 2015 09:33:15 +0000 Subject: [PATCH] NTP fail to sync if time sync is not performed Time sensitive services may not be able to startup and be synchronized, specifically when deployed on multiple nodes, when the hardware clock of the booting system is not explicitly synchronized before attempting to start the services. With this, NTP itself may not be able to synchronize and start operating if the clock is skewed too much from the clock source(s), or if one clock source is out of sync itself. This change move the ntp stand-up earlier in setup so that time of any system is controlled befor time sensitive services startup. For example the database on controller nodes. The change also set the clock to the time of the given ntp server forcefully and updates the Hardware clock. It also fixes the invalid ntp.conf file. Closes-Bug: 1408379 Relates-To: I56a03dc0a899a8c515f2a05d678d7e80e9b7b93c Change-Id: Ib9931b84925d9ceb32f18e9adc5be64402fbf61e --- elements/ntp/os-apply-config/etc/ntp.conf | 12 ++++++++-- .../ntp/os-refresh-config/configure.d/50-ntp | 22 +++++++++++++++++++ .../os-refresh-config/post-configure.d/50-ntp | 2 -- elements/ntp/svc-map | 3 +++ 4 files changed, 35 insertions(+), 4 deletions(-) create mode 100755 elements/ntp/os-refresh-config/configure.d/50-ntp delete mode 100755 elements/ntp/os-refresh-config/post-configure.d/50-ntp create mode 100644 elements/ntp/svc-map diff --git a/elements/ntp/os-apply-config/etc/ntp.conf b/elements/ntp/os-apply-config/etc/ntp.conf index be3a8c563..4d40a92b2 100644 --- a/elements/ntp/os-apply-config/etc/ntp.conf +++ b/elements/ntp/os-apply-config/etc/ntp.conf @@ -14,11 +14,19 @@ filegen clockstats file clockstats type day enable restrict -4 default kod notrap nomodify nopeer noquery restrict -6 default kod notrap nomodify nopeer noquery -# Local users get more +# Local users may interrogate the ntp server more closely. restrict 127.0.0.1 restrict ::1 +# Remote NTP server(s) to synchronize with. {{#ntp.servers}} +{{#server}} server {{server}} -{{#fudge}}fudge {{server}} {{fudge}}{{/fudge}} +{{/server}} {{/ntp.servers}} + +# Specify the internal hardware clock as a reference clock. +# Set a high stratum so this is only used if all external clocks fail. +# This will mitigate skew until external clocks return to service. +server 127.127.1.0 # local clock address +fudge 127.127.1.0 stratum 10 diff --git a/elements/ntp/os-refresh-config/configure.d/50-ntp b/elements/ntp/os-refresh-config/configure.d/50-ntp new file mode 100755 index 000000000..d68345a8e --- /dev/null +++ b/elements/ntp/os-refresh-config/configure.d/50-ntp @@ -0,0 +1,22 @@ +#!/bin/bash +set -eu +set -o pipefail + +# NTP server to sync with. +NTP_SERVER="$(os-apply-config --key 'ntp.servers.0.server' --type raw --key-default '')" +SERVICE_NAME="$(svc-map ntpd)" + +if [ -n "${NTP_SERVER}" ]; then + ntpdate -q "${NTP_SERVER}" || + { echo "ERROR: ntpdate cannot connect to: ${NTP_SERVER}"; exit 1; } + + # Check ntpd is running, if it is stop the service. + service "${SERVICE_NAME}" status && service "${SERVICE_NAME}" stop + + # Set the system clock to the value of the NTP clock. + # Note: This will not sanity check the ntp server like ntpd. + ntpdate -b "${NTP_SERVER}" + hwclock --systohc # Re-align the H/W clock. (incase of power loss) + + service "${SERVICE_NAME}" start +fi diff --git a/elements/ntp/os-refresh-config/post-configure.d/50-ntp b/elements/ntp/os-refresh-config/post-configure.d/50-ntp deleted file mode 100755 index 8cba2c520..000000000 --- a/elements/ntp/os-refresh-config/post-configure.d/50-ntp +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/sh -service ntp restart || service ntpd restart diff --git a/elements/ntp/svc-map b/elements/ntp/svc-map new file mode 100644 index 000000000..7289d5649 --- /dev/null +++ b/elements/ntp/svc-map @@ -0,0 +1,3 @@ +ntpd: + default: ntp + redhat: ntpd