/[vps]/baselayout-vserver/trunk/lib/rcscripts/net.modules.d/bridge
Gentoo

Contents of /baselayout-vserver/trunk/lib/rcscripts/net.modules.d/bridge

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3 - (show annotations) (download)
Sat Sep 3 16:10:27 2005 UTC (9 years ago) by hollow
File size: 5130 byte(s)
import initial baselayout sources (1.12.0_pre8)
1 #!/bin/bash
2 # Copyright (c) 2004-2005 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
13 # char* bridge_provides(void)
14 #
15 # Returns a string to change module definition for starting up
16 bridge_provides() {
17 echo "bridge"
18 }
19
20 # void bridge_depend(void)
21 #
22 # Sets up the dependancies for the module
23 bridge_depend() {
24 after interface tuntap
25 before dhcp apipa
26 }
27
28 # bool bridge_check_installed(void)
29 #
30 # Returns 1 if bridge is installed, otherwise 0
31 bridge_check_installed() {
32 [[ -x /sbin/brctl ]] && return 0
33 ${1:-false} && eerror "For bridge support, emerge net-misc/bridge-utils"
34 return 1
35 }
36
37 # bool bridge_check_depends(void)
38 #
39 # Checks to see if we have the needed functions
40 bridge_check_depends() {
41 local f
42
43 for f in interface_down interface_del_addresses interface_set_flag; do
44 [[ $( type -t "${f}" ) == "function" ]] && continue
45 eerror "bridge: missing required function ${f}\n"
46 return 1
47 done
48
49 return 0
50 }
51
52 # char* bridge_get_vars(char *interface)
53 #
54 # Returns a string spaced with possible user set
55 # configuration variables
56 bridge_get_vars() {
57 echo "bridge_$1 brctl_$1"
58 }
59
60 # char* bridge_get_ports(char *interface)
61 #
62 # Returns the interfaces added to the given bridge
63 bridge_get_ports() {
64 brctl show 2>/dev/null \
65 | sed -n -e '/^'"$1"'/,/^\S/ { /^\('"$1"'\|\t\)/s/^.*\t//p }'
66 }
67
68 # char* bridge_get_bridge(char *interface)
69 #
70 # Returns the bridge interface of the given interface
71 bridge_get_bridge() {
72 local x=$( brctl show 2>/dev/null \
73 | sed -e '1 {d}; /^[^ ]/ { N }; /.*'"$1"'.*/ {s/^\([^ \t]\+\).*/\1/p}; d' )
74
75 local -a a=( ${x} )
76 if [[ ${#a[@]} == "1" ]]; then
77 echo "${x}"
78 elif [[ $1 == "${a[3]}" ]]; then
79 echo "${a[0]}"
80 fi
81 }
82
83 # bool bridge_exists(char *interface)
84 #
85 # Returns 0 if the bridge exists, otherwise 1
86 bridge_exists() {
87 brctl show 2>/dev/null | grep -q "^$1"
88 }
89
90 # bool bridge_create(char *interface)
91 #
92 # Creates the bridge - no ports are added here though
93 # Returns 0 on success otherwise 1
94 bridge_create() {
95 local iface="$1" ifvar=$( bash_variable "$1" ) x i
96 local -a opts
97
98 ebegin "Creating bridge ${iface}"
99 x=$( brctl addbr "${iface}" 2>&1 )
100 if [[ -n ${x} ]]; then
101 if [[ ${x} == "br_add_bridge: Package not installed" ]]; then
102 eend 1 "Bridging (802.1d) support is not present in this kernel"
103 else
104 eend 1 "${x}"
105 fi
106 return 1
107 fi
108
109 eval opts=( \"\$\{brctl_${ifvar}\[@\]\}\" )
110 for (( i=0; i<${#opts[@]}; i++ )); do
111 x="${opts[i]/ / ${iface} }"
112 [[ ${x} == "${opts[i]}" ]] && x="${x} ${iface}"
113 x=$( brctl ${x} 2>&1 1>/dev/null )
114 [[ -n ${x} ]] && ewarn "${x}"
115 done
116 eend 0
117 }
118
119 # bool bridge_add_port(char *interface, char *port)
120 #
121 # Adds the port to the bridge
122 bridge_add_port() {
123 local iface="$1" port="$2" e
124
125 interface_set_flag "${port}" promisc true
126 interface_up "${port}"
127 e=$( brctl addif "${iface}" "${port}" 2>&1 )
128 if [[ -n ${e} ]]; then
129 interface_set_flag "${port}" promisc false
130 echo "${e}" >&2
131 return 1
132 fi
133 return 0
134 }
135
136 # bool bridge_delete_port(char *interface, char *port)
137 #
138 # Deletes a port from a bridge
139 bridge_delete_port() {
140 interface_set_flag "$2" promisc false
141 brctl delif "$1" "$2"
142 }
143
144 # bool bridge_start(char *iface)
145 #
146 # set up bridge
147 # This can also be called by non-bridges so that the bridge can be created
148 # dynamically
149 bridge_pre_start() {
150 local iface="$1" ports briface i ifvar=$( bash_variable "$1" ) opts
151 eval ports=\"\$\{bridge_${ifvar}\[@\]\}\"
152 eval briface=\"\$\{bridge_add_${ifvar}\}\"
153 eval opts=\"\$\{brctl_${ifvar}\}\"
154
155 [[ -z ${ports} && -z ${briface} && -z ${opts} ]] && return 0
156
157 # Destroy the bridge if it exists
158 [[ -n ${ports} ]] && bridge_stop "${iface}"
159
160 # Allow ourselves to add to the bridge
161 if [[ -z ${ports} && -n ${briface} ]]; then
162 ports="${iface}"
163 iface="${briface}"
164 fi
165
166 # Create the bridge if needed
167 bridge_exists "${iface}" || bridge_create "${iface}"
168
169 if [[ -n ${ports} ]]; then
170 einfo "Adding ports to ${iface}"
171 eindent
172
173 for i in ${ports}; do
174 interface_exists "${i}" && continue
175 eerror "interface ${i} does not exist"
176 return 1
177 done
178
179 for i in ${ports}; do
180 ebegin "${i}"
181 bridge_add_port "${iface}" "${i}"
182 eend $? || return 1
183 done
184 eoutdent
185 fi
186
187 return 0
188 }
189
190 # bool bridge_stop(char *iface)
191 #
192 # Removes the device
193 # returns 0
194 bridge_stop() {
195 bridge_check_installed || return 0
196
197 local iface="$1" ports i deletebridge=false extra=""
198
199 if bridge_exists "${iface}" ; then
200 ebegin "Destroying bridge ${iface}"
201 interface_down "${iface}"
202 ports=$( bridge_get_ports "${iface}" )
203 deletebridge=true
204 eindent
205 else
206 # Work out if we're added to a bridge for removal or not
207 ports="${iface}"
208 iface=$( bridge_get_bridge "${iface}" )
209 [[ -z ${iface} ]] && return 0
210 extra=" from ${iface}"
211 fi
212
213 for i in ${ports}; do
214 ebegin "Removing port ${i}${extra}"
215 bridge_delete_port "${iface}" "${i}"
216 eend $?
217 done
218
219 if ${deletebridge} ; then
220 eoutdent
221 brctl delbr "${iface}" &>/dev/null
222 eend 0
223 fi
224 return 0
225 }
226
227 # vim:ts=4

  ViewVC Help
Powered by ViewVC 1.1.20