hw/arm/virt: Set minimum_page_bits to 12
[qemu/ar7.git] / tests / qemu-iotests / common.rc
blob126bd67043ee4948bcd4be8766fc7008fda023e1
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 [ "$IMGOPTSSYNTAX" = "true" ]; then
57 DRIVER="driver=$IMGFMT"
58 if [ "$IMGFMT" = "luks" ]; then
59 DRIVER="$DRIVER,key-secret=keysec0"
61 if [ "$IMGPROTO" = "file" ]; then
62 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
63 TEST_IMG="$DRIVER,file.filename=$TEST_DIR/t.$IMGFMT"
64 elif [ "$IMGPROTO" = "nbd" ]; then
65 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
66 TEST_IMG="$DRIVER,file.driver=nbd,file.host=127.0.0.1,file.port=10810"
67 elif [ "$IMGPROTO" = "ssh" ]; then
68 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
69 TEST_IMG="$DRIVER,file.driver=ssh,file.host=127.0.0.1,file.path=$TEST_IMG_FILE"
70 elif [ "$IMGPROTO" = "nfs" ]; then
71 TEST_DIR="$DRIVER,file.driver=nfs,file.filename=nfs://127.0.0.1/$TEST_DIR"
72 TEST_IMG=$TEST_DIR_OPTS/t.$IMGFMT
73 elif [ "$IMGPROTO" = "archipelago" ]; then
74 TEST_IMG="$DRIVER,file.driver=archipelago,file.volume=:at.$IMGFMT"
75 else
76 TEST_IMG="$DRIVER,file.driver=$IMGPROTO,file.filename=$TEST_DIR/t.$IMGFMT"
78 else
79 if [ "$IMGPROTO" = "file" ]; then
80 TEST_IMG=$TEST_DIR/t.$IMGFMT
81 elif [ "$IMGPROTO" = "nbd" ]; then
82 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
83 TEST_IMG="nbd:127.0.0.1:10810"
84 elif [ "$IMGPROTO" = "ssh" ]; then
85 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
86 TEST_IMG="ssh://127.0.0.1$TEST_IMG_FILE"
87 elif [ "$IMGPROTO" = "nfs" ]; then
88 TEST_DIR="nfs://127.0.0.1/$TEST_DIR"
89 TEST_IMG=$TEST_DIR/t.$IMGFMT
90 elif [ "$IMGPROTO" = "archipelago" ]; then
91 TEST_IMG="archipelago:at.$IMGFMT"
92 else
93 TEST_IMG=$IMGPROTO:$TEST_DIR/t.$IMGFMT
97 _optstr_add()
99 if [ -n "$1" ]; then
100 echo "$1,$2"
101 else
102 echo "$2"
106 _set_default_imgopts()
108 if [ "$IMGFMT" == "qcow2" ] && ! (echo "$IMGOPTS" | grep "compat=" > /dev/null); then
109 IMGOPTS=$(_optstr_add "$IMGOPTS" "compat=1.1")
113 _use_sample_img()
115 SAMPLE_IMG_FILE="${1%\.bz2}"
116 TEST_IMG="$TEST_DIR/$SAMPLE_IMG_FILE"
117 bzcat "$SAMPLE_IMG_DIR/$1" > "$TEST_IMG"
118 if [ $? -ne 0 ]
119 then
120 echo "_use_sample_img error, cannot extract '$SAMPLE_IMG_DIR/$1'"
121 exit 1
125 _make_test_img()
127 # extra qemu-img options can be added by tests
128 # at least one argument (the image size) needs to be added
129 local extra_img_options=""
130 local image_size=$*
131 local optstr=""
132 local img_name=""
133 local use_backing=0
134 local backing_file=""
135 local object_options=""
137 if [ -n "$TEST_IMG_FILE" ]; then
138 img_name=$TEST_IMG_FILE
139 else
140 img_name=$TEST_IMG
143 if [ -n "$IMGOPTS" ]; then
144 optstr=$(_optstr_add "$optstr" "$IMGOPTS")
146 if [ -n "$IMGKEYSECRET" ]; then
147 object_options="--object secret,id=keysec0,data=$IMGKEYSECRET"
148 optstr=$(_optstr_add "$optstr" "key-secret=keysec0")
151 if [ "$1" = "-b" ]; then
152 use_backing=1
153 backing_file=$2
154 image_size=$3
156 if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then
157 optstr=$(_optstr_add "$optstr" "cluster_size=$CLUSTER_SIZE")
160 if [ -n "$optstr" ]; then
161 extra_img_options="-o $optstr $extra_img_options"
164 # XXX(hch): have global image options?
166 if [ $use_backing = 1 ]; then
167 $QEMU_IMG create $object_options -f $IMGFMT $extra_img_options -b "$backing_file" "$img_name" $image_size 2>&1
168 else
169 $QEMU_IMG create $object_options -f $IMGFMT $extra_img_options "$img_name" $image_size 2>&1
171 ) | _filter_img_create
173 # Start an NBD server on the image file, which is what we'll be talking to
174 if [ $IMGPROTO = "nbd" ]; then
175 eval "$QEMU_NBD -v -t -b 127.0.0.1 -p 10810 -f $IMGFMT $TEST_IMG_FILE &"
176 sleep 1 # FIXME: qemu-nbd needs to be listening before we continue
180 _rm_test_img()
182 local img=$1
183 if [ "$IMGFMT" = "vmdk" ]; then
184 # Remove all the extents for vmdk
185 "$QEMU_IMG" info "$img" 2>/dev/null | grep 'filename:' | cut -f 2 -d: \
186 | xargs -I {} rm -f "{}"
188 rm -f "$img"
191 _cleanup_test_img()
193 case "$IMGPROTO" in
195 nbd)
196 if [ -f "${TEST_DIR}/qemu-nbd.pid" ]; then
197 local QEMU_NBD_PID
198 read QEMU_NBD_PID < "${TEST_DIR}/qemu-nbd.pid"
199 kill ${QEMU_NBD_PID}
200 rm -f "${TEST_DIR}/qemu-nbd.pid"
202 rm -f "$TEST_IMG_FILE"
204 file)
205 _rm_test_img "$TEST_DIR/t.$IMGFMT"
206 _rm_test_img "$TEST_DIR/t.$IMGFMT.orig"
207 _rm_test_img "$TEST_DIR/t.$IMGFMT.base"
208 if [ -n "$SAMPLE_IMG_FILE" ]
209 then
210 rm -f "$TEST_DIR/$SAMPLE_IMG_FILE"
214 rbd)
215 rbd --no-progress rm "$TEST_DIR/t.$IMGFMT" > /dev/null
218 archipelago)
219 vlmc remove "at.$IMGFMT" > /dev/null
222 sheepdog)
223 collie vdi delete "$TEST_DIR/t.$IMGFMT"
226 esac
229 _check_test_img()
232 if [ "$IMGOPTSSYNTAX" = "true" ]; then
233 $QEMU_IMG check $QEMU_IMG_EXTRA_ARGS "$@" "$TEST_IMG" 2>&1
234 else
235 $QEMU_IMG check "$@" -f $IMGFMT "$TEST_IMG" 2>&1
237 ) | _filter_testdir | _filter_qemu_img_check
240 _img_info()
242 if [[ "$1" == "--format-specific" ]]; then
243 local format_specific=1
244 shift
245 else
246 local format_specific=0
249 discard=0
250 regex_json_spec_start='^ *"format-specific": \{'
251 $QEMU_IMG info "$@" "$TEST_IMG" 2>&1 | \
252 sed -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \
253 -e "s#$TEST_DIR#TEST_DIR#g" \
254 -e "s#$IMGFMT#IMGFMT#g" \
255 -e "/^disk size:/ D" \
256 -e "/actual-size/ D" | \
257 while IFS='' read line; do
258 if [[ $format_specific == 1 ]]; then
259 discard=0
260 elif [[ $line == "Format specific information:" ]]; then
261 discard=1
262 elif [[ $line =~ $regex_json_spec_start ]]; then
263 discard=2
264 regex_json_spec_end="^${line%%[^ ]*}\\},? *$"
266 if [[ $discard == 0 ]]; then
267 echo "$line"
268 elif [[ $discard == 1 && ! $line ]]; then
269 echo
270 discard=0
271 elif [[ $discard == 2 && $line =~ $regex_json_spec_end ]]; then
272 discard=0
274 done
277 _get_pids_by_name()
279 if [ $# -ne 1 ]
280 then
281 echo "Usage: _get_pids_by_name process-name" 1>&2
282 exit 1
285 # Algorithm ... all ps(1) variants have a time of the form MM:SS or
286 # HH:MM:SS before the psargs field, use this as the search anchor.
288 # Matches with $1 (process-name) occur if the first psarg is $1
289 # or ends in /$1 ... the matching uses sed's regular expressions,
290 # so passing a regex into $1 will work.
292 ps $PS_ALL_FLAGS \
293 | sed -n \
294 -e 's/$/ /' \
295 -e 's/[ ][ ]*/ /g' \
296 -e 's/^ //' \
297 -e 's/^[^ ]* //' \
298 -e "/[0-9]:[0-9][0-9] *[^ ]*\/$1 /s/ .*//p" \
299 -e "/[0-9]:[0-9][0-9] *$1 /s/ .*//p"
302 # fqdn for localhost
304 _get_fqdn()
306 host=`hostname`
307 $NSLOOKUP_PROG $host | $AWK_PROG '{ if ($1 == "Name:") print $2 }'
310 # check if run as root
312 _need_to_be_root()
314 id=`id | $SED_PROG -e 's/(.*//' -e 's/.*=//'`
315 if [ "$id" -ne 0 ]
316 then
317 echo "Arrgh ... you need to be root (not uid=$id) to run this test"
318 exit 1
322 # bail out, setting up .notrun file
324 _notrun()
326 echo "$*" >"$OUTPUT_DIR/$seq.notrun"
327 echo "$seq not run: $*"
328 status=0
329 exit
332 # just plain bail out
334 _fail()
336 echo "$*" | tee -a "$OUTPUT_DIR/$seq.full"
337 echo "(see $seq.full for details)"
338 status=1
339 exit 1
342 # tests whether $IMGFMT is one of the supported image formats for a test
344 _supported_fmt()
346 # "generic" is suitable for most image formats. For some formats it doesn't
347 # work, however (most notably read-only formats), so they can opt out by
348 # setting IMGFMT_GENERIC to false.
349 for f; do
350 if [ "$f" = "$IMGFMT" -o "$f" = "generic" -a "$IMGFMT_GENERIC" = "true" ]; then
351 return
353 done
355 _notrun "not suitable for this image format: $IMGFMT"
358 # tests whether $IMGPROTO is one of the supported image protocols for a test
360 _supported_proto()
362 for f; do
363 if [ "$f" = "$IMGPROTO" -o "$f" = "generic" ]; then
364 return
366 done
368 _notrun "not suitable for this image protocol: $IMGPROTO"
371 # tests whether the host OS is one of the supported OSes for a test
373 _supported_os()
375 for h
377 if [ "$h" = "$HOSTOS" ]
378 then
379 return
381 done
383 _notrun "not suitable for this OS: $HOSTOS"
386 _supported_cache_modes()
388 for mode; do
389 if [ "$mode" = "$CACHEMODE" ]; then
390 return
392 done
393 _notrun "not suitable for cache mode: $CACHEMODE"
396 _default_cache_mode()
398 if $CACHEMODE_IS_DEFAULT; then
399 CACHEMODE="$1"
400 QEMU_IO="$QEMU_IO --cache $1"
401 return
405 _unsupported_imgopts()
407 for bad_opt
409 if echo "$IMGOPTS" | grep -q 2>/dev/null "$bad_opt"
410 then
411 _notrun "not suitable for image option: $bad_opt"
413 done
416 # this test requires that a specified command (executable) exists
418 _require_command()
420 if [ "$1" = "QEMU" ]; then
421 c=$QEMU_PROG
422 elif [ "$1" = "QEMU_IMG" ]; then
423 c=$QEMU_IMG_PROG
424 elif [ "$1" = "QEMU_IO" ]; then
425 c=$QEMU_IO_PROG
426 elif [ "$1" = "QEMU_NBD" ]; then
427 c=$QEMU_NBD_PROG
428 else
429 eval c=\$$1
431 [ -x "$c" ] || _notrun "$1 utility required, skipped this test"
434 _full_imgfmt_details()
436 if [ -n "$IMGOPTS" ]; then
437 echo "$IMGFMT ($IMGOPTS)"
438 else
439 echo "$IMGFMT"
443 _full_imgproto_details()
445 echo "$IMGPROTO"
448 _full_platform_details()
450 os=`uname -s`
451 host=`hostname -s`
452 kernel=`uname -r`
453 platform=`uname -m`
454 echo "$os/$platform $host $kernel"
457 _link_out_file()
459 if [ -z "$1" ]; then
460 echo Error must pass \$seq.
461 exit
463 rm -f $1
464 if [ "`uname`" == "IRIX64" ] || [ "`uname`" == "IRIX" ]; then
465 ln -s $1.irix $1
466 elif [ "`uname`" == "Linux" ]; then
467 ln -s $1.linux $1
468 else
469 echo Error test $seq does not run on the operating system: `uname`
470 exit
474 _die()
476 echo $@
477 exit 1
480 # make sure this script returns success
481 true