xref: /openbmc/qemu/tests/qemu-iotests/245 (revision 812b31d3)
1#!/usr/bin/env python3
2# group: rw
3#
4# Test cases for the QMP 'x-blockdev-reopen' command
5#
6# Copyright (C) 2018-2019 Igalia, S.L.
7# Author: Alberto Garcia <berto@igalia.com>
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program.  If not, see <http://www.gnu.org/licenses/>.
21#
22
23import os
24import re
25import iotests
26import copy
27import json
28from iotests import qemu_img, qemu_io
29
30hd_path = [
31    os.path.join(iotests.test_dir, 'hd0.img'),
32    os.path.join(iotests.test_dir, 'hd1.img'),
33    os.path.join(iotests.test_dir, 'hd2.img')
34]
35
36def hd_opts(idx):
37    return {'driver': iotests.imgfmt,
38            'node-name': 'hd%d' % idx,
39            'file': {'driver': 'file',
40                     'node-name': 'hd%d-file' % idx,
41                     'filename':  hd_path[idx] } }
42
43class TestBlockdevReopen(iotests.QMPTestCase):
44    total_io_cmds = 0
45
46    def setUp(self):
47        qemu_img('create', '-f', iotests.imgfmt, hd_path[0], '3M')
48        qemu_img('create', '-f', iotests.imgfmt, '-b', hd_path[0],
49                 '-F', iotests.imgfmt, hd_path[1])
50        qemu_img('create', '-f', iotests.imgfmt, hd_path[2], '3M')
51        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xa0  0 1M', hd_path[0])
52        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xa1 1M 1M', hd_path[1])
53        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xa2 2M 1M', hd_path[2])
54        self.vm = iotests.VM()
55        self.vm.launch()
56
57    def tearDown(self):
58        self.vm.shutdown()
59        self.check_qemu_io_errors()
60        os.remove(hd_path[0])
61        os.remove(hd_path[1])
62        os.remove(hd_path[2])
63
64    # The output of qemu-io is not returned by vm.hmp_qemu_io() but
65    # it's stored in the log and can only be read when the VM has been
66    # shut down. This function runs qemu-io and keeps track of the
67    # number of times it's been called.
68    def run_qemu_io(self, img, cmd):
69        result = self.vm.hmp_qemu_io(img, cmd)
70        self.assert_qmp(result, 'return', '')
71        self.total_io_cmds += 1
72
73    # Once the VM is shut down we can parse the log and see if qemu-io
74    # ran without errors.
75    def check_qemu_io_errors(self):
76        self.assertFalse(self.vm.is_running())
77        found = 0
78        log = self.vm.get_log()
79        for line in log.split("\n"):
80            if line.startswith("Pattern verification failed"):
81                raise Exception("%s (command #%d)" % (line, found))
82            if re.match("(read|wrote) .*/.* bytes at offset", line):
83                found += 1
84        self.assertEqual(found, self.total_io_cmds,
85                         "Expected output of %d qemu-io commands, found %d" %
86                         (found, self.total_io_cmds))
87
88    # Run x-blockdev-reopen with 'opts' but applying 'newopts'
89    # on top of it. The original 'opts' dict is unmodified
90    def reopen(self, opts, newopts = {}, errmsg = None):
91        opts = copy.deepcopy(opts)
92
93        # Apply the changes from 'newopts' on top of 'opts'
94        for key in newopts:
95            value = newopts[key]
96            # If key has the form "foo.bar" then we need to do
97            # opts["foo"]["bar"] = value, not opts["foo.bar"] = value
98            subdict = opts
99            while key.find('.') != -1:
100                [prefix, key] = key.split('.', 1)
101                subdict = opts[prefix]
102            subdict[key] = value
103
104        result = self.vm.qmp('x-blockdev-reopen', conv_keys = False, **opts)
105        if errmsg:
106            self.assert_qmp(result, 'error/class', 'GenericError')
107            self.assert_qmp(result, 'error/desc', errmsg)
108        else:
109            self.assert_qmp(result, 'return', {})
110
111
112    # Run query-named-block-nodes and return the specified entry
113    def get_node(self, node_name):
114        result = self.vm.qmp('query-named-block-nodes')
115        for node in result['return']:
116            if node['node-name'] == node_name:
117                return node
118        return None
119
120    # Run 'query-named-block-nodes' and compare its output with the
121    # value passed by the user in 'graph'
122    def check_node_graph(self, graph):
123        result = self.vm.qmp('query-named-block-nodes')
124        self.assertEqual(json.dumps(graph,  sort_keys=True),
125                         json.dumps(result, sort_keys=True))
126
127    # This test opens one single disk image (without backing files)
128    # and tries to reopen it with illegal / incorrect parameters.
129    def test_incorrect_parameters_single_file(self):
130        # Open 'hd0' only (no backing files)
131        opts = hd_opts(0)
132        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
133        self.assert_qmp(result, 'return', {})
134        original_graph = self.vm.qmp('query-named-block-nodes')
135
136        # We can reopen the image passing the same options
137        self.reopen(opts)
138
139        # We can also reopen passing a child reference in 'file'
140        self.reopen(opts, {'file': 'hd0-file'})
141
142        # We cannot change any of these
143        self.reopen(opts, {'node-name': 'not-found'}, "Failed to find node with node-name='not-found'")
144        self.reopen(opts, {'node-name': ''}, "Failed to find node with node-name=''")
145        self.reopen(opts, {'node-name': None}, "Invalid parameter type for 'node-name', expected: string")
146        self.reopen(opts, {'driver': 'raw'}, "Cannot change the option 'driver'")
147        self.reopen(opts, {'driver': ''}, "Invalid parameter ''")
148        self.reopen(opts, {'driver': None}, "Invalid parameter type for 'driver', expected: string")
149        self.reopen(opts, {'file': 'not-found'}, "Cannot find device='' nor node-name='not-found'")
150        self.reopen(opts, {'file': ''}, "Cannot find device='' nor node-name=''")
151        self.reopen(opts, {'file': None}, "Invalid parameter type for 'file', expected: BlockdevRef")
152        self.reopen(opts, {'file.node-name': 'newname'}, "Cannot change the option 'node-name'")
153        self.reopen(opts, {'file.driver': 'host_device'}, "Cannot change the option 'driver'")
154        self.reopen(opts, {'file.filename': hd_path[1]}, "Cannot change the option 'filename'")
155        self.reopen(opts, {'file.aio': 'native'}, "Cannot change the option 'aio'")
156        self.reopen(opts, {'file.locking': 'off'}, "Cannot change the option 'locking'")
157        self.reopen(opts, {'file.filename': None}, "Invalid parameter type for 'file.filename', expected: string")
158
159        # node-name is optional in BlockdevOptions, but x-blockdev-reopen needs it
160        del opts['node-name']
161        self.reopen(opts, {}, "node-name not specified")
162
163        # Check that nothing has changed
164        self.check_node_graph(original_graph)
165
166        # Remove the node
167        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
168        self.assert_qmp(result, 'return', {})
169
170    # This test opens an image with a backing file and tries to reopen
171    # it with illegal / incorrect parameters.
172    def test_incorrect_parameters_backing_file(self):
173        # Open hd1 omitting the backing options (hd0 will be opened
174        # with the default options)
175        opts = hd_opts(1)
176        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
177        self.assert_qmp(result, 'return', {})
178        original_graph = self.vm.qmp('query-named-block-nodes')
179
180        # We can't reopen the image passing the same options, 'backing' is mandatory
181        self.reopen(opts, {}, "backing is missing for 'hd1'")
182
183        # Everything works if we pass 'backing' using the existing node name
184        for node in original_graph['return']:
185            if node['drv'] == iotests.imgfmt and node['file'] == hd_path[0]:
186                backing_node_name = node['node-name']
187        self.reopen(opts, {'backing': backing_node_name})
188
189        # We can't use a non-existing or empty (non-NULL) node as the backing image
190        self.reopen(opts, {'backing': 'not-found'}, "Cannot find device=\'\' nor node-name=\'not-found\'")
191        self.reopen(opts, {'backing': ''}, "Cannot find device=\'\' nor node-name=\'\'")
192
193        # We can reopen the image just fine if we specify the backing options
194        opts['backing'] = {'driver': iotests.imgfmt,
195                           'file': {'driver': 'file',
196                                    'filename': hd_path[0]}}
197        self.reopen(opts)
198
199        # We cannot change any of these options
200        self.reopen(opts, {'backing.node-name': 'newname'}, "Cannot change the option 'node-name'")
201        self.reopen(opts, {'backing.driver': 'raw'}, "Cannot change the option 'driver'")
202        self.reopen(opts, {'backing.file.node-name': 'newname'}, "Cannot change the option 'node-name'")
203        self.reopen(opts, {'backing.file.driver': 'host_device'}, "Cannot change the option 'driver'")
204
205        # Check that nothing has changed since the beginning
206        self.check_node_graph(original_graph)
207
208        # Remove the node
209        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd1')
210        self.assert_qmp(result, 'return', {})
211
212    # Reopen an image several times changing some of its options
213    def test_reopen(self):
214        # Check whether the filesystem supports O_DIRECT
215        if 'O_DIRECT' in qemu_io('-f', 'raw', '-t', 'none', '-c', 'quit', hd_path[0]):
216            supports_direct = False
217        else:
218            supports_direct = True
219
220        # Open the hd1 image passing all backing options
221        opts = hd_opts(1)
222        opts['backing'] = hd_opts(0)
223        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
224        self.assert_qmp(result, 'return', {})
225        original_graph = self.vm.qmp('query-named-block-nodes')
226
227        # We can reopen the image passing the same options
228        self.reopen(opts)
229
230        # Reopen in read-only mode
231        self.assert_qmp(self.get_node('hd1'), 'ro', False)
232
233        self.reopen(opts, {'read-only': True})
234        self.assert_qmp(self.get_node('hd1'), 'ro', True)
235        self.reopen(opts)
236        self.assert_qmp(self.get_node('hd1'), 'ro', False)
237
238        # Change the cache options
239        self.assert_qmp(self.get_node('hd1'), 'cache/writeback', True)
240        self.assert_qmp(self.get_node('hd1'), 'cache/direct', False)
241        self.assert_qmp(self.get_node('hd1'), 'cache/no-flush', False)
242        self.reopen(opts, {'cache': { 'direct': supports_direct, 'no-flush': True }})
243        self.assert_qmp(self.get_node('hd1'), 'cache/writeback', True)
244        self.assert_qmp(self.get_node('hd1'), 'cache/direct', supports_direct)
245        self.assert_qmp(self.get_node('hd1'), 'cache/no-flush', True)
246
247        # Reopen again with the original options
248        self.reopen(opts)
249        self.assert_qmp(self.get_node('hd1'), 'cache/writeback', True)
250        self.assert_qmp(self.get_node('hd1'), 'cache/direct', False)
251        self.assert_qmp(self.get_node('hd1'), 'cache/no-flush', False)
252
253        # Change 'detect-zeroes' and 'discard'
254        self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'off')
255        self.reopen(opts, {'detect-zeroes': 'on'})
256        self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'on')
257        self.reopen(opts, {'detect-zeroes': 'unmap'},
258                    "setting detect-zeroes to unmap is not allowed " +
259                    "without setting discard operation to unmap")
260        self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'on')
261        self.reopen(opts, {'detect-zeroes': 'unmap', 'discard': 'unmap'})
262        self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'unmap')
263        self.reopen(opts)
264        self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'off')
265
266        # Changing 'force-share' is currently not supported
267        self.reopen(opts, {'force-share': True}, "Cannot change the option 'force-share'")
268
269        # Change some qcow2-specific options
270        # No way to test for success other than checking the return message
271        if iotests.imgfmt == 'qcow2':
272            self.reopen(opts, {'l2-cache-entry-size': 128 * 1024},
273                        "L2 cache entry size must be a power of two "+
274                        "between 512 and the cluster size (65536)")
275            self.reopen(opts, {'l2-cache-size': 1024 * 1024,
276                               'cache-size':     512 * 1024},
277                        "l2-cache-size may not exceed cache-size")
278            self.reopen(opts, {'l2-cache-size':        4 * 1024 * 1024,
279                               'refcount-cache-size':  4 * 1024 * 1024,
280                               'l2-cache-entry-size': 32 * 1024})
281            self.reopen(opts, {'pass-discard-request': True})
282
283        # Check that nothing has changed since the beginning
284        # (from the parameters that we can check)
285        self.check_node_graph(original_graph)
286
287        # Check that the node names (other than the top-level one) are optional
288        del opts['file']['node-name']
289        del opts['backing']['node-name']
290        del opts['backing']['file']['node-name']
291        self.reopen(opts)
292        self.check_node_graph(original_graph)
293
294        # Reopen setting backing = null, this removes the backing image from the chain
295        self.reopen(opts, {'backing': None})
296        self.assert_qmp_absent(self.get_node('hd1'), 'image/backing-image')
297
298        # Open the 'hd0' image
299        result = self.vm.qmp('blockdev-add', conv_keys = False, **hd_opts(0))
300        self.assert_qmp(result, 'return', {})
301
302        # Reopen the hd1 image setting 'hd0' as its backing image
303        self.reopen(opts, {'backing': 'hd0'})
304        self.assert_qmp(self.get_node('hd1'), 'image/backing-image/filename', hd_path[0])
305
306        # Check that nothing has changed since the beginning
307        self.reopen(hd_opts(0), {'read-only': True})
308        self.check_node_graph(original_graph)
309
310        # The backing file (hd0) is now a reference, we cannot change backing.* anymore
311        self.reopen(opts, {}, "Cannot change the option 'backing.driver'")
312
313        # We can't remove 'hd0' while it's a backing image of 'hd1'
314        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
315        self.assert_qmp(result, 'error/class', 'GenericError')
316        self.assert_qmp(result, 'error/desc', "Node 'hd0' is busy: node is used as backing hd of 'hd1'")
317
318        # But we can remove both nodes if done in the proper order
319        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd1')
320        self.assert_qmp(result, 'return', {})
321        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
322        self.assert_qmp(result, 'return', {})
323
324    # Reopen a raw image and see the effect of changing the 'offset' option
325    def test_reopen_raw(self):
326        opts = {'driver': 'raw', 'node-name': 'hd0',
327                'file': { 'driver': 'file',
328                          'filename': hd_path[0],
329                          'node-name': 'hd0-file' } }
330
331        # First we create a 2MB raw file, and fill each half with a
332        # different value
333        qemu_img('create', '-f', 'raw', hd_path[0], '2M')
334        qemu_io('-f', 'raw', '-c', 'write -P 0xa0  0 1M', hd_path[0])
335        qemu_io('-f', 'raw', '-c', 'write -P 0xa1 1M 1M', hd_path[0])
336
337        # Open the raw file with QEMU
338        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
339        self.assert_qmp(result, 'return', {})
340
341        # Read 1MB from offset 0
342        self.run_qemu_io("hd0", "read -P 0xa0  0 1M")
343
344        # Reopen the image with a 1MB offset.
345        # Now the results are different
346        self.reopen(opts, {'offset': 1024*1024})
347        self.run_qemu_io("hd0", "read -P 0xa1  0 1M")
348
349        # Reopen again with the original options.
350        # We get the original results again
351        self.reopen(opts)
352        self.run_qemu_io("hd0", "read -P 0xa0  0 1M")
353
354        # Remove the block device
355        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
356        self.assert_qmp(result, 'return', {})
357
358    # Omitting an option should reset it to the default value, but if
359    # an option cannot be changed it shouldn't be possible to reset it
360    # to its default value either
361    def test_reset_default_values(self):
362        opts = {'driver': 'qcow2', 'node-name': 'hd0',
363                'file': { 'driver': 'file',
364                          'filename': hd_path[0],
365                          'x-check-cache-dropped': True, # This one can be changed
366                          'locking': 'off',              # This one can NOT be changed
367                          'node-name': 'hd0-file' } }
368
369        # Open the file with QEMU
370        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
371        self.assert_qmp(result, 'return', {})
372
373        # file.x-check-cache-dropped can be changed...
374        self.reopen(opts, { 'file.x-check-cache-dropped': False })
375        # ...and dropped completely (resetting to the default value)
376        del opts['file']['x-check-cache-dropped']
377        self.reopen(opts)
378
379        # file.locking cannot be changed nor reset to the default value
380        self.reopen(opts, { 'file.locking': 'on' }, "Cannot change the option 'locking'")
381        del opts['file']['locking']
382        self.reopen(opts, {}, "Option 'locking' cannot be reset to its default value")
383        # But we can reopen it if we maintain its previous value
384        self.reopen(opts, { 'file.locking': 'off' })
385
386        # Remove the block device
387        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
388        self.assert_qmp(result, 'return', {})
389
390    # This test modifies the node graph a few times by changing the
391    # 'backing' option on reopen and verifies that the guest data that
392    # is read afterwards is consistent with the graph changes.
393    def test_io_with_graph_changes(self):
394        opts = []
395
396        # Open hd0, hd1 and hd2 without any backing image
397        for i in range(3):
398            opts.append(hd_opts(i))
399            opts[i]['backing'] = None
400            result = self.vm.qmp('blockdev-add', conv_keys = False, **opts[i])
401            self.assert_qmp(result, 'return', {})
402
403        # hd0
404        self.run_qemu_io("hd0", "read -P 0xa0  0 1M")
405        self.run_qemu_io("hd0", "read -P 0    1M 1M")
406        self.run_qemu_io("hd0", "read -P 0    2M 1M")
407
408        # hd1 <- hd0
409        self.reopen(opts[0], {'backing': 'hd1'})
410
411        self.run_qemu_io("hd0", "read -P 0xa0  0 1M")
412        self.run_qemu_io("hd0", "read -P 0xa1 1M 1M")
413        self.run_qemu_io("hd0", "read -P 0    2M 1M")
414
415        # hd1 <- hd0 , hd1 <- hd2
416        self.reopen(opts[2], {'backing': 'hd1'})
417
418        self.run_qemu_io("hd2", "read -P 0     0 1M")
419        self.run_qemu_io("hd2", "read -P 0xa1 1M 1M")
420        self.run_qemu_io("hd2", "read -P 0xa2 2M 1M")
421
422        # hd1 <- hd2 <- hd0
423        self.reopen(opts[0], {'backing': 'hd2'})
424
425        self.run_qemu_io("hd0", "read -P 0xa0  0 1M")
426        self.run_qemu_io("hd0", "read -P 0xa1 1M 1M")
427        self.run_qemu_io("hd0", "read -P 0xa2 2M 1M")
428
429        # hd2 <- hd0
430        self.reopen(opts[2], {'backing': None})
431
432        self.run_qemu_io("hd0", "read -P 0xa0  0 1M")
433        self.run_qemu_io("hd0", "read -P 0    1M 1M")
434        self.run_qemu_io("hd0", "read -P 0xa2 2M 1M")
435
436        # hd2 <- hd1 <- hd0
437        self.reopen(opts[1], {'backing': 'hd2'})
438        self.reopen(opts[0], {'backing': 'hd1'})
439
440        self.run_qemu_io("hd0", "read -P 0xa0  0 1M")
441        self.run_qemu_io("hd0", "read -P 0xa1 1M 1M")
442        self.run_qemu_io("hd0", "read -P 0xa2 2M 1M")
443
444        # Illegal operation: hd2 is a child of hd1
445        self.reopen(opts[2], {'backing': 'hd1'},
446                    "Making 'hd1' a backing child of 'hd2' would create a cycle")
447
448        # hd2 <- hd0, hd2 <- hd1
449        self.reopen(opts[0], {'backing': 'hd2'})
450
451        self.run_qemu_io("hd1", "read -P 0     0 1M")
452        self.run_qemu_io("hd1", "read -P 0xa1 1M 1M")
453        self.run_qemu_io("hd1", "read -P 0xa2 2M 1M")
454
455        # More illegal operations
456        self.reopen(opts[2], {'backing': 'hd1'},
457                    "Making 'hd1' a backing child of 'hd2' would create a cycle")
458        self.reopen(opts[2], {'file': 'hd0-file'},
459                    "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).")
460
461        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd2')
462        self.assert_qmp(result, 'error/class', 'GenericError')
463        self.assert_qmp(result, 'error/desc', "Node 'hd2' is busy: node is used as backing hd of 'hd0'")
464
465        # hd1 doesn't have a backing file now
466        self.reopen(opts[1], {'backing': None})
467
468        self.run_qemu_io("hd1", "read -P 0     0 1M")
469        self.run_qemu_io("hd1", "read -P 0xa1 1M 1M")
470        self.run_qemu_io("hd1", "read -P 0    2M 1M")
471
472        # We can't remove the 'backing' option if the image has a
473        # default backing file
474        del opts[1]['backing']
475        self.reopen(opts[1], {}, "backing is missing for 'hd1'")
476
477        self.run_qemu_io("hd1", "read -P 0     0 1M")
478        self.run_qemu_io("hd1", "read -P 0xa1 1M 1M")
479        self.run_qemu_io("hd1", "read -P 0    2M 1M")
480
481    # This test verifies that we can't change the children of a block
482    # device during a reopen operation in a way that would create
483    # cycles in the node graph
484    @iotests.skip_if_unsupported(['blkverify'])
485    def test_graph_cycles(self):
486        opts = []
487
488        # Open all three images without backing file
489        for i in range(3):
490            opts.append(hd_opts(i))
491            opts[i]['backing'] = None
492            result = self.vm.qmp('blockdev-add', conv_keys = False, **opts[i])
493            self.assert_qmp(result, 'return', {})
494
495        # hd1 <- hd0, hd1 <- hd2
496        self.reopen(opts[0], {'backing': 'hd1'})
497        self.reopen(opts[2], {'backing': 'hd1'})
498
499        # Illegal: hd2 is backed by hd1
500        self.reopen(opts[1], {'backing': 'hd2'},
501                    "Making 'hd2' a backing child of 'hd1' would create a cycle")
502
503        # hd1 <- hd0 <- hd2
504        self.reopen(opts[2], {'backing': 'hd0'})
505
506        # Illegal: hd2 is backed by hd0, which is backed by hd1
507        self.reopen(opts[1], {'backing': 'hd2'},
508                    "Making 'hd2' a backing child of 'hd1' would create a cycle")
509
510        # Illegal: hd1 cannot point to itself
511        self.reopen(opts[1], {'backing': 'hd1'},
512                    "Making 'hd1' a backing child of 'hd1' would create a cycle")
513
514        # Remove all backing files
515        self.reopen(opts[0])
516        self.reopen(opts[2])
517
518        ##########################################
519        # Add a blkverify node using hd0 and hd1 #
520        ##########################################
521        bvopts = {'driver': 'blkverify',
522                  'node-name': 'bv',
523                  'test': 'hd0',
524                  'raw': 'hd1'}
525        result = self.vm.qmp('blockdev-add', conv_keys = False, **bvopts)
526        self.assert_qmp(result, 'return', {})
527
528        # blkverify doesn't currently allow reopening. TODO: implement this
529        self.reopen(bvopts, {}, "Block format 'blkverify' used by node 'bv'" +
530                    " does not support reopening files")
531
532        # Illegal: hd0 is a child of the blkverify node
533        self.reopen(opts[0], {'backing': 'bv'},
534                    "Making 'bv' a backing child of 'hd0' would create a cycle")
535
536        # Delete the blkverify node
537        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'bv')
538        self.assert_qmp(result, 'return', {})
539
540    # Replace the protocol layer ('file' parameter) of a disk image
541    def test_replace_file(self):
542        # Create two small raw images and add them to a running VM
543        qemu_img('create', '-f', 'raw', hd_path[0], '10k')
544        qemu_img('create', '-f', 'raw', hd_path[1], '10k')
545
546        hd0_opts = {'driver': 'file', 'node-name': 'hd0-file', 'filename':  hd_path[0] }
547        hd1_opts = {'driver': 'file', 'node-name': 'hd1-file', 'filename':  hd_path[1] }
548
549        result = self.vm.qmp('blockdev-add', conv_keys = False, **hd0_opts)
550        self.assert_qmp(result, 'return', {})
551        result = self.vm.qmp('blockdev-add', conv_keys = False, **hd1_opts)
552        self.assert_qmp(result, 'return', {})
553
554        # Add a raw format layer that uses hd0-file as its protocol layer
555        opts = {'driver': 'raw', 'node-name': 'hd', 'file': 'hd0-file'}
556
557        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
558        self.assert_qmp(result, 'return', {})
559
560        # Fill the image with data
561        self.run_qemu_io("hd", "read  -P 0 0 10k")
562        self.run_qemu_io("hd", "write -P 0xa0 0 10k")
563
564        # Replace hd0-file with hd1-file and fill it with (different) data
565        self.reopen(opts, {'file': 'hd1-file'})
566        self.run_qemu_io("hd", "read  -P 0 0 10k")
567        self.run_qemu_io("hd", "write -P 0xa1 0 10k")
568
569        # Use hd0-file again and check that it contains the expected data
570        self.reopen(opts, {'file': 'hd0-file'})
571        self.run_qemu_io("hd", "read  -P 0xa0 0 10k")
572
573        # And finally do the same with hd1-file
574        self.reopen(opts, {'file': 'hd1-file'})
575        self.run_qemu_io("hd", "read  -P 0xa1 0 10k")
576
577    # Insert (and remove) a throttle filter
578    def test_insert_throttle_filter(self):
579        # Add an image to the VM
580        hd0_opts = hd_opts(0)
581        result = self.vm.qmp('blockdev-add', conv_keys = False, **hd0_opts)
582        self.assert_qmp(result, 'return', {})
583
584        # Create a throttle-group object
585        opts = { 'qom-type': 'throttle-group', 'id': 'group0',
586                 'limits': { 'iops-total': 1000 } }
587        result = self.vm.qmp('object-add', conv_keys = False, **opts)
588        self.assert_qmp(result, 'return', {})
589
590        # Add a throttle filter with the group that we just created.
591        # The filter is not used by anyone yet
592        opts = { 'driver': 'throttle', 'node-name': 'throttle0',
593                 'throttle-group': 'group0', 'file': 'hd0-file' }
594        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
595        self.assert_qmp(result, 'return', {})
596
597        # Insert the throttle filter between hd0 and hd0-file
598        self.reopen(hd0_opts, {'file': 'throttle0'})
599
600        # Remove the throttle filter from hd0
601        self.reopen(hd0_opts, {'file': 'hd0-file'})
602
603    # Insert (and remove) a compress filter
604    def test_insert_compress_filter(self):
605        # Add an image to the VM: hd (raw) -> hd0 (qcow2) -> hd0-file (file)
606        opts = {'driver': 'raw', 'node-name': 'hd', 'file': hd_opts(0)}
607        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
608        self.assert_qmp(result, 'return', {})
609
610        # Add a 'compress' filter
611        filter_opts = {'driver': 'compress',
612                       'node-name': 'compress0',
613                       'file': 'hd0'}
614        result = self.vm.qmp('blockdev-add', conv_keys = False, **filter_opts)
615        self.assert_qmp(result, 'return', {})
616
617        # Unmap the beginning of the image (we cannot write compressed
618        # data to an allocated cluster)
619        self.run_qemu_io("hd", "write -z -u 0 128k")
620
621        # Write data to the first cluster
622        self.run_qemu_io("hd", "write -P 0xa0 0 64k")
623
624        # Insert the filter then write to the second cluster
625        # hd -> compress0 -> hd0 -> hd0-file
626        self.reopen(opts, {'file': 'compress0'})
627        self.run_qemu_io("hd", "write -P 0xa1 64k 64k")
628
629        # Remove the filter then write to the third cluster
630        # hd -> hd0 -> hd0-file
631        self.reopen(opts, {'file': 'hd0'})
632        self.run_qemu_io("hd", "write -P 0xa2 128k 64k")
633
634        # Verify the data that we just wrote
635        self.run_qemu_io("hd", "read -P 0xa0    0 64k")
636        self.run_qemu_io("hd", "read -P 0xa1  64k 64k")
637        self.run_qemu_io("hd", "read -P 0xa2 128k 64k")
638
639        self.vm.shutdown()
640
641        # Check the first byte of the first three L2 entries and verify that
642        # the second one is compressed (0x40) while the others are not (0x80)
643        iotests.qemu_io_log('-f', 'raw', '-c', 'read -P 0x80 0x40000 1',
644                                         '-c', 'read -P 0x40 0x40008 1',
645                                         '-c', 'read -P 0x80 0x40010 1', hd_path[0])
646
647    # Misc reopen tests with different block drivers
648    @iotests.skip_if_unsupported(['quorum', 'throttle'])
649    def test_misc_drivers(self):
650        ####################
651        ###### quorum ######
652        ####################
653        for i in range(3):
654            opts = hd_opts(i)
655            # Open all three images without backing file
656            opts['backing'] = None
657            result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
658            self.assert_qmp(result, 'return', {})
659
660        opts = {'driver': 'quorum',
661                'node-name': 'quorum0',
662                'children': ['hd0', 'hd1', 'hd2'],
663                'vote-threshold': 2}
664        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
665        self.assert_qmp(result, 'return', {})
666
667        # Quorum doesn't currently allow reopening. TODO: implement this
668        self.reopen(opts, {}, "Block format 'quorum' used by node 'quorum0'" +
669                    " does not support reopening files")
670
671        # You can't make quorum0 a backing file of hd0:
672        # hd0 is already a child of quorum0.
673        self.reopen(hd_opts(0), {'backing': 'quorum0'},
674                    "Making 'quorum0' a backing child of 'hd0' would create a cycle")
675
676        # Delete quorum0
677        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'quorum0')
678        self.assert_qmp(result, 'return', {})
679
680        # Delete hd0, hd1 and hd2
681        for i in range(3):
682            result = self.vm.qmp('blockdev-del', conv_keys = True,
683                                 node_name = 'hd%d' % i)
684            self.assert_qmp(result, 'return', {})
685
686        ######################
687        ###### blkdebug ######
688        ######################
689        opts = {'driver': 'blkdebug',
690                'node-name': 'bd',
691                'config': '/dev/null',
692                'image': hd_opts(0)}
693        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
694        self.assert_qmp(result, 'return', {})
695
696        # blkdebug allows reopening if we keep the same options
697        self.reopen(opts)
698
699        # but it currently does not allow changes
700        self.reopen(opts, {'image': 'hd1'}, "Cannot change the option 'image'")
701        self.reopen(opts, {'align': 33554432}, "Cannot change the option 'align'")
702        self.reopen(opts, {'config': '/non/existent'}, "Cannot change the option 'config'")
703        del opts['config']
704        self.reopen(opts, {}, "Option 'config' cannot be reset to its default value")
705
706        # Delete the blkdebug node
707        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'bd')
708        self.assert_qmp(result, 'return', {})
709
710        ##################
711        ###### null ######
712        ##################
713        opts = {'driver': 'null-co', 'node-name': 'root', 'size': 1024}
714
715        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
716        self.assert_qmp(result, 'return', {})
717
718        # 1 << 30 is the default value, but we cannot change it explicitly
719        self.reopen(opts, {'size': (1 << 30)}, "Cannot change the option 'size'")
720
721        # We cannot change 'size' back to its default value either
722        del opts['size']
723        self.reopen(opts, {}, "Option 'size' cannot be reset to its default value")
724
725        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'root')
726        self.assert_qmp(result, 'return', {})
727
728        ##################
729        ###### file ######
730        ##################
731        opts = hd_opts(0)
732        opts['file']['locking'] = 'on'
733        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
734        self.assert_qmp(result, 'return', {})
735
736        # 'locking' cannot be changed
737        del opts['file']['locking']
738        self.reopen(opts, {'file.locking': 'on'})
739        self.reopen(opts, {'file.locking': 'off'}, "Cannot change the option 'locking'")
740        self.reopen(opts, {}, "Option 'locking' cannot be reset to its default value")
741
742        # Trying to reopen the 'file' node directly does not make a difference
743        opts = opts['file']
744        self.reopen(opts, {'locking': 'on'})
745        self.reopen(opts, {'locking': 'off'}, "Cannot change the option 'locking'")
746        self.reopen(opts, {}, "Option 'locking' cannot be reset to its default value")
747
748        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
749        self.assert_qmp(result, 'return', {})
750
751        ######################
752        ###### throttle ######
753        ######################
754        opts = { 'qom-type': 'throttle-group', 'id': 'group0',
755                 'limits': { 'iops-total': 1000 } }
756        result = self.vm.qmp('object-add', conv_keys = False, **opts)
757        self.assert_qmp(result, 'return', {})
758
759        opts = { 'qom-type': 'throttle-group', 'id': 'group1',
760                 'limits': { 'iops-total': 2000 } }
761        result = self.vm.qmp('object-add', conv_keys = False, **opts)
762        self.assert_qmp(result, 'return', {})
763
764        # Add a throttle filter with group = group0
765        opts = { 'driver': 'throttle', 'node-name': 'throttle0',
766                 'throttle-group': 'group0', 'file': hd_opts(0) }
767        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
768        self.assert_qmp(result, 'return', {})
769
770        # We can reopen it if we keep the same options
771        self.reopen(opts)
772
773        # We can also reopen if 'file' is a reference to the child
774        self.reopen(opts, {'file': 'hd0'})
775
776        # This is illegal
777        self.reopen(opts, {'throttle-group': 'notfound'}, "Throttle group 'notfound' does not exist")
778
779        # But it's possible to change the group to group1
780        self.reopen(opts, {'throttle-group': 'group1'})
781
782        # Now group1 is in use, it cannot be deleted
783        result = self.vm.qmp('object-del', id = 'group1')
784        self.assert_qmp(result, 'error/class', 'GenericError')
785        self.assert_qmp(result, 'error/desc', "object 'group1' is in use, can not be deleted")
786
787        # Default options, this switches the group back to group0
788        self.reopen(opts)
789
790        # So now we cannot delete group0
791        result = self.vm.qmp('object-del', id = 'group0')
792        self.assert_qmp(result, 'error/class', 'GenericError')
793        self.assert_qmp(result, 'error/desc', "object 'group0' is in use, can not be deleted")
794
795        # But group1 is free this time, and it can be deleted
796        result = self.vm.qmp('object-del', id = 'group1')
797        self.assert_qmp(result, 'return', {})
798
799        # Let's delete the filter node
800        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'throttle0')
801        self.assert_qmp(result, 'return', {})
802
803        # And we can finally get rid of group0
804        result = self.vm.qmp('object-del', id = 'group0')
805        self.assert_qmp(result, 'return', {})
806
807    # If an image has a backing file then the 'backing' option must be
808    # passed on reopen. We don't allow leaving the option out in this
809    # case because it's unclear what the correct semantics would be.
810    def test_missing_backing_options_1(self):
811        # hd2
812        opts = hd_opts(2)
813        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
814        self.assert_qmp(result, 'return', {})
815
816        # hd0
817        opts = hd_opts(0)
818        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
819        self.assert_qmp(result, 'return', {})
820
821        # hd0 has no backing file: we can omit the 'backing' option
822        self.reopen(opts)
823
824        # hd2 <- hd0
825        self.reopen(opts, {'backing': 'hd2'})
826
827        # hd0 has a backing file: we must set the 'backing' option
828        self.reopen(opts, {}, "backing is missing for 'hd0'")
829
830        # hd2 can't be removed because it's the backing file of hd0
831        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd2')
832        self.assert_qmp(result, 'error/class', 'GenericError')
833        self.assert_qmp(result, 'error/desc', "Node 'hd2' is busy: node is used as backing hd of 'hd0'")
834
835        # Detach hd2 from hd0.
836        self.reopen(opts, {'backing': None})
837
838        # Without a backing file, we can omit 'backing' again
839        self.reopen(opts)
840
841        # Remove both hd0 and hd2
842        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
843        self.assert_qmp(result, 'return', {})
844
845        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd2')
846        self.assert_qmp(result, 'return', {})
847
848    # If an image has default backing file (as part of its metadata)
849    # then the 'backing' option must be passed on reopen. We don't
850    # allow leaving the option out in this case because it's unclear
851    # what the correct semantics would be.
852    def test_missing_backing_options_2(self):
853        # hd0 <- hd1
854        # (hd0 is hd1's default backing file)
855        opts = hd_opts(1)
856        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
857        self.assert_qmp(result, 'return', {})
858
859        # hd1 has a backing file: we can't omit the 'backing' option
860        self.reopen(opts, {}, "backing is missing for 'hd1'")
861
862        # Let's detach the backing file
863        self.reopen(opts, {'backing': None})
864
865        # No backing file attached to hd1 now, but we still can't omit the 'backing' option
866        self.reopen(opts, {}, "backing is missing for 'hd1'")
867
868        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd1')
869        self.assert_qmp(result, 'return', {})
870
871    # Test that making 'backing' a reference to an existing child
872    # keeps its current options
873    def test_backing_reference(self):
874        # hd2 <- hd1 <- hd0
875        opts = hd_opts(0)
876        opts['backing'] = hd_opts(1)
877        opts['backing']['backing'] = hd_opts(2)
878        # Enable 'detect-zeroes' on all three nodes
879        opts['detect-zeroes'] = 'on'
880        opts['backing']['detect-zeroes'] = 'on'
881        opts['backing']['backing']['detect-zeroes'] = 'on'
882        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
883        self.assert_qmp(result, 'return', {})
884
885        # Reopen the chain passing the minimum amount of required options.
886        # By making 'backing' a reference to hd1 (instead of a sub-dict)
887        # we tell QEMU to keep its current set of options.
888        opts = {'driver': iotests.imgfmt,
889                'node-name': 'hd0',
890                'file': 'hd0-file',
891                'backing': 'hd1' }
892        self.reopen(opts)
893
894        # This has reset 'detect-zeroes' on hd0, but not on hd1 and hd2.
895        self.assert_qmp(self.get_node('hd0'), 'detect_zeroes', 'off')
896        self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'on')
897        self.assert_qmp(self.get_node('hd2'), 'detect_zeroes', 'on')
898
899    # Test what happens if the graph changes due to other operations
900    # such as block-stream
901    def test_block_stream_1(self):
902        # hd1 <- hd0
903        opts = hd_opts(0)
904        opts['backing'] = hd_opts(1)
905        opts['backing']['backing'] = None
906        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
907        self.assert_qmp(result, 'return', {})
908
909        # Stream hd1 into hd0 and wait until it's done
910        result = self.vm.qmp('block-stream', conv_keys = True, job_id = 'stream0', device = 'hd0')
911        self.assert_qmp(result, 'return', {})
912        self.wait_until_completed(drive = 'stream0')
913
914        # Now we have only hd0
915        self.assertEqual(self.get_node('hd1'), None)
916
917        # We have backing.* options but there's no backing file anymore
918        self.reopen(opts, {}, "Cannot change the option 'backing.driver'")
919
920        # If we remove the 'backing' option then we can reopen hd0 just fine
921        del opts['backing']
922        self.reopen(opts)
923
924        # We can also reopen hd0 if we set 'backing' to null
925        self.reopen(opts, {'backing': None})
926
927        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
928        self.assert_qmp(result, 'return', {})
929
930    # Another block_stream test
931    def test_block_stream_2(self):
932        # hd2 <- hd1 <- hd0
933        opts = hd_opts(0)
934        opts['backing'] = hd_opts(1)
935        opts['backing']['backing'] = hd_opts(2)
936        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
937        self.assert_qmp(result, 'return', {})
938
939        # Stream hd1 into hd0 and wait until it's done
940        result = self.vm.qmp('block-stream', conv_keys = True, job_id = 'stream0',
941                             device = 'hd0', base_node = 'hd2')
942        self.assert_qmp(result, 'return', {})
943        self.wait_until_completed(drive = 'stream0')
944
945        # The chain is hd2 <- hd0 now. hd1 is missing
946        self.assertEqual(self.get_node('hd1'), None)
947
948        # The backing options in the dict were meant for hd1, but we cannot
949        # use them with hd2 because hd1 had a backing file while hd2 does not.
950        self.reopen(opts, {}, "Cannot change the option 'backing.driver'")
951
952        # If we remove hd1's options from the dict then things work fine
953        opts['backing'] = opts['backing']['backing']
954        self.reopen(opts)
955
956        # We can also reopen hd0 if we use a reference to the backing file
957        self.reopen(opts, {'backing': 'hd2'})
958
959        # But we cannot leave the option out
960        del opts['backing']
961        self.reopen(opts, {}, "backing is missing for 'hd0'")
962
963        # Now we can delete hd0 (and hd2)
964        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
965        self.assert_qmp(result, 'return', {})
966        self.assertEqual(self.get_node('hd2'), None)
967
968    # Reopen the chain during a block-stream job (from hd1 to hd0)
969    def test_block_stream_3(self):
970        # hd2 <- hd1 <- hd0
971        opts = hd_opts(0)
972        opts['backing'] = hd_opts(1)
973        opts['backing']['backing'] = hd_opts(2)
974        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
975        self.assert_qmp(result, 'return', {})
976
977        # hd2 <- hd0
978        result = self.vm.qmp('block-stream', conv_keys = True, job_id = 'stream0',
979                             device = 'hd0', base_node = 'hd2',
980                             auto_finalize = False)
981        self.assert_qmp(result, 'return', {})
982
983        # We can remove hd2 while the stream job is ongoing
984        opts['backing']['backing'] = None
985        self.reopen(opts, {})
986
987        # We can't remove hd1 while the stream job is ongoing
988        opts['backing'] = None
989        self.reopen(opts, {}, "Cannot change frozen 'backing' link from 'hd0' to 'hd1'")
990
991        self.vm.run_job('stream0', auto_finalize = False, auto_dismiss = True)
992
993    # Reopen the chain during a block-stream job (from hd2 to hd1)
994    def test_block_stream_4(self):
995        # hd2 <- hd1 <- hd0
996        opts = hd_opts(0)
997        opts['backing'] = hd_opts(1)
998        opts['backing']['backing'] = hd_opts(2)
999        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
1000        self.assert_qmp(result, 'return', {})
1001
1002        # hd1 <- hd0
1003        result = self.vm.qmp('block-stream', conv_keys = True, job_id = 'stream0',
1004                             device = 'hd1', filter_node_name='cor',
1005                             auto_finalize = False)
1006        self.assert_qmp(result, 'return', {})
1007
1008        # We can't reopen with the original options because there is a filter
1009        # inserted by stream job above hd1.
1010        self.reopen(opts, {},
1011                    "Cannot change the option 'backing.backing.file.node-name'")
1012
1013        # We can't reopen hd1 to read-only, as block-stream requires it to be
1014        # read-write
1015        self.reopen(opts['backing'], {'read-only': True},
1016                    "Read-only block node 'hd1' cannot support read-write users")
1017
1018        # We can't remove hd2 while the stream job is ongoing
1019        opts['backing']['backing'] = None
1020        self.reopen(opts['backing'], {'read-only': False},
1021                    "Cannot change frozen 'backing' link from 'hd1' to 'hd2'")
1022
1023        # We can detach hd1 from hd0 because it doesn't affect the stream job
1024        opts['backing'] = None
1025        self.reopen(opts)
1026
1027        self.vm.run_job('stream0', auto_finalize = False, auto_dismiss = True)
1028
1029    # Reopen the chain during a block-commit job (from hd0 to hd2)
1030    def test_block_commit_1(self):
1031        # hd2 <- hd1 <- hd0
1032        opts = hd_opts(0)
1033        opts['backing'] = hd_opts(1)
1034        opts['backing']['backing'] = hd_opts(2)
1035        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
1036        self.assert_qmp(result, 'return', {})
1037
1038        result = self.vm.qmp('block-commit', conv_keys = True, job_id = 'commit0',
1039                             device = 'hd0')
1040        self.assert_qmp(result, 'return', {})
1041
1042        # We can't remove hd2 while the commit job is ongoing
1043        opts['backing']['backing'] = None
1044        self.reopen(opts, {}, "Cannot change frozen 'backing' link from 'hd1' to 'hd2'")
1045
1046        # We can't remove hd1 while the commit job is ongoing
1047        opts['backing'] = None
1048        self.reopen(opts, {}, "Cannot change frozen 'backing' link from 'hd0' to 'hd1'")
1049
1050        event = self.vm.event_wait(name='BLOCK_JOB_READY')
1051        self.assert_qmp(event, 'data/device', 'commit0')
1052        self.assert_qmp(event, 'data/type', 'commit')
1053        self.assert_qmp_absent(event, 'data/error')
1054
1055        result = self.vm.qmp('block-job-complete', device='commit0')
1056        self.assert_qmp(result, 'return', {})
1057
1058        self.wait_until_completed(drive = 'commit0')
1059
1060    # Reopen the chain during a block-commit job (from hd1 to hd2)
1061    def test_block_commit_2(self):
1062        # hd2 <- hd1 <- hd0
1063        opts = hd_opts(0)
1064        opts['backing'] = hd_opts(1)
1065        opts['backing']['backing'] = hd_opts(2)
1066        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
1067        self.assert_qmp(result, 'return', {})
1068
1069        result = self.vm.qmp('block-commit', conv_keys = True, job_id = 'commit0',
1070                             device = 'hd0', top_node = 'hd1',
1071                             auto_finalize = False)
1072        self.assert_qmp(result, 'return', {})
1073
1074        # We can't remove hd2 while the commit job is ongoing
1075        opts['backing']['backing'] = None
1076        self.reopen(opts, {}, "Cannot change the option 'backing.driver'")
1077
1078        # We can't remove hd1 while the commit job is ongoing
1079        opts['backing'] = None
1080        self.reopen(opts, {}, "Cannot replace implicit backing child of hd0")
1081
1082        # hd2 <- hd0
1083        self.vm.run_job('commit0', auto_finalize = False, auto_dismiss = True)
1084
1085        self.assert_qmp(self.get_node('hd0'), 'ro', False)
1086        self.assertEqual(self.get_node('hd1'), None)
1087        self.assert_qmp(self.get_node('hd2'), 'ro', True)
1088
1089    def run_test_iothreads(self, iothread_a, iothread_b, errmsg = None):
1090        opts = hd_opts(0)
1091        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
1092        self.assert_qmp(result, 'return', {})
1093
1094        opts2 = hd_opts(2)
1095        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts2)
1096        self.assert_qmp(result, 'return', {})
1097
1098        result = self.vm.qmp('object-add', qom_type='iothread', id='iothread0')
1099        self.assert_qmp(result, 'return', {})
1100
1101        result = self.vm.qmp('object-add', qom_type='iothread', id='iothread1')
1102        self.assert_qmp(result, 'return', {})
1103
1104        result = self.vm.qmp('device_add', driver='virtio-scsi', id='scsi0',
1105                             iothread=iothread_a)
1106        self.assert_qmp(result, 'return', {})
1107
1108        result = self.vm.qmp('device_add', driver='virtio-scsi', id='scsi1',
1109                             iothread=iothread_b)
1110        self.assert_qmp(result, 'return', {})
1111
1112        if iothread_a:
1113            result = self.vm.qmp('device_add', driver='scsi-hd', drive='hd0',
1114                                 share_rw=True, bus="scsi0.0")
1115            self.assert_qmp(result, 'return', {})
1116
1117        if iothread_b:
1118            result = self.vm.qmp('device_add', driver='scsi-hd', drive='hd2',
1119                                 share_rw=True, bus="scsi1.0")
1120            self.assert_qmp(result, 'return', {})
1121
1122        # Attaching the backing file may or may not work
1123        self.reopen(opts, {'backing': 'hd2'}, errmsg)
1124
1125        # But removing the backing file should always work
1126        self.reopen(opts, {'backing': None})
1127
1128        self.vm.shutdown()
1129
1130    # We don't allow setting a backing file that uses a different AioContext if
1131    # neither of them can switch to the other AioContext
1132    def test_iothreads_error(self):
1133        self.run_test_iothreads('iothread0', 'iothread1',
1134                                "Cannot change iothread of active block backend")
1135
1136    def test_iothreads_compatible_users(self):
1137        self.run_test_iothreads('iothread0', 'iothread0')
1138
1139    def test_iothreads_switch_backing(self):
1140        self.run_test_iothreads('iothread0', None)
1141
1142    def test_iothreads_switch_overlay(self):
1143        self.run_test_iothreads(None, 'iothread0')
1144
1145if __name__ == '__main__':
1146    iotests.activate_logging()
1147    iotests.main(supported_fmts=["qcow2"],
1148                 supported_protocols=["file"])
1149