Files
webgui/etc/rc.d/rc.ntpd
T
bergware 87475a8f47 rc updates
NTP: exclude WG tunnels and user defined interfaces
NTP: add interface name in config
SSH: add interface name in config
2023-06-19 19:54:09 +02:00

126 lines
2.7 KiB
Bash
Executable File

#!/bin/sh
# Start/stop/restart ntpd.
# limetech - modified to initialize ntp.conf file from config
# bergware - added interface bind functionality
CALLER="ntp"
NTPD="/usr/sbin/ntpd"
OPTIONS="-g -u ntp:ntp"
CONF="/etc/ntp.conf"
IDENT="/boot/config/ident.cfg"
# library functions
. /etc/rc.d/rc.library.source
build_ntp() {
cp $CONF- $CONF
echo "# Generated entries follow:" >>$CONF
echo "interface ignore wildcard" >>$CONF
if check && [[ -n $bind ]]; then
# ignore unused protocol
[[ $ipv4 == no ]] && echo "interface ignore ipv4" >>$CONF
[[ $ipv6 == no ]] && echo "interface ignore ipv6" >>$CONF
# add listen interfaces
for net in $bind; do
echo "interface listen $net # $(show $net)" >>$CONF
done
fi
# add configured NTP servers
[[ -n $NTP_SERVER1 ]] && echo "server $NTP_SERVER1 iburst" >>$CONF
[[ -n $NTP_SERVER2 ]] && echo "server $NTP_SERVER2 iburst" >>$CONF
[[ -n $NTP_SERVER3 ]] && echo "server $NTP_SERVER3 iburst" >>$CONF
[[ -n $NTP_SERVER4 ]] && echo "server $NTP_SERVER4 iburst" >>$CONF
}
ntpd_start() {
if [[ $(pgrep -cf $NTPD) -ne 0 ]]; then
# already running
echo "ntpd already running, not starting again"
return
fi
# read Unraid settings
[[ -r $IDENT ]] && . <(/usr/bin/fromdos <$IDENT)
# if ntp not enabled, don't start ntp
if [[ $USE_NTP != yes ]]; then
echo "NTP not enabled"
return
fi
# generate our config file
build_ntp
# restore previously saved drift file if present
install --owner=ntp --group=ntp --mode=644 /boot/config/drift /var/lib/ntp 2>/dev/null
echo -n "Starting NTP daemon: $NTPD $OPTIONS"
$NTPD $OPTIONS 2>/dev/null
echo
}
ntpd_stop() {
echo -n "Stopping NTP daemon..."
if [[ -r /var/run/ntpd.pid ]]; then
kill -HUP $(cat /var/run/ntpd.pid)
rm -f /var/run/ntpd.pid
else
killall -HUP -q ntpd
fi
# save the drift file if present
cp /var/lib/ntp/drift /boot/config 2>/dev/null
echo
}
ntpd_restart() {
ntpd_stop
sleep 1
ntpd_start
}
ntpd_status() {
if [[ -e /var/run/ntpd.pid ]]; then
echo "ntpd is running as pid $(cat /var/run/ntpd.pid)."
else
echo "ntpd is stopped."
exit 1
fi
}
ntpd_reload() {
killall -HUP -q ntpd
. <(/usr/bin/fromdos <$IDENT)
build_ntp
$NTPD $OPTIONS 2>/dev/null
}
ntpd_update() {
[[ $(pgrep -cf $NTPD) -eq 0 ]] && exit 1 # not running
if check && [[ "$(this 'interface listen')" == "$bind" ]]; then
# no action required
exit 1
else
# service update required
exit 0
fi
}
case "$1" in
'start')
ntpd_start
;;
'stop')
ntpd_stop
;;
'restart')
ntpd_restart
;;
'status')
ntpd_status
;;
'reload')
ntpd_reload
;;
'update')
ntpd_update
;;
*)
echo "usage $0 start|stop|restart|status|reload|update"
esac