#!/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

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()
{
	echo -n "Starting iSCSI enterprise target service: "
	configure_memsize
	modprobe -q crc32c
	modprobe iscsi_trgt
	start-stop-daemon --start --exec $DAEMON --quiet
	RETVAL=$?
	if [ $RETVAL = "0" ]; then
	    echo "succeeded."
	else
	    echo "failed."
	fi	    
}
	
ietd_stop()
{
	echo -n "Removing iSCSI enterprise target devices: "
	# ugly, but ietadm does not allways provides correct exit values
	RETURN=`ietadm --op delete 2>&1`
	RETVAL=$?
	if [ $RETVAL = "0" ] && [ "$RETURN" != "something wrong" ] ; then
	    echo "succeeded."
	else
	    echo "failed with reason :$RETURN"
	    exit 1
	fi

        echo -n "Stopping iSCSI enterprise target service: "
        start-stop-daemon --stop --quiet --exec $DAEMON --pidfile $PID_FILE
        RETVAL=$?
        if [ $RETVAL = "0" ]; then
            echo "succeeded."
        else
            echo "failed."
        fi
        # ugly, but pid file is not removed ba ietd
        rm -f $PID_FILE

	echo -n "Removing iSCSI enterprise target modules: "
	modprobe -r iscsi_trgt
	RETVAL=$?
	modprobe -r crc32c 2>/dev/null
	if [ $RETVAL = "0" ]; then
	    echo "succeeded."
	else
	    echo "failed."
	    exit 1
	fi
}

case "$1" in
  start)
        ietd_start
        ;;
  stop)
        ietd_stop
        ;;
  restart|force-reload)
        ietd_stop
	sleep 1
	ietd_start
        ;;
  status)
	PID=`pidof ietd`
	if [ $PID ]; then
		echo "iSCSI enterprise target is running at pid $PID"
	else
		echo "no iSCSI enterprise target found!"
		exit 1
	fi	
        ;;
  dump)
	DUMP=`tempfile -p ietd`
	RETVAL=$?
	if [ $RETVAL != "0" ]; then
	    echo "Failed to create dump file $DUMP"
	    exit 1
	fi
        ietadm --mode dump --all >$DUMP
	RETVAL=$?
	if [ $RETVAL != "0" ]; then
	    echo "Error dumping config from daemon"
	    rm $DUMP
	    exit 1
	fi
	mv -u $DUMP $CONFIG_FILE
	echo "Config dumped to $CONFIG_FILE"
        ;;
  *)
        echo "Usage: $0 {start|stop|restart|status|dump}"
        exit 1
esac

exit 0
