Merge branch 'stable-2.9' into stable-2.10
[ganeti-local] / tools / master-ip-setup
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=5
27 FPING_PACKET_INTERVAL_MS=200
28
29 # Start the master IP
30 start() {
31   case $CLUSTER_IP_VERSION in
32     4)
33       ARP_COMMAND="arping -q -U -c 3 -I $MASTER_NETDEV -s $MASTER_IP $MASTER_IP"
34       ;;
35     6)
36       ARP_COMMAND="ndisc6 -q r 3 $MASTER_IP $MASTER_NETDEV"
37       ;;
38     *)
39       echo "Invalid cluster IP version specified: $CLUSTER_IP_VERSION" >&2
40       exit 1
41       ;;
42   esac
43
44   # Check if the master IP address is already configured on this machine
45   if fping -c $FPING_PACKET_COUNT -p $FPING_PACKET_INTERVAL_MS \
46       -S 127.0.0.1 $MASTER_IP >/dev/null 2>&1; then
47     echo "Master IP address already configured on this machine. Doing nothing."
48     exit 0
49   fi
50
51   # Check if the master IP address is already configured on another machine
52   if fping -c $FPING_PACKET_COUNT -p $FPING_PACKET_INTERVAL_MS \
53       $MASTER_IP >/dev/null 2>&1; then
54     echo "Error: master IP address configured on another machine." >&2
55     exit 1
56   fi
57
58   if ! ip addr add $MASTER_IP/$MASTER_NETMASK \
59      dev $MASTER_NETDEV label $MASTER_NETDEV:0; then
60     echo "Error during the activation of the master IP address" >&2
61     exit 1
62   fi
63
64   # Send gratuituous ARP to update neighbours' ARP cache
65   $ARP_COMMAND || :
66 }
67
68 # Stop the master IP
69 stop() {
70   # Check if the master IP address is still configured on this machine
71   if ! ip addr show dev $MASTER_NETDEV | \
72      grep -F " $MASTER_IP/$MASTER_NETMASK" >/dev/null 2>&1; then
73     # Check if the master IP address is configured on a wrong device
74     if fping -c $FPING_PACKET_COUNT -p $FPING_PACKET_INTERVAL_MS \
75         -S 127.0.0.1 $MASTER_IP >/dev/null 2>&1; then
76       echo "Error: master IP address configured on wrong device," \
77            "can't shut it down." >&2
78       exit 1
79     else
80       echo "Master IP address not configured on this machine. Doing nothing."
81       exit 0
82     fi
83   fi
84
85   if ! ip addr del $MASTER_IP/$MASTER_NETMASK dev $MASTER_NETDEV; then
86     echo "Error during the deactivation of the master IP address" >&2
87     exit 1
88   fi
89 }
90
91 if (( $# < 1 )); then
92   echo $USAGE_MSG >&2
93   exit 1
94 fi
95
96 case "$1" in
97   start)
98     start
99     ;;
100   stop)
101     stop
102     ;;
103   *)
104     echo $USAGE_MSG >&2
105     exit 1
106     ;;
107 esac
108
109 exit 0