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