use different methods for interacting with host machine virtual nodes are on
[tests.git] / system / template-qemu / env-qemu
1 #!/bin/bash
2
3
4 # Establishment of a runtime environment for a
5 # virtual  machine  under QEMU, This script permits
6 # to the virtual machine to share the
7 # network connection with a host machine under FC6.
8
9 #Author: Amine chaoui
10
11 # Default Value
12 IP_GATEWAY=0.0.0.0
13 IP_HOST=0.0.0.0
14 IP_BROADCAST=0.0.0.0
15 IP_NETMASK=0.0.0.0
16
17 INTERFACE_LAN=eth0
18 INTERFACE_BRIDGE=br0
19 brctl=/usr/sbin/brctl
20
21 # Fonction de mise en place du pont
22 start () {
23     #if we have already configured the same host_box no need to do it again
24     set $(/sbin/ifconfig | grep $INTERFACE_BRIDGE) >/dev/null
25     if [ -n "$1" ]; then
26         echo "Interface bridge $INTERFACE_BRIDGE already exist."
27         exit 0
28     fi
29     if [ -n "$1" ]; then
30         INTERFACE_LAN=$1
31         shift
32     fi
33     set $(/sbin/ifconfig | grep $INTERFACE_LAN) >/dev/null
34     if [ -z "$1" ]; then
35         echo "Interface réseau $IF_HOTE non trouvée."
36         exit 1
37     fi
38     shift $(($# - 1))
39     echo "Using the interface" $INTERFACE_LAN
40         
41     #Restarting the udev
42     echo "Starting the udev ..."
43     /sbin/udevd restart
44     echo "Starting the kqemu patch module ..."
45     modprobe kqemu
46     #Loding the tun/tap model
47     echo "Loading the kqemu patch module ..."
48     modprobe kqemu
49     #Loding the tun/tap model
50     echo "Loading the tun module ..."
51     modprobe tun
52     set $(lsmod | grep tun) >/dev/null
53     if [ -z "$1" ]; then
54         echo "Module tun/tap not activated"
55         exit 1
56     fi
57     shift $(($# - 1))
58
59     #Giving acces in Read/Write to the tun module
60     echo "Granting the Read/Write acces to the tun module..."
61     chmod 666 /dev/net/tun
62
63
64     ##Get The BROADCAST ip @
65     set $(/sbin/ip addr show $INTERFACE_LAN | grep inet) >/dev/null 2>&1
66     if [ -n "$2" ]; then
67         IP_BROADCAST=$4
68     fi
69     shift $(($# - 1))
70     
71     #Getting the GATEWAY IP @
72     set $(netstat -rn | grep UG ) >/dev/null 2>&1
73     if [ -n "$2" ]; then
74         IP_GATEWAY=$2
75     fi
76     shift $(($# - 1))
77
78     #Getting the host IP
79     set $(ifconfig $INTERFACE_LAN 2> /dev/null | grep "inet addr:" | \
80         sed -e "s/.*addr:\([^ ]*\).*/\1/")
81     if [ -n "$1" ]; then
82         IP_HOST=$1
83     fi
84     shift $(($# - 1))
85  
86     ##Getting the Netmask address
87     set $(ifconfig $INTERFACE_LAN 2> /dev/null | grep "inet addr:" | \
88         sed -e "s/.*Mask:\([^ ]*\).*/\1/")
89     if [ -n "$1" ]; then
90         IP_NETMASK=$1
91     fi
92     shift $(($# - 1))
93     #check if the module brctl is there
94     if  [ ! -e $brctl ] ; then
95         echo "Module Brctl  '/usr/sbin/brctl' is not found"
96         exit 0
97     fi
98     # Création et paramétrage du pont
99     echo "Configure $INTERFACE_BRIDGE bridge..."
100     $brctl addbr $INTERFACE_BRIDGE
101     #/usr/sbin/brctl stp $INTERFACE_BRIDGE yes
102     $brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
103     echo "Activating promiscuous mode  $INTERFACE_LAN..."
104     /sbin/ifconfig $INTERFACE_LAN 0.0.0.0 promisc up
105     sleep 2
106     echo "IP address on $INTERFACE_BRIDGE..."
107     # static
108     /sbin/ifconfig $INTERFACE_BRIDGE $IP_HOST  broadcast $IP_BROADCAST  netmask $IP_NETMASK up
109     sleep 1
110         
111     #Reconfigure the Bridge IP @ in the host machine
112     echo "Configuring  the IP  Gateway @:" $IP_GATEWAY
113     route add default gw $IP_GATEWAY
114
115     
116     #wipe the host firewall otherwise the guest qemu can't acces to the LAN
117     echo "Wiping the firewall..." 
118     iptables -F
119     
120 }
121
122
123 #Adding a new interface to the bridge
124 add () {
125         /sbin/ifconfig $1 0.0.0.0 promisc up
126         /usr/sbin/brctl addif $INTERFACE_BRIDGE $1
127 }
128
129
130 #Stop the actual bridged network  and Restore the original network
131 stop () {
132         if [ -n "$1" ]; then
133                 INTERFACE_LAN=$1
134         fi
135         TESTPONT=$(/sbin/ifconfig | grep $INTERFACE_BRIDGE)
136         if [ -z "$TESTPONT" ]; then
137                 echo "Attention : pont réseau non trouvé. Vérifier la config réseau ..."
138                 exit 1
139         fi
140         #check if the module brctl is there
141         if [  ! -e $brctl  ] ;then
142             echo "Module Brctl  '/usr/sbin/brctl' is not found"
143             exit 0
144         fi
145         $brctl delif $INTERFACE_BRIDGE $INTERFACE_LAN
146         /sbin/ifconfig $INTERFACE_BRIDGE down
147         $brctl delbr $INTERFACE_BRIDGE
148         /sbin/service network restart
149 }
150
151
152 case $1 in
153         start)
154                 start $2
155         ;;
156         stop)
157                 stop $2
158         ;;
159         add)
160                 add $2
161         ;;
162         *)
163                 echo $"Use: env-qemu {start|add|stop} [interface]"
164                 exit 1
165 esac
166
167 exit 0