Advertisement
AnrDaemon

ifupdown macvlan manager

Apr 14th, 2015
918
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.68 KB | None | 0 0
  1. #!/bin/sh
  2. # $Id: macvlan.sh 153 2015-04-15 06:14:07Z anrdaemon $
  3.  
  4. set -e
  5.  
  6. print_version()
  7. {
  8.   echo 'IPROUTE2 MACVLAN helper script $Rev: 153 $'
  9. }
  10.  
  11. print_examples()
  12. {
  13.   echo "
  14. Example configurations:
  15.  
  16.  # Private interface (no packet forwarding inside the host)
  17.  # Imagine your regular network interface attached to a separate cable.
  18.  # Except it is the same cable.
  19.  auto mac0
  20.  iface mac0 inet static
  21.    address 192.168.0.2
  22.    netmask 255.255.255.0
  23.    gateway 192.168.0.1
  24.    dns-nameservers 192.168.0.1
  25.    macvlan_link eth0
  26.  
  27.  # Local bridge interface (lightweight alternative to bridge-utils)
  28.  # Similar interfaces can be used by f.e. LXC to communicate between them
  29.  # the host, and the rest.
  30.  auto mac1
  31.  iface mac1 inet static
  32.    address 192.168.1.1
  33.    netmask 255.255.255.0
  34.    macvlan_link eth0
  35.    macvlan_mode bridge"
  36.   test "$1" || echo ""
  37. }
  38.  
  39. print_refs()
  40. {
  41.   echo "
  42. SEE ALSO
  43.  
  44.  $(basename "$0") --examples, $(basename "$0") --help-full, ip(8), RFC 7042 Section 2.1"
  45.   test "$1" || echo ""
  46. }
  47.  
  48. print_help()
  49. {
  50.   echo "
  51. Switches:
  52.  
  53.  -I,--install  install the helper from current location.
  54.  -V,--version  print version info
  55.  --help        print help page
  56.  
  57. Use /etc/network/interfaces to configure your macvlan interfaces.
  58. Special parameters:
  59.  
  60.  macvlan_link <physical eth device>
  61.        (mandatory) specifies raw device to create macvlan device on
  62.  
  63.  macvlan_mode { private | vepa | bridge | passthru }
  64.        (optional) sets interface mode. Default is 'private'.
  65.  
  66. Use 'hwaddress ether ...' to assign persistent MAC address to the interface.
  67. Make sure the bit 0x02 is set in your custom MAC to avoid collisions."
  68.   test "$1" || echo ""
  69. }
  70.  
  71. print_help_full()
  72. {
  73.   print_version
  74.   print_help -n
  75.   print_examples -n
  76.   print_refs
  77. }
  78.  
  79.  
  80. for param; do
  81.   case $param in
  82.     '-I'|'--install')
  83.       ln -fs "$(readlink -fn "$0")" /etc/network/if-pre-up.d/macvlan
  84.       ln -fs "$(readlink -fn "$0")" /etc/network/if-post-down.d/macvlan
  85.       ;;
  86.     '-V'|'--version')
  87.       print_version
  88.       ;;
  89.     '--examples')
  90.       print_version
  91.       print_examples
  92.       ;;
  93.     '--help-full')
  94.       print_version
  95.       print_help -n
  96.       print_examples -n
  97.       print_refs
  98.       ;;
  99.     *)
  100.       print_version
  101.       print_help -n
  102.       print_refs
  103.       ;;
  104.   esac
  105.   exit 0
  106. done
  107.  
  108. test $IF_MACVLAN_LINK || exit 0
  109.  
  110. test $IF_HWADDRESS && HWADDR="address $IF_HWADDRESS"
  111.  
  112. case $PHASE in
  113.   'pre-up')
  114.     ip link set dev $IF_MACVLAN_LINK up || exit 1
  115.     ip link add name $IFACE link $IF_MACVLAN_LINK $HWADDR type macvlan mode ${IF_MACVLAN_MODE:-private}
  116.     ;;
  117.   'post-down')
  118.     ip link delete dev $IFACE type macvlan
  119.     ;;
  120. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement