add support for the vdi image format
[qemu-iotests/stefanha.git] / common.rc
blobc4337d50cb85846f58901a15a95c6b92a6f77445
1 #!/bin/sh
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, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 # USA
22 dd()
24 if [ "$HOSTOS" == "Linux" ]
25 then
26 command dd --help | grep noxfer > /dev/null 2>&1
28 if [ "$?" -eq 0 ]
29 then
30 command dd status=noxfer $@
31 else
32 command dd $@
34 else
35 command dd $@
39 # we need common.config
40 if [ "$iam" != "check" ]
41 then
42 if ! . ./common.config
43 then
44 echo "$iam: failed to source common.config"
45 exit 1
49 # make sure we have a standard umask
50 umask 022
52 TEST_IMG=$TEST_DIR/t.$IMGFMT
54 _make_test_img()
56 # extra qemu-img options can be added by tests
57 # at least one argument (the image size) needs to be added
58 local extra_img_options=$*
60 # XXX(hch): have global image options?
61 $QEMU_IMG create -f $IMGFMT $TEST_IMG $extra_img_options | \
62 sed -e "s#$TEST_DIR#TEST_DIR#g" | \
63 sed -e "s#$IMGFMT#IMGFMT#g" | \
64 sed -e "s# encryption=off##g" | \
65 sed -e "s# cluster_size=0##g" | \
66 sed -e "s# compat6=off##g"
70 _cleanup_test_img()
72 rm -f $TEST_DIR/t.$IMGFMT
73 rm -f $TEST_DIR/t.$IMGFMT.orig
76 _check_test_img()
78 $QEMU_IMG check -f $IMGFMT $TEST_IMG
81 _get_pids_by_name()
83 if [ $# -ne 1 ]
84 then
85 echo "Usage: _get_pids_by_name process-name" 1>&2
86 exit 1
89 # Algorithm ... all ps(1) variants have a time of the form MM:SS or
90 # HH:MM:SS before the psargs field, use this as the search anchor.
92 # Matches with $1 (process-name) occur if the first psarg is $1
93 # or ends in /$1 ... the matching uses sed's regular expressions,
94 # so passing a regex into $1 will work.
96 ps $PS_ALL_FLAGS \
97 | sed -n \
98 -e 's/$/ /' \
99 -e 's/[ ][ ]*/ /g' \
100 -e 's/^ //' \
101 -e 's/^[^ ]* //' \
102 -e "/[0-9]:[0-9][0-9] *[^ ]*\/$1 /s/ .*//p" \
103 -e "/[0-9]:[0-9][0-9] *$1 /s/ .*//p"
106 # fqdn for localhost
108 _get_fqdn()
110 host=`hostname`
111 $NSLOOKUP_PROG $host | $AWK_PROG '{ if ($1 == "Name:") print $2 }'
114 # check if run as root
116 _need_to_be_root()
118 id=`id | $SED_PROG -e 's/(.*//' -e 's/.*=//'`
119 if [ "$id" -ne 0 ]
120 then
121 echo "Arrgh ... you need to be root (not uid=$id) to run this test"
122 exit 1
127 # Do a command, log it to $seq.full, optionally test return status
128 # and die if command fails. If called with one argument _do executes the
129 # command, logs it, and returns its exit status. With two arguments _do
130 # first prints the message passed in the first argument, and then "done"
131 # or "fail" depending on the return status of the command passed in the
132 # second argument. If the command fails and the variable _do_die_on_error
133 # is set to "always" or the two argument form is used and _do_die_on_error
134 # is set to "message_only" _do will print an error message to
135 # $seq.out and exit.
137 _do()
139 if [ $# -eq 1 ]; then
140 _cmd=$1
141 elif [ $# -eq 2 ]; then
142 _note=$1
143 _cmd=$2
144 echo -n "$_note... "
145 else
146 echo "Usage: _do [note] cmd" 1>&2
147 status=1; exit
150 (eval "echo '---' \"$_cmd\"") >>$here/$seq.full
151 (eval "$_cmd") >$tmp._out 2>&1; ret=$?
152 cat $tmp._out >>$here/$seq.full
153 if [ $# -eq 2 ]; then
154 if [ $ret -eq 0 ]; then
155 echo "done"
156 else
157 echo "fail"
160 if [ $ret -ne 0 ] \
161 && [ "$_do_die_on_error" = "always" \
162 -o \( $# -eq 2 -a "$_do_die_on_error" = "message_only" \) ]
163 then
164 [ $# -ne 2 ] && echo
165 eval "echo \"$_cmd\" failed \(returned $ret\): see $seq.full"
166 status=1; exit
169 return $ret
172 # bail out, setting up .notrun file
174 _notrun()
176 echo "$*" >$seq.notrun
177 echo "$seq not run: $*"
178 status=0
179 exit
182 # just plain bail out
184 _fail()
186 echo "$*" | tee -a $here/$seq.full
187 echo "(see $seq.full for details)"
188 status=1
189 exit 1
192 # tests whether $IMGFMT is one of the supported image formats for a test
194 _supported_fmt()
196 for f; do
197 if [ "$f" = "$IMGFMT" -o "$f" = "generic" ]; then
198 return
200 done
202 _notrun "not suitable for this image format: $IMGFMT"
205 # tests whether the host OS is one of the supported OSes for a test
207 _supported_os()
209 for h
211 if [ "$h" = "$HOSTOS" ]
212 then
213 return
215 done
217 _notrun "not suitable for this OS: $HOSTOS"
220 # this test requires that a specified command (executable) exists
222 _require_command()
224 [ -x "$1" ] || _notrun "$1 utility required, skipped this test"
227 _full_imgfmt_details()
229 echo "$IMGFMT"
232 _full_platform_details()
234 os=`uname -s`
235 host=`hostname -s`
236 kernel=`uname -r`
237 platform=`uname -m`
238 echo "$os/$platform $host $kernel"
241 _link_out_file()
243 if [ -z "$1" ]; then
244 echo Error must pass \$seq.
245 exit
247 rm -f $1
248 if [ "`uname`" == "IRIX64" ] || [ "`uname`" == "IRIX" ]; then
249 ln -s $1.irix $1
250 elif [ "`uname`" == "Linux" ]; then
251 ln -s $1.linux $1
252 else
253 echo Error test $seq does not run on the operating system: `uname`
254 exit
258 _die()
260 echo $@
261 exit 1
264 # make sure this script returns success
265 /bin/true