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