master-ip-setup 3.54 KB
Newer Older
1 2 3 4
#!/bin/bash
#

# Copyright (C) 2011 Google Inc.
5
# All rights reserved.
6
#
7 8 9
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
10
#
11 12
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
13
#
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 30 31 32 33 34

set -e -u

USAGE_MSG="Usage: $0 {start|stop}"
PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin

35 36
FPING_PACKET_COUNT=5
FPING_PACKET_INTERVAL_MS=200
37

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
# Start the master IP
start() {
  case $CLUSTER_IP_VERSION in
    4)
      ARP_COMMAND="arping -q -U -c 3 -I $MASTER_NETDEV -s $MASTER_IP $MASTER_IP"
      ;;
    6)
      ARP_COMMAND="ndisc6 -q r 3 $MASTER_IP $MASTER_NETDEV"
      ;;
    *)
      echo "Invalid cluster IP version specified: $CLUSTER_IP_VERSION" >&2
      exit 1
      ;;
  esac

  # Check if the master IP address is already configured on this machine
54 55
  if fping -c $FPING_PACKET_COUNT -p $FPING_PACKET_INTERVAL_MS \
      -S 127.0.0.1 $MASTER_IP >/dev/null 2>&1; then
56 57 58 59 60
    echo "Master IP address already configured on this machine. Doing nothing."
    exit 0
  fi

  # Check if the master IP address is already configured on another machine
61 62
  if fping -c $FPING_PACKET_COUNT -p $FPING_PACKET_INTERVAL_MS \
      $MASTER_IP >/dev/null 2>&1; then
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    echo "Error: master IP address configured on another machine." >&2
    exit 1
  fi

  if ! ip addr add $MASTER_IP/$MASTER_NETMASK \
     dev $MASTER_NETDEV label $MASTER_NETDEV:0; then
    echo "Error during the activation of the master IP address" >&2
    exit 1
  fi

  # Send gratuituous ARP to update neighbours' ARP cache
  $ARP_COMMAND || :
}

# Stop the master IP
stop() {
79 80 81 82
  # Check if the master IP address is still configured on this machine
  if ! ip addr show dev $MASTER_NETDEV | \
     grep -F " $MASTER_IP/$MASTER_NETMASK" >/dev/null 2>&1; then
    # Check if the master IP address is configured on a wrong device
83 84
    if fping -c $FPING_PACKET_COUNT -p $FPING_PACKET_INTERVAL_MS \
        -S 127.0.0.1 $MASTER_IP >/dev/null 2>&1; then
85 86 87 88 89 90 91 92 93
      echo "Error: master IP address configured on wrong device," \
           "can't shut it down." >&2
      exit 1
    else
      echo "Master IP address not configured on this machine. Doing nothing."
      exit 0
    fi
  fi

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
  if ! ip addr del $MASTER_IP/$MASTER_NETMASK dev $MASTER_NETDEV; then
    echo "Error during the deactivation of the master IP address" >&2
    exit 1
  fi
}

if (( $# < 1 )); then
  echo $USAGE_MSG >&2
  exit 1
fi

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  *)
    echo $USAGE_MSG >&2
    exit 1
    ;;
esac

exit 0