#! /bin/sh

#
# Copyright (c) 1990-2026, Dell
#
# All rights reserved.  This is an UNPUBLISHED work, and
# comprises proprietary and confidential information of EMC.
# Unauthorized use, disclosure, and distribution are strictly
# prohibited.  Use, duplication, or disclosure of the software
# and documentation by the U.S. Government is subject to
# restrictions set forth in a license agreement between the
# Government and EMC or other written agreement specifying
# the Government's rights to use the software and any applicable
# FAR provisions, such as FAR 52.227-19.
#

# chkconfig: 35 94 06
# description: Block-based Backup driver.

### BEGIN INIT INFO
# Provides: nsrbbb
# Required-Start: syslog network
# Required-Stop: syslog network
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Description: nsrbbb. Block-based backup driver service.
### END INIT INFO

kernel=`uname -r`
tracked_majors=199,253,252,254
driver_file=/lib/modules/"$kernel"/extra/nsrbbb.ko
driver_file_path=`dirname "$driver_file"`
control_file=/dev/nsrbbb-control
sles11sp1_control_file=/dev/nsrbbb

verify_nsrbbb_control()
{
	if [ ! -e "$control_file" ]; then
		if [ -e "$sles11sp1_control_file" ]; then
			# Create symbolic link
   		 	ln -s "$sles11sp1_control_file" "$control_file"
		else
			(echo 'Block-based backup control device could not be created')
		fi
	fi
}

start_service()
{
	(echo 'Starting block-based backup driver service:') > /dev/console
	status=`lsmod | grep nsrbbb | awk {'print $1'}`
	if [ "$status" == "nsrbbb" ]; then
		(echo 'Block-based backup driver service already started')
	else
		if [ ! -h "$driver_file" ]; then
			init_bbb
			bbbret="$?"
			if [ "$bbbret" -ne 1 ]; then
				exit 4
			fi
		fi
		sync
                kallsyms_lookup=`grep " kallsyms_lookup_name"  /proc/kallsyms | cut  -d " " -f 1 | tr 'abcdef' 'ABCDEF'`
                kallsyms_lookup_name=`echo "ibase=16; " \$kallsyms_lookup | bc`
		insmod "$driver_file" tracked_majors=$tracked_majors resolve_syms=$kallsyms_lookup_name
		sleep 5
		status=`lsmod | grep nsrbbb | awk {'print $1'}`
		if [ "$status" == "nsrbbb" ]; then
			# On SLES 11 SP1 control device is created as nsrbbb instead of nsrbbb-control
			verify_nsrbbb_control
		else
			(echo 'Unable to start block-based backup driver service')
		fi
	fi
}

stop_service()
{
	(echo 'Stopping block-based backup driver service:') > /dev/console
	rmmod nsrbbb
	if [ "$?" -ne 0 ]; then
		(echo ' Block-based backup driver service not stopped')
	fi

	# Only for SLES 11 SP1
	if [ -h "$control_file" ]; then
		rm "$control_file"
	fi
}

show_status()
{
	(echo 'Status for block-based backup driver service: ') > /dev/console
	status=`lsmod | grep nsrbbb | awk {'print $1'}`
	if [ "$status" == "nsrbbb" ]; then
		(echo 'Block-based backup driver service: running')
	else
		(echo 'Block-based backup driver service: stopped')
	fi
}

init_bbb()
{
	issupported=0
	nsrbbb=`/bin/sh /opt/dpsapps/fsagent/bin/bbb-driver.sh -d`

	if [ -f /opt/dpsapps/fsagent/bin/"$nsrbbb" ]; then
		rm -f "$driver_file"
		mkdir -p "$driver_file_path"
		ln -s /opt/dpsapps/fsagent/bin/"$nsrbbb" "$driver_file"
		issupported=1
	else
		(echo 'OS not supported for block-based backup.')
		issupported=0
	fi

	return $issupported
}

# MAIN
case "$1" in
	start)
		start_service
		;;
	stop)
		stop_service
		;;
	status)
		show_status
		;;
	*)
		echo "usage: `basename $0` {start|stop|status}"
		;;
esac

