linux-headers: update to 3.12-rc1
[qemu/ar7.git] / tests / qemu-iotests / common.rc
blob88fecf7870fb33fb2a03624d214186e7172ee960
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 _make_test_img()
96 # extra qemu-img options can be added by tests
97 # at least one argument (the image size) needs to be added
98 local extra_img_options=""
99 local image_size=$*
100 local optstr=""
101 local img_name=""
103 if [ -n "$TEST_IMG_FILE" ]; then
104 img_name=$TEST_IMG_FILE
105 else
106 img_name=$TEST_IMG
109 if [ -n "$IMGOPTS" ]; then
110 optstr=$(_optstr_add "$optstr" "$IMGOPTS")
113 if [ "$1" = "-b" ]; then
114 extra_img_options="$1 $2"
115 image_size=$3
117 if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then
118 optstr=$(_optstr_add "$optstr" "cluster_size=$CLUSTER_SIZE")
121 if [ -n "$optstr" ]; then
122 extra_img_options="-o $optstr $extra_img_options"
125 # XXX(hch): have global image options?
126 $QEMU_IMG create -f $IMGFMT $extra_img_options $img_name $image_size | \
127 sed -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \
128 -e "s#$TEST_DIR#TEST_DIR#g" \
129 -e "s#$IMGFMT#IMGFMT#g" \
130 -e "s# encryption=off##g" \
131 -e "s# cluster_size=[0-9]\\+##g" \
132 -e "s# table_size=[0-9]\\+##g" \
133 -e "s# compat='[^']*'##g" \
134 -e "s# compat6=\\(on\\|off\\)##g" \
135 -e "s# static=\\(on\\|off\\)##g" \
136 -e "s# zeroed_grain=\\(on\\|off\\)##g" \
137 -e "s# subformat='[^']*'##g" \
138 -e "s# adapter_type='[^']*'##g" \
139 -e "s# lazy_refcounts=\\(on\\|off\\)##g"
141 # Start an NBD server on the image file, which is what we'll be talking to
142 if [ $IMGPROTO = "nbd" ]; then
143 eval "$QEMU_NBD -v -t -b 127.0.0.1 -p 10810 $TEST_IMG_FILE &"
144 QEMU_NBD_PID=$!
145 sleep 1 # FIXME: qemu-nbd needs to be listening before we continue
149 _cleanup_test_img()
151 case "$IMGPROTO" in
153 nbd)
154 kill $QEMU_NBD_PID
155 rm -f $TEST_IMG_FILE
157 file)
158 rm -f $TEST_DIR/t.$IMGFMT
159 rm -f $TEST_DIR/t.$IMGFMT.orig
160 rm -f $TEST_DIR/t.$IMGFMT.base
163 rbd)
164 rbd rm $TEST_DIR/t.$IMGFMT > /dev/null
167 sheepdog)
168 collie vdi delete $TEST_DIR/t.$IMGFMT
171 esac
174 _check_test_img()
176 $QEMU_IMG check "$@" -f $IMGFMT $TEST_IMG 2>&1 | _filter_testdir | \
177 sed -e '/allocated.*fragmented.*compressed clusters/d' \
178 -e 's/qemu-img: This image format does not support checks/No errors were found on the image./' \
179 -e '/Image end offset: [0-9]\+/d'
182 _img_info()
184 $QEMU_IMG info "$@" $TEST_IMG 2>&1 | \
185 sed -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \
186 -e "s#$TEST_DIR#TEST_DIR#g" \
187 -e "s#$IMGFMT#IMGFMT#g" \
188 -e "/^disk size:/ D" \
189 -e "/actual-size/ D"
192 _get_pids_by_name()
194 if [ $# -ne 1 ]
195 then
196 echo "Usage: _get_pids_by_name process-name" 1>&2
197 exit 1
200 # Algorithm ... all ps(1) variants have a time of the form MM:SS or
201 # HH:MM:SS before the psargs field, use this as the search anchor.
203 # Matches with $1 (process-name) occur if the first psarg is $1
204 # or ends in /$1 ... the matching uses sed's regular expressions,
205 # so passing a regex into $1 will work.
207 ps $PS_ALL_FLAGS \
208 | sed -n \
209 -e 's/$/ /' \
210 -e 's/[ ][ ]*/ /g' \
211 -e 's/^ //' \
212 -e 's/^[^ ]* //' \
213 -e "/[0-9]:[0-9][0-9] *[^ ]*\/$1 /s/ .*//p" \
214 -e "/[0-9]:[0-9][0-9] *$1 /s/ .*//p"
217 # fqdn for localhost
219 _get_fqdn()
221 host=`hostname`
222 $NSLOOKUP_PROG $host | $AWK_PROG '{ if ($1 == "Name:") print $2 }'
225 # check if run as root
227 _need_to_be_root()
229 id=`id | $SED_PROG -e 's/(.*//' -e 's/.*=//'`
230 if [ "$id" -ne 0 ]
231 then
232 echo "Arrgh ... you need to be root (not uid=$id) to run this test"
233 exit 1
238 # Do a command, log it to $seq.full, optionally test return status
239 # and die if command fails. If called with one argument _do executes the
240 # command, logs it, and returns its exit status. With two arguments _do
241 # first prints the message passed in the first argument, and then "done"
242 # or "fail" depending on the return status of the command passed in the
243 # second argument. If the command fails and the variable _do_die_on_error
244 # is set to "always" or the two argument form is used and _do_die_on_error
245 # is set to "message_only" _do will print an error message to
246 # $seq.out and exit.
248 _do()
250 if [ $# -eq 1 ]; then
251 _cmd=$1
252 elif [ $# -eq 2 ]; then
253 _note=$1
254 _cmd=$2
255 echo -n "$_note... "
256 else
257 echo "Usage: _do [note] cmd" 1>&2
258 status=1; exit
261 (eval "echo '---' \"$_cmd\"") >>$here/$seq.full
262 (eval "$_cmd") >$tmp._out 2>&1; ret=$?
263 cat $tmp._out >>$here/$seq.full
264 if [ $# -eq 2 ]; then
265 if [ $ret -eq 0 ]; then
266 echo "done"
267 else
268 echo "fail"
271 if [ $ret -ne 0 ] \
272 && [ "$_do_die_on_error" = "always" \
273 -o \( $# -eq 2 -a "$_do_die_on_error" = "message_only" \) ]
274 then
275 [ $# -ne 2 ] && echo
276 eval "echo \"$_cmd\" failed \(returned $ret\): see $seq.full"
277 status=1; exit
280 return $ret
283 # bail out, setting up .notrun file
285 _notrun()
287 echo "$*" >$seq.notrun
288 echo "$seq not run: $*"
289 status=0
290 exit
293 # just plain bail out
295 _fail()
297 echo "$*" | tee -a $here/$seq.full
298 echo "(see $seq.full for details)"
299 status=1
300 exit 1
303 # tests whether $IMGFMT is one of the supported image formats for a test
305 _supported_fmt()
307 for f; do
308 if [ "$f" = "$IMGFMT" -o "$f" = "generic" ]; then
309 return
311 done
313 _notrun "not suitable for this image format: $IMGFMT"
316 # tests whether $IMGPROTO is one of the supported image protocols for a test
318 _supported_proto()
320 for f; do
321 if [ "$f" = "$IMGPROTO" -o "$f" = "generic" ]; then
322 return
324 done
326 _notrun "not suitable for this image protocol: $IMGPROTO"
329 # tests whether the host OS is one of the supported OSes for a test
331 _supported_os()
333 for h
335 if [ "$h" = "$HOSTOS" ]
336 then
337 return
339 done
341 _notrun "not suitable for this OS: $HOSTOS"
344 _unsupported_qemu_io_options()
346 for bad_opt
348 for opt in $QEMU_IO_OPTIONS
350 if [ "$bad_opt" = "$opt" ]
351 then
352 _notrun "not suitable for qemu-io option: $bad_opt"
354 done
355 done
358 # this test requires that a specified command (executable) exists
360 _require_command()
362 [ -x "$1" ] || _notrun "$1 utility required, skipped this test"
365 _full_imgfmt_details()
367 if [ -n "$IMGOPTS" ]; then
368 echo "$IMGFMT ($IMGOPTS)"
369 else
370 echo "$IMGFMT"
374 _full_imgproto_details()
376 echo "$IMGPROTO"
379 _full_platform_details()
381 os=`uname -s`
382 host=`hostname -s`
383 kernel=`uname -r`
384 platform=`uname -m`
385 echo "$os/$platform $host $kernel"
388 _link_out_file()
390 if [ -z "$1" ]; then
391 echo Error must pass \$seq.
392 exit
394 rm -f $1
395 if [ "`uname`" == "IRIX64" ] || [ "`uname`" == "IRIX" ]; then
396 ln -s $1.irix $1
397 elif [ "`uname`" == "Linux" ]; then
398 ln -s $1.linux $1
399 else
400 echo Error test $seq does not run on the operating system: `uname`
401 exit
405 _die()
407 echo $@
408 exit 1
411 # make sure this script returns success
412 /bin/true