rc script for I SCSI

Danny Braniss danny at cs.huji.ac.il
Wed Jan 14 06:42:11 PST 2009


this is an early attempt:
#!/bin/sh

# PROVIDE: iscsi
# REQUIRE: NETWORKING
# BEFORE:  DAEMON
# KEYWORD: nojail shutdown

#
# Add the following lines to /etc/rc.conf to enable iscsi:
#
# iscsi_enable="YES"
# iscsi_fstab="/etc/fstab.iscsi"

. /etc/rc.subr
. /cs/share/etc/rc.subr

name=iscsi
rcvar=`set_rcvar`

command=/sbin/iscontrol

iscsi_enable=${iscsi_enable:-"NO"}
iscsi_fstab=${iscsi_fstab:-"/etc/fstab.iscsi"}
iscsi_exports=${iscsi_exports:-"/etc/exports.iscsi"}
iscsi_debug=${iscsi_debug:-0}
start_cmd="iscsi_start"
faststop_cmp="iscsi_stop"
stop_cmd="iscsi_stop"

start_precmd="iscontrol_precmd"
iscontrol_prog=${iscontrol_prog:-"iscontrol"}
iscontrol_log=${iscontrol_log:-"/var/log/$iscontrol_prog"}
iscontrol_syslog=${iscontrol_syslog:-"644  3     100  *     JC"}

iscontrol_precmd()
{
    setup_syslog "$iscontrol_prog" "$iscontrol_log" "$iscontrol_syslog"
}

iscsi_wait()
{
    dev=$1
    trap "echo 'wait loop cancelled'; exit 1" 2
    count=0
    while true; do
	if [ -c $dev ]; then
	    break;
	fi
	if [ $count -eq 0 ]; then
	     echo -n Waiting for ${dev}': '
	fi
	count=$((${count} + 1))
	if [ $count -eq 6 ]; then
	    echo " Failed for dev=$dev"
	    return 0
	    break
	fi
	echo -n '.'
	sleep 5;
    done
    echo "$dev ok."
    return 1
}

iscsi_start()
{
    #
    # load needed modules
    for m in iscsi_initiator geom_label; do
	kldstat -qm $m || kldload $m
    done

    sysctl debug.iscsi_initiator=$iscsi_debug
    #
    # start iscontrol for each target
    if [ -n "${iscsi_targets}" ]; then
	for target in ${iscsi_targets}; do
	    ${command} ${rc_flags} -n ${target}
	done
    fi

    if [ -f "${iscsi_fstab}" ]; then
	while read spec file type opt t1 t2
	do
	  case ${spec} in
	  \#*|'')
		;;
	  *)
	  	if iscsi_wait ${spec}; then
		    break;
		fi
		echo type=$type spec=$spec file=$file
		fsck -p ${spec} && mkdir -p ${file} && mount ${spec} ${file}
		chmod 755 ${file} 
		;;
	  esac
	done < ${iscsi_fstab} 
    fi

    if [ -f "${iscsi_exports}" ]; then
	cat ${iscsi_exports} >> /etc/exports
	#/etc/rc.d/mountd reload
	kill -1 `cat /var/run/mountd.pid`
    fi
    if checkyesno zfs_enable; then
	if checkyesno iscsi_zfs; then
	    sleep 4
  	    zpool import -d /dev/label -a
# presumably we can ignore zfs mount -a share -a and swapon
# some zfs dataset may be exported "legacy", so hup mountd
	    kill -1 `cat /var/run/mountd.pid`
	fi
    fi
}

iscsi_stop()
{
    echo 'iscsi stopping'
    while read spec file type opt t1 t2
	do
	  case ${spec} in
	  \#*|'')
		;;
	  *)
	  	echo iscsi: umount $spec
	  	umount -fv $spec
		;;
	  esac
     done < ${iscsi_fstab} 
}

load_rc_config $name
run_rc_command "$1"




More information about the freebsd-scsi mailing list