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