ctdb-scripts: Error message, comment and whitespace cleanups
[nivanova/samba-autobuild/.git] / ctdb / config / events.d / 70.iscsi
1 #!/bin/sh
2
3 # CTDB event script for TGTD based iSCSI
4
5 [ -n "$CTDB_BASE" ] || \
6     export CTDB_BASE=$(cd -P $(dirname "$0") ; dirname "$PWD")
7
8 . $CTDB_BASE/functions
9
10 service_name="iscsi"
11
12 loadconfig
13
14 ctdb_start_stop_service
15
16 is_ctdb_managed_service || exit 0
17
18 [ -z "$CTDB_START_ISCSI_SCRIPTS" ] && {
19         echo "No iscsi start script directory found"
20         exit 0
21 }
22
23 case "$1" in
24     ipreallocated)
25         all_ips=$(ctdb -X ip | tail -n +2)
26
27         # Block the iSCSI port.  Only block for the address families
28         # we have configured.  This copes with, for example, ip6tables
29         # being unavailable on an IPv4-only system.
30         have_ipv4=false
31         have_ipv6=false
32         while IFS='|' read x ip pnn x ; do
33             case "$ip" in
34                 *:*) have_ipv6=true ;;
35                 *)   have_ipv4=true ;;
36                 esac
37         done <<EOF
38 $all_ips
39 EOF
40         if $have_ipv4 ; then
41             iptables -I INPUT 1 -p tcp --dport 3260 -j DROP
42         fi
43         if $have_ipv6 ; then
44             ip6tables -I INPUT 1 -p tcp --dport 3260 -j DROP
45         fi
46
47         # Stop iSCSI daemon
48         killall -9 tgtd >/dev/null 2>/dev/null
49
50         # What node is this?
51         this_node=$(ctdb xpnn | sed -e 's@PNN:@@')
52         [ -n "$this_node" ] || die "Failed to get node pnn"
53
54         # Start iSCSI daemon
55         tgtd >/dev/null 2>&1
56
57         # Run a script for each currently hosted public IP address
58         ips=$(echo "$all_ips" | awk -F'|' -v pnn=$this_node '$3 == pnn {print $2}')
59         for ip in $ips ; do
60             script="${CTDB_START_ISCSI_SCRIPTS}/${ip}.sh"
61             if [ -x "$script" ] ; then
62                 echo "Starting iSCSI service for public address ${ip}"
63                 "$script"
64             fi
65         done
66
67         # Unblock iSCSI port.  These can be unconditional (compared to
68         # blocking above), since errors are redirected.
69         while iptables -D INPUT -p tcp --dport 3260 -j DROP >/dev/null 2>&1 ; do
70             :
71         done
72         while ip6tables -D INPUT -p tcp --dport 3260 -j DROP >/dev/null 2>&1 ; do
73             :
74         done
75
76         ;;
77
78     shutdown)
79         # Shutdown iSCSI daemon when ctdb goes down
80         killall -9 tgtd >/dev/null 2>&1
81         ;;
82
83     monitor)
84         ctdb_check_tcp_ports 3260 || exit $?
85         ;;
86
87     *)
88         ctdb_standard_event_handler "$@"
89         ;;
90 esac
91
92 exit 0