#!/bin/sh
#
### BEGIN INIT INFO
# Provides:          cluster manager
# Required-Start:    $network $time
# Required-Stop:     $network $time
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts and stops the iSCSI target
### END INIT INFO

PID_FILE=/var/run/iscsi_trgt.pid
CONFIG_FILE=/etc/ietd.conf
DAEMON=/usr/sbin/ietd

PATH=/sbin:/bin:/usr/sbin:/usr/bin

# Don't touch this "memsize thingy" unless you are blessed
# with knowledge about it.
MEM_SIZE=1048576

. /lib/lsb/init-functions # log_{warn,failure}_msg
ISCSITARGET_ENABLE=false
ISCSITARGET_DEFAULTS_FILE=/etc/default/iscsitarget
if [ -s "$ISCSITARGET_DEFAULTS_FILE" ]; then
    . "$ISCSITARGET_DEFAULTS_FILE"
    case "x$ISCSITARGET_ENABLE" in
        xtrue|xfalse) ;;
        *)
            log_failure_msg "value of ISCSITARGET_ENABLE must be either 'true' or 'false';"
            log_failure_msg "not starting iscsitarget."
            exit 1
            ;;
    esac
fi

configure_memsize()
{
    if [ -e /proc/sys/net/core/wmem_max ]; then
        echo ${MEM_SIZE} > /proc/sys/net/core/wmem_max
    fi

    if [ -e /proc/sys/net/core/rmem_max ]; then
        echo ${MEM_SIZE} > /proc/sys/net/core/rmem_max
    fi

    if [ -e /proc/sys/net/core/wmem_default ]; then
        echo ${MEM_SIZE} > /proc/sys/net/core/wmem_default
    fi

    if [ -e /proc/sys/net/core/rmem_default ]; then
        echo ${MEM_SIZE} > /proc/sys/net/core/rmem_default
    fi

    if [ -e /proc/sys/net/ipv4/tcp_mem ]; then
        echo "${MEM_SIZE} ${MEM_SIZE} ${MEM_SIZE}" > /proc/sys/net/ipv4/tcp_mem
    fi

    if [ -e  /proc/sys/net/ipv4/tcp_rmem ]; then
        echo "${MEM_SIZE} ${MEM_SIZE} ${MEM_SIZE}" > /proc/sys/net/ipv4/tcp_rmem
    fi

    if [ -e /proc/sys/net/ipv4/tcp_wmem ]; then
        echo "${MEM_SIZE} ${MEM_SIZE} ${MEM_SIZE}" > /proc/sys/net/ipv4/tcp_wmem
    fi
}

RETVAL=0

ietd_start()
{
	log_daemon_msg "Starting iSCSI enterprise target service"
	configure_memsize
	modprobe -q crc32c && modprobe -q iscsi_trgt
	RETVAL=$?
	if [ $RETVAL != "0" ] ;  then 
		log_end_msg 1
		exit $RETVAL
	fi
	start-stop-daemon --start --exec $DAEMON --quiet --oknodo
	RETVAL=$?
	if [ $RETVAL != "0" ]; then
		log_end_msg 1
		exit $RETVAL
	fi
	log_end_msg 0
	exit 0
}
	
ietd_stop()
{
	log_daemon_msg "Removing iSCSI enterprise target devices"
	pgrep -s `cat $PID_FILE 2>/dev/null || echo "x"` >/dev/null 2>&1 
	RETVAL=$?
	if [ $RETVAL = "0" ] ; then
		# ugly, but ietadm does not allways provides correct exit values
		RETURN=`ietadm --op delete 2>&1`
		RETVAL=$?
		if [ $RETVAL = "0" ] && [ "$RETURN" != "something wrong" ] ; then
			log_end_msg 0
		else
			log_end_msg 1
			log_failure_msg "Failed with reason: $RETURN"
			exit $RETVAL
		fi
		log_daemon_msg "Stopping iSCSI enterprise target service"
		start-stop-daemon --stop --quiet --exec $DAEMON --pidfile $PID_FILE --oknodo
		RETVAL=$?
		if [ $RETVAL != "0" ]; then
			log_end_msg 1
		else 
			log_end_msg 0
		fi
	else
		log_end_msg 0
	fi
	# ugly, but pid file is not removed ba ietd
	rm -f $PID_FILE 2>/dev/null
	
	# check if the module is loaded at all
	lsmod | grep -q iscsi_trgt
	RETVAL=$?
	if [ $RETVAL = "0" ] ; then
		log_warning_msg "Removing iSCSI enterprise target modules"
		modprobe -r iscsi_trgt 2>/dev/null && modprobe -q crc32c 2>/dev/null
		RETVAL=$?
		if [ $RETVAL = "0" ]; then
			log_end_msg 0
		else
			log_end_msg 1
			# Lack of module unloading should be reported,
			# but not necessarily exit non-zero
		fi
	fi
}

case "$1" in
  start)
        if [ "$ISCSITARGET_ENABLE" = "true" ]; then
            ietd_start
        else
            log_warning_msg "iscsitarget not enabled in \"$ISCSITARGET_DEFAULTS_FILE\", not starting..."
        fi
        ;;
  stop)
        ietd_stop
        ;;
  restart|force-reload)
        ietd_stop
	sleep 1
        if [ "$ISCSITARGET_ENABLE" = "true" ]; then
            ietd_start
        else
            log_warning_msg "iscsitarget not enabled in \"$ISCSITARGET_DEFAULTS_FILE\", not starting..."
        fi
        ;;
  status)
	status_of_proc -p $PID_FILE $DAEMON "iSCSI enterprise target" && exit 0 || exit $?
	;;
  *)
        log_action_msg "Usage: $0 {start|stop|restart|status}"
        exit 1
esac

exit 0
