|
|
473877 |
#!/bin/bash
|
|
|
473877 |
#
|
|
|
473877 |
# ntp.sh: dhclient-script plugin for NTP settings,
|
|
|
473877 |
# place in /etc/dhcp/dhclient.d and 'chmod +x ntp.sh' to enable
|
|
|
473877 |
#
|
|
|
473877 |
# Copyright (C) 2008 Red Hat, Inc.
|
|
|
473877 |
#
|
|
|
473877 |
# This program is free software; you can redistribute it and/or modify
|
|
|
473877 |
# it under the terms of the GNU General Public License as published by
|
|
|
473877 |
# the Free Software Foundation; either version 2 of the License, or
|
|
|
473877 |
# (at your option) any later version.
|
|
|
473877 |
#
|
|
|
473877 |
# This program is distributed in the hope that it will be useful,
|
|
|
473877 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
473877 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
473877 |
# GNU General Public License for more details.
|
|
|
473877 |
#
|
|
|
473877 |
# You should have received a copy of the GNU General Public License
|
|
|
473877 |
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
473877 |
#
|
|
|
473877 |
# Author(s): David Cantrell <dcantrell@redhat.com>
|
|
|
473877 |
# Miroslav Lichvar <mlichvar@redhat.com>
|
|
|
473877 |
#
|
|
|
473877 |
|
|
|
473877 |
CONF=/etc/ntp.conf
|
|
|
473877 |
SAVECONF=${SAVEDIR}/${CONF##*/}.predhclient.${interface}
|
|
|
473877 |
|
|
|
473877 |
ntp_replace_conf() {
|
|
|
473877 |
echo "$1" | diff -q ${CONF} - > /dev/null 2>&1
|
|
|
473877 |
if [ $? -eq 1 ]; then
|
|
|
473877 |
echo "$1" > ${CONF}
|
|
|
473877 |
restorecon ${CONF} >/dev/null 2>&1
|
|
|
473877 |
systemctl try-restart ntpd.service > /dev/null 2>&1 ||
|
|
|
473877 |
service ntpd condrestart > /dev/null 2>&1
|
|
|
473877 |
fi
|
|
|
473877 |
}
|
|
|
473877 |
|
|
|
473877 |
ntp_config() {
|
|
|
473877 |
if [ ! "${PEERNTP}" = "no" ] && [ -n "${new_ntp_servers}" ] &&
|
|
|
473877 |
[ -e ${CONF} ] && [ -d ${SAVEDIR} ]; then
|
|
|
473877 |
local conf=$(grep -v '^server .* # added by /sbin/dhclient-script$' < ${CONF})
|
|
|
473877 |
local unique_servers=$(comm -23 \
|
|
|
473877 |
<(for s in ${new_ntp_servers}; do echo $s; done | sort -u) \
|
|
|
473877 |
<(echo "$conf" | awk '$1=="peer"||$1=="server"{print $2}' | sort -u))
|
|
|
473877 |
|
|
|
473877 |
conf=$(echo "$conf"
|
|
|
473877 |
for s in ${unique_servers}; do
|
|
|
473877 |
echo "server ${s} ${NTPSERVERARGS} # added by /sbin/dhclient-script"
|
|
|
473877 |
done)
|
|
|
473877 |
|
|
|
473877 |
[ -f ${SAVECONF} ] || touch ${SAVECONF}
|
|
|
473877 |
ntp_replace_conf "$conf"
|
|
|
473877 |
fi
|
|
|
473877 |
}
|
|
|
473877 |
|
|
|
473877 |
ntp_restore() {
|
|
|
473877 |
if [ -e ${CONF} ] && [ -f ${SAVECONF} ]; then
|
|
|
473877 |
local conf=$(grep -v '^server .* # added by /sbin/dhclient-script$' < ${CONF})
|
|
|
473877 |
|
|
|
473877 |
ntp_replace_conf "$conf"
|
|
|
473877 |
rm -f ${SAVECONF}
|
|
|
473877 |
fi
|
|
|
473877 |
}
|