xref: /openbmc/qemu/tests/qemu-iotests/245 (revision 64ed6f92)
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        self.reopen(opts, {}, "backing is missing for 'hd0'")
729
730        # Remove both hd0 and hd2
731        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
732        self.assert_qmp(result, 'return', {})
733
734        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd2')
735        self.assert_qmp(result, 'return', {})
736
737    # If an image has default backing file (as part of its metadata)
738    # then the 'backing' option must be passed on reopen. We don't
739    # allow leaving the option out in this case because it's unclear
740    # what the correct semantics would be.
741    def test_missing_backing_options_2(self):
742        # hd0 <- hd1
743        # (hd0 is hd1's default backing file)
744        opts = hd_opts(1)
745        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
746        self.assert_qmp(result, 'return', {})
747
748        # hd1 has a backing file: we can't omit the 'backing' option
749        self.reopen(opts, {}, "backing is missing for 'hd1'")
750
751        # Let's detach the backing file
752        self.reopen(opts, {'backing': None})
753
754        # No backing file attached to hd1 now, but we still can't omit the 'backing' option
755        self.reopen(opts, {}, "backing is missing for 'hd1'")
756
757        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd1')
758        self.assert_qmp(result, 'return', {})
759
760    # Test that making 'backing' a reference to an existing child
761    # keeps its current options
762    def test_backing_reference(self):
763        # hd2 <- hd1 <- hd0
764        opts = hd_opts(0)
765        opts['backing'] = hd_opts(1)
766        opts['backing']['backing'] = hd_opts(2)
767        # Enable 'detect-zeroes' on all three nodes
768        opts['detect-zeroes'] = 'on'
769        opts['backing']['detect-zeroes'] = 'on'
770        opts['backing']['backing']['detect-zeroes'] = 'on'
771        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
772        self.assert_qmp(result, 'return', {})
773
774        # Reopen the chain passing the minimum amount of required options.
775        # By making 'backing' a reference to hd1 (instead of a sub-dict)
776        # we tell QEMU to keep its current set of options.
777        opts = {'driver': iotests.imgfmt,
778                'node-name': 'hd0',
779                'file': 'hd0-file',
780                'backing': 'hd1' }
781        self.reopen(opts)
782
783        # This has reset 'detect-zeroes' on hd0, but not on hd1 and hd2.
784        self.assert_qmp(self.get_node('hd0'), 'detect_zeroes', 'off')
785        self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'on')
786        self.assert_qmp(self.get_node('hd2'), 'detect_zeroes', 'on')
787
788    # Test what happens if the graph changes due to other operations
789    # such as block-stream
790    def test_block_stream_1(self):
791        # hd1 <- hd0
792        opts = hd_opts(0)
793        opts['backing'] = hd_opts(1)
794        opts['backing']['backing'] = None
795        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
796        self.assert_qmp(result, 'return', {})
797
798        # Stream hd1 into hd0 and wait until it's done
799        result = self.vm.qmp('block-stream', conv_keys = True, job_id = 'stream0', device = 'hd0')
800        self.assert_qmp(result, 'return', {})
801        self.wait_until_completed(drive = 'stream0')
802
803        # Now we have only hd0
804        self.assertEqual(self.get_node('hd1'), None)
805
806        # We have backing.* options but there's no backing file anymore
807        self.reopen(opts, {}, "Cannot change the option 'backing.driver'")
808
809        # If we remove the 'backing' option then we can reopen hd0 just fine
810        del opts['backing']
811        self.reopen(opts)
812
813        # We can also reopen hd0 if we set 'backing' to null
814        self.reopen(opts, {'backing': None})
815
816        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
817        self.assert_qmp(result, 'return', {})
818
819    # Another block_stream test
820    def test_block_stream_2(self):
821        # hd2 <- hd1 <- hd0
822        opts = hd_opts(0)
823        opts['backing'] = hd_opts(1)
824        opts['backing']['backing'] = hd_opts(2)
825        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
826        self.assert_qmp(result, 'return', {})
827
828        # Stream hd1 into hd0 and wait until it's done
829        result = self.vm.qmp('block-stream', conv_keys = True, job_id = 'stream0',
830                             device = 'hd0', base_node = 'hd2')
831        self.assert_qmp(result, 'return', {})
832        self.wait_until_completed(drive = 'stream0')
833
834        # The chain is hd2 <- hd0 now. hd1 is missing
835        self.assertEqual(self.get_node('hd1'), None)
836
837        # The backing options in the dict were meant for hd1, but we cannot
838        # use them with hd2 because hd1 had a backing file while hd2 does not.
839        self.reopen(opts, {}, "Cannot change the option 'backing.driver'")
840
841        # If we remove hd1's options from the dict then things work fine
842        opts['backing'] = opts['backing']['backing']
843        self.reopen(opts)
844
845        # We can also reopen hd0 if we use a reference to the backing file
846        self.reopen(opts, {'backing': 'hd2'})
847
848        # But we cannot leave the option out
849        del opts['backing']
850        self.reopen(opts, {}, "backing is missing for 'hd0'")
851
852        # Now we can delete hd0 (and hd2)
853        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
854        self.assert_qmp(result, 'return', {})
855        self.assertEqual(self.get_node('hd2'), None)
856
857    # Reopen the chain during a block-stream job (from hd1 to hd0)
858    def test_block_stream_3(self):
859        # hd2 <- hd1 <- hd0
860        opts = hd_opts(0)
861        opts['backing'] = hd_opts(1)
862        opts['backing']['backing'] = hd_opts(2)
863        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
864        self.assert_qmp(result, 'return', {})
865
866        # hd2 <- hd0
867        result = self.vm.qmp('block-stream', conv_keys = True, job_id = 'stream0',
868                             device = 'hd0', base_node = 'hd2',
869                             auto_finalize = False)
870        self.assert_qmp(result, 'return', {})
871
872        # We can remove hd2 while the stream job is ongoing
873        opts['backing']['backing'] = None
874        self.reopen(opts, {})
875
876        # We can't remove hd1 while the stream job is ongoing
877        opts['backing'] = None
878        self.reopen(opts, {}, "Cannot change 'backing' link from 'hd0' to 'hd1'")
879
880        self.vm.run_job('stream0', auto_finalize = False, auto_dismiss = True)
881
882    # Reopen the chain during a block-stream job (from hd2 to hd1)
883    def test_block_stream_4(self):
884        # hd2 <- hd1 <- hd0
885        opts = hd_opts(0)
886        opts['backing'] = hd_opts(1)
887        opts['backing']['backing'] = hd_opts(2)
888        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
889        self.assert_qmp(result, 'return', {})
890
891        # hd1 <- hd0
892        result = self.vm.qmp('block-stream', conv_keys = True, job_id = 'stream0',
893                             device = 'hd1', auto_finalize = False)
894        self.assert_qmp(result, 'return', {})
895
896        # We can't reopen with the original options because that would
897        # make hd1 read-only and block-stream requires it to be read-write
898        # (Which error message appears depends on whether the stream job is
899        # already done with copying at this point.)
900        self.reopen(opts, {},
901            ["Can't set node 'hd1' to r/o with copy-on-read enabled",
902             "Cannot make block node read-only, there is a writer on it"])
903
904        # We can't remove hd2 while the stream job is ongoing
905        opts['backing']['backing'] = None
906        self.reopen(opts, {'backing.read-only': False}, "Cannot change 'backing' link from 'hd1' to 'hd2'")
907
908        # We can detach hd1 from hd0 because it doesn't affect the stream job
909        opts['backing'] = None
910        self.reopen(opts)
911
912        self.vm.run_job('stream0', auto_finalize = False, auto_dismiss = True)
913
914    # Reopen the chain during a block-commit job (from hd0 to hd2)
915    def test_block_commit_1(self):
916        # hd2 <- hd1 <- hd0
917        opts = hd_opts(0)
918        opts['backing'] = hd_opts(1)
919        opts['backing']['backing'] = hd_opts(2)
920        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
921        self.assert_qmp(result, 'return', {})
922
923        result = self.vm.qmp('block-commit', conv_keys = True, job_id = 'commit0',
924                             device = 'hd0')
925        self.assert_qmp(result, 'return', {})
926
927        # We can't remove hd2 while the commit job is ongoing
928        opts['backing']['backing'] = None
929        self.reopen(opts, {}, "Cannot change 'backing' link from 'hd1' to 'hd2'")
930
931        # We can't remove hd1 while the commit job is ongoing
932        opts['backing'] = None
933        self.reopen(opts, {}, "Cannot change 'backing' link from 'hd0' to 'hd1'")
934
935        event = self.vm.event_wait(name='BLOCK_JOB_READY')
936        self.assert_qmp(event, 'data/device', 'commit0')
937        self.assert_qmp(event, 'data/type', 'commit')
938        self.assert_qmp_absent(event, 'data/error')
939
940        result = self.vm.qmp('block-job-complete', device='commit0')
941        self.assert_qmp(result, 'return', {})
942
943        self.wait_until_completed(drive = 'commit0')
944
945    # Reopen the chain during a block-commit job (from hd1 to hd2)
946    def test_block_commit_2(self):
947        # hd2 <- hd1 <- hd0
948        opts = hd_opts(0)
949        opts['backing'] = hd_opts(1)
950        opts['backing']['backing'] = hd_opts(2)
951        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
952        self.assert_qmp(result, 'return', {})
953
954        result = self.vm.qmp('block-commit', conv_keys = True, job_id = 'commit0',
955                             device = 'hd0', top_node = 'hd1',
956                             auto_finalize = False)
957        self.assert_qmp(result, 'return', {})
958
959        # We can't remove hd2 while the commit job is ongoing
960        opts['backing']['backing'] = None
961        self.reopen(opts, {}, "Cannot change the option 'backing.driver'")
962
963        # We can't remove hd1 while the commit job is ongoing
964        opts['backing'] = None
965        self.reopen(opts, {}, "Cannot change backing link if 'hd0' has an implicit backing file")
966
967        # hd2 <- hd0
968        self.vm.run_job('commit0', auto_finalize = False, auto_dismiss = True)
969
970        self.assert_qmp(self.get_node('hd0'), 'ro', False)
971        self.assertEqual(self.get_node('hd1'), None)
972        self.assert_qmp(self.get_node('hd2'), 'ro', True)
973
974    def run_test_iothreads(self, iothread_a, iothread_b, errmsg = None):
975        opts = hd_opts(0)
976        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
977        self.assert_qmp(result, 'return', {})
978
979        opts2 = hd_opts(2)
980        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts2)
981        self.assert_qmp(result, 'return', {})
982
983        result = self.vm.qmp('object-add', qom_type='iothread', id='iothread0')
984        self.assert_qmp(result, 'return', {})
985
986        result = self.vm.qmp('object-add', qom_type='iothread', id='iothread1')
987        self.assert_qmp(result, 'return', {})
988
989        result = self.vm.qmp('device_add', driver='virtio-scsi', id='scsi0',
990                             iothread=iothread_a)
991        self.assert_qmp(result, 'return', {})
992
993        result = self.vm.qmp('device_add', driver='virtio-scsi', id='scsi1',
994                             iothread=iothread_b)
995        self.assert_qmp(result, 'return', {})
996
997        if iothread_a:
998            result = self.vm.qmp('device_add', driver='scsi-hd', drive='hd0',
999                                 share_rw=True, bus="scsi0.0")
1000            self.assert_qmp(result, 'return', {})
1001
1002        if iothread_b:
1003            result = self.vm.qmp('device_add', driver='scsi-hd', drive='hd2',
1004                                 share_rw=True, bus="scsi1.0")
1005            self.assert_qmp(result, 'return', {})
1006
1007        # Attaching the backing file may or may not work
1008        self.reopen(opts, {'backing': 'hd2'}, errmsg)
1009
1010        # But removing the backing file should always work
1011        self.reopen(opts, {'backing': None})
1012
1013        self.vm.shutdown()
1014
1015    # We don't allow setting a backing file that uses a different AioContext if
1016    # neither of them can switch to the other AioContext
1017    def test_iothreads_error(self):
1018        self.run_test_iothreads('iothread0', 'iothread1',
1019                                "Cannot change iothread of active block backend")
1020
1021    def test_iothreads_compatible_users(self):
1022        self.run_test_iothreads('iothread0', 'iothread0')
1023
1024    def test_iothreads_switch_backing(self):
1025        self.run_test_iothreads('iothread0', None)
1026
1027    def test_iothreads_switch_overlay(self):
1028        self.run_test_iothreads(None, 'iothread0')
1029
1030if __name__ == '__main__':
1031    iotests.activate_logging()
1032    iotests.main(supported_fmts=["qcow2"],
1033                 supported_protocols=["file"])
1034