improve chrony-helper to keep track of servers added from DHCP (#787042)
This commit is contained in:
parent
7457494fee
commit
2ca8fa6882
@ -5,20 +5,18 @@ SERVERFILE=$SAVEDIR/chrony.servers.$interface
|
|||||||
chrony_config() {
|
chrony_config() {
|
||||||
rm -f $SERVERFILE
|
rm -f $SERVERFILE
|
||||||
if [ "$PEERNTP" != "no" ]; then
|
if [ "$PEERNTP" != "no" ]; then
|
||||||
/usr/libexec/chrony-helper command "$(
|
for server in $new_ntp_servers; do
|
||||||
for server in $new_ntp_servers; do
|
echo "$server $NTPSERVERARGS" >> $SERVERFILE
|
||||||
echo "add server $server $NTPSERVERARGS"
|
done
|
||||||
echo "$server $NTPSERVERARGS" >> $SERVERFILE
|
/usr/libexec/chrony-helper is-running &&
|
||||||
done)" &> /dev/null || :
|
/usr/libexec/chrony-helper add-dhclient-servers || :
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
chrony_restore() {
|
chrony_restore() {
|
||||||
if [ -f $SERVERFILE ]; then
|
if [ -f $SERVERFILE ]; then
|
||||||
/usr/libexec/chrony-helper command "$(
|
|
||||||
while read server serverargs; do
|
|
||||||
echo "delete $server"
|
|
||||||
done < $SERVERFILE)" &> /dev/null
|
|
||||||
rm -f $SERVERFILE
|
rm -f $SERVERFILE
|
||||||
|
/usr/libexec/chrony-helper is-running &&
|
||||||
|
/usr/libexec/chrony-helper remove-dhclient-servers || :
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ config=/etc/chrony.conf
|
|||||||
keyfile=/etc/chrony.keys
|
keyfile=/etc/chrony.keys
|
||||||
chronyc=/usr/bin/chronyc
|
chronyc=/usr/bin/chronyc
|
||||||
dhclient_servers=/var/lib/dhclient/chrony.servers.*
|
dhclient_servers=/var/lib/dhclient/chrony.servers.*
|
||||||
|
dhclient_added_servers=/var/lib/dhclient/chrony.added_servers
|
||||||
service_name=chronyd.service
|
service_name=chronyd.service
|
||||||
|
|
||||||
get_key() {
|
get_key() {
|
||||||
@ -36,14 +37,43 @@ generate_commandkey() {
|
|||||||
[ -n "$commandkey" ] && echo "$commandkeyid $commandkey" >> $keyfile
|
[ -n "$commandkey" ] && echo "$commandkeyid $commandkey" >> $keyfile
|
||||||
}
|
}
|
||||||
|
|
||||||
|
update_dhclient_added_servers() {
|
||||||
|
new_servers=$(echo "$1" | sort -u)
|
||||||
|
old_servers=$(cat $dhclient_added_servers 2> /dev/null)
|
||||||
|
[ "$old_servers" = "$new_servers" ] && return 0
|
||||||
|
[ -n "$new_servers" ] && echo "$new_servers" > $dhclient_added_servers ||
|
||||||
|
rm -f $dhclient_added_servers
|
||||||
|
}
|
||||||
|
|
||||||
add_dhclient_servers() {
|
add_dhclient_servers() {
|
||||||
command=$(cat $dhclient_servers 2> /dev/null |
|
shopt -s nullglob
|
||||||
while read server serverargs; do
|
servers_files=($dhclient_servers)
|
||||||
echo "add server $server $serverargs"
|
shopt -u nullglob
|
||||||
done)
|
(( ${#servers_files[*]} )) || return 0
|
||||||
if [ -n "$command" ]; then
|
|
||||||
chrony_command "$command" &> /dev/null
|
added_servers=$(
|
||||||
fi
|
cat $dhclient_added_servers 2> /dev/null
|
||||||
|
cat ${servers_files[*]} |
|
||||||
|
while read server serverargs; do
|
||||||
|
chrony_command "add server $server $serverargs" &> /dev/null &&
|
||||||
|
echo "$server"
|
||||||
|
done)
|
||||||
|
update_dhclient_added_servers "$added_servers"
|
||||||
|
}
|
||||||
|
|
||||||
|
remove_dhclient_servers() {
|
||||||
|
[ -f $dhclient_added_servers ] || return 0
|
||||||
|
all_servers=$(
|
||||||
|
cat $dhclient_servers 2> /dev/null |
|
||||||
|
while read server serverargs; do
|
||||||
|
echo "$server"
|
||||||
|
done | sort -u)
|
||||||
|
echo "$all_servers" | comm -23 $dhclient_added_servers - |
|
||||||
|
while read server; do
|
||||||
|
chrony_command "delete $server" &> /dev/null
|
||||||
|
done
|
||||||
|
added_servers=$(echo "$all_servers" | comm -12 $dhclient_added_servers -)
|
||||||
|
update_dhclient_added_servers "$added_servers"
|
||||||
}
|
}
|
||||||
|
|
||||||
is_running() {
|
is_running() {
|
||||||
@ -57,6 +87,12 @@ case "$1" in
|
|||||||
add-dhclient-servers)
|
add-dhclient-servers)
|
||||||
add_dhclient_servers
|
add_dhclient_servers
|
||||||
;;
|
;;
|
||||||
|
remove-dhclient-servers)
|
||||||
|
remove_dhclient_servers
|
||||||
|
;;
|
||||||
|
is-running)
|
||||||
|
is_running
|
||||||
|
;;
|
||||||
command)
|
command)
|
||||||
is_running && chrony_command "$2"
|
is_running && chrony_command "$2"
|
||||||
;;
|
;;
|
||||||
@ -64,7 +100,7 @@ case "$1" in
|
|||||||
chrony_command "$2"
|
chrony_command "$2"
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
echo $"Usage: $0 {generate-commandkey|add-dhclient-servers|command|forced-command}"
|
echo $"Usage: $0 {generate-commandkey|add-dhclient-servers|remove-dhclient-servers|is-running|command|forced-command}"
|
||||||
exit 2
|
exit 2
|
||||||
esac
|
esac
|
||||||
exit $?
|
exit $?
|
||||||
|
Loading…
Reference in New Issue
Block a user