From efb78c301b10f9a7b957dadf16d9cc4f2e64f64a Mon Sep 17 00:00:00 2001 From: GONG Jie Date: Thu, 12 Jul 2018 12:32:16 +0800 Subject: [PATCH 1/4] Bash script setupchrony for chrony configuring. --- xCAT/postscripts/setupchrony | 293 +++++++++++++++++++++++++++++++++++ 1 file changed, 293 insertions(+) create mode 100755 xCAT/postscripts/setupchrony diff --git a/xCAT/postscripts/setupchrony b/xCAT/postscripts/setupchrony new file mode 100755 index 000000000..934398262 --- /dev/null +++ b/xCAT/postscripts/setupchrony @@ -0,0 +1,293 @@ +#!/bin/bash +# +# setupchrony - Set up chrony +# +# Copyright (C) 2018 International Business Machines +# Eclipse Public License, Version 1.0 (EPL-1.0) +# +# +# 2018-07-11 GONG Jie +# - Draft +# 2018-07-13 GONG Jie +# - Release to field +# + +PATH="/usr/sbin:/usr/bin:/sbin:/bin" +export PATH + +# +# warn_if_bad Put out warning message(s) if $1 has bad RC. +# +# $1 0 (pass) or non-zero (fail). +# $2+ Remaining arguments printed only if the $1 is non-zero. +# +# Incoming $1 is returned unless it is 0 +# +function warn_if_bad() +{ + local -i rc="$1" + local script="${0##*/}" + + # Ignore if no problems + [ "${rc}" -eq "0" ] && return 0 + + # Broken + shift + logger -t xcat -p local4.info "${script}: $@" >/dev/null 2>&1 + echo "${script}: $@" >&2 + return "${rc}" +} + +# +# exit_if_bad Put out error message(s) if $1 has bad RC. +# +# $1 0 (pass) or non-zero (fail). +# $2+ Remaining arguments printed only if the $1 is non-zero. +# +# Exits with 1 unless $1 is 0 +# +function exit_if_bad() +{ + warn_if_bad "$@" || exit 1 + return 0 +} + +# +# check_executes Check for executable(s) +# +# Returns 0 if true. +# Returns 1 if not. +# +function check_executes() +{ + local cmd + local all_ok="yes" + for cmd in "$@" + do + if ! type "${cmd}" &>/dev/null + then + echo "Command \"${cmd}\" not found." >&2 + all_ok="no" + fi + done + [ "${all_ok}" = "yes" ] +} + +# +# check_exec_or_exit Check for required executables. +# +# Exits (not returns) if commands listed on command line do not exist. +# +# Returns 0 if true. +# Exits with 1 if not. +# +function check_exec_or_exit() +{ + check_executes "$@" + exit_if_bad "$?" "Above listed required command(s) not found." + return 0 +} + +[ "${UID}" -eq "0" ] +exit_if_bad "$?" "Must be run by UID=0. Actual UID=${UID}." + +declare -a NTP_SERVERS=() + +# Handle command line arguments +while [ "$#" -gt "0" ] +do + case "$1" in + *) + NTP_SERVERS+=($1) + ;; + esac + shift +done + +if [ "${#NTP_SERVERS[@]}" -eq "0" ] +then + # Handle xCAT passed environment variables + case "${NTPSERVERS}" in + ""|"<xcatmaster>"|"") + NTP_SERVERS=(${MASTER}) + ;; + *) + OLD_IFS="${IFS}" + IFS="," + NTP_SERVERS=(${NTPSERVERS}) + IFS="${OLD_IFS}" + unset OLD_IFS + ;; + esac +fi + +# Unset xCAT passed environment variables +unset MASTER +unset NTPSERVERS + +check_exec_or_exit cp cat logger +check_exec_or_exit systemctl timedatectl hwclock chronyd + +systemctl stop ntp.service 2>/dev/null +systemctl disable ntp.service 2>/dev/null +systemctl stop ntpd.service 2>/dev/null +systemctl disable ntpd.service 2>/dev/null + +systemctl disable ntp-wait.service 2>/dev/null +systemctl disable ntpdate.service 2>/dev/null + +systemctl stop chronyd.service + +# The system is configured to maintain the RTC in universal time. +timedatectl set-local-rtc 0 +warn_if_bad "$?" "Failed to configure the system to maintain the RTC in universal time" + +# Synchronize and set the system clock once +logger -t xcat -p local4.info "Syncing the clock ..." +chronyd -f /dev/null -q "$( + if [ "${#NTP_SERVERS[@]}" -gt "0" ] + then + echo "server ${NTP_SERVERS[0]} iburst" + else + echo "pool pool.ntp.org iburst" + fi +)" "pidfile /dev/null" + +rm -f /etc/adjtime +# Set the hardware clock from the system clock +hwclock --systohc --utc +warn_if_bad "$?" "Failed to set the hardware clock" + +## On RHEL 8 +#CHRONY_USER="chrony" +#CHRONY_GROUP="chrony" +## On Ubuntu 18.04 +#CHRONY_USER="_chrony" +#CHRONY_GROUP="_chrony" + +CHRONY_CONF="/etc/chrony.conf" +KEY_FILE="/etc/chrony.keys" + +if [ -d "/etc/chrony" ] +then + # Ubuntu 18.04 default + CHRONY_CONF="/etc/chrony/chrony.conf" + KEY_FILE="/etc/chrony/chrony.keys" +fi + +# Take the best educated guess for the pathname of the chrony drift file +DRIFT_FILE="$( + for file in /var/lib/chrony/drift /var/lib/chrony/chrony.drift + do + if [ -f "${file}" ] + then + echo "${file}" + exit + fi + done + + if [ "chrony" = \ + "$(rpm -qf /var/lib/chrony/drift --qf '%{name}' 2>/dev/null)" ] + then + echo /var/lib/chrony/drift + exit + fi + + for file in /usr/share/chrony/chrony.conf \ + /etc/chrony.conf /etc/chrony/chrony.conf \ + $(grep -a -m 1 -o -P '/([^\x00]+/)*[^\x00]+\.conf' \ + "$(type -p chronyd)" 2>/dev/null) + do + if [ -f "${file}" ] + then + while read -r key val blah + do + if [ "${key}" = "driftfile" -a -n "${val}" ] + then + echo "${val}" + exit + fi + done <"${file}" + fi + done + + # The default value + echo /var/lib/chrony/drift +)" + +LOG_DIR="/var/log/chrony" + +logger -t xcat -p local4.info "Install: Setup NTP - chrony" +cp "${CHRONY_CONF}" "${CHRONY_CONF}.xcatsave" +cat >"${CHRONY_CONF}" < Date: Thu, 19 Jul 2018 11:23:36 +0800 Subject: [PATCH 2/4] Move setupntp -> setupntp.traditional --- xCAT/postscripts/{setupntp => setupntp.traditional} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename xCAT/postscripts/{setupntp => setupntp.traditional} (100%) diff --git a/xCAT/postscripts/setupntp b/xCAT/postscripts/setupntp.traditional similarity index 100% rename from xCAT/postscripts/setupntp rename to xCAT/postscripts/setupntp.traditional From 07a13a7697df4dbb12aef5f76572d6909c1eed82 Mon Sep 17 00:00:00 2001 From: GONG Jie Date: Thu, 19 Jul 2018 11:24:55 +0800 Subject: [PATCH 3/4] Move setupchrony -> setupntp --- xCAT/postscripts/{setupchrony => setupntp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename xCAT/postscripts/{setupchrony => setupntp} (100%) diff --git a/xCAT/postscripts/setupchrony b/xCAT/postscripts/setupntp similarity index 100% rename from xCAT/postscripts/setupchrony rename to xCAT/postscripts/setupntp From 803521a4ad85d33bdcc7ea6b80ceb3bac3a9855b Mon Sep 17 00:00:00 2001 From: GONG Jie Date: Thu, 19 Jul 2018 13:16:08 +0800 Subject: [PATCH 4/4] Call setupntp.traditional when there is no chrony installed --- xCAT/postscripts/setupntp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/xCAT/postscripts/setupntp b/xCAT/postscripts/setupntp index 934398262..e839ad0bb 100755 --- a/xCAT/postscripts/setupntp +++ b/xCAT/postscripts/setupntp @@ -97,6 +97,10 @@ declare -a NTP_SERVERS=() while [ "$#" -gt "0" ] do case "$1" in + "--use-ntpd") + # Use traditional ntpd + USE_NTPD="yes" + ;; *) NTP_SERVERS+=($1) ;; @@ -125,8 +129,19 @@ fi unset MASTER unset NTPSERVERS +check_executes chronyd || USE_NTPD="yes" + +if [ -n "${USE_NTPD}" ] +then + # Call setupntp.traditional, and pass the parsed ntp servers + logger -t xcat -p local4.info "Will call setupntp.traditional" + export NTPSERVERS="$(IFS=','; echo "${NTP_SERVERS[*]:-pool.ntp.org}")" + exec "${0%/*}/setupntp.traditional" + exit 255 +fi + check_exec_or_exit cp cat logger -check_exec_or_exit systemctl timedatectl hwclock chronyd +check_exec_or_exit systemctl timedatectl hwclock systemctl stop ntp.service 2>/dev/null systemctl disable ntp.service 2>/dev/null