README: explain how to use a local kernel source tree
[qemu-test.git] / qemu-test
1 #!/bin/sh
2
3 if test -z "$1" -o -z "$2"; then
4     echo "Usage: $0 QEMU TEST1 [TEST2 ...]"
5     exit 1
6 fi
7
8 qemu=$1
9 shift
10
11 FREEZE_THRESHOLD=10
12 QMP_TIMEOUT=10
13
14 kernel=bin/vmlinuz-3.0
15 initrd=bin/initramfs-1.9.2.img.gz
16
17 if test -z $QEMU_SRC; then
18     QEMU_SRC=~/git/qemu
19 fi
20
21 qmp="${QEMU_SRC}/QMP/qmp"
22
23 if ! test -x "${qmp}"; then
24     echo "Please set QEMU_SRC to set to a recent qemu.git tree"
25     exit 1
26 fi
27
28 tmpdir=.tmp-$$
29 tmpinitrd=$tmpdir/initramfs-$$.img.gz
30 tmplog=$tmpdir/logfile-$$.log
31 tmppid=$tmpdir/pidfile-$$.pid
32 tmpqmp=$tmpdir/qmpsock-$$.sock
33
34 if test "$QEMU_TEST_SEED"; then
35     seed=$QEMU_TEST_SEED
36 else
37     seed=`(dd if=/dev/urandom bs=2 count=1 | hexdump -e '/2 "%u\n"') 2>/dev/null`
38 fi
39
40 RANDOM=$seed
41
42 echo "Using RANDOM seed $seed"
43
44 mkdir -p $tmpdir
45 util/mktest-initramfs.sh $initrd "$@" > $tmpinitrd
46
47 checkpid() {
48     kill -0 $1 2>/dev/null
49 }
50
51 get_file_size() {
52     ls -al $1 | cut -f5 -d' ' 2>/dev/null
53     if test $? != 0; then
54         echo 0
55     fi
56 }
57
58 qmp() {
59     $qmp --path=$tmpqmp "$@" &
60     qmp_pid=$!
61     count=0
62     while checkpid $qmp_pid; do
63         sleep 1
64         count=$(($count + 1))
65         if test $count -gt $QMP_TIMEOUT; then
66             echo $count, $QMP_TIMEOUT
67             kill -9 $qmp_pid
68             return 1
69         fi
70     done
71     return 0
72 }
73
74 e() {
75     echo "$@"
76     exec "$@"
77 }
78
79 choose() {
80     local count=0
81     local target=0
82
83     target=$(($RANDOM % $#))
84     count=0
85     for i in "$@"; do
86         if test $count = $target; then
87             echo $i
88             return 0
89         fi
90         count=$(($count + 1))
91     done
92
93     # not supposed to happen...
94     echo $1
95 }
96
97 qemu() {
98     e $qemu -kernel $kernel -initrd $tmpinitrd \
99             -append "console=ttyS0 seed=$seed" "$@" \
100             -pidfile $tmppid -qmp unix:$tmpqmp,server,nowait | tee $tmplog &
101     pid=$!
102
103     last_log_size=`get_file_size $tmplog`
104     freeze_count=0
105     while checkpid $pid; do
106         sleep 1
107         log_size=`get_file_size $tmplog`
108         if test $last_log_size = $log_size; then
109             freeze_count=$(($freeze_count + 1))
110         else
111             freeze_count=0
112             last_log_size=$log_size
113         fi
114         if test $freeze_count -gt $FREEZE_THRESHOLD; then
115             qemu_pid=`cat $tmppid`
116             kill -9 $qemu_pid
117             echo "Guest ($qemu_pid) has not had output in $FREEZE_THRESHOLD seconds!"
118             return 1
119         fi
120         if ! qmp query-status >/dev/null 2>/dev/null; then
121             qemu_pid=`cat $tmppid`
122
123             kill -9 $qemu_pid
124             echo "QEMU is hung!"
125             return 2
126         fi
127     done
128 }
129
130 QEMU_TEST=1
131
132 . "$1"
133 rc=$?
134
135 rm -f $tmplog $tmppid $tmpqmp $tmpinitrd
136 rm -rf $tmpdir
137
138 exit $rc