fixes
[tests.git] / system / template-qemu / qemu-bridge-init
index 38de588..399907c 100755 (executable)
@@ -1,11 +1,12 @@
 #!/bin/bash
 
+# Thierry Parmentelat <thierry.parmentelat@inria.fr>
+# Copyright (C) 2010 INRIA 
+#
 # Establishment of a runtime environment for a
 # virtual  machine  under QEMU, This script allows the host box
 # to share its network connection with qemu-based guests
 #
-# Author: Amine chaoui
-#
 
 COMMAND=$(basename $0)
 cd $(dirname $0)
@@ -15,18 +16,50 @@ set -x
 
 # constant
 INTERFACE_BRIDGE=br0
-# Default Value
-INTERFACE_LAN=eth0
+
+# Default Value for INTERFACE_LAN
+# use /proc/net/dev instead of a hard-wired list
+function gather_interfaces () {
+    python <<EOF
+for line in file("/proc/net/dev"):
+    if ':' not in line: continue
+    ifname=line.replace(" ","").split(":")[0]
+    if ifname.find("lo")==0: continue
+    if ifname.find("virbr")==0: continue
+    if ifname.find("tap")==0: continue
+    print ifname
+EOF
+}
+    
+# let's try to figure out the interface to use - try these in order
+function discover_interface () {
+    for ifname in $(gather_interfaces); do
+       ip link show $ifname | grep -qi 'state UP' && { INTERFACE_LAN=$ifname; return; }
+    done
+    # still not found ? that's bad
+    INTERFACE_LAN=unknown
+}
+discover_interface
+echo $INTERFACE_LAN
 
 # Fonction de mise en place du pont
-start () {
+function start () {
 
     echo "========== $COMMAND: entering start - beg"
     hostname
     uname -a
     ifconfig
+    netstat -rn
     echo "========== $COMMAND: entering start - end"
 
+    # disable netfilter calls for bridge interface (they cause panick on 2.6.35 anyway)
+    #
+    # another option would be to accept the all forward packages for
+    # bridged interface like: -A FORWARD -m physdev --physdev-is-bridged -j ACCEPT
+    sysctl net.bridge.bridge-nf-call-iptables=0
+    sysctl net.bridge.bridge-nf-call-ip6tables=0
+    sysctl net.bridge.bridge-nf-call-arptables=0
+
     # take extra arg for ifname, if provided
     [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
 
@@ -92,17 +125,19 @@ start () {
 
     echo "========== $COMMAND: exiting start - beg"
     ifconfig
+    netstat -rn
     echo "========== $COMMAND: exiting start - end"
 }
 
 #Adding a new interface to the bridge: this is used by qemu-ifup 
-add () {
+function add () {
 
     [[ -z "$@" ]] && { echo "Usage: $COMMAND add ifname" ; exit 1 ; }
     INTERFACE_LAN=$1; shift
 
     echo "========== $COMMAND: entering add - beg"
     ifconfig
+    netstat -rn
     echo "========== $COMMAND: entering add - end"
 
     echo "Activating link for $INTERFACE_LAN..."
@@ -127,6 +162,7 @@ add () {
     echo "========== $COMMAND: exiting add - beg"
 
     ifconfig
+    netstat -rn
 
     echo "Installed iptables"
     iptables-save
@@ -135,7 +171,7 @@ add () {
 }
 
 #Stop the bridge and restore the original setting
-stop () {
+function stop () {
     # take extra arg for ifname, if provided
     [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }