]> git.proxmox.com Git - mirror_qemu.git/blame - tests/qemu-iotests/common.rc
qcow2: Zero write support
[mirror_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
21af8148 60 local extra_img_options=""
21af8148 61 local image_size=$*
6bf19c94 62
21af8148
SW
63 if [ "$1" = "-b" ]; then
64 extra_img_options="$1 $2"
65 image_size=$3
66 fi
f5a4bbd9 67 if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then
8fc1024c
KW
68 extra_img_options="-o cluster_size=$CLUSTER_SIZE $extra_img_options"
69 fi
70
6bf19c94 71 # XXX(hch): have global image options?
21af8148 72 $QEMU_IMG create -f $IMGFMT $extra_img_options $TEST_IMG $image_size | \
9cdfa1b3 73 sed -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" | \
6bf19c94
CH
74 sed -e "s#$TEST_DIR#TEST_DIR#g" | \
75 sed -e "s#$IMGFMT#IMGFMT#g" | \
76 sed -e "s# encryption=off##g" | \
7299550b 77 sed -e "s# cluster_size=[0-9]\\+##g" | \
f5a4bbd9 78 sed -e "s# table_size=0##g" | \
3b5fe6e6
SW
79 sed -e "s# compat6=off##g" | \
80 sed -e "s# static=off##g"
6bf19c94
CH
81}
82
83_cleanup_test_img()
84{
9cdfa1b3
MK
85 case "$IMGPROTO" in
86
87 file)
88 rm -f $TEST_DIR/t.$IMGFMT
89 rm -f $TEST_DIR/t.$IMGFMT.orig
90 rm -f $TEST_DIR/t.$IMGFMT.base
91 ;;
92
93 rbd)
94 rbd rm $TEST_DIR/t.$IMGFMT > /dev/null
95 ;;
96
97 sheepdog)
98 collie vdi delete $TEST_DIR/t.$IMGFMT
99 ;;
100
101 esac
6bf19c94
CH
102}
103
104_check_test_img()
105{
e76a8e89
CH
106 $QEMU_IMG check -f $IMGFMT $TEST_IMG 2>&1 | \
107 sed -e 's/qemu-img\: This image format does not support checks/No errors were found on the image./'
6bf19c94
CH
108}
109
110_get_pids_by_name()
111{
112 if [ $# -ne 1 ]
113 then
114 echo "Usage: _get_pids_by_name process-name" 1>&2
115 exit 1
116 fi
117
118 # Algorithm ... all ps(1) variants have a time of the form MM:SS or
119 # HH:MM:SS before the psargs field, use this as the search anchor.
120 #
121 # Matches with $1 (process-name) occur if the first psarg is $1
122 # or ends in /$1 ... the matching uses sed's regular expressions,
123 # so passing a regex into $1 will work.
124
125 ps $PS_ALL_FLAGS \
126 | sed -n \
127 -e 's/$/ /' \
128 -e 's/[ ][ ]*/ /g' \
129 -e 's/^ //' \
130 -e 's/^[^ ]* //' \
131 -e "/[0-9]:[0-9][0-9] *[^ ]*\/$1 /s/ .*//p" \
132 -e "/[0-9]:[0-9][0-9] *$1 /s/ .*//p"
133}
134
135# fqdn for localhost
136#
137_get_fqdn()
138{
139 host=`hostname`
140 $NSLOOKUP_PROG $host | $AWK_PROG '{ if ($1 == "Name:") print $2 }'
141}
142
143# check if run as root
144#
145_need_to_be_root()
146{
147 id=`id | $SED_PROG -e 's/(.*//' -e 's/.*=//'`
148 if [ "$id" -ne 0 ]
149 then
150 echo "Arrgh ... you need to be root (not uid=$id) to run this test"
151 exit 1
152 fi
153}
154
155
156# Do a command, log it to $seq.full, optionally test return status
157# and die if command fails. If called with one argument _do executes the
158# command, logs it, and returns its exit status. With two arguments _do
159# first prints the message passed in the first argument, and then "done"
160# or "fail" depending on the return status of the command passed in the
161# second argument. If the command fails and the variable _do_die_on_error
162# is set to "always" or the two argument form is used and _do_die_on_error
163# is set to "message_only" _do will print an error message to
164# $seq.out and exit.
165
166_do()
167{
168 if [ $# -eq 1 ]; then
169 _cmd=$1
170 elif [ $# -eq 2 ]; then
171 _note=$1
172 _cmd=$2
173 echo -n "$_note... "
174 else
175 echo "Usage: _do [note] cmd" 1>&2
176 status=1; exit
177 fi
178
179 (eval "echo '---' \"$_cmd\"") >>$here/$seq.full
180 (eval "$_cmd") >$tmp._out 2>&1; ret=$?
181 cat $tmp._out >>$here/$seq.full
182 if [ $# -eq 2 ]; then
183 if [ $ret -eq 0 ]; then
184 echo "done"
185 else
186 echo "fail"
187 fi
188 fi
189 if [ $ret -ne 0 ] \
190 && [ "$_do_die_on_error" = "always" \
191 -o \( $# -eq 2 -a "$_do_die_on_error" = "message_only" \) ]
192 then
193 [ $# -ne 2 ] && echo
194 eval "echo \"$_cmd\" failed \(returned $ret\): see $seq.full"
195 status=1; exit
196 fi
197
198 return $ret
199}
200
201# bail out, setting up .notrun file
202#
203_notrun()
204{
205 echo "$*" >$seq.notrun
206 echo "$seq not run: $*"
207 status=0
208 exit
209}
210
211# just plain bail out
212#
213_fail()
214{
215 echo "$*" | tee -a $here/$seq.full
216 echo "(see $seq.full for details)"
217 status=1
218 exit 1
219}
220
221# tests whether $IMGFMT is one of the supported image formats for a test
222#
223_supported_fmt()
224{
225 for f; do
226 if [ "$f" = "$IMGFMT" -o "$f" = "generic" ]; then
227 return
228 fi
229 done
230
231 _notrun "not suitable for this image format: $IMGFMT"
232}
233
9cdfa1b3
MK
234# tests whether $IMGPROTO is one of the supported image protocols for a test
235#
236_supported_proto()
237{
238 for f; do
239 if [ "$f" = "$IMGPROTO" -o "$f" = "generic" ]; then
240 return
241 fi
242 done
243
244 _notrun "not suitable for this image protocol: $IMGPROTO"
245}
246
6bf19c94
CH
247# tests whether the host OS is one of the supported OSes for a test
248#
249_supported_os()
250{
251 for h
252 do
253 if [ "$h" = "$HOSTOS" ]
254 then
255 return
256 fi
257 done
258
259 _notrun "not suitable for this OS: $HOSTOS"
260}
261
262# this test requires that a specified command (executable) exists
263#
264_require_command()
265{
266 [ -x "$1" ] || _notrun "$1 utility required, skipped this test"
267}
268
269_full_imgfmt_details()
270{
271 echo "$IMGFMT"
272}
273
9cdfa1b3
MK
274_full_imgproto_details()
275{
276 echo "$IMGPROTO"
277}
278
6bf19c94
CH
279_full_platform_details()
280{
281 os=`uname -s`
282 host=`hostname -s`
283 kernel=`uname -r`
284 platform=`uname -m`
285 echo "$os/$platform $host $kernel"
286}
287
288_link_out_file()
289{
290 if [ -z "$1" ]; then
291 echo Error must pass \$seq.
292 exit
293 fi
294 rm -f $1
295 if [ "`uname`" == "IRIX64" ] || [ "`uname`" == "IRIX" ]; then
296 ln -s $1.irix $1
297 elif [ "`uname`" == "Linux" ]; then
298 ln -s $1.linux $1
299 else
300 echo Error test $seq does not run on the operating system: `uname`
301 exit
302 fi
303}
304
305_die()
306{
307 echo $@
308 exit 1
309}
310
311# make sure this script returns success
312/bin/true