1#!/usr/bin/env bash
2#
3# Test command line configuration of block devices and driver-specific options
4#
5# Copyright (C) 2013 Red Hat, Inc.
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19#
20
21# creator
22owner=kwolf@redhat.com
23
24seq=`basename $0`
25echo "QA output created by $seq"
26
27status=1	# failure is the default!
28
29_cleanup()
30{
31	_cleanup_test_img
32}
33trap "_cleanup; exit \$status" 0 1 2 3 15
34
35# get standard environment, filters and checks
36. ./common.rc
37. ./common.filter
38
39_supported_fmt qcow2
40_supported_proto file
41# A compat=0.10 image is created in this test which does not support anything
42# other than refcount_bits=16
43_unsupported_imgopts 'refcount_bits=\([^1]\|.\([^6]\|$\)\)'
44
45do_run_qemu()
46{
47    echo Testing: "$@"
48    (
49        if ! test -t 0; then
50            while read cmd; do
51                echo $cmd
52            done
53        fi
54        echo quit
55    ) | $QEMU -nographic -monitor stdio -serial none "$@"
56    echo
57}
58
59run_qemu()
60{
61    do_run_qemu "$@" 2>&1 | _filter_testdir | _filter_qemu |
62        _filter_generated_node_ids | _filter_hmp
63}
64
65size=128M
66device_id="drive0"
67
68_make_test_img $size
69cp "$TEST_IMG" "$TEST_IMG.orig"
70mv "$TEST_IMG" "$TEST_IMG.base"
71_make_test_img -b "$TEST_IMG.base" $size
72
73echo
74echo === Unknown option ===
75echo
76
77run_qemu -drive file="$TEST_IMG",format=qcow2,unknown_opt=,if=none,id=$device_id
78run_qemu -drive file="$TEST_IMG",format=qcow2,unknown_opt=on,if=none,id=$device_id
79run_qemu -drive file="$TEST_IMG",format=qcow2,unknown_opt=1234,if=none,id=$device_id
80run_qemu -drive file="$TEST_IMG",format=qcow2,unknown_opt=foo,if=none,id=$device_id
81
82echo
83echo === Unknown protocol option ===
84echo
85
86run_qemu -drive file="$TEST_IMG",format=qcow2,file.unknown_opt=
87run_qemu -drive file="$TEST_IMG",format=qcow2,file.unknown_opt=on
88run_qemu -drive file="$TEST_IMG",format=qcow2,file.unknown_opt=1234
89run_qemu -drive file="$TEST_IMG",format=qcow2,file.unknown_opt=foo
90
91echo
92echo === Invalid format ===
93echo
94
95run_qemu -drive file="$TEST_IMG",format=foo
96run_qemu -drive file="$TEST_IMG",driver=foo
97run_qemu -drive file="$TEST_IMG",driver=raw,format=qcow2
98run_qemu -drive file="$TEST_IMG",driver=qcow2,format=qcow2
99
100echo
101echo === Node names ===
102echo
103
104# Maximum length: 31 characters
105run_qemu -drive file="$TEST_IMG",node-name=x123456789012345678901234567890
106run_qemu -drive file="$TEST_IMG",node-name=x1234567890123456789012345678901
107
108# First character must be alphabetic
109# Following characters alphanumeric or -._
110run_qemu -drive file="$TEST_IMG",node-name=All-Types.of_all0wed_chars
111run_qemu -drive file="$TEST_IMG",node-name=123foo
112run_qemu -drive file="$TEST_IMG",node-name=_foo
113run_qemu -drive file="$TEST_IMG",node-name=foo#12
114
115echo
116echo === Device without drive ===
117echo
118
119case "$QEMU_DEFAULT_MACHINE" in
120  s390-ccw-virtio)
121      virtio_scsi=virtio-scsi-ccw
122      ;;
123  *)
124      virtio_scsi=virtio-scsi-pci
125      ;;
126esac
127
128run_qemu -device $virtio_scsi -device scsi-hd |
129    sed -e "s/$virtio_scsi/VIRTIO_SCSI/"
130
131echo
132echo === Overriding backing file ===
133echo
134
135echo "info block" | run_qemu -drive file="$TEST_IMG",driver=qcow2,backing.file.filename="$TEST_IMG.orig",if=none,id=$device_id -nodefaults\
136                  | _filter_generated_node_ids
137
138# Drivers that don't support backing files
139run_qemu -drive file="$TEST_IMG",driver=raw,backing.file.filename="$TEST_IMG.orig"
140run_qemu -drive file="$TEST_IMG",file.backing.driver=file,file.backing.filename="$TEST_IMG.orig"
141run_qemu -drive file="$TEST_IMG",file.backing.driver=qcow2,file.backing.file.filename="$TEST_IMG.orig"
142
143echo
144echo === Enable and disable lazy refcounting on the command line, plus some invalid values ===
145echo
146
147_make_test_img -o compat=1.1 "$size"
148
149run_qemu -drive file="$TEST_IMG",format=qcow2,lazy-refcounts=on
150run_qemu -drive file="$TEST_IMG",format=qcow2,lazy-refcounts=off
151run_qemu -drive file="$TEST_IMG",format=qcow2,lazy-refcounts=
152run_qemu -drive file="$TEST_IMG",format=qcow2,lazy-refcounts=42
153run_qemu -drive file="$TEST_IMG",format=qcow2,lazy-refcounts=foo
154
155
156echo
157echo === With version 2 images enabling lazy refcounts must fail ===
158echo
159
160_make_test_img -ocompat=0.10 $size
161
162run_qemu -drive file="$TEST_IMG",format=qcow2,lazy-refcounts=on
163run_qemu -drive file="$TEST_IMG",format=qcow2,lazy-refcounts=off
164
165echo
166echo === No medium ===
167echo
168
169case "$QEMU_DEFAULT_MACHINE" in
170    pc)
171        run_qemu -drive if=floppy
172        run_qemu -drive if=ide,media=cdrom
173        run_qemu -drive if=ide
174        ;;
175     *)
176        ;;
177esac
178
179run_qemu -drive if=virtio
180
181case "$QEMU_DEFAULT_MACHINE" in
182    pc)
183        run_qemu -drive if=none,id=disk -device ide-cd,drive=disk
184        run_qemu -drive if=none,id=disk -device lsi53c895a -device scsi-cd,drive=disk
185        run_qemu -drive if=none,id=disk -device ide-drive,drive=disk
186        run_qemu -drive if=none,id=disk -device ide-hd,drive=disk
187        run_qemu -drive if=none,id=disk -device lsi53c895a -device scsi-disk,drive=disk
188        run_qemu -drive if=none,id=disk -device lsi53c895a -device scsi-hd,drive=disk
189        ;;
190     *)
191        ;;
192esac
193
194echo
195echo === Attach to node in non-default iothread ===
196echo
197
198case "$QEMU_DEFAULT_MACHINE" in
199    pc)
200        iothread="-drive file=$TEST_IMG,if=none,node-name=disk -object iothread,id=thread0 -device virtio-scsi,iothread=thread0,id=virtio-scsi0 -device scsi-hd,bus=virtio-scsi0.0,drive=disk,share-rw=on"
201
202        # Can't add a device in the main thread while virtio-scsi0 uses the node
203        run_qemu $iothread -device ide-hd,drive=disk,share-rw=on
204        run_qemu $iothread -device virtio-blk-pci,drive=disk,share-rw=on
205        run_qemu $iothread -device lsi53c895a,id=lsi0 -device scsi-hd,bus=lsi0.0,drive=disk,share-rw=on
206        run_qemu $iothread -device virtio-scsi,id=virtio-scsi1 -device scsi-hd,bus=virtio-scsi1.0,drive=disk,share-rw=on
207
208        # virtio-blk enables the iothread only when the driver initialises the
209        # device, so a second virtio-blk device can't be added even with the
210        # same iothread. virtio-scsi allows this.
211        run_qemu $iothread -device virtio-blk-pci,drive=disk,iohtread=iothread0,share-rw=on
212        run_qemu $iothread -device virtio-scsi,id=virtio-scsi1,iothread=thread0 -device scsi-hd,bus=virtio-scsi1.0,drive=disk,share-rw=on
213        ;;
214     *)
215        ;;
216esac
217
218echo
219echo === Read-only ===
220echo
221
222case "$QEMU_DEFAULT_MACHINE" in
223    pc)
224        run_qemu -drive file="$TEST_IMG",if=floppy,readonly=on
225        run_qemu -drive file="$TEST_IMG",if=ide,media=cdrom,readonly=on
226        run_qemu -drive file="$TEST_IMG",if=ide,readonly=on
227        ;;
228     *)
229        ;;
230esac
231
232run_qemu -drive file="$TEST_IMG",if=virtio,readonly=on
233
234case "$QEMU_DEFAULT_MACHINE" in
235    pc)
236        run_qemu -drive file="$TEST_IMG",if=none,id=disk,readonly=on -device ide-cd,drive=disk
237        run_qemu -drive file="$TEST_IMG",if=none,id=disk,readonly=on -device lsi53c895a -device scsi-cd,drive=disk
238        run_qemu -drive file="$TEST_IMG",if=none,id=disk,readonly=on -device ide-drive,drive=disk
239        run_qemu -drive file="$TEST_IMG",if=none,id=disk,readonly=on -device ide-hd,drive=disk
240        run_qemu -drive file="$TEST_IMG",if=none,id=disk,readonly=on -device lsi53c895a -device scsi-disk,drive=disk
241        run_qemu -drive file="$TEST_IMG",if=none,id=disk,readonly=on -device lsi53c895a -device scsi-hd,drive=disk
242        ;;
243     *)
244        ;;
245esac
246
247echo
248echo === Cache modes ===
249echo
250
251# Cannot use the test image because cache=none might not work on the host FS
252# Use cdrom so that we won't get errors about missing media
253
254run_qemu -drive driver=null-co,read-zeroes=on,cache=none
255run_qemu -drive driver=null-co,read-zeroes=on,cache=directsync
256run_qemu -drive driver=null-co,read-zeroes=on,cache=writeback
257run_qemu -drive driver=null-co,read-zeroes=on,cache=writethrough
258run_qemu -drive driver=null-co,read-zeroes=on,cache=unsafe
259run_qemu -drive driver=null-co,cache=invalid_value
260
261# Can't test direct=on here because O_DIRECT might not be supported on this FS
262# Test 142 checks the direct=on cases
263
264for cache in writeback writethrough unsafe invalid_value; do
265    printf "info block %s\n" '' file backing backing-file | \
266    run_qemu -drive file="$TEST_IMG",cache=$cache,backing.file.filename="$TEST_IMG.base",backing.cache.no-flush=on,backing.node-name=backing,backing.file.node-name=backing-file,file.node-name=file,if=none,id=$device_id -nodefaults
267done
268
269echo
270echo === Specifying the protocol layer ===
271echo
272
273run_qemu -drive file="$TEST_IMG",file.driver=file
274
275echo
276echo === Leaving out required options ===
277echo
278
279run_qemu -drive driver=file
280run_qemu -drive driver=file,filename=
281run_qemu -drive driver=nbd
282run_qemu -drive driver=raw
283run_qemu -drive file.driver=file
284run_qemu -drive file.driver=nbd
285run_qemu -drive file.driver=raw
286run_qemu -drive foo=bar
287
288echo
289echo === Specifying both an option and its legacy alias ===
290echo
291
292run_qemu -drive file="$TEST_IMG",iops=1234,throttling.iops-total=5678
293run_qemu -drive file="$TEST_IMG",iops_rd=1234,throttling.iops-read=5678
294run_qemu -drive file="$TEST_IMG",iops_wr=1234,throttling.iops-write=5678
295
296run_qemu -drive file="$TEST_IMG",bps=1234,throttling.bps-total=5678
297run_qemu -drive file="$TEST_IMG",bps_rd=1234,throttling.bps-read=5678
298run_qemu -drive file="$TEST_IMG",bps_wr=1234,throttling.bps-write=5678
299
300run_qemu -drive file="$TEST_IMG",iops_max=1234,throttling.iops-total-max=5678
301run_qemu -drive file="$TEST_IMG",iops_rd_max=1234,throttling.iops-read-max=5678
302run_qemu -drive file="$TEST_IMG",iops_wr_max=1234,throttling.iops-write-max=5678
303
304run_qemu -drive file="$TEST_IMG",bps_max=1234,throttling.bps-total-max=5678
305run_qemu -drive file="$TEST_IMG",bps_rd_max=1234,throttling.bps-read-max=5678
306run_qemu -drive file="$TEST_IMG",bps_wr_max=1234,throttling.bps-write-max=5678
307
308run_qemu -drive file="$TEST_IMG",iops_size=1234,throttling.iops-size=5678
309run_qemu -drive file="$TEST_IMG",readonly=on,read-only=off
310
311echo
312echo === Catching negative/large throttling values ===
313echo
314
315run_qemu -drive file="$TEST_IMG",iops=-1
316run_qemu -drive file="$TEST_IMG",bps=-2
317run_qemu -drive file="$TEST_IMG",bps_rd=-3
318run_qemu -drive file="$TEST_IMG",bps_rd_max=-3
319run_qemu -drive file="$TEST_IMG",throttling.iops-total=-4
320run_qemu -drive file="$TEST_IMG",throttling.bps-total=-5
321# These are accepted
322run_qemu -drive file="$TEST_IMG",bps=0
323run_qemu -drive file="$TEST_IMG",bps=1
324run_qemu -drive file="$TEST_IMG",bps=1000000000000000
325# While these are not
326run_qemu -drive file="$TEST_IMG",bps=1000000000000001
327run_qemu -drive file="$TEST_IMG",bps=9999999999999999
328
329echo
330echo === Parsing protocol from file name ===
331echo
332
333# Protocol strings are supposed to be parsed from traditional option strings,
334# but not when using driver-specific options. We can distinguish them by the
335# error message for non-existing files.
336
337run_qemu -hda foo:bar
338run_qemu -drive file=foo:bar
339run_qemu -drive file.filename=foo:bar
340
341run_qemu -hda "file:$TEST_IMG"
342run_qemu -drive file="file:$TEST_IMG"
343run_qemu -drive file.filename="file:$TEST_IMG"
344
345echo
346echo === Snapshot mode ===
347echo
348
349$QEMU_IO -c "write -P 0x11 0 4k" "$TEST_IMG" | _filter_qemu_io
350
351
352echo "qemu-io $device_id \"write -P 0x22 0 4k\"" | run_qemu -drive file="$TEST_IMG",if=none,id=$device_id -snapshot | _filter_qemu_io
353echo "qemu-io $device_id \"write -P 0x22 0 4k\"" | run_qemu -drive file="$TEST_IMG",snapshot=on,if=none,id=$device_id | _filter_qemu_io
354echo "qemu-io $device_id \"write -P 0x22 0 4k\"" | run_qemu -drive file.filename="$TEST_IMG",driver=qcow2,snapshot=on,if=none,id=$device_id\
355                                                 | _filter_qemu_io
356echo "qemu-io $device_id \"write -P 0x22 0 4k\"" | run_qemu -drive file.filename="$TEST_IMG",driver=qcow2,if=none,id=$device_id -snapshot\
357                                                 | _filter_qemu_io
358echo "qemu-io $device_id \"write -P 0x22 0 4k\"" | run_qemu -drive file="file:$TEST_IMG",if=none,id=$device_id -snapshot | _filter_qemu_io
359echo "qemu-io $device_id \"write -P 0x22 0 4k\"" | run_qemu -drive file="file:$TEST_IMG",snapshot=on,if=none,id=$device_id | _filter_qemu_io
360
361# Opening a read-only file r/w with snapshot=on
362chmod u-w "$TEST_IMG"
363echo "qemu-io $device_id \"write -P 0x22 0 4k\"" | run_qemu -drive file="$TEST_IMG",if=none,id=$device_id -snapshot | _filter_qemu_io
364echo "qemu-io $device_id \"write -P 0x22 0 4k\"" | run_qemu -drive file="$TEST_IMG",snapshot=on,if=none,id=$device_id | _filter_qemu_io
365chmod u+w "$TEST_IMG"
366
367$QEMU_IO -c "read -P 0x11 0 4k" "$TEST_IMG" | _filter_qemu_io
368
369echo "qemu-io $device_id \"write -P 0x22 0 4k\"" | run_qemu -drive file="$TEST_IMG",snapshot=off,if=none,id=$device_id | _filter_qemu_io
370
371$QEMU_IO -c "read -P 0x22 0 4k" "$TEST_IMG" | _filter_qemu_io
372
373printf %b "qemu-io $device_id \"write -P 0x33 0 4k\"\ncommit $device_id\n" |
374    run_qemu -drive file="$TEST_IMG",snapshot=on,if=none,id=$device_id |
375    _filter_qemu_io
376
377$QEMU_IO -c "read -P 0x33 0 4k" "$TEST_IMG" | _filter_qemu_io
378
379# Using snapshot=on with a non-existent TMPDIR
380if [ "${VALGRIND_QEMU_VM}" == "y" ]; then
381    _casenotrun "Valgrind needs a valid TMPDIR for itself"
382fi
383VALGRIND_QEMU_VM= \
384TMPDIR=/nonexistent run_qemu -drive driver=null-co,snapshot=on
385
386# Using snapshot=on together with read-only=on
387echo "info block" |
388    run_qemu -drive file="$TEST_IMG",snapshot=on,read-only=on,if=none,id=$device_id |
389    _filter_qemu_io |
390    sed -e 's#"[^"]*/vl\.[A-Za-z0-9]\{6\}"#SNAPSHOT_PATH#g'
391
392
393# success, all done
394echo "*** done"
395rm -f $seq.full
396status=0
397