fix check_tcp - display iptables before running qemu - get test specs in build summary
[tests.git] / system / template-qemu / qemu-bridge-init
1 #!/bin/bash
2
3 # Establishment of a runtime environment for a
4 # virtual  machine  under QEMU, This script allows the host box
5 # to share its network connection with qemu-based guests
6 #
7 # Author: Amine chaoui
8 #
9
10 COMMAND=$(basename $0)
11 cd $(dirname $0)
12
13 # constant
14 INTERFACE_BRIDGE=br0
15 # Default Value
16 INTERFACE_LAN=eth0
17
18 # Fonction de mise en place du pont
19 start () {
20
21     # take extra arg for ifname, if provided
22     [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
23
24     ### Checking
25     type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; }
26
27     #if we have already configured the same host_box no need to do it again
28     /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null && {
29         echo "Interface bridge $INTERFACE_BRIDGE already exist."
30         exit 0
31     }
32     /sbin/ifconfig $INTERFACE_LAN &>/dev/null || {
33         echo "Cannot use interface $INTERFACE_LAN - exiting"
34         exit 1
35     }
36
37     #Getting host IP/masklen
38     address=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | awk '{print $2;}')
39     [ -z "$address" ] && { echo "ERROR: Could not determine IP address for $INTERFACE_LAN" ; exit 1 ; }
40     
41     broadcast=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | awk '{print $4;}')
42     [ -z "$broadcast" ] && echo "WARNING: Could not determine broadcast address for $INTERFACE_LAN"
43
44     gateway=$(netstat -rn | grep '^0.0.0.0' | awk '{print $2;}')
45     [ -z "$gateway" ] && echo "WARNING: Could not determine gateway IP"
46
47     ### do it
48     #Restarting udev
49     echo "Starting udev ..."
50     /sbin/udevd restart
51     if modprobe kqemu ; then
52         echo "kqemu loadded"
53     else
54         echo "WARNING : Could not modprobe kqemu"
55     fi
56     #Loding the tun/tap model
57     if modprobe tun ; then
58         echo "tun loaded"
59         # Giving read/write access
60         echo "Granting read/write acces to the tun device"
61         chmod 666 /dev/net/tun
62     else
63         echo "Could not modprobe tun - exiting"
64         exit 1
65     fi
66
67     # creating the bridge
68     echo "Creating bridge $INTERFACE_BRIDGE"
69     brctl addbr $INTERFACE_BRIDGE
70     #brctl stp $INTERFACE_BRIDGE yes
71     brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
72     echo "Activating promiscuous mode  $INTERFACE_LAN..."
73     /sbin/ifconfig $INTERFACE_LAN 0.0.0.0 promisc up
74     sleep 2
75     echo "Setting bridge $address $broadcast"
76     # static
77     /sbin/ifconfig $INTERFACE_BRIDGE $address broadcast $broadcast up
78     sleep 1
79         
80     #Reconfigure the routing table
81     echo "Configuring  the IP  Gateway @:" $gateway
82     route add default gw $gateway
83
84 }
85
86 #Adding a new interface to the bridge: this is used by qemu-ifup 
87 add () {
88
89     [[ -z "$@" ]] && { echo "Usage: $COMMAND add ifname" ; exit 1 ; }
90     INTERFACE_LAN=$1; shift
91
92     echo "Activating link for $INTERFACE_LAN..."
93     /sbin/ip link set $INTERFACE_LAN up
94     sleep 1
95     echo "Adding $INTERFACE_LAN to $INTERFACE_BRIDGE"
96     brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
97
98     # turn off filtering on this interface
99     ########## from the test environment
100     # expected vars are MACADDR, NODE_ISO, HOSTNAME, IP and TARGET_ARCH
101     CONFIG=qemu.conf
102     [ -f "$CONFIG" ] || { echo "Config file for qemu $CONFIG not found in $(pwd)" ; exit 1 ; }
103     . $CONFIG
104
105     echo "Tweaking iptables"
106     iptables-save > iptables.pre
107     # rewrite a new config - quick and dirty
108     ./iptables.py iptables.pre iptables.post $IP
109     iptables-restore < iptables.post
110
111     echo "Installed iptables"
112     iptables-save
113     
114 }
115
116 #Stop the bridge and restore the original setting
117 stop () {
118     # take extra arg for ifname, if provided
119     [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
120
121     ### Checking
122     type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; }
123
124     /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null || {
125         echo "Interface bridge $INTERFACE_BRIDGE does not exist."
126         exit 0
127     }
128     brctl delif $INTERFACE_BRIDGE $INTERFACE_LAN
129     /sbin/ifconfig $INTERFACE_BRIDGE down
130     brctl delbr $INTERFACE_BRIDGE
131     /sbin/service network restart
132     /sbin/service iptables restart
133 }
134
135
136 case "$1" in
137     start)
138         shift; start "$@" ;;
139     stop)
140         shift; stop "$@" ;;
141     add)
142         shift; add "$@" ;;
143     *)
144         echo $"Usage: env-qemu {start|add|stop} [interface]" ; exit 1 ;;
145 esac
146
147 exit 0