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/>.
27 from iotests import qemu_img, qemu_io
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')
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):
46 qemu_img('create', '-f', iotests.imgfmt, hd_path[0], '3M')
47 qemu_img('create', '-f', iotests.imgfmt, '-b', hd_path[0],
48 '-F', iotests.imgfmt, hd_path[1])
49 qemu_img('create', '-f', iotests.imgfmt, hd_path[2], '3M')
50 qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xa0 0 1M', hd_path[0])
51 qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xa1 1M 1M', hd_path[1])
52 qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xa2 2M 1M', hd_path[2])
53 self.vm = iotests.VM()
58 self.check_qemu_io_errors()
63 # The output of qemu-io is not returned by vm.hmp_qemu_io() but
64 # it's stored in the log and can only be read when the VM has been
65 # shut down. This function runs qemu-io and keeps track of the
66 # number of times it's been called.
67 def run_qemu_io(self, img, cmd):
68 result = self.vm.hmp_qemu_io(img, cmd)
69 self.assert_qmp(result, 'return', '')
70 self.total_io_cmds += 1
72 # Once the VM is shut down we can parse the log and see if qemu-io
74 def check_qemu_io_errors(self):
75 self.assertFalse(self.vm.is_running())
77 log = self.vm.get_log()
78 for line in log.split("\n"):
79 if line.startswith("Pattern verification failed"):
80 raise Exception("%s (command #%d)" % (line, found))
81 if re.match("read .*/.* bytes at offset", line):
83 self.assertEqual(found, self.total_io_cmds,
84 "Expected output of %d qemu-io commands, found %d" %
85 (found, self.total_io_cmds))
87 # Run x-blockdev-reopen with 'opts' but applying 'newopts'
88 # on top of it. The original 'opts' dict is unmodified
89 def reopen(self, opts, newopts = {}, errmsg = None):
90 opts = copy.deepcopy(opts)
92 # Apply the changes from 'newopts' on top of 'opts'
95 # If key has the form "foo.bar" then we need to do
96 # opts["foo"]["bar"] = value, not opts["foo.bar"] = value
98 while key.find('.') != -1:
99 [prefix, key] = key.split('.', 1)
100 subdict = opts[prefix]
103 result = self.vm.qmp('x-blockdev-reopen', conv_keys = False, **opts)
105 self.assert_qmp(result, 'error/class', 'GenericError')
106 self.assert_qmp(result, 'error/desc', errmsg)
108 self.assert_qmp(result, 'return', {})
111 # Run query-named-block-nodes and return the specified entry
112 def get_node(self, node_name):
113 result = self.vm.qmp('query-named-block-nodes')
114 for node in result['return']:
115 if node['node-name'] == node_name:
119 # Run 'query-named-block-nodes' and compare its output with the
120 # value passed by the user in 'graph'
121 def check_node_graph(self, graph):
122 result = self.vm.qmp('query-named-block-nodes')
123 self.assertEqual(json.dumps(graph, sort_keys=True),
124 json.dumps(result, sort_keys=True))
126 # This test opens one single disk image (without backing files)
127 # and tries to reopen it with illegal / incorrect parameters.
128 def test_incorrect_parameters_single_file(self):
129 # Open 'hd0' only (no backing files)
131 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
132 self.assert_qmp(result, 'return', {})
133 original_graph = self.vm.qmp('query-named-block-nodes')
135 # We can reopen the image passing the same options
138 # We can also reopen passing a child reference in 'file'
139 self.reopen(opts, {'file': 'hd0-file'})
141 # We cannot change any of these
142 self.reopen(opts, {'node-name': 'not-found'}, "Cannot find node named 'not-found'")
143 self.reopen(opts, {'node-name': ''}, "Cannot find node named ''")
144 self.reopen(opts, {'node-name': None}, "Invalid parameter type for 'node-name', expected: string")
145 self.reopen(opts, {'driver': 'raw'}, "Cannot change the option 'driver'")
146 self.reopen(opts, {'driver': ''}, "Invalid parameter ''")
147 self.reopen(opts, {'driver': None}, "Invalid parameter type for 'driver', expected: string")
148 self.reopen(opts, {'file': 'not-found'}, "Cannot change the option 'file'")
149 self.reopen(opts, {'file': ''}, "Cannot change the option 'file'")
150 self.reopen(opts, {'file': None}, "Invalid parameter type for 'file', expected: BlockdevRef")
151 self.reopen(opts, {'file.node-name': 'newname'}, "Cannot change the option 'node-name'")
152 self.reopen(opts, {'file.driver': 'host_device'}, "Cannot change the option 'driver'")
153 self.reopen(opts, {'file.filename': hd_path[1]}, "Cannot change the option 'filename'")
154 self.reopen(opts, {'file.aio': 'native'}, "Cannot change the option 'aio'")
155 self.reopen(opts, {'file.locking': 'off'}, "Cannot change the option 'locking'")
156 self.reopen(opts, {'file.filename': None}, "Invalid parameter type for 'file.filename', expected: string")
158 # node-name is optional in BlockdevOptions, but x-blockdev-reopen needs it
159 del opts['node-name']
160 self.reopen(opts, {}, "Node name not specified")
162 # Check that nothing has changed
163 self.check_node_graph(original_graph)
166 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
167 self.assert_qmp(result, 'return', {})
169 # This test opens an image with a backing file and tries to reopen
170 # it with illegal / incorrect parameters.
171 def test_incorrect_parameters_backing_file(self):
172 # Open hd1 omitting the backing options (hd0 will be opened
173 # with the default options)
175 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
176 self.assert_qmp(result, 'return', {})
177 original_graph = self.vm.qmp('query-named-block-nodes')
179 # We can't reopen the image passing the same options, 'backing' is mandatory
180 self.reopen(opts, {}, "backing is missing for 'hd1'")
182 # Everything works if we pass 'backing' using the existing node name
183 for node in original_graph['return']:
184 if node['drv'] == iotests.imgfmt and node['file'] == hd_path[0]:
185 backing_node_name = node['node-name']
186 self.reopen(opts, {'backing': backing_node_name})
188 # We can't use a non-existing or empty (non-NULL) node as the backing image
189 self.reopen(opts, {'backing': 'not-found'}, "Cannot find device= nor node_name=not-found")
190 self.reopen(opts, {'backing': ''}, "Cannot find device= nor node_name=")
192 # We can reopen the image just fine if we specify the backing options
193 opts['backing'] = {'driver': iotests.imgfmt,
194 'file': {'driver': 'file',
195 'filename': hd_path[0]}}
198 # We cannot change any of these options
199 self.reopen(opts, {'backing.node-name': 'newname'}, "Cannot change the option 'node-name'")
200 self.reopen(opts, {'backing.driver': 'raw'}, "Cannot change the option 'driver'")
201 self.reopen(opts, {'backing.file.node-name': 'newname'}, "Cannot change the option 'node-name'")
202 self.reopen(opts, {'backing.file.driver': 'host_device'}, "Cannot change the option 'driver'")
204 # Check that nothing has changed since the beginning
205 self.check_node_graph(original_graph)
208 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd1')
209 self.assert_qmp(result, 'return', {})
211 # Reopen an image several times changing some of its options
212 def test_reopen(self):
213 # Check whether the filesystem supports O_DIRECT
214 if 'O_DIRECT' in qemu_io('-f', 'raw', '-t', 'none', '-c', 'quit', hd_path[0]):
215 supports_direct = False
217 supports_direct = True
219 # Open the hd1 image passing all backing options
221 opts['backing'] = hd_opts(0)
222 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
223 self.assert_qmp(result, 'return', {})
224 original_graph = self.vm.qmp('query-named-block-nodes')
226 # We can reopen the image passing the same options
229 # Reopen in read-only mode
230 self.assert_qmp(self.get_node('hd1'), 'ro', False)
232 self.reopen(opts, {'read-only': True})
233 self.assert_qmp(self.get_node('hd1'), 'ro', True)
235 self.assert_qmp(self.get_node('hd1'), 'ro', False)
237 # Change the cache options
238 self.assert_qmp(self.get_node('hd1'), 'cache/writeback', True)
239 self.assert_qmp(self.get_node('hd1'), 'cache/direct', False)
240 self.assert_qmp(self.get_node('hd1'), 'cache/no-flush', False)
241 self.reopen(opts, {'cache': { 'direct': supports_direct, 'no-flush': True }})
242 self.assert_qmp(self.get_node('hd1'), 'cache/writeback', True)
243 self.assert_qmp(self.get_node('hd1'), 'cache/direct', supports_direct)
244 self.assert_qmp(self.get_node('hd1'), 'cache/no-flush', True)
246 # Reopen again with the original options
248 self.assert_qmp(self.get_node('hd1'), 'cache/writeback', True)
249 self.assert_qmp(self.get_node('hd1'), 'cache/direct', False)
250 self.assert_qmp(self.get_node('hd1'), 'cache/no-flush', False)
252 # Change 'detect-zeroes' and 'discard'
253 self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'off')
254 self.reopen(opts, {'detect-zeroes': 'on'})
255 self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'on')
256 self.reopen(opts, {'detect-zeroes': 'unmap'},
257 "setting detect-zeroes to unmap is not allowed " +
258 "without setting discard operation to unmap")
259 self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'on')
260 self.reopen(opts, {'detect-zeroes': 'unmap', 'discard': 'unmap'})
261 self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'unmap')
263 self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'off')
265 # Changing 'force-share' is currently not supported
266 self.reopen(opts, {'force-share': True}, "Cannot change the option 'force-share'")
268 # Change some qcow2-specific options
269 # No way to test for success other than checking the return message
270 if iotests.imgfmt == 'qcow2':
271 self.reopen(opts, {'l2-cache-entry-size': 128 * 1024},
272 "L2 cache entry size must be a power of two "+
273 "between 512 and the cluster size (65536)")
274 self.reopen(opts, {'l2-cache-size': 1024 * 1024,
275 'cache-size': 512 * 1024},
276 "l2-cache-size may not exceed cache-size")
277 self.reopen(opts, {'l2-cache-size': 4 * 1024 * 1024,
278 'refcount-cache-size': 4 * 1024 * 1024,
279 'l2-cache-entry-size': 32 * 1024})
280 self.reopen(opts, {'pass-discard-request': True})
282 # Check that nothing has changed since the beginning
283 # (from the parameters that we can check)
284 self.check_node_graph(original_graph)
286 # Check that the node names (other than the top-level one) are optional
287 del opts['file']['node-name']
288 del opts['backing']['node-name']
289 del opts['backing']['file']['node-name']
291 self.check_node_graph(original_graph)
293 # Reopen setting backing = null, this removes the backing image from the chain
294 self.reopen(opts, {'backing': None})
295 self.assert_qmp_absent(self.get_node('hd1'), 'image/backing-image')
297 # Open the 'hd0' image
298 result = self.vm.qmp('blockdev-add', conv_keys = False, **hd_opts(0))
299 self.assert_qmp(result, 'return', {})
301 # Reopen the hd1 image setting 'hd0' as its backing image
302 self.reopen(opts, {'backing': 'hd0'})
303 self.assert_qmp(self.get_node('hd1'), 'image/backing-image/filename', hd_path[0])
305 # Check that nothing has changed since the beginning
306 self.reopen(hd_opts(0), {'read-only': True})
307 self.check_node_graph(original_graph)
309 # The backing file (hd0) is now a reference, we cannot change backing.* anymore
310 self.reopen(opts, {}, "Cannot change the option 'backing.driver'")
312 # We can't remove 'hd0' while it's a backing image of 'hd1'
313 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
314 self.assert_qmp(result, 'error/class', 'GenericError')
315 self.assert_qmp(result, 'error/desc', "Node 'hd0' is busy: node is used as backing hd of 'hd1'")
317 # But we can remove both nodes if done in the proper order
318 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd1')
319 self.assert_qmp(result, 'return', {})
320 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
321 self.assert_qmp(result, 'return', {})
323 # Reopen a raw image and see the effect of changing the 'offset' option
324 def test_reopen_raw(self):
325 opts = {'driver': 'raw', 'node-name': 'hd0',
326 'file': { 'driver': 'file',
327 'filename': hd_path[0],
328 'node-name': 'hd0-file' } }
330 # First we create a 2MB raw file, and fill each half with a
332 qemu_img('create', '-f', 'raw', hd_path[0], '2M')
333 qemu_io('-f', 'raw', '-c', 'write -P 0xa0 0 1M', hd_path[0])
334 qemu_io('-f', 'raw', '-c', 'write -P 0xa1 1M 1M', hd_path[0])
336 # Open the raw file with QEMU
337 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
338 self.assert_qmp(result, 'return', {})
340 # Read 1MB from offset 0
341 self.run_qemu_io("hd0", "read -P 0xa0 0 1M")
343 # Reopen the image with a 1MB offset.
344 # Now the results are different
345 self.reopen(opts, {'offset': 1024*1024})
346 self.run_qemu_io("hd0", "read -P 0xa1 0 1M")
348 # Reopen again with the original options.
349 # We get the original results again
351 self.run_qemu_io("hd0", "read -P 0xa0 0 1M")
353 # Remove the block device
354 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
355 self.assert_qmp(result, 'return', {})
357 # Omitting an option should reset it to the default value, but if
358 # an option cannot be changed it shouldn't be possible to reset it
359 # to its default value either
360 def test_reset_default_values(self):
361 opts = {'driver': 'qcow2', 'node-name': 'hd0',
362 'file': { 'driver': 'file',
363 'filename': hd_path[0],
364 'x-check-cache-dropped': True, # This one can be changed
365 'locking': 'off', # This one can NOT be changed
366 'node-name': 'hd0-file' } }
368 # Open the file with QEMU
369 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
370 self.assert_qmp(result, 'return', {})
372 # file.x-check-cache-dropped can be changed...
373 self.reopen(opts, { 'file.x-check-cache-dropped': False })
374 # ...and dropped completely (resetting to the default value)
375 del opts['file']['x-check-cache-dropped']
378 # file.locking cannot be changed nor reset to the default value
379 self.reopen(opts, { 'file.locking': 'on' }, "Cannot change the option 'locking'")
380 del opts['file']['locking']
381 self.reopen(opts, {}, "Option 'locking' cannot be reset to its default value")
382 # But we can reopen it if we maintain its previous value
383 self.reopen(opts, { 'file.locking': 'off' })
385 # Remove the block device
386 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
387 self.assert_qmp(result, 'return', {})
389 # This test modifies the node graph a few times by changing the
390 # 'backing' option on reopen and verifies that the guest data that
391 # is read afterwards is consistent with the graph changes.
392 def test_io_with_graph_changes(self):
395 # Open hd0, hd1 and hd2 without any backing image
397 opts.append(hd_opts(i))
398 opts[i]['backing'] = None
399 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts[i])
400 self.assert_qmp(result, 'return', {})
403 self.run_qemu_io("hd0", "read -P 0xa0 0 1M")
404 self.run_qemu_io("hd0", "read -P 0 1M 1M")
405 self.run_qemu_io("hd0", "read -P 0 2M 1M")
408 self.reopen(opts[0], {'backing': 'hd1'})
410 self.run_qemu_io("hd0", "read -P 0xa0 0 1M")
411 self.run_qemu_io("hd0", "read -P 0xa1 1M 1M")
412 self.run_qemu_io("hd0", "read -P 0 2M 1M")
414 # hd1 <- hd0 , hd1 <- hd2
415 self.reopen(opts[2], {'backing': 'hd1'})
417 self.run_qemu_io("hd2", "read -P 0 0 1M")
418 self.run_qemu_io("hd2", "read -P 0xa1 1M 1M")
419 self.run_qemu_io("hd2", "read -P 0xa2 2M 1M")
422 self.reopen(opts[0], {'backing': 'hd2'})
424 self.run_qemu_io("hd0", "read -P 0xa0 0 1M")
425 self.run_qemu_io("hd0", "read -P 0xa1 1M 1M")
426 self.run_qemu_io("hd0", "read -P 0xa2 2M 1M")
429 self.reopen(opts[2], {'backing': None})
431 self.run_qemu_io("hd0", "read -P 0xa0 0 1M")
432 self.run_qemu_io("hd0", "read -P 0 1M 1M")
433 self.run_qemu_io("hd0", "read -P 0xa2 2M 1M")
436 self.reopen(opts[1], {'backing': 'hd2'})
437 self.reopen(opts[0], {'backing': 'hd1'})
439 self.run_qemu_io("hd0", "read -P 0xa0 0 1M")
440 self.run_qemu_io("hd0", "read -P 0xa1 1M 1M")
441 self.run_qemu_io("hd0", "read -P 0xa2 2M 1M")
443 # Illegal operation: hd2 is a child of hd1
444 self.reopen(opts[2], {'backing': 'hd1'},
445 "Making 'hd1' a backing file of 'hd2' would create a cycle")
447 # hd2 <- hd0, hd2 <- hd1
448 self.reopen(opts[0], {'backing': 'hd2'})
450 self.run_qemu_io("hd1", "read -P 0 0 1M")
451 self.run_qemu_io("hd1", "read -P 0xa1 1M 1M")
452 self.run_qemu_io("hd1", "read -P 0xa2 2M 1M")
454 # More illegal operations
455 self.reopen(opts[2], {'backing': 'hd1'},
456 "Making 'hd1' a backing file of 'hd2' would create a cycle")
457 self.reopen(opts[2], {'file': 'hd0-file'}, "Cannot change the option 'file'")
459 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd2')
460 self.assert_qmp(result, 'error/class', 'GenericError')
461 self.assert_qmp(result, 'error/desc', "Node 'hd2' is busy: node is used as backing hd of 'hd0'")
463 # hd1 doesn't have a backing file now
464 self.reopen(opts[1], {'backing': None})
466 self.run_qemu_io("hd1", "read -P 0 0 1M")
467 self.run_qemu_io("hd1", "read -P 0xa1 1M 1M")
468 self.run_qemu_io("hd1", "read -P 0 2M 1M")
470 # We can't remove the 'backing' option if the image has a
471 # default backing file
472 del opts[1]['backing']
473 self.reopen(opts[1], {}, "backing is missing for 'hd1'")
475 self.run_qemu_io("hd1", "read -P 0 0 1M")
476 self.run_qemu_io("hd1", "read -P 0xa1 1M 1M")
477 self.run_qemu_io("hd1", "read -P 0 2M 1M")
479 # This test verifies that we can't change the children of a block
480 # device during a reopen operation in a way that would create
481 # cycles in the node graph
482 @iotests.skip_if_unsupported(['blkverify'])
483 def test_graph_cycles(self):
486 # Open all three images without backing file
488 opts.append(hd_opts(i))
489 opts[i]['backing'] = None
490 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts[i])
491 self.assert_qmp(result, 'return', {})
493 # hd1 <- hd0, hd1 <- hd2
494 self.reopen(opts[0], {'backing': 'hd1'})
495 self.reopen(opts[2], {'backing': 'hd1'})
497 # Illegal: hd2 is backed by hd1
498 self.reopen(opts[1], {'backing': 'hd2'},
499 "Making 'hd2' a backing file of 'hd1' would create a cycle")
502 self.reopen(opts[2], {'backing': 'hd0'})
504 # Illegal: hd2 is backed by hd0, which is backed by hd1
505 self.reopen(opts[1], {'backing': 'hd2'},
506 "Making 'hd2' a backing file of 'hd1' would create a cycle")
508 # Illegal: hd1 cannot point to itself
509 self.reopen(opts[1], {'backing': 'hd1'},
510 "Making 'hd1' a backing file of 'hd1' would create a cycle")
512 # Remove all backing files
516 ##########################################
517 # Add a blkverify node using hd0 and hd1 #
518 ##########################################
519 bvopts = {'driver': 'blkverify',
523 result = self.vm.qmp('blockdev-add', conv_keys = False, **bvopts)
524 self.assert_qmp(result, 'return', {})
526 # blkverify doesn't currently allow reopening. TODO: implement this
527 self.reopen(bvopts, {}, "Block format 'blkverify' used by node 'bv'" +
528 " does not support reopening files")
530 # Illegal: hd0 is a child of the blkverify node
531 self.reopen(opts[0], {'backing': 'bv'},
532 "Making 'bv' a backing file of 'hd0' would create a cycle")
534 # Delete the blkverify node
535 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'bv')
536 self.assert_qmp(result, 'return', {})
538 # Misc reopen tests with different block drivers
539 @iotests.skip_if_unsupported(['quorum', 'throttle'])
540 def test_misc_drivers(self):
546 # Open all three images without backing file
547 opts['backing'] = None
548 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
549 self.assert_qmp(result, 'return', {})
551 opts = {'driver': 'quorum',
552 'node-name': 'quorum0',
553 'children': ['hd0', 'hd1', 'hd2'],
555 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
556 self.assert_qmp(result, 'return', {})
558 # Quorum doesn't currently allow reopening. TODO: implement this
559 self.reopen(opts, {}, "Block format 'quorum' used by node 'quorum0'" +
560 " does not support reopening files")
562 # You can't make quorum0 a backing file of hd0:
563 # hd0 is already a child of quorum0.
564 self.reopen(hd_opts(0), {'backing': 'quorum0'},
565 "Making 'quorum0' a backing file of 'hd0' would create a cycle")
568 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'quorum0')
569 self.assert_qmp(result, 'return', {})
571 # Delete hd0, hd1 and hd2
573 result = self.vm.qmp('blockdev-del', conv_keys = True,
574 node_name = 'hd%d' % i)
575 self.assert_qmp(result, 'return', {})
577 ######################
578 ###### blkdebug ######
579 ######################
580 opts = {'driver': 'blkdebug',
582 'config': '/dev/null',
584 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
585 self.assert_qmp(result, 'return', {})
587 # blkdebug allows reopening if we keep the same options
590 # but it currently does not allow changes
591 self.reopen(opts, {'image': 'hd1'}, "Cannot change the option 'image'")
592 self.reopen(opts, {'align': 33554432}, "Cannot change the option 'align'")
593 self.reopen(opts, {'config': '/non/existent'}, "Cannot change the option 'config'")
595 self.reopen(opts, {}, "Option 'config' cannot be reset to its default value")
597 # Delete the blkdebug node
598 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'bd')
599 self.assert_qmp(result, 'return', {})
604 opts = {'driver': 'null-co', 'node-name': 'root', 'size': 1024}
606 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
607 self.assert_qmp(result, 'return', {})
609 # 1 << 30 is the default value, but we cannot change it explicitly
610 self.reopen(opts, {'size': (1 << 30)}, "Cannot change the option 'size'")
612 # We cannot change 'size' back to its default value either
614 self.reopen(opts, {}, "Option 'size' cannot be reset to its default value")
616 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'root')
617 self.assert_qmp(result, 'return', {})
623 opts['file']['locking'] = 'on'
624 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
625 self.assert_qmp(result, 'return', {})
627 # 'locking' cannot be changed
628 del opts['file']['locking']
629 self.reopen(opts, {'file.locking': 'on'})
630 self.reopen(opts, {'file.locking': 'off'}, "Cannot change the option 'locking'")
631 self.reopen(opts, {}, "Option 'locking' cannot be reset to its default value")
633 # Trying to reopen the 'file' node directly does not make a difference
635 self.reopen(opts, {'locking': 'on'})
636 self.reopen(opts, {'locking': 'off'}, "Cannot change the option 'locking'")
637 self.reopen(opts, {}, "Option 'locking' cannot be reset to its default value")
639 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
640 self.assert_qmp(result, 'return', {})
642 ######################
643 ###### throttle ######
644 ######################
645 opts = { 'qom-type': 'throttle-group', 'id': 'group0',
646 'props': { 'limits': { 'iops-total': 1000 } } }
647 result = self.vm.qmp('object-add', conv_keys = False, **opts)
648 self.assert_qmp(result, 'return', {})
650 opts = { 'qom-type': 'throttle-group', 'id': 'group1',
651 'props': { 'limits': { 'iops-total': 2000 } } }
652 result = self.vm.qmp('object-add', conv_keys = False, **opts)
653 self.assert_qmp(result, 'return', {})
655 # Add a throttle filter with group = group0
656 opts = { 'driver': 'throttle', 'node-name': 'throttle0',
657 'throttle-group': 'group0', 'file': hd_opts(0) }
658 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
659 self.assert_qmp(result, 'return', {})
661 # We can reopen it if we keep the same options
664 # We can also reopen if 'file' is a reference to the child
665 self.reopen(opts, {'file': 'hd0'})
668 self.reopen(opts, {'throttle-group': 'notfound'}, "Throttle group 'notfound' does not exist")
670 # But it's possible to change the group to group1
671 self.reopen(opts, {'throttle-group': 'group1'})
673 # Now group1 is in use, it cannot be deleted
674 result = self.vm.qmp('object-del', id = 'group1')
675 self.assert_qmp(result, 'error/class', 'GenericError')
676 self.assert_qmp(result, 'error/desc', "object 'group1' is in use, can not be deleted")
678 # Default options, this switches the group back to group0
681 # So now we cannot delete group0
682 result = self.vm.qmp('object-del', id = 'group0')
683 self.assert_qmp(result, 'error/class', 'GenericError')
684 self.assert_qmp(result, 'error/desc', "object 'group0' is in use, can not be deleted")
686 # But group1 is free this time, and it can be deleted
687 result = self.vm.qmp('object-del', id = 'group1')
688 self.assert_qmp(result, 'return', {})
690 # Let's delete the filter node
691 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'throttle0')
692 self.assert_qmp(result, 'return', {})
694 # And we can finally get rid of group0
695 result = self.vm.qmp('object-del', id = 'group0')
696 self.assert_qmp(result, 'return', {})
698 # If an image has a backing file then the 'backing' option must be
699 # passed on reopen. We don't allow leaving the option out in this
700 # case because it's unclear what the correct semantics would be.
701 def test_missing_backing_options_1(self):
704 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
705 self.assert_qmp(result, 'return', {})
709 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
710 self.assert_qmp(result, 'return', {})
712 # hd0 has no backing file: we can omit the 'backing' option
716 self.reopen(opts, {'backing': 'hd2'})
718 # hd0 has a backing file: we must set the 'backing' option
719 self.reopen(opts, {}, "backing is missing for 'hd0'")
721 # hd2 can't be removed because it's the backing file of hd0
722 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd2')
723 self.assert_qmp(result, 'error/class', 'GenericError')
724 self.assert_qmp(result, 'error/desc', "Node 'hd2' is busy: node is used as backing hd of 'hd0'")
726 # Detach hd2 from hd0.
727 self.reopen(opts, {'backing': None})
728 self.reopen(opts, {}, "backing is missing for 'hd0'")
730 # Remove both hd0 and hd2
731 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
732 self.assert_qmp(result, 'return', {})
734 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd2')
735 self.assert_qmp(result, 'return', {})
737 # If an image has default backing file (as part of its metadata)
738 # then the 'backing' option must be passed on reopen. We don't
739 # allow leaving the option out in this case because it's unclear
740 # what the correct semantics would be.
741 def test_missing_backing_options_2(self):
743 # (hd0 is hd1's default backing file)
745 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
746 self.assert_qmp(result, 'return', {})
748 # hd1 has a backing file: we can't omit the 'backing' option
749 self.reopen(opts, {}, "backing is missing for 'hd1'")
751 # Let's detach the backing file
752 self.reopen(opts, {'backing': None})
754 # No backing file attached to hd1 now, but we still can't omit the 'backing' option
755 self.reopen(opts, {}, "backing is missing for 'hd1'")
757 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd1')
758 self.assert_qmp(result, 'return', {})
760 # Test that making 'backing' a reference to an existing child
761 # keeps its current options
762 def test_backing_reference(self):
765 opts['backing'] = hd_opts(1)
766 opts['backing']['backing'] = hd_opts(2)
767 # Enable 'detect-zeroes' on all three nodes
768 opts['detect-zeroes'] = 'on'
769 opts['backing']['detect-zeroes'] = 'on'
770 opts['backing']['backing']['detect-zeroes'] = 'on'
771 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
772 self.assert_qmp(result, 'return', {})
774 # Reopen the chain passing the minimum amount of required options.
775 # By making 'backing' a reference to hd1 (instead of a sub-dict)
776 # we tell QEMU to keep its current set of options.
777 opts = {'driver': iotests.imgfmt,
783 # This has reset 'detect-zeroes' on hd0, but not on hd1 and hd2.
784 self.assert_qmp(self.get_node('hd0'), 'detect_zeroes', 'off')
785 self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'on')
786 self.assert_qmp(self.get_node('hd2'), 'detect_zeroes', 'on')
788 # Test what happens if the graph changes due to other operations
789 # such as block-stream
790 def test_block_stream_1(self):
793 opts['backing'] = hd_opts(1)
794 opts['backing']['backing'] = None
795 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
796 self.assert_qmp(result, 'return', {})
798 # Stream hd1 into hd0 and wait until it's done
799 result = self.vm.qmp('block-stream', conv_keys = True, job_id = 'stream0', device = 'hd0')
800 self.assert_qmp(result, 'return', {})
801 self.wait_until_completed(drive = 'stream0')
803 # Now we have only hd0
804 self.assertEqual(self.get_node('hd1'), None)
806 # We have backing.* options but there's no backing file anymore
807 self.reopen(opts, {}, "Cannot change the option 'backing.driver'")
809 # If we remove the 'backing' option then we can reopen hd0 just fine
813 # We can also reopen hd0 if we set 'backing' to null
814 self.reopen(opts, {'backing': None})
816 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
817 self.assert_qmp(result, 'return', {})
819 # Another block_stream test
820 def test_block_stream_2(self):
823 opts['backing'] = hd_opts(1)
824 opts['backing']['backing'] = hd_opts(2)
825 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
826 self.assert_qmp(result, 'return', {})
828 # Stream hd1 into hd0 and wait until it's done
829 result = self.vm.qmp('block-stream', conv_keys = True, job_id = 'stream0',
830 device = 'hd0', base_node = 'hd2')
831 self.assert_qmp(result, 'return', {})
832 self.wait_until_completed(drive = 'stream0')
834 # The chain is hd2 <- hd0 now. hd1 is missing
835 self.assertEqual(self.get_node('hd1'), None)
837 # The backing options in the dict were meant for hd1, but we cannot
838 # use them with hd2 because hd1 had a backing file while hd2 does not.
839 self.reopen(opts, {}, "Cannot change the option 'backing.driver'")
841 # If we remove hd1's options from the dict then things work fine
842 opts['backing'] = opts['backing']['backing']
845 # We can also reopen hd0 if we use a reference to the backing file
846 self.reopen(opts, {'backing': 'hd2'})
848 # But we cannot leave the option out
850 self.reopen(opts, {}, "backing is missing for 'hd0'")
852 # Now we can delete hd0 (and hd2)
853 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
854 self.assert_qmp(result, 'return', {})
855 self.assertEqual(self.get_node('hd2'), None)
857 # Reopen the chain during a block-stream job (from hd1 to hd0)
858 def test_block_stream_3(self):
861 opts['backing'] = hd_opts(1)
862 opts['backing']['backing'] = hd_opts(2)
863 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
864 self.assert_qmp(result, 'return', {})
867 result = self.vm.qmp('block-stream', conv_keys = True, job_id = 'stream0',
868 device = 'hd0', base_node = 'hd2',
869 auto_finalize = False)
870 self.assert_qmp(result, 'return', {})
872 # We can remove hd2 while the stream job is ongoing
873 opts['backing']['backing'] = None
874 self.reopen(opts, {})
876 # We can't remove hd1 while the stream job is ongoing
877 opts['backing'] = None
878 self.reopen(opts, {}, "Cannot change 'backing' link from 'hd0' to 'hd1'")
880 self.vm.run_job('stream0', auto_finalize = False, auto_dismiss = True)
882 # Reopen the chain during a block-stream job (from hd2 to hd1)
883 def test_block_stream_4(self):
886 opts['backing'] = hd_opts(1)
887 opts['backing']['backing'] = hd_opts(2)
888 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
889 self.assert_qmp(result, 'return', {})
892 result = self.vm.qmp('block-stream', conv_keys = True, job_id = 'stream0',
893 device = 'hd1', auto_finalize = False)
894 self.assert_qmp(result, 'return', {})
896 # We can't reopen with the original options because that would
897 # make hd1 read-only and block-stream requires it to be read-write
898 # (Which error message appears depends on whether the stream job is
899 # already done with copying at this point.)
900 self.reopen(opts, {},
901 ["Can't set node 'hd1' to r/o with copy-on-read enabled",
902 "Cannot make block node read-only, there is a writer on it"])
904 # We can't remove hd2 while the stream job is ongoing
905 opts['backing']['backing'] = None
906 self.reopen(opts, {'backing.read-only': False}, "Cannot change 'backing' link from 'hd1' to 'hd2'")
908 # We can detach hd1 from hd0 because it doesn't affect the stream job
909 opts['backing'] = None
912 self.vm.run_job('stream0', auto_finalize = False, auto_dismiss = True)
914 # Reopen the chain during a block-commit job (from hd0 to hd2)
915 def test_block_commit_1(self):
918 opts['backing'] = hd_opts(1)
919 opts['backing']['backing'] = hd_opts(2)
920 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
921 self.assert_qmp(result, 'return', {})
923 result = self.vm.qmp('block-commit', conv_keys = True, job_id = 'commit0',
925 self.assert_qmp(result, 'return', {})
927 # We can't remove hd2 while the commit job is ongoing
928 opts['backing']['backing'] = None
929 self.reopen(opts, {}, "Cannot change 'backing' link from 'hd1' to 'hd2'")
931 # We can't remove hd1 while the commit job is ongoing
932 opts['backing'] = None
933 self.reopen(opts, {}, "Cannot change 'backing' link from 'hd0' to 'hd1'")
935 event = self.vm.event_wait(name='BLOCK_JOB_READY')
936 self.assert_qmp(event, 'data/device', 'commit0')
937 self.assert_qmp(event, 'data/type', 'commit')
938 self.assert_qmp_absent(event, 'data/error')
940 result = self.vm.qmp('block-job-complete', device='commit0')
941 self.assert_qmp(result, 'return', {})
943 self.wait_until_completed(drive = 'commit0')
945 # Reopen the chain during a block-commit job (from hd1 to hd2)
946 def test_block_commit_2(self):
949 opts['backing'] = hd_opts(1)
950 opts['backing']['backing'] = hd_opts(2)
951 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
952 self.assert_qmp(result, 'return', {})
954 result = self.vm.qmp('block-commit', conv_keys = True, job_id = 'commit0',
955 device = 'hd0', top_node = 'hd1',
956 auto_finalize = False)
957 self.assert_qmp(result, 'return', {})
959 # We can't remove hd2 while the commit job is ongoing
960 opts['backing']['backing'] = None
961 self.reopen(opts, {}, "Cannot change the option 'backing.driver'")
963 # We can't remove hd1 while the commit job is ongoing
964 opts['backing'] = None
965 self.reopen(opts, {}, "Cannot change backing link if 'hd0' has an implicit backing file")
968 self.vm.run_job('commit0', auto_finalize = False, auto_dismiss = True)
970 self.assert_qmp(self.get_node('hd0'), 'ro', False)
971 self.assertEqual(self.get_node('hd1'), None)
972 self.assert_qmp(self.get_node('hd2'), 'ro', True)
974 def run_test_iothreads(self, iothread_a, iothread_b, errmsg = None):
976 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
977 self.assert_qmp(result, 'return', {})
980 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts2)
981 self.assert_qmp(result, 'return', {})
983 result = self.vm.qmp('object-add', qom_type='iothread', id='iothread0')
984 self.assert_qmp(result, 'return', {})
986 result = self.vm.qmp('object-add', qom_type='iothread', id='iothread1')
987 self.assert_qmp(result, 'return', {})
989 result = self.vm.qmp('device_add', driver='virtio-scsi', id='scsi0',
991 self.assert_qmp(result, 'return', {})
993 result = self.vm.qmp('device_add', driver='virtio-scsi', id='scsi1',
995 self.assert_qmp(result, 'return', {})
998 result = self.vm.qmp('device_add', driver='scsi-hd', drive='hd0',
999 share_rw=True, bus="scsi0.0")
1000 self.assert_qmp(result, 'return', {})
1003 result = self.vm.qmp('device_add', driver='scsi-hd', drive='hd2',
1004 share_rw=True, bus="scsi1.0")
1005 self.assert_qmp(result, 'return', {})
1007 # Attaching the backing file may or may not work
1008 self.reopen(opts, {'backing': 'hd2'}, errmsg)
1010 # But removing the backing file should always work
1011 self.reopen(opts, {'backing': None})
1015 # We don't allow setting a backing file that uses a different AioContext if
1016 # neither of them can switch to the other AioContext
1017 def test_iothreads_error(self):
1018 self.run_test_iothreads('iothread0', 'iothread1',
1019 "Cannot change iothread of active block backend")
1021 def test_iothreads_compatible_users(self):
1022 self.run_test_iothreads('iothread0', 'iothread0')
1024 def test_iothreads_switch_backing(self):
1025 self.run_test_iothreads('iothread0', None)
1027 def test_iothreads_switch_overlay(self):
1028 self.run_test_iothreads(None, 'iothread0')
1030 if __name__ == '__main__':
1031 iotests.activate_logging()
1032 iotests.main(supported_fmts=["qcow2"],
1033 supported_protocols=["file"])