PageRenderTime 41ms CodeModel.GetById 36ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/network/if-down.d/ifupdown-scripts-zg2-95vlan

http://github.com/brinkman83/bashrc
Shell | 134 lines | 86 code | 18 blank | 30 comment | 34 complexity | 7ea9d68ebb87e26a30ca23ce0dd43dcf MD5 | raw file
  1#! /bin/sh
  2# $Header$
  3
  4# Environment:
  5#  IFACE        = Logical interface name
  6#  MODE         = { start | stop }
  7#  METHOD       = manual, otherwise exit
  8#  ADDRFAM      = inet, otherwise exit
  9#  IF_DEVICE    = physical interface name (defaults to logical)
 10#  IF_MASTER    = master interface (optional)
 11#  IF_MAC       = mac of the master interface
 12#  IF_VLAN_ID   = VLAN id
 13
 14. /etc/network/ifupdown-scripts-zg2.d/common-functions
 15
 16# remove state if interface is being stopped
 17
 18if [ "$MODE" = 'stop' ]; then
 19    DEV=$(state_entry vlan-dev)
 20    if [ -n "$DEV" ]; then
 21        # we have a vlanned interface
 22        if ! has_ip_address $DEV; then
 23            exec_down "vlan" "vconfig"
 24            IF_MASTER_DEV=$(state_entry vlan-master)
 25	    if ! is_active_vlan_master "$IF_MASTER_DEV" && ! has_ip_address "$IF_MASTER_DEV"; then
 26	        ip link set dev $IF_MASTER_DEV down
 27	    fi
 28        fi
 29    fi
 30    exec_down "vlan" ""
 31    exec_down "vlan-mac" ""
 32    exec_down "vlan-master" ""
 33    exec_down "vlan-id" ""
 34    exec_down "vlan-dev" ""
 35    exit 0
 36fi
 37
 38[ "$MODE" = 'start' ] || exit 0
 39
 40# check if vlan needs to be configured
 41[ "$IF_VLAN_ID" -ne 0 ] || exit 0
 42
 43# check if vlans are supported
 44if [ "$VLAN_SUPPORT" != 'yes' ]; then
 45  abort "No VLAN support in kernel"
 46fi
 47
 48# check if encapsulation is correct
 49
 50verbose "encapsulation $IF_ENCAPSULATION"
 51if [ "$IF_ENCAPSULATION" != "dot1q" ]; then
 52  abort "invalid encapsulation $IF_ENCAPSULATION"
 53fi
 54
 55# find master interface
 56if [ -z "${IF_MASTER:-}" -a -z "${IF_MAC:-}" ]; then
 57    abort "no master configured for VLAN interface $IFACE"
 58fi
 59
 60if [ -n "$IF_MASTER" ]; then
 61    # master by name
 62    # resolve master to a physical device
 63    IF_MASTER_DEV=$(dev_state_entry "$IF_MASTER" dev)
 64    [ -z "${IF_MASTER_DEV:-}" ] && IF_MASTER_DEV="$IF_MASTER"
 65    if ! if_exists "$IF_MASTER_DEV"; then
 66        abort "master $IF_MASTER for VLAN interface $IFACE does not exist"
 67    fi
 68    IF_MAC=$(get_if_mac "$IF_MASTER_DEV")
 69    if [ -z "${IF_MAC:-}" ]; then
 70        abort "master $IF_MASTER for $IFACE is no ethernet device"
 71    fi
 72    IF_MASTER="$IF_MASTER_DEV"
 73else
 74    # master by mac
 75    for iface in $(list_ifaces); do
 76        if [ "$(get_if_mac "$iface")" = "$IF_MAC" ]; then
 77            IF_MASTER="$iface"
 78            break
 79        fi
 80    done
 81    if [ -z "${IF_MASTER:-}" ]; then
 82        abort "no master with MAC $IF_MAC found for $IFACE"
 83    fi
 84fi
 85
 86# IF_MASTER and IF_MAC are set and valid now
 87
 88# ensure that master is up
 89if ! is_if_up "$IF_MASTER"; then
 90    verbose "ip link set dev \"$IF_MASTER\" up"
 91    ip link set dev "$IF_MASTER" up
 92fi
 93
 94# check if VLAN already exists
 95vlan=$(find_vlan "$IF_MASTER" $IF_VLAN_ID)
 96verbose "find_vlan $IF_MASTER $IF_VLAN_ID = $vlan"
 97if [ -n "$vlan" ]; then
 98    # VLAN exists, check if it's the one we want
 99    for iface in $(list_if_by_state dev "^$vlan[ \t]*\$"); do
100    	verbose "test $iface"
101        if [ "$iface" != "$IFACE" -a "$vlan" != "$IF_DEVICE" ]; then
102            abort "device $IF_MASTER VLAN $IF_VLAN_ID already up as $vlan for $iface"
103        fi
104    done
105    # it's the one we want
106    newif="$vlan"
107else
108    # create new vlan
109    verbose "vconfig add \"$IF_MASTER\" $IF_VLAN_ID"
110    vconfig add "$IF_MASTER" $IF_VLAN_ID >/dev/null
111    newif=$(find_vlan "$IF_MASTER" $IF_VLAN_ID)
112    if [ -z "${newif:-}" ]; then
113        abort "could not add VLAN $IF_VLAN_ID on $IF_MASTER for $IFACE"
114    fi
115fi
116
117# newif is set to the VLAN interface
118
119# rename the interface if necessary
120if [ "$newif" != "$IF_DEVICE" ]; then
121    verbose "ip link set dev \"$newif\" down"
122    ip link set dev "$newif" down
123    verbose "ip link set dev \"$newif\" name \"$IF_DEVICE\""
124    ip link set dev "$newif" name "$IF_DEVICE"
125fi
126
127add_down "vlan-id" "$IF_VLAN_ID"
128add_down "vlan-master" "$IF_MASTER"
129add_down "vlan-mac" "$IF_MAC"
130add_down "vlan-dev" "$IF_DEVICE"
131add_down "vlan" "rem \"$IF_DEVICE\" >/dev/null"
132
133# vi:sw=4 
134# end of file