Debian init script from scratch

I am trying to write an init script for the SCST iSCSI target software for Debian / Ubuntu. The current one in the download does not work and seems to be outfitted to work on SuSE and others.

This what I have so far, but I am basing this off of my limited knowledge of init scripts and a lot of google searches.

I am just wondering if I am over thinking this, do I need to unload the modules everytime I stop the script. The modules to no load on startup, I know I need to start the iscsi daemon and load the config.

Any help would be appreciated.

#!/bin/sh

DAEMON=/usr/local/sbin/iscsi-scstd
PIDFILE=/var/run/iscsi-scstd.pid
CONFIG=/etc/scst/scst.conf

case "$1" in

start)
    echo "Starting SCST iSCSI target service: "
    modprobe scst
    modprobe scst_vdisk
    modprobe iscsi_scst
    start-stop-daemon --start --quiet --exec $DAEMON
    scstadmin -config $CONFIG
    $RETVAL=$?
    if [ $RETVAL == "0" ]; then
        echo -n "Success"
    fi
    ;;

stop)
    echo "Stopping SCST iSCSI target service: "
    modprobe -r scst
    modprobe -r scst_vdisk
    modprobe -r iscsi_scst
    start-stop-daemon --stop --quiet --exec $DAEMON --pidfile $PIDFILE
    $RETVAL=$?
    if [ $RETVAL == "3" ]; then
        echo -n "Success"
    fi
    if [ $RETVAL == "1" ]; then
        rm -f $PIDFILE
        echo -n "Success"
    fi
    ;;

restart)
    echo "Stopping SCST iSCSI target service: "
    modprobe -r scst
    modprobe -r scst_vdisk
    modprobe -r iscsi_scst
    start-stop-daemon --stop --quiet --exec $DAEMON --pidfile $PIDFILE
    sleep 2
    echo "Stopping SCST iSCSI target service: "
    modprobe scst
    modprobe scst_vdisk
    modprobe iscsi_scst
    start-stop-daemon --start --quiet --exec $DAEMON
    scstadmin -config $CONFIG
    ;;

status)
    $RETVAL=$?
    if [ $RETVAL == "0" ]; then
        echo -n "SCST iSCSI target service is running"
    fi
    if [ $RETVAL == "3" ]; then
        echo -n "SCST iSCSI target service is not running"
    fi
    if [ $RETVAL == "4" ]; then
        echo -n "SCST iSCSI target service is unknown"
    fi  


*)
    echo "Usage: /etc/init.d/scst {start|stop|restart}"
    exit 1

esac

exit 0

I actually ended up finding exactly what I was looking for in an old Ubuntu PPA for my iSCSI target. So this question is pretty much moot.


Debian Squeeze is moving to a dependency-based ordering of init scripts rather than numbering the scripts sequentially. If you want to use this, you'll need special comments in your script for insserv (the update-rc.d replacement) to pick up.

There is a guide to writing a "proper" init script on Debian's wiki here.

Ubuntu has gone with upstart to replace init, but has left an init compatibility system in place so the init script you write following Debian's rules should work properly in Ubuntu. Otherwise, you can write an upstart service file (a too-basic-for-your-needs example is given on Ubuntu's wiki here)

As for the rest, unloading the modules is probably not necessary (and not always possible). If you do unload the modules, I would assume they need to be unloaded after the service is stopped. You may also need to reverse the order of unloading the modules if the modules started later depend on the modules started earlier. Depending on how quickly the daemon gets from "started" to "ready to have configuration loaded into it", you may need a sleep 1 in there before running scstadmin (or you may not. Just something to be aware of it fails.)


Check out /etc/init.d/skeleton for an example. This should be present on an Ubuntu host.