4 # Test cases for the QMP 'x-blockdev-reopen' command
6 # Copyright (C) 2018-2019 Igalia, S.L.
7 # Author: Alberto Garcia <berto@igalia.com>
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
28 from iotests import qemu_img, qemu_io
31 os.path.join(iotests.test_dir, 'hd0.img'),
32 os.path.join(iotests.test_dir, 'hd1.img'),
33 os.path.join(iotests.test_dir, 'hd2.img')
37 return {'driver': iotests.imgfmt,
38 'node-name': 'hd%d' % idx,
39 'file': {'driver': 'file',
40 'node-name': 'hd%d-file' % idx,
41 'filename': hd_path[idx] } }
43 class TestBlockdevReopen(iotests.QMPTestCase):
47 qemu_img('create', '-f', iotests.imgfmt, hd_path[0], '3M')
48 qemu_img('create', '-f', iotests.imgfmt, '-b', hd_path[0],
49 '-F', iotests.imgfmt, hd_path[1])
50 qemu_img('create', '-f', iotests.imgfmt, hd_path[2], '3M')
51 qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xa0 0 1M', hd_path[0])
52 qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xa1 1M 1M', hd_path[1])
53 qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xa2 2M 1M', hd_path[2])
54 self.vm = iotests.VM()
59 self.check_qemu_io_errors()
64 # The output of qemu-io is not returned by vm.hmp_qemu_io() but
65 # it's stored in the log and can only be read when the VM has been
66 # shut down. This function runs qemu-io and keeps track of the
67 # number of times it's been called.
68 def run_qemu_io(self, img, cmd):
69 result = self.vm.hmp_qemu_io(img, cmd)
70 self.assert_qmp(result, 'return', '')
71 self.total_io_cmds += 1
73 # Once the VM is shut down we can parse the log and see if qemu-io
75 def check_qemu_io_errors(self):
76 self.assertFalse(self.vm.is_running())
78 log = self.vm.get_log()
79 for line in log.split("\n"):
80 if line.startswith("Pattern verification failed"):
81 raise Exception("%s (command #%d)" % (line, found))
82 if re.match("read .*/.* bytes at offset", line):
84 self.assertEqual(found, self.total_io_cmds,
85 "Expected output of %d qemu-io commands, found %d" %
86 (found, self.total_io_cmds))
88 # Run x-blockdev-reopen with 'opts' but applying 'newopts'
89 # on top of it. The original 'opts' dict is unmodified
90 def reopen(self, opts, newopts = {}, errmsg = None):
91 opts = copy.deepcopy(opts)
93 # Apply the changes from 'newopts' on top of 'opts'
96 # If key has the form "foo.bar" then we need to do
97 # opts["foo"]["bar"] = value, not opts["foo.bar"] = value
99 while key.find('.') != -1:
100 [prefix, key] = key.split('.', 1)
101 subdict = opts[prefix]
104 result = self.vm.qmp('x-blockdev-reopen', conv_keys = False, **opts)
106 self.assert_qmp(result, 'error/class', 'GenericError')
107 self.assert_qmp(result, 'error/desc', errmsg)
109 self.assert_qmp(result, 'return', {})
112 # Run query-named-block-nodes and return the specified entry
113 def get_node(self, node_name):
114 result = self.vm.qmp('query-named-block-nodes')
115 for node in result['return']:
116 if node['node-name'] == node_name:
120 # Run 'query-named-block-nodes' and compare its output with the
121 # value passed by the user in 'graph'
122 def check_node_graph(self, graph):
123 result = self.vm.qmp('query-named-block-nodes')
124 self.assertEqual(json.dumps(graph, sort_keys=True),
125 json.dumps(result, sort_keys=True))
127 # This test opens one single disk image (without backing files)
128 # and tries to reopen it with illegal / incorrect parameters.
129 def test_incorrect_parameters_single_file(self):
130 # Open 'hd0' only (no backing files)
132 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
133 self.assert_qmp(result, 'return', {})
134 original_graph = self.vm.qmp('query-named-block-nodes')
136 # We can reopen the image passing the same options
139 # We can also reopen passing a child reference in 'file'
140 self.reopen(opts, {'file': 'hd0-file'})
142 # We cannot change any of these
143 self.reopen(opts, {'node-name': 'not-found'}, "Cannot find node named 'not-found'")
144 self.reopen(opts, {'node-name': ''}, "Cannot find node named ''")
145 self.reopen(opts, {'node-name': None}, "Invalid parameter type for 'node-name', expected: string")
146 self.reopen(opts, {'driver': 'raw'}, "Cannot change the option 'driver'")
147 self.reopen(opts, {'driver': ''}, "Invalid parameter ''")
148 self.reopen(opts, {'driver': None}, "Invalid parameter type for 'driver', expected: string")
149 self.reopen(opts, {'file': 'not-found'}, "Cannot change the option 'file'")
150 self.reopen(opts, {'file': ''}, "Cannot change the option 'file'")
151 self.reopen(opts, {'file': None}, "Invalid parameter type for 'file', expected: BlockdevRef")
152 self.reopen(opts, {'file.node-name': 'newname'}, "Cannot change the option 'node-name'")
153 self.reopen(opts, {'file.driver': 'host_device'}, "Cannot change the option 'driver'")
154 self.reopen(opts, {'file.filename': hd_path[1]}, "Cannot change the option 'filename'")
155 self.reopen(opts, {'file.aio': 'native'}, "Cannot change the option 'aio'")
156 self.reopen(opts, {'file.locking': 'off'}, "Cannot change the option 'locking'")
157 self.reopen(opts, {'file.filename': None}, "Invalid parameter type for 'file.filename', expected: string")
159 # node-name is optional in BlockdevOptions, but x-blockdev-reopen needs it
160 del opts['node-name']
161 self.reopen(opts, {}, "Node name not specified")
163 # Check that nothing has changed
164 self.check_node_graph(original_graph)
167 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
168 self.assert_qmp(result, 'return', {})
170 # This test opens an image with a backing file and tries to reopen
171 # it with illegal / incorrect parameters.
172 def test_incorrect_parameters_backing_file(self):
173 # Open hd1 omitting the backing options (hd0 will be opened
174 # with the default options)
176 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
177 self.assert_qmp(result, 'return', {})
178 original_graph = self.vm.qmp('query-named-block-nodes')
180 # We can't reopen the image passing the same options, 'backing' is mandatory
181 self.reopen(opts, {}, "backing is missing for 'hd1'")
183 # Everything works if we pass 'backing' using the existing node name
184 for node in original_graph['return']:
185 if node['drv'] == iotests.imgfmt and node['file'] == hd_path[0]:
186 backing_node_name = node['node-name']
187 self.reopen(opts, {'backing': backing_node_name})
189 # We can't use a non-existing or empty (non-NULL) node as the backing image
190 self.reopen(opts, {'backing': 'not-found'}, "Cannot find device= nor node_name=not-found")
191 self.reopen(opts, {'backing': ''}, "Cannot find device= nor node_name=")
193 # We can reopen the image just fine if we specify the backing options
194 opts['backing'] = {'driver': iotests.imgfmt,
195 'file': {'driver': 'file',
196 'filename': hd_path[0]}}
199 # We cannot change any of these options
200 self.reopen(opts, {'backing.node-name': 'newname'}, "Cannot change the option 'node-name'")
201 self.reopen(opts, {'backing.driver': 'raw'}, "Cannot change the option 'driver'")
202 self.reopen(opts, {'backing.file.node-name': 'newname'}, "Cannot change the option 'node-name'")
203 self.reopen(opts, {'backing.file.driver': 'host_device'}, "Cannot change the option 'driver'")
205 # Check that nothing has changed since the beginning
206 self.check_node_graph(original_graph)
209 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd1')
210 self.assert_qmp(result, 'return', {})
212 # Reopen an image several times changing some of its options
213 def test_reopen(self):
214 # Check whether the filesystem supports O_DIRECT
215 if 'O_DIRECT' in qemu_io('-f', 'raw', '-t', 'none', '-c', 'quit', hd_path[0]):
216 supports_direct = False
218 supports_direct = True
220 # Open the hd1 image passing all backing options
222 opts['backing'] = hd_opts(0)
223 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
224 self.assert_qmp(result, 'return', {})
225 original_graph = self.vm.qmp('query-named-block-nodes')
227 # We can reopen the image passing the same options
230 # Reopen in read-only mode
231 self.assert_qmp(self.get_node('hd1'), 'ro', False)
233 self.reopen(opts, {'read-only': True})
234 self.assert_qmp(self.get_node('hd1'), 'ro', True)
236 self.assert_qmp(self.get_node('hd1'), 'ro', False)
238 # Change the cache options
239 self.assert_qmp(self.get_node('hd1'), 'cache/writeback', True)
240 self.assert_qmp(self.get_node('hd1'), 'cache/direct', False)
241 self.assert_qmp(self.get_node('hd1'), 'cache/no-flush', False)
242 self.reopen(opts, {'cache': { 'direct': supports_direct, 'no-flush': True }})
243 self.assert_qmp(self.get_node('hd1'), 'cache/writeback', True)
244 self.assert_qmp(self.get_node('hd1'), 'cache/direct', supports_direct)
245 self.assert_qmp(self.get_node('hd1'), 'cache/no-flush', True)
247 # Reopen again with the original options
249 self.assert_qmp(self.get_node('hd1'), 'cache/writeback', True)
250 self.assert_qmp(self.get_node('hd1'), 'cache/direct', False)
251 self.assert_qmp(self.get_node('hd1'), 'cache/no-flush', False)
253 # Change 'detect-zeroes' and 'discard'
254 self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'off')
255 self.reopen(opts, {'detect-zeroes': 'on'})
256 self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'on')
257 self.reopen(opts, {'detect-zeroes': 'unmap'},
258 "setting detect-zeroes to unmap is not allowed " +
259 "without setting discard operation to unmap")
260 self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'on')
261 self.reopen(opts, {'detect-zeroes': 'unmap', 'discard': 'unmap'})
262 self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'unmap')
264 self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'off')
266 # Changing 'force-share' is currently not supported
267 self.reopen(opts, {'force-share': True}, "Cannot change the option 'force-share'")
269 # Change some qcow2-specific options
270 # No way to test for success other than checking the return message
271 if iotests.imgfmt == 'qcow2':
272 self.reopen(opts, {'l2-cache-entry-size': 128 * 1024},
273 "L2 cache entry size must be a power of two "+
274 "between 512 and the cluster size (65536)")
275 self.reopen(opts, {'l2-cache-size': 1024 * 1024,
276 'cache-size': 512 * 1024},
277 "l2-cache-size may not exceed cache-size")
278 self.reopen(opts, {'l2-cache-size': 4 * 1024 * 1024,
279 'refcount-cache-size': 4 * 1024 * 1024,
280 'l2-cache-entry-size': 32 * 1024})
281 self.reopen(opts, {'pass-discard-request': True})
283 # Check that nothing has changed since the beginning
284 # (from the parameters that we can check)
285 self.check_node_graph(original_graph)
287 # Check that the node names (other than the top-level one) are optional
288 del opts['file']['node-name']
289 del opts['backing']['node-name']
290 del opts['backing']['file']['node-name']
292 self.check_node_graph(original_graph)
294 # Reopen setting backing = null, this removes the backing image from the chain
295 self.reopen(opts, {'backing': None})
296 self.assert_qmp_absent(self.get_node('hd1'), 'image/backing-image')
298 # Open the 'hd0' image
299 result = self.vm.qmp('blockdev-add', conv_keys = False, **hd_opts(0))
300 self.assert_qmp(result, 'return', {})
302 # Reopen the hd1 image setting 'hd0' as its backing image
303 self.reopen(opts, {'backing': 'hd0'})
304 self.assert_qmp(self.get_node('hd1'), 'image/backing-image/filename', hd_path[0])
306 # Check that nothing has changed since the beginning
307 self.reopen(hd_opts(0), {'read-only': True})
308 self.check_node_graph(original_graph)
310 # The backing file (hd0) is now a reference, we cannot change backing.* anymore
311 self.reopen(opts, {}, "Cannot change the option 'backing.driver'")
313 # We can't remove 'hd0' while it's a backing image of 'hd1'
314 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
315 self.assert_qmp(result, 'error/class', 'GenericError')
316 self.assert_qmp(result, 'error/desc', "Node 'hd0' is busy: node is used as backing hd of 'hd1'")
318 # But we can remove both nodes if done in the proper order
319 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd1')
320 self.assert_qmp(result, 'return', {})
321 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
322 self.assert_qmp(result, 'return', {})
324 # Reopen a raw image and see the effect of changing the 'offset' option
325 def test_reopen_raw(self):
326 opts = {'driver': 'raw', 'node-name': 'hd0',
327 'file': { 'driver': 'file',
328 'filename': hd_path[0],
329 'node-name': 'hd0-file' } }
331 # First we create a 2MB raw file, and fill each half with a
333 qemu_img('create', '-f', 'raw', hd_path[0], '2M')
334 qemu_io('-f', 'raw', '-c', 'write -P 0xa0 0 1M', hd_path[0])
335 qemu_io('-f', 'raw', '-c', 'write -P 0xa1 1M 1M', hd_path[0])
337 # Open the raw file with QEMU
338 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
339 self.assert_qmp(result, 'return', {})
341 # Read 1MB from offset 0
342 self.run_qemu_io("hd0", "read -P 0xa0 0 1M")
344 # Reopen the image with a 1MB offset.
345 # Now the results are different
346 self.reopen(opts, {'offset': 1024*1024})
347 self.run_qemu_io("hd0", "read -P 0xa1 0 1M")
349 # Reopen again with the original options.
350 # We get the original results again
352 self.run_qemu_io("hd0", "read -P 0xa0 0 1M")
354 # Remove the block device
355 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
356 self.assert_qmp(result, 'return', {})
358 # Omitting an option should reset it to the default value, but if
359 # an option cannot be changed it shouldn't be possible to reset it
360 # to its default value either
361 def test_reset_default_values(self):
362 opts = {'driver': 'qcow2', 'node-name': 'hd0',
363 'file': { 'driver': 'file',
364 'filename': hd_path[0],
365 'x-check-cache-dropped': True, # This one can be changed
366 'locking': 'off', # This one can NOT be changed
367 'node-name': 'hd0-file' } }
369 # Open the file with QEMU
370 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
371 self.assert_qmp(result, 'return', {})
373 # file.x-check-cache-dropped can be changed...
374 self.reopen(opts, { 'file.x-check-cache-dropped': False })
375 # ...and dropped completely (resetting to the default value)
376 del opts['file']['x-check-cache-dropped']
379 # file.locking cannot be changed nor reset to the default value
380 self.reopen(opts, { 'file.locking': 'on' }, "Cannot change the option 'locking'")
381 del opts['file']['locking']
382 self.reopen(opts, {}, "Option 'locking' cannot be reset to its default value")
383 # But we can reopen it if we maintain its previous value
384 self.reopen(opts, { 'file.locking': 'off' })
386 # Remove the block device
387 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
388 self.assert_qmp(result, 'return', {})
390 # This test modifies the node graph a few times by changing the
391 # 'backing' option on reopen and verifies that the guest data that
392 # is read afterwards is consistent with the graph changes.
393 def test_io_with_graph_changes(self):
396 # Open hd0, hd1 and hd2 without any backing image
398 opts.append(hd_opts(i))
399 opts[i]['backing'] = None
400 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts[i])
401 self.assert_qmp(result, 'return', {})
404 self.run_qemu_io("hd0", "read -P 0xa0 0 1M")
405 self.run_qemu_io("hd0", "read -P 0 1M 1M")
406 self.run_qemu_io("hd0", "read -P 0 2M 1M")
409 self.reopen(opts[0], {'backing': 'hd1'})
411 self.run_qemu_io("hd0", "read -P 0xa0 0 1M")
412 self.run_qemu_io("hd0", "read -P 0xa1 1M 1M")
413 self.run_qemu_io("hd0", "read -P 0 2M 1M")
415 # hd1 <- hd0 , hd1 <- hd2
416 self.reopen(opts[2], {'backing': 'hd1'})
418 self.run_qemu_io("hd2", "read -P 0 0 1M")
419 self.run_qemu_io("hd2", "read -P 0xa1 1M 1M")
420 self.run_qemu_io("hd2", "read -P 0xa2 2M 1M")
423 self.reopen(opts[0], {'backing': 'hd2'})
425 self.run_qemu_io("hd0", "read -P 0xa0 0 1M")
426 self.run_qemu_io("hd0", "read -P 0xa1 1M 1M")
427 self.run_qemu_io("hd0", "read -P 0xa2 2M 1M")
430 self.reopen(opts[2], {'backing': None})
432 self.run_qemu_io("hd0", "read -P 0xa0 0 1M")
433 self.run_qemu_io("hd0", "read -P 0 1M 1M")
434 self.run_qemu_io("hd0", "read -P 0xa2 2M 1M")
437 self.reopen(opts[1], {'backing': 'hd2'})
438 self.reopen(opts[0], {'backing': 'hd1'})
440 self.run_qemu_io("hd0", "read -P 0xa0 0 1M")
441 self.run_qemu_io("hd0", "read -P 0xa1 1M 1M")
442 self.run_qemu_io("hd0", "read -P 0xa2 2M 1M")
444 # Illegal operation: hd2 is a child of hd1
445 self.reopen(opts[2], {'backing': 'hd1'},
446 "Making 'hd1' a backing file of 'hd2' would create a cycle")
448 # hd2 <- hd0, hd2 <- hd1
449 self.reopen(opts[0], {'backing': 'hd2'})
451 self.run_qemu_io("hd1", "read -P 0 0 1M")
452 self.run_qemu_io("hd1", "read -P 0xa1 1M 1M")
453 self.run_qemu_io("hd1", "read -P 0xa2 2M 1M")
455 # More illegal operations
456 self.reopen(opts[2], {'backing': 'hd1'},
457 "Making 'hd1' a backing file of 'hd2' would create a cycle")
458 self.reopen(opts[2], {'file': 'hd0-file'}, "Cannot change the option 'file'")
460 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd2')
461 self.assert_qmp(result, 'error/class', 'GenericError')
462 self.assert_qmp(result, 'error/desc', "Node 'hd2' is busy: node is used as backing hd of 'hd0'")
464 # hd1 doesn't have a backing file now
465 self.reopen(opts[1], {'backing': None})
467 self.run_qemu_io("hd1", "read -P 0 0 1M")
468 self.run_qemu_io("hd1", "read -P 0xa1 1M 1M")
469 self.run_qemu_io("hd1", "read -P 0 2M 1M")
471 # We can't remove the 'backing' option if the image has a
472 # default backing file
473 del opts[1]['backing']
474 self.reopen(opts[1], {}, "backing is missing for 'hd1'")
476 self.run_qemu_io("hd1", "read -P 0 0 1M")
477 self.run_qemu_io("hd1", "read -P 0xa1 1M 1M")
478 self.run_qemu_io("hd1", "read -P 0 2M 1M")
480 # This test verifies that we can't change the children of a block
481 # device during a reopen operation in a way that would create
482 # cycles in the node graph
483 @iotests.skip_if_unsupported(['blkverify'])
484 def test_graph_cycles(self):
487 # Open all three images without backing file
489 opts.append(hd_opts(i))
490 opts[i]['backing'] = None
491 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts[i])
492 self.assert_qmp(result, 'return', {})
494 # hd1 <- hd0, hd1 <- hd2
495 self.reopen(opts[0], {'backing': 'hd1'})
496 self.reopen(opts[2], {'backing': 'hd1'})
498 # Illegal: hd2 is backed by hd1
499 self.reopen(opts[1], {'backing': 'hd2'},
500 "Making 'hd2' a backing file of 'hd1' would create a cycle")
503 self.reopen(opts[2], {'backing': 'hd0'})
505 # Illegal: hd2 is backed by hd0, which is backed by hd1
506 self.reopen(opts[1], {'backing': 'hd2'},
507 "Making 'hd2' a backing file of 'hd1' would create a cycle")
509 # Illegal: hd1 cannot point to itself
510 self.reopen(opts[1], {'backing': 'hd1'},
511 "Making 'hd1' a backing file of 'hd1' would create a cycle")
513 # Remove all backing files
517 ##########################################
518 # Add a blkverify node using hd0 and hd1 #
519 ##########################################
520 bvopts = {'driver': 'blkverify',
524 result = self.vm.qmp('blockdev-add', conv_keys = False, **bvopts)
525 self.assert_qmp(result, 'return', {})
527 # blkverify doesn't currently allow reopening. TODO: implement this
528 self.reopen(bvopts, {}, "Block format 'blkverify' used by node 'bv'" +
529 " does not support reopening files")
531 # Illegal: hd0 is a child of the blkverify node
532 self.reopen(opts[0], {'backing': 'bv'},
533 "Making 'bv' a backing file of 'hd0' would create a cycle")
535 # Delete the blkverify node
536 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'bv')
537 self.assert_qmp(result, 'return', {})
539 # Misc reopen tests with different block drivers
540 @iotests.skip_if_unsupported(['quorum', 'throttle'])
541 def test_misc_drivers(self):
547 # Open all three images without backing file
548 opts['backing'] = None
549 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
550 self.assert_qmp(result, 'return', {})
552 opts = {'driver': 'quorum',
553 'node-name': 'quorum0',
554 'children': ['hd0', 'hd1', 'hd2'],
556 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
557 self.assert_qmp(result, 'return', {})
559 # Quorum doesn't currently allow reopening. TODO: implement this
560 self.reopen(opts, {}, "Block format 'quorum' used by node 'quorum0'" +
561 " does not support reopening files")
563 # You can't make quorum0 a backing file of hd0:
564 # hd0 is already a child of quorum0.
565 self.reopen(hd_opts(0), {'backing': 'quorum0'},
566 "Making 'quorum0' a backing file of 'hd0' would create a cycle")
569 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'quorum0')
570 self.assert_qmp(result, 'return', {})
572 # Delete hd0, hd1 and hd2
574 result = self.vm.qmp('blockdev-del', conv_keys = True,
575 node_name = 'hd%d' % i)
576 self.assert_qmp(result, 'return', {})
578 ######################
579 ###### blkdebug ######
580 ######################
581 opts = {'driver': 'blkdebug',
583 'config': '/dev/null',
585 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
586 self.assert_qmp(result, 'return', {})
588 # blkdebug allows reopening if we keep the same options
591 # but it currently does not allow changes
592 self.reopen(opts, {'image': 'hd1'}, "Cannot change the option 'image'")
593 self.reopen(opts, {'align': 33554432}, "Cannot change the option 'align'")
594 self.reopen(opts, {'config': '/non/existent'}, "Cannot change the option 'config'")
596 self.reopen(opts, {}, "Option 'config' cannot be reset to its default value")
598 # Delete the blkdebug node
599 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'bd')
600 self.assert_qmp(result, 'return', {})
605 opts = {'driver': 'null-co', 'node-name': 'root', 'size': 1024}
607 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
608 self.assert_qmp(result, 'return', {})
610 # 1 << 30 is the default value, but we cannot change it explicitly
611 self.reopen(opts, {'size': (1 << 30)}, "Cannot change the option 'size'")
613 # We cannot change 'size' back to its default value either
615 self.reopen(opts, {}, "Option 'size' cannot be reset to its default value")
617 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'root')
618 self.assert_qmp(result, 'return', {})
624 opts['file']['locking'] = 'on'
625 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
626 self.assert_qmp(result, 'return', {})
628 # 'locking' cannot be changed
629 del opts['file']['locking']
630 self.reopen(opts, {'file.locking': 'on'})
631 self.reopen(opts, {'file.locking': 'off'}, "Cannot change the option 'locking'")
632 self.reopen(opts, {}, "Option 'locking' cannot be reset to its default value")
634 # Trying to reopen the 'file' node directly does not make a difference
636 self.reopen(opts, {'locking': 'on'})
637 self.reopen(opts, {'locking': 'off'}, "Cannot change the option 'locking'")
638 self.reopen(opts, {}, "Option 'locking' cannot be reset to its default value")
640 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
641 self.assert_qmp(result, 'return', {})
643 ######################
644 ###### throttle ######
645 ######################
646 opts = { 'qom-type': 'throttle-group', 'id': 'group0',
647 'props': { 'limits': { 'iops-total': 1000 } } }
648 result = self.vm.qmp('object-add', conv_keys = False, **opts)
649 self.assert_qmp(result, 'return', {})
651 opts = { 'qom-type': 'throttle-group', 'id': 'group1',
652 'props': { 'limits': { 'iops-total': 2000 } } }
653 result = self.vm.qmp('object-add', conv_keys = False, **opts)
654 self.assert_qmp(result, 'return', {})
656 # Add a throttle filter with group = group0
657 opts = { 'driver': 'throttle', 'node-name': 'throttle0',
658 'throttle-group': 'group0', 'file': hd_opts(0) }
659 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
660 self.assert_qmp(result, 'return', {})
662 # We can reopen it if we keep the same options
665 # We can also reopen if 'file' is a reference to the child
666 self.reopen(opts, {'file': 'hd0'})
669 self.reopen(opts, {'throttle-group': 'notfound'}, "Throttle group 'notfound' does not exist")
671 # But it's possible to change the group to group1
672 self.reopen(opts, {'throttle-group': 'group1'})
674 # Now group1 is in use, it cannot be deleted
675 result = self.vm.qmp('object-del', id = 'group1')
676 self.assert_qmp(result, 'error/class', 'GenericError')
677 self.assert_qmp(result, 'error/desc', "object 'group1' is in use, can not be deleted")
679 # Default options, this switches the group back to group0
682 # So now we cannot delete group0
683 result = self.vm.qmp('object-del', id = 'group0')
684 self.assert_qmp(result, 'error/class', 'GenericError')
685 self.assert_qmp(result, 'error/desc', "object 'group0' is in use, can not be deleted")
687 # But group1 is free this time, and it can be deleted
688 result = self.vm.qmp('object-del', id = 'group1')
689 self.assert_qmp(result, 'return', {})
691 # Let's delete the filter node
692 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'throttle0')
693 self.assert_qmp(result, 'return', {})
695 # And we can finally get rid of group0
696 result = self.vm.qmp('object-del', id = 'group0')
697 self.assert_qmp(result, 'return', {})
699 # If an image has a backing file then the 'backing' option must be
700 # passed on reopen. We don't allow leaving the option out in this
701 # case because it's unclear what the correct semantics would be.
702 def test_missing_backing_options_1(self):
705 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
706 self.assert_qmp(result, 'return', {})
710 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
711 self.assert_qmp(result, 'return', {})
713 # hd0 has no backing file: we can omit the 'backing' option
717 self.reopen(opts, {'backing': 'hd2'})
719 # hd0 has a backing file: we must set the 'backing' option
720 self.reopen(opts, {}, "backing is missing for 'hd0'")
722 # hd2 can't be removed because it's the backing file of hd0
723 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd2')
724 self.assert_qmp(result, 'error/class', 'GenericError')
725 self.assert_qmp(result, 'error/desc', "Node 'hd2' is busy: node is used as backing hd of 'hd0'")
727 # Detach hd2 from hd0.
728 self.reopen(opts, {'backing': None})
730 # Without a backing file, we can omit 'backing' again
733 # Remove both hd0 and hd2
734 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
735 self.assert_qmp(result, 'return', {})
737 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd2')
738 self.assert_qmp(result, 'return', {})
740 # If an image has default backing file (as part of its metadata)
741 # then the 'backing' option must be passed on reopen. We don't
742 # allow leaving the option out in this case because it's unclear
743 # what the correct semantics would be.
744 def test_missing_backing_options_2(self):
746 # (hd0 is hd1's default backing file)
748 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
749 self.assert_qmp(result, 'return', {})
751 # hd1 has a backing file: we can't omit the 'backing' option
752 self.reopen(opts, {}, "backing is missing for 'hd1'")
754 # Let's detach the backing file
755 self.reopen(opts, {'backing': None})
757 # No backing file attached to hd1 now, but we still can't omit the 'backing' option
758 self.reopen(opts, {}, "backing is missing for 'hd1'")
760 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd1')
761 self.assert_qmp(result, 'return', {})
763 # Test that making 'backing' a reference to an existing child
764 # keeps its current options
765 def test_backing_reference(self):
768 opts['backing'] = hd_opts(1)
769 opts['backing']['backing'] = hd_opts(2)
770 # Enable 'detect-zeroes' on all three nodes
771 opts['detect-zeroes'] = 'on'
772 opts['backing']['detect-zeroes'] = 'on'
773 opts['backing']['backing']['detect-zeroes'] = 'on'
774 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
775 self.assert_qmp(result, 'return', {})
777 # Reopen the chain passing the minimum amount of required options.
778 # By making 'backing' a reference to hd1 (instead of a sub-dict)
779 # we tell QEMU to keep its current set of options.
780 opts = {'driver': iotests.imgfmt,
786 # This has reset 'detect-zeroes' on hd0, but not on hd1 and hd2.
787 self.assert_qmp(self.get_node('hd0'), 'detect_zeroes', 'off')
788 self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'on')
789 self.assert_qmp(self.get_node('hd2'), 'detect_zeroes', 'on')
791 # Test what happens if the graph changes due to other operations
792 # such as block-stream
793 def test_block_stream_1(self):
796 opts['backing'] = hd_opts(1)
797 opts['backing']['backing'] = None
798 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
799 self.assert_qmp(result, 'return', {})
801 # Stream hd1 into hd0 and wait until it's done
802 result = self.vm.qmp('block-stream', conv_keys = True, job_id = 'stream0', device = 'hd0')
803 self.assert_qmp(result, 'return', {})
804 self.wait_until_completed(drive = 'stream0')
806 # Now we have only hd0
807 self.assertEqual(self.get_node('hd1'), None)
809 # We have backing.* options but there's no backing file anymore
810 self.reopen(opts, {}, "Cannot change the option 'backing.driver'")
812 # If we remove the 'backing' option then we can reopen hd0 just fine
816 # We can also reopen hd0 if we set 'backing' to null
817 self.reopen(opts, {'backing': None})
819 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
820 self.assert_qmp(result, 'return', {})
822 # Another block_stream test
823 def test_block_stream_2(self):
826 opts['backing'] = hd_opts(1)
827 opts['backing']['backing'] = hd_opts(2)
828 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
829 self.assert_qmp(result, 'return', {})
831 # Stream hd1 into hd0 and wait until it's done
832 result = self.vm.qmp('block-stream', conv_keys = True, job_id = 'stream0',
833 device = 'hd0', base_node = 'hd2')
834 self.assert_qmp(result, 'return', {})
835 self.wait_until_completed(drive = 'stream0')
837 # The chain is hd2 <- hd0 now. hd1 is missing
838 self.assertEqual(self.get_node('hd1'), None)
840 # The backing options in the dict were meant for hd1, but we cannot
841 # use them with hd2 because hd1 had a backing file while hd2 does not.
842 self.reopen(opts, {}, "Cannot change the option 'backing.driver'")
844 # If we remove hd1's options from the dict then things work fine
845 opts['backing'] = opts['backing']['backing']
848 # We can also reopen hd0 if we use a reference to the backing file
849 self.reopen(opts, {'backing': 'hd2'})
851 # But we cannot leave the option out
853 self.reopen(opts, {}, "backing is missing for 'hd0'")
855 # Now we can delete hd0 (and hd2)
856 result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
857 self.assert_qmp(result, 'return', {})
858 self.assertEqual(self.get_node('hd2'), None)
860 # Reopen the chain during a block-stream job (from hd1 to hd0)
861 def test_block_stream_3(self):
864 opts['backing'] = hd_opts(1)
865 opts['backing']['backing'] = hd_opts(2)
866 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
867 self.assert_qmp(result, 'return', {})
870 result = self.vm.qmp('block-stream', conv_keys = True, job_id = 'stream0',
871 device = 'hd0', base_node = 'hd2',
872 auto_finalize = False)
873 self.assert_qmp(result, 'return', {})
875 # We can remove hd2 while the stream job is ongoing
876 opts['backing']['backing'] = None
877 self.reopen(opts, {})
879 # We can't remove hd1 while the stream job is ongoing
880 opts['backing'] = None
881 self.reopen(opts, {}, "Cannot change 'backing' link from 'hd0' to 'hd1'")
883 self.vm.run_job('stream0', auto_finalize = False, auto_dismiss = True)
885 # Reopen the chain during a block-stream job (from hd2 to hd1)
886 def test_block_stream_4(self):
889 opts['backing'] = hd_opts(1)
890 opts['backing']['backing'] = hd_opts(2)
891 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
892 self.assert_qmp(result, 'return', {})
895 result = self.vm.qmp('block-stream', conv_keys = True, job_id = 'stream0',
896 device = 'hd1', filter_node_name='cor',
897 auto_finalize = False)
898 self.assert_qmp(result, 'return', {})
900 # We can't reopen with the original options because there is a filter
901 # inserted by stream job above hd1.
902 self.reopen(opts, {},
903 "Cannot change the option 'backing.backing.file.node-name'")
905 # We can't reopen hd1 to read-only, as block-stream requires it to be
907 self.reopen(opts['backing'], {'read-only': True},
908 "Cannot make block node read-only, there is a writer on it")
910 # We can't remove hd2 while the stream job is ongoing
911 opts['backing']['backing'] = None
912 self.reopen(opts['backing'], {'read-only': False},
913 "Cannot change 'backing' link from 'hd1' to 'hd2'")
915 # We can detach hd1 from hd0 because it doesn't affect the stream job
916 opts['backing'] = None
919 self.vm.run_job('stream0', auto_finalize = False, auto_dismiss = True)
921 # Reopen the chain during a block-commit job (from hd0 to hd2)
922 def test_block_commit_1(self):
925 opts['backing'] = hd_opts(1)
926 opts['backing']['backing'] = hd_opts(2)
927 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
928 self.assert_qmp(result, 'return', {})
930 result = self.vm.qmp('block-commit', conv_keys = True, job_id = 'commit0',
932 self.assert_qmp(result, 'return', {})
934 # We can't remove hd2 while the commit job is ongoing
935 opts['backing']['backing'] = None
936 self.reopen(opts, {}, "Cannot change 'backing' link from 'hd1' to 'hd2'")
938 # We can't remove hd1 while the commit job is ongoing
939 opts['backing'] = None
940 self.reopen(opts, {}, "Cannot change 'backing' link from 'hd0' to 'hd1'")
942 event = self.vm.event_wait(name='BLOCK_JOB_READY')
943 self.assert_qmp(event, 'data/device', 'commit0')
944 self.assert_qmp(event, 'data/type', 'commit')
945 self.assert_qmp_absent(event, 'data/error')
947 result = self.vm.qmp('block-job-complete', device='commit0')
948 self.assert_qmp(result, 'return', {})
950 self.wait_until_completed(drive = 'commit0')
952 # Reopen the chain during a block-commit job (from hd1 to hd2)
953 def test_block_commit_2(self):
956 opts['backing'] = hd_opts(1)
957 opts['backing']['backing'] = hd_opts(2)
958 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
959 self.assert_qmp(result, 'return', {})
961 result = self.vm.qmp('block-commit', conv_keys = True, job_id = 'commit0',
962 device = 'hd0', top_node = 'hd1',
963 auto_finalize = False)
964 self.assert_qmp(result, 'return', {})
966 # We can't remove hd2 while the commit job is ongoing
967 opts['backing']['backing'] = None
968 self.reopen(opts, {}, "Cannot change the option 'backing.driver'")
970 # We can't remove hd1 while the commit job is ongoing
971 opts['backing'] = None
972 self.reopen(opts, {}, "Cannot change backing link if 'hd0' has an implicit backing file")
975 self.vm.run_job('commit0', auto_finalize = False, auto_dismiss = True)
977 self.assert_qmp(self.get_node('hd0'), 'ro', False)
978 self.assertEqual(self.get_node('hd1'), None)
979 self.assert_qmp(self.get_node('hd2'), 'ro', True)
981 def run_test_iothreads(self, iothread_a, iothread_b, errmsg = None):
983 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
984 self.assert_qmp(result, 'return', {})
987 result = self.vm.qmp('blockdev-add', conv_keys = False, **opts2)
988 self.assert_qmp(result, 'return', {})
990 result = self.vm.qmp('object-add', qom_type='iothread', id='iothread0')
991 self.assert_qmp(result, 'return', {})
993 result = self.vm.qmp('object-add', qom_type='iothread', id='iothread1')
994 self.assert_qmp(result, 'return', {})
996 result = self.vm.qmp('device_add', driver='virtio-scsi', id='scsi0',
998 self.assert_qmp(result, 'return', {})
1000 result = self.vm.qmp('device_add', driver='virtio-scsi', id='scsi1',
1001 iothread=iothread_b)
1002 self.assert_qmp(result, 'return', {})
1005 result = self.vm.qmp('device_add', driver='scsi-hd', drive='hd0',
1006 share_rw=True, bus="scsi0.0")
1007 self.assert_qmp(result, 'return', {})
1010 result = self.vm.qmp('device_add', driver='scsi-hd', drive='hd2',
1011 share_rw=True, bus="scsi1.0")
1012 self.assert_qmp(result, 'return', {})
1014 # Attaching the backing file may or may not work
1015 self.reopen(opts, {'backing': 'hd2'}, errmsg)
1017 # But removing the backing file should always work
1018 self.reopen(opts, {'backing': None})
1022 # We don't allow setting a backing file that uses a different AioContext if
1023 # neither of them can switch to the other AioContext
1024 def test_iothreads_error(self):
1025 self.run_test_iothreads('iothread0', 'iothread1',
1026 "Cannot change iothread of active block backend")
1028 def test_iothreads_compatible_users(self):
1029 self.run_test_iothreads('iothread0', 'iothread0')
1031 def test_iothreads_switch_backing(self):
1032 self.run_test_iothreads('iothread0', None)
1034 def test_iothreads_switch_overlay(self):
1035 self.run_test_iothreads(None, 'iothread0')
1037 if __name__ == '__main__':
1038 iotests.activate_logging()
1039 iotests.main(supported_fmts=["qcow2"],
1040 supported_protocols=["file"])