es1370: check total frame count against current frame
[qemu/ar7.git] / tests / qemu-iotests / 245
blob4f5f0bb9010446b11f25441c1cf32ef63bf8e411
1 #!/usr/bin/env python3
3 # Test cases for the QMP 'x-blockdev-reopen' command
5 # Copyright (C) 2018-2019 Igalia, S.L.
6 # Author: Alberto Garcia <berto@igalia.com>
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.
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.
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/>.
22 import os
23 import re
24 import iotests
25 import copy
26 import json
27 from iotests import qemu_img, qemu_io
29 hd_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')
35 def 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] } }
42 class TestBlockdevReopen(iotests.QMPTestCase):
43     total_io_cmds = 0
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()
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])
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
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))
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)
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
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', {})
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
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))
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')
134         # We can reopen the image passing the same options
135         self.reopen(opts)
137         # We can also reopen passing a child reference in 'file'
138         self.reopen(opts, {'file': 'hd0-file'})
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")
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")
161         # Check that nothing has changed
162         self.check_node_graph(original_graph)
164         # Remove the node
165         result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
166         self.assert_qmp(result, 'return', {})
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')
178         # We can't reopen the image passing the same options, 'backing' is mandatory
179         self.reopen(opts, {}, "backing is missing for 'hd1'")
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})
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=")
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)
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'")
203         # Check that nothing has changed since the beginning
204         self.check_node_graph(original_graph)
206         # Remove the node
207         result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd1')
208         self.assert_qmp(result, 'return', {})
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
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')
225         # We can reopen the image passing the same options
226         self.reopen(opts)
228         # Reopen in read-only mode
229         self.assert_qmp(self.get_node('hd1'), 'ro', False)
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)
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)
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)
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')
264         # Changing 'force-share' is currently not supported
265         self.reopen(opts, {'force-share': True}, "Cannot change the option 'force-share'")
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})
281         # Check that nothing has changed since the beginning
282         # (from the parameters that we can check)
283         self.check_node_graph(original_graph)
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)
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')
296         # Open the 'hd0' image
297         result = self.vm.qmp('blockdev-add', conv_keys = False, **hd_opts(0))
298         self.assert_qmp(result, 'return', {})
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])
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)
308         # The backing file (hd0) is now a reference, we cannot change backing.* anymore
309         self.reopen(opts, {}, "Cannot change the option 'backing.driver'")
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'")
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', {})
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' } }
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])
335         # Open the raw file with QEMU
336         result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
337         self.assert_qmp(result, 'return', {})
339         # Read 1MB from offset 0
340         self.run_qemu_io("hd0", "read -P 0xa0  0 1M")
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")
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")
352         # Remove the block device
353         result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
354         self.assert_qmp(result, 'return', {})
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' } }
367         # Open the file with QEMU
368         result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
369         self.assert_qmp(result, 'return', {})
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)
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' })
384         # Remove the block device
385         result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
386         self.assert_qmp(result, 'return', {})
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 = []
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', {})
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")
406         # hd1 <- hd0
407         self.reopen(opts[0], {'backing': 'hd1'})
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")
413         # hd1 <- hd0 , hd1 <- hd2
414         self.reopen(opts[2], {'backing': 'hd1'})
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")
420         # hd1 <- hd2 <- hd0
421         self.reopen(opts[0], {'backing': 'hd2'})
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")
427         # hd2 <- hd0
428         self.reopen(opts[2], {'backing': None})
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")
434         # hd2 <- hd1 <- hd0
435         self.reopen(opts[1], {'backing': 'hd2'})
436         self.reopen(opts[0], {'backing': 'hd1'})
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")
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")
446         # hd2 <- hd0, hd2 <- hd1
447         self.reopen(opts[0], {'backing': 'hd2'})
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")
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'")
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'")
462         # hd1 doesn't have a backing file now
463         self.reopen(opts[1], {'backing': None})
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")
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'")
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")
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     @iotests.skip_if_unsupported(['blkverify'])
482     def test_graph_cycles(self):
483         opts = []
485         # Open all three images without backing file
486         for i in range(3):
487             opts.append(hd_opts(i))
488             opts[i]['backing'] = None
489             result = self.vm.qmp('blockdev-add', conv_keys = False, **opts[i])
490             self.assert_qmp(result, 'return', {})
492         # hd1 <- hd0, hd1 <- hd2
493         self.reopen(opts[0], {'backing': 'hd1'})
494         self.reopen(opts[2], {'backing': 'hd1'})
496         # Illegal: hd2 is backed by hd1
497         self.reopen(opts[1], {'backing': 'hd2'},
498                     "Making 'hd2' a backing file of 'hd1' would create a cycle")
500         # hd1 <- hd0 <- hd2
501         self.reopen(opts[2], {'backing': 'hd0'})
503         # Illegal: hd2 is backed by hd0, which is backed by hd1
504         self.reopen(opts[1], {'backing': 'hd2'},
505                     "Making 'hd2' a backing file of 'hd1' would create a cycle")
507         # Illegal: hd1 cannot point to itself
508         self.reopen(opts[1], {'backing': 'hd1'},
509                     "Making 'hd1' a backing file of 'hd1' would create a cycle")
511         # Remove all backing files
512         self.reopen(opts[0])
513         self.reopen(opts[2])
515         ##########################################
516         # Add a blkverify node using hd0 and hd1 #
517         ##########################################
518         bvopts = {'driver': 'blkverify',
519                   'node-name': 'bv',
520                   'test': 'hd0',
521                   'raw': 'hd1'}
522         result = self.vm.qmp('blockdev-add', conv_keys = False, **bvopts)
523         self.assert_qmp(result, 'return', {})
525         # blkverify doesn't currently allow reopening. TODO: implement this
526         self.reopen(bvopts, {}, "Block format 'blkverify' used by node 'bv'" +
527                     " does not support reopening files")
529         # Illegal: hd0 is a child of the blkverify node
530         self.reopen(opts[0], {'backing': 'bv'},
531                     "Making 'bv' a backing file of 'hd0' would create a cycle")
533         # Delete the blkverify node
534         result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'bv')
535         self.assert_qmp(result, 'return', {})
537     # Misc reopen tests with different block drivers
538     @iotests.skip_if_unsupported(['quorum', 'throttle'])
539     def test_misc_drivers(self):
540         ####################
541         ###### quorum ######
542         ####################
543         for i in range(3):
544             opts = hd_opts(i)
545             # Open all three images without backing file
546             opts['backing'] = None
547             result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
548             self.assert_qmp(result, 'return', {})
550         opts = {'driver': 'quorum',
551                 'node-name': 'quorum0',
552                 'children': ['hd0', 'hd1', 'hd2'],
553                 'vote-threshold': 2}
554         result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
555         self.assert_qmp(result, 'return', {})
557         # Quorum doesn't currently allow reopening. TODO: implement this
558         self.reopen(opts, {}, "Block format 'quorum' used by node 'quorum0'" +
559                     " does not support reopening files")
561         # You can't make quorum0 a backing file of hd0:
562         # hd0 is already a child of quorum0.
563         self.reopen(hd_opts(0), {'backing': 'quorum0'},
564                     "Making 'quorum0' a backing file of 'hd0' would create a cycle")
566         # Delete quorum0
567         result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'quorum0')
568         self.assert_qmp(result, 'return', {})
570         # Delete hd0, hd1 and hd2
571         for i in range(3):
572             result = self.vm.qmp('blockdev-del', conv_keys = True,
573                                  node_name = 'hd%d' % i)
574             self.assert_qmp(result, 'return', {})
576         ######################
577         ###### blkdebug ######
578         ######################
579         opts = {'driver': 'blkdebug',
580                 'node-name': 'bd',
581                 'config': '/dev/null',
582                 'image': hd_opts(0)}
583         result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
584         self.assert_qmp(result, 'return', {})
586         # blkdebug allows reopening if we keep the same options
587         self.reopen(opts)
589         # but it currently does not allow changes
590         self.reopen(opts, {'image': 'hd1'}, "Cannot change the option 'image'")
591         self.reopen(opts, {'align': 33554432}, "Cannot change the option 'align'")
592         self.reopen(opts, {'config': '/non/existent'}, "Cannot change the option 'config'")
593         del opts['config']
594         self.reopen(opts, {}, "Option 'config' cannot be reset to its default value")
596         # Delete the blkdebug node
597         result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'bd')
598         self.assert_qmp(result, 'return', {})
600         ##################
601         ###### null ######
602         ##################
603         opts = {'driver': 'null-co', 'node-name': 'root', 'size': 1024}
605         result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
606         self.assert_qmp(result, 'return', {})
608         # 1 << 30 is the default value, but we cannot change it explicitly
609         self.reopen(opts, {'size': (1 << 30)}, "Cannot change the option 'size'")
611         # We cannot change 'size' back to its default value either
612         del opts['size']
613         self.reopen(opts, {}, "Option 'size' cannot be reset to its default value")
615         result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'root')
616         self.assert_qmp(result, 'return', {})
618         ##################
619         ###### file ######
620         ##################
621         opts = hd_opts(0)
622         opts['file']['locking'] = 'on'
623         result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
624         self.assert_qmp(result, 'return', {})
626         # 'locking' cannot be changed
627         del opts['file']['locking']
628         self.reopen(opts, {'file.locking': 'on'})
629         self.reopen(opts, {'file.locking': 'off'}, "Cannot change the option 'locking'")
630         self.reopen(opts, {}, "Option 'locking' cannot be reset to its default value")
632         # Trying to reopen the 'file' node directly does not make a difference
633         opts = opts['file']
634         self.reopen(opts, {'locking': 'on'})
635         self.reopen(opts, {'locking': 'off'}, "Cannot change the option 'locking'")
636         self.reopen(opts, {}, "Option 'locking' cannot be reset to its default value")
638         result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
639         self.assert_qmp(result, 'return', {})
641         ######################
642         ###### throttle ######
643         ######################
644         opts = { 'qom-type': 'throttle-group', 'id': 'group0',
645                  'props': { 'limits': { 'iops-total': 1000 } } }
646         result = self.vm.qmp('object-add', conv_keys = False, **opts)
647         self.assert_qmp(result, 'return', {})
649         opts = { 'qom-type': 'throttle-group', 'id': 'group1',
650                  'props': { 'limits': { 'iops-total': 2000 } } }
651         result = self.vm.qmp('object-add', conv_keys = False, **opts)
652         self.assert_qmp(result, 'return', {})
654         # Add a throttle filter with group = group0
655         opts = { 'driver': 'throttle', 'node-name': 'throttle0',
656                  'throttle-group': 'group0', 'file': hd_opts(0) }
657         result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
658         self.assert_qmp(result, 'return', {})
660         # We can reopen it if we keep the same options
661         self.reopen(opts)
663         # We can also reopen if 'file' is a reference to the child
664         self.reopen(opts, {'file': 'hd0'})
666         # This is illegal
667         self.reopen(opts, {'throttle-group': 'notfound'}, "Throttle group 'notfound' does not exist")
669         # But it's possible to change the group to group1
670         self.reopen(opts, {'throttle-group': 'group1'})
672         # Now group1 is in use, it cannot be deleted
673         result = self.vm.qmp('object-del', id = 'group1')
674         self.assert_qmp(result, 'error/class', 'GenericError')
675         self.assert_qmp(result, 'error/desc', "object 'group1' is in use, can not be deleted")
677         # Default options, this switches the group back to group0
678         self.reopen(opts)
680         # So now we cannot delete group0
681         result = self.vm.qmp('object-del', id = 'group0')
682         self.assert_qmp(result, 'error/class', 'GenericError')
683         self.assert_qmp(result, 'error/desc', "object 'group0' is in use, can not be deleted")
685         # But group1 is free this time, and it can be deleted
686         result = self.vm.qmp('object-del', id = 'group1')
687         self.assert_qmp(result, 'return', {})
689         # Let's delete the filter node
690         result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'throttle0')
691         self.assert_qmp(result, 'return', {})
693         # And we can finally get rid of group0
694         result = self.vm.qmp('object-del', id = 'group0')
695         self.assert_qmp(result, 'return', {})
697     # If an image has a backing file then the 'backing' option must be
698     # passed on reopen. We don't allow leaving the option out in this
699     # case because it's unclear what the correct semantics would be.
700     def test_missing_backing_options_1(self):
701         # hd2
702         opts = hd_opts(2)
703         result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
704         self.assert_qmp(result, 'return', {})
706         # hd0
707         opts = hd_opts(0)
708         result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
709         self.assert_qmp(result, 'return', {})
711         # hd0 has no backing file: we can omit the 'backing' option
712         self.reopen(opts)
714         # hd2 <- hd0
715         self.reopen(opts, {'backing': 'hd2'})
717         # hd0 has a backing file: we must set the 'backing' option
718         self.reopen(opts, {}, "backing is missing for 'hd0'")
720         # hd2 can't be removed because it's the backing file of hd0
721         result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd2')
722         self.assert_qmp(result, 'error/class', 'GenericError')
723         self.assert_qmp(result, 'error/desc', "Node 'hd2' is busy: node is used as backing hd of 'hd0'")
725         # Detach hd2 from hd0.
726         self.reopen(opts, {'backing': None})
727         self.reopen(opts, {}, "backing is missing for 'hd0'")
729         # Remove both hd0 and hd2
730         result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
731         self.assert_qmp(result, 'return', {})
733         result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd2')
734         self.assert_qmp(result, 'return', {})
736     # If an image has default backing file (as part of its metadata)
737     # then the 'backing' option must be passed on reopen. We don't
738     # allow leaving the option out in this case because it's unclear
739     # what the correct semantics would be.
740     def test_missing_backing_options_2(self):
741         # hd0 <- hd1
742         # (hd0 is hd1's default backing file)
743         opts = hd_opts(1)
744         result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
745         self.assert_qmp(result, 'return', {})
747         # hd1 has a backing file: we can't omit the 'backing' option
748         self.reopen(opts, {}, "backing is missing for 'hd1'")
750         # Let's detach the backing file
751         self.reopen(opts, {'backing': None})
753         # No backing file attached to hd1 now, but we still can't omit the 'backing' option
754         self.reopen(opts, {}, "backing is missing for 'hd1'")
756         result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd1')
757         self.assert_qmp(result, 'return', {})
759     # Test that making 'backing' a reference to an existing child
760     # keeps its current options
761     def test_backing_reference(self):
762         # hd2 <- hd1 <- hd0
763         opts = hd_opts(0)
764         opts['backing'] = hd_opts(1)
765         opts['backing']['backing'] = hd_opts(2)
766         # Enable 'detect-zeroes' on all three nodes
767         opts['detect-zeroes'] = 'on'
768         opts['backing']['detect-zeroes'] = 'on'
769         opts['backing']['backing']['detect-zeroes'] = 'on'
770         result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
771         self.assert_qmp(result, 'return', {})
773         # Reopen the chain passing the minimum amount of required options.
774         # By making 'backing' a reference to hd1 (instead of a sub-dict)
775         # we tell QEMU to keep its current set of options.
776         opts = {'driver': iotests.imgfmt,
777                 'node-name': 'hd0',
778                 'file': 'hd0-file',
779                 'backing': 'hd1' }
780         self.reopen(opts)
782         # This has reset 'detect-zeroes' on hd0, but not on hd1 and hd2.
783         self.assert_qmp(self.get_node('hd0'), 'detect_zeroes', 'off')
784         self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'on')
785         self.assert_qmp(self.get_node('hd2'), 'detect_zeroes', 'on')
787     # Test what happens if the graph changes due to other operations
788     # such as block-stream
789     def test_block_stream_1(self):
790         # hd1 <- hd0
791         opts = hd_opts(0)
792         opts['backing'] = hd_opts(1)
793         opts['backing']['backing'] = None
794         result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
795         self.assert_qmp(result, 'return', {})
797         # Stream hd1 into hd0 and wait until it's done
798         result = self.vm.qmp('block-stream', conv_keys = True, job_id = 'stream0', device = 'hd0')
799         self.assert_qmp(result, 'return', {})
800         self.wait_until_completed(drive = 'stream0')
802         # Now we have only hd0
803         self.assertEqual(self.get_node('hd1'), None)
805         # We have backing.* options but there's no backing file anymore
806         self.reopen(opts, {}, "Cannot change the option 'backing.driver'")
808         # If we remove the 'backing' option then we can reopen hd0 just fine
809         del opts['backing']
810         self.reopen(opts)
812         # We can also reopen hd0 if we set 'backing' to null
813         self.reopen(opts, {'backing': None})
815         result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
816         self.assert_qmp(result, 'return', {})
818     # Another block_stream test
819     def test_block_stream_2(self):
820         # hd2 <- hd1 <- hd0
821         opts = hd_opts(0)
822         opts['backing'] = hd_opts(1)
823         opts['backing']['backing'] = hd_opts(2)
824         result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
825         self.assert_qmp(result, 'return', {})
827         # Stream hd1 into hd0 and wait until it's done
828         result = self.vm.qmp('block-stream', conv_keys = True, job_id = 'stream0',
829                              device = 'hd0', base_node = 'hd2')
830         self.assert_qmp(result, 'return', {})
831         self.wait_until_completed(drive = 'stream0')
833         # The chain is hd2 <- hd0 now. hd1 is missing
834         self.assertEqual(self.get_node('hd1'), None)
836         # The backing options in the dict were meant for hd1, but we cannot
837         # use them with hd2 because hd1 had a backing file while hd2 does not.
838         self.reopen(opts, {}, "Cannot change the option 'backing.driver'")
840         # If we remove hd1's options from the dict then things work fine
841         opts['backing'] = opts['backing']['backing']
842         self.reopen(opts)
844         # We can also reopen hd0 if we use a reference to the backing file
845         self.reopen(opts, {'backing': 'hd2'})
847         # But we cannot leave the option out
848         del opts['backing']
849         self.reopen(opts, {}, "backing is missing for 'hd0'")
851         # Now we can delete hd0 (and hd2)
852         result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
853         self.assert_qmp(result, 'return', {})
854         self.assertEqual(self.get_node('hd2'), None)
856     # Reopen the chain during a block-stream job (from hd1 to hd0)
857     def test_block_stream_3(self):
858         # hd2 <- hd1 <- hd0
859         opts = hd_opts(0)
860         opts['backing'] = hd_opts(1)
861         opts['backing']['backing'] = hd_opts(2)
862         result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
863         self.assert_qmp(result, 'return', {})
865         # hd2 <- hd0
866         result = self.vm.qmp('block-stream', conv_keys = True, job_id = 'stream0',
867                              device = 'hd0', base_node = 'hd2',
868                              auto_finalize = False)
869         self.assert_qmp(result, 'return', {})
871         # We can remove hd2 while the stream job is ongoing
872         opts['backing']['backing'] = None
873         self.reopen(opts, {})
875         # We can't remove hd1 while the stream job is ongoing
876         opts['backing'] = None
877         self.reopen(opts, {}, "Cannot change 'backing' link from 'hd0' to 'hd1'")
879         self.vm.run_job('stream0', auto_finalize = False, auto_dismiss = True)
881     # Reopen the chain during a block-stream job (from hd2 to hd1)
882     def test_block_stream_4(self):
883         # hd2 <- hd1 <- hd0
884         opts = hd_opts(0)
885         opts['backing'] = hd_opts(1)
886         opts['backing']['backing'] = hd_opts(2)
887         result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
888         self.assert_qmp(result, 'return', {})
890         # hd1 <- hd0
891         result = self.vm.qmp('block-stream', conv_keys = True, job_id = 'stream0',
892                              device = 'hd1', auto_finalize = False)
893         self.assert_qmp(result, 'return', {})
895         # We can't reopen with the original options because that would
896         # make hd1 read-only and block-stream requires it to be read-write
897         # (Which error message appears depends on whether the stream job is
898         # already done with copying at this point.)
899         self.reopen(opts, {},
900             ["Can't set node 'hd1' to r/o with copy-on-read enabled",
901              "Cannot make block node read-only, there is a writer on it"])
903         # We can't remove hd2 while the stream job is ongoing
904         opts['backing']['backing'] = None
905         self.reopen(opts, {'backing.read-only': False}, "Cannot change 'backing' link from 'hd1' to 'hd2'")
907         # We can detach hd1 from hd0 because it doesn't affect the stream job
908         opts['backing'] = None
909         self.reopen(opts)
911         self.vm.run_job('stream0', auto_finalize = False, auto_dismiss = True)
913     # Reopen the chain during a block-commit job (from hd0 to hd2)
914     def test_block_commit_1(self):
915         # hd2 <- hd1 <- hd0
916         opts = hd_opts(0)
917         opts['backing'] = hd_opts(1)
918         opts['backing']['backing'] = hd_opts(2)
919         result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
920         self.assert_qmp(result, 'return', {})
922         result = self.vm.qmp('block-commit', conv_keys = True, job_id = 'commit0',
923                              device = 'hd0')
924         self.assert_qmp(result, 'return', {})
926         # We can't remove hd2 while the commit job is ongoing
927         opts['backing']['backing'] = None
928         self.reopen(opts, {}, "Cannot change 'backing' link from 'hd1' to 'hd2'")
930         # We can't remove hd1 while the commit job is ongoing
931         opts['backing'] = None
932         self.reopen(opts, {}, "Cannot change 'backing' link from 'hd0' to 'hd1'")
934         event = self.vm.event_wait(name='BLOCK_JOB_READY')
935         self.assert_qmp(event, 'data/device', 'commit0')
936         self.assert_qmp(event, 'data/type', 'commit')
937         self.assert_qmp_absent(event, 'data/error')
939         result = self.vm.qmp('block-job-complete', device='commit0')
940         self.assert_qmp(result, 'return', {})
942         self.wait_until_completed(drive = 'commit0')
944     # Reopen the chain during a block-commit job (from hd1 to hd2)
945     def test_block_commit_2(self):
946         # hd2 <- hd1 <- hd0
947         opts = hd_opts(0)
948         opts['backing'] = hd_opts(1)
949         opts['backing']['backing'] = hd_opts(2)
950         result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
951         self.assert_qmp(result, 'return', {})
953         result = self.vm.qmp('block-commit', conv_keys = True, job_id = 'commit0',
954                              device = 'hd0', top_node = 'hd1',
955                              auto_finalize = False)
956         self.assert_qmp(result, 'return', {})
958         # We can't remove hd2 while the commit job is ongoing
959         opts['backing']['backing'] = None
960         self.reopen(opts, {}, "Cannot change the option 'backing.driver'")
962         # We can't remove hd1 while the commit job is ongoing
963         opts['backing'] = None
964         self.reopen(opts, {}, "Cannot change backing link if 'hd0' has an implicit backing file")
966         # hd2 <- hd0
967         self.vm.run_job('commit0', auto_finalize = False, auto_dismiss = True)
969         self.assert_qmp(self.get_node('hd0'), 'ro', False)
970         self.assertEqual(self.get_node('hd1'), None)
971         self.assert_qmp(self.get_node('hd2'), 'ro', True)
973     def run_test_iothreads(self, iothread_a, iothread_b, errmsg = None):
974         opts = hd_opts(0)
975         result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
976         self.assert_qmp(result, 'return', {})
978         opts2 = hd_opts(2)
979         result = self.vm.qmp('blockdev-add', conv_keys = False, **opts2)
980         self.assert_qmp(result, 'return', {})
982         result = self.vm.qmp('object-add', qom_type='iothread', id='iothread0')
983         self.assert_qmp(result, 'return', {})
985         result = self.vm.qmp('object-add', qom_type='iothread', id='iothread1')
986         self.assert_qmp(result, 'return', {})
988         result = self.vm.qmp('device_add', driver='virtio-scsi', id='scsi0',
989                              iothread=iothread_a)
990         self.assert_qmp(result, 'return', {})
992         result = self.vm.qmp('device_add', driver='virtio-scsi', id='scsi1',
993                              iothread=iothread_b)
994         self.assert_qmp(result, 'return', {})
996         if iothread_a:
997             result = self.vm.qmp('device_add', driver='scsi-hd', drive='hd0',
998                                  share_rw=True, bus="scsi0.0")
999             self.assert_qmp(result, 'return', {})
1001         if iothread_b:
1002             result = self.vm.qmp('device_add', driver='scsi-hd', drive='hd2',
1003                                  share_rw=True, bus="scsi1.0")
1004             self.assert_qmp(result, 'return', {})
1006         # Attaching the backing file may or may not work
1007         self.reopen(opts, {'backing': 'hd2'}, errmsg)
1009         # But removing the backing file should always work
1010         self.reopen(opts, {'backing': None})
1012         self.vm.shutdown()
1014     # We don't allow setting a backing file that uses a different AioContext if
1015     # neither of them can switch to the other AioContext
1016     def test_iothreads_error(self):
1017         self.run_test_iothreads('iothread0', 'iothread1',
1018                                 "Cannot change iothread of active block backend")
1020     def test_iothreads_compatible_users(self):
1021         self.run_test_iothreads('iothread0', 'iothread0')
1023     def test_iothreads_switch_backing(self):
1024         self.run_test_iothreads('iothread0', None)
1026     def test_iothreads_switch_overlay(self):
1027         self.run_test_iothreads(None, 'iothread0')
1029 if __name__ == '__main__':
1030     iotests.activate_logging()
1031     iotests.main(supported_fmts=["qcow2"],
1032                  supported_protocols=["file"])