]> Git Repo - qemu.git/blame - tests/qemu-iotests/common.rc
qcow2: Check refcount table size (CVE-2014-0144)
[qemu.git] / tests / qemu-iotests / common.rc
CommitLineData
908eaf68 1#!/bin/bash
6bf19c94
CH
2#
3# Copyright (C) 2009 Red Hat, Inc.
4# Copyright (c) 2000-2006 Silicon Graphics, Inc. All Rights Reserved.
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
e8c212d6 17# along with this program. If not, see <http://www.gnu.org/licenses/>.
6bf19c94
CH
18#
19
20dd()
21{
22 if [ "$HOSTOS" == "Linux" ]
79e40ab1
KW
23 then
24 command dd --help | grep noxfer > /dev/null 2>&1
25
26 if [ "$?" -eq 0 ]
27 then
28 command dd status=noxfer $@
29 else
30 command dd $@
31 fi
6bf19c94 32 else
79e40ab1 33 command dd $@
6bf19c94
CH
34 fi
35}
36
23ea2ecc
SH
37# poke_file 'test.img' 512 '\xff\xfe'
38poke_file()
39{
40 printf "$3" | dd "of=$1" bs=1 "seek=$2" conv=notrunc &>/dev/null
41}
42
6bf19c94
CH
43# we need common.config
44if [ "$iam" != "check" ]
45then
46 if ! . ./common.config
47 then
48 echo "$iam: failed to source common.config"
49 exit 1
50 fi
51fi
52
53# make sure we have a standard umask
54umask 022
55
9cdfa1b3
MK
56if [ "$IMGPROTO" = "file" ]; then
57 TEST_IMG=$TEST_DIR/t.$IMGFMT
a9660664
NT
58elif [ "$IMGPROTO" = "nbd" ]; then
59 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
60 TEST_IMG="nbd:127.0.0.1:10810"
342809e8
RJ
61elif [ "$IMGPROTO" = "ssh" ]; then
62 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
63 TEST_IMG="ssh://127.0.0.1$TEST_IMG_FILE"
170632db
PL
64elif [ "$IMGPROTO" = "nfs" ]; then
65 TEST_DIR="nfs://127.0.0.1/$TEST_DIR"
66 TEST_IMG=$TEST_DIR/t.$IMGFMT
9cdfa1b3
MK
67else
68 TEST_IMG=$IMGPROTO:$TEST_DIR/t.$IMGFMT
69fi
6bf19c94 70
2f24e8fb
KW
71function valgrind_qemu_io()
72{
73 valgrind --log-file=/tmp/$$.valgrind --error-exitcode=99 $REAL_QEMU_IO "$@"
74 if [ $? != 0 ]; then
75 cat /tmp/$$.valgrind
76 fi
77 rm -f /tmp/$$.valgrind
78}
79
80
89004368
KW
81_optstr_add()
82{
83 if [ -n "$1" ]; then
84 echo "$1,$2"
85 else
86 echo "$2"
87 fi
88}
89
90_set_default_imgopts()
91{
92 if [ "$IMGFMT" == "qcow2" ] && ! (echo "$IMGOPTS" | grep "compat=" > /dev/null); then
93 IMGOPTS=$(_optstr_add "$IMGOPTS" "compat=1.1")
94 fi
95}
96
85edbd37
JC
97_use_sample_img()
98{
99 SAMPLE_IMG_FILE="${1%\.bz2}"
100 TEST_IMG="$TEST_DIR/$SAMPLE_IMG_FILE"
101 bzcat "$SAMPLE_IMG_DIR/$1" > "$TEST_IMG"
102 if [ $? -ne 0 ]
103 then
104 echo "_use_sample_img error, cannot extract '$SAMPLE_IMG_DIR/$1'"
105 exit 1
106 fi
107}
108
6bf19c94
CH
109_make_test_img()
110{
111 # extra qemu-img options can be added by tests
112 # at least one argument (the image size) needs to be added
21af8148 113 local extra_img_options=""
21af8148 114 local image_size=$*
89004368 115 local optstr=""
a9660664 116 local img_name=""
0018c03f
JC
117 local use_backing=0
118 local backing_file=""
a9660664
NT
119
120 if [ -n "$TEST_IMG_FILE" ]; then
121 img_name=$TEST_IMG_FILE
122 else
123 img_name=$TEST_IMG
124 fi
89004368
KW
125
126 if [ -n "$IMGOPTS" ]; then
127 optstr=$(_optstr_add "$optstr" "$IMGOPTS")
128 fi
6bf19c94 129
21af8148 130 if [ "$1" = "-b" ]; then
0018c03f
JC
131 use_backing=1
132 backing_file=$2
21af8148
SW
133 image_size=$3
134 fi
f5a4bbd9 135 if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then
89004368
KW
136 optstr=$(_optstr_add "$optstr" "cluster_size=$CLUSTER_SIZE")
137 fi
138
139 if [ -n "$optstr" ]; then
140 extra_img_options="-o $optstr $extra_img_options"
8fc1024c
KW
141 fi
142
6bf19c94 143 # XXX(hch): have global image options?
0018c03f
JC
144 (
145 if [ $use_backing = 1 ]; then
146 $QEMU_IMG create -f $IMGFMT $extra_img_options -b "$backing_file" "$img_name" $image_size 2>&1
147 else
148 $QEMU_IMG create -f $IMGFMT $extra_img_options "$img_name" $image_size 2>&1
149 fi
150 ) | \
353a41be
KW
151 sed -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \
152 -e "s#$TEST_DIR#TEST_DIR#g" \
153 -e "s#$IMGFMT#IMGFMT#g" \
154 -e "s# encryption=off##g" \
155 -e "s# cluster_size=[0-9]\\+##g" \
156 -e "s# table_size=[0-9]\\+##g" \
157 -e "s# compat='[^']*'##g" \
158 -e "s# compat6=\\(on\\|off\\)##g" \
159 -e "s# static=\\(on\\|off\\)##g" \
50522d96
FZ
160 -e "s# zeroed_grain=\\(on\\|off\\)##g" \
161 -e "s# subformat='[^']*'##g" \
86abefd6 162 -e "s# adapter_type='[^']*'##g" \
228b234d
JC
163 -e "s# lazy_refcounts=\\(on\\|off\\)##g" \
164 -e "s# block_size=[0-9]\\+##g" \
165 -e "s# block_state_zero=\\(on\\|off\\)##g" \
166 -e "s# log_size=[0-9]\\+##g"
a9660664
NT
167
168 # Start an NBD server on the image file, which is what we'll be talking to
169 if [ $IMGPROTO = "nbd" ]; then
170 eval "$QEMU_NBD -v -t -b 127.0.0.1 -p 10810 $TEST_IMG_FILE &"
171 QEMU_NBD_PID=$!
172 sleep 1 # FIXME: qemu-nbd needs to be listening before we continue
173 fi
6bf19c94
CH
174}
175
487c1910
FZ
176_rm_test_img()
177{
178 local img=$1
179 if [ "$IMGFMT" = "vmdk" ]; then
180 # Remove all the extents for vmdk
181 $QEMU_IMG info $img 2>/dev/null | grep 'filename:' | cut -f 2 -d: \
182 | xargs -I {} rm -f "{}"
183 fi
184 rm -f $img
185}
186
6bf19c94
CH
187_cleanup_test_img()
188{
9cdfa1b3
MK
189 case "$IMGPROTO" in
190
a9660664
NT
191 nbd)
192 kill $QEMU_NBD_PID
fef9c191 193 rm -f "$TEST_IMG_FILE"
a9660664 194 ;;
9cdfa1b3 195 file)
487c1910
FZ
196 _rm_test_img "$TEST_DIR/t.$IMGFMT"
197 _rm_test_img "$TEST_DIR/t.$IMGFMT.orig"
198 _rm_test_img "$TEST_DIR/t.$IMGFMT.base"
85edbd37
JC
199 if [ -n "$SAMPLE_IMG_FILE" ]
200 then
201 rm -f "$TEST_DIR/$SAMPLE_IMG_FILE"
202 fi
9cdfa1b3
MK
203 ;;
204
205 rbd)
9147d019 206 rbd --no-progress rm "$TEST_DIR/t.$IMGFMT" > /dev/null
9cdfa1b3
MK
207 ;;
208
209 sheepdog)
fef9c191 210 collie vdi delete "$TEST_DIR/t.$IMGFMT"
9cdfa1b3
MK
211 ;;
212
213 esac
6bf19c94
CH
214}
215
216_check_test_img()
217{
fef9c191 218 $QEMU_IMG check "$@" -f $IMGFMT "$TEST_IMG" 2>&1 | _filter_testdir | \
e6439d78 219 sed -e '/allocated.*fragmented.*compressed clusters/d' \
c6bb9ad1
FS
220 -e 's/qemu-img: This image format does not support checks/No errors were found on the image./' \
221 -e '/Image end offset: [0-9]\+/d'
6bf19c94
CH
222}
223
514d9da5
SH
224_img_info()
225{
4c2e9465
HR
226 discard=0
227 regex_json_spec_start='^ *"format-specific": \{'
fef9c191 228 $QEMU_IMG info "$@" "$TEST_IMG" 2>&1 | \
514d9da5
SH
229 sed -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \
230 -e "s#$TEST_DIR#TEST_DIR#g" \
231 -e "s#$IMGFMT#IMGFMT#g" \
232 -e "/^disk size:/ D" \
4c2e9465
HR
233 -e "/actual-size/ D" | \
234 while IFS='' read line; do
235 if [[ $line == "Format specific information:" ]]; then
236 discard=1
237 elif [[ $line =~ $regex_json_spec_start ]]; then
238 discard=2
239 regex_json_spec_end="^${line%%[^ ]*}\\},? *$"
240 fi
241 if [[ $discard == 0 ]]; then
242 echo "$line"
243 elif [[ $discard == 1 && ! $line ]]; then
244 echo
245 discard=0
246 elif [[ $discard == 2 && $line =~ $regex_json_spec_end ]]; then
247 discard=0
248 fi
249 done
514d9da5
SH
250}
251
6bf19c94
CH
252_get_pids_by_name()
253{
254 if [ $# -ne 1 ]
255 then
79e40ab1
KW
256 echo "Usage: _get_pids_by_name process-name" 1>&2
257 exit 1
6bf19c94
CH
258 fi
259
260 # Algorithm ... all ps(1) variants have a time of the form MM:SS or
261 # HH:MM:SS before the psargs field, use this as the search anchor.
262 #
263 # Matches with $1 (process-name) occur if the first psarg is $1
264 # or ends in /$1 ... the matching uses sed's regular expressions,
265 # so passing a regex into $1 will work.
266
267 ps $PS_ALL_FLAGS \
268 | sed -n \
79e40ab1
KW
269 -e 's/$/ /' \
270 -e 's/[ ][ ]*/ /g' \
271 -e 's/^ //' \
272 -e 's/^[^ ]* //' \
273 -e "/[0-9]:[0-9][0-9] *[^ ]*\/$1 /s/ .*//p" \
274 -e "/[0-9]:[0-9][0-9] *$1 /s/ .*//p"
6bf19c94
CH
275}
276
277# fqdn for localhost
278#
279_get_fqdn()
280{
281 host=`hostname`
282 $NSLOOKUP_PROG $host | $AWK_PROG '{ if ($1 == "Name:") print $2 }'
283}
284
285# check if run as root
286#
287_need_to_be_root()
288{
289 id=`id | $SED_PROG -e 's/(.*//' -e 's/.*=//'`
290 if [ "$id" -ne 0 ]
291 then
79e40ab1
KW
292 echo "Arrgh ... you need to be root (not uid=$id) to run this test"
293 exit 1
6bf19c94
CH
294 fi
295}
296
297
298# Do a command, log it to $seq.full, optionally test return status
299# and die if command fails. If called with one argument _do executes the
300# command, logs it, and returns its exit status. With two arguments _do
301# first prints the message passed in the first argument, and then "done"
302# or "fail" depending on the return status of the command passed in the
303# second argument. If the command fails and the variable _do_die_on_error
304# is set to "always" or the two argument form is used and _do_die_on_error
305# is set to "message_only" _do will print an error message to
306# $seq.out and exit.
307
308_do()
309{
310 if [ $# -eq 1 ]; then
79e40ab1 311 _cmd=$1
6bf19c94 312 elif [ $# -eq 2 ]; then
79e40ab1
KW
313 _note=$1
314 _cmd=$2
315 echo -n "$_note... "
6bf19c94 316 else
79e40ab1
KW
317 echo "Usage: _do [note] cmd" 1>&2
318 status=1; exit
6bf19c94
CH
319 fi
320
321 (eval "echo '---' \"$_cmd\"") >>$here/$seq.full
322 (eval "$_cmd") >$tmp._out 2>&1; ret=$?
323 cat $tmp._out >>$here/$seq.full
324 if [ $# -eq 2 ]; then
79e40ab1
KW
325 if [ $ret -eq 0 ]; then
326 echo "done"
327 else
328 echo "fail"
329 fi
6bf19c94
CH
330 fi
331 if [ $ret -ne 0 ] \
79e40ab1
KW
332 && [ "$_do_die_on_error" = "always" \
333 -o \( $# -eq 2 -a "$_do_die_on_error" = "message_only" \) ]
6bf19c94 334 then
79e40ab1
KW
335 [ $# -ne 2 ] && echo
336 eval "echo \"$_cmd\" failed \(returned $ret\): see $seq.full"
337 status=1; exit
6bf19c94
CH
338 fi
339
340 return $ret
341}
342
343# bail out, setting up .notrun file
344#
345_notrun()
346{
347 echo "$*" >$seq.notrun
348 echo "$seq not run: $*"
349 status=0
350 exit
351}
352
353# just plain bail out
354#
355_fail()
356{
357 echo "$*" | tee -a $here/$seq.full
358 echo "(see $seq.full for details)"
359 status=1
360 exit 1
361}
362
363# tests whether $IMGFMT is one of the supported image formats for a test
364#
365_supported_fmt()
366{
47f73da0
SH
367 # "generic" is suitable for most image formats. For some formats it doesn't
368 # work, however (most notably read-only formats), so they can opt out by
369 # setting IMGFMT_GENERIC to false.
6bf19c94 370 for f; do
89e91181 371 if [ "$f" = "$IMGFMT" -o "$f" = "generic" -a "$IMGFMT_GENERIC" = "true" ]; then
79e40ab1
KW
372 return
373 fi
6bf19c94
CH
374 done
375
376 _notrun "not suitable for this image format: $IMGFMT"
377}
378
9cdfa1b3
MK
379# tests whether $IMGPROTO is one of the supported image protocols for a test
380#
381_supported_proto()
382{
383 for f; do
79e40ab1
KW
384 if [ "$f" = "$IMGPROTO" -o "$f" = "generic" ]; then
385 return
386 fi
9cdfa1b3
MK
387 done
388
389 _notrun "not suitable for this image protocol: $IMGPROTO"
390}
391
6bf19c94
CH
392# tests whether the host OS is one of the supported OSes for a test
393#
394_supported_os()
395{
396 for h
397 do
79e40ab1
KW
398 if [ "$h" = "$HOSTOS" ]
399 then
400 return
401 fi
6bf19c94
CH
402 done
403
404 _notrun "not suitable for this OS: $HOSTOS"
405}
406
f210a83c 407_supported_cache_modes()
166f3c7b 408{
f210a83c
FZ
409 for mode; do
410 if [ "$mode" = "$CACHEMODE" ]; then
411 return
412 fi
166f3c7b 413 done
f210a83c
FZ
414 _notrun "not suitable for cache mode: $CACHEMODE"
415}
416
417_default_cache_mode()
418{
419 if $CACHEMODE_IS_DEFAULT; then
420 CACHEMODE="$1"
421 QEMU_IO="$QEMU_IO --cache $1"
422 return
423 fi
166f3c7b
SH
424}
425
2c77f52e
FZ
426_unsupported_imgopts()
427{
428 for bad_opt
429 do
430 if echo "$IMGOPTS" | grep -q 2>/dev/null "$bad_opt"
431 then
432 _notrun "not suitable for image option: $bad_opt"
433 fi
434 done
435}
436
6bf19c94
CH
437# this test requires that a specified command (executable) exists
438#
439_require_command()
440{
9c468a01
WX
441 eval c=\$$1
442 [ -x "$c" ] || _notrun "$1 utility required, skipped this test"
6bf19c94
CH
443}
444
445_full_imgfmt_details()
446{
89004368
KW
447 if [ -n "$IMGOPTS" ]; then
448 echo "$IMGFMT ($IMGOPTS)"
449 else
450 echo "$IMGFMT"
451 fi
6bf19c94
CH
452}
453
9cdfa1b3
MK
454_full_imgproto_details()
455{
456 echo "$IMGPROTO"
457}
458
6bf19c94
CH
459_full_platform_details()
460{
461 os=`uname -s`
462 host=`hostname -s`
463 kernel=`uname -r`
464 platform=`uname -m`
465 echo "$os/$platform $host $kernel"
466}
467
468_link_out_file()
469{
470 if [ -z "$1" ]; then
471 echo Error must pass \$seq.
472 exit
473 fi
474 rm -f $1
475 if [ "`uname`" == "IRIX64" ] || [ "`uname`" == "IRIX" ]; then
476 ln -s $1.irix $1
477 elif [ "`uname`" == "Linux" ]; then
478 ln -s $1.linux $1
479 else
480 echo Error test $seq does not run on the operating system: `uname`
481 exit
482 fi
483}
484
485_die()
486{
487 echo $@
488 exit 1
489}
490
491# make sure this script returns success
492/bin/true
This page took 0.334003 seconds and 4 git commands to generate.