]> git.proxmox.com Git - qemu.git/blame - tests/qemu-iotests/common.rc
block: vhdx - update _make_test_img() to filter out vhdx options
[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" \
228b234d
JC
160 -e "s# lazy_refcounts=\\(on\\|off\\)##g" \
161 -e "s# block_size=[0-9]\\+##g" \
162 -e "s# block_state_zero=\\(on\\|off\\)##g" \
163 -e "s# log_size=[0-9]\\+##g"
a9660664
NT
164
165 # Start an NBD server on the image file, which is what we'll be talking to
166 if [ $IMGPROTO = "nbd" ]; then
167 eval "$QEMU_NBD -v -t -b 127.0.0.1 -p 10810 $TEST_IMG_FILE &"
168 QEMU_NBD_PID=$!
169 sleep 1 # FIXME: qemu-nbd needs to be listening before we continue
170 fi
6bf19c94
CH
171}
172
173_cleanup_test_img()
174{
9cdfa1b3
MK
175 case "$IMGPROTO" in
176
a9660664
NT
177 nbd)
178 kill $QEMU_NBD_PID
fef9c191 179 rm -f "$TEST_IMG_FILE"
a9660664 180 ;;
9cdfa1b3 181 file)
fef9c191
JC
182 rm -f "$TEST_DIR/t.$IMGFMT"
183 rm -f "$TEST_DIR/t.$IMGFMT.orig"
184 rm -f "$TEST_DIR/t.$IMGFMT.base"
85edbd37
JC
185 if [ -n "$SAMPLE_IMG_FILE" ]
186 then
187 rm -f "$TEST_DIR/$SAMPLE_IMG_FILE"
188 fi
9cdfa1b3
MK
189 ;;
190
191 rbd)
fef9c191 192 rbd rm "$TEST_DIR/t.$IMGFMT" > /dev/null
9cdfa1b3
MK
193 ;;
194
195 sheepdog)
fef9c191 196 collie vdi delete "$TEST_DIR/t.$IMGFMT"
9cdfa1b3
MK
197 ;;
198
199 esac
6bf19c94
CH
200}
201
202_check_test_img()
203{
fef9c191 204 $QEMU_IMG check "$@" -f $IMGFMT "$TEST_IMG" 2>&1 | _filter_testdir | \
e6439d78 205 sed -e '/allocated.*fragmented.*compressed clusters/d' \
c6bb9ad1
FS
206 -e 's/qemu-img: This image format does not support checks/No errors were found on the image./' \
207 -e '/Image end offset: [0-9]\+/d'
6bf19c94
CH
208}
209
514d9da5
SH
210_img_info()
211{
4c2e9465
MR
212 discard=0
213 regex_json_spec_start='^ *"format-specific": \{'
fef9c191 214 $QEMU_IMG info "$@" "$TEST_IMG" 2>&1 | \
514d9da5
SH
215 sed -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \
216 -e "s#$TEST_DIR#TEST_DIR#g" \
217 -e "s#$IMGFMT#IMGFMT#g" \
218 -e "/^disk size:/ D" \
4c2e9465
MR
219 -e "/actual-size/ D" | \
220 while IFS='' read line; do
221 if [[ $line == "Format specific information:" ]]; then
222 discard=1
223 elif [[ $line =~ $regex_json_spec_start ]]; then
224 discard=2
225 regex_json_spec_end="^${line%%[^ ]*}\\},? *$"
226 fi
227 if [[ $discard == 0 ]]; then
228 echo "$line"
229 elif [[ $discard == 1 && ! $line ]]; then
230 echo
231 discard=0
232 elif [[ $discard == 2 && $line =~ $regex_json_spec_end ]]; then
233 discard=0
234 fi
235 done
514d9da5
SH
236}
237
6bf19c94
CH
238_get_pids_by_name()
239{
240 if [ $# -ne 1 ]
241 then
79e40ab1
KW
242 echo "Usage: _get_pids_by_name process-name" 1>&2
243 exit 1
6bf19c94
CH
244 fi
245
246 # Algorithm ... all ps(1) variants have a time of the form MM:SS or
247 # HH:MM:SS before the psargs field, use this as the search anchor.
248 #
249 # Matches with $1 (process-name) occur if the first psarg is $1
250 # or ends in /$1 ... the matching uses sed's regular expressions,
251 # so passing a regex into $1 will work.
252
253 ps $PS_ALL_FLAGS \
254 | sed -n \
79e40ab1
KW
255 -e 's/$/ /' \
256 -e 's/[ ][ ]*/ /g' \
257 -e 's/^ //' \
258 -e 's/^[^ ]* //' \
259 -e "/[0-9]:[0-9][0-9] *[^ ]*\/$1 /s/ .*//p" \
260 -e "/[0-9]:[0-9][0-9] *$1 /s/ .*//p"
6bf19c94
CH
261}
262
263# fqdn for localhost
264#
265_get_fqdn()
266{
267 host=`hostname`
268 $NSLOOKUP_PROG $host | $AWK_PROG '{ if ($1 == "Name:") print $2 }'
269}
270
271# check if run as root
272#
273_need_to_be_root()
274{
275 id=`id | $SED_PROG -e 's/(.*//' -e 's/.*=//'`
276 if [ "$id" -ne 0 ]
277 then
79e40ab1
KW
278 echo "Arrgh ... you need to be root (not uid=$id) to run this test"
279 exit 1
6bf19c94
CH
280 fi
281}
282
283
284# Do a command, log it to $seq.full, optionally test return status
285# and die if command fails. If called with one argument _do executes the
286# command, logs it, and returns its exit status. With two arguments _do
287# first prints the message passed in the first argument, and then "done"
288# or "fail" depending on the return status of the command passed in the
289# second argument. If the command fails and the variable _do_die_on_error
290# is set to "always" or the two argument form is used and _do_die_on_error
291# is set to "message_only" _do will print an error message to
292# $seq.out and exit.
293
294_do()
295{
296 if [ $# -eq 1 ]; then
79e40ab1 297 _cmd=$1
6bf19c94 298 elif [ $# -eq 2 ]; then
79e40ab1
KW
299 _note=$1
300 _cmd=$2
301 echo -n "$_note... "
6bf19c94 302 else
79e40ab1
KW
303 echo "Usage: _do [note] cmd" 1>&2
304 status=1; exit
6bf19c94
CH
305 fi
306
307 (eval "echo '---' \"$_cmd\"") >>$here/$seq.full
308 (eval "$_cmd") >$tmp._out 2>&1; ret=$?
309 cat $tmp._out >>$here/$seq.full
310 if [ $# -eq 2 ]; then
79e40ab1
KW
311 if [ $ret -eq 0 ]; then
312 echo "done"
313 else
314 echo "fail"
315 fi
6bf19c94
CH
316 fi
317 if [ $ret -ne 0 ] \
79e40ab1
KW
318 && [ "$_do_die_on_error" = "always" \
319 -o \( $# -eq 2 -a "$_do_die_on_error" = "message_only" \) ]
6bf19c94 320 then
79e40ab1
KW
321 [ $# -ne 2 ] && echo
322 eval "echo \"$_cmd\" failed \(returned $ret\): see $seq.full"
323 status=1; exit
6bf19c94
CH
324 fi
325
326 return $ret
327}
328
329# bail out, setting up .notrun file
330#
331_notrun()
332{
333 echo "$*" >$seq.notrun
334 echo "$seq not run: $*"
335 status=0
336 exit
337}
338
339# just plain bail out
340#
341_fail()
342{
343 echo "$*" | tee -a $here/$seq.full
344 echo "(see $seq.full for details)"
345 status=1
346 exit 1
347}
348
349# tests whether $IMGFMT is one of the supported image formats for a test
350#
351_supported_fmt()
352{
353 for f; do
89e91181 354 if [ "$f" = "$IMGFMT" -o "$f" = "generic" -a "$IMGFMT_GENERIC" = "true" ]; then
79e40ab1
KW
355 return
356 fi
6bf19c94
CH
357 done
358
359 _notrun "not suitable for this image format: $IMGFMT"
360}
361
9cdfa1b3
MK
362# tests whether $IMGPROTO is one of the supported image protocols for a test
363#
364_supported_proto()
365{
366 for f; do
79e40ab1
KW
367 if [ "$f" = "$IMGPROTO" -o "$f" = "generic" ]; then
368 return
369 fi
9cdfa1b3
MK
370 done
371
372 _notrun "not suitable for this image protocol: $IMGPROTO"
373}
374
6bf19c94
CH
375# tests whether the host OS is one of the supported OSes for a test
376#
377_supported_os()
378{
379 for h
380 do
79e40ab1
KW
381 if [ "$h" = "$HOSTOS" ]
382 then
383 return
384 fi
6bf19c94
CH
385 done
386
387 _notrun "not suitable for this OS: $HOSTOS"
388}
389
166f3c7b
SH
390_unsupported_qemu_io_options()
391{
392 for bad_opt
393 do
394 for opt in $QEMU_IO_OPTIONS
395 do
396 if [ "$bad_opt" = "$opt" ]
397 then
398 _notrun "not suitable for qemu-io option: $bad_opt"
399 fi
400 done
401 done
402}
403
6bf19c94
CH
404# this test requires that a specified command (executable) exists
405#
406_require_command()
407{
408 [ -x "$1" ] || _notrun "$1 utility required, skipped this test"
409}
410
411_full_imgfmt_details()
412{
89004368
KW
413 if [ -n "$IMGOPTS" ]; then
414 echo "$IMGFMT ($IMGOPTS)"
415 else
416 echo "$IMGFMT"
417 fi
6bf19c94
CH
418}
419
9cdfa1b3
MK
420_full_imgproto_details()
421{
422 echo "$IMGPROTO"
423}
424
6bf19c94
CH
425_full_platform_details()
426{
427 os=`uname -s`
428 host=`hostname -s`
429 kernel=`uname -r`
430 platform=`uname -m`
431 echo "$os/$platform $host $kernel"
432}
433
434_link_out_file()
435{
436 if [ -z "$1" ]; then
437 echo Error must pass \$seq.
438 exit
439 fi
440 rm -f $1
441 if [ "`uname`" == "IRIX64" ] || [ "`uname`" == "IRIX" ]; then
442 ln -s $1.irix $1
443 elif [ "`uname`" == "Linux" ]; then
444 ln -s $1.linux $1
445 else
446 echo Error test $seq does not run on the operating system: `uname`
447 exit
448 fi
449}
450
451_die()
452{
453 echo $@
454 exit 1
455}
456
457# make sure this script returns success
458/bin/true