summaryrefslogtreecommitdiffstats
path: root/ctdb/config/events.d/70.iscsi
blob: 42d261b8f022b98c81ac6c5cd60d65097005e2aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/sh

# CTDB event script for TGTD based iSCSI

[ -n "$CTDB_BASE" ] || \
    export CTDB_BASE=$(cd -P $(dirname "$0") ; 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 "$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

	# What node is this?
	this_node=$(ctdb xpnn | sed -e 's@PNN:@@')
	[ -n "$this_node" ] || die "Failed to get node pnn"

	# Start iSCSI daemon
	tgtd >/dev/null 2>&1

	# Run a script for each currently hosted public IP address
	ips=$(echo "$all_ips" | awk -F'|' -v pnn=$this_node '$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

	# 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 daemon when ctdb goes down
	killall -9 tgtd >/dev/null 2>&1
	;;

    monitor)
	ctdb_check_tcp_ports 3260 || exit $?
	;;

    *)
	ctdb_standard_event_handler "$@"
	;;
esac

exit 0