0828d473b7a9ddb053c627cf7dc7361a14e2ac3a
[kai/samba-autobuild/.git] / ctdb / config / events / legacy / 70.iscsi.script
1 #!/bin/sh
2
3 # CTDB event script for TGTD based iSCSI
4
5 [ -n "$CTDB_BASE" ] || \
6     CTDB_BASE=$(d=$(dirname "$0") ; cd -P "$d" ; dirname "$PWD")
7
8 . "${CTDB_BASE}/functions"
9
10 # service_name is used by various functions
11 # shellcheck disable=SC2034
12 service_name="iscsi"
13
14 load_script_options
15
16 [ -z "$CTDB_START_ISCSI_SCRIPTS" ] && {
17         echo "No iscsi start script directory found"
18         exit 0
19 }
20
21 case "$1" in
22 ipreallocated)
23         all_ips=$($CTDB -X ip | tail -n +2)
24
25         # Block the iSCSI port.  Only block for the address families
26         # we have configured.  This copes with, for example, ip6tables
27         # being unavailable on an IPv4-only system.
28         have_ipv4=false
29         have_ipv6=false
30         # x is intentionally ignored
31         # shellcheck disable=SC2034
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         pnn=$(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 esac
86
87 exit 0