qemu-iotests: Add _default_cache_mode and _supported_cache_modes
[qemu/kevin.git] / tests / qemu-iotests / common.rc
blob47cef6dbe47f24d7e57d6dc7e25d1f0cbd20e6b0
1 #!/bin/bash
3 # Copyright (C) 2009 Red Hat, Inc.
4 # Copyright (c) 2000-2006 Silicon Graphics, Inc. All Rights Reserved.
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.
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.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 dd()
22 if [ "$HOSTOS" == "Linux" ]
23 then
24 command dd --help | grep noxfer > /dev/null 2>&1
26 if [ "$?" -eq 0 ]
27 then
28 command dd status=noxfer $@
29 else
30 command dd $@
32 else
33 command dd $@
37 # poke_file 'test.img' 512 '\xff\xfe'
38 poke_file()
40 printf "$3" | dd "of=$1" bs=1 "seek=$2" conv=notrunc &>/dev/null
43 # we need common.config
44 if [ "$iam" != "check" ]
45 then
46 if ! . ./common.config
47 then
48 echo "$iam: failed to source common.config"
49 exit 1
53 # make sure we have a standard umask
54 umask 022
56 if [ "$IMGPROTO" = "file" ]; then
57 TEST_IMG=$TEST_DIR/t.$IMGFMT
58 elif [ "$IMGPROTO" = "nbd" ]; then
59 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
60 TEST_IMG="nbd:127.0.0.1:10810"
61 elif [ "$IMGPROTO" = "ssh" ]; then
62 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
63 TEST_IMG="ssh://127.0.0.1$TEST_IMG_FILE"
64 else
65 TEST_IMG=$IMGPROTO:$TEST_DIR/t.$IMGFMT
68 function valgrind_qemu_io()
70 valgrind --log-file=/tmp/$$.valgrind --error-exitcode=99 $REAL_QEMU_IO "$@"
71 if [ $? != 0 ]; then
72 cat /tmp/$$.valgrind
74 rm -f /tmp/$$.valgrind
78 _optstr_add()
80 if [ -n "$1" ]; then
81 echo "$1,$2"
82 else
83 echo "$2"
87 _set_default_imgopts()
89 if [ "$IMGFMT" == "qcow2" ] && ! (echo "$IMGOPTS" | grep "compat=" > /dev/null); then
90 IMGOPTS=$(_optstr_add "$IMGOPTS" "compat=1.1")
94 _use_sample_img()
96 SAMPLE_IMG_FILE="${1%\.bz2}"
97 TEST_IMG="$TEST_DIR/$SAMPLE_IMG_FILE"
98 bzcat "$SAMPLE_IMG_DIR/$1" > "$TEST_IMG"
99 if [ $? -ne 0 ]
100 then
101 echo "_use_sample_img error, cannot extract '$SAMPLE_IMG_DIR/$1'"
102 exit 1
106 _make_test_img()
108 # extra qemu-img options can be added by tests
109 # at least one argument (the image size) needs to be added
110 local extra_img_options=""
111 local image_size=$*
112 local optstr=""
113 local img_name=""
114 local use_backing=0
115 local backing_file=""
117 if [ -n "$TEST_IMG_FILE" ]; then
118 img_name=$TEST_IMG_FILE
119 else
120 img_name=$TEST_IMG
123 if [ -n "$IMGOPTS" ]; then
124 optstr=$(_optstr_add "$optstr" "$IMGOPTS")
127 if [ "$1" = "-b" ]; then
128 use_backing=1
129 backing_file=$2
130 image_size=$3
132 if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then
133 optstr=$(_optstr_add "$optstr" "cluster_size=$CLUSTER_SIZE")
136 if [ -n "$optstr" ]; then
137 extra_img_options="-o $optstr $extra_img_options"
140 # XXX(hch): have global image options?
142 if [ $use_backing = 1 ]; then
143 $QEMU_IMG create -f $IMGFMT $extra_img_options -b "$backing_file" "$img_name" $image_size 2>&1
144 else
145 $QEMU_IMG create -f $IMGFMT $extra_img_options "$img_name" $image_size 2>&1
147 ) | \
148 sed -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \
149 -e "s#$TEST_DIR#TEST_DIR#g" \
150 -e "s#$IMGFMT#IMGFMT#g" \
151 -e "s# encryption=off##g" \
152 -e "s# cluster_size=[0-9]\\+##g" \
153 -e "s# table_size=[0-9]\\+##g" \
154 -e "s# compat='[^']*'##g" \
155 -e "s# compat6=\\(on\\|off\\)##g" \
156 -e "s# static=\\(on\\|off\\)##g" \
157 -e "s# zeroed_grain=\\(on\\|off\\)##g" \
158 -e "s# subformat='[^']*'##g" \
159 -e "s# adapter_type='[^']*'##g" \
160 -e "s# lazy_refcounts=\\(on\\|off\\)##g" \
161 -e "s# block_size=[0-9]\\+##g" \
162 -e "s# block_state_zero=\\(on\\|off\\)##g" \
163 -e "s# log_size=[0-9]\\+##g"
165 # Start an NBD server on the image file, which is what we'll be talking to
166 if [ $IMGPROTO = "nbd" ]; then
167 eval "$QEMU_NBD -v -t -b 127.0.0.1 -p 10810 $TEST_IMG_FILE &"
168 QEMU_NBD_PID=$!
169 sleep 1 # FIXME: qemu-nbd needs to be listening before we continue
173 _cleanup_test_img()
175 case "$IMGPROTO" in
177 nbd)
178 kill $QEMU_NBD_PID
179 rm -f "$TEST_IMG_FILE"
181 file)
182 rm -f "$TEST_DIR/t.$IMGFMT"
183 rm -f "$TEST_DIR/t.$IMGFMT.orig"
184 rm -f "$TEST_DIR/t.$IMGFMT.base"
185 if [ -n "$SAMPLE_IMG_FILE" ]
186 then
187 rm -f "$TEST_DIR/$SAMPLE_IMG_FILE"
191 rbd)
192 rbd rm "$TEST_DIR/t.$IMGFMT" > /dev/null
195 sheepdog)
196 collie vdi delete "$TEST_DIR/t.$IMGFMT"
199 esac
202 _check_test_img()
204 $QEMU_IMG check "$@" -f $IMGFMT "$TEST_IMG" 2>&1 | _filter_testdir | \
205 sed -e '/allocated.*fragmented.*compressed clusters/d' \
206 -e 's/qemu-img: This image format does not support checks/No errors were found on the image./' \
207 -e '/Image end offset: [0-9]\+/d'
210 _img_info()
212 discard=0
213 regex_json_spec_start='^ *"format-specific": \{'
214 $QEMU_IMG info "$@" "$TEST_IMG" 2>&1 | \
215 sed -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \
216 -e "s#$TEST_DIR#TEST_DIR#g" \
217 -e "s#$IMGFMT#IMGFMT#g" \
218 -e "/^disk size:/ D" \
219 -e "/actual-size/ D" | \
220 while IFS='' read line; do
221 if [[ $line == "Format specific information:" ]]; then
222 discard=1
223 elif [[ $line =~ $regex_json_spec_start ]]; then
224 discard=2
225 regex_json_spec_end="^${line%%[^ ]*}\\},? *$"
227 if [[ $discard == 0 ]]; then
228 echo "$line"
229 elif [[ $discard == 1 && ! $line ]]; then
230 echo
231 discard=0
232 elif [[ $discard == 2 && $line =~ $regex_json_spec_end ]]; then
233 discard=0
235 done
238 _get_pids_by_name()
240 if [ $# -ne 1 ]
241 then
242 echo "Usage: _get_pids_by_name process-name" 1>&2
243 exit 1
246 # Algorithm ... all ps(1) variants have a time of the form MM:SS or
247 # HH:MM:SS before the psargs field, use this as the search anchor.
249 # Matches with $1 (process-name) occur if the first psarg is $1
250 # or ends in /$1 ... the matching uses sed's regular expressions,
251 # so passing a regex into $1 will work.
253 ps $PS_ALL_FLAGS \
254 | sed -n \
255 -e 's/$/ /' \
256 -e 's/[ ][ ]*/ /g' \
257 -e 's/^ //' \
258 -e 's/^[^ ]* //' \
259 -e "/[0-9]:[0-9][0-9] *[^ ]*\/$1 /s/ .*//p" \
260 -e "/[0-9]:[0-9][0-9] *$1 /s/ .*//p"
263 # fqdn for localhost
265 _get_fqdn()
267 host=`hostname`
268 $NSLOOKUP_PROG $host | $AWK_PROG '{ if ($1 == "Name:") print $2 }'
271 # check if run as root
273 _need_to_be_root()
275 id=`id | $SED_PROG -e 's/(.*//' -e 's/.*=//'`
276 if [ "$id" -ne 0 ]
277 then
278 echo "Arrgh ... you need to be root (not uid=$id) to run this test"
279 exit 1
284 # Do a command, log it to $seq.full, optionally test return status
285 # and die if command fails. If called with one argument _do executes the
286 # command, logs it, and returns its exit status. With two arguments _do
287 # first prints the message passed in the first argument, and then "done"
288 # or "fail" depending on the return status of the command passed in the
289 # second argument. If the command fails and the variable _do_die_on_error
290 # is set to "always" or the two argument form is used and _do_die_on_error
291 # is set to "message_only" _do will print an error message to
292 # $seq.out and exit.
294 _do()
296 if [ $# -eq 1 ]; then
297 _cmd=$1
298 elif [ $# -eq 2 ]; then
299 _note=$1
300 _cmd=$2
301 echo -n "$_note... "
302 else
303 echo "Usage: _do [note] cmd" 1>&2
304 status=1; exit
307 (eval "echo '---' \"$_cmd\"") >>$here/$seq.full
308 (eval "$_cmd") >$tmp._out 2>&1; ret=$?
309 cat $tmp._out >>$here/$seq.full
310 if [ $# -eq 2 ]; then
311 if [ $ret -eq 0 ]; then
312 echo "done"
313 else
314 echo "fail"
317 if [ $ret -ne 0 ] \
318 && [ "$_do_die_on_error" = "always" \
319 -o \( $# -eq 2 -a "$_do_die_on_error" = "message_only" \) ]
320 then
321 [ $# -ne 2 ] && echo
322 eval "echo \"$_cmd\" failed \(returned $ret\): see $seq.full"
323 status=1; exit
326 return $ret
329 # bail out, setting up .notrun file
331 _notrun()
333 echo "$*" >$seq.notrun
334 echo "$seq not run: $*"
335 status=0
336 exit
339 # just plain bail out
341 _fail()
343 echo "$*" | tee -a $here/$seq.full
344 echo "(see $seq.full for details)"
345 status=1
346 exit 1
349 # tests whether $IMGFMT is one of the supported image formats for a test
351 _supported_fmt()
353 for f; do
354 if [ "$f" = "$IMGFMT" -o "$f" = "generic" -a "$IMGFMT_GENERIC" = "true" ]; then
355 return
357 done
359 _notrun "not suitable for this image format: $IMGFMT"
362 # tests whether $IMGPROTO is one of the supported image protocols for a test
364 _supported_proto()
366 for f; do
367 if [ "$f" = "$IMGPROTO" -o "$f" = "generic" ]; then
368 return
370 done
372 _notrun "not suitable for this image protocol: $IMGPROTO"
375 # tests whether the host OS is one of the supported OSes for a test
377 _supported_os()
379 for h
381 if [ "$h" = "$HOSTOS" ]
382 then
383 return
385 done
387 _notrun "not suitable for this OS: $HOSTOS"
390 _supported_cache_modes()
392 for mode; do
393 if [ "$mode" = "$CACHEMODE" ]; then
394 return
396 done
397 _notrun "not suitable for cache mode: $CACHEMODE"
400 _default_cache_mode()
402 if $CACHEMODE_IS_DEFAULT; then
403 CACHEMODE="$1"
404 QEMU_IO="$QEMU_IO --cache $1"
405 return
409 # this test requires that a specified command (executable) exists
411 _require_command()
413 [ -x "$1" ] || _notrun "$1 utility required, skipped this test"
416 _full_imgfmt_details()
418 if [ -n "$IMGOPTS" ]; then
419 echo "$IMGFMT ($IMGOPTS)"
420 else
421 echo "$IMGFMT"
425 _full_imgproto_details()
427 echo "$IMGPROTO"
430 _full_platform_details()
432 os=`uname -s`
433 host=`hostname -s`
434 kernel=`uname -r`
435 platform=`uname -m`
436 echo "$os/$platform $host $kernel"
439 _link_out_file()
441 if [ -z "$1" ]; then
442 echo Error must pass \$seq.
443 exit
445 rm -f $1
446 if [ "`uname`" == "IRIX64" ] || [ "`uname`" == "IRIX" ]; then
447 ln -s $1.irix $1
448 elif [ "`uname`" == "Linux" ]; then
449 ln -s $1.linux $1
450 else
451 echo Error test $seq does not run on the operating system: `uname`
452 exit
456 _die()
458 echo $@
459 exit 1
462 # make sure this script returns success
463 /bin/true