dsdb: Honour LDB_FLG_NOSYNC for metadata.tdb
[samba.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     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 [ "$CTDB_MANAGES_ISCSI" = "yes" ] || 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         # x is intentionally ignored
33         # shellcheck disable=SC2034
34         while IFS='|' read x ip pnn x ; do
35             case "$ip" in
36                 *:*) have_ipv6=true ;;
37                 *)   have_ipv4=true ;;
38                 esac
39         done <<EOF
40 $all_ips
41 EOF
42         if $have_ipv4 ; then
43             iptables -I INPUT 1 -p tcp --dport 3260 -j DROP
44         fi
45         if $have_ipv6 ; then
46             ip6tables -I INPUT 1 -p tcp --dport 3260 -j DROP
47         fi
48
49         # Stop iSCSI daemon
50         killall -9 tgtd >/dev/null 2>/dev/null
51
52         pnn=$(ctdb_get_pnn)
53         [ -n "$pnn" ] || die "Failed to get node pnn"
54
55         # Start iSCSI daemon
56         tgtd >/dev/null 2>&1
57
58         # Run a script for each currently hosted public IP address
59         ips=$(echo "$all_ips" | awk -F'|' -v pnn="$pnn" '$3 == pnn {print $2}')
60         for ip in $ips ; do
61             script="${CTDB_START_ISCSI_SCRIPTS}/${ip}.sh"
62             if [ -x "$script" ] ; then
63                 echo "Starting iSCSI service for public address ${ip}"
64                 "$script"
65             fi
66         done
67
68         # Unblock iSCSI port.  These can be unconditional (compared to
69         # blocking above), since errors are redirected.
70         while iptables -D INPUT -p tcp --dport 3260 -j DROP >/dev/null 2>&1 ; do
71             :
72         done
73         while ip6tables -D INPUT -p tcp --dport 3260 -j DROP >/dev/null 2>&1 ; do
74             :
75         done
76
77         ;;
78
79 shutdown)
80         # Shutdown iSCSI daemon when ctdb goes down
81         killall -9 tgtd >/dev/null 2>&1
82         ;;
83
84 monitor)
85         ctdb_check_tcp_ports 3260 || exit $?
86         ;;
87 esac
88
89 exit 0