diff --git a/heartbeat/iSCSITarget b/heartbeat/iSCSITarget index e5eb7af45..323fe3744 100644 --- a/heartbeat/iSCSITarget +++ b/heartbeat/iSCSITarget @@ -1,342 +1,381 @@ #!/bin/bash # # # iSCSITarget OCF RA. Exports and manages iSCSI targets. # # Copyright (c) 2009 LINBIT HA-Solutions GmbH, Florian Haas # All Rights Reserved. # # 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_ROOT}/resource.d/heartbeat/.ocf-shellfuncs LC_ALL="C" LANG="C" # 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" fi : ${OCF_RESKEY_implementation=${OCF_RESKEY_implementation_default}} ####################################################################### meta_data() { cat < 0.9 Manages iSCSI targets. An iSCSI target is a collection of SCSI Logical Units (LUs) exported via a daemon that speaks the iSCSI protocol. iSCSI target export agent The iSCSI target daemon implementation. Must be one of "iet" or "tgt". If unspecified, an implementation is selected based on the availability of management utilities, with "iet" being tried first, then "tgt". iSCSI target daemon implementation The numeric target ID. Must not be zero. iSCSI target ID The logical target name. Should follow the conventional "iqn.yyyy-mm.<reversed domain name>[:identifier]" syntax. iSCSI target name Target 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. List of iSCSI target parameters + + +Allowed initiators. A space-separated list of initiators allowed to +connect to this target. Initiators may be listed in any syntax +the target implementation allows. If this parameter is empty or +not set, access to this target will be allowed from any initiator. + +List of iSCSI initiators allowed to connect +to this target + + + END } ####################################################################### do_cmd() { # Wrap local commands to capture their exit code and output. Some # implementations (IET, notably) have management commands with # very terse output. It helps to at least capture exit codes in # the logs. local cmd="$*" ocf_log debug "Calling $cmd" local cmd_out cmd_out=$($cmd 2>&1) ret=$? if [ $ret -ne 0 ]; then ocf_log err "Called \"$cmd\"" ocf_log err "Exit code $ret" ocf_log err "Command output: \"$cmd_out\"" else ocf_log debug "Exit code $ret" ocf_log debug "Command output: \"$cmd_out\"" fi echo $cmd_out return $ret } iSCSITarget_usage() { cat <> /etc/initiators.deny + echo "${OCF_RESKEY_name} ${OCF_RESKEY_initiators// /,}" >> /etc/initiators.allow + fi return $OCF_SUCCESS ;; tgt) do_cmd tgtadm --lld iscsi --op new --mode target \ --tid=${OCF_RESKEY_tid} \ --targetname ${OCF_RESKEY_name} || return $OCF_ERR_GENERIC for param in ${OCF_RESKEY_params}; do name=${param%=*} value=${param#*=} do_cmd tgtadm --lld iscsi --op update --mode target \ --tid=${OCF_RESKEY_tid} \ --name=${name} --value=${value} || return $OCF_ERR_GENERIC done - do_cmd tgtadm --lld iscsi --op bind --mode target \ - --tid=${OCF_RESKEY_tid} \ - --initiator-address=ALL && return $OCF_SUCCESS + # For tgt, we always have to add access per initiator; + # access to targets is denied by default. If "initiators" + # is unset, we must use the special keyword ALL. + for initiator in ${OCF_RESKEY_initiators=ALL}; do + do_cmd tgtadm --lld iscsi --op bind --mode target \ + --tid=${OCF_RESKEY_tid} \ + --initiator-address=${initiator} || return $OCF_ERR_GENERIC + done + return $OCF_SUCCESS ;; esac return $OCF_ERR_GENERIC } iSCSITarget_stop() { iSCSITarget_monitor if [ $? = $OCF_SUCCESS ]; then case $OCF_RESKEY_implementation in iet) # Close existing connections. There is no other way to # do this in IET than to parse the contents of # /proc/net/iet/session. set -- $(sed -ne '/^tid:'${OCF_RESKEY_tid}' /,/^tid/ { /^[[:space:]]*sid:\([0-9]\+\)/ { s/^[[:space:]]*sid:\([0-9]*\).*/--sid=\1/; h; }; /^[[:space:]]*cid:\([0-9]\+\)/ { s/^[[:space:]]*cid:\([0-9]*\).*/--cid=\1/; G; p; }; }' < /proc/net/iet/session) while [[ -n $2 ]]; do # $2 $1 looks like "--sid=X --cid=Y" do_cmd ietadm --op delete \ --tid=${OCF_RESKEY_tid} $2 $1 shift 2 done do_cmd ietadm --op delete \ - --tid=${OCF_RESKEY_tid} && return $OCF_SUCCESS + --tid=${OCF_RESKEY_tid} || return $OCF_ERR_GENERIC + if [ -n ${OCF_RESKEY_initiators} ]; then + # Avoid stale /etc/initiators.{allow,deny} entries + # for this target + do_cmd sed -e "/^${OCF_RESKEY_name}[[:space:]]/d" \ + -i /etc/initiators.deny + do_cmd sed -e "/^${OCF_RESKEY_name}[[:space:]]/d" \ + -i /etc/initiators.allow + fi + return $OCF_SUCCESS ;; tgt) # Close existing connections. There is no other way to # do this in tgt than to parse the output of "tgtadm --op # show". set -- $(tgtadm --lld iscsi --op show --mode target \ | sed -ne '/^Target '${OCF_RESKEY_tid}':/,/^Target/ { /^[[:space:]]*I_T nexus: \([0-9]\+\)/ { s/^.*: \([0-9]*\).*/--sid=\1/; h; }; /^[[:space:]]*Connection: \([0-9]\+\)/ { s/^.*: \([0-9]*\).*/--cid=\1/; G; p; }; /^[[:space:]]*LUN information:/ q; }') while [[ -n $2 ]]; do # $2 $1 looks like "--sid=X --cid=Y" do_cmd tgtadm --lld iscsi --op delete --mode connection \ --tid=${OCF_RESKEY_tid} $2 $1 shift 2 done + # In tgt, we don't have to worry about our ACL + # entries. They are automatically removed upon target + # deletion. do_cmd tgtadm --lld iscsi --op delete --mode target \ --tid=${OCF_RESKEY_tid} && return $OCF_SUCCESS ;; esac else return $OCF_SUCCESS fi return $OCF_ERR_GENERIC } iSCSITarget_monitor() { case $OCF_RESKEY_implementation in iet) grep -q "tid:${OCF_RESKEY_tid} name:${OCF_RESKEY_name}" /proc/net/iet/volume && return $OCF_SUCCESS ;; tgt) do_cmd tgtadm --lld iscsi --op show --mode target \ | grep -q "Target ${OCF_RESKEY_tid}: ${OCF_RESKEY_name}" && return $OCF_SUCCESS ;; esac return $OCF_NOT_RUNNING } iSCSITarget_validate() { # Do we have all required variables? for var in implementation tid name; do param="OCF_RESKEY_${var}" if [ -z "${!param}" ]; then ocf_log error "Missing resource parameter \"$var\"!" return $OCF_ERR_CONFIGURED fi done # Do we have all required binaries? case $OCF_RESKEY_implementation in iet) check_binary ietadm || return $OCF_ERR_INSTALLED ;; tgt) check_binary tgtadm || return $OCF_ERR_INSTALLED ;; *) # and by the way, is the implementation supported? ocf_log error "Unsupported iSCSI target implementation \"$OCF_RESKEY_implementation\"!" return $OCF_ERR_CONFIGURED esac # Do we have a valid target ID? [ $OCF_RESKEY_tid -ge 1 ] case $? in 0) # OK ;; 1) ocf_log err "Invalid target ID $OCF_RESKEY_tid (must be greater than 0)." return $OCF_ERR_CONFIGURED ;; *) ocf_log err "Invalid target ID $OCF_RESKEY_tid (must be an integer)." return $OCF_ERR_CONFIGURED 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." return $OCF_ERR_INSTALLED fi ;; tgt) # tgt is userland only ;; esac return $OCF_SUCCESS } case $1 in meta-data) meta_data exit $OCF_SUCCESS ;; usage|help) iSCSITarget_usage exit $OCF_SUCCESS ;; esac # Everything except usage and meta-data must pass the validate test iSCSITarget_validate || exit $? case $__OCF_ACTION in start) iSCSITarget_start;; stop) iSCSITarget_stop;; monitor) iSCSITarget_monitor;; reload) ocf_log err "Reloading..." iSCSITarget_start ;; validate-all) ;; *) iSCSITarget_usage exit $OCF_ERR_UNIMPLEMENTED ;; esac rc=$? ocf_log debug "${OCF_RESOURCE_INSTANCE} $__OCF_ACTION : $rc" exit $rc