]> git.proxmox.com Git - qemu.git/blame - tests/qemu-iotests/common.rc
block: vhdx - add .bdrv_create() support
[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" ]
79e40ab1
KW
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
6bf19c94 32 else
79e40ab1 33 command dd $@
6bf19c94
CH
34 fi
35}
36
23ea2ecc
SH
37# poke_file 'test.img' 512 '\xff\xfe'
38poke_file()
39{
40 printf "$3" | dd "of=$1" bs=1 "seek=$2" conv=notrunc &>/dev/null
41}
42
6bf19c94
CH
43# we need common.config
44if [ "$iam" != "check" ]
45then
46 if ! . ./common.config
47 then
48 echo "$iam: failed to source common.config"
49 exit 1
50 fi
51fi
52
53# make sure we have a standard umask
54umask 022
55
9cdfa1b3
MK
56if [ "$IMGPROTO" = "file" ]; then
57 TEST_IMG=$TEST_DIR/t.$IMGFMT
a9660664
NT
58elif [ "$IMGPROTO" = "nbd" ]; then
59 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
60 TEST_IMG="nbd:127.0.0.1:10810"
342809e8
RJ
61elif [ "$IMGPROTO" = "ssh" ]; then
62 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
63 TEST_IMG="ssh://127.0.0.1$TEST_IMG_FILE"
9cdfa1b3
MK
64else
65 TEST_IMG=$IMGPROTO:$TEST_DIR/t.$IMGFMT
66fi
6bf19c94 67
2f24e8fb
KW
68function valgrind_qemu_io()
69{
70 valgrind --log-file=/tmp/$$.valgrind --error-exitcode=99 $REAL_QEMU_IO "$@"
71 if [ $? != 0 ]; then
72 cat /tmp/$$.valgrind
73 fi
74 rm -f /tmp/$$.valgrind
75}
76
77
89004368
KW
78_optstr_add()
79{
80 if [ -n "$1" ]; then
81 echo "$1,$2"
82 else
83 echo "$2"
84 fi
85}
86
87_set_default_imgopts()
88{
89 if [ "$IMGFMT" == "qcow2" ] && ! (echo "$IMGOPTS" | grep "compat=" > /dev/null); then
90 IMGOPTS=$(_optstr_add "$IMGOPTS" "compat=1.1")
91 fi
92}
93
85edbd37
JC
94_use_sample_img()
95{
96 SAMPLE_IMG_FILE="${1%\.bz2}"
97 TEST_IMG="$TEST_DIR/$SAMPLE_IMG_FILE"
98 bzcat "$SAMPLE_IMG_DIR/$1" > "$TEST_IMG"
99 if [ $? -ne 0 ]
100 then
101 echo "_use_sample_img error, cannot extract '$SAMPLE_IMG_DIR/$1'"
102 exit 1
103 fi
104}
105
6bf19c94
CH
106_make_test_img()
107{
108 # extra qemu-img options can be added by tests
109 # at least one argument (the image size) needs to be added
21af8148 110 local extra_img_options=""
21af8148 111 local image_size=$*
89004368 112 local optstr=""
a9660664 113 local img_name=""
0018c03f
JC
114 local use_backing=0
115 local backing_file=""
a9660664
NT
116
117 if [ -n "$TEST_IMG_FILE" ]; then
118 img_name=$TEST_IMG_FILE
119 else
120 img_name=$TEST_IMG
121 fi
89004368
KW
122
123 if [ -n "$IMGOPTS" ]; then
124 optstr=$(_optstr_add "$optstr" "$IMGOPTS")
125 fi
6bf19c94 126
21af8148 127 if [ "$1" = "-b" ]; then
0018c03f
JC
128 use_backing=1
129 backing_file=$2
21af8148
SW
130 image_size=$3
131 fi
f5a4bbd9 132 if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then
89004368
KW
133 optstr=$(_optstr_add "$optstr" "cluster_size=$CLUSTER_SIZE")
134 fi
135
136 if [ -n "$optstr" ]; then
137 extra_img_options="-o $optstr $extra_img_options"
8fc1024c
KW
138 fi
139
6bf19c94 140 # XXX(hch): have global image options?
0018c03f
JC
141 (
142 if [ $use_backing = 1 ]; then
143 $QEMU_IMG create -f $IMGFMT $extra_img_options -b "$backing_file" "$img_name" $image_size 2>&1
144 else
145 $QEMU_IMG create -f $IMGFMT $extra_img_options "$img_name" $image_size 2>&1
146 fi
147 ) | \
353a41be
KW
148 sed -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \
149 -e "s#$TEST_DIR#TEST_DIR#g" \
150 -e "s#$IMGFMT#IMGFMT#g" \
151 -e "s# encryption=off##g" \
152 -e "s# cluster_size=[0-9]\\+##g" \
153 -e "s# table_size=[0-9]\\+##g" \
154 -e "s# compat='[^']*'##g" \
155 -e "s# compat6=\\(on\\|off\\)##g" \
156 -e "s# static=\\(on\\|off\\)##g" \
50522d96
FZ
157 -e "s# zeroed_grain=\\(on\\|off\\)##g" \
158 -e "s# subformat='[^']*'##g" \
86abefd6 159 -e "s# adapter_type='[^']*'##g" \
353a41be 160 -e "s# lazy_refcounts=\\(on\\|off\\)##g"
a9660664
NT
161
162 # Start an NBD server on the image file, which is what we'll be talking to
163 if [ $IMGPROTO = "nbd" ]; then
164 eval "$QEMU_NBD -v -t -b 127.0.0.1 -p 10810 $TEST_IMG_FILE &"
165 QEMU_NBD_PID=$!
166 sleep 1 # FIXME: qemu-nbd needs to be listening before we continue
167 fi
6bf19c94
CH
168}
169
170_cleanup_test_img()
171{
9cdfa1b3
MK
172 case "$IMGPROTO" in
173
a9660664
NT
174 nbd)
175 kill $QEMU_NBD_PID
fef9c191 176 rm -f "$TEST_IMG_FILE"
a9660664 177 ;;
9cdfa1b3 178 file)
fef9c191
JC
179 rm -f "$TEST_DIR/t.$IMGFMT"
180 rm -f "$TEST_DIR/t.$IMGFMT.orig"
181 rm -f "$TEST_DIR/t.$IMGFMT.base"
85edbd37
JC
182 if [ -n "$SAMPLE_IMG_FILE" ]
183 then
184 rm -f "$TEST_DIR/$SAMPLE_IMG_FILE"
185 fi
9cdfa1b3
MK
186 ;;
187
188 rbd)
fef9c191 189 rbd rm "$TEST_DIR/t.$IMGFMT" > /dev/null
9cdfa1b3
MK
190 ;;
191
192 sheepdog)
fef9c191 193 collie vdi delete "$TEST_DIR/t.$IMGFMT"
9cdfa1b3
MK
194 ;;
195
196 esac
6bf19c94
CH
197}
198
199_check_test_img()
200{
fef9c191 201 $QEMU_IMG check "$@" -f $IMGFMT "$TEST_IMG" 2>&1 | _filter_testdir | \
e6439d78 202 sed -e '/allocated.*fragmented.*compressed clusters/d' \
c6bb9ad1
FS
203 -e 's/qemu-img: This image format does not support checks/No errors were found on the image./' \
204 -e '/Image end offset: [0-9]\+/d'
6bf19c94
CH
205}
206
514d9da5
SH
207_img_info()
208{
4c2e9465
MR
209 discard=0
210 regex_json_spec_start='^ *"format-specific": \{'
fef9c191 211 $QEMU_IMG info "$@" "$TEST_IMG" 2>&1 | \
514d9da5
SH
212 sed -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \
213 -e "s#$TEST_DIR#TEST_DIR#g" \
214 -e "s#$IMGFMT#IMGFMT#g" \
215 -e "/^disk size:/ D" \
4c2e9465
MR
216 -e "/actual-size/ D" | \
217 while IFS='' read line; do
218 if [[ $line == "Format specific information:" ]]; then
219 discard=1
220 elif [[ $line =~ $regex_json_spec_start ]]; then
221 discard=2
222 regex_json_spec_end="^${line%%[^ ]*}\\},? *$"
223 fi
224 if [[ $discard == 0 ]]; then
225 echo "$line"
226 elif [[ $discard == 1 && ! $line ]]; then
227 echo
228 discard=0
229 elif [[ $discard == 2 && $line =~ $regex_json_spec_end ]]; then
230 discard=0
231 fi
232 done
514d9da5
SH
233}
234
6bf19c94
CH
235_get_pids_by_name()
236{
237 if [ $# -ne 1 ]
238 then
79e40ab1
KW
239 echo "Usage: _get_pids_by_name process-name" 1>&2
240 exit 1
6bf19c94
CH
241 fi
242
243 # Algorithm ... all ps(1) variants have a time of the form MM:SS or
244 # HH:MM:SS before the psargs field, use this as the search anchor.
245 #
246 # Matches with $1 (process-name) occur if the first psarg is $1
247 # or ends in /$1 ... the matching uses sed's regular expressions,
248 # so passing a regex into $1 will work.
249
250 ps $PS_ALL_FLAGS \
251 | sed -n \
79e40ab1
KW
252 -e 's/$/ /' \
253 -e 's/[ ][ ]*/ /g' \
254 -e 's/^ //' \
255 -e 's/^[^ ]* //' \
256 -e "/[0-9]:[0-9][0-9] *[^ ]*\/$1 /s/ .*//p" \
257 -e "/[0-9]:[0-9][0-9] *$1 /s/ .*//p"
6bf19c94
CH
258}
259
260# fqdn for localhost
261#
262_get_fqdn()
263{
264 host=`hostname`
265 $NSLOOKUP_PROG $host | $AWK_PROG '{ if ($1 == "Name:") print $2 }'
266}
267
268# check if run as root
269#
270_need_to_be_root()
271{
272 id=`id | $SED_PROG -e 's/(.*//' -e 's/.*=//'`
273 if [ "$id" -ne 0 ]
274 then
79e40ab1
KW
275 echo "Arrgh ... you need to be root (not uid=$id) to run this test"
276 exit 1
6bf19c94
CH
277 fi
278}
279
280
281# Do a command, log it to $seq.full, optionally test return status
282# and die if command fails. If called with one argument _do executes the
283# command, logs it, and returns its exit status. With two arguments _do
284# first prints the message passed in the first argument, and then "done"
285# or "fail" depending on the return status of the command passed in the
286# second argument. If the command fails and the variable _do_die_on_error
287# is set to "always" or the two argument form is used and _do_die_on_error
288# is set to "message_only" _do will print an error message to
289# $seq.out and exit.
290
291_do()
292{
293 if [ $# -eq 1 ]; then
79e40ab1 294 _cmd=$1
6bf19c94 295 elif [ $# -eq 2 ]; then
79e40ab1
KW
296 _note=$1
297 _cmd=$2
298 echo -n "$_note... "
6bf19c94 299 else
79e40ab1
KW
300 echo "Usage: _do [note] cmd" 1>&2
301 status=1; exit
6bf19c94
CH
302 fi
303
304 (eval "echo '---' \"$_cmd\"") >>$here/$seq.full
305 (eval "$_cmd") >$tmp._out 2>&1; ret=$?
306 cat $tmp._out >>$here/$seq.full
307 if [ $# -eq 2 ]; then
79e40ab1
KW
308 if [ $ret -eq 0 ]; then
309 echo "done"
310 else
311 echo "fail"
312 fi
6bf19c94
CH
313 fi
314 if [ $ret -ne 0 ] \
79e40ab1
KW
315 && [ "$_do_die_on_error" = "always" \
316 -o \( $# -eq 2 -a "$_do_die_on_error" = "message_only" \) ]
6bf19c94 317 then
79e40ab1
KW
318 [ $# -ne 2 ] && echo
319 eval "echo \"$_cmd\" failed \(returned $ret\): see $seq.full"
320 status=1; exit
6bf19c94
CH
321 fi
322
323 return $ret
324}
325
326# bail out, setting up .notrun file
327#
328_notrun()
329{
330 echo "$*" >$seq.notrun
331 echo "$seq not run: $*"
332 status=0
333 exit
334}
335
336# just plain bail out
337#
338_fail()
339{
340 echo "$*" | tee -a $here/$seq.full
341 echo "(see $seq.full for details)"
342 status=1
343 exit 1
344}
345
346# tests whether $IMGFMT is one of the supported image formats for a test
347#
348_supported_fmt()
349{
350 for f; do
89e91181 351 if [ "$f" = "$IMGFMT" -o "$f" = "generic" -a "$IMGFMT_GENERIC" = "true" ]; then
79e40ab1
KW
352 return
353 fi
6bf19c94
CH
354 done
355
356 _notrun "not suitable for this image format: $IMGFMT"
357}
358
9cdfa1b3
MK
359# tests whether $IMGPROTO is one of the supported image protocols for a test
360#
361_supported_proto()
362{
363 for f; do
79e40ab1
KW
364 if [ "$f" = "$IMGPROTO" -o "$f" = "generic" ]; then
365 return
366 fi
9cdfa1b3
MK
367 done
368
369 _notrun "not suitable for this image protocol: $IMGPROTO"
370}
371
6bf19c94
CH
372# tests whether the host OS is one of the supported OSes for a test
373#
374_supported_os()
375{
376 for h
377 do
79e40ab1
KW
378 if [ "$h" = "$HOSTOS" ]
379 then
380 return
381 fi
6bf19c94
CH
382 done
383
384 _notrun "not suitable for this OS: $HOSTOS"
385}
386
166f3c7b
SH
387_unsupported_qemu_io_options()
388{
389 for bad_opt
390 do
391 for opt in $QEMU_IO_OPTIONS
392 do
393 if [ "$bad_opt" = "$opt" ]
394 then
395 _notrun "not suitable for qemu-io option: $bad_opt"
396 fi
397 done
398 done
399}
400
6bf19c94
CH
401# this test requires that a specified command (executable) exists
402#
403_require_command()
404{
405 [ -x "$1" ] || _notrun "$1 utility required, skipped this test"
406}
407
408_full_imgfmt_details()
409{
89004368
KW
410 if [ -n "$IMGOPTS" ]; then
411 echo "$IMGFMT ($IMGOPTS)"
412 else
413 echo "$IMGFMT"
414 fi
6bf19c94
CH
415}
416
9cdfa1b3
MK
417_full_imgproto_details()
418{
419 echo "$IMGPROTO"
420}
421
6bf19c94
CH
422_full_platform_details()
423{
424 os=`uname -s`
425 host=`hostname -s`
426 kernel=`uname -r`
427 platform=`uname -m`
428 echo "$os/$platform $host $kernel"
429}
430
431_link_out_file()
432{
433 if [ -z "$1" ]; then
434 echo Error must pass \$seq.
435 exit
436 fi
437 rm -f $1
438 if [ "`uname`" == "IRIX64" ] || [ "`uname`" == "IRIX" ]; then
439 ln -s $1.irix $1
440 elif [ "`uname`" == "Linux" ]; then
441 ln -s $1.linux $1
442 else
443 echo Error test $seq does not run on the operating system: `uname`
444 exit
445 fi
446}
447
448_die()
449{
450 echo $@
451 exit 1
452}
453
454# make sure this script returns success
455/bin/true