take the bridge-creation code out in lbuild-bridge.sh so we can reuse this in regular...
[build.git] / lbuild-bridge.sh
1 #!/bin/bash
2
3 # taking this bridge-initialization code out of lbuild-initvm.sh 
4 # so we can use it on our libvirt/lxc local infra 
5 # there's something very similar in 
6 # tests/system/template-qemu/qemu-bridge-init
7 # that the current code was actually based on, but 
8 # nobody was ever bold enough to reconcile these two 
9
10 # hard-wired 
11 DEFAULT_PUBLIC_BRIDGE=br0
12
13 ##############################
14 # use /proc/net/dev instead of a hard-wired list
15 function gather_interfaces () {
16     python <<EOF
17 for line in file("/proc/net/dev"):
18     if ':' not in line: continue
19     ifname=line.replace(" ","").split(":")[0]
20     if ifname.find("lo")==0: continue
21     if ifname.find("br")==0: continue
22     if ifname.find("virbr")==0: continue
23     if ifname.find("veth")==0: continue
24     if ifname.find("tap")==0: continue
25     print ifname
26 EOF
27 }
28
29 function discover_interface () {
30     for ifname in $(gather_interfaces); do
31         ip link show $ifname | grep -qi 'state UP' && { echo $ifname; return; }
32     done
33     # still not found ? that's bad
34     echo unknown
35 }
36
37 #################### bridge initialization
38 function create_bridge_if_needed() {
39
40     # turn on verbosity
41     set -x
42
43     public_bridge=$1; shift
44
45     # already created ? - we're done
46     ip addr show $public_bridge >& /dev/null && {
47         echo "Bridge already set up - skipping create_bridge_if_needed"
48         return 0
49     }
50
51     # find out the physical interface to bridge onto
52     if_lan=$(discover_interface)
53
54     ip addr show $if_lan &>/dev/null || {
55         echo "Cannot use interface $if_lan - exiting"
56         exit 1
57     }
58
59     #################### bride initialization
60     check_yum_installed bridge-utils
61
62     echo "========== $COMMAND: entering create_bridge - beg"
63     hostname
64     uname -a
65     ip addr show
66     ip route
67     echo "========== $COMMAND: entering create_bridge - end"
68
69     # disable netfilter calls for bridge interface (they cause panick on 2.6.35 anyway)
70     #
71     # another option would be to accept the all forward packages for
72     # bridged interface like: -A FORWARD -m physdev --physdev-is-bridged -j ACCEPT
73     sysctl net.bridge.bridge-nf-call-iptables=0
74     sysctl net.bridge.bridge-nf-call-ip6tables=0
75     sysctl net.bridge.bridge-nf-call-arptables=0
76
77     
78     #Getting host IP/masklen
79     address=$(ip addr show $if_lan | grep -v inet6 | grep inet | head --lines=1 | awk '{print $2;}')
80     [ -z "$address" ] && { echo "ERROR: Could not determine IP address for $if_lan" ; exit 1 ; }
81
82     broadcast=$(ip addr show $if_lan | grep -v inet6 | grep inet | head --lines=1 | awk '{print $4;}')
83     [ -z "$broadcast" ] && echo "WARNING: Could not determine broadcast address for $if_lan"
84
85     gateway=$(ip route show | grep default | awk '{print $3;}')
86     [ -z "$gateway" ] && echo "WARNING: Could not determine gateway IP"
87
88
89     # creating the bridge
90     echo "Creating public bridge interface $public_bridge"
91     brctl addbr $public_bridge
92     brctl addif $public_bridge $if_lan
93     echo "Activating promiscuous mode if_lan=$if_lan"
94     ip link set $if_lan up promisc on
95     sleep 2
96     # rely on dhcp to re assign IP.. 
97     echo "Starting dhclient on $public_bridge"
98     dhclient $public_bridge
99     sleep 1
100
101     #Reconfigure the routing table
102     echo "Configuring gateway=$gateway"
103     ip route add default via $gateway dev $public_bridge
104     ip route del default via $gateway dev $if_lan
105     # at this point we have an extra route like e.g.
106     ## ip route show
107     #default via 138.96.112.250 dev br0
108     #138.96.112.0/21 dev em1  proto kernel  scope link  src 138.96.112.57
109     #138.96.112.0/21 dev br0  proto kernel  scope link  src 138.96.112.57
110     #192.168.122.0/24 dev virbr0  proto kernel  scope link  src 192.168.122.1
111     route_dest=$(ip route show | grep -v default | grep "dev $public_bridge" | awk '{print $1;}')
112     ip route del $route_dest dev $if_lan
113
114     echo "========== $COMMAND: exiting create_bridge - beg"
115     ip addr show
116     ip route show
117     echo "========== $COMMAND: exiting create_bridge - end"
118
119     # for safety
120     sleep 3
121     return 0
122
123 }
124
125 function main () {
126     if [[ -n "$@" ]] ; then 
127         public_bridge="$1"; shift
128     else
129         public_bridge="$DEFAULT_PUBLIC_BRIDGE"
130     fi
131     create_bridge_if_needed $public_bridge
132 }
133
134 main "$@"