/[vps]/baselayout-vserver/branches/baselayout-1_12/net-scripts/net.modules.d/bridge
Gentoo

Contents of /baselayout-vserver/branches/baselayout-1_12/net-scripts/net.modules.d/bridge

Parent Directory Parent Directory | Revision Log Revision Log


Revision 206 - (show annotations) (download)
Sun Jan 15 10:27:03 2006 UTC (8 years, 10 months ago) by phreak
File size: 4453 byte(s)
Merging r1822 for branches/baselayout-1_12
1 #!/bin/bash
2 # Copyright (c) 2004-2006 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
4
5 # Contributed by Roy Marples (uberlord@gentoo.org)
6
7 # Fix any potential localisation problems
8 # Note that LC_ALL trumps LC_anything_else according to locale(7)
9 brctl() {
10 LC_ALL=C /sbin/brctl "$@"
11 }
12 # void bridge_depend(void)
13 #
14 # Sets up the dependancies for the module
15 bridge_depend() {
16 after interface macnet tuntap
17 before dhcp
18 functions interface_down interface_del_addresses interface_set_flag
19 variables bridge bridge_add brctl
20 }
21
22 # bool bridge_check_installed(void)
23 #
24 # Returns 1 if bridge is installed, otherwise 0
25 bridge_check_installed() {
26 [[ -x /sbin/brctl ]] && return 0
27 ${1:-false} && eerror "For bridge support, emerge net-misc/bridge-utils"
28 return 1
29 }
30
31 # char* bridge_get_ports(char *interface)
32 #
33 # Returns the interfaces added to the given bridge
34 bridge_get_ports() {
35 brctl show 2>/dev/null \
36 | sed -n -e '/^'"$1"'/,/^\S/ { /^\('"$1"'\|\t\)/s/^.*\t//p }'
37 }
38
39 # char* bridge_get_bridge(char *interface)
40 #
41 # Returns the bridge interface of the given interface
42 bridge_get_bridge() {
43 local x=$( brctl show 2>/dev/null \
44 | sed -e '1 {d}; /^[^ ]/ { N }; /.*'"$1"'.*/ {s/^\([^ \t]\+\).*/\1/p}; d' )
45
46 local -a a=( ${x} )
47 if [[ ${#a[@]} == "1" ]]; then
48 echo "${x}"
49 elif [[ $1 == "${a[3]}" ]]; then
50 echo "${a[0]}"
51 fi
52 }
53
54 # bool bridge_exists(char *interface)
55 #
56 # Returns 0 if the bridge exists, otherwise 1
57 bridge_exists() {
58 brctl show 2>/dev/null | grep -q "^$1"
59 }
60
61 # bool bridge_create(char *interface)
62 #
63 # Creates the bridge - no ports are added here though
64 # Returns 0 on success otherwise 1
65 bridge_create() {
66 local iface="$1" ifvar=$( bash_variable "$1" ) x i opts
67
68 ebegin "Creating bridge ${iface}"
69 x=$( brctl addbr "${iface}" 2>&1 )
70 if [[ -n ${x} ]]; then
71 if [[ ${x//Package not installed/} != "${x}" ]]; then
72 eend 1 "Bridging (802.1d) support is not present in this kernel"
73 else
74 eend 1 "${x}"
75 fi
76 return 1
77 fi
78
79 opts="brctl_${ifvar}[@]"
80 for i in "${!opts}" ; do
81 x="${i/ / ${iface} }"
82 [[ ${x} == "${i}" ]] && x="${x} ${iface}"
83 x=$( brctl ${x} 2>&1 1>/dev/null )
84 [[ -n ${x} ]] && ewarn "${x}"
85 done
86 eend 0
87 }
88
89 # bool bridge_add_port(char *interface, char *port)
90 #
91 # Adds the port to the bridge
92 bridge_add_port() {
93 local iface="$1" port="$2" e
94
95 interface_set_flag "${port}" promisc true
96 interface_up "${port}"
97 e=$( brctl addif "${iface}" "${port}" 2>&1 )
98 if [[ -n ${e} ]]; then
99 interface_set_flag "${port}" promisc false
100 echo "${e}" >&2
101 return 1
102 fi
103 return 0
104 }
105
106 # bool bridge_delete_port(char *interface, char *port)
107 #
108 # Deletes a port from a bridge
109 bridge_delete_port() {
110 interface_set_flag "$2" promisc false
111 brctl delif "$1" "$2"
112 }
113
114 # bool bridge_start(char *iface)
115 #
116 # set up bridge
117 # This can also be called by non-bridges so that the bridge can be created
118 # dynamically
119 bridge_pre_start() {
120 local iface="$1" ports briface i ifvar=$( bash_variable "$1" ) opts
121 ports="bridge_${ifvar}[@]"
122 briface="bridge_add_${ifvar}"
123 opts="brctl_${ifvar}[@]"
124
125 [[ -z ${!ports} && -z ${!briface} && -z ${!opts} ]] && return 0
126
127 # Destroy the bridge if it exists
128 [[ -n ${!ports} ]] && bridge_stop "${iface}"
129
130 # Allow ourselves to add to the bridge
131 if [[ -z ${!ports} && -n ${!briface} ]]; then
132 ports="${iface}"
133 iface="${!briface}"
134 else
135 ports="${!ports}"
136 fi
137
138 # Create the bridge if needed
139 bridge_exists "${iface}" || bridge_create "${iface}"
140
141 if [[ -n ${ports} ]]; then
142 einfo "Adding ports to ${iface}"
143 eindent
144
145 for i in ${ports}; do
146 interface_exists "${i}" true || return 1
147 done
148
149 for i in ${ports}; do
150 ebegin "${i}"
151 bridge_add_port "${iface}" "${i}"
152 eend $? || return 1
153 done
154 eoutdent
155 fi
156
157 return 0
158 }
159
160 # bool bridge_stop(char *iface)
161 #
162 # Removes the device
163 # returns 0
164 bridge_stop() {
165 local iface="$1" ports i deletebridge=false extra=""
166
167 if bridge_exists "${iface}" ; then
168 ebegin "Destroying bridge ${iface}"
169 interface_down "${iface}"
170 ports=$( bridge_get_ports "${iface}" )
171 deletebridge=true
172 eindent
173 else
174 # Work out if we're added to a bridge for removal or not
175 ports="${iface}"
176 iface=$( bridge_get_bridge "${iface}" )
177 [[ -z ${iface} ]] && return 0
178 extra=" from ${iface}"
179 fi
180
181 for i in ${ports}; do
182 ebegin "Removing port ${i}${extra}"
183 bridge_delete_port "${iface}" "${i}"
184 eend $?
185 done
186
187 if ${deletebridge} ; then
188 eoutdent
189 brctl delbr "${iface}" &>/dev/null
190 eend 0
191 fi
192 return 0
193 }
194
195 # vim:ts=4

  ViewVC Help
Powered by ViewVC 1.1.20