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