# HG changeset patch
# User Florian Haas <[email protected]>
# Date 1245084482 -7200
# Node ID f8bfb4f8340cf3889dc4b378c772373388fd3147
# Parent  73e18422f657c37fb2f3585c132d801c20dc90ab
Improvements to the iSCSITarget and iSCSILogicalUnit RAs.

Add a generic command wrapper to log command, their output, and their
exit code (stolen from lmb's original drbd OCF RA).

Set a system-dependent default implementation based on the
availability of administration utilities.

Implement forced connection shutdown on target stop (many thanks to
Lars Ellenberg for the sed wizardry).

diff -r 73e18422f657 -r f8bfb4f8340c resources/OCF/iSCSILogicalUnit
--- a/resources/OCF/iSCSILogicalUnit    Mon Jun 15 11:02:33 2009 +0200
+++ b/resources/OCF/iSCSILogicalUnit    Mon Jun 15 18:48:02 2009 +0200
@@ -33,6 +33,11 @@
 LANG="C"
 
 # Defaults
+# Set a default implementation based on software installed
+if have_binary ietadm; then
+    OCF_RESKEY_implementation_default="iet"
+fi
+: ${OCF_RESKEY_implementation=${OCF_RESKEY_implementation_default}}
 #######################################################################
 
 meta_data() {
@@ -49,13 +54,15 @@
 <shortdesc lang="en">iSCSI target export agent</shortdesc>
 
 <parameters>
-<parameter name="implementation" required="1" unique="0">
+<parameter name="implementation" required="0" unique="0">
 <longdesc lang="en">
 The iSCSI target daemon implementation. Must be one of "iet", "tgt",
-or "lio".
+or "lio". If unspecified, an implementation is selected based on the
+availability of management utilities, with "iet" being tried first,
+them "tgt", then "lio".
 </longdesc>
 <shortdesc lang="en">iSCSI target daemon implementation</shortdesc>
-<content type="string" />
+<content type="string" default="${OCF_RESKEY_implementation_default}"/>
 </parameter>
 
 <parameter name="tid" required="1" unique="0">
@@ -98,6 +105,31 @@
 
 #######################################################################
 
+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
+}
+
+
 iSCSILogicalUnit_usage() {
        cat <<END
 usage: $0 {start|stop|monitor|validate-all|meta-data}
@@ -114,7 +146,7 @@
 
     case $OCF_RESKEY_implementation in
        iet)
-           ietadm --op new \
+           do_cmd ietadm --op new \
                --tid=${OCF_RESKEY_tid} \
                --lun=${OCF_RESKEY_lun} \
                --params Path=${OCF_RESKEY_path} && return $OCF_SUCCESS
@@ -128,7 +160,7 @@
     if [ $? =  $OCF_SUCCESS ]; then
        case $OCF_RESKEY_implementation in
            iet)
-               ietadm --op delete \
+               do_cmd ietadm --op delete \
                    --tid=${OCF_RESKEY_tid} \
                    --lun=${OCF_RESKEY_lun} && return $OCF_SUCCESS
                ;;
diff -r 73e18422f657 -r f8bfb4f8340c resources/OCF/iSCSITarget
--- a/resources/OCF/iSCSITarget Mon Jun 15 11:02:33 2009 +0200
+++ b/resources/OCF/iSCSITarget Mon Jun 15 18:48:02 2009 +0200
@@ -33,6 +33,11 @@
 LANG="C"
 
 # Defaults
+# Set a default implementation based on software installed
+if have_binary ietadm; then
+    OCF_RESKEY_implementation_default="iet"
+fi
+: ${OCF_RESKEY_implementation=${OCF_RESKEY_implementation_default}}
 #######################################################################
 
 meta_data() {
@@ -49,13 +54,15 @@
 <shortdesc lang="en">iSCSI target export agent</shortdesc>
 
 <parameters>
-<parameter name="implementation" required="1" unique="0">
+<parameter name="implementation" required="0" unique="0">
 <longdesc lang="en">
 The iSCSI target daemon implementation. Must be one of "iet", "tgt",
-or "lio".
+or "lio". If unspecified, an implementation is selected based on the
+availability of management utilities, with "iet" being tried first,
+them "tgt", then "lio".
 </longdesc>
 <shortdesc lang="en">iSCSI target daemon implementation</shortdesc>
-<content type="string" />
+<content type="string" default="${OCF_RESKEY_implementation_default}"/>
 </parameter>
 
 <parameter name="tid" required="1" unique="0">
@@ -100,6 +107,31 @@
 
 #######################################################################
 
+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 <<END
 usage: $0 {start|stop|monitor|validate-all|meta-data}
@@ -120,13 +152,13 @@
 
     case $OCF_RESKEY_implementation in
        iet)
-           ietadm --op new \
+           do_cmd ietadm --op new \
                --tid=${OCF_RESKEY_tid} \
                --params Name=${OCF_RESKEY_name} || return $OCF_ERR_GENERIC
            for param in ${OCF_RESKEY_params}; do
                name=${param%=*}
                value=${param#*=}
-               ietadm --op update \
+               do_cmd ietadm --op update \
                    --tid=${OCF_RESKEY_tid} \
                    --params ${name}=${value} || return $OCF_ERR_GENERIC
            done
@@ -141,7 +173,24 @@
     if [ $? =  $OCF_SUCCESS ]; then
        case $OCF_RESKEY_implementation in
            iet)
-               ietadm --op delete \
+               # Close existing connections. There is no other way to
+               # do this in IET than to parse the contents of
+               # /proc/net/iet/session.
+               local line
+               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; 
s/\n/ /;p; 
+                          }; 
+                      }' < /proc/net/iet/session \
+                   | while read line; do
+                        # $line looks like "--sid=X --cid=Y"
+                        do_cmd ietadm --op delete \
+                            --tid ${OCF_RESKEY_tid} $line
+               done
+               do_cmd ietadm --op delete \
                    --tid=${OCF_RESKEY_tid} && return $OCF_SUCCESS
                ;;
        esac
_______________________________________________________
Linux-HA-Dev: [email protected]
http://lists.linux-ha.org/mailman/listinfo/linux-ha-dev
Home Page: http://linux-ha.org/

Reply via email to