#!/bin/sh # # Manage NTP configuration. # . /opt/puzzlefw/lib/functions.sh # Show current configuration. ntpcfg_show() { echo "Active NTP configuration:" if [ -s /etc/chrony/sources.d/ntp.sources ]; then cat /etc/chrony/sources.d/ntp.sources else echo "disabled" fi echo echo "Saved NTP configuration:" if [ -s ${CONFIG_DIR}/ntp.sources ]; then cat ${CONFIG_DIR}/ntp.sources else echo "disabled" fi echo } # Check that parameter is a well-formed IPv4 address. check_ipaddr() { IFS="." read a b c d <&2 exit 1 fi done } # Configure and enable Chrony. ntpcfg_server() { if [ "$1" != "server" ]; then echo "ERROR: Invalid command '$1'" >&2 exit 1 fi NTPSERVER="$2" if [ -z "$NTPSERVER" ]; then echo "ERROR: Server IP address not specified" >&2 exit 1 fi check_ipaddr "$NTPSERVER" POLLOPTS="" if [ "$#" -gt 2 ]; then if [ "$3" != "poll" ]; then echo "ERROR: Unknown option '$3'" >&2 exit 1 fi POLLINT="$4" if [ -z "$POLLINT" ]; then echo "ERROR: Poll interval not specified" >&2 exit 1 fi if ! [ "$POLLINT" -ge "-1" -a "$POLLINT" -le "10" ]; then echo "ERROR: Invalid poll interval '$POLLINT', must be between -1 and 10" >&2 exit 1 fi POLLOPTS="minpoll $POLLINT maxpoll $POLLINT" if [ "$#" -gt 4 ]; then echo "ERROR: Unexpected option '$5'" >&2 exit 1 fi fi # Lock to avoid conflicting changes. lock_config || exit 1 echo "Configuring Chrony to start on boot with server $NTPSERVER ..." SERVERLINE="server $NTPSERVER $POLLOPTS iburst prefer" echo "$SERVERLINE" > ${CONFIG_DIR}/ntp.sources.new sync_config ntp.sources || exit 1 mkdir -p /etc/chrony/sources.d cp -p ${CONFIG_DIR}/ntp.sources /etc/chrony/sources.d echo echo "New NTP configuration:" cat /etc/chrony/sources.d/ntp.sources echo echo "Restarting Chrony ..." /etc/init.d/S49chrony restart } # Disable starting Chrony during boot. ntpcfg_disable() { # Lock to avoid conflicting changes. lock_config || exit 1 echo "Disabling Chrony startup on boot ..." echo -n "" > ${CONFIG_DIR}/ntp.sources.new sync_config ntp.sources || exit 1 echo "Stopping Chrony ..." /etc/init.d/S49chrony stop rm -f /etc/chrony/sources.d/ntp.sources } case "$1" in show) ntpcfg_show ;; server) ntpcfg_server "$@" ;; disable) ntpcfg_disable ;; *) script="${0##*/}" cat <