File zfcp_san_disc of Package s390-tools

#!/bin/bash
#
# zfcp_san_disc
#
# Outputs a list of zFCP WWPNs or LUNs
#
# Usage:
#   zfcp_san_disc [-h | -W | -L -p <WWPN> ] -b <BUS_ID>
#
# Return codes
#   1  zFCP sysfs directory not available
#   2  Invalid command line parameter
#   3  Too many commands used
#   4  Error retrieving HBA list
#   5  Bus ID not found
#   6  Error retrieving Port list
#   7  WWPN not found
#   8  Bus ID sysfs directory not available
#   9  WWPN sysfs directory not available/unable to add port to Bus ID
#  10  Error retrieving LUN list
#  11  HBA API device not available
#

START_DIR=`pwd`
SCRIPT_NAME=`basename $0`
cd `dirname $0`
SCRIPT_DIR=`pwd`
cd "${START_DIR}"

FCP_SYS_DIR='/sys/bus/ccw/drivers/zfcp'

# Commands available
LIST_WWPN='-W'
LIST_LUN='-L'

COMMAND=''
BUSID=''
WWPN=''

echo_err()
{
	echo "$SCRIPT_NAME: $1" 1>&2
}

usage()
{
	echo "$0 [-h | $LIST_WWPN | $LIST_LUN -p <WWPN>] -b <BUS_ID>" 1>&2
	echo 1>&2
	echo "Commands:" 1>&2
	echo "	$LIST_WWPN		List WWPNs for the given BUS_ID" 1>&2
	echo "	$LIST_LUN		List LUNs for the given BUS_ID and WWPN" 1>&2
	echo "	-h		This usage information" 1>&2
	echo "Options:" 1>&2
	echo "	-b BUSID	Bus ID to use for listing" 1>&2
	echo "	-p WWPN		WWPN to use for listing" 1>&2
}

list_lun()
{
	local PRINT_WWPN
	local BUSID_DIR
	local WWPN_DIR
	local ADDED_PORT

}

while [ $# -gt 0 ]
do
	case "$1" in
		-b* )
			if [ "$1" == "-b" ]
			then
				shift
				BUSID="$1"
			else
				BUSID="${1:2}"
			fi
			BUSID=`echo $BUSID | tr A-F a-f`
			;;
		-p* )
			if [ "$1" == "-p" ]
			then
				shift
				WWPN="$1"
			else
				WWPN="${1:2}"
			fi
			WWPN=`echo $WWPN | tr A-FX a-fx`
			;;
		"$LIST_WWPN"|"$LIST_LUN" )
			if [ -z "$COMMAND" -o "$1" == "$COMMAND" ]
			then
				COMMAND=$1
			else
				echo_err "You have alread specified the $COMMAND command, and cannot use the $1 command also."
				exit 3
			fi
			;;
		-h )
			usage
			exit 0
			;;
		* )
			usage
			echo_err "Unknown command line parameter : $1"
			exit 2
			;;
	esac
	shift
done

if [ -z "$BUSID" ] ; then
    echo_err "No bus ID given"
    exit 2
fi

if [ -z "$COMMAND" ] ; then
    echo_err "Please specify either -W or -L"
    exit 2
fi

if [ ! -d /sys/bus/ccw/devices/$BUSID ] ; then
    echo_err "Unknown bus ID $BUSID"
    exit 2
fi

read devtype < /sys/bus/ccw/devices/$BUSID/devtype
read cutype < /sys/bus/ccw/devices/$BUSID/cutype

if [ "$cutype" != "1731/03" ] ; then
    echo_err "Bus ID $BUSID is not an zfcp adapter"
    exit 2
fi

if [ "$devtype" != "1732/03" -a "$devtype" != "1732/04" ] ; then
    echo_err "Bus ID $BUSID is not an zfcp adapter"
    exit 2
fi

# Now we're sure we're dealing with zfcp devices
if [ ! -d "$FCP_SYS_DIR" ] ; then
    modprobe zfcp
fi

read online < /sys/bus/ccw/devices/$BUSID/online

if [ "$online" != 1 ] ; then
    # Activate adapter
    echo 1 > /sys/bus/ccw/devices/$BUSID/online
    read online < /sys/bus/ccw/devices/$BUSID/online

    if [ "$online" != 1 ] ; then
	echo_err "Cannot activate zfcp adapter at $BUSID"
	exit 2
    fi
    online=2
fi

[ "$COMMAND" == "$LIST_LUN" -a -z "$WWPN" ] && usage && exit 2

for loop in 1 2 3 4 5 ; do
	read status < /sys/bus/ccw/devices/$BUSID/status
	(( $status & 0x10000000 )) && break;
done
read wwpn_status < /sys/bus/ccw/devices/$BUSID/status
if !(( $wwpn_status & 0x10000000 )) ; then
	echo_err "Adapter activation failed, status $wwpn_status"
	[ "$online" = 2 ] && echo 0 > /sys/bus/ccw/devices/$BUSID/online
	exit 3
fi

for host in /sys/bus/ccw/devices/$BUSID/host* ; do
    if [ -d $host ] ; then
	hba_num=${host##*host}
    fi
done
if [ -z "$hba_num" ] ; then
    echo_err "No SCSI host allocated"
    [ "$online" = 2 ] && echo 0 > /sys/bus/ccw/devices/$BUSID/online
    exit 3
fi

if [ "$COMMAND" == "$LIST_WWPN" ]
then
	for PRINT_WWPN in /sys/bus/ccw/devices/$BUSID/0x*
	do
		test -d $PRINT_WWPN && echo ${PRINT_WWPN##*/}
	done
	[ "$online" = 2 ] && echo 0 > /sys/bus/ccw/devices/$BUSID/online

	exit 0
elif [ "$COMMAND" == "$LIST_LUN" ]
then
	ERR=0
	PRINT_WWPN=''
	WWPN_DIR=/sys/bus/ccw/devices/$BUSID/$WWPN
	for FIND_WWPN in /sys/bus/ccw/devices/$BUSID/0x*
	do
		if [ "$FIND_WWPN" = "$WWPN_DIR" ]
		then
			PRINT_WWPN="${FIND_WWPN##*/}"
			break
		fi
	done

	if [ ! -d "${WWPN_DIR}" ]
	then
		echo_err "port $WWPN not found on zfcp $BUSID"
		[ "$online" = 2 ] && echo 0 > /sys/bus/ccw/devices/$BUSID/online
		exit 9
	fi

	# Activate WLUN
	if [ ! -d $WWPN_DIR/0xc101000000000000 ] ; then
	    echo 0xc101000000000000 > $WWPN_DIR/unit_add
	    activated=1

	    # Wait for udev to catch up
	    udevadm settle
	    sleep 1

	    # Find corresponding sg device
	    sgdev=$(lsscsi -t -g $hba_num:-:-:49409 | sed -n "s/.* fc:$WWPN,.* \(\/dev\/sg.*\)/\1/p")
	    if [ -c "$sgdev" ] ; then
		wlun=1
	    else
		echo 0xc101000000000000 > $WWPN_DIR/unit_remove
		activated=
		sgdev=
	    fi
	fi
	if [ -z "$wlun" ] ; then
	    # Activate LUN 0
	    if [ ! -d $WWPN_DIR/0x0000000000000000 ] ; then
		echo 0 > $WWPN_DIR/unit_add
		activated=1
	    fi

	    # Wait for udev to catch up
	    udevadm settle
	    sleep 1

	    # Find corresponding sg device
	    sgdev=$(lsscsi -t -g $hba_num:-:-:0 | sed -n "s/.* fc:$WWPN,.* \(\/dev\/sg.*\)/\1/p")
	    if [ ! -c "$sgdev" ] ; then
		echo_err "Unable to activate LUN 0"
		echo 0x0000000000000000 > $WWPN_DIR/unit_remove
		activated=
		sgdev=
		ERR=10
	    fi
	fi
	if [ "$sgdev" ] ; then
	    if sg_luns $sgdev > /dev/null; then
		LUN_LIST=`sg_luns $sgdev | sed -n 's/^    \(.*\)/\1/p'`
	    else
		ERR=10
	    fi
	fi

	for LUN in $LUN_LIST ; do
	    echo 0x$LUN
	done
	if [ "$activated" = 1 ] ; then
	    if [ -n "$sgdev" ] ; then
		sgnum=${sgdev#/dev/sg}
		: deactivate /sys/class/scsi_generic/sg$sgnum/device/delete
		echo 1 > /sys/class/scsi_generic/sg$sgnum/device/delete
		udevadm settle
	    fi
	    if [ "$wlun" = 1 ] ; then
		echo 0xc101000000000000 > $WWPN_DIR/unit_remove
		wlun=
	    else
		echo 0x0000000000000000 > $WWPN_DIR/unit_remove
	    fi
	    activated=
	fi
	[ "$online" = 2 ] && echo 0 > /sys/bus/ccw/devices/$BUSID/online

	exit $ERR
fi