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