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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20