root / tools / master-ip-setup @ 71b75e7a
History | View | Annotate | Download (2.8 kB)
1 |
#!/bin/bash |
---|---|
2 |
# |
3 |
|
4 |
# Copyright (C) 2011 Google Inc. |
5 |
# |
6 |
# This program is free software; you can redistribute it and/or modify |
7 |
# it under the terms of the GNU General Public License as published by |
8 |
# the Free Software Foundation; either version 2 of the License, or |
9 |
# (at your option) any later version. |
10 |
# |
11 |
# This program is distributed in the hope that it will be useful, but |
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
# General Public License for more details. |
15 |
# |
16 |
# You should have received a copy of the GNU General Public License |
17 |
# along with this program; if not, write to the Free Software |
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
19 |
# 02110-1301, USA. |
20 |
|
21 |
set -e -u |
22 |
|
23 |
USAGE_MSG="Usage: $0 {start|stop}" |
24 |
PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin |
25 |
|
26 |
FPING_PACKET_COUNT=1 |
27 |
|
28 |
# Start the master IP |
29 |
start() { |
30 |
case $CLUSTER_IP_VERSION in |
31 |
4) |
32 |
ARP_COMMAND="arping -q -U -c 3 -I $MASTER_NETDEV -s $MASTER_IP $MASTER_IP" |
33 |
;; |
34 |
6) |
35 |
ARP_COMMAND="ndisc6 -q r 3 $MASTER_IP $MASTER_NETDEV" |
36 |
;; |
37 |
*) |
38 |
echo "Invalid cluster IP version specified: $CLUSTER_IP_VERSION" >&2 |
39 |
exit 1 |
40 |
;; |
41 |
esac |
42 |
|
43 |
# Check if the master IP address is already configured on this machine |
44 |
if fping -c $FPING_PACKET_COUNT -S 127.0.0.1 $MASTER_IP >/dev/null 2>&1; then |
45 |
echo "Master IP address already configured on this machine. Doing nothing." |
46 |
exit 0 |
47 |
fi |
48 |
|
49 |
# Check if the master IP address is already configured on another machine |
50 |
if fping -c $FPING_PACKET_COUNT $MASTER_IP >/dev/null 2>&1; then |
51 |
echo "Error: master IP address configured on another machine." >&2 |
52 |
exit 1 |
53 |
fi |
54 |
|
55 |
if ! ip addr add $MASTER_IP/$MASTER_NETMASK \ |
56 |
dev $MASTER_NETDEV label $MASTER_NETDEV:0; then |
57 |
echo "Error during the activation of the master IP address" >&2 |
58 |
exit 1 |
59 |
fi |
60 |
|
61 |
# Send gratuituous ARP to update neighbours' ARP cache |
62 |
$ARP_COMMAND || : |
63 |
} |
64 |
|
65 |
# Stop the master IP |
66 |
stop() { |
67 |
# Check if the master IP address is still configured on this machine |
68 |
if ! ip addr show dev $MASTER_NETDEV | \ |
69 |
grep -F " $MASTER_IP/$MASTER_NETMASK" >/dev/null 2>&1; then |
70 |
# Check if the master IP address is configured on a wrong device |
71 |
if fping -c $FPING_PACKET_COUNT -S 127.0.0.1 $MASTER_IP >/dev/null 2>&1; then |
72 |
echo "Error: master IP address configured on wrong device," \ |
73 |
"can't shut it down." >&2 |
74 |
exit 1 |
75 |
else |
76 |
echo "Master IP address not configured on this machine. Doing nothing." |
77 |
exit 0 |
78 |
fi |
79 |
fi |
80 |
|
81 |
if ! ip addr del $MASTER_IP/$MASTER_NETMASK dev $MASTER_NETDEV; then |
82 |
echo "Error during the deactivation of the master IP address" >&2 |
83 |
exit 1 |
84 |
fi |
85 |
} |
86 |
|
87 |
if (( $# < 1 )); then |
88 |
echo $USAGE_MSG >&2 |
89 |
exit 1 |
90 |
fi |
91 |
|
92 |
case "$1" in |
93 |
start) |
94 |
start |
95 |
;; |
96 |
stop) |
97 |
stop |
98 |
;; |
99 |
*) |
100 |
echo $USAGE_MSG >&2 |
101 |
exit 1 |
102 |
;; |
103 |
esac |
104 |
|
105 |
exit 0 |