#!/bin/bash
#
#
#   iSCSILogicalUnit OCF RA. Exports and manages iSCSI Logical Units.
#
#   (c) 2009-2010 Florian Haas, Dejan Muhamedagic,
#                 and Linux-HA contributors
#				  
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it would be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# Further, this software is distributed without any warranty that it is
# free of the rightful claim of any third person regarding infringement
# or the like.  Any license provided herein, whether implied or
# otherwise, applies only to this software file.  Patent licenses, if
# any, provided herein do not apply to combinations of this program with
# other software, or any other product whatsoever.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
#

#######################################################################
# Initialization:
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs

# Defaults
# Set a default implementation based on software installed
if have_binary ietadm; then
    OCF_RESKEY_implementation_default="iet"
elif have_binary tgtadm; then
    OCF_RESKEY_implementation_default="tgt"
elif have_binary lio_node; then
    OCF_RESKEY_implementation_default="lio"
fi
: ${OCF_RESKEY_implementation=${OCF_RESKEY_implementation_default}}

# Use a default SCSI ID and SCSI SN that is unique across the cluster,
# and persistent in the event of resource migration.
# SCSI IDs are limited to 24 bytes, but only 16 bytes are known to be
# supported by all iSCSI implementations this RA cares about. Thus,
# for a default, use the first 16 characters of
# $OCF_RESOURCE_INSTANCE.
OCF_RESKEY_scsi_id_default="${OCF_RESOURCE_INSTANCE:0:16}"
: ${OCF_RESKEY_scsi_id=${OCF_RESKEY_scsi_id_default}}
# To have a reasonably unique default SCSI SN, use the first 8 bytes
# of an MD5 hash of of $OCF_RESOURCE_INSTANCE
sn=`echo -n "${OCF_RESOURCE_INSTANCE}" | openssl md5 | sed -e 's/(stdin)= //'`
OCF_RESKEY_scsi_sn_default=${sn:0:8}
: ${OCF_RESKEY_scsi_sn=${OCF_RESKEY_scsi_sn_default}}
#######################################################################

meta_data() {
	cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="iSCSILogicalUnit" version="0.9">
<version>0.9</version>

<longdesc lang="en">
Manages iSCSI Logical Unit. An iSCSI Logical unit is a subdivision of 
an SCSI Target, exported via a daemon that speaks the iSCSI protocol.
</longdesc>
<shortdesc lang="en">Manages iSCSI Logical Units (LUs)</shortdesc>

<parameters>
<parameter name="implementation" required="0" unique="0">
<longdesc lang="en">
The iSCSI target daemon implementation. Must be one of "iet", "tgt",
or "lio".  If unspecified, an implementation is selected based on the
availability of management utilities, with "iet" being tried first,
then "tgt", then "lio".
</longdesc>
<shortdesc lang="en">iSCSI target daemon implementation</shortdesc>
<content type="string" default="${OCF_RESKEY_implementation_default}"/>
</parameter>

<parameter name="target_iqn" required="1" unique="0">
<longdesc lang="en">
The iSCSI Qualified Name (IQN) that this Logical Unit belongs to.
</longdesc>
<shortdesc lang="en">iSCSI target IQN</shortdesc>
<content type="string" />
</parameter>

<parameter name="lun" required="1" unique="0">
<longdesc lang="en">
The Logical Unit number (LUN) exposed to initiators.
</longdesc>
<shortdesc lang="en">Logical Unit number (LUN)</shortdesc>
<content type="integer" />
</parameter>

<parameter name="path" required="1" unique="0">
<longdesc lang="en">
The path to the block device exposed. Some implementations allow this
to be a regular file, too.
</longdesc>
<shortdesc lang="en">Block device (or file) path</shortdesc>
<content type="string" />
</parameter>

<parameter name="scsi_id" required="0" unique="1">
<longdesc lang="en">
The SCSI ID to be configured for this Logical Unit. The default
is the resource name, truncated to 24 bytes.
</longdesc>
<shortdesc lang="en">SCSI ID</shortdesc>
<content type="string" default="${OCF_RESKEY_scsi_id_default}"/>
</parameter>

<parameter name="scsi_sn" required="0" unique="1">
<longdesc lang="en">
The SCSI serial number to be configured for this Logical Unit.
The default is a hash of the resource name, truncated to 8 bytes.
</longdesc>
<shortdesc lang="en">SCSI serial number</shortdesc>
<content type="string" default="${OCF_RESKEY_scsi_sn_default}"/>
</parameter>

<parameter name="vendor_id" required="0" unique="0">
<longdesc lang="en">
The SCSI vendor ID to be configured for this Logical Unit.
</longdesc>
<shortdesc lang="en">SCSI vendor ID</shortdesc>
<content type="string" />
</parameter>

<parameter name="product_id" required="0" unique="0">
<longdesc lang="en">
The SCSI product ID to be configured for this Logical Unit.
</longdesc>
<shortdesc lang="en">SCSI product ID</shortdesc>
<content type="string" />
</parameter>

<parameter name="additional_parameters" required="0" unique="0">
<longdesc lang="en">
Additional LU parameters. A space-separated list of "name=value" pairs
which will be passed through to the iSCSI daemon's management
interface. The supported parameters are implementation
dependent. Neither the name nor the value may contain whitespace.
</longdesc>
<shortdesc lang="en">List of iSCSI LU parameters</shortdesc>
<content type="string" />
</parameter>

<parameter name="allowed_initiators" required="0" unique="0">
<longdesc lang="en">
Allowed initiators. A space-separated list of initiators allowed to
connect to this lun. Initiators may be listed in any syntax
the target implementation allows. If this parameter is empty or
not set, access to this lun will not be allowed from any initiator,
if target is not in demo mode.

This parameter is only necessary, when using LIO.
</longdesc>
<shortdesc lang="en">List of iSCSI initiators allowed to connect
to this lun.</shortdesc>
<content type="string" default=""/>
</parameter>

</parameters>

<actions>
<action name="start"        timeout="10" />
<action name="stop"         timeout="10" />
<action name="status"       timeout="10" interval="10" depth="0" />
<action name="monitor"      timeout="10" interval="10" depth="0" />
<action name="meta-data"    timeout="5" />
<action name="validate-all"   timeout="10" />
</actions>
</resource-agent>
END
}

#######################################################################

iSCSILogicalUnit_usage() {
	cat <<END
usage: $0 {start|stop|status|monitor|validate-all|meta-data}

Expects to have a fully populated OCF RA-compliant environment set.
END
}

iSCSILogicalUnit_start() {
    iSCSILogicalUnit_monitor
    if [ $? =  $OCF_SUCCESS ]; then
	return $OCF_SUCCESS
    fi

    local params

    case $OCF_RESKEY_implementation in
	iet)
	    params="Path=${OCF_RESKEY_path}"
	    # use blockio if path points to a block device, fileio
	    # otherwise.
	    if [ -b "${OCF_RESKEY_path}" ]; then
		params="${params} Type=blockio"
	    else
		params="${params} Type=fileio"
	    fi
	    # in IET, we have to set LU parameters on creation
	    if [ -n "${OCF_RESKEY_scsi_id}" ]; then
		params="${params} ScsiId=${OCF_RESKEY_scsi_id}"
	    fi
	    if [ -n "${OCF_RESKEY_scsi_sn}" ]; then
		params="${params} ScsiSN=${OCF_RESKEY_scsi_sn}"
	    fi
	    params="${params} ${OCF_RESKEY_additional_parameters}"
	    ocf_run ietadm --op new \
		--tid=${TID} \
		--lun=${OCF_RESKEY_lun} \
		--params ${params// /,} || exit $OCF_ERR_GENERIC
	    ;;
	tgt)
	    # tgt requires that we create the LU first, then set LU
	    # parameters
	    params=""
	    local var
            local envar
	    for var in scsi_id scsi_sn vendor_id product_id; do
		envar="OCF_RESKEY_${var}"
		if [ -n "${!envar}" ]; then
		    params="${params} ${var}=${!envar}"
		fi
	    done
	    params="${params} ${OCF_RESKEY_additional_parameters}"
	    ocf_run tgtadm --lld iscsi --op new --mode logicalunit \
		--tid=${TID} \
		--lun=${OCF_RESKEY_lun} \
	    	--backing-store ${OCF_RESKEY_path} || exit $OCF_ERR_GENERIC
	    if [ -z "$params" ]; then
		return $OCF_SUCCESS
	    else
		ocf_run tgtadm --lld iscsi --op update --mode logicalunit \
		    --tid=${TID} \
		    --lun=${OCF_RESKEY_lun} \
		    --params ${params// /,} || exit $OCF_ERR_GENERIC
	    fi
	    ;;
	lio)
	    # For lio, we first have to create a target device, then
	    # add it to the Target Portal Group as an LU.
	    ocf_run tcm_node --createdev=iblock_0/${OCF_RESOURCE_INSTANCE} \
		${OCF_RESKEY_path} || exit $OCF_ERR_GENERIC
	    if [ -n "${OCF_RESKEY_scsi_sn}" ]; then
		ocf_run tcm_node --setunitserial=iblock_0/${OCF_RESOURCE_INSTANCE} \
		    ${OCF_RESKEY_scsi_sn} || exit $OCF_ERR_GENERIC
	    fi
	    ocf_run lio_node --addlun=${OCF_RESKEY_target_iqn} 1 ${OCF_RESKEY_lun} \
		${OCF_RESOURCE_INSTANCE} iblock_0/${OCF_RESOURCE_INSTANCE} || exit $OCF_ERR_GENERIC

           if [ -n "${OCF_RESKEY_allowed_initiators}" ]; then
               for initiator in ${OCF_RESKEY_allowed_initiators}; do
                       ocf_run lio_node --addlunacl=${OCF_RESKEY_target_iqn} 1 \
                       ${initiator} ${OCF_RESKEY_lun} ${OCF_RESKEY_lun} || exit $OCF_ERR_GENERIC
               done
           fi
	    ;;
    esac

    return $OCF_SUCCESS
}

iSCSILogicalUnit_stop() {
    iSCSILogicalUnit_monitor
    if [ $? =  $OCF_SUCCESS ]; then
	case $OCF_RESKEY_implementation in
	    iet)
		# IET allows us to remove LUs while they are in use
		ocf_run ietadm --op delete \
		    --tid=${TID} \
		    --lun=${OCF_RESKEY_lun} || exit $OCF_ERR_GENERIC
		;;
	    tgt)
		# tgt will fail to remove an LU while it is in use,
		# but at the same time does not allow us to
		# selectively shut down a connection that is using a
		# specific LU. Thus, we need to loop here until tgtd
		# decides that the LU is no longer in use, or we get
		# timed out by the LRM.
		while ! ocf_run -warn tgtadm --lld iscsi --op delete --mode logicalunit \
		    --tid ${TID} \
		    --lun=${OCF_RESKEY_lun}; do
		    sleep 1
		done
		;;
	    lio)
               if [ -n "${OCF_RESKEY_allowed_initiators}" ]; then
                       for initiator in ${OCF_RESKEY_allowed_initiators}; do
                               ocf_run lio_node --dellunacl=${OCF_RESKEY_target_iqn} 1 \
                               ${initiator} ${OCF_RESKEY_lun} || exit $OCF_ERR_GENERIC
                       done
               fi
		ocf_run lio_node --dellun=${OCF_RESKEY_target_iqn} 1 ${OCF_RESKEY_lun} || exit $OCF_ERR_GENERIC
		ocf_run tcm_node --freedev=iblock_0/${OCF_RESOURCE_INSTANCE} || exit $OCF_ERR_GENERIC
	esac
    fi
    
    return $OCF_SUCCESS
}

iSCSILogicalUnit_monitor() {
    case $OCF_RESKEY_implementation in
	iet)
 	    # Figure out and set the target ID
	    TID=`sed -ne "s/tid:\([[:digit:]]\+\) name:${OCF_RESKEY_target_iqn}$/\1/p" < /proc/net/iet/volume`
	    if [ -z "${TID}" ]; then
		# Our target is not configured, thus we're not
		# running.
		return $OCF_NOT_RUNNING
	    fi
	    # FIXME: this looks for a matching LUN and path, but does
	    # not actually test for the correct target ID.
	    grep -E -q "[[:space:]]+lun:${OCF_RESKEY_lun}.*path:${OCF_RESKEY_path}$" /proc/net/iet/volume && return $OCF_SUCCESS
	    ;;
	tgt)
	    # Figure out and set the target ID
	    TID=`tgtadm --lld iscsi --op show --mode target \
		| sed -ne "s/^Target \([[:digit:]]\+\): ${OCF_RESKEY_target_iqn}$/\1/p"`
	    if [ -z "$TID" ]; then
		# Our target is not configured, thus we're not
		# running.
		return $OCF_NOT_RUNNING
	    fi
	    # This only looks for the backing store, but does not test
	    # for the correct target ID and LUN.
	    tgtadm --lld iscsi --op show --mode target \
		| grep -E -q "[[:space:]]+Backing store.*: ${OCF_RESKEY_path}$" && return $OCF_SUCCESS
	    ;;
	lio)
	    configfs_path="/sys/kernel/config/target/iscsi/${OCF_RESKEY_target_iqn}/tpgt_1/lun/lun_${OCF_RESKEY_lun}/${OCF_RESOURCE_INSTANCE}/udev_path"
	    [ -e ${configfs_path} ] && [ `cat ${configfs_path}` = "${OCF_RESKEY_path}" ] && return $OCF_SUCCESS
	    ;;
    esac
    
    return $OCF_NOT_RUNNING
}

iSCSILogicalUnit_validate() {
    # Do we have all required variables?
    for var in target_iqn lun path; do
	param="OCF_RESKEY_${var}"
	if [ -z "${!param}" ]; then
	    ocf_log error "Missing resource parameter \"$var\"!"
	    exit $OCF_ERR_CONFIGURED
	fi
    done

    # Is the configured implementation supported?
    case "$OCF_RESKEY_implementation" in
	"iet"|"tgt"|"lio")
	    ;;
	"")
	    # The user didn't specify an implementation, and we were
	    # unable to determine one from installed binaries (in
	    # other words: no binaries for any supported
	    # implementation could be found)
	    ocf_log error "Undefined iSCSI target implementation"
	    exit $OCF_ERR_INSTALLED
	    ;;
	*)
	    ocf_log error "Unsupported iSCSI target implementation \"$OCF_RESKEY_implementation\"!"
	    exit $OCF_ERR_CONFIGURED
	    ;;
    esac

    # Do we have a valid LUN?
    case $OCF_RESKEY_implementation in
	iet)
	    # IET allows LUN 0 and up
	    [ $OCF_RESKEY_lun -ge 0 ]
	    case $? in
		0)
	            # OK
		    ;;
		1)
		    ocf_log err "Invalid LUN $OCF_RESKEY_lun (must be a non-negative integer)."
		    exit $OCF_ERR_CONFIGURED
		    ;;
		*)
		    ocf_log err "Invalid LUN $OCF_RESKEY_lun (must be an integer)."
		    exit $OCF_ERR_CONFIGURED
		    ;;
	    esac
	    ;;
	tgt)
	    # tgt reserves LUN 0 for its own purposes
	    [ $OCF_RESKEY_lun -ge 1 ]
	    case $? in
		0)
	            # OK
		    ;;
		1)
		    ocf_log err "Invalid LUN $OCF_RESKEY_lun (must be greater than 0)."
		    exit $OCF_ERR_CONFIGURED
		    ;;
		*)
		    ocf_log err "Invalid LUN $OCF_RESKEY_lun (must be an integer)."
		    exit $OCF_ERR_CONFIGURED
		    ;;
	    esac
	    ;;
    esac

    # Do we have any configuration parameters that the current
    # implementation does not support?
    local unsupported_params
    local var
    local envar
    case $OCF_RESKEY_implementation in
	iet)
	    # IET does not support setting the vendor and product ID
	    # (it always uses "IET" and "VIRTUAL-DISK")
	    unsupported_params="vendor_id product_id allowed_initiators"
	    ;;
	tgt)
	    unsupported_params="allowed_initiators"
	    ;;
	lio)
	    unsupported_params="scsi_id vendor_id product_id"
	    ;;
    esac
    for var in ${unsupported_params}; do
	envar=OCF_RESKEY_${var}
	if [ -n "${!envar}" ]; then
	    ocf_log warn "Configuration parameter \"${var}\"" \
		"is not supported by the iSCSI implementation" \
		"and will be ignored."
	fi
    done

    if ! ocf_is_probe; then
    # Do we have all required binaries?
	case $OCF_RESKEY_implementation in
	    iet)
		check_binary ietadm
		;;
	    tgt)
		check_binary tgtadm
		;;
	    lio)
		check_binary tcm_node
		check_binary lio_node
		;;
	esac

        # Is the required kernel functionality available?
	case $OCF_RESKEY_implementation in
	    iet)
		[ -d /proc/net/iet ]
		if [ $? -ne 0 ]; then
		    ocf_log err "/proc/net/iet does not exist or is not a directory -- check if required modules are loaded."
		    exit $OCF_ERR_INSTALLED
		fi
		;;
	    tgt)
	        # tgt is userland only
		;;
	esac
    fi

    return $OCF_SUCCESS
}


case $1 in
  meta-data)
	meta_data
	exit $OCF_SUCCESS
	;;
  usage|help)
	iSCSILogicalUnit_usage
	exit $OCF_SUCCESS
	;;
esac

# Everything except usage and meta-data must pass the validate test
iSCSILogicalUnit_validate

case $__OCF_ACTION in
start)		iSCSILogicalUnit_start;;
stop)		iSCSILogicalUnit_stop;;
monitor|status)	iSCSILogicalUnit_monitor;;
reload)		ocf_log err "Reloading..."
	        iSCSILogicalUnit_start
		;;
validate-all)	;;
*)		iSCSILogicalUnit_usage
		exit $OCF_ERR_UNIMPLEMENTED
		;;
esac
rc=$?
ocf_log debug "${OCF_RESOURCE_INSTANCE} $__OCF_ACTION : $rc"
exit $rc
