Git init
[external/ifupdown.git] / _darcs / current / examples / bridge
1 #!/bin/sh 
2 # The following script example, if dropped in /etc/network/if-pre-up.d/
3 # and under /etc/network/if-down.d/, will manage to configure a bridge 
4 # if defined in the /etc/network/interfaces file as either:
5 #
6 # Note: The bridge-utils package already provide a similar (more
7 # powerful) script this is just provided here for convenience and to
8 # show how the /etc/network/if-*.d/ methods can be defined.
9
10 # [ a bridge with an associated IP address ]
11 #  iface br0 inet static
12 #       bridge-ifaces eth0 eth1
13 #       address 192.168.1.1
14 #       netmask 255.255.255.0
15 # [ a bridge which acts as an anonymous bridge ]
16 #  iface br0 inet manual
17 #       bridge-ifaces eth0 eth1
18 #       up ifconfig $IFACE up
19 #
20 # For more information read:
21 #  http://bridge.sourceforge.net/howto.html
22
23 brctl=`which brctl`
24
25 # Notice that the bridge-utils package must be installed and 
26 # we need to have the BRIDGE_IFACES in order to work
27 [ "$IF_BRIDGE_IFACES" = "" ] && exit 0
28 if [ -z "$brctl" ] ; then
29         # ? Somebody is trying to use us without having bridge-utils?
30         echo "Cannot find the 'brctl' program to setup the bridge"
31         echo "Hint: Have you installed the bridge-utils package?"
32         exit 1
33 fi 
34
35 # Check all interfaces before proceeding
36 for i in $IF_BRIDGE_IFACES; do
37         ip link show $i >/dev/null 2>&1
38         if [ $? -ne 0 ] ; then
39                 echo "Interface $i is not available, aborting"
40                 exit 1
41         fi
42 done
43
44 if [ "$MODE" = "start" ] ; then
45         # We are being called by ifup:
46         # Bring up all the bridge interfaces
47         for i in $IF_BRIDGE_IFACES; do
48                 ifconfig $i 0.0.0.0 up
49         done
50         # And now add the bridge itself and the interfaces which are part
51         # of the bridge
52         brctl addbr $IFACE
53         for i in $IF_BRIDGE_IFACES; do
54                 brctl addif $IFACE $i
55         done
56 elif [ "$MODE" = "stop" ];  then
57         # We are being called by ifdown:
58         # Remove the bridge itself and the bridge association
59         for i in $IF_BRIDGE_IFACES; do
60                 brctl delif $IFACE $i
61         done
62         brctl delbr $IFACE
63         # Bring down all the bridge interfaces
64         for i in $IF_BRIDGE_IFACES; do
65                 ifconfig $i down
66         done
67 fi
68
69 exit 0