Merge branch 'master' of ssh://git.onelab.eu/git/tests
[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 # 1 gigabyte ram
18 RAM=1024
19 DISK_SIZE=18G
20 DISK_FORMAT=qcow2
21 DISK_IMAGE=hdd-${DISK_FORMAT}-${DISK_SIZE}.img
22
23 SCRIPT=qemu-ifup
24
25 ####### we want this script to be invokable through ssh without the ssh client to remain hanging
26 # close stdin
27 exec <&-
28 # redirect stdout
29 exec >> log.txt
30 # redirect stderr on stdout
31 exec 2>&1
32
33 ########## from the test environment
34 # expected vars are MACADDR, NODE_ISO, HOSTNAME, IP and TARGET_ARCH
35 CONFIG=qemu.conf
36 [ -f "$CONFIG" ] || { echo "Config file for qemu $CONFIG not found in $(pwd)" ; exit 1 ; }
37 . $CONFIG
38
39 # NOTE: check if the machine supports 64bits. We'll add -no-kqemu only
40 # if it does. On 32bits host, qemu-system-x86_64 doesn't accept this
41 # parameter (although it's there in the man page)
42 function is_64bits () {
43     return $(cat /proc/cpuinfo | grep "^flags" | grep " lm " > /dev/null)
44 }
45
46
47 # the launcher, depending on target arch
48 # make sure to check qemu-kill-node for consistency
49 has_kvm=$(type -p qemu-kvm)
50 if [ -n "has_kvm" ] ; then
51     QEMU="qemu-kvm" ; ARGS=""
52 else
53     case $TARGET_ARCH in
54         i386)           QEMU=qemu ; ARGS="" ;;
55         x86_64)         QEMU=qemu-system-x86_64 ; if is_64bits; then ARGS="-no-kqemu"; else ARGS=""; fi ;;
56         *)                      echo "Cannot handle TARGET_ARCH=$TARGET_ARCH"; exit 1 ;;
57     esac
58 fi
59
60 echo "Running $COMMAND in $(pwd)"
61 echo "Starting at $(date)"
62
63 #Creating new DISK_IMAGE if needed only
64 if [ -f $DISK_IMAGE ] ; then
65     echo "Using existing $DISK_IMAGE"
66 else
67     echo -n "Creating hard disk image (${DISK_SIZE}) as $DISK_IMAGE .. "
68     img=$(qemu-img create -f ${DISK_FORMAT} $DISK_IMAGE ${DISK_SIZE})
69     if [ -z "$img" ];then
70         echo "Failed"
71         exit 1
72     fi
73     echo "Done"
74 fi
75
76 echo 'Trying to load the kqemu module'
77 if modprobe kqemu &> /dev/null ; then
78     echo "kqemu loaded"
79 else
80     echo "WARNING : Could not modprobe kqemu"
81 fi
82
83 echo 'Checking for a loaded kqemu module'
84 lsmod | grep kqemu
85 echo 'Checking for /dev/kqemu'
86 ls -l /dev/kqemu
87
88 echo 'Cleaning up pid file'
89 rm -f qemu.pid
90
91
92
93 # qemu options
94 # basics
95 ARGS="$ARGS -m ${RAM}"
96 ARGS="$ARGS -hda ${DISK_IMAGE}"
97 ARGS="$ARGS -nographic"
98 ARGS="$ARGS -pidfile qemu.pid"
99 # boot from CD
100 ARGS="$ARGS -boot d"
101 ARGS="$ARGS -cdrom ${NODE_ISO}"
102 # set mac address
103 ARGS="$ARGS -net nic,macaddr=${MACADDR}"
104 # set init script
105 ARGS="$ARGS -net tap,script=${SCRIPT}"
106 echo "Running $QEMU $ARGS"
107 exec $QEMU $ARGS &