ctdb-scripts: Changed uses of "ctdb xpnn" to ctdb_get_pnn()
[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         ctdb_get_pnn
51         [ -n "$pnn" ] || die "Failed to get node pnn"
52
53         # Start iSCSI daemon
54         tgtd >/dev/null 2>&1
55
56         # Run a script for each currently hosted public IP address
57         ips=$(echo "$all_ips" | awk -F'|' -v pnn=$pnn '$3 == pnn {print $2}')
58         for ip in $ips ; do
59             script="${CTDB_START_ISCSI_SCRIPTS}/${ip}.sh"
60             if [ -x "$script" ] ; then
61                 echo "Starting iSCSI service for public address ${ip}"
62                 "$script"
63             fi
64         done
65
66         # Unblock iSCSI port.  These can be unconditional (compared to
67         # blocking above), since errors are redirected.
68         while iptables -D INPUT -p tcp --dport 3260 -j DROP >/dev/null 2>&1 ; do
69             :
70         done
71         while ip6tables -D INPUT -p tcp --dport 3260 -j DROP >/dev/null 2>&1 ; do
72             :
73         done
74
75         ;;
76
77     shutdown)
78         # Shutdown iSCSI daemon when ctdb goes down
79         killall -9 tgtd >/dev/null 2>&1
80         ;;
81
82     monitor)
83         ctdb_check_tcp_ports 3260 || exit $?
84         ;;
85
86     *)
87         ctdb_standard_event_handler "$@"
88         ;;
89 esac
90
91 exit 0