Debian Codegraph @

tools/master-ip-setup

raw
1 #!/bin/bash
2 #
3
4 # Copyright (C) 2011 Google Inc.
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are
9 # met:
10 #
11 # 1. Redistributions of source code must retain the above copyright notice,
12 # this list of conditions and the following disclaimer.
13 #
14 # 2. Redistributions in binary form must reproduce the above copyright
15 # notice, this list of conditions and the following disclaimer in the
16 # documentation and/or other materials provided with the distribution.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
19 # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 set -e -u
31
32 USAGE_MSG="Usage: $0 {start|stop}"
33 PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin
34
35 FPING_PACKET_COUNT=5
36 FPING_PACKET_INTERVAL_MS=200
37
38 # Start the master IP
39 start() {
40 case $CLUSTER_IP_VERSION in
41 4)
42 ARP_COMMAND="arping -q -U -c 3 -I $MASTER_NETDEV -s $MASTER_IP $MASTER_IP"
43 ;;
44 6)
45 ARP_COMMAND="ndisc6 -q -r 3 $MASTER_IP $MASTER_NETDEV"
46 ;;
47 *)
48 echo "Invalid cluster IP version specified: $CLUSTER_IP_VERSION" >&2
49 exit 1
50 ;;
51 esac
52
53 # Check if the master IP address is already configured on this machine
54 if fping -c $FPING_PACKET_COUNT -p $FPING_PACKET_INTERVAL_MS \
55 -S 127.0.0.1 $MASTER_IP >/dev/null 2>&1; then
56 echo "Master IP address already configured on this machine. Doing nothing."
57 exit 0
58 fi
59
60 # Check if the master IP address is already configured on another machine
61 if fping -c $FPING_PACKET_COUNT -p $FPING_PACKET_INTERVAL_MS \
62 $MASTER_IP >/dev/null 2>&1; then
63 echo "Error: master IP address configured on another machine." >&2
64 exit 1
65 fi
66
67 if ! ip addr add $MASTER_IP/$MASTER_NETMASK \
68 dev $MASTER_NETDEV label $MASTER_NETDEV:0 preferred_lft 0; then
69 echo "Error during the activation of the master IP address" >&2
70 exit 1
71 fi
72
73 # Send gratuituous ARP to update neighbours' ARP cache
74 $ARP_COMMAND || :
75 }
76
77 # Stop the master IP
78 stop() {
79 # Check if the master IP address is still configured on this machine
80 if ! ip addr show dev $MASTER_NETDEV | \
81 grep -F " $MASTER_IP/$MASTER_NETMASK" >/dev/null 2>&1; then
82 # Check if the master IP address is configured on a wrong device
83 if fping -c $FPING_PACKET_COUNT -p $FPING_PACKET_INTERVAL_MS \
84 -S 127.0.0.1 $MASTER_IP >/dev/null 2>&1; then
85 echo "Error: master IP address configured on wrong device," \
86 "can't shut it down." >&2
87 exit 1
88 else
89 echo "Master IP address not configured on this machine. Doing nothing."
90 exit 0
91 fi
92 fi
93
94 if ! ip addr del $MASTER_IP/$MASTER_NETMASK dev $MASTER_NETDEV; then
95 echo "Error during the deactivation of the master IP address" >&2
96 exit 1
97 fi
98 }
99
100 if (( $# < 1 )); then
101 echo $USAGE_MSG >&2
102 exit 1
103 fi
104
105 case "$1" in
106 start)
107 start
108 ;;
109 stop)
110 stop
111 ;;
112 *)
113 echo $USAGE_MSG >&2
114 exit 1
115 ;;
116 esac
117
118 exit 0