Add qemu 2.4.0
[kvmfornfv.git] / qemu / tests / qemu-iotests / common
1 #!/bin/bash
2 #
3 # Copyright (C) 2009 Red Hat, Inc.
4 # Copyright (c) 2000-2005 Silicon Graphics, Inc.  All Rights Reserved.
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation.
9 #
10 # This program is distributed in the hope that it would be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18 #
19 # common procedures for QA scripts
20 #
21
22 _setenvironment()
23 {
24     MSGVERB="text:action"
25     export MSGVERB
26 }
27
28 rm -f "$OUTPUT_DIR/$iam.out"
29 _setenvironment
30
31 check=${check-true}
32
33 diff="diff -u"
34 verbose=false
35 debug=false
36 group=false
37 xgroup=false
38 imgopts=false
39 showme=false
40 sortme=false
41 expunge=true
42 have_test_arg=false
43 randomize=false
44 valgrind=false
45 cachemode=false
46 rm -f $tmp.list $tmp.tmp $tmp.sed
47
48 export IMGFMT=raw
49 export IMGFMT_GENERIC=true
50 export IMGPROTO=file
51 export IMGOPTS=""
52 export CACHEMODE="writeback"
53 export QEMU_IO_OPTIONS=""
54 export CACHEMODE_IS_DEFAULT=true
55
56 for r
57 do
58
59     if $group
60     then
61         # arg after -g
62         group_list=`sed -n <"$source_iotests/group" -e 's/$/ /' -e "/^[0-9][0-9][0-9].* $r /"'{
63 s/ .*//p
64 }'`
65         if [ -z "$group_list" ]
66         then
67             echo "Group \"$r\" is empty or not defined?"
68             exit 1
69         fi
70         [ ! -s $tmp.list ] && touch $tmp.list
71         for t in $group_list
72         do
73             if grep -s "^$t\$" $tmp.list >/dev/null
74             then
75                 :
76             else
77                 echo "$t" >>$tmp.list
78             fi
79         done
80         group=false
81         continue
82
83     elif $xgroup
84     then
85         # arg after -x
86         [ ! -s $tmp.list ] && ls [0-9][0-9][0-9] [0-9][0-9][0-9][0-9] >$tmp.list 2>/dev/null
87         group_list=`sed -n <"$source_iotests/group" -e 's/$/ /' -e "/^[0-9][0-9][0-9].* $r /"'{
88 s/ .*//p
89 }'`
90         if [ -z "$group_list" ]
91         then
92             echo "Group \"$r\" is empty or not defined?"
93             exit 1
94         fi
95         numsed=0
96         rm -f $tmp.sed
97         for t in $group_list
98         do
99             if [ $numsed -gt 100 ]
100             then
101                 sed -f $tmp.sed <$tmp.list >$tmp.tmp
102                 mv $tmp.tmp $tmp.list
103                 numsed=0
104                 rm -f $tmp.sed
105             fi
106             echo "/^$t\$/d" >>$tmp.sed
107             numsed=`expr $numsed + 1`
108         done
109         sed -f $tmp.sed <$tmp.list >$tmp.tmp
110         mv $tmp.tmp $tmp.list
111         xgroup=false
112         continue
113
114     elif $imgopts
115     then
116         IMGOPTS="$r"
117         imgopts=false
118         continue
119     elif $cachemode
120     then
121         CACHEMODE="$r"
122         CACHEMODE_IS_DEFAULT=false
123         cachemode=false
124         continue
125     fi
126
127     xpand=true
128     case "$r"
129     in
130
131         -\? | -h | --help)        # usage
132             echo "Usage: $0 [options] [testlist]"'
133
134 common options
135     -v                  verbose
136     -d                  debug
137
138 check options
139     -raw                test raw (default)
140     -bochs              test bochs
141     -cloop              test cloop
142     -parallels          test parallels
143     -qcow               test qcow
144     -qcow2              test qcow2
145     -qed                test qed
146     -vdi                test vdi
147     -vpc                test vpc
148     -vhdx               test vhdx
149     -vmdk               test vmdk
150     -file               test file (default)
151     -rbd                test rbd
152     -sheepdog           test sheepdog
153     -nbd                test nbd
154     -ssh                test ssh
155     -nfs                test nfs
156     -archipelago        test archipelago
157     -xdiff              graphical mode diff
158     -nocache            use O_DIRECT on backing file
159     -misalign           misalign memory allocations
160     -n                  show me, do not run tests
161     -o options          -o options to pass to qemu-img create/convert
162     -T                  output timestamps
163     -r                  randomize test order
164     -c mode             cache mode
165
166 testlist options
167     -g group[,group...]        include tests from these groups
168     -x group[,group...]        exclude tests from these groups
169     NNN                        include test NNN
170     NNN-NNN                    include test range (eg. 012-021)
171 '
172             exit 0
173             ;;
174
175         -raw)
176             IMGFMT=raw
177             xpand=false
178             ;;
179
180         -bochs)
181             IMGFMT=bochs
182             IMGFMT_GENERIC=false
183             xpand=false
184             ;;
185
186         -cloop)
187             IMGFMT=cloop
188             IMGFMT_GENERIC=false
189             xpand=false
190             ;;
191
192         -parallels)
193             IMGFMT=parallels
194             IMGFMT_GENERIC=false
195             xpand=false
196             ;;
197
198         -qcow)
199             IMGFMT=qcow
200             xpand=false
201             ;;
202
203         -qcow2)
204             IMGFMT=qcow2
205             xpand=false
206             ;;
207
208         -qed)
209             IMGFMT=qed
210             xpand=false
211             ;;
212
213         -vdi)
214             IMGFMT=vdi
215             xpand=false
216             ;;
217
218         -vmdk)
219             IMGFMT=vmdk
220             xpand=false
221             ;;
222
223         -vpc)
224             IMGFMT=vpc
225             xpand=false
226             ;;
227
228         -vhdx)
229             IMGFMT=vhdx
230             xpand=false
231             ;;
232
233         -file)
234             IMGPROTO=file
235             xpand=false
236             ;;
237
238         -rbd)
239             IMGPROTO=rbd
240             xpand=false
241             ;;
242
243         -sheepdog)
244             IMGPROTO=sheepdog
245             xpand=false
246             ;;
247
248         -nbd)
249             IMGPROTO=nbd
250             xpand=false
251             ;;
252
253         -ssh)
254             IMGPROTO=ssh
255             xpand=false
256             ;;
257
258         -nfs)
259             IMGPROTO=nfs
260             xpand=false
261             ;;
262
263         -archipelago)
264             IMGPROTO=archipelago
265             xpand=false
266             ;;
267
268         -nocache)
269             CACHEMODE="none"
270             CACHEMODE_IS_DEFAULT=false
271             xpand=false
272             ;;
273
274         -misalign)
275             QEMU_IO_OPTIONS="$QEMU_IO_OPTIONS --misalign"
276             xpand=false
277             ;;
278
279         -valgrind)
280             valgrind=true
281             xpand=false
282             ;;
283
284         -g)        # -g group ... pick from group file
285             group=true
286             xpand=false
287             ;;
288
289         -xdiff)        # graphical diff mode
290             xpand=false
291
292             if [ ! -z "$DISPLAY" ]
293             then
294                 command -v xdiff >/dev/null 2>&1 && diff=xdiff
295                 command -v gdiff >/dev/null 2>&1 && diff=gdiff
296                 command -v tkdiff >/dev/null 2>&1 && diff=tkdiff
297                 command -v xxdiff >/dev/null 2>&1 && diff=xxdiff
298             fi
299             ;;
300
301         -n)        # show me, don't do it
302             showme=true
303             xpand=false
304             ;;
305         -o)
306             imgopts=true
307             xpand=false
308             ;;
309         -c)
310             cachemode=true
311             xpand=false
312             ;;
313         -r)        # randomize test order
314             randomize=true
315             xpand=false
316             ;;
317
318         -T)        # turn on timestamp output
319             timestamp=true
320             xpand=false
321             ;;
322
323         -v)
324             verbose=true
325             xpand=false
326             ;;
327         -d)
328             debug=true
329             xpand=false
330             ;;
331         -x)        # -x group ... exclude from group file
332             xgroup=true
333             xpand=false
334             ;;
335         '[0-9][0-9][0-9] [0-9][0-9][0-9][0-9]')
336             echo "No tests?"
337             status=1
338             exit $status
339             ;;
340
341         [0-9]*-[0-9]*)
342             eval `echo $r | sed -e 's/^/start=/' -e 's/-/ end=/'`
343             ;;
344
345         [0-9]*-)
346             eval `echo $r | sed -e 's/^/start=/' -e 's/-//'`
347             end=`echo [0-9][0-9][0-9] [0-9][0-9][0-9][0-9] | sed -e 's/\[0-9]//g' -e 's/  *$//' -e 's/.* //'`
348             if [ -z "$end" ]
349             then
350                 echo "No tests in range \"$r\"?"
351                 status=1
352                 exit $status
353             fi
354             ;;
355
356         *)
357             start=$r
358             end=$r
359             ;;
360
361     esac
362
363     # get rid of leading 0s as can be interpreted as octal
364     start=`echo $start | sed 's/^0*//'`
365     end=`echo $end | sed 's/^0*//'`
366
367     if $xpand
368     then
369         have_test_arg=true
370         $AWK_PROG </dev/null '
371 BEGIN        { for (t='$start'; t<='$end'; t++) printf "%03d\n",t }' \
372         | while read id
373         do
374             if grep -s "^$id " "$source_iotests/group" >/dev/null
375             then
376                 # in group file ... OK
377                 echo $id >>$tmp.list
378             else
379                 if [ -f expunged ] && $expunge && egrep "^$id([         ]|\$)" expunged >/dev/null
380                 then
381                     # expunged ... will be reported, but not run, later
382                     echo $id >>$tmp.list
383                 else
384                     # oops
385                     if [ "$start" == "$end" -a "$id" == "$end" ]
386                     then
387                         echo "$id - unknown test"
388                         exit 1
389                     else
390                         echo "$id - unknown test, ignored"
391                     fi
392                 fi
393             fi
394         done || exit 1
395     fi
396
397 done
398
399 # Set qemu-io cache mode with $CACHEMODE we have
400 QEMU_IO_OPTIONS="$QEMU_IO_OPTIONS -f $IMGFMT --cache $CACHEMODE"
401
402 # Set default options for qemu-img create -o if they were not specified
403 _set_default_imgopts
404
405 if [ -s $tmp.list ]
406 then
407     # found some valid test numbers ... this is good
408     :
409 else
410     if $have_test_arg
411     then
412         # had test numbers, but none in group file ... do nothing
413         touch $tmp.list
414     else
415         # no test numbers, do everything from group file
416         sed -n -e '/^[0-9][0-9][0-9]*/s/[         ].*//p' <"$source_iotests/group" >$tmp.list
417     fi
418 fi
419
420 # should be sort -n, but this did not work for Linux when this
421 # was ported from IRIX
422 #
423 list=`sort $tmp.list`
424 rm -f $tmp.list $tmp.tmp $tmp.sed
425
426 if $randomize
427 then
428     list=`echo $list | awk -f randomize.awk`
429 fi
430
431 [ "$QEMU" = "" ] && _fatal "qemu not found"
432 [ "$QEMU_IMG" = "" ] && _fatal "qemu-img not found"
433 [ "$QEMU_IO" = "" ] && _fatal "qemu-io not found"
434
435 if [ "$IMGPROTO" = "nbd" ] ; then
436     [ "$QEMU_NBD" = "" ] && _fatal "qemu-nbd not found"
437 fi
438
439 if $valgrind; then
440     export REAL_QEMU_IO="$QEMU_IO_PROG"
441     export QEMU_IO_PROG=valgrind_qemu_io
442 fi