1#!/bin/sh 2 3### BEGIN INIT INFO 4# Provides: tgtd 5# Required-Start: $remote_fs $network $syslog 6# Required-Stop: $remote_fs $syslog 7# Default-Start: 3 5 8# Default-Stop: 0 1 2 6 9# Short-Description: SCSI target daemon 10# Description: Linux SCSI target framework (tgt) 11### END INIT INFO 12 13DESC="tgtd" 14DAEMON="/usr/sbin/tgtd" 15TGTD_CONFIG=/etc/tgt/targets.conf 16 17start () 18{ 19 echo -n "Starting $DESC..." 20 21 # Ensure service isn't running 22 tgt-admin -s >/dev/null 2>&1 23 RETVAL=$? 24 if [ "$RETVAL" -ne 107 ] ; then 25 echo "$DESC is already running." 26 exit 1 27 fi 28 29 # Start tgtd first 30 $DAEMON &>/dev/null 31 RETVAL=$? 32 if [ "$RETVAL" -ne 0 ]; then 33 echo "failed." 34 exit 1 35 fi 36 37 # Put tgtd into "offline" state until all the targets are configured. 38 # We don't want initiators to (re)connect and fail the connection 39 # if it's not ready. 40 tgtadm --op update --mode sys --name State -v offline 41 # Configure the targets. 42 tgt-admin -f -e -c $TGTD_CONFIG 43 # Put tgtd into "ready" state. 44 tgtadm --op update --mode sys --name State -v ready 45 46 echo "done." 47} 48 49stop () 50{ 51 echo -n "Stopping $DESC..." 52 53 # Remove all targets. It only removes targets which are not in use. 54 tgt-admin --update ALL -c /dev/null &>/dev/null 55 # tgtd will exit if all targets were removed 56 tgtadm --op delete --mode system &>/dev/null 57 RETVAL=$? 58 if [ "$RETVAL" -eq 107 ] ; then 59 if [ "$TASK" != "restart" ] ; then 60 return 1 61 fi 62 elif [ "$RETVAL" -ne 0 ] ; then 63 echo "Some initiators are still connected - could not stop tgtd" 64 return 2 65 fi 66 echo -n 67} 68 69reload() 70{ 71 echo "Reloading configuration of $DESC" "$NAME" 72 # Update configuration for targets. Only targets which 73 # are not in use will be updated. 74 tgt-admin --update ALL -c $TGTD_CONFIG &>/dev/null 75 RETVAL=$? 76 if [ "$RETVAL" -eq 107 ] ; then 77 echo "tgtd is not running" 78 exit 1 79 fi 80} 81 82status() 83{ 84 tgt-admin -s >/dev/null 2>&1 85 RETVAL=$? 86 if [ "$RETVAL" -eq 107 ] ; then 87 echo "tgtd is not running" 88 else 89 echo "tgtd is running" 90 fi 91} 92 93case "$1" in 94 start) 95 start 96 ;; 97 stop) 98 stop 99 ;; 100 restart|force-reload) 101 stop 102 start 103 ;; 104 reload) 105 reload 106 ;; 107 status) 108 status 109 ;; 110 *) 111 echo "Usage: $0 {start|stop|force-reload|restart|status|reload}" 112 exit 1 113 ;; 114esac 115 116exit 0 117