comment for keeping track of the memory size
[tests.git] / system / template-qemu / qemu-start-node
1 #!/bin/bash
2
3 # Thierry Parmentelat <thierry.parmentelat@inria.fr>
4 # Copyright (C) 2010 INRIA 
5 #
6 # it does the following:
7 # (*) close all file descriptors and redirect output to log.txt 
8 #     this is because it is designed for use through ssh from a remote test master controller
9 # (*) initialize a disk image if it does not exist yet
10 # (*) starts a qemu instance 
11
12 # cd in this command's directory
13 COMMAND=$(basename $0)
14 cd $(dirname $0)
15
16 ########## globals
17 # use 2Gb to be safe now that we have a big infra
18 # 1Gb used to be anough up to f18
19 # with f20 we went to 1.5 Gb
20 # with f21 1.5 Gb might be enough too, but we now have much more memory space so WTH
21 RAM=2048
22 DISK_SIZE=100G
23 DISK_FORMAT=qcow2
24 DISK_IMAGE=hdd-${DISK_FORMAT}-${DISK_SIZE}.img
25
26 SCRIPT=qemu-ifup
27
28 ####### we want this script to be invokable through ssh without the ssh client to remain hanging
29 # close stdin
30 exec <&-
31 # redirect stdout
32 exec >> log.txt
33 # redirect stderr on stdout
34 exec 2>&1
35
36 ########## from the test environment
37 # expected vars are MACADDR, NODE_ISO, HOSTNAME, IP and TARGET_ARCH
38 CONFIG=qemu.conf
39 [ -f "$CONFIG" ] || { echo "Config file for qemu $CONFIG not found in $(pwd)" ; exit 1 ; }
40 . $CONFIG
41
42 # NOTE: check if the machine supports 64bits. We'll add -no-kqemu only
43 # if it does. On 32bits host, qemu-system-x86_64 doesn't accept this
44 # parameter (although it's there in the man page)
45 function is_64bits () {
46     return $(cat /proc/cpuinfo | grep "^flags" | grep " lm " > /dev/null)
47 }
48
49
50 # the launcher, depending on target arch
51 # make sure to check qemu-kill-node for consistency
52
53 # use kvm if available
54 has_kvm=$(type -p qemu-kvm)
55 if [ -n "$has_kvm" ] ; then
56     QEMU="qemu-kvm" ; ARGS=""
57 else
58     case $TARGET_ARCH in
59         i386)           QEMU=qemu ; ARGS="" ;;
60         x86_64)         QEMU=qemu-system-x86_64 ; if is_64bits; then ARGS="-no-kqemu"; else ARGS=""; fi ;;
61         *)                      echo "Cannot handle TARGET_ARCH=$TARGET_ARCH"; exit 1 ;;
62     esac
63 fi
64
65 echo "Running $COMMAND in $(pwd)"
66 echo "Starting at $(date)"
67
68 #Creating new DISK_IMAGE if needed only
69 if [ -f $DISK_IMAGE ] ; then
70     echo "Using existing $DISK_IMAGE"
71 else
72     echo -n "Creating hard disk image (${DISK_SIZE}) as $DISK_IMAGE .. "
73     img=$(qemu-img create -f ${DISK_FORMAT} $DISK_IMAGE ${DISK_SIZE})
74     if [ -z "$img" ];then
75         echo "Failed"
76         exit 1
77     fi
78     echo "Done"
79 fi
80
81 echo 'Trying to load the kqemu module'
82 if modprobe kqemu &> /dev/null ; then
83     echo "kqemu loaded"
84 else
85     echo "WARNING : Could not modprobe kqemu"
86 fi
87
88 echo 'Checking for a loaded kqemu module'
89 lsmod | grep kqemu
90 echo 'Checking for /dev/kqemu'
91 ls -l /dev/kqemu
92
93 echo 'Cleaning up pid file'
94 rm -f qemu.pid
95
96
97
98 # qemu options
99 # basics
100 ARGS="$ARGS -m ${RAM}"
101 ARGS="$ARGS -hda ${DISK_IMAGE}"
102 ARGS="$ARGS -nographic"
103 ARGS="$ARGS -pidfile qemu.pid"
104 # boot from CD
105 ARGS="$ARGS -boot d"
106 ARGS="$ARGS -cdrom ${NODE_ISO}"
107 # set mac address
108 ARGS="$ARGS -net nic,macaddr=${MACADDR}"
109 # set init script
110 ARGS="$ARGS -net tap,script=${SCRIPT}"
111 echo "Running $QEMU $ARGS"
112 exec $QEMU $ARGS &