ctdb-scripts: Quote some variable expansions
[nivanova/samba-autobuild/.git] / ctdb / config / events.d / 70.iscsi
index 8cbf457eb4eb446420e9c5ed8c5e31a28561e8b3..2d9e4a64fa18c053c9acdcc9077f65a52306de2f 100755 (executable)
@@ -1,61 +1,91 @@
 #!/bin/sh
-# ctdb event script for TGTD based iSCSI
 
-. $CTDB_BASE/functions
+# CTDB event script for TGTD based iSCSI
+
+[ -n "$CTDB_BASE" ] || \
+    CTDB_BASE=$(d=$(dirname "$0") ; cd -P "$d" ; dirname "$PWD")
+
+. "${CTDB_BASE}/functions"
 
 service_name="iscsi"
 
+loadconfig
+
 ctdb_start_stop_service
 
+is_ctdb_managed_service || exit 0
+
 [ -z "$CTDB_START_ISCSI_SCRIPTS" ] && {
        echo "No iscsi start script directory found"
        exit 0
 }
 
-case $cmd in 
-     recovered)
-       # block the iscsi port
-       iptables -I INPUT 1 -p tcp --dport 3260 -j DROP
-       
-       # shut down the iscsi service
+case "$1" in
+    ipreallocated)
+       all_ips=$($CTDB -X ip | tail -n +2)
+
+       # Block the iSCSI port.  Only block for the address families
+       # we have configured.  This copes with, for example, ip6tables
+       # being unavailable on an IPv4-only system.
+       have_ipv4=false
+       have_ipv6=false
+       while IFS='|' read x ip pnn x ; do
+           case "$ip" in
+               *:*) have_ipv6=true ;;
+               *)   have_ipv4=true ;;
+               esac
+       done <<EOF
+$all_ips
+EOF
+       if $have_ipv4 ; then
+           iptables -I INPUT 1 -p tcp --dport 3260 -j DROP
+       fi
+       if $have_ipv6 ; then
+           ip6tables -I INPUT 1 -p tcp --dport 3260 -j DROP
+       fi
+
+       # Stop iSCSI daemon
        killall -9 tgtd >/dev/null 2>/dev/null
 
-       THIS_NODE=`ctdb status | grep "THIS NODE" | sed -e "s/pnn://" -e "s/ .*//"`
-       [ -z $THIS_NODE ] && {
-               echo "70.iscsi: Failed to get node pnn"
-               exit 0
-       }
+       ctdb_get_pnn
+       [ -n "$pnn" ] || die "Failed to get node pnn"
 
-       # start the iscsi daemon
-       tgtd >/dev/null 2>/dev/null
+       # Start iSCSI daemon
+       tgtd >/dev/null 2>&1
 
-       for NODE in `ctdb ip | grep -v "Public" | egrep " ${THIS_NODE}$" | sed -e "s/ .*//"`; do
-               [ -f $CTDB_START_ISCSI_SCRIPTS/${NODE}.sh ] && {
-                       echo Starting iscsi service for public address $NODE
-                       $CTDB_START_ISCSI_SCRIPTS/${NODE}.sh
-               }
+       # Run a script for each currently hosted public IP address
+       ips=$(echo "$all_ips" | awk -F'|' -v pnn="$pnn" '$3 == pnn {print $2}')
+       for ip in $ips ; do
+           script="${CTDB_START_ISCSI_SCRIPTS}/${ip}.sh"
+           if [ -x "$script" ] ; then
+               echo "Starting iSCSI service for public address ${ip}"
+               "$script"
+           fi
        done
 
-       # remove all iptables rules
-       while iptables -D INPUT -p tcp --dport 3260 -j DROP 2>/dev/null >/dev/null ;  do
+       # Unblock iSCSI port.  These can be unconditional (compared to
+       # blocking above), since errors are redirected.
+       while iptables -D INPUT -p tcp --dport 3260 -j DROP >/dev/null 2>&1 ; do
+           :
+       done
+       while ip6tables -D INPUT -p tcp --dport 3260 -j DROP >/dev/null 2>&1 ; do
            :
        done
 
        ;;
 
-     shutdown)
-       # shutdown iscsi when ctdb goes down
-       killall -9 tgtd >/dev/null 2>/dev/null
+    shutdown)
+       # Shutdown iSCSI daemon when ctdb goes down
+       killall -9 tgtd >/dev/null 2>&1
        ;;
 
-     monitor)
+    monitor)
        ctdb_check_tcp_ports 3260 || exit $?
        ;;
 
-    status)
-       ctdb_checkstatus || exit $?
+    *)
+       ctdb_standard_event_handler "$@"
        ;;
 esac
 
-# ignore unknown commands
 exit 0