#!/bin/bash
#
#
#   iSCSILogicalUnit OCF RA. Exports and manages iSCSI Logical Units.
#
#   (c) 2013 LINBIT, Lars Ellenberg
#   (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"
elif have_binary targetcli; then
	OCF_RESKEY_implementation_default="lio-t"
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}" | md5sum | sed -e 's/ .*//'`
OCF_RESKEY_scsi_sn_default=${sn:0:8}
: ${OCF_RESKEY_scsi_sn=${OCF_RESKEY_scsi_sn_default}}
# set 0 as a default value for lio iblock device number
OCF_RESKEY_lio_iblock_default=0
OCF_RESKEY_lio_iblock=${OCF_RESKEY_lio_iblock:-$OCF_RESKEY_lio_iblock_default}

## tgt specifics
# tgt has "backing store type" and "backing store open flags",
# as well as device-type.
#
# suggestions how to make this generic accross all supported implementations?
# how should they be named, how should they be mapped to implementation specifics?
#
# OCF_RESKEY_tgt_bstype
# OCF_RESKEY_tgt_bsoflags
# OCF_RESKEY_tgt_bsopts
# OCF_RESKEY_tgt_device_type

# targetcli: iSCSITarget and iSCSILogicalUnit must use the same lockfile
TARGETLOCKFILE=${HA_RSCTMP}/targetcli.lock
#######################################################################

meta_data() {
	cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="iSCSILogicalUnit">
<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",
"lio", or "lio-t".  If unspecified, an implementation is selected based on the
availability of management utilities, with "iet" being tried first,
then "tgt", then "lio", then "lio-t".
</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="emulate_tpu" required="0" unique="0">
<longdesc lang="en">
The SCSI UNMAP command to be configured for this Logical Unit.
Setting this integer to 1 will enable TPU IOCTL emulation.
</longdesc>
<shortdesc lang="en">SCSI UNMAP (for TRIM / DISCARD)</shortdesc>
<content type="integer" />
</parameter>

<parameter name="emulate_3pc" required="0" unique="0">
<longdesc lang="en">
The SCSI EXTENDED COPY command to be configured for this Logical Unit.
Setting this integer to 1 will enable 3PC IOCTL emulation.
</longdesc>
<shortdesc lang="en">SCSI extended write</shortdesc>
<content type="integer" />
</parameter>

<parameter name="emulate_caw" required="0" unique="0">
<longdesc lang="en">
The SCSI Compare and Write command to be configured for this Logical Unit.
Setting this integer to 1 will enable CAW IOCTL emulation.
</longdesc>
<shortdesc lang="en">SCSI compare and write</shortdesc>
<content type="integer" />
</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="tgt_bstype" required="0" unique="0">
<longdesc lang="en">
TGT specific backing store type. If you want to use aio,
make sure your tgtadm is built against libaio.
See tgtadm(8).
</longdesc>
<shortdesc lang="en">TGT backing store type</shortdesc>
<content type="string" />
</parameter>

<parameter name="tgt_bsoflags" required="0" unique="0">
<longdesc lang="en">
TGT specific backing store open flags (direct|sync).
See tgtadm(8).
</longdesc>
<shortdesc lang="en">TGT backing store open flags</shortdesc>
<content type="string" />
</parameter>

<parameter name="tgt_bsopts" required="0" unique="0">
<longdesc lang="en">
TGT specific backing store options.
See tgtadm(8).
</longdesc>
<shortdesc lang="en">TGT backing store options</shortdesc>
<content type="string" />
</parameter>

<parameter name="tgt_device_type" required="0" unique="0">
<longdesc lang="en">
TGT specific device type.
See tgtadm(8).
</longdesc>
<shortdesc lang="en">TGT device type</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 im                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   