VOL-758 OpenOLT - Create Linux services for BAL and openolt

service bal_core_dist status|stop|start
service openolt status|stop|start

bal_core_dist and openolt started as init.d services at system startup.

Change-Id: If8a97e74b999b68f202a8a3c05331766b84a4641
diff --git a/Makefile b/Makefile
index ea3f452..a32dfbd 100644
--- a/Makefile
+++ b/Makefile
@@ -233,6 +233,7 @@
 	cp $(BUILD_DIR)/openolt mkdebian/debian
 	cp $(BUILD_DIR)/libgrpc.so.6 mkdebian/debian
 	cp $(BUILD_DIR)/libgrpc++.so.1 mkdebian/debian
+	cp -a scripts/init.d mkdebian/debian
 	cd mkdebian && ./build_$(DEVICE)_deb.sh
 	mv *.deb $(BUILD_DIR)/openolt.deb
 	make deb-cleanup
diff --git a/README.md b/README.md
index fc50f4b..498c5b7 100644
--- a/README.md
+++ b/README.md
@@ -72,13 +72,31 @@
 dpkg -i openolt.deb
 ```
 
-Reboot the OLT:
+## Run OpenOLT as a Linux service
 
+Rebooting the OLT (after the installation) will start bal_core_dist and openolt as init.d services:
+
+Rebooting the OLT will start the bal_core_dist and openolt services:
 ```shell
 reboot
 ```
+The services can also be stopped/started manually:
+```shell 
+service bal_core_dist stop
+service openolt stop
+service bal_core_dist start
+service openolt start
+```
 
-## Run OpenOLT
+Check the status of the services:
+```shell
+service bal_core_dist status
+service openolt status
+```
+
+## Run OpenOLT in foreground
+
+Running the bal_core_dist and/or openolt services in the forground is useful for development and debugging. Make sure to first stop the services if they are running in background.
 
 Open a terminal and run the Broadcom BAL software (*bal_core_dist*):
 
diff --git a/mkdebian/build_asfvolt16_deb.sh b/mkdebian/build_asfvolt16_deb.sh
index 1aed343..6c60959 100755
--- a/mkdebian/build_asfvolt16_deb.sh
+++ b/mkdebian/build_asfvolt16_deb.sh
@@ -1,4 +1,3 @@
 #!/bin/bash
 export ONL_ARCH="amd64"
 dpkg-buildpackage -b -us -uc -a"$ONL_ARCH"
-
diff --git a/mkdebian/debian/asfvolt16.postinst b/mkdebian/debian/asfvolt16.postinst
index 5369f78..b12bfb2 100644
--- a/mkdebian/debian/asfvolt16.postinst
+++ b/mkdebian/debian/asfvolt16.postinst
@@ -5,8 +5,16 @@
 tar zxf /tmp/release_asfvolt16_V02.06.201804301043.tar.gz -C / 2>&1 > /dev/null
 grep -q '\/opt\/bcm68620\/svk_init.sh' /etc/rc.local || sed -i -e '$i \/opt\/bcm68620\/svk_init.sh' /etc/rc.local
 rm -f /run/bcm68620
+cp /tmp/init.d/bal_core_dist /etc/init.d
+cp /tmp/init.d/openolt /etc/init.d
+chmod +x /etc/init.d/bal_core_dist
+chmod +x /etc/init.d/openolt
+update-rc.d bal_core_dist defaults
+update-rc.d openolt defaults
 mv /tmp/openolt /broadcom
 mv /tmp/libgrpc++.so.1 /usr/local/lib
 mv /tmp/libgrpc.so.6 /usr/local/lib
 mv /broadcom/libbal_api_dist.so /usr/local/lib
 /opt/bcm68620/svk_init.sh
+#service bal_core_dist start
+#service openolt start
diff --git a/mkdebian/debian/asfvolt16.preinst b/mkdebian/debian/asfvolt16.preinst
index 9377c9a..047e7fe 100644
--- a/mkdebian/debian/asfvolt16.preinst
+++ b/mkdebian/debian/asfvolt16.preinst
@@ -4,9 +4,13 @@
 echo "kill running time application"
 [ -n "`pidof bcm_user_appl`" ] && killall bcm_user_appl
 [ -n "`pidof bcm.user`" ] && killall bcm.user
-[ -n "`pidof bal_core_dist`" ] && killall bal_core_dist 
 [ -n "`pidof voltha_bal_driver`" ] && killall voltha_bal_driver
+# service bal_core_dist stop
+# service openolt stop
+[ -n "`pidof bal_core_dist`" ] && killall bal_core_dist 
+rm -f /var/run/bal_core_dist.pid
 [ -n "`pidof openolt`" ] && killall openolt
+rm -f /var/run/openolt.pid
 
 # unload drivers
 echo "unload kernel drivers"
diff --git a/mkdebian/debian/rules b/mkdebian/debian/rules
index 8a7f968..33d257a 100755
--- a/mkdebian/debian/rules
+++ b/mkdebian/debian/rules
@@ -30,6 +30,7 @@
 	cp -a $(CURDIR)/debian/libgrpc++.so.1 $(DEB_DH_INSTALL_SOURCEDIR)/tmp
 	cp -a $(CURDIR)/debian/libgrpc.so.6 $(DEB_DH_INSTALL_SOURCEDIR)/tmp
 	cp -a $(CURDIR)/debian/openolt $(DEB_DH_INSTALL_SOURCEDIR)/tmp
+	cp -a $(CURDIR)/debian/init.d $(DEB_DH_INSTALL_SOURCEDIR)/tmp
 
 #override_dh_shlibdeps:
 #	dh_shlibdeps --dpkg-shlibdeps-params=--ignore-missing-info -l$(ONLP_LIB_PATH):$(OFDPA_LIB_PATH)
diff --git a/scripts/init.d/bal_core_dist b/scripts/init.d/bal_core_dist
new file mode 100755
index 0000000..3fc9d78
--- /dev/null
+++ b/scripts/init.d/bal_core_dist
@@ -0,0 +1,78 @@
+#!/bin/sh
+
+### BEGIN INIT INFO
+# Provides:          bal_core_dist
+# Required-Start:    $local_fs $network $syslog
+# Required-Stop:     $network $local_fs $syslog
+# Default-Start:     2 3 4 5
+# Default-Stop:      0 1 6
+# Short-Description: Broadcom BAL core daemon
+# Description:       Broadcom BAL core daemon
+### END INIT INFO
+
+NAME="bal_core_dist"
+PATH="/broadcom:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"
+APPDIR="/broadcom"
+#APPBIN="/broadcom/bal_core_dist"
+APPBIN="bal_core_dist"
+APPARGS="-C :55001"
+USER="root"
+GROUP="root"
+
+# Include functions 
+set -e
+. /lib/lsb/init-functions
+
+start() {
+  touch /var/log/$NAME.log
+  printf "Starting '$NAME'... "
+  start-stop-daemon --verbose --start --chuid "$USER:$GROUP" --background --no-close --make-pidfile --pidfile /var/run/$NAME.pid --chdir "$APPDIR" --exec "$APPBIN" -- $APPARGS < /dev/tty1 >> /var/log/$NAME.log 2>&1 || true
+  printf "done\n"
+}
+
+#We need this function to ensure the whole process tree will be killed
+killtree() {
+    local _pid=$1
+    local _sig=${2-TERM}
+    for _child in $(ps -o pid --no-headers --ppid ${_pid}); do
+        killtree ${_child} ${_sig}
+    done
+    kill -${_sig} ${_pid}
+}
+
+stop() {
+  printf "Stopping '$NAME'... "
+  [ -z `cat /var/run/$NAME.pid 2>/dev/null` ] || \
+  while test -d /proc/$(cat /var/run/$NAME.pid); do
+    killtree $(cat /var/run/$NAME.pid) 15
+    sleep 0.5
+  done 
+  [ -z `cat /var/run/$NAME.pid 2>/dev/null` ] || rm /var/run/$NAME.pid
+  printf "done\n"
+}
+
+status() {
+  status_of_proc -p /var/run/$NAME.pid $APPDIR/$APPBIN $NAME && exit 0 || exit $?
+}
+
+case "$1" in
+  start)
+    start
+    ;;
+  stop)
+    stop
+    ;;
+  restart)
+    stop
+    start
+    ;;
+  status)
+    status
+    ;;
+  *)
+    echo "Usage: $NAME {start|stop|restart|status}" >&2
+    exit 1
+    ;;
+esac
+
+exit 0
diff --git a/scripts/init.d/openolt b/scripts/init.d/openolt
new file mode 100755
index 0000000..bd414c9
--- /dev/null
+++ b/scripts/init.d/openolt
@@ -0,0 +1,79 @@
+#!/bin/sh
+
+### BEGIN INIT INFO
+# Provides:          openolt
+# Required-Start:    $all
+# Required-Stop:     $network $local_fs $syslog
+# Default-Start:     2 3 4 5
+# Default-Stop:      0 1 6
+# Short-Description: Openolt
+# Description:       Openolt start-stop-daemon - Debian
+### END INIT INFO
+
+NAME="openolt"
+PATH="/broadcom:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"
+APPDIR="/broadcom"
+#APPBIN="/broadcom/openolt"
+APPBIN="openolt"
+APPARGS="-C 127.0.0.1:55001"
+USER="root"
+GROUP="root"
+
+# Include functions 
+set -e
+. /lib/lsb/init-functions
+
+start() {
+  touch /var/log/$NAME.log
+  printf "Starting '$NAME'... "
+  sleep 20
+  start-stop-daemon --verbose --start --chuid "$USER:$GROUP" --background --no-close --make-pidfile --pidfile /var/run/$NAME.pid --chdir "$APPDIR" --exec "$APPBIN" -- $APPARGS < /dev/tty1 >> /var/log/$NAME.log 2>&1 || true
+  printf "done\n"
+}
+
+#We need this function to ensure the whole process tree will be killed
+killtree() {
+    local _pid=$1
+    local _sig=${2-TERM}
+    for _child in $(ps -o pid --no-headers --ppid ${_pid}); do
+        killtree ${_child} ${_sig}
+    done
+    kill -${_sig} ${_pid}
+}
+
+stop() {
+  printf "Stopping '$NAME'... "
+  [ -z `cat /var/run/$NAME.pid 2>/dev/null` ] || \
+  while test -d /proc/$(cat /var/run/$NAME.pid); do
+    killtree $(cat /var/run/$NAME.pid) 15
+    sleep 0.5
+  done 
+  [ -z `cat /var/run/$NAME.pid 2>/dev/null` ] || rm /var/run/$NAME.pid
+  printf "done\n"
+}
+
+status() {
+  status_of_proc -p /var/run/$NAME.pid $APPDIR/$APPBIN $NAME && exit 0 || exit $?
+}
+
+case "$1" in
+  start)
+    start
+    ;;
+  stop)
+    stop
+    ;;
+  restart)
+    stop
+    start
+    ;;
+  status)
+    status
+    ;;
+  *)
+    echo "Usage: $NAME {start|stop|restart|status}" >&2
+    exit 1
+    ;;
+esac
+
+exit 0