*Manage nodes log files into a .tar
[tests.git] / system / qemu.sh
1 #!/bin/bash
2
3 # =============================================================================
4 # Author: Marco Yuen
5 # $Date$
6 # $Revision$
7 # $Author$
8 # =============================================================================
9 # README
10 # ------
11 # Make sure you have the rights to run SUDO. Otherwise, this script will not
12 # work.
13 # =============================================================================
14
15 ###############################################################################
16 # Configurations
17 ###############################################################################
18
19 # Directory where VDE and QEMU are installed
20 # Only needed when not in path or when you want to override
21 #VDEROOT="/usr/local/bin/"
22 #QEMUROOT="/usr/local/bin/"
23
24 # The network info for tun0 (private interface)
25 PRIVATE_IP="10.0.2.1"
26 PRIVATE_NETMASK="255.255.255.0"
27
28 # The network info for tun1 (public interface)
29 PUBLIC_IP="172.20.0.1"
30 PUBLIC_NETMASK="255.255.0.0"
31
32 # The default memory size
33 MEGS="512"
34
35 # The default #CPUS size
36 CPUS="1"
37
38 # By default do not specify the disk, cdrom, floppy, or boot
39 DISK1=""
40 DISK2=""
41 DISK3=""
42 DISK4=""
43 CDROM=""
44 FLOPPY=""
45 BOOT=""
46
47 # If VDE is off, use tap/tun by default. Otherwise use ``user''.
48 MULTIHOMED="no"
49 TYPE="vde"
50
51 # Which QEMU executable to use
52 #QEMUEXE="qemu-system_x86_64"
53 QEMUEXE="qemu"
54
55
56
57 ###############################################################################
58 # Functions 
59 # Please, DO NOT modify anything below this line, unless you know what you are
60 # doing.
61 ###############################################################################
62
63 NETWORK_SET=""
64 function network_set {
65     if [[ ! -z ${NETWORK_SET} ]]; then
66         echo "Don't mix -u, -v, -t. Use only one"
67         exit 1
68     fi
69 }
70
71 function image_check {
72     IMG=${1}
73     if [[ ! -w ${IMG} ]]; then
74         echo "The disk image '${IMG}' doesn't exist or not writtable"
75         exit 1
76     fi
77 }
78
79 function kill_or_start_vde {
80     # For good measure, let's kill all the existing vde_switch(es)
81     if [[ -S /tmp/private.ctl && -S /tmp/public.ctl ]]; then
82         echo -n "Existing public and private switches found. Kill them?(yes or no) "
83         read KILLTHEM
84
85         if [[ ${KILLTHEM} == "yes" ]]; then
86             create_switches
87         elif [[ ${KILLTHEM} == "no" ]]; then
88             echo "You are exposing weakness ..."
89         fi
90     else
91         create_switches
92     fi
93 }
94
95 function create_switches {
96     sudo killall -9 vde_switch
97     echo "Creating new sockets ..."
98     # Create the private switch
99     sudo ${VDEROOT}vde_switch -tap tun0 \
100         -sock /tmp/private.ctl -daemon
101     # Create the public switch
102     sudo ${VDEROOT}vde_switch -tap tun1 \
103         -sock /tmp/public.ctl -daemon
104     # Correct the permissions
105     sudo chmod 666 /tmp/private.ctl
106     sudo chmod 666 /tmp/public.ctl
107     # Setting up network info
108     sudo ifconfig tun0 ${PRIVATE_IP} netmask ${PRIVATE_NETMASK}
109     sudo ifconfig tun1 ${PUBLIC_IP} netmask ${PUBLIC_NETMASK}
110     # Turn on ip forwarding and set up IPTABLE
111     sudo sysctl -w net.ipv4.ip_forward=1
112     # Disable IPTABLES first
113     sudo /etc/init.d/iptables stop
114     # XXX Define some rules.
115     sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
116     sudo /etc/init.d/iptables save
117     sudo /etc/init.d/iptables start
118 }
119
120 ###############################################################################
121 # Main
122 ###############################################################################
123
124 function usage() {
125     echo `basename $0` "file"
126     echo "      start qemu with file as hard disk 0 image"
127     echo "-b  [a|c|d]"
128     echo "      Boot on floppy (a), hard disk (c) or CD-ROM (d). Hard disk boot is the default."
129     echo "-c  file"
130     echo "      Use file as CD-ROM image. You can use the host CD-ROM by using /dev/cdrom as filename."
131     echo "-f  file"
132     echo "      Use file as fda floppy disk image.  You can use the host floppy by using /dev/fd0 as filename."
133     echo "-m  megs"
134     echo "      Set virtual RAM size to megs megabytes. Default is ${MEGS} MB."
135     echo "-s  #CPUS"
136     echo "      Set the number of CPUs. Default is ${CPUS} MB."
137     echo "-n  [user|tap|vde]"
138     echo "      vde == VDE based networking. (default = ON)"
139     echo "      tap == tun/tap networking. (default = OFF)"
140     echo "      user == user mode stack for networking. (default = OFF)"
141     echo "-p  mac"
142     echo "      Set the mac address of the nic to specified mac"
143     echo "-l vmsnapshot"
144     echo "      load vm snapshot"
145     echo "-o"
146     echo "      Multihome this machine"
147     echo "-x"
148     echo "      serial console"
149     echo "-h  print help"
150 }
151
152 MAC="52:54:00:12:34:56"
153 GRAPHIC=
154 SNAPSHOT=
155 while getopts "b:c:f:l:m:n:s:hp:ox" opt ; do
156     case ${opt} in
157         # Boot mode options
158         b)
159             BOOT=${OPTARG}
160             ;;
161         c)
162             CDROM=${OPTARG}
163             ;;
164         f)
165             FLOPPY=${OPTARG}
166             ;;
167         m)
168             MEGS=${OPTARG}
169             ;;
170         s)
171             CPUS=${OPTARG}
172             ;;
173         n)
174             network_set
175             [ "${TYPE}" != "${OPTARG}" ] && NETWORK_SET="yes"
176             TYPE=${OPTARG}
177             ;;
178         o)
179             MULTIHOMED="yes"
180             ;;
181         p)
182             MAC=${OPTARG}
183             ;;
184         l)
185             SNAPSHOT=${OPTARG}
186             ;;
187         x)
188             GRAPHIC=-nographic
189             ;;
190         h)
191             usage
192             exit 0
193             ;;
194         ?)
195             usage
196             exit 1
197             ;;
198     esac
199 done
200
201 shift $(($OPTIND - 1))
202 DISK1=$1
203 if [ -z "$DISK1" ]; then
204     usage
205     exit 1
206 fi
207 image_check ${DISK1}
208
209 shift
210 DISK2=$1
211 shift
212 DISK3=$1
213 shift
214 DISK4=$1
215 if [ ! -z "${CDROM}" -a ! -z "${DISK4}" ]; then
216     echo "canot specify cdrom and 4th ide drive (${DISK4})"
217     exit 1
218 fi
219
220 echo "Checking if the kqemu module is loaded ..."
221 module=$(lsmod | grep kqemu | awk '{print $1}')
222
223 if [[ ${module} == "" ]]; then
224     echo "Loading kqemu module ... "
225     #sudo modprobe kqemu
226     [[ $? != 0 ]] && exit 1
227     echo "kqemu loaded."
228 elif [[ ${module} == "kqemu" ]]; then
229     echo "kqemu already loaded. Continue ..."
230 fi
231
232 if [ ! -c /dev/kqemu ] ; then
233     mknod /dev/kqemu c 250 0 
234     chmod 600 /dev/kqemu   
235 fi
236
237 QEMU_CMD="${QEMUROOT}${QEMUEXE} ${GRAPHIC} -m ${MEGS} -smp ${CPUS}"
238 [ ! -z "${SNAPSHOT}" ] && QEMU_CMD="${QEMU_CMD} -loadvm ${SNAPSHOT}"
239 [ ! -z "${FLOPPY}" ] && QEMU_CMD="${QEMU_CMD} -fda ${FLOPPY}"
240 [ ! -z "${CDROM}" ] && QEMU_CMD="${QEMU_CMD} -cdrom ${CDROM}"
241 [ ! -z "${BOOT}" ] && QEMU_CMD="${QEMU_CMD} -boot ${BOOT}"
242
243 [ ! -z "${DISK1}" ] && QEMU_CMD="${QEMU_CMD} -hda ${DISK1}"
244 [ ! -z "${DISK2}" ] && QEMU_CMD="${QEMU_CMD} -hdb ${DISK2}"
245 [ ! -z "${CDROM}" -a ! -z "${DISK3}" ] && QEMU_CMD="${QEMU_CMD} -hdd ${DISK3}"
246 [   -z "${CDROM}" -a ! -z "${DISK3}" ] && QEMU_CMD="${QEMU_CMD} -hdc ${DISK3}"
247
248 case ${TYPE} in
249     vde)
250         QEMU_CMD="${VDEROOT}vdeq ${QEMU_CMD}"
251         echo "Running with VDE ..."
252         kill_or_start_vde
253         QEMU_CMD="${QEMU_CMD} -net nic,vlan=0,macaddr=${MAC}"
254         QEMU_CMD="${QEMU_CMD} -net vde,vlan=0,sock=/tmp/private.ctl"
255         if [ "${MULTIHOMED}" == "yes" ]; then
256             QEMU_CMD="${QEMU_CMD} -net nic,vlan=1"
257             QEMU_CMD="${QEMU_CMD} -net vde,vlan=1,sock=/tmp/public.ctl"
258         fi
259         ;;
260     tap)
261         echo "still have to fix code -- mef"
262         exit 1
263         ;;
264     user)
265         QEMU_CMD="${QEMU_CMD} -net nic -net user"
266         ;;
267     *)
268         echo "invalid networking type"
269         ;;
270 esac
271
272 # Executing frontend.
273 echo ${QEMU_CMD}
274 if [ -z "${GRAPHIC}" ]; then
275     ${QEMU_CMD} &
276 else
277     ${QEMU_CMD}
278 fi