]> git.proxmox.com Git - qemu.git/blame - tests/qemu-iotests/common.rc
qemu-iotests: Test unknown qcow2 header extensions
[qemu.git] / tests / qemu-iotests / common.rc
CommitLineData
908eaf68 1#!/bin/bash
6bf19c94
CH
2#
3# Copyright (C) 2009 Red Hat, Inc.
4# Copyright (c) 2000-2006 Silicon Graphics, Inc. All Rights Reserved.
5#
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.
10#
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.
15#
16# You should have received a copy of the GNU General Public License
e8c212d6 17# along with this program. If not, see <http://www.gnu.org/licenses/>.
6bf19c94
CH
18#
19
20dd()
21{
22 if [ "$HOSTOS" == "Linux" ]
23 then
24 command dd --help | grep noxfer > /dev/null 2>&1
25
26 if [ "$?" -eq 0 ]
27 then
28 command dd status=noxfer $@
29 else
30 command dd $@
31 fi
32 else
33 command dd $@
34 fi
35}
36
37# we need common.config
38if [ "$iam" != "check" ]
39then
40 if ! . ./common.config
41 then
42 echo "$iam: failed to source common.config"
43 exit 1
44 fi
45fi
46
47# make sure we have a standard umask
48umask 022
49
9cdfa1b3
MK
50if [ "$IMGPROTO" = "file" ]; then
51 TEST_IMG=$TEST_DIR/t.$IMGFMT
52else
53 TEST_IMG=$IMGPROTO:$TEST_DIR/t.$IMGFMT
54fi
6bf19c94
CH
55
56_make_test_img()
57{
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=$*
bfe85248 61 local cluster_size_filter="s# cluster_size=[0-9]\\+##g"
6bf19c94 62
f5a4bbd9 63 if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then
8fc1024c 64 extra_img_options="-o cluster_size=$CLUSTER_SIZE $extra_img_options"
bfe85248 65 cluster_size_filter=""
8fc1024c
KW
66 fi
67
6bf19c94
CH
68 # XXX(hch): have global image options?
69 $QEMU_IMG create -f $IMGFMT $TEST_IMG $extra_img_options | \
9cdfa1b3 70 sed -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" | \
6bf19c94
CH
71 sed -e "s#$TEST_DIR#TEST_DIR#g" | \
72 sed -e "s#$IMGFMT#IMGFMT#g" | \
73 sed -e "s# encryption=off##g" | \
bfe85248 74 sed -e "$cluster_size_filter" | \
f5a4bbd9 75 sed -e "s# table_size=0##g" | \
3b5fe6e6
SW
76 sed -e "s# compat6=off##g" | \
77 sed -e "s# static=off##g"
6bf19c94
CH
78}
79
80_cleanup_test_img()
81{
9cdfa1b3
MK
82 case "$IMGPROTO" in
83
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
88 ;;
89
90 rbd)
91 rbd rm $TEST_DIR/t.$IMGFMT > /dev/null
92 ;;
93
94 sheepdog)
95 collie vdi delete $TEST_DIR/t.$IMGFMT
96 ;;
97
98 esac
6bf19c94
CH
99}
100
101_check_test_img()
102{
e76a8e89
CH
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./'
6bf19c94
CH
105}
106
107_get_pids_by_name()
108{
109 if [ $# -ne 1 ]
110 then
111 echo "Usage: _get_pids_by_name process-name" 1>&2
112 exit 1
113 fi
114
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.
117 #
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.
121
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"
130}
131
132# fqdn for localhost
133#
134_get_fqdn()
135{
136 host=`hostname`
137 $NSLOOKUP_PROG $host | $AWK_PROG '{ if ($1 == "Name:") print $2 }'
138}
139
140# check if run as root
141#
142_need_to_be_root()
143{
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
149 fi
150}
151
152
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.
162
163_do()
164{
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
174 fi
175
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"
184 fi
185 fi
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
193 fi
194
195 return $ret
196}
197
198# bail out, setting up .notrun file
199#
200_notrun()
201{
202 echo "$*" >$seq.notrun
203 echo "$seq not run: $*"
204 status=0
205 exit
206}
207
208# just plain bail out
209#
210_fail()
211{
212 echo "$*" | tee -a $here/$seq.full
213 echo "(see $seq.full for details)"
214 status=1
215 exit 1
216}
217
218# tests whether $IMGFMT is one of the supported image formats for a test
219#
220_supported_fmt()
221{
222 for f; do
223 if [ "$f" = "$IMGFMT" -o "$f" = "generic" ]; then
224 return
225 fi
226 done
227
228 _notrun "not suitable for this image format: $IMGFMT"
229}
230
9cdfa1b3
MK
231# tests whether $IMGPROTO is one of the supported image protocols for a test
232#
233_supported_proto()
234{
235 for f; do
236 if [ "$f" = "$IMGPROTO" -o "$f" = "generic" ]; then
237 return
238 fi
239 done
240
241 _notrun "not suitable for this image protocol: $IMGPROTO"
242}
243
6bf19c94
CH
244# tests whether the host OS is one of the supported OSes for a test
245#
246_supported_os()
247{
248 for h
249 do
250 if [ "$h" = "$HOSTOS" ]
251 then
252 return
253 fi
254 done
255
256 _notrun "not suitable for this OS: $HOSTOS"
257}
258
259# this test requires that a specified command (executable) exists
260#
261_require_command()
262{
263 [ -x "$1" ] || _notrun "$1 utility required, skipped this test"
264}
265
266_full_imgfmt_details()
267{
268 echo "$IMGFMT"
269}
270
9cdfa1b3
MK
271_full_imgproto_details()
272{
273 echo "$IMGPROTO"
274}
275
6bf19c94
CH
276_full_platform_details()
277{
278 os=`uname -s`
279 host=`hostname -s`
280 kernel=`uname -r`
281 platform=`uname -m`
282 echo "$os/$platform $host $kernel"
283}
284
285_link_out_file()
286{
287 if [ -z "$1" ]; then
288 echo Error must pass \$seq.
289 exit
290 fi
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
299 fi
300}
301
302_die()
303{
304 echo $@
305 exit 1
306}
307
308# make sure this script returns success
309/bin/true