#!/bin/bash
#
# lve_namespaces        Startup script for lve namespaces initialization
#
# chkconfig: 2345 11 90
# description: lve namespaces

# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2021 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENSE.TXT


# Source function library.
. /etc/rc.d/init.d/functions

prog="lve_namespaces"

RETVAL=0

start() {
    # Do not start if there is no config file.
    [ ! -f /proc/lve/list ] && return 6

    echo -n $"Starting $prog: "

    GREATER=$(uname -r |awk -F'.' '{if ($1 > 3) print "TRUE";else if ($1 == 3 && $2 >= 10) print "TRUE";else print "FALSE"}')
    if [ "x"${GREATER} == "xTRUE" ];then
        /bin/mount --make-rprivate /
        RETVAL=$?
    else
        RETVAL=0
    fi

    # CAG-796: use hidepid=2 when mounting /proc
    /usr/share/cloudlinux/remount_proc.py
    REMOUNT_RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
        RETVAL=$REMOUNT_RETVAL
    fi

    /usr/sbin/lvectl start

    # report retcode for earlier error
    RETVAL_LVESTART=$?
    if [ $RETVAL -eq 0 ]; then
        RETVAL=$RETVAL_LVESTART
    fi

    if [ $RETVAL -eq 0 ]; then
        success
    else
        failure
    fi
    sleep 3
    echo
    return $RETVAL
}

stop() {
    echo -n $"Stopping $prog: "
    success
    echo
    return 0
}

reload() {
    echo -n $"Reloading $prog: "
    /usr/sbin/lvectl start
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
        success
    else
        failure
    fi
    echo
    return $RETVAL
}

status() {
    echo -n "LVE Enabled: "
    if [ ! -f /proc/lve/list ]; then
        failure
        echo
        return 6
    else
        success
        echo
        return 0
    fi
}

# See how we were called.
case "$1" in
  start)
      start
  ;;
  stop)
      stop
  ;;
  restart)
      stop
      start
  ;;
  status)
    status
  ;;
  reload)
      reload
  ;;
  *)
            echo $"Usage: $prog {start|stop|status|restart|reload}"
            exit 1
esac

RETVAL=$?

exit $RETVAL
