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