2020-03-24 23:21:15 -05:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
# This file was originally created automatically as part of default NTP application installation from debian package.
|
|
|
|
# This is now manually modified for supporting NTP in management VRF.
|
|
|
|
# When management VRF is enabled, the NTP application should be started using "ip vrf exec mgmt".
|
|
|
|
# Check has been added to verify the management VRF enabled status and use "ip vrf exec mgmt" when it is enabled.
|
2023-10-23 10:44:13 -05:00
|
|
|
# This file will be copied to /usr/libexec/ntpsec/ntp-systemd-wrapper file that gets created during build process.
|
2020-03-24 23:21:15 -05:00
|
|
|
DAEMON=/usr/sbin/ntpd
|
2023-10-23 10:44:13 -05:00
|
|
|
PIDFILE=/run/ntpd.pid
|
|
|
|
LOCKFILE=/run/lock/ntpsec-ntpdate
|
2020-03-24 23:21:15 -05:00
|
|
|
|
2023-10-23 10:44:13 -05:00
|
|
|
if [ -r /etc/default/ntpsec ]; then
|
|
|
|
. /etc/default/ntpsec
|
2020-03-24 23:21:15 -05:00
|
|
|
fi
|
|
|
|
|
2023-12-11 15:31:35 -06:00
|
|
|
dhcp=$(/usr/local/bin/sonic-cfggen -d -v 'NTP["global"]["dhcp"]' 2> /dev/null)
|
|
|
|
if [ "$IGNORE_DHCP" != "yes" ] && [ -e /run/ntpsec/ntp.conf.dhcp ] && [ "$dhcp" = "enabled" ]; then
|
2023-10-23 10:44:13 -05:00
|
|
|
NTPD_OPTS="$NTPD_OPTS -c /run/ntpsec/ntp.conf.dhcp"
|
|
|
|
else
|
|
|
|
# List the default -c first, so if the admin has specified -c in
|
|
|
|
# NTPD_OPTS, it is honored.
|
|
|
|
NTPD_OPTS="-c /etc/ntpsec/ntp.conf $NTPD_OPTS"
|
2020-03-24 23:21:15 -05:00
|
|
|
fi
|
|
|
|
|
2023-10-23 10:44:13 -05:00
|
|
|
NTPD_OPTS="$NTPD_OPTS -u ntpsec:ntpsec"
|
2020-03-24 23:21:15 -05:00
|
|
|
|
2023-10-23 10:44:13 -05:00
|
|
|
# Protect the service startup against concurrent ntpdate ifup hooks
|
2020-03-24 23:21:15 -05:00
|
|
|
(
|
2023-10-23 10:44:13 -05:00
|
|
|
if flock -w 180 9; then
|
2023-12-11 15:31:35 -06:00
|
|
|
ntpEnabled=$(/usr/local/bin/sonic-cfggen -d -v 'NTP["global"]["admin_state"]' 2> /dev/null)
|
|
|
|
if [ "$ntpEnabled" = "disabled" ]
|
|
|
|
then
|
|
|
|
echo "Stopping NTP daemon"
|
|
|
|
kill -9 $(cat $PIDFILE)
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2023-10-23 10:44:13 -05:00
|
|
|
# when mgmt vrf is configured, ntp starts in mgmt vrf by default unless user configures otherwise
|
|
|
|
vrfEnabled=$(/usr/local/bin/sonic-cfggen -d -v 'MGMT_VRF_CONFIG["vrf_global"]["mgmtVrfEnabled"]' 2> /dev/null)
|
|
|
|
vrfConfigured=$(/usr/local/bin/sonic-cfggen -d -v 'NTP["global"]["vrf"]' 2> /dev/null)
|
|
|
|
if [ "$vrfEnabled" = "true" ]
|
2020-12-21 07:34:13 -06:00
|
|
|
then
|
2023-10-23 10:44:13 -05:00
|
|
|
if [ "$vrfConfigured" = "default" ]
|
|
|
|
then
|
|
|
|
echo "Starting NTP server in default-vrf for default set as NTP vrf"
|
|
|
|
exec $DAEMON -p $PIDFILE $NTPD_OPTS
|
|
|
|
else
|
|
|
|
echo "Starting NTP server in mgmt-vrf"
|
|
|
|
exec ip vrf exec mgmt $DAEMON -p $PIDFILE $NTPD_OPTS
|
|
|
|
fi
|
2020-12-21 07:34:13 -06:00
|
|
|
else
|
2023-10-23 10:44:13 -05:00
|
|
|
echo "Starting NTP server in default-vrf"
|
|
|
|
exec $DAEMON -p $PIDFILE $NTPD_OPTS
|
2020-12-21 07:34:13 -06:00
|
|
|
fi
|
2020-03-24 23:21:15 -05:00
|
|
|
else
|
2023-10-23 10:44:13 -05:00
|
|
|
echo "Timeout waiting for $LOCKFILE"
|
|
|
|
exit 1
|
2020-03-24 23:21:15 -05:00
|
|
|
fi
|
|
|
|
) 9>$LOCKFILE
|