7b3c01609d27aabd93acf46e0266c6c9aeecb82a
[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
12 # constant
13 INTERFACE_BRIDGE=br0
14 # Default Value
15 INTERFACE_LAN=eth0
16
17 # Fonction de mise en place du pont
18 start () {
19
20     # take extra arg for ifname, if provided
21     [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
22
23     ### Checking
24     type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; }
25
26     #if we have already configured the same host_box no need to do it again
27     /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null && {
28         echo "Interface bridge $INTERFACE_BRIDGE already exist."
29         exit 0
30     }
31     /sbin/ifconfig $INTERFACE_LAN &>/dev/null || {
32         echo "Cannot use interface $INTERFACE_LAN - exiting"
33         exit 1
34     }
35
36     #Getting host IP/masklen
37     address=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | awk '{print $2;}')
38     [ -z "$address" ] && { echo "ERROR: Could not determine IP address for $INTERFACE_LAN" ; exit 1 ; }
39     
40     broadcast=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | awk '{print $4;}')
41     [ -z "$broadcast" ] && echo "WARNING: Could not determine broadcast address for $INTERFACE_LAN"
42
43     gateway=$(netstat -rn | grep '^0.0.0.0' | awk '{print $2;}')
44     [ -z "$gateway" ] && echo "WARNING: Could not determine gateway IP"
45
46     ### do it
47     #Restarting udev
48     echo "Starting udev ..."
49     /sbin/udevd restart
50     if modprobe kqemu ; then
51         echo "kqemu loadded"
52     else
53         echo "WARNING : Could not modprobe kqemu"
54     fi
55     #Loding the tun/tap model
56     if modprobe tun ; then
57         echo "tun loaded"
58         # Giving read/write access
59         echo "Granting read/write acces to the tun device"
60         chmod 666 /dev/net/tun
61     else
62         echo "Could not modprobe tun - exiting"
63         exit 1
64     fi
65
66     # creating the bridge
67     echo "Creating bridge $INTERFACE_BRIDGE"
68     brctl addbr $INTERFACE_BRIDGE
69     #brctl stp $INTERFACE_BRIDGE yes
70     brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
71     echo "Activating promiscuous mode  $INTERFACE_LAN..."
72     /sbin/ifconfig $INTERFACE_LAN 0.0.0.0 promisc up
73     sleep 2
74     echo "Setting bridge $address $broadcast"
75     # static
76     /sbin/ifconfig $INTERFACE_BRIDGE $address broadcast $broadcast up
77     sleep 1
78         
79     #Reconfigure the routing table
80     echo "Configuring  the IP  Gateway @:" $gateway
81     route add default gw $gateway
82
83     # xxx this is *wrong*
84     # wipe the host firewall otherwise the guest qemu can't access the LAN
85     # echo "Wiping the firewall..." 
86     # iptables -F
87     
88 }
89
90 #Adding a new interface to the bridge: this is used by qemu-ifup 
91 add () {
92
93     [[ -z "$@" ]] && { echo "Usage: $COMMAND add ifname" ; exit 1 ; }
94     INTERFACE_LAN=$1; shift
95
96     echo "Activating link for $INTERFACE_LAN..."
97     /sbin/ip link set $INTERFACE_LAN up
98     sleep 1
99     echo "Adding $INTERFACE_LAN to $INTERFACE_BRIDGE"
100     brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
101
102 }
103
104 #Stop the bridge and restore the original setting
105 stop () {
106     # take extra arg for ifname, if provided
107     [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
108
109     ### Checking
110     type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; }
111
112     /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null || {
113         echo "Interface bridge $INTERFACE_BRIDGE does not exist."
114         exit 0
115     }
116     brctl delif $INTERFACE_BRIDGE $INTERFACE_LAN
117     /sbin/ifconfig $INTERFACE_BRIDGE down
118     brctl delbr $INTERFACE_BRIDGE
119     /sbin/service network restart
120 }
121
122
123 case "$1" in
124     start)
125         shift; start "$@" ;;
126     stop)
127         shift; stop "$@" ;;
128     add)
129         shift; add "$@" ;;
130     *)
131         echo $"Usage: env-qemu {start|add|stop} [interface]" ; exit 1 ;;
132 esac
133
134 exit 0