From 012c832ce5382971cd85b9ddc936c36cefe3951f Mon Sep 17 00:00:00 2001
From: bsun-sudo <56011247+bsun-sudo@users.noreply.github.com>
Date: Tue, 24 Mar 2020 21:21:15 -0700
Subject: [PATCH] [ntp] add ntp support in buster with mgmt vrf (#55)

- create a file in files/image_config/ntp/ntp-systemd-wrapper to add mgmt vrf related start cmd for ntp service. So that the default /usr/lib/ntp/ntp-systemd-wrapper can be overriden during build time.

- modify build_debian.sh to cp files/image_config/ntp/ntp-systemd-wrapper to /usr/lib/ntp/ntp-systemd-wrapper during build time.

Co-authored-by: Bing Sun <Bing_Sun@dell.com>
---
 build_debian.sh                            |  4 +++
 files/image_config/ntp/ntp-systemd-wrapper | 38 ++++++++++++++++++++++
 2 files changed, 42 insertions(+)
 create mode 100644 files/image_config/ntp/ntp-systemd-wrapper

diff --git a/build_debian.sh b/build_debian.sh
index f52630334a79..fa45853e1597 100755
--- a/build_debian.sh
+++ b/build_debian.sh
@@ -451,6 +451,10 @@ if [ -f files/image_config/ntp/ntp ]; then
     sudo cp ./files/image_config/ntp/ntp $FILESYSTEM_ROOT/etc/init.d/
 fi
 
+if [ -f files/image_config/ntp/ntp-systemd-wrapper ]; then
+    sudo cp ./files/image_config/ntp/ntp-systemd-wrapper $FILESYSTEM_ROOT/usr/lib/ntp/
+fi
+
 ## Version file
 sudo mkdir -p $FILESYSTEM_ROOT/etc/sonic
 sudo tee $FILESYSTEM_ROOT/etc/sonic/sonic_version.yml > /dev/null <<EOF
diff --git a/files/image_config/ntp/ntp-systemd-wrapper b/files/image_config/ntp/ntp-systemd-wrapper
new file mode 100644
index 000000000000..759a69496b2c
--- /dev/null
+++ b/files/image_config/ntp/ntp-systemd-wrapper
@@ -0,0 +1,38 @@
+#!/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.
+# This file will be copied to /usr/lib/ntp/ntp-systemd-wrapper file that gets created during build process.
+
+DAEMON=/usr/sbin/ntpd
+PIDFILE=/var/run/ntpd.pid
+
+if [ -r /etc/default/ntp ]; then
+        . /etc/default/ntp
+fi
+
+if [ -e /run/ntp.conf.dhcp ]; then
+        NTPD_OPTS="$NTPD_OPTS -c /run/ntp.conf.dhcp"
+fi
+
+LOCKFILE=/run/lock/ntpdate
+
+RUNASUSER=ntp
+UGID=$(getent passwd $RUNASUSER | cut -f 3,4 -d:) || true
+if test "$(uname -s)" = "Linux"; then
+        NTPD_OPTS="$NTPD_OPTS -u $UGID"
+fi
+
+(
+        flock -w 180 9
+        vrfEnabled=$(/usr/local/bin/sonic-cfggen -d -v 'MGMT_VRF_CONFIG["vrf_global"]["mgmtVrfEnabled"]' 2> /dev/null)
+        if [ "$vrfEnabled" = "true" ]
+        then
+                ip vrf exec mgmt start-stop-daemon --start --quiet --oknodo --pidfile $PIDFILE --startas $DAEMON -- -p $PIDFILE $NTPD_OPTS
+        else
+                start-stop-daemon --start --quiet --oknodo --pidfile $PIDFILE --startas $DAEMON -- -p $PIDFILE $NTPD_OPTS
+        fi
+) 9>$LOCKFILE
+