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