common.config: Allow use of arbitrary qemu* paths
[qemu-iotests.git] / common.rc
blob26811ca660b65a8c8d4a9a9106c2ea37e7313465
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 # we need common.config
38 if [ "$iam" != "check" ]
39 then
40 if ! . ./common.config
41 then
42 echo "$iam: failed to source common.config"
43 exit 1
47 # make sure we have a standard umask
48 umask 022
50 if [ "$IMGPROTO" = "file" ]; then
51 TEST_IMG=$TEST_DIR/t.$IMGFMT
52 else
53 TEST_IMG=$IMGPROTO:$TEST_DIR/t.$IMGFMT
56 _make_test_img()
58 # extra qemu-img options can be added by tests
59 # at least one argument (the image size) needs to be added
60 local extra_img_options=$*
61 local cluster_size_filter="s# cluster_size=[0-9]\\+##g"
63 if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then
64 extra_img_options="-o cluster_size=$CLUSTER_SIZE $extra_img_options"
65 cluster_size_filter=""
68 # XXX(hch): have global image options?
69 $QEMU_IMG create -f $IMGFMT $TEST_IMG $extra_img_options | \
70 sed -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" | \
71 sed -e "s#$TEST_DIR#TEST_DIR#g" | \
72 sed -e "s#$IMGFMT#IMGFMT#g" | \
73 sed -e "s# encryption=off##g" | \
74 sed -e "$cluster_size_filter" | \
75 sed -e "s# table_size=0##g" | \
76 sed -e "s# compat6=off##g" | \
77 sed -e "s# static=off##g"
80 _cleanup_test_img()
82 case "$IMGPROTO" in
84 file)
85 rm -f $TEST_DIR/t.$IMGFMT
86 rm -f $TEST_DIR/t.$IMGFMT.orig
87 rm -f $TEST_DIR/t.$IMGFMT.base
90 rbd)
91 rbd rm $TEST_DIR/t.$IMGFMT > /dev/null
94 sheepdog)
95 collie vdi delete $TEST_DIR/t.$IMGFMT
98 esac
101 _check_test_img()
103 $QEMU_IMG check -f $IMGFMT $TEST_IMG 2>&1 | \
104 sed -e 's/qemu-img\: This image format does not support checks/No errors were found on the image./'
107 _get_pids_by_name()
109 if [ $# -ne 1 ]
110 then
111 echo "Usage: _get_pids_by_name process-name" 1>&2
112 exit 1
115 # Algorithm ... all ps(1) variants have a time of the form MM:SS or
116 # HH:MM:SS before the psargs field, use this as the search anchor.
118 # Matches with $1 (process-name) occur if the first psarg is $1
119 # or ends in /$1 ... the matching uses sed's regular expressions,
120 # so passing a regex into $1 will work.
122 ps $PS_ALL_FLAGS \
123 | sed -n \
124 -e 's/$/ /' \
125 -e 's/[ ][ ]*/ /g' \
126 -e 's/^ //' \
127 -e 's/^[^ ]* //' \
128 -e "/[0-9]:[0-9][0-9] *[^ ]*\/$1 /s/ .*//p" \
129 -e "/[0-9]:[0-9][0-9] *$1 /s/ .*//p"
132 # fqdn for localhost
134 _get_fqdn()
136 host=`hostname`
137 $NSLOOKUP_PROG $host | $AWK_PROG '{ if ($1 == "Name:") print $2 }'
140 # check if run as root
142 _need_to_be_root()
144 id=`id | $SED_PROG -e 's/(.*//' -e 's/.*=//'`
145 if [ "$id" -ne 0 ]
146 then
147 echo "Arrgh ... you need to be root (not uid=$id) to run this test"
148 exit 1
153 # Do a command, log it to $seq.full, optionally test return status
154 # and die if command fails. If called with one argument _do executes the
155 # command, logs it, and returns its exit status. With two arguments _do
156 # first prints the message passed in the first argument, and then "done"
157 # or "fail" depending on the return status of the command passed in the
158 # second argument. If the command fails and the variable _do_die_on_error
159 # is set to "always" or the two argument form is used and _do_die_on_error
160 # is set to "message_only" _do will print an error message to
161 # $seq.out and exit.
163 _do()
165 if [ $# -eq 1 ]; then
166 _cmd=$1
167 elif [ $# -eq 2 ]; then
168 _note=$1
169 _cmd=$2
170 echo -n "$_note... "
171 else
172 echo "Usage: _do [note] cmd" 1>&2
173 status=1; exit
176 (eval "echo '---' \"$_cmd\"") >>$here/$seq.full
177 (eval "$_cmd") >$tmp._out 2>&1; ret=$?
178 cat $tmp._out >>$here/$seq.full
179 if [ $# -eq 2 ]; then
180 if [ $ret -eq 0 ]; then
181 echo "done"
182 else
183 echo "fail"
186 if [ $ret -ne 0 ] \
187 && [ "$_do_die_on_error" = "always" \
188 -o \( $# -eq 2 -a "$_do_die_on_error" = "message_only" \) ]
189 then
190 [ $# -ne 2 ] && echo
191 eval "echo \"$_cmd\" failed \(returned $ret\): see $seq.full"
192 status=1; exit
195 return $ret
198 # bail out, setting up .notrun file
200 _notrun()
202 echo "$*" >$seq.notrun
203 echo "$seq not run: $*"
204 status=0
205 exit
208 # just plain bail out
210 _fail()
212 echo "$*" | tee -a $here/$seq.full
213 echo "(see $seq.full for details)"
214 status=1
215 exit 1
218 # tests whether $IMGFMT is one of the supported image formats for a test
220 _supported_fmt()
222 for f; do
223 if [ "$f" = "$IMGFMT" -o "$f" = "generic" ]; then
224 return
226 done
228 _notrun "not suitable for this image format: $IMGFMT"
231 # tests whether $IMGPROTO is one of the supported image protocols for a test
233 _supported_proto()
235 for f; do
236 if [ "$f" = "$IMGPROTO" -o "$f" = "generic" ]; then
237 return
239 done
241 _notrun "not suitable for this image protocol: $IMGPROTO"
244 # tests whether the host OS is one of the supported OSes for a test
246 _supported_os()
248 for h
250 if [ "$h" = "$HOSTOS" ]
251 then
252 return
254 done
256 _notrun "not suitable for this OS: $HOSTOS"
259 # this test requires that a specified command (executable) exists
261 _require_command()
263 [ -x "$1" ] || _notrun "$1 utility required, skipped this test"
266 _full_imgfmt_details()
268 echo "$IMGFMT"
271 _full_imgproto_details()
273 echo "$IMGPROTO"
276 _full_platform_details()
278 os=`uname -s`
279 host=`hostname -s`
280 kernel=`uname -r`
281 platform=`uname -m`
282 echo "$os/$platform $host $kernel"
285 _link_out_file()
287 if [ -z "$1" ]; then
288 echo Error must pass \$seq.
289 exit
291 rm -f $1
292 if [ "`uname`" == "IRIX64" ] || [ "`uname`" == "IRIX" ]; then
293 ln -s $1.irix $1
294 elif [ "`uname`" == "Linux" ]; then
295 ln -s $1.linux $1
296 else
297 echo Error test $seq does not run on the operating system: `uname`
298 exit
302 _die()
304 echo $@
305 exit 1
308 # make sure this script returns success
309 /bin/true