xref: /qemu/tests/qemu-iotests/245 (revision 78935fcd)
1903cb1bfSPhilippe Mathieu-Daudé#!/usr/bin/env python3
29dd003a9SVladimir Sementsov-Ogievskiy# group: rw
3bf3e50f6SAlberto Garcia#
4e60edf69SAlberto Garcia# Test cases for the QMP 'blockdev-reopen' command
5bf3e50f6SAlberto Garcia#
6bf3e50f6SAlberto Garcia# Copyright (C) 2018-2019 Igalia, S.L.
7bf3e50f6SAlberto Garcia# Author: Alberto Garcia <berto@igalia.com>
8bf3e50f6SAlberto Garcia#
9bf3e50f6SAlberto Garcia# This program is free software; you can redistribute it and/or modify
10bf3e50f6SAlberto Garcia# it under the terms of the GNU General Public License as published by
11bf3e50f6SAlberto Garcia# the Free Software Foundation; either version 2 of the License, or
12bf3e50f6SAlberto Garcia# (at your option) any later version.
13bf3e50f6SAlberto Garcia#
14bf3e50f6SAlberto Garcia# This program is distributed in the hope that it will be useful,
15bf3e50f6SAlberto Garcia# but WITHOUT ANY WARRANTY; without even the implied warranty of
16bf3e50f6SAlberto Garcia# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17bf3e50f6SAlberto Garcia# GNU General Public License for more details.
18bf3e50f6SAlberto Garcia#
19bf3e50f6SAlberto Garcia# You should have received a copy of the GNU General Public License
20bf3e50f6SAlberto Garcia# along with this program.  If not, see <http://www.gnu.org/licenses/>.
21bf3e50f6SAlberto Garcia#
22bf3e50f6SAlberto Garcia
23bf3e50f6SAlberto Garciaimport copy
24bf3e50f6SAlberto Garciaimport json
256dede6a4SJohn Snowimport os
266dede6a4SJohn Snowimport re
276dede6a4SJohn Snowfrom subprocess import CalledProcessError
286dede6a4SJohn Snow
296dede6a4SJohn Snowimport iotests
30bf3e50f6SAlberto Garciafrom iotests import qemu_img, qemu_io
31bf3e50f6SAlberto Garcia
32bf3e50f6SAlberto Garciahd_path = [
33bf3e50f6SAlberto Garcia    os.path.join(iotests.test_dir, 'hd0.img'),
34bf3e50f6SAlberto Garcia    os.path.join(iotests.test_dir, 'hd1.img'),
35bf3e50f6SAlberto Garcia    os.path.join(iotests.test_dir, 'hd2.img')
36bf3e50f6SAlberto Garcia]
37bf3e50f6SAlberto Garcia
38bf3e50f6SAlberto Garciadef hd_opts(idx):
39bf3e50f6SAlberto Garcia    return {'driver': iotests.imgfmt,
40bf3e50f6SAlberto Garcia            'node-name': 'hd%d' % idx,
41bf3e50f6SAlberto Garcia            'file': {'driver': 'file',
42bf3e50f6SAlberto Garcia                     'node-name': 'hd%d-file' % idx,
43bf3e50f6SAlberto Garcia                     'filename':  hd_path[idx] } }
44bf3e50f6SAlberto Garcia
45bf3e50f6SAlberto Garciaclass TestBlockdevReopen(iotests.QMPTestCase):
46bf3e50f6SAlberto Garcia    total_io_cmds = 0
47bf3e50f6SAlberto Garcia
48bf3e50f6SAlberto Garcia    def setUp(self):
49bf3e50f6SAlberto Garcia        qemu_img('create', '-f', iotests.imgfmt, hd_path[0], '3M')
50b66ff2c2SEric Blake        qemu_img('create', '-f', iotests.imgfmt, '-b', hd_path[0],
51b66ff2c2SEric Blake                 '-F', iotests.imgfmt, hd_path[1])
52bf3e50f6SAlberto Garcia        qemu_img('create', '-f', iotests.imgfmt, hd_path[2], '3M')
53bf3e50f6SAlberto Garcia        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xa0  0 1M', hd_path[0])
54bf3e50f6SAlberto Garcia        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xa1 1M 1M', hd_path[1])
55bf3e50f6SAlberto Garcia        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xa2 2M 1M', hd_path[2])
56bf3e50f6SAlberto Garcia        self.vm = iotests.VM()
57bf3e50f6SAlberto Garcia        self.vm.launch()
58bf3e50f6SAlberto Garcia
59bf3e50f6SAlberto Garcia    def tearDown(self):
60bf3e50f6SAlberto Garcia        self.vm.shutdown()
61bf3e50f6SAlberto Garcia        self.check_qemu_io_errors()
62bf3e50f6SAlberto Garcia        os.remove(hd_path[0])
63bf3e50f6SAlberto Garcia        os.remove(hd_path[1])
64bf3e50f6SAlberto Garcia        os.remove(hd_path[2])
65bf3e50f6SAlberto Garcia
66bf3e50f6SAlberto Garcia    # The output of qemu-io is not returned by vm.hmp_qemu_io() but
67bf3e50f6SAlberto Garcia    # it's stored in the log and can only be read when the VM has been
68bf3e50f6SAlberto Garcia    # shut down. This function runs qemu-io and keeps track of the
69bf3e50f6SAlberto Garcia    # number of times it's been called.
70bf3e50f6SAlberto Garcia    def run_qemu_io(self, img, cmd):
71bf3e50f6SAlberto Garcia        result = self.vm.hmp_qemu_io(img, cmd)
72bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', '')
73bf3e50f6SAlberto Garcia        self.total_io_cmds += 1
74bf3e50f6SAlberto Garcia
75bf3e50f6SAlberto Garcia    # Once the VM is shut down we can parse the log and see if qemu-io
76bf3e50f6SAlberto Garcia    # ran without errors.
77bf3e50f6SAlberto Garcia    def check_qemu_io_errors(self):
78bf3e50f6SAlberto Garcia        self.assertFalse(self.vm.is_running())
79bf3e50f6SAlberto Garcia        found = 0
80bf3e50f6SAlberto Garcia        log = self.vm.get_log()
81bf3e50f6SAlberto Garcia        for line in log.split("\n"):
82bf3e50f6SAlberto Garcia            if line.startswith("Pattern verification failed"):
83bf3e50f6SAlberto Garcia                raise Exception("%s (command #%d)" % (line, found))
844c5393f1SAlberto Garcia            if re.match("(read|wrote) .*/.* bytes at offset", line):
85bf3e50f6SAlberto Garcia                found += 1
86bf3e50f6SAlberto Garcia        self.assertEqual(found, self.total_io_cmds,
87bf3e50f6SAlberto Garcia                         "Expected output of %d qemu-io commands, found %d" %
88bf3e50f6SAlberto Garcia                         (found, self.total_io_cmds))
89bf3e50f6SAlberto Garcia
90e60edf69SAlberto Garcia    # Run blockdev-reopen on a list of block devices
913908b7a8SAlberto Garcia    def reopenMultiple(self, opts, errmsg = None):
92e60edf69SAlberto Garcia        result = self.vm.qmp('blockdev-reopen', conv_keys=False, options=opts)
933908b7a8SAlberto Garcia        if errmsg:
943908b7a8SAlberto Garcia            self.assert_qmp(result, 'error/class', 'GenericError')
953908b7a8SAlberto Garcia            self.assert_qmp(result, 'error/desc', errmsg)
963908b7a8SAlberto Garcia        else:
973908b7a8SAlberto Garcia            self.assert_qmp(result, 'return', {})
983908b7a8SAlberto Garcia
99e60edf69SAlberto Garcia    # Run blockdev-reopen on a single block device (specified by
1003908b7a8SAlberto Garcia    # 'opts') but applying 'newopts' on top of it. The original 'opts'
1013908b7a8SAlberto Garcia    # dict is unmodified
102bf3e50f6SAlberto Garcia    def reopen(self, opts, newopts = {}, errmsg = None):
103bf3e50f6SAlberto Garcia        opts = copy.deepcopy(opts)
104bf3e50f6SAlberto Garcia
105bf3e50f6SAlberto Garcia        # Apply the changes from 'newopts' on top of 'opts'
106bf3e50f6SAlberto Garcia        for key in newopts:
107bf3e50f6SAlberto Garcia            value = newopts[key]
108bf3e50f6SAlberto Garcia            # If key has the form "foo.bar" then we need to do
109bf3e50f6SAlberto Garcia            # opts["foo"]["bar"] = value, not opts["foo.bar"] = value
110bf3e50f6SAlberto Garcia            subdict = opts
111bf3e50f6SAlberto Garcia            while key.find('.') != -1:
112bf3e50f6SAlberto Garcia                [prefix, key] = key.split('.', 1)
113bf3e50f6SAlberto Garcia                subdict = opts[prefix]
114bf3e50f6SAlberto Garcia            subdict[key] = value
115bf3e50f6SAlberto Garcia
1163908b7a8SAlberto Garcia        self.reopenMultiple([ opts ], errmsg)
117bf3e50f6SAlberto Garcia
118bf3e50f6SAlberto Garcia
119bf3e50f6SAlberto Garcia    # Run query-named-block-nodes and return the specified entry
120bf3e50f6SAlberto Garcia    def get_node(self, node_name):
121bf3e50f6SAlberto Garcia        result = self.vm.qmp('query-named-block-nodes')
122bf3e50f6SAlberto Garcia        for node in result['return']:
123bf3e50f6SAlberto Garcia            if node['node-name'] == node_name:
124bf3e50f6SAlberto Garcia                return node
125bf3e50f6SAlberto Garcia        return None
126bf3e50f6SAlberto Garcia
127bf3e50f6SAlberto Garcia    # Run 'query-named-block-nodes' and compare its output with the
128bf3e50f6SAlberto Garcia    # value passed by the user in 'graph'
129bf3e50f6SAlberto Garcia    def check_node_graph(self, graph):
130bf3e50f6SAlberto Garcia        result = self.vm.qmp('query-named-block-nodes')
131bf3e50f6SAlberto Garcia        self.assertEqual(json.dumps(graph,  sort_keys=True),
132bf3e50f6SAlberto Garcia                         json.dumps(result, sort_keys=True))
133bf3e50f6SAlberto Garcia
134bf3e50f6SAlberto Garcia    # This test opens one single disk image (without backing files)
135bf3e50f6SAlberto Garcia    # and tries to reopen it with illegal / incorrect parameters.
136bf3e50f6SAlberto Garcia    def test_incorrect_parameters_single_file(self):
137bf3e50f6SAlberto Garcia        # Open 'hd0' only (no backing files)
138bf3e50f6SAlberto Garcia        opts = hd_opts(0)
139bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
140bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
141bf3e50f6SAlberto Garcia        original_graph = self.vm.qmp('query-named-block-nodes')
142bf3e50f6SAlberto Garcia
143bf3e50f6SAlberto Garcia        # We can reopen the image passing the same options
144bf3e50f6SAlberto Garcia        self.reopen(opts)
145bf3e50f6SAlberto Garcia
146bf3e50f6SAlberto Garcia        # We can also reopen passing a child reference in 'file'
147bf3e50f6SAlberto Garcia        self.reopen(opts, {'file': 'hd0-file'})
148bf3e50f6SAlberto Garcia
149bf3e50f6SAlberto Garcia        # We cannot change any of these
150ef2e38a1SConnor Kuehl        self.reopen(opts, {'node-name': 'not-found'}, "Failed to find node with node-name='not-found'")
151ef2e38a1SConnor Kuehl        self.reopen(opts, {'node-name': ''}, "Failed to find node with node-name=''")
1523908b7a8SAlberto Garcia        self.reopen(opts, {'node-name': None}, "Invalid parameter type for 'options[0].node-name', expected: string")
153bf3e50f6SAlberto Garcia        self.reopen(opts, {'driver': 'raw'}, "Cannot change the option 'driver'")
154ea29331bSMarkus Armbruster        self.reopen(opts, {'driver': ''}, "Parameter 'driver' does not accept value ''")
1553908b7a8SAlberto Garcia        self.reopen(opts, {'driver': None}, "Invalid parameter type for 'options[0].driver', expected: string")
156ecd30d2dSAlberto Garcia        self.reopen(opts, {'file': 'not-found'}, "Cannot find device='' nor node-name='not-found'")
157ecd30d2dSAlberto Garcia        self.reopen(opts, {'file': ''}, "Cannot find device='' nor node-name=''")
158bf3e50f6SAlberto Garcia        self.reopen(opts, {'file': None}, "Invalid parameter type for 'file', expected: BlockdevRef")
159bf3e50f6SAlberto Garcia        self.reopen(opts, {'file.node-name': 'newname'}, "Cannot change the option 'node-name'")
160bf3e50f6SAlberto Garcia        self.reopen(opts, {'file.driver': 'host_device'}, "Cannot change the option 'driver'")
161bf3e50f6SAlberto Garcia        self.reopen(opts, {'file.filename': hd_path[1]}, "Cannot change the option 'filename'")
162bf3e50f6SAlberto Garcia        self.reopen(opts, {'file.aio': 'native'}, "Cannot change the option 'aio'")
163bf3e50f6SAlberto Garcia        self.reopen(opts, {'file.locking': 'off'}, "Cannot change the option 'locking'")
1643908b7a8SAlberto Garcia        self.reopen(opts, {'file.filename': None}, "Invalid parameter type for 'options[0].file.filename', expected: string")
165bf3e50f6SAlberto Garcia
166e60edf69SAlberto Garcia        # node-name is optional in BlockdevOptions, but blockdev-reopen needs it
167bf3e50f6SAlberto Garcia        del opts['node-name']
168ef2e38a1SConnor Kuehl        self.reopen(opts, {}, "node-name not specified")
169bf3e50f6SAlberto Garcia
170bf3e50f6SAlberto Garcia        # Check that nothing has changed
171bf3e50f6SAlberto Garcia        self.check_node_graph(original_graph)
172bf3e50f6SAlberto Garcia
173bf3e50f6SAlberto Garcia        # Remove the node
174bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
175bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
176bf3e50f6SAlberto Garcia
177bf3e50f6SAlberto Garcia    # This test opens an image with a backing file and tries to reopen
178bf3e50f6SAlberto Garcia    # it with illegal / incorrect parameters.
179bf3e50f6SAlberto Garcia    def test_incorrect_parameters_backing_file(self):
180bf3e50f6SAlberto Garcia        # Open hd1 omitting the backing options (hd0 will be opened
181bf3e50f6SAlberto Garcia        # with the default options)
182bf3e50f6SAlberto Garcia        opts = hd_opts(1)
183bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
184bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
185bf3e50f6SAlberto Garcia        original_graph = self.vm.qmp('query-named-block-nodes')
186bf3e50f6SAlberto Garcia
187bf3e50f6SAlberto Garcia        # We can't reopen the image passing the same options, 'backing' is mandatory
188bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "backing is missing for 'hd1'")
189bf3e50f6SAlberto Garcia
190bf3e50f6SAlberto Garcia        # Everything works if we pass 'backing' using the existing node name
191bf3e50f6SAlberto Garcia        for node in original_graph['return']:
192bf3e50f6SAlberto Garcia            if node['drv'] == iotests.imgfmt and node['file'] == hd_path[0]:
193bf3e50f6SAlberto Garcia                backing_node_name = node['node-name']
194bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing': backing_node_name})
195bf3e50f6SAlberto Garcia
196bf3e50f6SAlberto Garcia        # We can't use a non-existing or empty (non-NULL) node as the backing image
197785ec4b1SConnor Kuehl        self.reopen(opts, {'backing': 'not-found'}, "Cannot find device=\'\' nor node-name=\'not-found\'")
198785ec4b1SConnor Kuehl        self.reopen(opts, {'backing': ''}, "Cannot find device=\'\' nor node-name=\'\'")
199bf3e50f6SAlberto Garcia
200bf3e50f6SAlberto Garcia        # We can reopen the image just fine if we specify the backing options
201bf3e50f6SAlberto Garcia        opts['backing'] = {'driver': iotests.imgfmt,
202bf3e50f6SAlberto Garcia                           'file': {'driver': 'file',
203bf3e50f6SAlberto Garcia                                    'filename': hd_path[0]}}
204bf3e50f6SAlberto Garcia        self.reopen(opts)
205bf3e50f6SAlberto Garcia
206bf3e50f6SAlberto Garcia        # We cannot change any of these options
207bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing.node-name': 'newname'}, "Cannot change the option 'node-name'")
208bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing.driver': 'raw'}, "Cannot change the option 'driver'")
209bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing.file.node-name': 'newname'}, "Cannot change the option 'node-name'")
210bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing.file.driver': 'host_device'}, "Cannot change the option 'driver'")
211bf3e50f6SAlberto Garcia
212bf3e50f6SAlberto Garcia        # Check that nothing has changed since the beginning
213bf3e50f6SAlberto Garcia        self.check_node_graph(original_graph)
214bf3e50f6SAlberto Garcia
215bf3e50f6SAlberto Garcia        # Remove the node
216bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd1')
217bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
218bf3e50f6SAlberto Garcia
219bf3e50f6SAlberto Garcia    # Reopen an image several times changing some of its options
220bf3e50f6SAlberto Garcia    def test_reopen(self):
2216dede6a4SJohn Snow        try:
2226dede6a4SJohn Snow            qemu_io('-f', 'raw', '-t', 'none', '-c', 'quit', hd_path[0])
2236dede6a4SJohn Snow            supports_direct = True
2246dede6a4SJohn Snow        except CalledProcessError as exc:
2256dede6a4SJohn Snow            if 'O_DIRECT' in exc.stdout:
22623e1d054SMax Reitz                supports_direct = False
22723e1d054SMax Reitz            else:
2286dede6a4SJohn Snow                raise
22923e1d054SMax Reitz
230bf3e50f6SAlberto Garcia        # Open the hd1 image passing all backing options
231bf3e50f6SAlberto Garcia        opts = hd_opts(1)
232bf3e50f6SAlberto Garcia        opts['backing'] = hd_opts(0)
233bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
234bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
235bf3e50f6SAlberto Garcia        original_graph = self.vm.qmp('query-named-block-nodes')
236bf3e50f6SAlberto Garcia
237bf3e50f6SAlberto Garcia        # We can reopen the image passing the same options
238bf3e50f6SAlberto Garcia        self.reopen(opts)
239bf3e50f6SAlberto Garcia
240bf3e50f6SAlberto Garcia        # Reopen in read-only mode
241bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'ro', False)
242bf3e50f6SAlberto Garcia
243bf3e50f6SAlberto Garcia        self.reopen(opts, {'read-only': True})
244bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'ro', True)
245bf3e50f6SAlberto Garcia        self.reopen(opts)
246bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'ro', False)
247bf3e50f6SAlberto Garcia
248bf3e50f6SAlberto Garcia        # Change the cache options
249bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'cache/writeback', True)
250bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'cache/direct', False)
251bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'cache/no-flush', False)
25223e1d054SMax Reitz        self.reopen(opts, {'cache': { 'direct': supports_direct, 'no-flush': True }})
253bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'cache/writeback', True)
25423e1d054SMax Reitz        self.assert_qmp(self.get_node('hd1'), 'cache/direct', supports_direct)
255bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'cache/no-flush', True)
256bf3e50f6SAlberto Garcia
257bf3e50f6SAlberto Garcia        # Reopen again with the original options
258bf3e50f6SAlberto Garcia        self.reopen(opts)
259bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'cache/writeback', True)
260bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'cache/direct', False)
261bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'cache/no-flush', False)
262bf3e50f6SAlberto Garcia
263bf3e50f6SAlberto Garcia        # Change 'detect-zeroes' and 'discard'
264bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'off')
265bf3e50f6SAlberto Garcia        self.reopen(opts, {'detect-zeroes': 'on'})
266bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'on')
267bf3e50f6SAlberto Garcia        self.reopen(opts, {'detect-zeroes': 'unmap'},
268bf3e50f6SAlberto Garcia                    "setting detect-zeroes to unmap is not allowed " +
269bf3e50f6SAlberto Garcia                    "without setting discard operation to unmap")
270bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'on')
271bf3e50f6SAlberto Garcia        self.reopen(opts, {'detect-zeroes': 'unmap', 'discard': 'unmap'})
272bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'unmap')
273bf3e50f6SAlberto Garcia        self.reopen(opts)
274bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'off')
275bf3e50f6SAlberto Garcia
276bf3e50f6SAlberto Garcia        # Changing 'force-share' is currently not supported
277bf3e50f6SAlberto Garcia        self.reopen(opts, {'force-share': True}, "Cannot change the option 'force-share'")
278bf3e50f6SAlberto Garcia
279bf3e50f6SAlberto Garcia        # Change some qcow2-specific options
280bf3e50f6SAlberto Garcia        # No way to test for success other than checking the return message
281bf3e50f6SAlberto Garcia        if iotests.imgfmt == 'qcow2':
282bf3e50f6SAlberto Garcia            self.reopen(opts, {'l2-cache-entry-size': 128 * 1024},
283bf3e50f6SAlberto Garcia                        "L2 cache entry size must be a power of two "+
284bf3e50f6SAlberto Garcia                        "between 512 and the cluster size (65536)")
285bf3e50f6SAlberto Garcia            self.reopen(opts, {'l2-cache-size': 1024 * 1024,
286bf3e50f6SAlberto Garcia                               'cache-size':     512 * 1024},
287bf3e50f6SAlberto Garcia                        "l2-cache-size may not exceed cache-size")
288bf3e50f6SAlberto Garcia            self.reopen(opts, {'l2-cache-size':        4 * 1024 * 1024,
289bf3e50f6SAlberto Garcia                               'refcount-cache-size':  4 * 1024 * 1024,
290bf3e50f6SAlberto Garcia                               'l2-cache-entry-size': 32 * 1024})
291bf3e50f6SAlberto Garcia            self.reopen(opts, {'pass-discard-request': True})
292bf3e50f6SAlberto Garcia
293bf3e50f6SAlberto Garcia        # Check that nothing has changed since the beginning
294bf3e50f6SAlberto Garcia        # (from the parameters that we can check)
295bf3e50f6SAlberto Garcia        self.check_node_graph(original_graph)
296bf3e50f6SAlberto Garcia
297bf3e50f6SAlberto Garcia        # Check that the node names (other than the top-level one) are optional
298bf3e50f6SAlberto Garcia        del opts['file']['node-name']
299bf3e50f6SAlberto Garcia        del opts['backing']['node-name']
300bf3e50f6SAlberto Garcia        del opts['backing']['file']['node-name']
301bf3e50f6SAlberto Garcia        self.reopen(opts)
302bf3e50f6SAlberto Garcia        self.check_node_graph(original_graph)
303bf3e50f6SAlberto Garcia
304bf3e50f6SAlberto Garcia        # Reopen setting backing = null, this removes the backing image from the chain
305bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing': None})
306bf3e50f6SAlberto Garcia        self.assert_qmp_absent(self.get_node('hd1'), 'image/backing-image')
307bf3e50f6SAlberto Garcia
308bf3e50f6SAlberto Garcia        # Open the 'hd0' image
309bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **hd_opts(0))
310bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
311bf3e50f6SAlberto Garcia
312bf3e50f6SAlberto Garcia        # Reopen the hd1 image setting 'hd0' as its backing image
313bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing': 'hd0'})
314bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'image/backing-image/filename', hd_path[0])
315bf3e50f6SAlberto Garcia
316bf3e50f6SAlberto Garcia        # Check that nothing has changed since the beginning
317bf3e50f6SAlberto Garcia        self.reopen(hd_opts(0), {'read-only': True})
318bf3e50f6SAlberto Garcia        self.check_node_graph(original_graph)
319bf3e50f6SAlberto Garcia
320bf3e50f6SAlberto Garcia        # The backing file (hd0) is now a reference, we cannot change backing.* anymore
321bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "Cannot change the option 'backing.driver'")
322bf3e50f6SAlberto Garcia
323bf3e50f6SAlberto Garcia        # We can't remove 'hd0' while it's a backing image of 'hd1'
324bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
325bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'error/class', 'GenericError')
326bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'error/desc', "Node 'hd0' is busy: node is used as backing hd of 'hd1'")
327bf3e50f6SAlberto Garcia
328bf3e50f6SAlberto Garcia        # But we can remove both nodes if done in the proper order
329bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd1')
330bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
331bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
332bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
333bf3e50f6SAlberto Garcia
334bf3e50f6SAlberto Garcia    # Reopen a raw image and see the effect of changing the 'offset' option
335bf3e50f6SAlberto Garcia    def test_reopen_raw(self):
336bf3e50f6SAlberto Garcia        opts = {'driver': 'raw', 'node-name': 'hd0',
337bf3e50f6SAlberto Garcia                'file': { 'driver': 'file',
338bf3e50f6SAlberto Garcia                          'filename': hd_path[0],
339bf3e50f6SAlberto Garcia                          'node-name': 'hd0-file' } }
340bf3e50f6SAlberto Garcia
341bf3e50f6SAlberto Garcia        # First we create a 2MB raw file, and fill each half with a
342bf3e50f6SAlberto Garcia        # different value
343bf3e50f6SAlberto Garcia        qemu_img('create', '-f', 'raw', hd_path[0], '2M')
344bf3e50f6SAlberto Garcia        qemu_io('-f', 'raw', '-c', 'write -P 0xa0  0 1M', hd_path[0])
345bf3e50f6SAlberto Garcia        qemu_io('-f', 'raw', '-c', 'write -P 0xa1 1M 1M', hd_path[0])
346bf3e50f6SAlberto Garcia
347bf3e50f6SAlberto Garcia        # Open the raw file with QEMU
348bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
349bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
350bf3e50f6SAlberto Garcia
351bf3e50f6SAlberto Garcia        # Read 1MB from offset 0
352bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa0  0 1M")
353bf3e50f6SAlberto Garcia
354bf3e50f6SAlberto Garcia        # Reopen the image with a 1MB offset.
355bf3e50f6SAlberto Garcia        # Now the results are different
356bf3e50f6SAlberto Garcia        self.reopen(opts, {'offset': 1024*1024})
357bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa1  0 1M")
358bf3e50f6SAlberto Garcia
359bf3e50f6SAlberto Garcia        # Reopen again with the original options.
360bf3e50f6SAlberto Garcia        # We get the original results again
361bf3e50f6SAlberto Garcia        self.reopen(opts)
362bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa0  0 1M")
363bf3e50f6SAlberto Garcia
364bf3e50f6SAlberto Garcia        # Remove the block device
365bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
366bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
367bf3e50f6SAlberto Garcia
368bf3e50f6SAlberto Garcia    # Omitting an option should reset it to the default value, but if
369bf3e50f6SAlberto Garcia    # an option cannot be changed it shouldn't be possible to reset it
370bf3e50f6SAlberto Garcia    # to its default value either
371bf3e50f6SAlberto Garcia    def test_reset_default_values(self):
372bf3e50f6SAlberto Garcia        opts = {'driver': 'qcow2', 'node-name': 'hd0',
373bf3e50f6SAlberto Garcia                'file': { 'driver': 'file',
374bf3e50f6SAlberto Garcia                          'filename': hd_path[0],
375bf3e50f6SAlberto Garcia                          'x-check-cache-dropped': True, # This one can be changed
376bf3e50f6SAlberto Garcia                          'locking': 'off',              # This one can NOT be changed
377bf3e50f6SAlberto Garcia                          'node-name': 'hd0-file' } }
378bf3e50f6SAlberto Garcia
379bf3e50f6SAlberto Garcia        # Open the file with QEMU
380bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
381bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
382bf3e50f6SAlberto Garcia
383bf3e50f6SAlberto Garcia        # file.x-check-cache-dropped can be changed...
384bf3e50f6SAlberto Garcia        self.reopen(opts, { 'file.x-check-cache-dropped': False })
385bf3e50f6SAlberto Garcia        # ...and dropped completely (resetting to the default value)
386bf3e50f6SAlberto Garcia        del opts['file']['x-check-cache-dropped']
387bf3e50f6SAlberto Garcia        self.reopen(opts)
388bf3e50f6SAlberto Garcia
389bf3e50f6SAlberto Garcia        # file.locking cannot be changed nor reset to the default value
390bf3e50f6SAlberto Garcia        self.reopen(opts, { 'file.locking': 'on' }, "Cannot change the option 'locking'")
391bf3e50f6SAlberto Garcia        del opts['file']['locking']
392bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "Option 'locking' cannot be reset to its default value")
393bf3e50f6SAlberto Garcia        # But we can reopen it if we maintain its previous value
394bf3e50f6SAlberto Garcia        self.reopen(opts, { 'file.locking': 'off' })
395bf3e50f6SAlberto Garcia
396bf3e50f6SAlberto Garcia        # Remove the block device
397bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
398bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
399bf3e50f6SAlberto Garcia
400bf3e50f6SAlberto Garcia    # This test modifies the node graph a few times by changing the
401bf3e50f6SAlberto Garcia    # 'backing' option on reopen and verifies that the guest data that
402bf3e50f6SAlberto Garcia    # is read afterwards is consistent with the graph changes.
403bf3e50f6SAlberto Garcia    def test_io_with_graph_changes(self):
404bf3e50f6SAlberto Garcia        opts = []
405bf3e50f6SAlberto Garcia
406bf3e50f6SAlberto Garcia        # Open hd0, hd1 and hd2 without any backing image
407bf3e50f6SAlberto Garcia        for i in range(3):
408bf3e50f6SAlberto Garcia            opts.append(hd_opts(i))
409bf3e50f6SAlberto Garcia            opts[i]['backing'] = None
410bf3e50f6SAlberto Garcia            result = self.vm.qmp('blockdev-add', conv_keys = False, **opts[i])
411bf3e50f6SAlberto Garcia            self.assert_qmp(result, 'return', {})
412bf3e50f6SAlberto Garcia
413bf3e50f6SAlberto Garcia        # hd0
414bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa0  0 1M")
415bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0    1M 1M")
416bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0    2M 1M")
417bf3e50f6SAlberto Garcia
418bf3e50f6SAlberto Garcia        # hd1 <- hd0
419bf3e50f6SAlberto Garcia        self.reopen(opts[0], {'backing': 'hd1'})
420bf3e50f6SAlberto Garcia
421bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa0  0 1M")
422bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa1 1M 1M")
423bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0    2M 1M")
424bf3e50f6SAlberto Garcia
425bf3e50f6SAlberto Garcia        # hd1 <- hd0 , hd1 <- hd2
426bf3e50f6SAlberto Garcia        self.reopen(opts[2], {'backing': 'hd1'})
427bf3e50f6SAlberto Garcia
428bf3e50f6SAlberto Garcia        self.run_qemu_io("hd2", "read -P 0     0 1M")
429bf3e50f6SAlberto Garcia        self.run_qemu_io("hd2", "read -P 0xa1 1M 1M")
430bf3e50f6SAlberto Garcia        self.run_qemu_io("hd2", "read -P 0xa2 2M 1M")
431bf3e50f6SAlberto Garcia
432bf3e50f6SAlberto Garcia        # hd1 <- hd2 <- hd0
433bf3e50f6SAlberto Garcia        self.reopen(opts[0], {'backing': 'hd2'})
434bf3e50f6SAlberto Garcia
435bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa0  0 1M")
436bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa1 1M 1M")
437bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa2 2M 1M")
438bf3e50f6SAlberto Garcia
439bf3e50f6SAlberto Garcia        # hd2 <- hd0
440bf3e50f6SAlberto Garcia        self.reopen(opts[2], {'backing': None})
441bf3e50f6SAlberto Garcia
442bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa0  0 1M")
443bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0    1M 1M")
444bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa2 2M 1M")
445bf3e50f6SAlberto Garcia
446bf3e50f6SAlberto Garcia        # hd2 <- hd1 <- hd0
447bf3e50f6SAlberto Garcia        self.reopen(opts[1], {'backing': 'hd2'})
448bf3e50f6SAlberto Garcia        self.reopen(opts[0], {'backing': 'hd1'})
449bf3e50f6SAlberto Garcia
450bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa0  0 1M")
451bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa1 1M 1M")
452bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa2 2M 1M")
453bf3e50f6SAlberto Garcia
454bf3e50f6SAlberto Garcia        # Illegal operation: hd2 is a child of hd1
455bf3e50f6SAlberto Garcia        self.reopen(opts[2], {'backing': 'hd1'},
456ecd30d2dSAlberto Garcia                    "Making 'hd1' a backing child of 'hd2' would create a cycle")
457bf3e50f6SAlberto Garcia
458bf3e50f6SAlberto Garcia        # hd2 <- hd0, hd2 <- hd1
459bf3e50f6SAlberto Garcia        self.reopen(opts[0], {'backing': 'hd2'})
460bf3e50f6SAlberto Garcia
461bf3e50f6SAlberto Garcia        self.run_qemu_io("hd1", "read -P 0     0 1M")
462bf3e50f6SAlberto Garcia        self.run_qemu_io("hd1", "read -P 0xa1 1M 1M")
463bf3e50f6SAlberto Garcia        self.run_qemu_io("hd1", "read -P 0xa2 2M 1M")
464bf3e50f6SAlberto Garcia
465bf3e50f6SAlberto Garcia        # More illegal operations
466bf3e50f6SAlberto Garcia        self.reopen(opts[2], {'backing': 'hd1'},
467ecd30d2dSAlberto Garcia                    "Making 'hd1' a backing child of 'hd2' would create a cycle")
468ecd30d2dSAlberto Garcia        self.reopen(opts[2], {'file': 'hd0-file'},
469ecd30d2dSAlberto Garcia                    "Permission conflict on node 'hd0-file': permissions 'write, resize' are both required by node 'hd0' (uses node 'hd0-file' as 'file' child) and unshared by node 'hd2' (uses node 'hd0-file' as 'file' child).")
470bf3e50f6SAlberto Garcia
471bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd2')
472bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'error/class', 'GenericError')
473bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'error/desc', "Node 'hd2' is busy: node is used as backing hd of 'hd0'")
474bf3e50f6SAlberto Garcia
475bf3e50f6SAlberto Garcia        # hd1 doesn't have a backing file now
476bf3e50f6SAlberto Garcia        self.reopen(opts[1], {'backing': None})
477bf3e50f6SAlberto Garcia
478bf3e50f6SAlberto Garcia        self.run_qemu_io("hd1", "read -P 0     0 1M")
479bf3e50f6SAlberto Garcia        self.run_qemu_io("hd1", "read -P 0xa1 1M 1M")
480bf3e50f6SAlberto Garcia        self.run_qemu_io("hd1", "read -P 0    2M 1M")
481bf3e50f6SAlberto Garcia
482bf3e50f6SAlberto Garcia        # We can't remove the 'backing' option if the image has a
483bf3e50f6SAlberto Garcia        # default backing file
484bf3e50f6SAlberto Garcia        del opts[1]['backing']
485bf3e50f6SAlberto Garcia        self.reopen(opts[1], {}, "backing is missing for 'hd1'")
486bf3e50f6SAlberto Garcia
487bf3e50f6SAlberto Garcia        self.run_qemu_io("hd1", "read -P 0     0 1M")
488bf3e50f6SAlberto Garcia        self.run_qemu_io("hd1", "read -P 0xa1 1M 1M")
489bf3e50f6SAlberto Garcia        self.run_qemu_io("hd1", "read -P 0    2M 1M")
490bf3e50f6SAlberto Garcia
491bf3e50f6SAlberto Garcia    # This test verifies that we can't change the children of a block
492bf3e50f6SAlberto Garcia    # device during a reopen operation in a way that would create
493bf3e50f6SAlberto Garcia    # cycles in the node graph
4949442bebeSThomas Huth    @iotests.skip_if_unsupported(['blkverify'])
495bf3e50f6SAlberto Garcia    def test_graph_cycles(self):
496bf3e50f6SAlberto Garcia        opts = []
497bf3e50f6SAlberto Garcia
498bf3e50f6SAlberto Garcia        # Open all three images without backing file
499bf3e50f6SAlberto Garcia        for i in range(3):
500bf3e50f6SAlberto Garcia            opts.append(hd_opts(i))
501bf3e50f6SAlberto Garcia            opts[i]['backing'] = None
502bf3e50f6SAlberto Garcia            result = self.vm.qmp('blockdev-add', conv_keys = False, **opts[i])
503bf3e50f6SAlberto Garcia            self.assert_qmp(result, 'return', {})
504bf3e50f6SAlberto Garcia
505bf3e50f6SAlberto Garcia        # hd1 <- hd0, hd1 <- hd2
506bf3e50f6SAlberto Garcia        self.reopen(opts[0], {'backing': 'hd1'})
507bf3e50f6SAlberto Garcia        self.reopen(opts[2], {'backing': 'hd1'})
508bf3e50f6SAlberto Garcia
509bf3e50f6SAlberto Garcia        # Illegal: hd2 is backed by hd1
510bf3e50f6SAlberto Garcia        self.reopen(opts[1], {'backing': 'hd2'},
511ecd30d2dSAlberto Garcia                    "Making 'hd2' a backing child of 'hd1' would create a cycle")
512bf3e50f6SAlberto Garcia
513bf3e50f6SAlberto Garcia        # hd1 <- hd0 <- hd2
514bf3e50f6SAlberto Garcia        self.reopen(opts[2], {'backing': 'hd0'})
515bf3e50f6SAlberto Garcia
516bf3e50f6SAlberto Garcia        # Illegal: hd2 is backed by hd0, which is backed by hd1
517bf3e50f6SAlberto Garcia        self.reopen(opts[1], {'backing': 'hd2'},
518ecd30d2dSAlberto Garcia                    "Making 'hd2' a backing child of 'hd1' would create a cycle")
519bf3e50f6SAlberto Garcia
520bf3e50f6SAlberto Garcia        # Illegal: hd1 cannot point to itself
521bf3e50f6SAlberto Garcia        self.reopen(opts[1], {'backing': 'hd1'},
522ecd30d2dSAlberto Garcia                    "Making 'hd1' a backing child of 'hd1' would create a cycle")
523bf3e50f6SAlberto Garcia
524bf3e50f6SAlberto Garcia        # Remove all backing files
525bf3e50f6SAlberto Garcia        self.reopen(opts[0])
526bf3e50f6SAlberto Garcia        self.reopen(opts[2])
527bf3e50f6SAlberto Garcia
528bf3e50f6SAlberto Garcia        ##########################################
529bf3e50f6SAlberto Garcia        # Add a blkverify node using hd0 and hd1 #
530bf3e50f6SAlberto Garcia        ##########################################
531bf3e50f6SAlberto Garcia        bvopts = {'driver': 'blkverify',
532bf3e50f6SAlberto Garcia                  'node-name': 'bv',
533bf3e50f6SAlberto Garcia                  'test': 'hd0',
534bf3e50f6SAlberto Garcia                  'raw': 'hd1'}
535bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **bvopts)
536bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
537bf3e50f6SAlberto Garcia
538bf3e50f6SAlberto Garcia        # blkverify doesn't currently allow reopening. TODO: implement this
539bf3e50f6SAlberto Garcia        self.reopen(bvopts, {}, "Block format 'blkverify' used by node 'bv'" +
540bf3e50f6SAlberto Garcia                    " does not support reopening files")
541bf3e50f6SAlberto Garcia
542bf3e50f6SAlberto Garcia        # Illegal: hd0 is a child of the blkverify node
543bf3e50f6SAlberto Garcia        self.reopen(opts[0], {'backing': 'bv'},
544ecd30d2dSAlberto Garcia                    "Making 'bv' a backing child of 'hd0' would create a cycle")
545bf3e50f6SAlberto Garcia
546bf3e50f6SAlberto Garcia        # Delete the blkverify node
547bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'bv')
548bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
549bf3e50f6SAlberto Garcia
5504c5393f1SAlberto Garcia    # Replace the protocol layer ('file' parameter) of a disk image
5514c5393f1SAlberto Garcia    def test_replace_file(self):
5524c5393f1SAlberto Garcia        # Create two small raw images and add them to a running VM
5534c5393f1SAlberto Garcia        qemu_img('create', '-f', 'raw', hd_path[0], '10k')
5544c5393f1SAlberto Garcia        qemu_img('create', '-f', 'raw', hd_path[1], '10k')
5554c5393f1SAlberto Garcia
5564c5393f1SAlberto Garcia        hd0_opts = {'driver': 'file', 'node-name': 'hd0-file', 'filename':  hd_path[0] }
5574c5393f1SAlberto Garcia        hd1_opts = {'driver': 'file', 'node-name': 'hd1-file', 'filename':  hd_path[1] }
5584c5393f1SAlberto Garcia
5594c5393f1SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **hd0_opts)
5604c5393f1SAlberto Garcia        self.assert_qmp(result, 'return', {})
5614c5393f1SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **hd1_opts)
5624c5393f1SAlberto Garcia        self.assert_qmp(result, 'return', {})
5634c5393f1SAlberto Garcia
5644c5393f1SAlberto Garcia        # Add a raw format layer that uses hd0-file as its protocol layer
5654c5393f1SAlberto Garcia        opts = {'driver': 'raw', 'node-name': 'hd', 'file': 'hd0-file'}
5664c5393f1SAlberto Garcia
5674c5393f1SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
5684c5393f1SAlberto Garcia        self.assert_qmp(result, 'return', {})
5694c5393f1SAlberto Garcia
5704c5393f1SAlberto Garcia        # Fill the image with data
5714c5393f1SAlberto Garcia        self.run_qemu_io("hd", "read  -P 0 0 10k")
5724c5393f1SAlberto Garcia        self.run_qemu_io("hd", "write -P 0xa0 0 10k")
5734c5393f1SAlberto Garcia
5744c5393f1SAlberto Garcia        # Replace hd0-file with hd1-file and fill it with (different) data
5754c5393f1SAlberto Garcia        self.reopen(opts, {'file': 'hd1-file'})
5764c5393f1SAlberto Garcia        self.run_qemu_io("hd", "read  -P 0 0 10k")
5774c5393f1SAlberto Garcia        self.run_qemu_io("hd", "write -P 0xa1 0 10k")
5784c5393f1SAlberto Garcia
5794c5393f1SAlberto Garcia        # Use hd0-file again and check that it contains the expected data
5804c5393f1SAlberto Garcia        self.reopen(opts, {'file': 'hd0-file'})
5814c5393f1SAlberto Garcia        self.run_qemu_io("hd", "read  -P 0xa0 0 10k")
5824c5393f1SAlberto Garcia
5834c5393f1SAlberto Garcia        # And finally do the same with hd1-file
5844c5393f1SAlberto Garcia        self.reopen(opts, {'file': 'hd1-file'})
5854c5393f1SAlberto Garcia        self.run_qemu_io("hd", "read  -P 0xa1 0 10k")
5864c5393f1SAlberto Garcia
5874c5393f1SAlberto Garcia    # Insert (and remove) a throttle filter
5884c5393f1SAlberto Garcia    def test_insert_throttle_filter(self):
5894c5393f1SAlberto Garcia        # Add an image to the VM
5904c5393f1SAlberto Garcia        hd0_opts = hd_opts(0)
5914c5393f1SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **hd0_opts)
5924c5393f1SAlberto Garcia        self.assert_qmp(result, 'return', {})
5934c5393f1SAlberto Garcia
5944c5393f1SAlberto Garcia        # Create a throttle-group object
5954c5393f1SAlberto Garcia        opts = { 'qom-type': 'throttle-group', 'id': 'group0',
5964c5393f1SAlberto Garcia                 'limits': { 'iops-total': 1000 } }
5974c5393f1SAlberto Garcia        result = self.vm.qmp('object-add', conv_keys = False, **opts)
5984c5393f1SAlberto Garcia        self.assert_qmp(result, 'return', {})
5994c5393f1SAlberto Garcia
6004c5393f1SAlberto Garcia        # Add a throttle filter with the group that we just created.
6014c5393f1SAlberto Garcia        # The filter is not used by anyone yet
6024c5393f1SAlberto Garcia        opts = { 'driver': 'throttle', 'node-name': 'throttle0',
6034c5393f1SAlberto Garcia                 'throttle-group': 'group0', 'file': 'hd0-file' }
6044c5393f1SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
6054c5393f1SAlberto Garcia        self.assert_qmp(result, 'return', {})
6064c5393f1SAlberto Garcia
6074c5393f1SAlberto Garcia        # Insert the throttle filter between hd0 and hd0-file
6084c5393f1SAlberto Garcia        self.reopen(hd0_opts, {'file': 'throttle0'})
6094c5393f1SAlberto Garcia
6104c5393f1SAlberto Garcia        # Remove the throttle filter from hd0
6114c5393f1SAlberto Garcia        self.reopen(hd0_opts, {'file': 'hd0-file'})
6124c5393f1SAlberto Garcia
6134c5393f1SAlberto Garcia    # Insert (and remove) a compress filter
61478935fcdSKevin Wolf    @iotests.skip_if_unsupported(['compress'])
6154c5393f1SAlberto Garcia    def test_insert_compress_filter(self):
6164c5393f1SAlberto Garcia        # Add an image to the VM: hd (raw) -> hd0 (qcow2) -> hd0-file (file)
6174c5393f1SAlberto Garcia        opts = {'driver': 'raw', 'node-name': 'hd', 'file': hd_opts(0)}
6184c5393f1SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
6194c5393f1SAlberto Garcia        self.assert_qmp(result, 'return', {})
6204c5393f1SAlberto Garcia
6214c5393f1SAlberto Garcia        # Add a 'compress' filter
6224c5393f1SAlberto Garcia        filter_opts = {'driver': 'compress',
6234c5393f1SAlberto Garcia                       'node-name': 'compress0',
6244c5393f1SAlberto Garcia                       'file': 'hd0'}
6254c5393f1SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **filter_opts)
6264c5393f1SAlberto Garcia        self.assert_qmp(result, 'return', {})
6274c5393f1SAlberto Garcia
6284c5393f1SAlberto Garcia        # Unmap the beginning of the image (we cannot write compressed
6294c5393f1SAlberto Garcia        # data to an allocated cluster)
6304c5393f1SAlberto Garcia        self.run_qemu_io("hd", "write -z -u 0 128k")
6314c5393f1SAlberto Garcia
6324c5393f1SAlberto Garcia        # Write data to the first cluster
6334c5393f1SAlberto Garcia        self.run_qemu_io("hd", "write -P 0xa0 0 64k")
6344c5393f1SAlberto Garcia
6354c5393f1SAlberto Garcia        # Insert the filter then write to the second cluster
6364c5393f1SAlberto Garcia        # hd -> compress0 -> hd0 -> hd0-file
6374c5393f1SAlberto Garcia        self.reopen(opts, {'file': 'compress0'})
6384c5393f1SAlberto Garcia        self.run_qemu_io("hd", "write -P 0xa1 64k 64k")
6394c5393f1SAlberto Garcia
6404c5393f1SAlberto Garcia        # Remove the filter then write to the third cluster
6414c5393f1SAlberto Garcia        # hd -> hd0 -> hd0-file
6424c5393f1SAlberto Garcia        self.reopen(opts, {'file': 'hd0'})
6434c5393f1SAlberto Garcia        self.run_qemu_io("hd", "write -P 0xa2 128k 64k")
6444c5393f1SAlberto Garcia
6454c5393f1SAlberto Garcia        # Verify the data that we just wrote
6464c5393f1SAlberto Garcia        self.run_qemu_io("hd", "read -P 0xa0    0 64k")
6474c5393f1SAlberto Garcia        self.run_qemu_io("hd", "read -P 0xa1  64k 64k")
6484c5393f1SAlberto Garcia        self.run_qemu_io("hd", "read -P 0xa2 128k 64k")
6494c5393f1SAlberto Garcia
6504c5393f1SAlberto Garcia        self.vm.shutdown()
6514c5393f1SAlberto Garcia
6524c5393f1SAlberto Garcia        # Check the first byte of the first three L2 entries and verify that
6534c5393f1SAlberto Garcia        # the second one is compressed (0x40) while the others are not (0x80)
65478935fcdSKevin Wolf        iotests.qemu_io('-f', 'raw', '-c', 'read -P 0x80 0x40000 1',
6554c5393f1SAlberto Garcia                                     '-c', 'read -P 0x40 0x40008 1',
6564c5393f1SAlberto Garcia                                     '-c', 'read -P 0x80 0x40010 1', hd_path[0])
6574c5393f1SAlberto Garcia
658246ebc2dSAlberto Garcia    # Swap the disk images of two active block devices
659246ebc2dSAlberto Garcia    def test_swap_files(self):
660246ebc2dSAlberto Garcia        # Add hd0 and hd2 (none of them with backing files)
661246ebc2dSAlberto Garcia        opts0 = hd_opts(0)
662246ebc2dSAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts0)
663246ebc2dSAlberto Garcia        self.assert_qmp(result, 'return', {})
664246ebc2dSAlberto Garcia
665246ebc2dSAlberto Garcia        opts2 = hd_opts(2)
666246ebc2dSAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts2)
667246ebc2dSAlberto Garcia        self.assert_qmp(result, 'return', {})
668246ebc2dSAlberto Garcia
669246ebc2dSAlberto Garcia        # Write different data to both block devices
670246ebc2dSAlberto Garcia        self.run_qemu_io("hd0", "write -P 0xa0 0 1k")
671246ebc2dSAlberto Garcia        self.run_qemu_io("hd2", "write -P 0xa2 0 1k")
672246ebc2dSAlberto Garcia
673246ebc2dSAlberto Garcia        # Check that the data reads correctly
674246ebc2dSAlberto Garcia        self.run_qemu_io("hd0", "read  -P 0xa0 0 1k")
675246ebc2dSAlberto Garcia        self.run_qemu_io("hd2", "read  -P 0xa2 0 1k")
676246ebc2dSAlberto Garcia
677246ebc2dSAlberto Garcia        # It's not possible to make a block device use an image that
678246ebc2dSAlberto Garcia        # is already being used by the other device.
679246ebc2dSAlberto Garcia        self.reopen(opts0, {'file': 'hd2-file'},
680246ebc2dSAlberto Garcia                    "Permission conflict on node 'hd2-file': permissions "
681246ebc2dSAlberto Garcia                    "'write, resize' are both required by node 'hd2' (uses "
682246ebc2dSAlberto Garcia                    "node 'hd2-file' as 'file' child) and unshared by node "
683246ebc2dSAlberto Garcia                    "'hd0' (uses node 'hd2-file' as 'file' child).")
684246ebc2dSAlberto Garcia        self.reopen(opts2, {'file': 'hd0-file'},
685246ebc2dSAlberto Garcia                    "Permission conflict on node 'hd0-file': permissions "
686246ebc2dSAlberto Garcia                    "'write, resize' are both required by node 'hd0' (uses "
687246ebc2dSAlberto Garcia                    "node 'hd0-file' as 'file' child) and unshared by node "
688246ebc2dSAlberto Garcia                    "'hd2' (uses node 'hd0-file' as 'file' child).")
689246ebc2dSAlberto Garcia
690246ebc2dSAlberto Garcia        # But we can swap the images if we reopen both devices at the
691246ebc2dSAlberto Garcia        # same time
692246ebc2dSAlberto Garcia        opts0['file'] = 'hd2-file'
693246ebc2dSAlberto Garcia        opts2['file'] = 'hd0-file'
694246ebc2dSAlberto Garcia        self.reopenMultiple([opts0, opts2])
695246ebc2dSAlberto Garcia        self.run_qemu_io("hd0", "read  -P 0xa2 0 1k")
696246ebc2dSAlberto Garcia        self.run_qemu_io("hd2", "read  -P 0xa0 0 1k")
697246ebc2dSAlberto Garcia
698246ebc2dSAlberto Garcia        # And we can of course come back to the original state
699246ebc2dSAlberto Garcia        opts0['file'] = 'hd0-file'
700246ebc2dSAlberto Garcia        opts2['file'] = 'hd2-file'
701246ebc2dSAlberto Garcia        self.reopenMultiple([opts0, opts2])
702246ebc2dSAlberto Garcia        self.run_qemu_io("hd0", "read  -P 0xa0 0 1k")
703246ebc2dSAlberto Garcia        self.run_qemu_io("hd2", "read  -P 0xa2 0 1k")
704246ebc2dSAlberto Garcia
705bf3e50f6SAlberto Garcia    # Misc reopen tests with different block drivers
7069442bebeSThomas Huth    @iotests.skip_if_unsupported(['quorum', 'throttle'])
707bf3e50f6SAlberto Garcia    def test_misc_drivers(self):
708bf3e50f6SAlberto Garcia        ####################
709bf3e50f6SAlberto Garcia        ###### quorum ######
710bf3e50f6SAlberto Garcia        ####################
711bf3e50f6SAlberto Garcia        for i in range(3):
712bf3e50f6SAlberto Garcia            opts = hd_opts(i)
713bf3e50f6SAlberto Garcia            # Open all three images without backing file
714bf3e50f6SAlberto Garcia            opts['backing'] = None
715bf3e50f6SAlberto Garcia            result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
716bf3e50f6SAlberto Garcia            self.assert_qmp(result, 'return', {})
717bf3e50f6SAlberto Garcia
718bf3e50f6SAlberto Garcia        opts = {'driver': 'quorum',
719bf3e50f6SAlberto Garcia                'node-name': 'quorum0',
720bf3e50f6SAlberto Garcia                'children': ['hd0', 'hd1', 'hd2'],
721bf3e50f6SAlberto Garcia                'vote-threshold': 2}
722bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
723bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
724bf3e50f6SAlberto Garcia
725bf3e50f6SAlberto Garcia        # Quorum doesn't currently allow reopening. TODO: implement this
726bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "Block format 'quorum' used by node 'quorum0'" +
727bf3e50f6SAlberto Garcia                    " does not support reopening files")
728bf3e50f6SAlberto Garcia
729bf3e50f6SAlberto Garcia        # You can't make quorum0 a backing file of hd0:
730bf3e50f6SAlberto Garcia        # hd0 is already a child of quorum0.
731bf3e50f6SAlberto Garcia        self.reopen(hd_opts(0), {'backing': 'quorum0'},
732ecd30d2dSAlberto Garcia                    "Making 'quorum0' a backing child of 'hd0' would create a cycle")
733bf3e50f6SAlberto Garcia
734bf3e50f6SAlberto Garcia        # Delete quorum0
735bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'quorum0')
736bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
737bf3e50f6SAlberto Garcia
738bf3e50f6SAlberto Garcia        # Delete hd0, hd1 and hd2
739bf3e50f6SAlberto Garcia        for i in range(3):
740bf3e50f6SAlberto Garcia            result = self.vm.qmp('blockdev-del', conv_keys = True,
741bf3e50f6SAlberto Garcia                                 node_name = 'hd%d' % i)
742bf3e50f6SAlberto Garcia            self.assert_qmp(result, 'return', {})
743bf3e50f6SAlberto Garcia
744bf3e50f6SAlberto Garcia        ######################
745bf3e50f6SAlberto Garcia        ###### blkdebug ######
746bf3e50f6SAlberto Garcia        ######################
747bf3e50f6SAlberto Garcia        opts = {'driver': 'blkdebug',
748bf3e50f6SAlberto Garcia                'node-name': 'bd',
749bf3e50f6SAlberto Garcia                'config': '/dev/null',
750bf3e50f6SAlberto Garcia                'image': hd_opts(0)}
751bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
752bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
753bf3e50f6SAlberto Garcia
754bf3e50f6SAlberto Garcia        # blkdebug allows reopening if we keep the same options
755bf3e50f6SAlberto Garcia        self.reopen(opts)
756bf3e50f6SAlberto Garcia
757bf3e50f6SAlberto Garcia        # but it currently does not allow changes
758bf3e50f6SAlberto Garcia        self.reopen(opts, {'image': 'hd1'}, "Cannot change the option 'image'")
759bf3e50f6SAlberto Garcia        self.reopen(opts, {'align': 33554432}, "Cannot change the option 'align'")
760bf3e50f6SAlberto Garcia        self.reopen(opts, {'config': '/non/existent'}, "Cannot change the option 'config'")
761bf3e50f6SAlberto Garcia        del opts['config']
762bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "Option 'config' cannot be reset to its default value")
763bf3e50f6SAlberto Garcia
764bf3e50f6SAlberto Garcia        # Delete the blkdebug node
765bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'bd')
766bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
767bf3e50f6SAlberto Garcia
768bf3e50f6SAlberto Garcia        ##################
769bf3e50f6SAlberto Garcia        ###### null ######
770bf3e50f6SAlberto Garcia        ##################
771a6f8f9f8SMax Reitz        opts = {'driver': 'null-co', 'node-name': 'root', 'size': 1024}
772bf3e50f6SAlberto Garcia
773bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
774bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
775bf3e50f6SAlberto Garcia
776bf3e50f6SAlberto Garcia        # 1 << 30 is the default value, but we cannot change it explicitly
777bf3e50f6SAlberto Garcia        self.reopen(opts, {'size': (1 << 30)}, "Cannot change the option 'size'")
778bf3e50f6SAlberto Garcia
779bf3e50f6SAlberto Garcia        # We cannot change 'size' back to its default value either
780bf3e50f6SAlberto Garcia        del opts['size']
781bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "Option 'size' cannot be reset to its default value")
782bf3e50f6SAlberto Garcia
783bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'root')
784bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
785bf3e50f6SAlberto Garcia
786bf3e50f6SAlberto Garcia        ##################
787bf3e50f6SAlberto Garcia        ###### file ######
788bf3e50f6SAlberto Garcia        ##################
789bf3e50f6SAlberto Garcia        opts = hd_opts(0)
790bf3e50f6SAlberto Garcia        opts['file']['locking'] = 'on'
791bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
792bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
793bf3e50f6SAlberto Garcia
794bf3e50f6SAlberto Garcia        # 'locking' cannot be changed
795bf3e50f6SAlberto Garcia        del opts['file']['locking']
796bf3e50f6SAlberto Garcia        self.reopen(opts, {'file.locking': 'on'})
797bf3e50f6SAlberto Garcia        self.reopen(opts, {'file.locking': 'off'}, "Cannot change the option 'locking'")
798bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "Option 'locking' cannot be reset to its default value")
799bf3e50f6SAlberto Garcia
800bf3e50f6SAlberto Garcia        # Trying to reopen the 'file' node directly does not make a difference
801bf3e50f6SAlberto Garcia        opts = opts['file']
802bf3e50f6SAlberto Garcia        self.reopen(opts, {'locking': 'on'})
803bf3e50f6SAlberto Garcia        self.reopen(opts, {'locking': 'off'}, "Cannot change the option 'locking'")
804bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "Option 'locking' cannot be reset to its default value")
805bf3e50f6SAlberto Garcia
806bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
807bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
808bf3e50f6SAlberto Garcia
809bf3e50f6SAlberto Garcia        ######################
810bf3e50f6SAlberto Garcia        ###### throttle ######
811bf3e50f6SAlberto Garcia        ######################
812bf3e50f6SAlberto Garcia        opts = { 'qom-type': 'throttle-group', 'id': 'group0',
813fa818b2fSAlberto Garcia                 'limits': { 'iops-total': 1000 } }
814bf3e50f6SAlberto Garcia        result = self.vm.qmp('object-add', conv_keys = False, **opts)
815bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
816bf3e50f6SAlberto Garcia
817bf3e50f6SAlberto Garcia        opts = { 'qom-type': 'throttle-group', 'id': 'group1',
818fa818b2fSAlberto Garcia                 'limits': { 'iops-total': 2000 } }
819bf3e50f6SAlberto Garcia        result = self.vm.qmp('object-add', conv_keys = False, **opts)
820bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
821bf3e50f6SAlberto Garcia
822bf3e50f6SAlberto Garcia        # Add a throttle filter with group = group0
823bf3e50f6SAlberto Garcia        opts = { 'driver': 'throttle', 'node-name': 'throttle0',
824bf3e50f6SAlberto Garcia                 'throttle-group': 'group0', 'file': hd_opts(0) }
825bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
826bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
827bf3e50f6SAlberto Garcia
828bf3e50f6SAlberto Garcia        # We can reopen it if we keep the same options
829bf3e50f6SAlberto Garcia        self.reopen(opts)
830bf3e50f6SAlberto Garcia
831bf3e50f6SAlberto Garcia        # We can also reopen if 'file' is a reference to the child
832bf3e50f6SAlberto Garcia        self.reopen(opts, {'file': 'hd0'})
833bf3e50f6SAlberto Garcia
834bf3e50f6SAlberto Garcia        # This is illegal
835bf3e50f6SAlberto Garcia        self.reopen(opts, {'throttle-group': 'notfound'}, "Throttle group 'notfound' does not exist")
836bf3e50f6SAlberto Garcia
837bf3e50f6SAlberto Garcia        # But it's possible to change the group to group1
838bf3e50f6SAlberto Garcia        self.reopen(opts, {'throttle-group': 'group1'})
839bf3e50f6SAlberto Garcia
840bf3e50f6SAlberto Garcia        # Now group1 is in use, it cannot be deleted
841bf3e50f6SAlberto Garcia        result = self.vm.qmp('object-del', id = 'group1')
842bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'error/class', 'GenericError')
843bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'error/desc', "object 'group1' is in use, can not be deleted")
844bf3e50f6SAlberto Garcia
845bf3e50f6SAlberto Garcia        # Default options, this switches the group back to group0
846bf3e50f6SAlberto Garcia        self.reopen(opts)
847bf3e50f6SAlberto Garcia
848bf3e50f6SAlberto Garcia        # So now we cannot delete group0
849bf3e50f6SAlberto Garcia        result = self.vm.qmp('object-del', id = 'group0')
850bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'error/class', 'GenericError')
851bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'error/desc', "object 'group0' is in use, can not be deleted")
852bf3e50f6SAlberto Garcia
853bf3e50f6SAlberto Garcia        # But group1 is free this time, and it can be deleted
854bf3e50f6SAlberto Garcia        result = self.vm.qmp('object-del', id = 'group1')
855bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
856bf3e50f6SAlberto Garcia
857bf3e50f6SAlberto Garcia        # Let's delete the filter node
858bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'throttle0')
859bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
860bf3e50f6SAlberto Garcia
861bf3e50f6SAlberto Garcia        # And we can finally get rid of group0
862bf3e50f6SAlberto Garcia        result = self.vm.qmp('object-del', id = 'group0')
863bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
864bf3e50f6SAlberto Garcia
865bf3e50f6SAlberto Garcia    # If an image has a backing file then the 'backing' option must be
866bf3e50f6SAlberto Garcia    # passed on reopen. We don't allow leaving the option out in this
867bf3e50f6SAlberto Garcia    # case because it's unclear what the correct semantics would be.
868bf3e50f6SAlberto Garcia    def test_missing_backing_options_1(self):
869bf3e50f6SAlberto Garcia        # hd2
870bf3e50f6SAlberto Garcia        opts = hd_opts(2)
871bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
872bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
873bf3e50f6SAlberto Garcia
874bf3e50f6SAlberto Garcia        # hd0
875bf3e50f6SAlberto Garcia        opts = hd_opts(0)
876bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
877bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
878bf3e50f6SAlberto Garcia
879bf3e50f6SAlberto Garcia        # hd0 has no backing file: we can omit the 'backing' option
880bf3e50f6SAlberto Garcia        self.reopen(opts)
881bf3e50f6SAlberto Garcia
882bf3e50f6SAlberto Garcia        # hd2 <- hd0
883bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing': 'hd2'})
884bf3e50f6SAlberto Garcia
885bf3e50f6SAlberto Garcia        # hd0 has a backing file: we must set the 'backing' option
886bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "backing is missing for 'hd0'")
887bf3e50f6SAlberto Garcia
888bf3e50f6SAlberto Garcia        # hd2 can't be removed because it's the backing file of hd0
889bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd2')
890bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'error/class', 'GenericError')
891bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'error/desc', "Node 'hd2' is busy: node is used as backing hd of 'hd0'")
892bf3e50f6SAlberto Garcia
893bf3e50f6SAlberto Garcia        # Detach hd2 from hd0.
894bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing': None})
8950b877d09SMax Reitz
8960b877d09SMax Reitz        # Without a backing file, we can omit 'backing' again
8970b877d09SMax Reitz        self.reopen(opts)
898bf3e50f6SAlberto Garcia
899bf3e50f6SAlberto Garcia        # Remove both hd0 and hd2
900bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
901bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
902bf3e50f6SAlberto Garcia
903bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd2')
904bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
905bf3e50f6SAlberto Garcia
906bf3e50f6SAlberto Garcia    # If an image has default backing file (as part of its metadata)
907bf3e50f6SAlberto Garcia    # then the 'backing' option must be passed on reopen. We don't
908bf3e50f6SAlberto Garcia    # allow leaving the option out in this case because it's unclear
909bf3e50f6SAlberto Garcia    # what the correct semantics would be.
910bf3e50f6SAlberto Garcia    def test_missing_backing_options_2(self):
911bf3e50f6SAlberto Garcia        # hd0 <- hd1
912bf3e50f6SAlberto Garcia        # (hd0 is hd1's default backing file)
913bf3e50f6SAlberto Garcia        opts = hd_opts(1)
914bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
915bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
916bf3e50f6SAlberto Garcia
917bf3e50f6SAlberto Garcia        # hd1 has a backing file: we can't omit the 'backing' option
918bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "backing is missing for 'hd1'")
919bf3e50f6SAlberto Garcia
920bf3e50f6SAlberto Garcia        # Let's detach the backing file
921bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing': None})
922bf3e50f6SAlberto Garcia
923bf3e50f6SAlberto Garcia        # No backing file attached to hd1 now, but we still can't omit the 'backing' option
924bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "backing is missing for 'hd1'")
925bf3e50f6SAlberto Garcia
926bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd1')
927bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
928bf3e50f6SAlberto Garcia
929bf3e50f6SAlberto Garcia    # Test that making 'backing' a reference to an existing child
930bf3e50f6SAlberto Garcia    # keeps its current options
931bf3e50f6SAlberto Garcia    def test_backing_reference(self):
932bf3e50f6SAlberto Garcia        # hd2 <- hd1 <- hd0
933bf3e50f6SAlberto Garcia        opts = hd_opts(0)
934bf3e50f6SAlberto Garcia        opts['backing'] = hd_opts(1)
935bf3e50f6SAlberto Garcia        opts['backing']['backing'] = hd_opts(2)
936bf3e50f6SAlberto Garcia        # Enable 'detect-zeroes' on all three nodes
937bf3e50f6SAlberto Garcia        opts['detect-zeroes'] = 'on'
938bf3e50f6SAlberto Garcia        opts['backing']['detect-zeroes'] = 'on'
939bf3e50f6SAlberto Garcia        opts['backing']['backing']['detect-zeroes'] = 'on'
940bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
941bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
942bf3e50f6SAlberto Garcia
943bf3e50f6SAlberto Garcia        # Reopen the chain passing the minimum amount of required options.
944bf3e50f6SAlberto Garcia        # By making 'backing' a reference to hd1 (instead of a sub-dict)
945bf3e50f6SAlberto Garcia        # we tell QEMU to keep its current set of options.
946bf3e50f6SAlberto Garcia        opts = {'driver': iotests.imgfmt,
947bf3e50f6SAlberto Garcia                'node-name': 'hd0',
948bf3e50f6SAlberto Garcia                'file': 'hd0-file',
949bf3e50f6SAlberto Garcia                'backing': 'hd1' }
950bf3e50f6SAlberto Garcia        self.reopen(opts)
951bf3e50f6SAlberto Garcia
952bf3e50f6SAlberto Garcia        # This has reset 'detect-zeroes' on hd0, but not on hd1 and hd2.
953bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd0'), 'detect_zeroes', 'off')
954bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'on')
955bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd2'), 'detect_zeroes', 'on')
956bf3e50f6SAlberto Garcia
957bf3e50f6SAlberto Garcia    # Test what happens if the graph changes due to other operations
958bf3e50f6SAlberto Garcia    # such as block-stream
959bf3e50f6SAlberto Garcia    def test_block_stream_1(self):
960bf3e50f6SAlberto Garcia        # hd1 <- hd0
961bf3e50f6SAlberto Garcia        opts = hd_opts(0)
962bf3e50f6SAlberto Garcia        opts['backing'] = hd_opts(1)
963bf3e50f6SAlberto Garcia        opts['backing']['backing'] = None
964bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
965bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
966bf3e50f6SAlberto Garcia
967bf3e50f6SAlberto Garcia        # Stream hd1 into hd0 and wait until it's done
968bf3e50f6SAlberto Garcia        result = self.vm.qmp('block-stream', conv_keys = True, job_id = 'stream0', device = 'hd0')
969bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
970bf3e50f6SAlberto Garcia        self.wait_until_completed(drive = 'stream0')
971bf3e50f6SAlberto Garcia
972bf3e50f6SAlberto Garcia        # Now we have only hd0
973bf3e50f6SAlberto Garcia        self.assertEqual(self.get_node('hd1'), None)
974bf3e50f6SAlberto Garcia
975bf3e50f6SAlberto Garcia        # We have backing.* options but there's no backing file anymore
976bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "Cannot change the option 'backing.driver'")
977bf3e50f6SAlberto Garcia
978bf3e50f6SAlberto Garcia        # If we remove the 'backing' option then we can reopen hd0 just fine
979bf3e50f6SAlberto Garcia        del opts['backing']
980bf3e50f6SAlberto Garcia        self.reopen(opts)
981bf3e50f6SAlberto Garcia
982bf3e50f6SAlberto Garcia        # We can also reopen hd0 if we set 'backing' to null
983bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing': None})
984bf3e50f6SAlberto Garcia
985bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
986bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
987bf3e50f6SAlberto Garcia
988bf3e50f6SAlberto Garcia    # Another block_stream test
989bf3e50f6SAlberto Garcia    def test_block_stream_2(self):
990bf3e50f6SAlberto Garcia        # hd2 <- hd1 <- hd0
991bf3e50f6SAlberto Garcia        opts = hd_opts(0)
992bf3e50f6SAlberto Garcia        opts['backing'] = hd_opts(1)
993bf3e50f6SAlberto Garcia        opts['backing']['backing'] = hd_opts(2)
994bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
995bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
996bf3e50f6SAlberto Garcia
997bf3e50f6SAlberto Garcia        # Stream hd1 into hd0 and wait until it's done
998bf3e50f6SAlberto Garcia        result = self.vm.qmp('block-stream', conv_keys = True, job_id = 'stream0',
999bf3e50f6SAlberto Garcia                             device = 'hd0', base_node = 'hd2')
1000bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
1001bf3e50f6SAlberto Garcia        self.wait_until_completed(drive = 'stream0')
1002bf3e50f6SAlberto Garcia
1003bf3e50f6SAlberto Garcia        # The chain is hd2 <- hd0 now. hd1 is missing
1004bf3e50f6SAlberto Garcia        self.assertEqual(self.get_node('hd1'), None)
1005bf3e50f6SAlberto Garcia
1006bf3e50f6SAlberto Garcia        # The backing options in the dict were meant for hd1, but we cannot
1007bf3e50f6SAlberto Garcia        # use them with hd2 because hd1 had a backing file while hd2 does not.
1008bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "Cannot change the option 'backing.driver'")
1009bf3e50f6SAlberto Garcia
1010bf3e50f6SAlberto Garcia        # If we remove hd1's options from the dict then things work fine
1011bf3e50f6SAlberto Garcia        opts['backing'] = opts['backing']['backing']
1012bf3e50f6SAlberto Garcia        self.reopen(opts)
1013bf3e50f6SAlberto Garcia
1014bf3e50f6SAlberto Garcia        # We can also reopen hd0 if we use a reference to the backing file
1015bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing': 'hd2'})
1016bf3e50f6SAlberto Garcia
1017bf3e50f6SAlberto Garcia        # But we cannot leave the option out
1018bf3e50f6SAlberto Garcia        del opts['backing']
1019bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "backing is missing for 'hd0'")
1020bf3e50f6SAlberto Garcia
1021bf3e50f6SAlberto Garcia        # Now we can delete hd0 (and hd2)
1022bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
1023bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
1024bf3e50f6SAlberto Garcia        self.assertEqual(self.get_node('hd2'), None)
1025bf3e50f6SAlberto Garcia
1026bf3e50f6SAlberto Garcia    # Reopen the chain during a block-stream job (from hd1 to hd0)
1027bf3e50f6SAlberto Garcia    def test_block_stream_3(self):
1028bf3e50f6SAlberto Garcia        # hd2 <- hd1 <- hd0
1029bf3e50f6SAlberto Garcia        opts = hd_opts(0)
1030bf3e50f6SAlberto Garcia        opts['backing'] = hd_opts(1)
1031bf3e50f6SAlberto Garcia        opts['backing']['backing'] = hd_opts(2)
1032bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
1033bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
1034bf3e50f6SAlberto Garcia
1035bf3e50f6SAlberto Garcia        # hd2 <- hd0
1036bf3e50f6SAlberto Garcia        result = self.vm.qmp('block-stream', conv_keys = True, job_id = 'stream0',
1037c423a6afSMax Reitz                             device = 'hd0', base_node = 'hd2',
1038c423a6afSMax Reitz                             auto_finalize = False)
1039bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
1040bf3e50f6SAlberto Garcia
1041c624b015SAndrey Shinkevich        # We can remove hd2 while the stream job is ongoing
1042bf3e50f6SAlberto Garcia        opts['backing']['backing'] = None
1043c624b015SAndrey Shinkevich        self.reopen(opts, {})
1044bf3e50f6SAlberto Garcia
1045bf3e50f6SAlberto Garcia        # We can't remove hd1 while the stream job is ongoing
1046bf3e50f6SAlberto Garcia        opts['backing'] = None
1047bfae052aSVladimir Sementsov-Ogievskiy        self.reopen(opts, {}, "Cannot change frozen 'backing' link from 'hd0' to 'hd1'")
1048bf3e50f6SAlberto Garcia
1049c423a6afSMax Reitz        self.vm.run_job('stream0', auto_finalize = False, auto_dismiss = True)
1050bf3e50f6SAlberto Garcia
1051bf3e50f6SAlberto Garcia    # Reopen the chain during a block-stream job (from hd2 to hd1)
1052bf3e50f6SAlberto Garcia    def test_block_stream_4(self):
1053bf3e50f6SAlberto Garcia        # hd2 <- hd1 <- hd0
1054bf3e50f6SAlberto Garcia        opts = hd_opts(0)
1055bf3e50f6SAlberto Garcia        opts['backing'] = hd_opts(1)
1056bf3e50f6SAlberto Garcia        opts['backing']['backing'] = hd_opts(2)
1057bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
1058bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
1059bf3e50f6SAlberto Garcia
1060bf3e50f6SAlberto Garcia        # hd1 <- hd0
1061bf3e50f6SAlberto Garcia        result = self.vm.qmp('block-stream', conv_keys = True, job_id = 'stream0',
1062205736f4SAndrey Shinkevich                             device = 'hd1', filter_node_name='cor',
1063205736f4SAndrey Shinkevich                             auto_finalize = False)
1064bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
1065bf3e50f6SAlberto Garcia
1066205736f4SAndrey Shinkevich        # We can't reopen with the original options because there is a filter
1067205736f4SAndrey Shinkevich        # inserted by stream job above hd1.
1068c423a6afSMax Reitz        self.reopen(opts, {},
1069205736f4SAndrey Shinkevich                    "Cannot change the option 'backing.backing.file.node-name'")
1070205736f4SAndrey Shinkevich
1071205736f4SAndrey Shinkevich        # We can't reopen hd1 to read-only, as block-stream requires it to be
1072205736f4SAndrey Shinkevich        # read-write
1073205736f4SAndrey Shinkevich        self.reopen(opts['backing'], {'read-only': True},
1074c20555e1SVladimir Sementsov-Ogievskiy                    "Read-only block node 'hd1' cannot support read-write users")
1075bf3e50f6SAlberto Garcia
1076bf3e50f6SAlberto Garcia        # We can't remove hd2 while the stream job is ongoing
1077bf3e50f6SAlberto Garcia        opts['backing']['backing'] = None
1078205736f4SAndrey Shinkevich        self.reopen(opts['backing'], {'read-only': False},
1079bfae052aSVladimir Sementsov-Ogievskiy                    "Cannot change frozen 'backing' link from 'hd1' to 'hd2'")
1080bf3e50f6SAlberto Garcia
1081bf3e50f6SAlberto Garcia        # We can detach hd1 from hd0 because it doesn't affect the stream job
1082bf3e50f6SAlberto Garcia        opts['backing'] = None
1083bf3e50f6SAlberto Garcia        self.reopen(opts)
1084bf3e50f6SAlberto Garcia
1085c423a6afSMax Reitz        self.vm.run_job('stream0', auto_finalize = False, auto_dismiss = True)
1086bf3e50f6SAlberto Garcia
1087bf3e50f6SAlberto Garcia    # Reopen the chain during a block-commit job (from hd0 to hd2)
1088bf3e50f6SAlberto Garcia    def test_block_commit_1(self):
1089bf3e50f6SAlberto Garcia        # hd2 <- hd1 <- hd0
1090bf3e50f6SAlberto Garcia        opts = hd_opts(0)
1091bf3e50f6SAlberto Garcia        opts['backing'] = hd_opts(1)
1092bf3e50f6SAlberto Garcia        opts['backing']['backing'] = hd_opts(2)
1093bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
1094bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
1095bf3e50f6SAlberto Garcia
1096bf3e50f6SAlberto Garcia        result = self.vm.qmp('block-commit', conv_keys = True, job_id = 'commit0',
1097c423a6afSMax Reitz                             device = 'hd0')
1098bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
1099bf3e50f6SAlberto Garcia
1100bf3e50f6SAlberto Garcia        # We can't remove hd2 while the commit job is ongoing
1101bf3e50f6SAlberto Garcia        opts['backing']['backing'] = None
1102bfae052aSVladimir Sementsov-Ogievskiy        self.reopen(opts, {}, "Cannot change frozen 'backing' link from 'hd1' to 'hd2'")
1103bf3e50f6SAlberto Garcia
1104bf3e50f6SAlberto Garcia        # We can't remove hd1 while the commit job is ongoing
1105bf3e50f6SAlberto Garcia        opts['backing'] = None
1106bfae052aSVladimir Sementsov-Ogievskiy        self.reopen(opts, {}, "Cannot change frozen 'backing' link from 'hd0' to 'hd1'")
1107bf3e50f6SAlberto Garcia
1108bf3e50f6SAlberto Garcia        event = self.vm.event_wait(name='BLOCK_JOB_READY')
1109bf3e50f6SAlberto Garcia        self.assert_qmp(event, 'data/device', 'commit0')
1110bf3e50f6SAlberto Garcia        self.assert_qmp(event, 'data/type', 'commit')
1111bf3e50f6SAlberto Garcia        self.assert_qmp_absent(event, 'data/error')
1112bf3e50f6SAlberto Garcia
1113bf3e50f6SAlberto Garcia        result = self.vm.qmp('block-job-complete', device='commit0')
1114bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
1115bf3e50f6SAlberto Garcia
1116bf3e50f6SAlberto Garcia        self.wait_until_completed(drive = 'commit0')
1117bf3e50f6SAlberto Garcia
1118bf3e50f6SAlberto Garcia    # Reopen the chain during a block-commit job (from hd1 to hd2)
1119bf3e50f6SAlberto Garcia    def test_block_commit_2(self):
1120bf3e50f6SAlberto Garcia        # hd2 <- hd1 <- hd0
1121bf3e50f6SAlberto Garcia        opts = hd_opts(0)
1122bf3e50f6SAlberto Garcia        opts['backing'] = hd_opts(1)
1123bf3e50f6SAlberto Garcia        opts['backing']['backing'] = hd_opts(2)
1124bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
1125bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
1126bf3e50f6SAlberto Garcia
1127bf3e50f6SAlberto Garcia        result = self.vm.qmp('block-commit', conv_keys = True, job_id = 'commit0',
1128c423a6afSMax Reitz                             device = 'hd0', top_node = 'hd1',
1129c423a6afSMax Reitz                             auto_finalize = False)
1130bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
1131bf3e50f6SAlberto Garcia
1132bf3e50f6SAlberto Garcia        # We can't remove hd2 while the commit job is ongoing
1133bf3e50f6SAlberto Garcia        opts['backing']['backing'] = None
1134bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "Cannot change the option 'backing.driver'")
1135bf3e50f6SAlberto Garcia
1136bf3e50f6SAlberto Garcia        # We can't remove hd1 while the commit job is ongoing
1137bf3e50f6SAlberto Garcia        opts['backing'] = None
1138ecd30d2dSAlberto Garcia        self.reopen(opts, {}, "Cannot replace implicit backing child of hd0")
1139bf3e50f6SAlberto Garcia
1140bf3e50f6SAlberto Garcia        # hd2 <- hd0
1141c423a6afSMax Reitz        self.vm.run_job('commit0', auto_finalize = False, auto_dismiss = True)
1142bf3e50f6SAlberto Garcia
1143bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd0'), 'ro', False)
1144bf3e50f6SAlberto Garcia        self.assertEqual(self.get_node('hd1'), None)
1145bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd2'), 'ro', True)
1146bf3e50f6SAlberto Garcia
1147ee810602SKevin Wolf    def run_test_iothreads(self, iothread_a, iothread_b, errmsg = None,
1148ee810602SKevin Wolf                           opts_a = None, opts_b = None):
1149ee810602SKevin Wolf        opts = opts_a or hd_opts(0)
1150bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
1151bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
1152bf3e50f6SAlberto Garcia
1153ee810602SKevin Wolf        opts2 = opts_b or hd_opts(2)
1154bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts2)
1155bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
1156bf3e50f6SAlberto Garcia
1157bf3e50f6SAlberto Garcia        result = self.vm.qmp('object-add', qom_type='iothread', id='iothread0')
1158bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
1159bf3e50f6SAlberto Garcia
1160bf3e50f6SAlberto Garcia        result = self.vm.qmp('object-add', qom_type='iothread', id='iothread1')
1161bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
1162bf3e50f6SAlberto Garcia
116397518e11SKevin Wolf        result = self.vm.qmp('device_add', driver='virtio-scsi', id='scsi0',
116497518e11SKevin Wolf                             iothread=iothread_a)
1165bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
1166bf3e50f6SAlberto Garcia
116797518e11SKevin Wolf        result = self.vm.qmp('device_add', driver='virtio-scsi', id='scsi1',
116897518e11SKevin Wolf                             iothread=iothread_b)
1169bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
1170bf3e50f6SAlberto Garcia
117197518e11SKevin Wolf        if iothread_a:
117297518e11SKevin Wolf            result = self.vm.qmp('device_add', driver='scsi-hd', drive='hd0',
117397518e11SKevin Wolf                                 share_rw=True, bus="scsi0.0")
1174bf3e50f6SAlberto Garcia            self.assert_qmp(result, 'return', {})
1175bf3e50f6SAlberto Garcia
117697518e11SKevin Wolf        if iothread_b:
117797518e11SKevin Wolf            result = self.vm.qmp('device_add', driver='scsi-hd', drive='hd2',
117897518e11SKevin Wolf                                 share_rw=True, bus="scsi1.0")
117997518e11SKevin Wolf            self.assert_qmp(result, 'return', {})
118097518e11SKevin Wolf
118197518e11SKevin Wolf        # Attaching the backing file may or may not work
118297518e11SKevin Wolf        self.reopen(opts, {'backing': 'hd2'}, errmsg)
118397518e11SKevin Wolf
118497518e11SKevin Wolf        # But removing the backing file should always work
118597518e11SKevin Wolf        self.reopen(opts, {'backing': None})
118697518e11SKevin Wolf
118797518e11SKevin Wolf        self.vm.shutdown()
118897518e11SKevin Wolf
118997518e11SKevin Wolf    # We don't allow setting a backing file that uses a different AioContext if
119097518e11SKevin Wolf    # neither of them can switch to the other AioContext
119197518e11SKevin Wolf    def test_iothreads_error(self):
119297518e11SKevin Wolf        self.run_test_iothreads('iothread0', 'iothread1',
11931de6b45fSKevin Wolf                                "Cannot change iothread of active block backend")
119497518e11SKevin Wolf
119597518e11SKevin Wolf    def test_iothreads_compatible_users(self):
119697518e11SKevin Wolf        self.run_test_iothreads('iothread0', 'iothread0')
119797518e11SKevin Wolf
119897518e11SKevin Wolf    def test_iothreads_switch_backing(self):
1199e2c8eb14SKevin Wolf        self.run_test_iothreads('iothread0', '')
120097518e11SKevin Wolf
120197518e11SKevin Wolf    def test_iothreads_switch_overlay(self):
1202e2c8eb14SKevin Wolf        self.run_test_iothreads('', 'iothread0')
1203bf3e50f6SAlberto Garcia
1204ee810602SKevin Wolf    def test_iothreads_with_throttling(self):
1205ee810602SKevin Wolf        # Create a throttle-group object
1206ee810602SKevin Wolf        opts = { 'qom-type': 'throttle-group', 'id': 'group0',
1207ee810602SKevin Wolf                 'limits': { 'iops-total': 1000 } }
1208ee810602SKevin Wolf        result = self.vm.qmp('object-add', conv_keys = False, **opts)
1209ee810602SKevin Wolf        self.assert_qmp(result, 'return', {})
1210ee810602SKevin Wolf
1211ee810602SKevin Wolf        # Options with a throttle filter between format and protocol
1212ee810602SKevin Wolf        opts = [
1213ee810602SKevin Wolf            {
1214ee810602SKevin Wolf                'driver': iotests.imgfmt,
1215ee810602SKevin Wolf                'node-name': f'hd{idx}',
1216ee810602SKevin Wolf                'file' : {
1217ee810602SKevin Wolf                    'node-name': f'hd{idx}-throttle',
1218ee810602SKevin Wolf                    'driver': 'throttle',
1219ee810602SKevin Wolf                    'throttle-group': 'group0',
1220ee810602SKevin Wolf                    'file': {
1221ee810602SKevin Wolf                        'driver': 'file',
1222ee810602SKevin Wolf                        'node-name': f'hd{idx}-file',
1223ee810602SKevin Wolf                        'filename': hd_path[idx],
1224ee810602SKevin Wolf                    },
1225ee810602SKevin Wolf                },
1226ee810602SKevin Wolf            }
1227ee810602SKevin Wolf            for idx in (0, 2)
1228ee810602SKevin Wolf        ]
1229ee810602SKevin Wolf
1230ee810602SKevin Wolf        self.run_test_iothreads('iothread0', 'iothread0', None,
1231ee810602SKevin Wolf                                opts[0], opts[1])
1232ee810602SKevin Wolf
1233bf3e50f6SAlberto Garciaif __name__ == '__main__':
123452ea799eSJohn Snow    iotests.activate_logging()
1235103cbc77SMax Reitz    iotests.main(supported_fmts=["qcow2"],
1236103cbc77SMax Reitz                 supported_protocols=["file"])
1237