Fix some more bashisms
[metze/ctdb/wip.git] / config / events.d / 13.per_ip_routing
1 #!/bin/sh
2
3 . $CTDB_BASE/functions
4 loadconfig
5
6 [ -z "$CTDB_PER_IP_ROUTING_STATE" ] && {
7         CTDB_PER_IP_ROUTING_STATE="$CTDB_BASE/state/per_ip_routing"
8 }
9
10 AUTO_LINK_LOCAL="no"
11
12 case "$CTDB_PER_IP_ROUTING_CONF" in
13         __auto_link_local__)
14                 AUTO_LINK_LOCAL="yes"
15                 CTDB_PER_IP_ROUTING_CONF="$CTDB_PER_IP_ROUTING_STATE/auto_link_local.conf"
16                 ;;
17         *)
18                 [ -z "$CTDB_PER_IP_ROUTING_CONF" ] && {
19                         #echo "No config file found. Nothing to do for 13.per_ip_routing"
20                         exit 0;
21                 }
22                 ;;
23 esac
24
25 _low=$CTDB_PER_IP_ROUTING_TABLE_ID_LOW
26 _high=$CTDB_PER_IP_ROUTING_TABLE_ID_HIGH
27
28 test -z "$_low" && {
29         echo "$0: CTDB_PER_IP_ROUTING_TABLE_ID_LOW not configured";
30         exit 1;
31 }
32 test -z "$_high" && {
33         echo "$0: CTDB_PER_IP_ROUTING_TABLE_ID_HIGH not configured";
34         exit 1;
35 }
36 test "$_low" -ge "$_high" && {
37         echo "$0: CTDB_PER_IP_ROUTING_TABLE_ID_LOW[$_low] needs to be below CTDB_PER_IP_ROUTING_TABLE_ID_HIGH[$_high]";
38         exit 1;
39 }
40
41 test -z "$CTDB_PER_IP_ROUTING_RULE_PREF" && {
42         echo "$0: CTDB_PER_IP_ROUTING_RULE_PREF not configured";
43         exit 1;
44 }
45
46 locknesting=0
47 lock_root="$CTDB_PER_IP_ROUTING_STATE"
48 host=`hostname`
49
50 lock_debug()
51 {
52         echo -n ""
53 }
54
55 ############################
56 # grab a lock file. Not atomic, but close :)
57 # tries to cope with NFS
58 lock_file() {
59         if [ -z "$lock_root" ]; then
60                 lock_root=`pwd`;
61         fi
62         lckf="$lock_root/$1"
63         machine=`cat "$lckf" 2> /dev/null | cut -d: -f1`
64         pid=`cat "$lckf" 2> /dev/null | cut -d: -f2`
65
66         if [ "$pid" = "$$" ]; then
67                 locknesting=`expr $locknesting + 1`
68                 lock_debug "lock nesting now $locknesting"
69                 return 0
70         fi
71
72         if test -f "$lckf"; then
73                 test $machine = $host || {
74                         lock_debug "lock file $lckf is valid for other machine $machine"
75                         stat -c%y "$lckf"
76                         return 1
77                 }
78                 /bin/kill -0 $pid && {
79                         lock_debug "lock file $lckf is valid for process $pid"
80                         stat -c%y "$lckf"
81                         return 1
82                 }
83                 lock_debug "stale lock file $lckf for $machine:$pid"
84                 cat "$lckf"
85                 /bin/rm -f "$lckf"
86         fi
87         echo "$host:$$" > "$lckf"
88         return 0
89 }
90
91 ############################
92 # unlock a lock file
93 unlock_file() {
94         if [ -z "$lock_root" ]; then
95                 lock_root=`pwd`;
96         fi
97         if [ "$locknesting" != "0" ]; then
98                 locknesting=`expr $locknesting - 1`
99                 lock_debug "lock nesting now $locknesting"
100         else
101                 lckf="$lock_root/$1"
102                 /bin/rm -f "$lckf"
103         fi
104 }
105
106 generate_table_id () {
107         local _ip=$1
108         local _ipsdir="$CTDB_PER_IP_ROUTING_STATE/ips"
109         local _ipdir="$_ipsdir/$_ip"
110
111         mkdir -p $_ipdir
112
113         #echo "generate_table_id $_ip"
114
115         local _id=`cat $_ipdir/table_id 2>/dev/null| xargs`
116         test -n "$_id" && {
117                 #echo "IP: $_ip => OLD TABLE: $_id"
118                 table_id=$_id
119                 return 0;
120         }
121
122         local _low="$CTDB_PER_IP_ROUTING_TABLE_ID_LOW"
123         local _high="$CTDB_PER_IP_ROUTING_TABLE_ID_HIGH"
124
125         local _newid=""
126         for _id in `seq $_low $_high | xargs`; do
127                 local _table_lck="table_id_$_id.lock"
128                 lock_file $_table_lck 2>/dev/null || {
129                         continue;
130                 }
131                 local _taken=`grep "^$_id$" $_ipsdir/*/table_id 2>/dev/null| wc -l | xargs`
132                 test x"$_taken" != x"0" && {
133                         unlock_file $_table_lck
134                         #echo "tableid: $_id taken"
135                         continue
136                 }
137                 _newid=$_id;
138                 echo "$_newid" > $_ipdir/table_id
139                 unlock_file $_table_lck
140                 break;
141         done
142
143         test -z "$_newid" && {
144                 echo "generate_table_id: out of table ids: $_low - $_high"
145                 exit 1;
146         }
147
148         #echo "IP: $_ip => NEW TABLE: $_newid"
149         table_id=$_newid
150         return 0;
151 }
152
153 run_release_script_once()
154 {
155         local _script=$1
156
157         #echo "run_release_script_once[$_script]"
158
159         test -x "$_script" && {
160                 #echo "run it: start"
161                 $_script || {
162                         echo "release_script: $_script - failed $?"
163                         return $?;
164                 }
165                 #echo "run it: end"
166         }
167
168         echo '#!/bin/sh' > $_script
169         echo '#' >> $_script
170         echo >> $_script
171
172         chmod +x $_script
173
174         return 0;
175 }
176
177 generate_auto_link_local()
178 {
179         local _ip=$1
180         local _maskbits=$2
181
182         #echo "generate_auto_link_local $_ip $_maskbits"
183
184         local _netip=`ipv4_host_addr_to_net_addr $_ip $_maskbits`
185
186         local _line="$_ip $_netip/$_maskbits"
187
188         local _config=`cat $CTDB_PER_IP_ROUTING_CONF 2>/dev/null`
189
190         local _exact=`echo -n "$_config" | grep "^$line$" | wc -l | xargs`
191
192         test x"$_exact" = x"1" && {
193                 return 0;
194         }
195
196         local _tmp="$CTDB_PER_IP_ROUTING_CONF.$$.tmp"
197         echo -n "$_config" | grep -v "^$_ip " > $_tmp
198         echo "$_line" >> $_tmp
199
200         mv $_tmp $CTDB_PER_IP_ROUTING_CONF
201 }
202
203 generate_per_ip_routing()
204 {
205         local _ip=$1
206         local _maskbits=$2
207         local _iface=$3
208         local _readonly=$4
209         local _ipdir="$CTDB_PER_IP_ROUTING_STATE/ips/$_ip"
210
211         table_id=""
212         release_script="$_ipdir/per_ip_routing_release.sh"
213         setup_script="$_ipdir/per_ip_routing_setup.sh"
214
215         test x"$_readonly" = x"yes" && {
216                 test -d $_ipdir || {
217                         return 1;
218                 }
219                 return 0;
220         }
221
222         mkdir -p $_ipdir || {
223                 echo "mkdir -p $_ipdir failed"
224                 return 1;
225         }
226         echo "$_ip" > $_ipdir/ip
227
228         generate_table_id $_ip
229
230         test x"$AUTO_LINK_LOCAL" = x"yes" && {
231                 generate_auto_link_local $_ip $_maskbits
232         }
233
234         run_release_script_once $release_script
235
236         echo '#!/bin/sh' > $setup_script
237         echo '#' >> $setup_script
238         echo >> $setup_script
239         chmod +x $setup_script
240
241         return 0;
242 }
243
244 setup_per_ip_routing()
245 {
246         local _ip=$1
247         local _iface=$2
248         local _table_id=$3
249         local _release_script=$4
250         local _setup_script=$5
251
252         local _config=`cat $CTDB_PER_IP_ROUTING_CONF`
253         local _lines=`echo -n "$_config" | grep -n "^$_ip " | cut -d ':' -f1 | xargs`
254
255         local _pref="$CTDB_PER_IP_ROUTING_RULE_PREF"
256
257         test -n "$_lines" && {
258                 echo "ip rule del from $_ip pref $_pref table $_table_id" >> $_release_script
259                 echo "ip route flush table $_table_id 2>/dev/null" >> $_release_script
260
261                 cmd="ip rule del from $_ip pref $_pref 2>/dev/null"
262                 echo "$cmd" >> $_setup_script
263
264                 cmd="ip route flush table $_table_id 2>/dev/null"
265                 echo "$cmd" >> $_setup_script
266
267                 cmd="ip rule add from $_ip pref $_pref table $_table_id"
268                 echo "$cmd || {" >> $_setup_script
269                 echo "    echo \"$cmd - failed \$ret\"" >> $_setup_script
270                 echo "    exit \$ret" >> $_setup_script
271                 echo "}" >> $_setup_script
272         }
273         local _l
274         for _l in $_lines; do
275                 local _line=`echo -n "$_config" | head -n $_l | tail -n 1`
276                 local _dest=`echo -n "$_line" | cut -d ' ' -f 2`
277                 local _gw=`echo -n "$_line" | cut -d ' ' -f 3`
278
279                 local _via=""
280                 test -n "$_gw" && {
281                         _via="via $_gw"
282                 }
283
284                 cmd="ip route add $_dest $_via dev $_iface table $_table_id"
285                 echo "$cmd || {" >> $_setup_script
286                 echo "    echo \"$cmd - failed \$ret\"" >> $_setup_script
287                 echo "    exit \$ret" >> $_setup_script
288                 echo "}" >> $_setup_script
289         done
290
291         $_setup_script
292         return $?;
293 }
294
295 case "$1" in
296      #############################
297      # called when ctdbd starts up
298      startup)
299         # cleanup old rules
300         pref=$CTDB_PER_IP_ROUTING_RULE_PREF
301         rules=`ip rule show | grep "^$pref:" | sed -e 's/.*from \([^ ][^ ]*\) lookup \([^ ][^ ]*\)/\2;\1/' | xargs`
302         for r in $rules; do
303                 table_id=`echo -n "$r" | cut -d ';' -f1`
304                 ip=`echo -n "$r" | cut -d ';' -f2-`
305
306                 echo "Removing ip rule for public address $ip for routing table $table_id"
307                 cmd="ip rule del from $ip table $table_id pref $pref"
308                 #echo $cmd
309                 eval $cmd
310                 cmd="ip route flush table $table_id"
311                 #echo $cmd
312                 eval $cmd 2>/dev/null
313         done
314
315         # make sure that we only respond to ARP messages from the NIC where
316         # a particular ip address is associated.
317         [ -f /proc/sys/net/ipv4/conf/all/arp_filter ] && {
318             echo 1 > /proc/sys/net/ipv4/conf/all/arp_filter
319         }
320
321         mkdir -p $CTDB_PER_IP_ROUTING_STATE
322
323         ;;
324
325      shutdown)
326
327         for s in $CTDB_PER_IP_ROUTING_STATE/ips/*/per_ip_routing_release.sh; do
328                 run_release_script_once "$s"
329         done
330         rm -rf $CTDB_PER_IP_ROUTING_STATE
331
332         ;;
333
334      ################################################
335      # called when ctdbd wants to claim an IP address
336      takeip)
337         if [ $# != 4 ]; then
338            echo "must supply interface, IP and maskbits"
339            exit 1
340         fi
341         iface=$2
342         ip=$3
343         maskbits=$4
344
345         ipv4_is_valid_addr $ip || {
346                 echo "$0: $1 not an ipv4 address skipping IP:$ip"
347                 exit 0;
348         }
349
350         [ ! -d "$CTDB_PER_IP_ROUTING_STATE" ] && {
351                 echo "$0: $1 No state directory found, waiting for startup."
352                 exit 0;
353         }
354
355         generate_per_ip_routing $ip $maskbits $iface "no" || {
356                 echo "$0: $1: generate_per_ip_routing $ip $maskbits $iface no - failed"
357                 exit 1;
358         }
359
360         setup_per_ip_routing $ip $iface $table_id $release_script $setup_script || {
361                 echo "$0: $1: setup_per_ip_routing $ip $iface $table_id $release_script $setup_script - failed"
362                 exit 1;
363         }
364
365         setup_iface_ip_readd_script $iface $ip $maskbits $setup_script || {
366                 echo "$0: $1: setup_iface_ip_readd_script $iface $ip $maskbits $setup_script - failed"
367                 exit 1;
368         }
369
370         # flush our route cache
371         echo 1 > /proc/sys/net/ipv4/route/flush
372         ctdb gratiousarp $ip $iface
373
374         ;;
375
376      ################################################
377      # called when ctdbd wants to claim an IP address
378      updateip)
379         if [ $# != 5 ]; then
380            echo "must supply old interface, new interface, IP and maskbits"
381            exit 1
382         fi
383         oiface=$2
384         niface=$3
385         ip=$4
386         maskbits=$5
387
388         ipv4_is_valid_addr $ip || {
389                 echo "$0: $1 not an ipv4 address skipping IP:$ip"
390                 exit 0;
391         }
392
393         [ ! -d "$CTDB_PER_IP_ROUTING_STATE" ] && {
394                 echo "$0: $1 No state directory found, waiting for startup."
395                 exit 0;
396         }
397
398         generate_per_ip_routing $ip $maskbits $niface "no" || {
399                 echo "$0: $1: generate_per_ip_routing $ip $maskbits $niface no - failed"
400                 exit 1;
401         }
402
403         setup_per_ip_routing $ip $niface $table_id $release_script $setup_script || {
404                 echo "$0: $1: setup_per_ip_routing $ip $niface $table_id $release_script $setup_script - failed"
405                 exit 1;
406         }
407
408         setup_iface_ip_readd_script $niface $ip $maskbits $setup_script || {
409                 echo "$0: $1: setup_iface_ip_readd_script $niface $ip $maskbits $setup_script - failed"
410                 exit 1;
411         }
412
413         # flush our route cache
414         echo 1 > /proc/sys/net/ipv4/route/flush
415
416         ctdb gratiousarp $ip $niface
417         tickle_tcp_connections $ip
418
419         ;;
420
421      ##################################################
422      # called when ctdbd wants to release an IP address
423      releaseip)
424         if [ $# != 4 ]; then
425            echo "must supply interface, IP and maskbits"
426            exit 1
427         fi
428
429         iface=$2
430         ip=$3
431         maskbits=$4
432
433         ipv4_is_valid_addr $ip || {
434                 echo "$0: $1 not an ipv4 address skipping IP:$ip"
435                 exit 0;
436         }
437
438         [ ! -d "$CTDB_PER_IP_ROUTING_STATE" ] && {
439                 echo "$0: $1 No state directory found, waiting for startup."
440                 exit 0;
441         }
442
443         generate_per_ip_routing $ip $maskbits $iface "yes" || {
444                 echo "$0: $1: generate_per_ip_routing $ip $maskbits $iface yes - failed"
445                 exit 1;
446         }
447
448         run_release_script_once "$release_script"
449
450         ;;
451
452
453      ###########################################
454      # called when ctdbd has finished a recovery
455      recovered)
456         ;;
457
458      ####################################
459      # called when ctdbd is shutting down
460      shutdown)
461         ;;
462
463      monitor)
464         ;;
465     *)
466         ctdb_standard_event_handler "$@"
467         ;;
468 esac
469
470 exit 0
471