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