PageRenderTime 84ms CodeModel.GetById 76ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/network/if-down.d/ifupdown-scripts-zg2-60address

http://github.com/brinkman83/bashrc
#! | 102 lines | 86 code | 16 blank | 0 comment | 0 complexity | 382523840e110c53c6cb646873428c33 MD5 | raw file
  1#!/bin/bash
  2# $Header$
  3
  4# IFACE      = Logical interface name
  5# MODE       = start | stop
  6# METHOD     = manual, otherwise exit!
  7# IF_ADDRESS = address/prefix
  8# IF_DEVICE  = physical interface name
  9# IF_SCOPE   = scope of address
 10# IF_BRD     = broadcast address (all1 default, all0/none allowed)
 11# IF_FLAGS   = "secondary"
 12
 13. /etc/network/ifupdown-scripts-zg2.d/common-functions
 14
 15# check that an address has been configured for this interface
 16# TODO: Maybe check if it actually looks like an IP address?
 17[ -z "${IF_ADDRESS:-}" ] && exit 0
 18
 19case "$MODE" in
 20  start)
 21  
 22    # build address parameters, parse flags
 23  
 24    ADDRPARM="dev $IF_DEVICE"
 25    if [ "$IF_DEVICE" != "$IFACE" ]; then
 26      # if logical device name differs from physical, label interface
 27      ADDRPARM="$ADDRPARM label $IF_DEVICE:$IFACE"
 28    fi
 29    IF_BRD=${IF_BRD:="all1"}
 30    case "$IF_BRD" in
 31      none) : ;;
 32      all1) ADDRPARM="$ADDRPARM broadcast +";;
 33      all0) ADDRPARM="$ADDRPARM broadcast -";;
 34      *)    ADDRPARM="$ADDRPARM broadcast $IF_BRD";;
 35    esac
 36
 37    ADDRPARM="$ADDRPARM $IF_ADDRESS"
 38    ADDRPARM="$ADDRPARM ${IF_SCOPE:+scope $IF_SCOPE}"
 39
 40    SECONDARY=""
 41    if echo $IF_FLAGS | grep -s -q -i "secondary"; then
 42      SECONDARY="1"
 43    fi
 44
 45    # initialize interface
 46
 47    cmd "ip addr add $ADDRPARM"
 48
 49    # check if primary or secondary IP
 50    # primary / secondary IP addresses need to be addresed differently since
 51    # taking down a primary IP also removes the secondaries. This can have
 52    # unwanted effects to network connectivity and wrecks our interface state.
 53    
 54    if ip addr show dev "$IF_DEVICE" secondary | \
 55    				grep -s -q "$IF_ADDRESS"; then
 56      # $IF_ADDRESS has become secondary IP
 57      if [ -z "${SECONDARY:-}" ]; then
 58      	# $IF_ADDRESS is not marked as secondary
 59	# take away IP again
 60	ip addr del "$ADDRPARM"
 61	abort "secondary IP not marked as such in /etc/network/interface"
 62      fi
 63    elif ip addr show dev "$IF_DEVICE" primary | \
 64    				grep -s -q "$IF_ADDRESS"; then
 65      # $IF_ADDRESS has become primary IP
 66      if [ -n "$SECONDARY" ]; then
 67        # $IF_ADDRESS is marked as secondary
 68	# take away IP again
 69	ip addr del "$ADDRPARM"
 70	abort "secondary IP came up as primary. Bring up primary before bringing up secondaries."
 71      fi
 72    fi
 73    
 74    add_down "address" "addr del $ADDRPARM"
 75    add_down "ip-address" "$IF_ADDRESS"
 76    add_down "ip-dev" "$IF_DEVICE"
 77
 78    ;;
 79  stop)
 80    ADDR=$(state_entry ip-address)
 81    DEV=$(state_entry ip-dev)
 82    exec_down "ip-address" ""
 83    exec_down "ip-dev" ""
 84    if [ -n "$DEV" ]; then
 85      if ip addr show dev "$DEV" primary | \
 86                                grep -s -q "$ADDR"; then
 87        # we are trying to take down a primary IP address
 88        if ip addr show dev "$DEV" secondary | \
 89      				grep -s -q inet[^6]; then
 90 	  # there are still secondary IPs present
 91 	  abort "take down secondary IPs before taking down primary IPs."
 92        fi
 93      fi
 94    fi
 95
 96    exec_down "address" "ip"
 97    ;;
 98  *)
 99    ;;
100esac
101
102# end of file