1#!/usr/bin/env bash
2#
3# Test automatic deletion of BDSes created by -drive/drive_add
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# get standard environment, filters and checks
30. ./common.rc
31. ./common.filter
32
33_supported_fmt qcow2
34_supported_proto file
35# Because anything other than 16 would change the output of query-block
36_unsupported_imgopts 'refcount_bits=\([^1]\|.\([^6]\|$\)\)'
37
38do_run_qemu()
39{
40    echo Testing: "$@"
41    $QEMU -nographic -qmp-pretty stdio -serial none "$@"
42    echo
43}
44
45# Remove QMP events from (pretty-printed) output. Doesn't handle
46# nested dicts correctly, but we don't get any of those in this test.
47_filter_qmp_events()
48{
49    tr '\n' '\t' | sed -e \
50	's/{\s*"timestamp":\s*{[^}]*},\s*"event":[^,}]*\(,\s*"data":\s*{[^}]*}\)\?\s*}\s*//g' \
51	| tr '\t' '\n'
52}
53
54run_qemu()
55{
56    do_run_qemu "$@" 2>&1 | _filter_testdir | _filter_qmp | _filter_qemu \
57                          | _filter_actual_image_size \
58                          | _filter_generated_node_ids | _filter_qmp_events \
59                          | _filter_img_info
60}
61
62size=128M
63
64_make_test_img $size
65
66echo
67echo === -drive/-device and device_del ===
68echo
69
70run_qemu -drive file=$TEST_IMG,format=$IMGFMT,if=none,id=disk -device virtio-blk,drive=disk,id=virtio0 <<EOF
71{ "execute": "qmp_capabilities" }
72{ "execute": "query-block" }
73{ "execute": "device_del", "arguments": { "id": "virtio0" } }
74{ "execute": "system_reset" }
75{ "execute": "query-block" }
76{ "execute": "quit" }
77EOF
78
79echo
80echo === -drive/device_add and device_del ===
81echo
82
83run_qemu -drive file=$TEST_IMG,format=$IMGFMT,if=none,id=disk <<EOF
84{ "execute": "qmp_capabilities" }
85{ "execute": "query-block" }
86{ "execute": "device_add",
87   "arguments": { "driver": "virtio-blk", "drive": "disk",
88                  "id": "virtio0" } }
89{ "execute": "device_del", "arguments": { "id": "virtio0" } }
90{ "execute": "system_reset" }
91{ "execute": "query-block" }
92{ "execute": "quit" }
93EOF
94
95echo
96echo === drive_add/device_add and device_del ===
97echo
98
99run_qemu <<EOF
100{ "execute": "qmp_capabilities" }
101{ "execute": "human-monitor-command",
102  "arguments": { "command-line": "drive_add 0 file=$TEST_IMG,format=$IMGFMT,if=none,id=disk" } }
103{ "execute": "query-block" }
104{ "execute": "device_add",
105   "arguments": { "driver": "virtio-blk", "drive": "disk",
106                  "id": "virtio0" } }
107{ "execute": "device_del", "arguments": { "id": "virtio0" } }
108{ "execute": "system_reset" }
109{ "execute": "query-block" }
110{ "execute": "quit" }
111EOF
112
113echo
114echo === blockdev_add/device_add and device_del ===
115echo
116
117run_qemu <<EOF
118{ "execute": "qmp_capabilities" }
119{ "execute": "blockdev-add",
120  "arguments": {
121      "driver": "$IMGFMT",
122      "node-name": "disk",
123      "file": {
124          "driver": "file",
125          "filename": "$TEST_IMG"
126      }
127    }
128  }
129{ "execute": "query-named-block-nodes" }
130{ "execute": "device_add",
131   "arguments": { "driver": "virtio-blk", "drive": "disk",
132                  "id": "virtio0" } }
133{ "execute": "device_del", "arguments": { "id": "virtio0" } }
134{ "execute": "system_reset" }
135{ "execute": "query-named-block-nodes" }
136{ "execute": "quit" }
137EOF
138
139echo
140echo === Empty drive with -device and device_del ===
141echo
142
143run_qemu -device virtio-scsi -device scsi-cd,id=cd0 <<EOF
144{ "execute": "qmp_capabilities" }
145{ "execute": "query-block" }
146{ "execute": "device_del", "arguments": { "id": "cd0" } }
147{ "execute": "system_reset" }
148{ "execute": "query-block" }
149{ "execute": "quit" }
150EOF
151
152# success, all done
153echo "*** done"
154rm -f $seq.full
155status=0
156