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