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], 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()
57 self
.check_qemu_io_errors()
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
73 def check_qemu_io_errors(self
):
74 self
.assertFalse(self
.vm
.is_running())
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
):
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'
94 # If key has the form "foo.bar" then we need to do
95 # opts["foo"]["bar"] = value, not opts["foo.bar"] = value
97 while key
.find('.') != -1:
98 [prefix
, key
] = key
.split('.', 1)
99 subdict
= opts
[prefix
]
102 result
= self
.vm
.qmp('x-blockdev-reopen', conv_keys
= False, **opts
)
104 self
.assert_qmp(result
, 'error/class', 'GenericError')
105 self
.assert_qmp(result
, 'error/desc', errmsg
)
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
:
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)
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
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
)
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)
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]}}
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
)
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
216 supports_direct
= True
218 # Open the hd1 image passing all backing options
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
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)
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
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')
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']
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
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
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']
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
):
394 # Open hd0, hd1 and hd2 without any backing image
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', {})
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")
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")
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")
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")
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 def test_graph_cycles(self
):
484 # Open all three images without backing file
486 opts
.append(hd_opts(i
))
487 opts
[i
]['backing'] = None
488 result
= self
.vm
.qmp('blockdev-add', conv_keys
= False, **opts
[i
])
489 self
.assert_qmp(result
, 'return', {})
491 # hd1 <- hd0, hd1 <- hd2
492 self
.reopen(opts
[0], {'backing': 'hd1'})
493 self
.reopen(opts
[2], {'backing': 'hd1'})
495 # Illegal: hd2 is backed by hd1
496 self
.reopen(opts
[1], {'backing': 'hd2'},
497 "Making 'hd2' a backing file of 'hd1' would create a cycle")
500 self
.reopen(opts
[2], {'backing': 'hd0'})
502 # Illegal: hd2 is backed by hd0, which is backed by hd1
503 self
.reopen(opts
[1], {'backing': 'hd2'},
504 "Making 'hd2' a backing file of 'hd1' would create a cycle")
506 # Illegal: hd1 cannot point to itself
507 self
.reopen(opts
[1], {'backing': 'hd1'},
508 "Making 'hd1' a backing file of 'hd1' would create a cycle")
510 # Remove all backing files
514 ##########################################
515 # Add a blkverify node using hd0 and hd1 #
516 ##########################################
517 bvopts
= {'driver': 'blkverify',
521 result
= self
.vm
.qmp('blockdev-add', conv_keys
= False, **bvopts
)
522 self
.assert_qmp(result
, 'return', {})
524 # blkverify doesn't currently allow reopening. TODO: implement this
525 self
.reopen(bvopts
, {}, "Block format 'blkverify' used by node 'bv'" +
526 " does not support reopening files")
528 # Illegal: hd0 is a child of the blkverify node
529 self
.reopen(opts
[0], {'backing': 'bv'},
530 "Making 'bv' a backing file of 'hd0' would create a cycle")
532 # Delete the blkverify node
533 result
= self
.vm
.qmp('blockdev-del', conv_keys
= True, node_name
= 'bv')
534 self
.assert_qmp(result
, 'return', {})
536 # Misc reopen tests with different block drivers
537 def test_misc_drivers(self
):
543 # Open all three images without backing file
544 opts
['backing'] = None
545 result
= self
.vm
.qmp('blockdev-add', conv_keys
= False, **opts
)
546 self
.assert_qmp(result
, 'return', {})
548 opts
= {'driver': 'quorum',
549 'node-name': 'quorum0',
550 'children': ['hd0', 'hd1', 'hd2'],
552 result
= self
.vm
.qmp('blockdev-add', conv_keys
= False, **opts
)
553 self
.assert_qmp(result
, 'return', {})
555 # Quorum doesn't currently allow reopening. TODO: implement this
556 self
.reopen(opts
, {}, "Block format 'quorum' used by node 'quorum0'" +
557 " does not support reopening files")
559 # You can't make quorum0 a backing file of hd0:
560 # hd0 is already a child of quorum0.
561 self
.reopen(hd_opts(0), {'backing': 'quorum0'},
562 "Making 'quorum0' a backing file of 'hd0' would create a cycle")
565 result
= self
.vm
.qmp('blockdev-del', conv_keys
= True, node_name
= 'quorum0')
566 self
.assert_qmp(result
, 'return', {})
568 # Delete hd0, hd1 and hd2
570 result
= self
.vm
.qmp('blockdev-del', conv_keys
= True,
571 node_name
= 'hd%d' % i
)
572 self
.assert_qmp(result
, 'return', {})
574 ######################
575 ###### blkdebug ######
576 ######################
577 opts
= {'driver': 'blkdebug',
579 'config': '/dev/null',
581 result
= self
.vm
.qmp('blockdev-add', conv_keys
= False, **opts
)
582 self
.assert_qmp(result
, 'return', {})
584 # blkdebug allows reopening if we keep the same options
587 # but it currently does not allow changes
588 self
.reopen(opts
, {'image': 'hd1'}, "Cannot change the option 'image'")
589 self
.reopen(opts
, {'align': 33554432}, "Cannot change the option 'align'")
590 self
.reopen(opts
, {'config': '/non/existent'}, "Cannot change the option 'config'")
592 self
.reopen(opts
, {}, "Option 'config' cannot be reset to its default value")
594 # Delete the blkdebug node
595 result
= self
.vm
.qmp('blockdev-del', conv_keys
= True, node_name
= 'bd')
596 self
.assert_qmp(result
, 'return', {})
601 opts
= {'driver': 'null-aio', 'node-name': 'root', 'size': 1024}
603 result
= self
.vm
.qmp('blockdev-add', conv_keys
= False, **opts
)
604 self
.assert_qmp(result
, 'return', {})
606 # 1 << 30 is the default value, but we cannot change it explicitly
607 self
.reopen(opts
, {'size': (1 << 30)}, "Cannot change the option 'size'")
609 # We cannot change 'size' back to its default value either
611 self
.reopen(opts
, {}, "Option 'size' cannot be reset to its default value")
613 result
= self
.vm
.qmp('blockdev-del', conv_keys
= True, node_name
= 'root')
614 self
.assert_qmp(result
, 'return', {})
620 opts
['file']['locking'] = 'on'
621 result
= self
.vm
.qmp('blockdev-add', conv_keys
= False, **opts
)
622 self
.assert_qmp(result
, 'return', {})
624 # 'locking' cannot be changed
625 del opts
['file']['locking']
626 self
.reopen(opts
, {'file.locking': 'on'})
627 self
.reopen(opts
, {'file.locking': 'off'}, "Cannot change the option 'locking'")
628 self
.reopen(opts
, {}, "Option 'locking' cannot be reset to its default value")
630 # Trying to reopen the 'file' node directly does not make a difference
632 self
.reopen(opts
, {'locking': 'on'})
633 self
.reopen(opts
, {'locking': 'off'}, "Cannot change the option 'locking'")
634 self
.reopen(opts
, {}, "Option 'locking' cannot be reset to its default value")
636 result
= self
.vm
.qmp('blockdev-del', conv_keys
= True, node_name
= 'hd0')
637 self
.assert_qmp(result
, 'return', {})
639 ######################
640 ###### throttle ######
641 ######################
642 opts
= { 'qom-type': 'throttle-group', 'id': 'group0',
643 'props': { 'limits': { 'iops-total': 1000 } } }
644 result
= self
.vm
.qmp('object-add', conv_keys
= False, **opts
)
645 self
.assert_qmp(result
, 'return', {})
647 opts
= { 'qom-type': 'throttle-group', 'id': 'group1',
648 'props': { 'limits': { 'iops-total': 2000 } } }
649 result
= self
.vm
.qmp('object-add', conv_keys
= False, **opts
)
650 self
.assert_qmp(result
, 'return', {})
652 # Add a throttle filter with group = group0
653 opts
= { 'driver': 'throttle', 'node-name': 'throttle0',
654 'throttle-group': 'group0', 'file': hd_opts(0) }
655 result
= self
.vm
.qmp('blockdev-add', conv_keys
= False, **opts
)
656 self
.assert_qmp(result
, 'return', {})
658 # We can reopen it if we keep the same options
661 # We can also reopen if 'file' is a reference to the child
662 self
.reopen(opts
, {'file': 'hd0'})
665 self
.reopen(opts
, {'throttle-group': 'notfound'}, "Throttle group 'notfound' does not exist")
667 # But it's possible to change the group to group1
668 self
.reopen(opts
, {'throttle-group': 'group1'})
670 # Now group1 is in use, it cannot be deleted
671 result
= self
.vm
.qmp('object-del', id = 'group1')
672 self
.assert_qmp(result
, 'error/class', 'GenericError')
673 self
.assert_qmp(result
, 'error/desc', "object 'group1' is in use, can not be deleted")
675 # Default options, this switches the group back to group0
678 # So now we cannot delete group0
679 result
= self
.vm
.qmp('object-del', id = 'group0')
680 self
.assert_qmp(result
, 'error/class', 'GenericError')
681 self
.assert_qmp(result
, 'error/desc', "object 'group0' is in use, can not be deleted")
683 # But group1 is free this time, and it can be deleted
684 result
= self
.vm
.qmp('object-del', id = 'group1')
685 self
.assert_qmp(result
, 'return', {})
687 # Let's delete the filter node
688 result
= self
.vm
.qmp('blockdev-del', conv_keys
= True, node_name
= 'throttle0')
689 self
.assert_qmp(result
, 'return', {})
691 # And we can finally get rid of group0
692 result
= self
.vm
.qmp('object-del', id = 'group0')
693 self
.assert_qmp(result
, 'return', {})
695 # If an image has a backing file then the 'backing' option must be
696 # passed on reopen. We don't allow leaving the option out in this
697 # case because it's unclear what the correct semantics would be.
698 def test_missing_backing_options_1(self
):
701 result
= self
.vm
.qmp('blockdev-add', conv_keys
= False, **opts
)
702 self
.assert_qmp(result
, 'return', {})
706 result
= self
.vm
.qmp('blockdev-add', conv_keys
= False, **opts
)
707 self
.assert_qmp(result
, 'return', {})
709 # hd0 has no backing file: we can omit the 'backing' option
713 self
.reopen(opts
, {'backing': 'hd2'})
715 # hd0 has a backing file: we must set the 'backing' option
716 self
.reopen(opts
, {}, "backing is missing for 'hd0'")
718 # hd2 can't be removed because it's the backing file of hd0
719 result
= self
.vm
.qmp('blockdev-del', conv_keys
= True, node_name
= 'hd2')
720 self
.assert_qmp(result
, 'error/class', 'GenericError')
721 self
.assert_qmp(result
, 'error/desc', "Node 'hd2' is busy: node is used as backing hd of 'hd0'")
723 # Detach hd2 from hd0.
724 self
.reopen(opts
, {'backing': None})
725 self
.reopen(opts
, {}, "backing is missing for 'hd0'")
727 # Remove both hd0 and hd2
728 result
= self
.vm
.qmp('blockdev-del', conv_keys
= True, node_name
= 'hd0')
729 self
.assert_qmp(result
, 'return', {})
731 result
= self
.vm
.qmp('blockdev-del', conv_keys
= True, node_name
= 'hd2')
732 self
.assert_qmp(result
, 'return', {})
734 # If an image has default backing file (as part of its metadata)
735 # then the 'backing' option must be passed on reopen. We don't
736 # allow leaving the option out in this case because it's unclear
737 # what the correct semantics would be.
738 def test_missing_backing_options_2(self
):
740 # (hd0 is hd1's default backing file)
742 result
= self
.vm
.qmp('blockdev-add', conv_keys
= False, **opts
)
743 self
.assert_qmp(result
, 'return', {})
745 # hd1 has a backing file: we can't omit the 'backing' option
746 self
.reopen(opts
, {}, "backing is missing for 'hd1'")
748 # Let's detach the backing file
749 self
.reopen(opts
, {'backing': None})
751 # No backing file attached to hd1 now, but we still can't omit the 'backing' option
752 self
.reopen(opts
, {}, "backing is missing for 'hd1'")
754 result
= self
.vm
.qmp('blockdev-del', conv_keys
= True, node_name
= 'hd1')
755 self
.assert_qmp(result
, 'return', {})
757 # Test that making 'backing' a reference to an existing child
758 # keeps its current options
759 def test_backing_reference(self
):
762 opts
['backing'] = hd_opts(1)
763 opts
['backing']['backing'] = hd_opts(2)
764 # Enable 'detect-zeroes' on all three nodes
765 opts
['detect-zeroes'] = 'on'
766 opts
['backing']['detect-zeroes'] = 'on'
767 opts
['backing']['backing']['detect-zeroes'] = 'on'
768 result
= self
.vm
.qmp('blockdev-add', conv_keys
= False, **opts
)
769 self
.assert_qmp(result
, 'return', {})
771 # Reopen the chain passing the minimum amount of required options.
772 # By making 'backing' a reference to hd1 (instead of a sub-dict)
773 # we tell QEMU to keep its current set of options.
774 opts
= {'driver': iotests
.imgfmt
,
780 # This has reset 'detect-zeroes' on hd0, but not on hd1 and hd2.
781 self
.assert_qmp(self
.get_node('hd0'), 'detect_zeroes', 'off')
782 self
.assert_qmp(self
.get_node('hd1'), 'detect_zeroes', 'on')
783 self
.assert_qmp(self
.get_node('hd2'), 'detect_zeroes', 'on')
785 # Test what happens if the graph changes due to other operations
786 # such as block-stream
787 def test_block_stream_1(self
):
790 opts
['backing'] = hd_opts(1)
791 opts
['backing']['backing'] = None
792 result
= self
.vm
.qmp('blockdev-add', conv_keys
= False, **opts
)
793 self
.assert_qmp(result
, 'return', {})
795 # Stream hd1 into hd0 and wait until it's done
796 result
= self
.vm
.qmp('block-stream', conv_keys
= True, job_id
= 'stream0', device
= 'hd0')
797 self
.assert_qmp(result
, 'return', {})
798 self
.wait_until_completed(drive
= 'stream0')
800 # Now we have only hd0
801 self
.assertEqual(self
.get_node('hd1'), None)
803 # We have backing.* options but there's no backing file anymore
804 self
.reopen(opts
, {}, "Cannot change the option 'backing.driver'")
806 # If we remove the 'backing' option then we can reopen hd0 just fine
810 # We can also reopen hd0 if we set 'backing' to null
811 self
.reopen(opts
, {'backing': None})
813 result
= self
.vm
.qmp('blockdev-del', conv_keys
= True, node_name
= 'hd0')
814 self
.assert_qmp(result
, 'return', {})
816 # Another block_stream test
817 def test_block_stream_2(self
):
820 opts
['backing'] = hd_opts(1)
821 opts
['backing']['backing'] = hd_opts(2)
822 result
= self
.vm
.qmp('blockdev-add', conv_keys
= False, **opts
)
823 self
.assert_qmp(result
, 'return', {})
825 # Stream hd1 into hd0 and wait until it's done
826 result
= self
.vm
.qmp('block-stream', conv_keys
= True, job_id
= 'stream0',
827 device
= 'hd0', base_node
= 'hd2')
828 self
.assert_qmp(result
, 'return', {})
829 self
.wait_until_completed(drive
= 'stream0')
831 # The chain is hd2 <- hd0 now. hd1 is missing
832 self
.assertEqual(self
.get_node('hd1'), None)
834 # The backing options in the dict were meant for hd1, but we cannot
835 # use them with hd2 because hd1 had a backing file while hd2 does not.
836 self
.reopen(opts
, {}, "Cannot change the option 'backing.driver'")
838 # If we remove hd1's options from the dict then things work fine
839 opts
['backing'] = opts
['backing']['backing']
842 # We can also reopen hd0 if we use a reference to the backing file
843 self
.reopen(opts
, {'backing': 'hd2'})
845 # But we cannot leave the option out
847 self
.reopen(opts
, {}, "backing is missing for 'hd0'")
849 # Now we can delete hd0 (and hd2)
850 result
= self
.vm
.qmp('blockdev-del', conv_keys
= True, node_name
= 'hd0')
851 self
.assert_qmp(result
, 'return', {})
852 self
.assertEqual(self
.get_node('hd2'), None)
854 # Reopen the chain during a block-stream job (from hd1 to hd0)
855 def test_block_stream_3(self
):
858 opts
['backing'] = hd_opts(1)
859 opts
['backing']['backing'] = hd_opts(2)
860 result
= self
.vm
.qmp('blockdev-add', conv_keys
= False, **opts
)
861 self
.assert_qmp(result
, 'return', {})
864 result
= self
.vm
.qmp('block-stream', conv_keys
= True, job_id
= 'stream0',
865 device
= 'hd0', base_node
= 'hd2',
866 auto_finalize
= False)
867 self
.assert_qmp(result
, 'return', {})
869 # We can remove hd2 while the stream job is ongoing
870 opts
['backing']['backing'] = None
871 self
.reopen(opts
, {})
873 # We can't remove hd1 while the stream job is ongoing
874 opts
['backing'] = None
875 self
.reopen(opts
, {}, "Cannot change 'backing' link from 'hd0' to 'hd1'")
877 self
.vm
.run_job('stream0', auto_finalize
= False, auto_dismiss
= True)
879 # Reopen the chain during a block-stream job (from hd2 to hd1)
880 def test_block_stream_4(self
):
883 opts
['backing'] = hd_opts(1)
884 opts
['backing']['backing'] = hd_opts(2)
885 result
= self
.vm
.qmp('blockdev-add', conv_keys
= False, **opts
)
886 self
.assert_qmp(result
, 'return', {})
889 result
= self
.vm
.qmp('block-stream', conv_keys
= True, job_id
= 'stream0',
890 device
= 'hd1', auto_finalize
= False)
891 self
.assert_qmp(result
, 'return', {})
893 # We can't reopen with the original options because that would
894 # make hd1 read-only and block-stream requires it to be read-write
895 # (Which error message appears depends on whether the stream job is
896 # already done with copying at this point.)
897 self
.reopen(opts
, {},
898 ["Can't set node 'hd1' to r/o with copy-on-read enabled",
899 "Cannot make block node read-only, there is a writer on it"])
901 # We can't remove hd2 while the stream job is ongoing
902 opts
['backing']['backing'] = None
903 self
.reopen(opts
, {'backing.read-only': False}, "Cannot change 'backing' link from 'hd1' to 'hd2'")
905 # We can detach hd1 from hd0 because it doesn't affect the stream job
906 opts
['backing'] = None
909 self
.vm
.run_job('stream0', auto_finalize
= False, auto_dismiss
= True)
911 # Reopen the chain during a block-commit job (from hd0 to hd2)
912 def test_block_commit_1(self
):
915 opts
['backing'] = hd_opts(1)
916 opts
['backing']['backing'] = hd_opts(2)
917 result
= self
.vm
.qmp('blockdev-add', conv_keys
= False, **opts
)
918 self
.assert_qmp(result
, 'return', {})
920 result
= self
.vm
.qmp('block-commit', conv_keys
= True, job_id
= 'commit0',
922 self
.assert_qmp(result
, 'return', {})
924 # We can't remove hd2 while the commit job is ongoing
925 opts
['backing']['backing'] = None
926 self
.reopen(opts
, {}, "Cannot change 'backing' link from 'hd1' to 'hd2'")
928 # We can't remove hd1 while the commit job is ongoing
929 opts
['backing'] = None
930 self
.reopen(opts
, {}, "Cannot change 'backing' link from 'hd0' to 'hd1'")
932 event
= self
.vm
.event_wait(name
='BLOCK_JOB_READY')
933 self
.assert_qmp(event
, 'data/device', 'commit0')
934 self
.assert_qmp(event
, 'data/type', 'commit')
935 self
.assert_qmp_absent(event
, 'data/error')
937 result
= self
.vm
.qmp('block-job-complete', device
='commit0')
938 self
.assert_qmp(result
, 'return', {})
940 self
.wait_until_completed(drive
= 'commit0')
942 # Reopen the chain during a block-commit job (from hd1 to hd2)
943 def test_block_commit_2(self
):
946 opts
['backing'] = hd_opts(1)
947 opts
['backing']['backing'] = hd_opts(2)
948 result
= self
.vm
.qmp('blockdev-add', conv_keys
= False, **opts
)
949 self
.assert_qmp(result
, 'return', {})
951 result
= self
.vm
.qmp('block-commit', conv_keys
= True, job_id
= 'commit0',
952 device
= 'hd0', top_node
= 'hd1',
953 auto_finalize
= False)
954 self
.assert_qmp(result
, 'return', {})
956 # We can't remove hd2 while the commit job is ongoing
957 opts
['backing']['backing'] = None
958 self
.reopen(opts
, {}, "Cannot change the option 'backing.driver'")
960 # We can't remove hd1 while the commit job is ongoing
961 opts
['backing'] = None
962 self
.reopen(opts
, {}, "Cannot change backing link if 'hd0' has an implicit backing file")
965 self
.vm
.run_job('commit0', auto_finalize
= False, auto_dismiss
= True)
967 self
.assert_qmp(self
.get_node('hd0'), 'ro', False)
968 self
.assertEqual(self
.get_node('hd1'), None)
969 self
.assert_qmp(self
.get_node('hd2'), 'ro', True)
971 # We don't allow setting a backing file that uses a different AioContext
972 def test_iothreads(self
):
974 result
= self
.vm
.qmp('blockdev-add', conv_keys
= False, **opts
)
975 self
.assert_qmp(result
, 'return', {})
978 result
= self
.vm
.qmp('blockdev-add', conv_keys
= False, **opts2
)
979 self
.assert_qmp(result
, 'return', {})
981 result
= self
.vm
.qmp('object-add', qom_type
='iothread', id='iothread0')
982 self
.assert_qmp(result
, 'return', {})
984 result
= self
.vm
.qmp('object-add', qom_type
='iothread', id='iothread1')
985 self
.assert_qmp(result
, 'return', {})
987 result
= self
.vm
.qmp('x-blockdev-set-iothread', node_name
='hd0', iothread
='iothread0')
988 self
.assert_qmp(result
, 'return', {})
990 self
.reopen(opts
, {'backing': 'hd2'}, "Cannot use a new backing file with a different AioContext")
992 result
= self
.vm
.qmp('x-blockdev-set-iothread', node_name
='hd2', iothread
='iothread1')
993 self
.assert_qmp(result
, 'return', {})
995 self
.reopen(opts
, {'backing': 'hd2'}, "Cannot use a new backing file with a different AioContext")
997 result
= self
.vm
.qmp('x-blockdev-set-iothread', node_name
='hd2', iothread
='iothread0')
998 self
.assert_qmp(result
, 'return', {})
1000 self
.reopen(opts
, {'backing': 'hd2'})
1002 if __name__
== '__main__':
1003 iotests
.main(supported_fmts
=["qcow2"])