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