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