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/>.
22 if [ "$HOSTOS" == "Linux" ]
24 command dd --help | grep noxfer > /dev/null 2>&1
28 command dd status=noxfer $@
37 # poke_file 'test.img' 512 '\xff\xfe'
40 printf "$3" | dd "of=$1" bs=1 "seek=$2" conv=notrunc &>/dev/null
43 # we need common.config
44 if [ "$iam" != "check" ]
46 if ! . ./common.config
48 echo "$iam: failed to source common.config"
53 # make sure we have a standard umask
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 elif [ "$IMGPROTO" = "nfs" ]; then
65 TEST_DIR="nfs://127.0.0.1/$TEST_DIR"
66 TEST_IMG=$TEST_DIR/t.$IMGFMT
68 TEST_IMG=$IMGPROTO:$TEST_DIR/t.$IMGFMT
71 function valgrind_qemu_io()
73 valgrind --log-file=/tmp/$$.valgrind --error-exitcode=99 $REAL_QEMU_IO "$@"
77 rm -f /tmp/$$.valgrind
90 _set_default_imgopts()
92 if [ "$IMGFMT" == "qcow2" ] && ! (echo "$IMGOPTS" | grep "compat=" > /dev/null); then
93 IMGOPTS=$(_optstr_add "$IMGOPTS" "compat=1.1")
99 SAMPLE_IMG_FILE="${1%\.bz2}"
100 TEST_IMG="$TEST_DIR/$SAMPLE_IMG_FILE"
101 bzcat "$SAMPLE_IMG_DIR/$1" > "$TEST_IMG"
104 echo "_use_sample_img error, cannot extract '$SAMPLE_IMG_DIR/$1'"
111 # extra qemu-img options can be added by tests
112 # at least one argument (the image size) needs to be added
113 local extra_img_options=""
118 local backing_file=""
120 if [ -n "$TEST_IMG_FILE" ]; then
121 img_name=$TEST_IMG_FILE
126 if [ -n "$IMGOPTS" ]; then
127 optstr=$(_optstr_add "$optstr" "$IMGOPTS")
130 if [ "$1" = "-b" ]; then
135 if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then
136 optstr=$(_optstr_add "$optstr" "cluster_size=$CLUSTER_SIZE")
139 if [ -n "$optstr" ]; then
140 extra_img_options="-o $optstr $extra_img_options"
143 # XXX(hch): have global image options?
145 if [ $use_backing = 1 ]; then
146 $QEMU_IMG create -f $IMGFMT $extra_img_options -b "$backing_file" "$img_name" $image_size 2>&1
148 $QEMU_IMG create -f $IMGFMT $extra_img_options "$img_name" $image_size 2>&1
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" \
160 -e "s# zeroed_grain=\\(on\\|off\\)##g" \
161 -e "s# subformat='[^']*'##g" \
162 -e "s# adapter_type='[^']*'##g" \
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"
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 &"
172 sleep 1 # FIXME: qemu-nbd needs to be listening before we continue
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 "{}"
193 rm -f "$TEST_IMG_FILE"
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"
199 if [ -n "$SAMPLE_IMG_FILE" ]
201 rm -f "$TEST_DIR/$SAMPLE_IMG_FILE"
206 rbd --no-progress rm "$TEST_DIR/t.$IMGFMT" > /dev/null
210 collie vdi delete "$TEST_DIR/t.$IMGFMT"
218 $QEMU_IMG check "$@" -f $IMGFMT "$TEST_IMG" 2>&1 | _filter_testdir | \
219 sed -e '/allocated.*fragmented.*compressed clusters/d' \
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'
227 regex_json_spec_start='^ *"format-specific": \{'
228 $QEMU_IMG info "$@" "$TEST_IMG" 2>&1 | \
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" \
233 -e "/actual-size/ D" | \
234 while IFS='' read line; do
235 if [[ $line == "Format specific information:" ]]; then
237 elif [[ $line =~ $regex_json_spec_start ]]; then
239 regex_json_spec_end="^${line%%[^ ]*}\\},? *$"
241 if [[ $discard == 0 ]]; then
243 elif [[ $discard == 1 && ! $line ]]; then
246 elif [[ $discard == 2 && $line =~ $regex_json_spec_end ]]; then
256 echo "Usage: _get_pids_by_name process-name" 1>&2
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.
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.
273 -e "/[0-9]:[0-9][0-9] *[^ ]*\/$1 /s/ .*//p" \
274 -e "/[0-9]:[0-9][0-9] *$1 /s/ .*//p"
282 $NSLOOKUP_PROG $host | $AWK_PROG '{ if ($1 == "Name:") print $2 }'
285 # check if run as root
289 id=`id | $SED_PROG -e 's/(.*//' -e 's/.*=//'`
292 echo "Arrgh ... you need to be root (not uid=$id) to run this test"
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
310 if [ $# -eq 1 ]; then
312 elif [ $# -eq 2 ]; then
317 echo "Usage: _do [note] cmd" 1>&2
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
325 if [ $ret -eq 0 ]; then
332 && [ "$_do_die_on_error" = "always" \
333 -o \( $# -eq 2 -a "$_do_die_on_error" = "message_only" \) ]
336 eval "echo \"$_cmd\" failed \(returned $ret\): see $seq.full"
343 # bail out, setting up .notrun file
347 echo "$*" >$seq.notrun
348 echo "$seq not run: $*"
353 # just plain bail out
357 echo "$*" | tee -a $here/$seq.full
358 echo "(see $seq.full for details)"
363 # tests whether $IMGFMT is one of the supported image formats for a test
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.
371 if [ "$f" = "$IMGFMT" -o "$f" = "generic" -a "$IMGFMT_GENERIC" = "true" ]; then
376 _notrun "not suitable for this image format: $IMGFMT"
379 # tests whether $IMGPROTO is one of the supported image protocols for a test
384 if [ "$f" = "$IMGPROTO" -o "$f" = "generic" ]; then
389 _notrun "not suitable for this image protocol: $IMGPROTO"
392 # tests whether the host OS is one of the supported OSes for a test
398 if [ "$h" = "$HOSTOS" ]
404 _notrun "not suitable for this OS: $HOSTOS"
407 _supported_cache_modes()
410 if [ "$mode" = "$CACHEMODE" ]; then
414 _notrun "not suitable for cache mode: $CACHEMODE"
417 _default_cache_mode()
419 if $CACHEMODE_IS_DEFAULT; then
421 QEMU_IO="$QEMU_IO --cache $1"
426 _unsupported_imgopts()
430 if echo "$IMGOPTS" | grep -q 2>/dev/null "$bad_opt"
432 _notrun "not suitable for image option: $bad_opt"
437 # this test requires that a specified command (executable) exists
442 [ -x "$c" ] || _notrun "$1 utility required, skipped this test"
445 _full_imgfmt_details()
447 if [ -n "$IMGOPTS" ]; then
448 echo "$IMGFMT ($IMGOPTS)"
454 _full_imgproto_details()
459 _full_platform_details()
465 echo "$os/$platform $host $kernel"
471 echo Error must pass \$seq.
475 if [ "`uname`" == "IRIX64" ] || [ "`uname`" == "IRIX" ]; then
477 elif [ "`uname`" == "Linux" ]; then
480 echo Error test $seq does not run on the operating system: `uname`
491 # make sure this script returns success