#!/sbin/runscript
#
# Start the iSCSI-SCST Target.
#

depend() {
        need net
        after firewall
}

configure_memsize() {
        if [ ! -z "$MEM_SIZE" ]; then
                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
        fi
}

check_dev_handlers() {
        if lsmod | grep "scst_" >/dev/null; then
                ewarn "Device handlers found"
        else
                ewarn "No SCST device handlers loaded!"
                ewarn "You can add them in /etc/modules"
        fi
}

start() {
        ebegin "Starting iscsi"
        check_dev_handlers
        modprobe -q crc32c
        modprobe -q iscsi-scst
        start-stop-daemon --start --exec /usr/sbin/iscsi-scstd
        /usr/sbin/scstadmin -config /etc/scst.conf
        eend 0
}

stop() {
        ebegin "Stopping iscsi"
        start-stop-daemon --stop --exec /usr/sbin/iscsi-scstd
        rmmod -w iscsi-scst
        eend 0
}