1903cb1bfSPhilippe Mathieu-Daudé#!/usr/bin/env python3 29dd003a9SVladimir Sementsov-Ogievskiy# group: rw 3bf3e50f6SAlberto Garcia# 4e60edf69SAlberto Garcia# Test cases for the QMP 'blockdev-reopen' command 5bf3e50f6SAlberto Garcia# 6bf3e50f6SAlberto Garcia# Copyright (C) 2018-2019 Igalia, S.L. 7bf3e50f6SAlberto Garcia# Author: Alberto Garcia <berto@igalia.com> 8bf3e50f6SAlberto Garcia# 9bf3e50f6SAlberto Garcia# This program is free software; you can redistribute it and/or modify 10bf3e50f6SAlberto Garcia# it under the terms of the GNU General Public License as published by 11bf3e50f6SAlberto Garcia# the Free Software Foundation; either version 2 of the License, or 12bf3e50f6SAlberto Garcia# (at your option) any later version. 13bf3e50f6SAlberto Garcia# 14bf3e50f6SAlberto Garcia# This program is distributed in the hope that it will be useful, 15bf3e50f6SAlberto Garcia# but WITHOUT ANY WARRANTY; without even the implied warranty of 16bf3e50f6SAlberto Garcia# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17bf3e50f6SAlberto Garcia# GNU General Public License for more details. 18bf3e50f6SAlberto Garcia# 19bf3e50f6SAlberto Garcia# You should have received a copy of the GNU General Public License 20bf3e50f6SAlberto Garcia# along with this program. If not, see <http://www.gnu.org/licenses/>. 21bf3e50f6SAlberto Garcia# 22bf3e50f6SAlberto Garcia 23bf3e50f6SAlberto Garciaimport os 24bf3e50f6SAlberto Garciaimport re 25bf3e50f6SAlberto Garciaimport iotests 26bf3e50f6SAlberto Garciaimport copy 27bf3e50f6SAlberto Garciaimport json 28bf3e50f6SAlberto Garciafrom iotests import qemu_img, qemu_io 29bf3e50f6SAlberto Garcia 30bf3e50f6SAlberto Garciahd_path = [ 31bf3e50f6SAlberto Garcia os.path.join(iotests.test_dir, 'hd0.img'), 32bf3e50f6SAlberto Garcia os.path.join(iotests.test_dir, 'hd1.img'), 33bf3e50f6SAlberto Garcia os.path.join(iotests.test_dir, 'hd2.img') 34bf3e50f6SAlberto Garcia] 35bf3e50f6SAlberto Garcia 36bf3e50f6SAlberto Garciadef hd_opts(idx): 37bf3e50f6SAlberto Garcia return {'driver': iotests.imgfmt, 38bf3e50f6SAlberto Garcia 'node-name': 'hd%d' % idx, 39bf3e50f6SAlberto Garcia 'file': {'driver': 'file', 40bf3e50f6SAlberto Garcia 'node-name': 'hd%d-file' % idx, 41bf3e50f6SAlberto Garcia 'filename': hd_path[idx] } } 42bf3e50f6SAlberto Garcia 43bf3e50f6SAlberto Garciaclass TestBlockdevReopen(iotests.QMPTestCase): 44bf3e50f6SAlberto Garcia total_io_cmds = 0 45bf3e50f6SAlberto Garcia 46bf3e50f6SAlberto Garcia def setUp(self): 47bf3e50f6SAlberto Garcia qemu_img('create', '-f', iotests.imgfmt, hd_path[0], '3M') 48b66ff2c2SEric Blake qemu_img('create', '-f', iotests.imgfmt, '-b', hd_path[0], 49b66ff2c2SEric Blake '-F', iotests.imgfmt, hd_path[1]) 50bf3e50f6SAlberto Garcia qemu_img('create', '-f', iotests.imgfmt, hd_path[2], '3M') 51bf3e50f6SAlberto Garcia qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xa0 0 1M', hd_path[0]) 52bf3e50f6SAlberto Garcia qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xa1 1M 1M', hd_path[1]) 53bf3e50f6SAlberto Garcia qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xa2 2M 1M', hd_path[2]) 54bf3e50f6SAlberto Garcia self.vm = iotests.VM() 55bf3e50f6SAlberto Garcia self.vm.launch() 56bf3e50f6SAlberto Garcia 57bf3e50f6SAlberto Garcia def tearDown(self): 58bf3e50f6SAlberto Garcia self.vm.shutdown() 59bf3e50f6SAlberto Garcia self.check_qemu_io_errors() 60bf3e50f6SAlberto Garcia os.remove(hd_path[0]) 61bf3e50f6SAlberto Garcia os.remove(hd_path[1]) 62bf3e50f6SAlberto Garcia os.remove(hd_path[2]) 63bf3e50f6SAlberto Garcia 64bf3e50f6SAlberto Garcia # The output of qemu-io is not returned by vm.hmp_qemu_io() but 65bf3e50f6SAlberto Garcia # it's stored in the log and can only be read when the VM has been 66bf3e50f6SAlberto Garcia # shut down. This function runs qemu-io and keeps track of the 67bf3e50f6SAlberto Garcia # number of times it's been called. 68bf3e50f6SAlberto Garcia def run_qemu_io(self, img, cmd): 69bf3e50f6SAlberto Garcia result = self.vm.hmp_qemu_io(img, cmd) 70bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', '') 71bf3e50f6SAlberto Garcia self.total_io_cmds += 1 72bf3e50f6SAlberto Garcia 73bf3e50f6SAlberto Garcia # Once the VM is shut down we can parse the log and see if qemu-io 74bf3e50f6SAlberto Garcia # ran without errors. 75bf3e50f6SAlberto Garcia def check_qemu_io_errors(self): 76bf3e50f6SAlberto Garcia self.assertFalse(self.vm.is_running()) 77bf3e50f6SAlberto Garcia found = 0 78bf3e50f6SAlberto Garcia log = self.vm.get_log() 79bf3e50f6SAlberto Garcia for line in log.split("\n"): 80bf3e50f6SAlberto Garcia if line.startswith("Pattern verification failed"): 81bf3e50f6SAlberto Garcia raise Exception("%s (command #%d)" % (line, found)) 824c5393f1SAlberto Garcia if re.match("(read|wrote) .*/.* bytes at offset", line): 83bf3e50f6SAlberto Garcia found += 1 84bf3e50f6SAlberto Garcia self.assertEqual(found, self.total_io_cmds, 85bf3e50f6SAlberto Garcia "Expected output of %d qemu-io commands, found %d" % 86bf3e50f6SAlberto Garcia (found, self.total_io_cmds)) 87bf3e50f6SAlberto Garcia 88e60edf69SAlberto Garcia # Run blockdev-reopen on a list of block devices 893908b7a8SAlberto Garcia def reopenMultiple(self, opts, errmsg = None): 90e60edf69SAlberto Garcia result = self.vm.qmp('blockdev-reopen', conv_keys=False, options=opts) 913908b7a8SAlberto Garcia if errmsg: 923908b7a8SAlberto Garcia self.assert_qmp(result, 'error/class', 'GenericError') 933908b7a8SAlberto Garcia self.assert_qmp(result, 'error/desc', errmsg) 943908b7a8SAlberto Garcia else: 953908b7a8SAlberto Garcia self.assert_qmp(result, 'return', {}) 963908b7a8SAlberto Garcia 97e60edf69SAlberto Garcia # Run blockdev-reopen on a single block device (specified by 983908b7a8SAlberto Garcia # 'opts') but applying 'newopts' on top of it. The original 'opts' 993908b7a8SAlberto Garcia # dict is unmodified 100bf3e50f6SAlberto Garcia def reopen(self, opts, newopts = {}, errmsg = None): 101bf3e50f6SAlberto Garcia opts = copy.deepcopy(opts) 102bf3e50f6SAlberto Garcia 103bf3e50f6SAlberto Garcia # Apply the changes from 'newopts' on top of 'opts' 104bf3e50f6SAlberto Garcia for key in newopts: 105bf3e50f6SAlberto Garcia value = newopts[key] 106bf3e50f6SAlberto Garcia # If key has the form "foo.bar" then we need to do 107bf3e50f6SAlberto Garcia # opts["foo"]["bar"] = value, not opts["foo.bar"] = value 108bf3e50f6SAlberto Garcia subdict = opts 109bf3e50f6SAlberto Garcia while key.find('.') != -1: 110bf3e50f6SAlberto Garcia [prefix, key] = key.split('.', 1) 111bf3e50f6SAlberto Garcia subdict = opts[prefix] 112bf3e50f6SAlberto Garcia subdict[key] = value 113bf3e50f6SAlberto Garcia 1143908b7a8SAlberto Garcia self.reopenMultiple([ opts ], errmsg) 115bf3e50f6SAlberto Garcia 116bf3e50f6SAlberto Garcia 117bf3e50f6SAlberto Garcia # Run query-named-block-nodes and return the specified entry 118bf3e50f6SAlberto Garcia def get_node(self, node_name): 119bf3e50f6SAlberto Garcia result = self.vm.qmp('query-named-block-nodes') 120bf3e50f6SAlberto Garcia for node in result['return']: 121bf3e50f6SAlberto Garcia if node['node-name'] == node_name: 122bf3e50f6SAlberto Garcia return node 123bf3e50f6SAlberto Garcia return None 124bf3e50f6SAlberto Garcia 125bf3e50f6SAlberto Garcia # Run 'query-named-block-nodes' and compare its output with the 126bf3e50f6SAlberto Garcia # value passed by the user in 'graph' 127bf3e50f6SAlberto Garcia def check_node_graph(self, graph): 128bf3e50f6SAlberto Garcia result = self.vm.qmp('query-named-block-nodes') 129bf3e50f6SAlberto Garcia self.assertEqual(json.dumps(graph, sort_keys=True), 130bf3e50f6SAlberto Garcia json.dumps(result, sort_keys=True)) 131bf3e50f6SAlberto Garcia 132bf3e50f6SAlberto Garcia # This test opens one single disk image (without backing files) 133bf3e50f6SAlberto Garcia # and tries to reopen it with illegal / incorrect parameters. 134bf3e50f6SAlberto Garcia def test_incorrect_parameters_single_file(self): 135bf3e50f6SAlberto Garcia # Open 'hd0' only (no backing files) 136bf3e50f6SAlberto Garcia opts = hd_opts(0) 137bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-add', conv_keys = False, **opts) 138bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 139bf3e50f6SAlberto Garcia original_graph = self.vm.qmp('query-named-block-nodes') 140bf3e50f6SAlberto Garcia 141bf3e50f6SAlberto Garcia # We can reopen the image passing the same options 142bf3e50f6SAlberto Garcia self.reopen(opts) 143bf3e50f6SAlberto Garcia 144bf3e50f6SAlberto Garcia # We can also reopen passing a child reference in 'file' 145bf3e50f6SAlberto Garcia self.reopen(opts, {'file': 'hd0-file'}) 146bf3e50f6SAlberto Garcia 147bf3e50f6SAlberto Garcia # We cannot change any of these 148ef2e38a1SConnor Kuehl self.reopen(opts, {'node-name': 'not-found'}, "Failed to find node with node-name='not-found'") 149ef2e38a1SConnor Kuehl self.reopen(opts, {'node-name': ''}, "Failed to find node with node-name=''") 1503908b7a8SAlberto Garcia self.reopen(opts, {'node-name': None}, "Invalid parameter type for 'options[0].node-name', expected: string") 151bf3e50f6SAlberto Garcia self.reopen(opts, {'driver': 'raw'}, "Cannot change the option 'driver'") 152*ea29331bSMarkus Armbruster self.reopen(opts, {'driver': ''}, "Parameter 'driver' does not accept value ''") 1533908b7a8SAlberto Garcia self.reopen(opts, {'driver': None}, "Invalid parameter type for 'options[0].driver', expected: string") 154ecd30d2dSAlberto Garcia self.reopen(opts, {'file': 'not-found'}, "Cannot find device='' nor node-name='not-found'") 155ecd30d2dSAlberto Garcia self.reopen(opts, {'file': ''}, "Cannot find device='' nor node-name=''") 156bf3e50f6SAlberto Garcia self.reopen(opts, {'file': None}, "Invalid parameter type for 'file', expected: BlockdevRef") 157bf3e50f6SAlberto Garcia self.reopen(opts, {'file.node-name': 'newname'}, "Cannot change the option 'node-name'") 158bf3e50f6SAlberto Garcia self.reopen(opts, {'file.driver': 'host_device'}, "Cannot change the option 'driver'") 159bf3e50f6SAlberto Garcia self.reopen(opts, {'file.filename': hd_path[1]}, "Cannot change the option 'filename'") 160bf3e50f6SAlberto Garcia self.reopen(opts, {'file.aio': 'native'}, "Cannot change the option 'aio'") 161bf3e50f6SAlberto Garcia self.reopen(opts, {'file.locking': 'off'}, "Cannot change the option 'locking'") 1623908b7a8SAlberto Garcia self.reopen(opts, {'file.filename': None}, "Invalid parameter type for 'options[0].file.filename', expected: string") 163bf3e50f6SAlberto Garcia 164e60edf69SAlberto Garcia # node-name is optional in BlockdevOptions, but blockdev-reopen needs it 165bf3e50f6SAlberto Garcia del opts['node-name'] 166ef2e38a1SConnor Kuehl self.reopen(opts, {}, "node-name not specified") 167bf3e50f6SAlberto Garcia 168bf3e50f6SAlberto Garcia # Check that nothing has changed 169bf3e50f6SAlberto Garcia self.check_node_graph(original_graph) 170bf3e50f6SAlberto Garcia 171bf3e50f6SAlberto Garcia # Remove the node 172bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0') 173bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 174bf3e50f6SAlberto Garcia 175bf3e50f6SAlberto Garcia # This test opens an image with a backing file and tries to reopen 176bf3e50f6SAlberto Garcia # it with illegal / incorrect parameters. 177bf3e50f6SAlberto Garcia def test_incorrect_parameters_backing_file(self): 178bf3e50f6SAlberto Garcia # Open hd1 omitting the backing options (hd0 will be opened 179bf3e50f6SAlberto Garcia # with the default options) 180bf3e50f6SAlberto Garcia opts = hd_opts(1) 181bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-add', conv_keys = False, **opts) 182bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 183bf3e50f6SAlberto Garcia original_graph = self.vm.qmp('query-named-block-nodes') 184bf3e50f6SAlberto Garcia 185bf3e50f6SAlberto Garcia # We can't reopen the image passing the same options, 'backing' is mandatory 186bf3e50f6SAlberto Garcia self.reopen(opts, {}, "backing is missing for 'hd1'") 187bf3e50f6SAlberto Garcia 188bf3e50f6SAlberto Garcia # Everything works if we pass 'backing' using the existing node name 189bf3e50f6SAlberto Garcia for node in original_graph['return']: 190bf3e50f6SAlberto Garcia if node['drv'] == iotests.imgfmt and node['file'] == hd_path[0]: 191bf3e50f6SAlberto Garcia backing_node_name = node['node-name'] 192bf3e50f6SAlberto Garcia self.reopen(opts, {'backing': backing_node_name}) 193bf3e50f6SAlberto Garcia 194bf3e50f6SAlberto Garcia # We can't use a non-existing or empty (non-NULL) node as the backing image 195785ec4b1SConnor Kuehl self.reopen(opts, {'backing': 'not-found'}, "Cannot find device=\'\' nor node-name=\'not-found\'") 196785ec4b1SConnor Kuehl self.reopen(opts, {'backing': ''}, "Cannot find device=\'\' nor node-name=\'\'") 197bf3e50f6SAlberto Garcia 198bf3e50f6SAlberto Garcia # We can reopen the image just fine if we specify the backing options 199bf3e50f6SAlberto Garcia opts['backing'] = {'driver': iotests.imgfmt, 200bf3e50f6SAlberto Garcia 'file': {'driver': 'file', 201bf3e50f6SAlberto Garcia 'filename': hd_path[0]}} 202bf3e50f6SAlberto Garcia self.reopen(opts) 203bf3e50f6SAlberto Garcia 204bf3e50f6SAlberto Garcia # We cannot change any of these options 205bf3e50f6SAlberto Garcia self.reopen(opts, {'backing.node-name': 'newname'}, "Cannot change the option 'node-name'") 206bf3e50f6SAlberto Garcia self.reopen(opts, {'backing.driver': 'raw'}, "Cannot change the option 'driver'") 207bf3e50f6SAlberto Garcia self.reopen(opts, {'backing.file.node-name': 'newname'}, "Cannot change the option 'node-name'") 208bf3e50f6SAlberto Garcia self.reopen(opts, {'backing.file.driver': 'host_device'}, "Cannot change the option 'driver'") 209bf3e50f6SAlberto Garcia 210bf3e50f6SAlberto Garcia # Check that nothing has changed since the beginning 211bf3e50f6SAlberto Garcia self.check_node_graph(original_graph) 212bf3e50f6SAlberto Garcia 213bf3e50f6SAlberto Garcia # Remove the node 214bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd1') 215bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 216bf3e50f6SAlberto Garcia 217bf3e50f6SAlberto Garcia # Reopen an image several times changing some of its options 218bf3e50f6SAlberto Garcia def test_reopen(self): 21923e1d054SMax Reitz # Check whether the filesystem supports O_DIRECT 22023e1d054SMax Reitz if 'O_DIRECT' in qemu_io('-f', 'raw', '-t', 'none', '-c', 'quit', hd_path[0]): 22123e1d054SMax Reitz supports_direct = False 22223e1d054SMax Reitz else: 22323e1d054SMax Reitz supports_direct = True 22423e1d054SMax Reitz 225bf3e50f6SAlberto Garcia # Open the hd1 image passing all backing options 226bf3e50f6SAlberto Garcia opts = hd_opts(1) 227bf3e50f6SAlberto Garcia opts['backing'] = hd_opts(0) 228bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-add', conv_keys = False, **opts) 229bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 230bf3e50f6SAlberto Garcia original_graph = self.vm.qmp('query-named-block-nodes') 231bf3e50f6SAlberto Garcia 232bf3e50f6SAlberto Garcia # We can reopen the image passing the same options 233bf3e50f6SAlberto Garcia self.reopen(opts) 234bf3e50f6SAlberto Garcia 235bf3e50f6SAlberto Garcia # Reopen in read-only mode 236bf3e50f6SAlberto Garcia self.assert_qmp(self.get_node('hd1'), 'ro', False) 237bf3e50f6SAlberto Garcia 238bf3e50f6SAlberto Garcia self.reopen(opts, {'read-only': True}) 239bf3e50f6SAlberto Garcia self.assert_qmp(self.get_node('hd1'), 'ro', True) 240bf3e50f6SAlberto Garcia self.reopen(opts) 241bf3e50f6SAlberto Garcia self.assert_qmp(self.get_node('hd1'), 'ro', False) 242bf3e50f6SAlberto Garcia 243bf3e50f6SAlberto Garcia # Change the cache options 244bf3e50f6SAlberto Garcia self.assert_qmp(self.get_node('hd1'), 'cache/writeback', True) 245bf3e50f6SAlberto Garcia self.assert_qmp(self.get_node('hd1'), 'cache/direct', False) 246bf3e50f6SAlberto Garcia self.assert_qmp(self.get_node('hd1'), 'cache/no-flush', False) 24723e1d054SMax Reitz self.reopen(opts, {'cache': { 'direct': supports_direct, 'no-flush': True }}) 248bf3e50f6SAlberto Garcia self.assert_qmp(self.get_node('hd1'), 'cache/writeback', True) 24923e1d054SMax Reitz self.assert_qmp(self.get_node('hd1'), 'cache/direct', supports_direct) 250bf3e50f6SAlberto Garcia self.assert_qmp(self.get_node('hd1'), 'cache/no-flush', True) 251bf3e50f6SAlberto Garcia 252bf3e50f6SAlberto Garcia # Reopen again with the original options 253bf3e50f6SAlberto Garcia self.reopen(opts) 254bf3e50f6SAlberto Garcia self.assert_qmp(self.get_node('hd1'), 'cache/writeback', True) 255bf3e50f6SAlberto Garcia self.assert_qmp(self.get_node('hd1'), 'cache/direct', False) 256bf3e50f6SAlberto Garcia self.assert_qmp(self.get_node('hd1'), 'cache/no-flush', False) 257bf3e50f6SAlberto Garcia 258bf3e50f6SAlberto Garcia # Change 'detect-zeroes' and 'discard' 259bf3e50f6SAlberto Garcia self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'off') 260bf3e50f6SAlberto Garcia self.reopen(opts, {'detect-zeroes': 'on'}) 261bf3e50f6SAlberto Garcia self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'on') 262bf3e50f6SAlberto Garcia self.reopen(opts, {'detect-zeroes': 'unmap'}, 263bf3e50f6SAlberto Garcia "setting detect-zeroes to unmap is not allowed " + 264bf3e50f6SAlberto Garcia "without setting discard operation to unmap") 265bf3e50f6SAlberto Garcia self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'on') 266bf3e50f6SAlberto Garcia self.reopen(opts, {'detect-zeroes': 'unmap', 'discard': 'unmap'}) 267bf3e50f6SAlberto Garcia self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'unmap') 268bf3e50f6SAlberto Garcia self.reopen(opts) 269bf3e50f6SAlberto Garcia self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'off') 270bf3e50f6SAlberto Garcia 271bf3e50f6SAlberto Garcia # Changing 'force-share' is currently not supported 272bf3e50f6SAlberto Garcia self.reopen(opts, {'force-share': True}, "Cannot change the option 'force-share'") 273bf3e50f6SAlberto Garcia 274bf3e50f6SAlberto Garcia # Change some qcow2-specific options 275bf3e50f6SAlberto Garcia # No way to test for success other than checking the return message 276bf3e50f6SAlberto Garcia if iotests.imgfmt == 'qcow2': 277bf3e50f6SAlberto Garcia self.reopen(opts, {'l2-cache-entry-size': 128 * 1024}, 278bf3e50f6SAlberto Garcia "L2 cache entry size must be a power of two "+ 279bf3e50f6SAlberto Garcia "between 512 and the cluster size (65536)") 280bf3e50f6SAlberto Garcia self.reopen(opts, {'l2-cache-size': 1024 * 1024, 281bf3e50f6SAlberto Garcia 'cache-size': 512 * 1024}, 282bf3e50f6SAlberto Garcia "l2-cache-size may not exceed cache-size") 283bf3e50f6SAlberto Garcia self.reopen(opts, {'l2-cache-size': 4 * 1024 * 1024, 284bf3e50f6SAlberto Garcia 'refcount-cache-size': 4 * 1024 * 1024, 285bf3e50f6SAlberto Garcia 'l2-cache-entry-size': 32 * 1024}) 286bf3e50f6SAlberto Garcia self.reopen(opts, {'pass-discard-request': True}) 287bf3e50f6SAlberto Garcia 288bf3e50f6SAlberto Garcia # Check that nothing has changed since the beginning 289bf3e50f6SAlberto Garcia # (from the parameters that we can check) 290bf3e50f6SAlberto Garcia self.check_node_graph(original_graph) 291bf3e50f6SAlberto Garcia 292bf3e50f6SAlberto Garcia # Check that the node names (other than the top-level one) are optional 293bf3e50f6SAlberto Garcia del opts['file']['node-name'] 294bf3e50f6SAlberto Garcia del opts['backing']['node-name'] 295bf3e50f6SAlberto Garcia del opts['backing']['file']['node-name'] 296bf3e50f6SAlberto Garcia self.reopen(opts) 297bf3e50f6SAlberto Garcia self.check_node_graph(original_graph) 298bf3e50f6SAlberto Garcia 299bf3e50f6SAlberto Garcia # Reopen setting backing = null, this removes the backing image from the chain 300bf3e50f6SAlberto Garcia self.reopen(opts, {'backing': None}) 301bf3e50f6SAlberto Garcia self.assert_qmp_absent(self.get_node('hd1'), 'image/backing-image') 302bf3e50f6SAlberto Garcia 303bf3e50f6SAlberto Garcia # Open the 'hd0' image 304bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-add', conv_keys = False, **hd_opts(0)) 305bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 306bf3e50f6SAlberto Garcia 307bf3e50f6SAlberto Garcia # Reopen the hd1 image setting 'hd0' as its backing image 308bf3e50f6SAlberto Garcia self.reopen(opts, {'backing': 'hd0'}) 309bf3e50f6SAlberto Garcia self.assert_qmp(self.get_node('hd1'), 'image/backing-image/filename', hd_path[0]) 310bf3e50f6SAlberto Garcia 311bf3e50f6SAlberto Garcia # Check that nothing has changed since the beginning 312bf3e50f6SAlberto Garcia self.reopen(hd_opts(0), {'read-only': True}) 313bf3e50f6SAlberto Garcia self.check_node_graph(original_graph) 314bf3e50f6SAlberto Garcia 315bf3e50f6SAlberto Garcia # The backing file (hd0) is now a reference, we cannot change backing.* anymore 316bf3e50f6SAlberto Garcia self.reopen(opts, {}, "Cannot change the option 'backing.driver'") 317bf3e50f6SAlberto Garcia 318bf3e50f6SAlberto Garcia # We can't remove 'hd0' while it's a backing image of 'hd1' 319bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0') 320bf3e50f6SAlberto Garcia self.assert_qmp(result, 'error/class', 'GenericError') 321bf3e50f6SAlberto Garcia self.assert_qmp(result, 'error/desc', "Node 'hd0' is busy: node is used as backing hd of 'hd1'") 322bf3e50f6SAlberto Garcia 323bf3e50f6SAlberto Garcia # But we can remove both nodes if done in the proper order 324bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd1') 325bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 326bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0') 327bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 328bf3e50f6SAlberto Garcia 329bf3e50f6SAlberto Garcia # Reopen a raw image and see the effect of changing the 'offset' option 330bf3e50f6SAlberto Garcia def test_reopen_raw(self): 331bf3e50f6SAlberto Garcia opts = {'driver': 'raw', 'node-name': 'hd0', 332bf3e50f6SAlberto Garcia 'file': { 'driver': 'file', 333bf3e50f6SAlberto Garcia 'filename': hd_path[0], 334bf3e50f6SAlberto Garcia 'node-name': 'hd0-file' } } 335bf3e50f6SAlberto Garcia 336bf3e50f6SAlberto Garcia # First we create a 2MB raw file, and fill each half with a 337bf3e50f6SAlberto Garcia # different value 338bf3e50f6SAlberto Garcia qemu_img('create', '-f', 'raw', hd_path[0], '2M') 339bf3e50f6SAlberto Garcia qemu_io('-f', 'raw', '-c', 'write -P 0xa0 0 1M', hd_path[0]) 340bf3e50f6SAlberto Garcia qemu_io('-f', 'raw', '-c', 'write -P 0xa1 1M 1M', hd_path[0]) 341bf3e50f6SAlberto Garcia 342bf3e50f6SAlberto Garcia # Open the raw file with QEMU 343bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-add', conv_keys = False, **opts) 344bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 345bf3e50f6SAlberto Garcia 346bf3e50f6SAlberto Garcia # Read 1MB from offset 0 347bf3e50f6SAlberto Garcia self.run_qemu_io("hd0", "read -P 0xa0 0 1M") 348bf3e50f6SAlberto Garcia 349bf3e50f6SAlberto Garcia # Reopen the image with a 1MB offset. 350bf3e50f6SAlberto Garcia # Now the results are different 351bf3e50f6SAlberto Garcia self.reopen(opts, {'offset': 1024*1024}) 352bf3e50f6SAlberto Garcia self.run_qemu_io("hd0", "read -P 0xa1 0 1M") 353bf3e50f6SAlberto Garcia 354bf3e50f6SAlberto Garcia # Reopen again with the original options. 355bf3e50f6SAlberto Garcia # We get the original results again 356bf3e50f6SAlberto Garcia self.reopen(opts) 357bf3e50f6SAlberto Garcia self.run_qemu_io("hd0", "read -P 0xa0 0 1M") 358bf3e50f6SAlberto Garcia 359bf3e50f6SAlberto Garcia # Remove the block device 360bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0') 361bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 362bf3e50f6SAlberto Garcia 363bf3e50f6SAlberto Garcia # Omitting an option should reset it to the default value, but if 364bf3e50f6SAlberto Garcia # an option cannot be changed it shouldn't be possible to reset it 365bf3e50f6SAlberto Garcia # to its default value either 366bf3e50f6SAlberto Garcia def test_reset_default_values(self): 367bf3e50f6SAlberto Garcia opts = {'driver': 'qcow2', 'node-name': 'hd0', 368bf3e50f6SAlberto Garcia 'file': { 'driver': 'file', 369bf3e50f6SAlberto Garcia 'filename': hd_path[0], 370bf3e50f6SAlberto Garcia 'x-check-cache-dropped': True, # This one can be changed 371bf3e50f6SAlberto Garcia 'locking': 'off', # This one can NOT be changed 372bf3e50f6SAlberto Garcia 'node-name': 'hd0-file' } } 373bf3e50f6SAlberto Garcia 374bf3e50f6SAlberto Garcia # Open the file with QEMU 375bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-add', conv_keys = False, **opts) 376bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 377bf3e50f6SAlberto Garcia 378bf3e50f6SAlberto Garcia # file.x-check-cache-dropped can be changed... 379bf3e50f6SAlberto Garcia self.reopen(opts, { 'file.x-check-cache-dropped': False }) 380bf3e50f6SAlberto Garcia # ...and dropped completely (resetting to the default value) 381bf3e50f6SAlberto Garcia del opts['file']['x-check-cache-dropped'] 382bf3e50f6SAlberto Garcia self.reopen(opts) 383bf3e50f6SAlberto Garcia 384bf3e50f6SAlberto Garcia # file.locking cannot be changed nor reset to the default value 385bf3e50f6SAlberto Garcia self.reopen(opts, { 'file.locking': 'on' }, "Cannot change the option 'locking'") 386bf3e50f6SAlberto Garcia del opts['file']['locking'] 387bf3e50f6SAlberto Garcia self.reopen(opts, {}, "Option 'locking' cannot be reset to its default value") 388bf3e50f6SAlberto Garcia # But we can reopen it if we maintain its previous value 389bf3e50f6SAlberto Garcia self.reopen(opts, { 'file.locking': 'off' }) 390bf3e50f6SAlberto Garcia 391bf3e50f6SAlberto Garcia # Remove the block device 392bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0') 393bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 394bf3e50f6SAlberto Garcia 395bf3e50f6SAlberto Garcia # This test modifies the node graph a few times by changing the 396bf3e50f6SAlberto Garcia # 'backing' option on reopen and verifies that the guest data that 397bf3e50f6SAlberto Garcia # is read afterwards is consistent with the graph changes. 398bf3e50f6SAlberto Garcia def test_io_with_graph_changes(self): 399bf3e50f6SAlberto Garcia opts = [] 400bf3e50f6SAlberto Garcia 401bf3e50f6SAlberto Garcia # Open hd0, hd1 and hd2 without any backing image 402bf3e50f6SAlberto Garcia for i in range(3): 403bf3e50f6SAlberto Garcia opts.append(hd_opts(i)) 404bf3e50f6SAlberto Garcia opts[i]['backing'] = None 405bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-add', conv_keys = False, **opts[i]) 406bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 407bf3e50f6SAlberto Garcia 408bf3e50f6SAlberto Garcia # hd0 409bf3e50f6SAlberto Garcia self.run_qemu_io("hd0", "read -P 0xa0 0 1M") 410bf3e50f6SAlberto Garcia self.run_qemu_io("hd0", "read -P 0 1M 1M") 411bf3e50f6SAlberto Garcia self.run_qemu_io("hd0", "read -P 0 2M 1M") 412bf3e50f6SAlberto Garcia 413bf3e50f6SAlberto Garcia # hd1 <- hd0 414bf3e50f6SAlberto Garcia self.reopen(opts[0], {'backing': 'hd1'}) 415bf3e50f6SAlberto Garcia 416bf3e50f6SAlberto Garcia self.run_qemu_io("hd0", "read -P 0xa0 0 1M") 417bf3e50f6SAlberto Garcia self.run_qemu_io("hd0", "read -P 0xa1 1M 1M") 418bf3e50f6SAlberto Garcia self.run_qemu_io("hd0", "read -P 0 2M 1M") 419bf3e50f6SAlberto Garcia 420bf3e50f6SAlberto Garcia # hd1 <- hd0 , hd1 <- hd2 421bf3e50f6SAlberto Garcia self.reopen(opts[2], {'backing': 'hd1'}) 422bf3e50f6SAlberto Garcia 423bf3e50f6SAlberto Garcia self.run_qemu_io("hd2", "read -P 0 0 1M") 424bf3e50f6SAlberto Garcia self.run_qemu_io("hd2", "read -P 0xa1 1M 1M") 425bf3e50f6SAlberto Garcia self.run_qemu_io("hd2", "read -P 0xa2 2M 1M") 426bf3e50f6SAlberto Garcia 427bf3e50f6SAlberto Garcia # hd1 <- hd2 <- hd0 428bf3e50f6SAlberto Garcia self.reopen(opts[0], {'backing': 'hd2'}) 429bf3e50f6SAlberto Garcia 430bf3e50f6SAlberto Garcia self.run_qemu_io("hd0", "read -P 0xa0 0 1M") 431bf3e50f6SAlberto Garcia self.run_qemu_io("hd0", "read -P 0xa1 1M 1M") 432bf3e50f6SAlberto Garcia self.run_qemu_io("hd0", "read -P 0xa2 2M 1M") 433bf3e50f6SAlberto Garcia 434bf3e50f6SAlberto Garcia # hd2 <- hd0 435bf3e50f6SAlberto Garcia self.reopen(opts[2], {'backing': None}) 436bf3e50f6SAlberto Garcia 437bf3e50f6SAlberto Garcia self.run_qemu_io("hd0", "read -P 0xa0 0 1M") 438bf3e50f6SAlberto Garcia self.run_qemu_io("hd0", "read -P 0 1M 1M") 439bf3e50f6SAlberto Garcia self.run_qemu_io("hd0", "read -P 0xa2 2M 1M") 440bf3e50f6SAlberto Garcia 441bf3e50f6SAlberto Garcia # hd2 <- hd1 <- hd0 442bf3e50f6SAlberto Garcia self.reopen(opts[1], {'backing': 'hd2'}) 443bf3e50f6SAlberto Garcia self.reopen(opts[0], {'backing': 'hd1'}) 444bf3e50f6SAlberto Garcia 445bf3e50f6SAlberto Garcia self.run_qemu_io("hd0", "read -P 0xa0 0 1M") 446bf3e50f6SAlberto Garcia self.run_qemu_io("hd0", "read -P 0xa1 1M 1M") 447bf3e50f6SAlberto Garcia self.run_qemu_io("hd0", "read -P 0xa2 2M 1M") 448bf3e50f6SAlberto Garcia 449bf3e50f6SAlberto Garcia # Illegal operation: hd2 is a child of hd1 450bf3e50f6SAlberto Garcia self.reopen(opts[2], {'backing': 'hd1'}, 451ecd30d2dSAlberto Garcia "Making 'hd1' a backing child of 'hd2' would create a cycle") 452bf3e50f6SAlberto Garcia 453bf3e50f6SAlberto Garcia # hd2 <- hd0, hd2 <- hd1 454bf3e50f6SAlberto Garcia self.reopen(opts[0], {'backing': 'hd2'}) 455bf3e50f6SAlberto Garcia 456bf3e50f6SAlberto Garcia self.run_qemu_io("hd1", "read -P 0 0 1M") 457bf3e50f6SAlberto Garcia self.run_qemu_io("hd1", "read -P 0xa1 1M 1M") 458bf3e50f6SAlberto Garcia self.run_qemu_io("hd1", "read -P 0xa2 2M 1M") 459bf3e50f6SAlberto Garcia 460bf3e50f6SAlberto Garcia # More illegal operations 461bf3e50f6SAlberto Garcia self.reopen(opts[2], {'backing': 'hd1'}, 462ecd30d2dSAlberto Garcia "Making 'hd1' a backing child of 'hd2' would create a cycle") 463ecd30d2dSAlberto Garcia self.reopen(opts[2], {'file': 'hd0-file'}, 464ecd30d2dSAlberto Garcia "Permission conflict on node 'hd0-file': permissions 'write, resize' are both required by node 'hd0' (uses node 'hd0-file' as 'file' child) and unshared by node 'hd2' (uses node 'hd0-file' as 'file' child).") 465bf3e50f6SAlberto Garcia 466bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd2') 467bf3e50f6SAlberto Garcia self.assert_qmp(result, 'error/class', 'GenericError') 468bf3e50f6SAlberto Garcia self.assert_qmp(result, 'error/desc', "Node 'hd2' is busy: node is used as backing hd of 'hd0'") 469bf3e50f6SAlberto Garcia 470bf3e50f6SAlberto Garcia # hd1 doesn't have a backing file now 471bf3e50f6SAlberto Garcia self.reopen(opts[1], {'backing': None}) 472bf3e50f6SAlberto Garcia 473bf3e50f6SAlberto Garcia self.run_qemu_io("hd1", "read -P 0 0 1M") 474bf3e50f6SAlberto Garcia self.run_qemu_io("hd1", "read -P 0xa1 1M 1M") 475bf3e50f6SAlberto Garcia self.run_qemu_io("hd1", "read -P 0 2M 1M") 476bf3e50f6SAlberto Garcia 477bf3e50f6SAlberto Garcia # We can't remove the 'backing' option if the image has a 478bf3e50f6SAlberto Garcia # default backing file 479bf3e50f6SAlberto Garcia del opts[1]['backing'] 480bf3e50f6SAlberto Garcia self.reopen(opts[1], {}, "backing is missing for 'hd1'") 481bf3e50f6SAlberto Garcia 482bf3e50f6SAlberto Garcia self.run_qemu_io("hd1", "read -P 0 0 1M") 483bf3e50f6SAlberto Garcia self.run_qemu_io("hd1", "read -P 0xa1 1M 1M") 484bf3e50f6SAlberto Garcia self.run_qemu_io("hd1", "read -P 0 2M 1M") 485bf3e50f6SAlberto Garcia 486bf3e50f6SAlberto Garcia # This test verifies that we can't change the children of a block 487bf3e50f6SAlberto Garcia # device during a reopen operation in a way that would create 488bf3e50f6SAlberto Garcia # cycles in the node graph 4899442bebeSThomas Huth @iotests.skip_if_unsupported(['blkverify']) 490bf3e50f6SAlberto Garcia def test_graph_cycles(self): 491bf3e50f6SAlberto Garcia opts = [] 492bf3e50f6SAlberto Garcia 493bf3e50f6SAlberto Garcia # Open all three images without backing file 494bf3e50f6SAlberto Garcia for i in range(3): 495bf3e50f6SAlberto Garcia opts.append(hd_opts(i)) 496bf3e50f6SAlberto Garcia opts[i]['backing'] = None 497bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-add', conv_keys = False, **opts[i]) 498bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 499bf3e50f6SAlberto Garcia 500bf3e50f6SAlberto Garcia # hd1 <- hd0, hd1 <- hd2 501bf3e50f6SAlberto Garcia self.reopen(opts[0], {'backing': 'hd1'}) 502bf3e50f6SAlberto Garcia self.reopen(opts[2], {'backing': 'hd1'}) 503bf3e50f6SAlberto Garcia 504bf3e50f6SAlberto Garcia # Illegal: hd2 is backed by hd1 505bf3e50f6SAlberto Garcia self.reopen(opts[1], {'backing': 'hd2'}, 506ecd30d2dSAlberto Garcia "Making 'hd2' a backing child of 'hd1' would create a cycle") 507bf3e50f6SAlberto Garcia 508bf3e50f6SAlberto Garcia # hd1 <- hd0 <- hd2 509bf3e50f6SAlberto Garcia self.reopen(opts[2], {'backing': 'hd0'}) 510bf3e50f6SAlberto Garcia 511bf3e50f6SAlberto Garcia # Illegal: hd2 is backed by hd0, which is backed by hd1 512bf3e50f6SAlberto Garcia self.reopen(opts[1], {'backing': 'hd2'}, 513ecd30d2dSAlberto Garcia "Making 'hd2' a backing child of 'hd1' would create a cycle") 514bf3e50f6SAlberto Garcia 515bf3e50f6SAlberto Garcia # Illegal: hd1 cannot point to itself 516bf3e50f6SAlberto Garcia self.reopen(opts[1], {'backing': 'hd1'}, 517ecd30d2dSAlberto Garcia "Making 'hd1' a backing child of 'hd1' would create a cycle") 518bf3e50f6SAlberto Garcia 519bf3e50f6SAlberto Garcia # Remove all backing files 520bf3e50f6SAlberto Garcia self.reopen(opts[0]) 521bf3e50f6SAlberto Garcia self.reopen(opts[2]) 522bf3e50f6SAlberto Garcia 523bf3e50f6SAlberto Garcia ########################################## 524bf3e50f6SAlberto Garcia # Add a blkverify node using hd0 and hd1 # 525bf3e50f6SAlberto Garcia ########################################## 526bf3e50f6SAlberto Garcia bvopts = {'driver': 'blkverify', 527bf3e50f6SAlberto Garcia 'node-name': 'bv', 528bf3e50f6SAlberto Garcia 'test': 'hd0', 529bf3e50f6SAlberto Garcia 'raw': 'hd1'} 530bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-add', conv_keys = False, **bvopts) 531bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 532bf3e50f6SAlberto Garcia 533bf3e50f6SAlberto Garcia # blkverify doesn't currently allow reopening. TODO: implement this 534bf3e50f6SAlberto Garcia self.reopen(bvopts, {}, "Block format 'blkverify' used by node 'bv'" + 535bf3e50f6SAlberto Garcia " does not support reopening files") 536bf3e50f6SAlberto Garcia 537bf3e50f6SAlberto Garcia # Illegal: hd0 is a child of the blkverify node 538bf3e50f6SAlberto Garcia self.reopen(opts[0], {'backing': 'bv'}, 539ecd30d2dSAlberto Garcia "Making 'bv' a backing child of 'hd0' would create a cycle") 540bf3e50f6SAlberto Garcia 541bf3e50f6SAlberto Garcia # Delete the blkverify node 542bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'bv') 543bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 544bf3e50f6SAlberto Garcia 5454c5393f1SAlberto Garcia # Replace the protocol layer ('file' parameter) of a disk image 5464c5393f1SAlberto Garcia def test_replace_file(self): 5474c5393f1SAlberto Garcia # Create two small raw images and add them to a running VM 5484c5393f1SAlberto Garcia qemu_img('create', '-f', 'raw', hd_path[0], '10k') 5494c5393f1SAlberto Garcia qemu_img('create', '-f', 'raw', hd_path[1], '10k') 5504c5393f1SAlberto Garcia 5514c5393f1SAlberto Garcia hd0_opts = {'driver': 'file', 'node-name': 'hd0-file', 'filename': hd_path[0] } 5524c5393f1SAlberto Garcia hd1_opts = {'driver': 'file', 'node-name': 'hd1-file', 'filename': hd_path[1] } 5534c5393f1SAlberto Garcia 5544c5393f1SAlberto Garcia result = self.vm.qmp('blockdev-add', conv_keys = False, **hd0_opts) 5554c5393f1SAlberto Garcia self.assert_qmp(result, 'return', {}) 5564c5393f1SAlberto Garcia result = self.vm.qmp('blockdev-add', conv_keys = False, **hd1_opts) 5574c5393f1SAlberto Garcia self.assert_qmp(result, 'return', {}) 5584c5393f1SAlberto Garcia 5594c5393f1SAlberto Garcia # Add a raw format layer that uses hd0-file as its protocol layer 5604c5393f1SAlberto Garcia opts = {'driver': 'raw', 'node-name': 'hd', 'file': 'hd0-file'} 5614c5393f1SAlberto Garcia 5624c5393f1SAlberto Garcia result = self.vm.qmp('blockdev-add', conv_keys = False, **opts) 5634c5393f1SAlberto Garcia self.assert_qmp(result, 'return', {}) 5644c5393f1SAlberto Garcia 5654c5393f1SAlberto Garcia # Fill the image with data 5664c5393f1SAlberto Garcia self.run_qemu_io("hd", "read -P 0 0 10k") 5674c5393f1SAlberto Garcia self.run_qemu_io("hd", "write -P 0xa0 0 10k") 5684c5393f1SAlberto Garcia 5694c5393f1SAlberto Garcia # Replace hd0-file with hd1-file and fill it with (different) data 5704c5393f1SAlberto Garcia self.reopen(opts, {'file': 'hd1-file'}) 5714c5393f1SAlberto Garcia self.run_qemu_io("hd", "read -P 0 0 10k") 5724c5393f1SAlberto Garcia self.run_qemu_io("hd", "write -P 0xa1 0 10k") 5734c5393f1SAlberto Garcia 5744c5393f1SAlberto Garcia # Use hd0-file again and check that it contains the expected data 5754c5393f1SAlberto Garcia self.reopen(opts, {'file': 'hd0-file'}) 5764c5393f1SAlberto Garcia self.run_qemu_io("hd", "read -P 0xa0 0 10k") 5774c5393f1SAlberto Garcia 5784c5393f1SAlberto Garcia # And finally do the same with hd1-file 5794c5393f1SAlberto Garcia self.reopen(opts, {'file': 'hd1-file'}) 5804c5393f1SAlberto Garcia self.run_qemu_io("hd", "read -P 0xa1 0 10k") 5814c5393f1SAlberto Garcia 5824c5393f1SAlberto Garcia # Insert (and remove) a throttle filter 5834c5393f1SAlberto Garcia def test_insert_throttle_filter(self): 5844c5393f1SAlberto Garcia # Add an image to the VM 5854c5393f1SAlberto Garcia hd0_opts = hd_opts(0) 5864c5393f1SAlberto Garcia result = self.vm.qmp('blockdev-add', conv_keys = False, **hd0_opts) 5874c5393f1SAlberto Garcia self.assert_qmp(result, 'return', {}) 5884c5393f1SAlberto Garcia 5894c5393f1SAlberto Garcia # Create a throttle-group object 5904c5393f1SAlberto Garcia opts = { 'qom-type': 'throttle-group', 'id': 'group0', 5914c5393f1SAlberto Garcia 'limits': { 'iops-total': 1000 } } 5924c5393f1SAlberto Garcia result = self.vm.qmp('object-add', conv_keys = False, **opts) 5934c5393f1SAlberto Garcia self.assert_qmp(result, 'return', {}) 5944c5393f1SAlberto Garcia 5954c5393f1SAlberto Garcia # Add a throttle filter with the group that we just created. 5964c5393f1SAlberto Garcia # The filter is not used by anyone yet 5974c5393f1SAlberto Garcia opts = { 'driver': 'throttle', 'node-name': 'throttle0', 5984c5393f1SAlberto Garcia 'throttle-group': 'group0', 'file': 'hd0-file' } 5994c5393f1SAlberto Garcia result = self.vm.qmp('blockdev-add', conv_keys = False, **opts) 6004c5393f1SAlberto Garcia self.assert_qmp(result, 'return', {}) 6014c5393f1SAlberto Garcia 6024c5393f1SAlberto Garcia # Insert the throttle filter between hd0 and hd0-file 6034c5393f1SAlberto Garcia self.reopen(hd0_opts, {'file': 'throttle0'}) 6044c5393f1SAlberto Garcia 6054c5393f1SAlberto Garcia # Remove the throttle filter from hd0 6064c5393f1SAlberto Garcia self.reopen(hd0_opts, {'file': 'hd0-file'}) 6074c5393f1SAlberto Garcia 6084c5393f1SAlberto Garcia # Insert (and remove) a compress filter 6094c5393f1SAlberto Garcia def test_insert_compress_filter(self): 6104c5393f1SAlberto Garcia # Add an image to the VM: hd (raw) -> hd0 (qcow2) -> hd0-file (file) 6114c5393f1SAlberto Garcia opts = {'driver': 'raw', 'node-name': 'hd', 'file': hd_opts(0)} 6124c5393f1SAlberto Garcia result = self.vm.qmp('blockdev-add', conv_keys = False, **opts) 6134c5393f1SAlberto Garcia self.assert_qmp(result, 'return', {}) 6144c5393f1SAlberto Garcia 6154c5393f1SAlberto Garcia # Add a 'compress' filter 6164c5393f1SAlberto Garcia filter_opts = {'driver': 'compress', 6174c5393f1SAlberto Garcia 'node-name': 'compress0', 6184c5393f1SAlberto Garcia 'file': 'hd0'} 6194c5393f1SAlberto Garcia result = self.vm.qmp('blockdev-add', conv_keys = False, **filter_opts) 6204c5393f1SAlberto Garcia self.assert_qmp(result, 'return', {}) 6214c5393f1SAlberto Garcia 6224c5393f1SAlberto Garcia # Unmap the beginning of the image (we cannot write compressed 6234c5393f1SAlberto Garcia # data to an allocated cluster) 6244c5393f1SAlberto Garcia self.run_qemu_io("hd", "write -z -u 0 128k") 6254c5393f1SAlberto Garcia 6264c5393f1SAlberto Garcia # Write data to the first cluster 6274c5393f1SAlberto Garcia self.run_qemu_io("hd", "write -P 0xa0 0 64k") 6284c5393f1SAlberto Garcia 6294c5393f1SAlberto Garcia # Insert the filter then write to the second cluster 6304c5393f1SAlberto Garcia # hd -> compress0 -> hd0 -> hd0-file 6314c5393f1SAlberto Garcia self.reopen(opts, {'file': 'compress0'}) 6324c5393f1SAlberto Garcia self.run_qemu_io("hd", "write -P 0xa1 64k 64k") 6334c5393f1SAlberto Garcia 6344c5393f1SAlberto Garcia # Remove the filter then write to the third cluster 6354c5393f1SAlberto Garcia # hd -> hd0 -> hd0-file 6364c5393f1SAlberto Garcia self.reopen(opts, {'file': 'hd0'}) 6374c5393f1SAlberto Garcia self.run_qemu_io("hd", "write -P 0xa2 128k 64k") 6384c5393f1SAlberto Garcia 6394c5393f1SAlberto Garcia # Verify the data that we just wrote 6404c5393f1SAlberto Garcia self.run_qemu_io("hd", "read -P 0xa0 0 64k") 6414c5393f1SAlberto Garcia self.run_qemu_io("hd", "read -P 0xa1 64k 64k") 6424c5393f1SAlberto Garcia self.run_qemu_io("hd", "read -P 0xa2 128k 64k") 6434c5393f1SAlberto Garcia 6444c5393f1SAlberto Garcia self.vm.shutdown() 6454c5393f1SAlberto Garcia 6464c5393f1SAlberto Garcia # Check the first byte of the first three L2 entries and verify that 6474c5393f1SAlberto Garcia # the second one is compressed (0x40) while the others are not (0x80) 6484c5393f1SAlberto Garcia iotests.qemu_io_log('-f', 'raw', '-c', 'read -P 0x80 0x40000 1', 6494c5393f1SAlberto Garcia '-c', 'read -P 0x40 0x40008 1', 6504c5393f1SAlberto Garcia '-c', 'read -P 0x80 0x40010 1', hd_path[0]) 6514c5393f1SAlberto Garcia 652246ebc2dSAlberto Garcia # Swap the disk images of two active block devices 653246ebc2dSAlberto Garcia def test_swap_files(self): 654246ebc2dSAlberto Garcia # Add hd0 and hd2 (none of them with backing files) 655246ebc2dSAlberto Garcia opts0 = hd_opts(0) 656246ebc2dSAlberto Garcia result = self.vm.qmp('blockdev-add', conv_keys = False, **opts0) 657246ebc2dSAlberto Garcia self.assert_qmp(result, 'return', {}) 658246ebc2dSAlberto Garcia 659246ebc2dSAlberto Garcia opts2 = hd_opts(2) 660246ebc2dSAlberto Garcia result = self.vm.qmp('blockdev-add', conv_keys = False, **opts2) 661246ebc2dSAlberto Garcia self.assert_qmp(result, 'return', {}) 662246ebc2dSAlberto Garcia 663246ebc2dSAlberto Garcia # Write different data to both block devices 664246ebc2dSAlberto Garcia self.run_qemu_io("hd0", "write -P 0xa0 0 1k") 665246ebc2dSAlberto Garcia self.run_qemu_io("hd2", "write -P 0xa2 0 1k") 666246ebc2dSAlberto Garcia 667246ebc2dSAlberto Garcia # Check that the data reads correctly 668246ebc2dSAlberto Garcia self.run_qemu_io("hd0", "read -P 0xa0 0 1k") 669246ebc2dSAlberto Garcia self.run_qemu_io("hd2", "read -P 0xa2 0 1k") 670246ebc2dSAlberto Garcia 671246ebc2dSAlberto Garcia # It's not possible to make a block device use an image that 672246ebc2dSAlberto Garcia # is already being used by the other device. 673246ebc2dSAlberto Garcia self.reopen(opts0, {'file': 'hd2-file'}, 674246ebc2dSAlberto Garcia "Permission conflict on node 'hd2-file': permissions " 675246ebc2dSAlberto Garcia "'write, resize' are both required by node 'hd2' (uses " 676246ebc2dSAlberto Garcia "node 'hd2-file' as 'file' child) and unshared by node " 677246ebc2dSAlberto Garcia "'hd0' (uses node 'hd2-file' as 'file' child).") 678246ebc2dSAlberto Garcia self.reopen(opts2, {'file': 'hd0-file'}, 679246ebc2dSAlberto Garcia "Permission conflict on node 'hd0-file': permissions " 680246ebc2dSAlberto Garcia "'write, resize' are both required by node 'hd0' (uses " 681246ebc2dSAlberto Garcia "node 'hd0-file' as 'file' child) and unshared by node " 682246ebc2dSAlberto Garcia "'hd2' (uses node 'hd0-file' as 'file' child).") 683246ebc2dSAlberto Garcia 684246ebc2dSAlberto Garcia # But we can swap the images if we reopen both devices at the 685246ebc2dSAlberto Garcia # same time 686246ebc2dSAlberto Garcia opts0['file'] = 'hd2-file' 687246ebc2dSAlberto Garcia opts2['file'] = 'hd0-file' 688246ebc2dSAlberto Garcia self.reopenMultiple([opts0, opts2]) 689246ebc2dSAlberto Garcia self.run_qemu_io("hd0", "read -P 0xa2 0 1k") 690246ebc2dSAlberto Garcia self.run_qemu_io("hd2", "read -P 0xa0 0 1k") 691246ebc2dSAlberto Garcia 692246ebc2dSAlberto Garcia # And we can of course come back to the original state 693246ebc2dSAlberto Garcia opts0['file'] = 'hd0-file' 694246ebc2dSAlberto Garcia opts2['file'] = 'hd2-file' 695246ebc2dSAlberto Garcia self.reopenMultiple([opts0, opts2]) 696246ebc2dSAlberto Garcia self.run_qemu_io("hd0", "read -P 0xa0 0 1k") 697246ebc2dSAlberto Garcia self.run_qemu_io("hd2", "read -P 0xa2 0 1k") 698246ebc2dSAlberto Garcia 699bf3e50f6SAlberto Garcia # Misc reopen tests with different block drivers 7009442bebeSThomas Huth @iotests.skip_if_unsupported(['quorum', 'throttle']) 701bf3e50f6SAlberto Garcia def test_misc_drivers(self): 702bf3e50f6SAlberto Garcia #################### 703bf3e50f6SAlberto Garcia ###### quorum ###### 704bf3e50f6SAlberto Garcia #################### 705bf3e50f6SAlberto Garcia for i in range(3): 706bf3e50f6SAlberto Garcia opts = hd_opts(i) 707bf3e50f6SAlberto Garcia # Open all three images without backing file 708bf3e50f6SAlberto Garcia opts['backing'] = None 709bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-add', conv_keys = False, **opts) 710bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 711bf3e50f6SAlberto Garcia 712bf3e50f6SAlberto Garcia opts = {'driver': 'quorum', 713bf3e50f6SAlberto Garcia 'node-name': 'quorum0', 714bf3e50f6SAlberto Garcia 'children': ['hd0', 'hd1', 'hd2'], 715bf3e50f6SAlberto Garcia 'vote-threshold': 2} 716bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-add', conv_keys = False, **opts) 717bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 718bf3e50f6SAlberto Garcia 719bf3e50f6SAlberto Garcia # Quorum doesn't currently allow reopening. TODO: implement this 720bf3e50f6SAlberto Garcia self.reopen(opts, {}, "Block format 'quorum' used by node 'quorum0'" + 721bf3e50f6SAlberto Garcia " does not support reopening files") 722bf3e50f6SAlberto Garcia 723bf3e50f6SAlberto Garcia # You can't make quorum0 a backing file of hd0: 724bf3e50f6SAlberto Garcia # hd0 is already a child of quorum0. 725bf3e50f6SAlberto Garcia self.reopen(hd_opts(0), {'backing': 'quorum0'}, 726ecd30d2dSAlberto Garcia "Making 'quorum0' a backing child of 'hd0' would create a cycle") 727bf3e50f6SAlberto Garcia 728bf3e50f6SAlberto Garcia # Delete quorum0 729bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'quorum0') 730bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 731bf3e50f6SAlberto Garcia 732bf3e50f6SAlberto Garcia # Delete hd0, hd1 and hd2 733bf3e50f6SAlberto Garcia for i in range(3): 734bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-del', conv_keys = True, 735bf3e50f6SAlberto Garcia node_name = 'hd%d' % i) 736bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 737bf3e50f6SAlberto Garcia 738bf3e50f6SAlberto Garcia ###################### 739bf3e50f6SAlberto Garcia ###### blkdebug ###### 740bf3e50f6SAlberto Garcia ###################### 741bf3e50f6SAlberto Garcia opts = {'driver': 'blkdebug', 742bf3e50f6SAlberto Garcia 'node-name': 'bd', 743bf3e50f6SAlberto Garcia 'config': '/dev/null', 744bf3e50f6SAlberto Garcia 'image': hd_opts(0)} 745bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-add', conv_keys = False, **opts) 746bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 747bf3e50f6SAlberto Garcia 748bf3e50f6SAlberto Garcia # blkdebug allows reopening if we keep the same options 749bf3e50f6SAlberto Garcia self.reopen(opts) 750bf3e50f6SAlberto Garcia 751bf3e50f6SAlberto Garcia # but it currently does not allow changes 752bf3e50f6SAlberto Garcia self.reopen(opts, {'image': 'hd1'}, "Cannot change the option 'image'") 753bf3e50f6SAlberto Garcia self.reopen(opts, {'align': 33554432}, "Cannot change the option 'align'") 754bf3e50f6SAlberto Garcia self.reopen(opts, {'config': '/non/existent'}, "Cannot change the option 'config'") 755bf3e50f6SAlberto Garcia del opts['config'] 756bf3e50f6SAlberto Garcia self.reopen(opts, {}, "Option 'config' cannot be reset to its default value") 757bf3e50f6SAlberto Garcia 758bf3e50f6SAlberto Garcia # Delete the blkdebug node 759bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'bd') 760bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 761bf3e50f6SAlberto Garcia 762bf3e50f6SAlberto Garcia ################## 763bf3e50f6SAlberto Garcia ###### null ###### 764bf3e50f6SAlberto Garcia ################## 765a6f8f9f8SMax Reitz opts = {'driver': 'null-co', 'node-name': 'root', 'size': 1024} 766bf3e50f6SAlberto Garcia 767bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-add', conv_keys = False, **opts) 768bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 769bf3e50f6SAlberto Garcia 770bf3e50f6SAlberto Garcia # 1 << 30 is the default value, but we cannot change it explicitly 771bf3e50f6SAlberto Garcia self.reopen(opts, {'size': (1 << 30)}, "Cannot change the option 'size'") 772bf3e50f6SAlberto Garcia 773bf3e50f6SAlberto Garcia # We cannot change 'size' back to its default value either 774bf3e50f6SAlberto Garcia del opts['size'] 775bf3e50f6SAlberto Garcia self.reopen(opts, {}, "Option 'size' cannot be reset to its default value") 776bf3e50f6SAlberto Garcia 777bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'root') 778bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 779bf3e50f6SAlberto Garcia 780bf3e50f6SAlberto Garcia ################## 781bf3e50f6SAlberto Garcia ###### file ###### 782bf3e50f6SAlberto Garcia ################## 783bf3e50f6SAlberto Garcia opts = hd_opts(0) 784bf3e50f6SAlberto Garcia opts['file']['locking'] = 'on' 785bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-add', conv_keys = False, **opts) 786bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 787bf3e50f6SAlberto Garcia 788bf3e50f6SAlberto Garcia # 'locking' cannot be changed 789bf3e50f6SAlberto Garcia del opts['file']['locking'] 790bf3e50f6SAlberto Garcia self.reopen(opts, {'file.locking': 'on'}) 791bf3e50f6SAlberto Garcia self.reopen(opts, {'file.locking': 'off'}, "Cannot change the option 'locking'") 792bf3e50f6SAlberto Garcia self.reopen(opts, {}, "Option 'locking' cannot be reset to its default value") 793bf3e50f6SAlberto Garcia 794bf3e50f6SAlberto Garcia # Trying to reopen the 'file' node directly does not make a difference 795bf3e50f6SAlberto Garcia opts = opts['file'] 796bf3e50f6SAlberto Garcia self.reopen(opts, {'locking': 'on'}) 797bf3e50f6SAlberto Garcia self.reopen(opts, {'locking': 'off'}, "Cannot change the option 'locking'") 798bf3e50f6SAlberto Garcia self.reopen(opts, {}, "Option 'locking' cannot be reset to its default value") 799bf3e50f6SAlberto Garcia 800bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0') 801bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 802bf3e50f6SAlberto Garcia 803bf3e50f6SAlberto Garcia ###################### 804bf3e50f6SAlberto Garcia ###### throttle ###### 805bf3e50f6SAlberto Garcia ###################### 806bf3e50f6SAlberto Garcia opts = { 'qom-type': 'throttle-group', 'id': 'group0', 807fa818b2fSAlberto Garcia 'limits': { 'iops-total': 1000 } } 808bf3e50f6SAlberto Garcia result = self.vm.qmp('object-add', conv_keys = False, **opts) 809bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 810bf3e50f6SAlberto Garcia 811bf3e50f6SAlberto Garcia opts = { 'qom-type': 'throttle-group', 'id': 'group1', 812fa818b2fSAlberto Garcia 'limits': { 'iops-total': 2000 } } 813bf3e50f6SAlberto Garcia result = self.vm.qmp('object-add', conv_keys = False, **opts) 814bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 815bf3e50f6SAlberto Garcia 816bf3e50f6SAlberto Garcia # Add a throttle filter with group = group0 817bf3e50f6SAlberto Garcia opts = { 'driver': 'throttle', 'node-name': 'throttle0', 818bf3e50f6SAlberto Garcia 'throttle-group': 'group0', 'file': hd_opts(0) } 819bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-add', conv_keys = False, **opts) 820bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 821bf3e50f6SAlberto Garcia 822bf3e50f6SAlberto Garcia # We can reopen it if we keep the same options 823bf3e50f6SAlberto Garcia self.reopen(opts) 824bf3e50f6SAlberto Garcia 825bf3e50f6SAlberto Garcia # We can also reopen if 'file' is a reference to the child 826bf3e50f6SAlberto Garcia self.reopen(opts, {'file': 'hd0'}) 827bf3e50f6SAlberto Garcia 828bf3e50f6SAlberto Garcia # This is illegal 829bf3e50f6SAlberto Garcia self.reopen(opts, {'throttle-group': 'notfound'}, "Throttle group 'notfound' does not exist") 830bf3e50f6SAlberto Garcia 831bf3e50f6SAlberto Garcia # But it's possible to change the group to group1 832bf3e50f6SAlberto Garcia self.reopen(opts, {'throttle-group': 'group1'}) 833bf3e50f6SAlberto Garcia 834bf3e50f6SAlberto Garcia # Now group1 is in use, it cannot be deleted 835bf3e50f6SAlberto Garcia result = self.vm.qmp('object-del', id = 'group1') 836bf3e50f6SAlberto Garcia self.assert_qmp(result, 'error/class', 'GenericError') 837bf3e50f6SAlberto Garcia self.assert_qmp(result, 'error/desc', "object 'group1' is in use, can not be deleted") 838bf3e50f6SAlberto Garcia 839bf3e50f6SAlberto Garcia # Default options, this switches the group back to group0 840bf3e50f6SAlberto Garcia self.reopen(opts) 841bf3e50f6SAlberto Garcia 842bf3e50f6SAlberto Garcia # So now we cannot delete group0 843bf3e50f6SAlberto Garcia result = self.vm.qmp('object-del', id = 'group0') 844bf3e50f6SAlberto Garcia self.assert_qmp(result, 'error/class', 'GenericError') 845bf3e50f6SAlberto Garcia self.assert_qmp(result, 'error/desc', "object 'group0' is in use, can not be deleted") 846bf3e50f6SAlberto Garcia 847bf3e50f6SAlberto Garcia # But group1 is free this time, and it can be deleted 848bf3e50f6SAlberto Garcia result = self.vm.qmp('object-del', id = 'group1') 849bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 850bf3e50f6SAlberto Garcia 851bf3e50f6SAlberto Garcia # Let's delete the filter node 852bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'throttle0') 853bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 854bf3e50f6SAlberto Garcia 855bf3e50f6SAlberto Garcia # And we can finally get rid of group0 856bf3e50f6SAlberto Garcia result = self.vm.qmp('object-del', id = 'group0') 857bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 858bf3e50f6SAlberto Garcia 859bf3e50f6SAlberto Garcia # If an image has a backing file then the 'backing' option must be 860bf3e50f6SAlberto Garcia # passed on reopen. We don't allow leaving the option out in this 861bf3e50f6SAlberto Garcia # case because it's unclear what the correct semantics would be. 862bf3e50f6SAlberto Garcia def test_missing_backing_options_1(self): 863bf3e50f6SAlberto Garcia # hd2 864bf3e50f6SAlberto Garcia opts = hd_opts(2) 865bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-add', conv_keys = False, **opts) 866bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 867bf3e50f6SAlberto Garcia 868bf3e50f6SAlberto Garcia # hd0 869bf3e50f6SAlberto Garcia opts = hd_opts(0) 870bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-add', conv_keys = False, **opts) 871bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 872bf3e50f6SAlberto Garcia 873bf3e50f6SAlberto Garcia # hd0 has no backing file: we can omit the 'backing' option 874bf3e50f6SAlberto Garcia self.reopen(opts) 875bf3e50f6SAlberto Garcia 876bf3e50f6SAlberto Garcia # hd2 <- hd0 877bf3e50f6SAlberto Garcia self.reopen(opts, {'backing': 'hd2'}) 878bf3e50f6SAlberto Garcia 879bf3e50f6SAlberto Garcia # hd0 has a backing file: we must set the 'backing' option 880bf3e50f6SAlberto Garcia self.reopen(opts, {}, "backing is missing for 'hd0'") 881bf3e50f6SAlberto Garcia 882bf3e50f6SAlberto Garcia # hd2 can't be removed because it's the backing file of hd0 883bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd2') 884bf3e50f6SAlberto Garcia self.assert_qmp(result, 'error/class', 'GenericError') 885bf3e50f6SAlberto Garcia self.assert_qmp(result, 'error/desc', "Node 'hd2' is busy: node is used as backing hd of 'hd0'") 886bf3e50f6SAlberto Garcia 887bf3e50f6SAlberto Garcia # Detach hd2 from hd0. 888bf3e50f6SAlberto Garcia self.reopen(opts, {'backing': None}) 8890b877d09SMax Reitz 8900b877d09SMax Reitz # Without a backing file, we can omit 'backing' again 8910b877d09SMax Reitz self.reopen(opts) 892bf3e50f6SAlberto Garcia 893bf3e50f6SAlberto Garcia # Remove both hd0 and hd2 894bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0') 895bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 896bf3e50f6SAlberto Garcia 897bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd2') 898bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 899bf3e50f6SAlberto Garcia 900bf3e50f6SAlberto Garcia # If an image has default backing file (as part of its metadata) 901bf3e50f6SAlberto Garcia # then the 'backing' option must be passed on reopen. We don't 902bf3e50f6SAlberto Garcia # allow leaving the option out in this case because it's unclear 903bf3e50f6SAlberto Garcia # what the correct semantics would be. 904bf3e50f6SAlberto Garcia def test_missing_backing_options_2(self): 905bf3e50f6SAlberto Garcia # hd0 <- hd1 906bf3e50f6SAlberto Garcia # (hd0 is hd1's default backing file) 907bf3e50f6SAlberto Garcia opts = hd_opts(1) 908bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-add', conv_keys = False, **opts) 909bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 910bf3e50f6SAlberto Garcia 911bf3e50f6SAlberto Garcia # hd1 has a backing file: we can't omit the 'backing' option 912bf3e50f6SAlberto Garcia self.reopen(opts, {}, "backing is missing for 'hd1'") 913bf3e50f6SAlberto Garcia 914bf3e50f6SAlberto Garcia # Let's detach the backing file 915bf3e50f6SAlberto Garcia self.reopen(opts, {'backing': None}) 916bf3e50f6SAlberto Garcia 917bf3e50f6SAlberto Garcia # No backing file attached to hd1 now, but we still can't omit the 'backing' option 918bf3e50f6SAlberto Garcia self.reopen(opts, {}, "backing is missing for 'hd1'") 919bf3e50f6SAlberto Garcia 920bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd1') 921bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 922bf3e50f6SAlberto Garcia 923bf3e50f6SAlberto Garcia # Test that making 'backing' a reference to an existing child 924bf3e50f6SAlberto Garcia # keeps its current options 925bf3e50f6SAlberto Garcia def test_backing_reference(self): 926bf3e50f6SAlberto Garcia # hd2 <- hd1 <- hd0 927bf3e50f6SAlberto Garcia opts = hd_opts(0) 928bf3e50f6SAlberto Garcia opts['backing'] = hd_opts(1) 929bf3e50f6SAlberto Garcia opts['backing']['backing'] = hd_opts(2) 930bf3e50f6SAlberto Garcia # Enable 'detect-zeroes' on all three nodes 931bf3e50f6SAlberto Garcia opts['detect-zeroes'] = 'on' 932bf3e50f6SAlberto Garcia opts['backing']['detect-zeroes'] = 'on' 933bf3e50f6SAlberto Garcia opts['backing']['backing']['detect-zeroes'] = 'on' 934bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-add', conv_keys = False, **opts) 935bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 936bf3e50f6SAlberto Garcia 937bf3e50f6SAlberto Garcia # Reopen the chain passing the minimum amount of required options. 938bf3e50f6SAlberto Garcia # By making 'backing' a reference to hd1 (instead of a sub-dict) 939bf3e50f6SAlberto Garcia # we tell QEMU to keep its current set of options. 940bf3e50f6SAlberto Garcia opts = {'driver': iotests.imgfmt, 941bf3e50f6SAlberto Garcia 'node-name': 'hd0', 942bf3e50f6SAlberto Garcia 'file': 'hd0-file', 943bf3e50f6SAlberto Garcia 'backing': 'hd1' } 944bf3e50f6SAlberto Garcia self.reopen(opts) 945bf3e50f6SAlberto Garcia 946bf3e50f6SAlberto Garcia # This has reset 'detect-zeroes' on hd0, but not on hd1 and hd2. 947bf3e50f6SAlberto Garcia self.assert_qmp(self.get_node('hd0'), 'detect_zeroes', 'off') 948bf3e50f6SAlberto Garcia self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'on') 949bf3e50f6SAlberto Garcia self.assert_qmp(self.get_node('hd2'), 'detect_zeroes', 'on') 950bf3e50f6SAlberto Garcia 951bf3e50f6SAlberto Garcia # Test what happens if the graph changes due to other operations 952bf3e50f6SAlberto Garcia # such as block-stream 953bf3e50f6SAlberto Garcia def test_block_stream_1(self): 954bf3e50f6SAlberto Garcia # hd1 <- hd0 955bf3e50f6SAlberto Garcia opts = hd_opts(0) 956bf3e50f6SAlberto Garcia opts['backing'] = hd_opts(1) 957bf3e50f6SAlberto Garcia opts['backing']['backing'] = None 958bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-add', conv_keys = False, **opts) 959bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 960bf3e50f6SAlberto Garcia 961bf3e50f6SAlberto Garcia # Stream hd1 into hd0 and wait until it's done 962bf3e50f6SAlberto Garcia result = self.vm.qmp('block-stream', conv_keys = True, job_id = 'stream0', device = 'hd0') 963bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 964bf3e50f6SAlberto Garcia self.wait_until_completed(drive = 'stream0') 965bf3e50f6SAlberto Garcia 966bf3e50f6SAlberto Garcia # Now we have only hd0 967bf3e50f6SAlberto Garcia self.assertEqual(self.get_node('hd1'), None) 968bf3e50f6SAlberto Garcia 969bf3e50f6SAlberto Garcia # We have backing.* options but there's no backing file anymore 970bf3e50f6SAlberto Garcia self.reopen(opts, {}, "Cannot change the option 'backing.driver'") 971bf3e50f6SAlberto Garcia 972bf3e50f6SAlberto Garcia # If we remove the 'backing' option then we can reopen hd0 just fine 973bf3e50f6SAlberto Garcia del opts['backing'] 974bf3e50f6SAlberto Garcia self.reopen(opts) 975bf3e50f6SAlberto Garcia 976bf3e50f6SAlberto Garcia # We can also reopen hd0 if we set 'backing' to null 977bf3e50f6SAlberto Garcia self.reopen(opts, {'backing': None}) 978bf3e50f6SAlberto Garcia 979bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0') 980bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 981bf3e50f6SAlberto Garcia 982bf3e50f6SAlberto Garcia # Another block_stream test 983bf3e50f6SAlberto Garcia def test_block_stream_2(self): 984bf3e50f6SAlberto Garcia # hd2 <- hd1 <- hd0 985bf3e50f6SAlberto Garcia opts = hd_opts(0) 986bf3e50f6SAlberto Garcia opts['backing'] = hd_opts(1) 987bf3e50f6SAlberto Garcia opts['backing']['backing'] = hd_opts(2) 988bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-add', conv_keys = False, **opts) 989bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 990bf3e50f6SAlberto Garcia 991bf3e50f6SAlberto Garcia # Stream hd1 into hd0 and wait until it's done 992bf3e50f6SAlberto Garcia result = self.vm.qmp('block-stream', conv_keys = True, job_id = 'stream0', 993bf3e50f6SAlberto Garcia device = 'hd0', base_node = 'hd2') 994bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 995bf3e50f6SAlberto Garcia self.wait_until_completed(drive = 'stream0') 996bf3e50f6SAlberto Garcia 997bf3e50f6SAlberto Garcia # The chain is hd2 <- hd0 now. hd1 is missing 998bf3e50f6SAlberto Garcia self.assertEqual(self.get_node('hd1'), None) 999bf3e50f6SAlberto Garcia 1000bf3e50f6SAlberto Garcia # The backing options in the dict were meant for hd1, but we cannot 1001bf3e50f6SAlberto Garcia # use them with hd2 because hd1 had a backing file while hd2 does not. 1002bf3e50f6SAlberto Garcia self.reopen(opts, {}, "Cannot change the option 'backing.driver'") 1003bf3e50f6SAlberto Garcia 1004bf3e50f6SAlberto Garcia # If we remove hd1's options from the dict then things work fine 1005bf3e50f6SAlberto Garcia opts['backing'] = opts['backing']['backing'] 1006bf3e50f6SAlberto Garcia self.reopen(opts) 1007bf3e50f6SAlberto Garcia 1008bf3e50f6SAlberto Garcia # We can also reopen hd0 if we use a reference to the backing file 1009bf3e50f6SAlberto Garcia self.reopen(opts, {'backing': 'hd2'}) 1010bf3e50f6SAlberto Garcia 1011bf3e50f6SAlberto Garcia # But we cannot leave the option out 1012bf3e50f6SAlberto Garcia del opts['backing'] 1013bf3e50f6SAlberto Garcia self.reopen(opts, {}, "backing is missing for 'hd0'") 1014bf3e50f6SAlberto Garcia 1015bf3e50f6SAlberto Garcia # Now we can delete hd0 (and hd2) 1016bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0') 1017bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 1018bf3e50f6SAlberto Garcia self.assertEqual(self.get_node('hd2'), None) 1019bf3e50f6SAlberto Garcia 1020bf3e50f6SAlberto Garcia # Reopen the chain during a block-stream job (from hd1 to hd0) 1021bf3e50f6SAlberto Garcia def test_block_stream_3(self): 1022bf3e50f6SAlberto Garcia # hd2 <- hd1 <- hd0 1023bf3e50f6SAlberto Garcia opts = hd_opts(0) 1024bf3e50f6SAlberto Garcia opts['backing'] = hd_opts(1) 1025bf3e50f6SAlberto Garcia opts['backing']['backing'] = hd_opts(2) 1026bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-add', conv_keys = False, **opts) 1027bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 1028bf3e50f6SAlberto Garcia 1029bf3e50f6SAlberto Garcia # hd2 <- hd0 1030bf3e50f6SAlberto Garcia result = self.vm.qmp('block-stream', conv_keys = True, job_id = 'stream0', 1031c423a6afSMax Reitz device = 'hd0', base_node = 'hd2', 1032c423a6afSMax Reitz auto_finalize = False) 1033bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 1034bf3e50f6SAlberto Garcia 1035c624b015SAndrey Shinkevich # We can remove hd2 while the stream job is ongoing 1036bf3e50f6SAlberto Garcia opts['backing']['backing'] = None 1037c624b015SAndrey Shinkevich self.reopen(opts, {}) 1038bf3e50f6SAlberto Garcia 1039bf3e50f6SAlberto Garcia # We can't remove hd1 while the stream job is ongoing 1040bf3e50f6SAlberto Garcia opts['backing'] = None 1041bfae052aSVladimir Sementsov-Ogievskiy self.reopen(opts, {}, "Cannot change frozen 'backing' link from 'hd0' to 'hd1'") 1042bf3e50f6SAlberto Garcia 1043c423a6afSMax Reitz self.vm.run_job('stream0', auto_finalize = False, auto_dismiss = True) 1044bf3e50f6SAlberto Garcia 1045bf3e50f6SAlberto Garcia # Reopen the chain during a block-stream job (from hd2 to hd1) 1046bf3e50f6SAlberto Garcia def test_block_stream_4(self): 1047bf3e50f6SAlberto Garcia # hd2 <- hd1 <- hd0 1048bf3e50f6SAlberto Garcia opts = hd_opts(0) 1049bf3e50f6SAlberto Garcia opts['backing'] = hd_opts(1) 1050bf3e50f6SAlberto Garcia opts['backing']['backing'] = hd_opts(2) 1051bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-add', conv_keys = False, **opts) 1052bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 1053bf3e50f6SAlberto Garcia 1054bf3e50f6SAlberto Garcia # hd1 <- hd0 1055bf3e50f6SAlberto Garcia result = self.vm.qmp('block-stream', conv_keys = True, job_id = 'stream0', 1056205736f4SAndrey Shinkevich device = 'hd1', filter_node_name='cor', 1057205736f4SAndrey Shinkevich auto_finalize = False) 1058bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 1059bf3e50f6SAlberto Garcia 1060205736f4SAndrey Shinkevich # We can't reopen with the original options because there is a filter 1061205736f4SAndrey Shinkevich # inserted by stream job above hd1. 1062c423a6afSMax Reitz self.reopen(opts, {}, 1063205736f4SAndrey Shinkevich "Cannot change the option 'backing.backing.file.node-name'") 1064205736f4SAndrey Shinkevich 1065205736f4SAndrey Shinkevich # We can't reopen hd1 to read-only, as block-stream requires it to be 1066205736f4SAndrey Shinkevich # read-write 1067205736f4SAndrey Shinkevich self.reopen(opts['backing'], {'read-only': True}, 1068c20555e1SVladimir Sementsov-Ogievskiy "Read-only block node 'hd1' cannot support read-write users") 1069bf3e50f6SAlberto Garcia 1070bf3e50f6SAlberto Garcia # We can't remove hd2 while the stream job is ongoing 1071bf3e50f6SAlberto Garcia opts['backing']['backing'] = None 1072205736f4SAndrey Shinkevich self.reopen(opts['backing'], {'read-only': False}, 1073bfae052aSVladimir Sementsov-Ogievskiy "Cannot change frozen 'backing' link from 'hd1' to 'hd2'") 1074bf3e50f6SAlberto Garcia 1075bf3e50f6SAlberto Garcia # We can detach hd1 from hd0 because it doesn't affect the stream job 1076bf3e50f6SAlberto Garcia opts['backing'] = None 1077bf3e50f6SAlberto Garcia self.reopen(opts) 1078bf3e50f6SAlberto Garcia 1079c423a6afSMax Reitz self.vm.run_job('stream0', auto_finalize = False, auto_dismiss = True) 1080bf3e50f6SAlberto Garcia 1081bf3e50f6SAlberto Garcia # Reopen the chain during a block-commit job (from hd0 to hd2) 1082bf3e50f6SAlberto Garcia def test_block_commit_1(self): 1083bf3e50f6SAlberto Garcia # hd2 <- hd1 <- hd0 1084bf3e50f6SAlberto Garcia opts = hd_opts(0) 1085bf3e50f6SAlberto Garcia opts['backing'] = hd_opts(1) 1086bf3e50f6SAlberto Garcia opts['backing']['backing'] = hd_opts(2) 1087bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-add', conv_keys = False, **opts) 1088bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 1089bf3e50f6SAlberto Garcia 1090bf3e50f6SAlberto Garcia result = self.vm.qmp('block-commit', conv_keys = True, job_id = 'commit0', 1091c423a6afSMax Reitz device = 'hd0') 1092bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 1093bf3e50f6SAlberto Garcia 1094bf3e50f6SAlberto Garcia # We can't remove hd2 while the commit job is ongoing 1095bf3e50f6SAlberto Garcia opts['backing']['backing'] = None 1096bfae052aSVladimir Sementsov-Ogievskiy self.reopen(opts, {}, "Cannot change frozen 'backing' link from 'hd1' to 'hd2'") 1097bf3e50f6SAlberto Garcia 1098bf3e50f6SAlberto Garcia # We can't remove hd1 while the commit job is ongoing 1099bf3e50f6SAlberto Garcia opts['backing'] = None 1100bfae052aSVladimir Sementsov-Ogievskiy self.reopen(opts, {}, "Cannot change frozen 'backing' link from 'hd0' to 'hd1'") 1101bf3e50f6SAlberto Garcia 1102bf3e50f6SAlberto Garcia event = self.vm.event_wait(name='BLOCK_JOB_READY') 1103bf3e50f6SAlberto Garcia self.assert_qmp(event, 'data/device', 'commit0') 1104bf3e50f6SAlberto Garcia self.assert_qmp(event, 'data/type', 'commit') 1105bf3e50f6SAlberto Garcia self.assert_qmp_absent(event, 'data/error') 1106bf3e50f6SAlberto Garcia 1107bf3e50f6SAlberto Garcia result = self.vm.qmp('block-job-complete', device='commit0') 1108bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 1109bf3e50f6SAlberto Garcia 1110bf3e50f6SAlberto Garcia self.wait_until_completed(drive = 'commit0') 1111bf3e50f6SAlberto Garcia 1112bf3e50f6SAlberto Garcia # Reopen the chain during a block-commit job (from hd1 to hd2) 1113bf3e50f6SAlberto Garcia def test_block_commit_2(self): 1114bf3e50f6SAlberto Garcia # hd2 <- hd1 <- hd0 1115bf3e50f6SAlberto Garcia opts = hd_opts(0) 1116bf3e50f6SAlberto Garcia opts['backing'] = hd_opts(1) 1117bf3e50f6SAlberto Garcia opts['backing']['backing'] = hd_opts(2) 1118bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-add', conv_keys = False, **opts) 1119bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 1120bf3e50f6SAlberto Garcia 1121bf3e50f6SAlberto Garcia result = self.vm.qmp('block-commit', conv_keys = True, job_id = 'commit0', 1122c423a6afSMax Reitz device = 'hd0', top_node = 'hd1', 1123c423a6afSMax Reitz auto_finalize = False) 1124bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 1125bf3e50f6SAlberto Garcia 1126bf3e50f6SAlberto Garcia # We can't remove hd2 while the commit job is ongoing 1127bf3e50f6SAlberto Garcia opts['backing']['backing'] = None 1128bf3e50f6SAlberto Garcia self.reopen(opts, {}, "Cannot change the option 'backing.driver'") 1129bf3e50f6SAlberto Garcia 1130bf3e50f6SAlberto Garcia # We can't remove hd1 while the commit job is ongoing 1131bf3e50f6SAlberto Garcia opts['backing'] = None 1132ecd30d2dSAlberto Garcia self.reopen(opts, {}, "Cannot replace implicit backing child of hd0") 1133bf3e50f6SAlberto Garcia 1134bf3e50f6SAlberto Garcia # hd2 <- hd0 1135c423a6afSMax Reitz self.vm.run_job('commit0', auto_finalize = False, auto_dismiss = True) 1136bf3e50f6SAlberto Garcia 1137bf3e50f6SAlberto Garcia self.assert_qmp(self.get_node('hd0'), 'ro', False) 1138bf3e50f6SAlberto Garcia self.assertEqual(self.get_node('hd1'), None) 1139bf3e50f6SAlberto Garcia self.assert_qmp(self.get_node('hd2'), 'ro', True) 1140bf3e50f6SAlberto Garcia 114197518e11SKevin Wolf def run_test_iothreads(self, iothread_a, iothread_b, errmsg = None): 1142bf3e50f6SAlberto Garcia opts = hd_opts(0) 1143bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-add', conv_keys = False, **opts) 1144bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 1145bf3e50f6SAlberto Garcia 1146bf3e50f6SAlberto Garcia opts2 = hd_opts(2) 1147bf3e50f6SAlberto Garcia result = self.vm.qmp('blockdev-add', conv_keys = False, **opts2) 1148bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 1149bf3e50f6SAlberto Garcia 1150bf3e50f6SAlberto Garcia result = self.vm.qmp('object-add', qom_type='iothread', id='iothread0') 1151bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 1152bf3e50f6SAlberto Garcia 1153bf3e50f6SAlberto Garcia result = self.vm.qmp('object-add', qom_type='iothread', id='iothread1') 1154bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 1155bf3e50f6SAlberto Garcia 115697518e11SKevin Wolf result = self.vm.qmp('device_add', driver='virtio-scsi', id='scsi0', 115797518e11SKevin Wolf iothread=iothread_a) 1158bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 1159bf3e50f6SAlberto Garcia 116097518e11SKevin Wolf result = self.vm.qmp('device_add', driver='virtio-scsi', id='scsi1', 116197518e11SKevin Wolf iothread=iothread_b) 1162bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 1163bf3e50f6SAlberto Garcia 116497518e11SKevin Wolf if iothread_a: 116597518e11SKevin Wolf result = self.vm.qmp('device_add', driver='scsi-hd', drive='hd0', 116697518e11SKevin Wolf share_rw=True, bus="scsi0.0") 1167bf3e50f6SAlberto Garcia self.assert_qmp(result, 'return', {}) 1168bf3e50f6SAlberto Garcia 116997518e11SKevin Wolf if iothread_b: 117097518e11SKevin Wolf result = self.vm.qmp('device_add', driver='scsi-hd', drive='hd2', 117197518e11SKevin Wolf share_rw=True, bus="scsi1.0") 117297518e11SKevin Wolf self.assert_qmp(result, 'return', {}) 117397518e11SKevin Wolf 117497518e11SKevin Wolf # Attaching the backing file may or may not work 117597518e11SKevin Wolf self.reopen(opts, {'backing': 'hd2'}, errmsg) 117697518e11SKevin Wolf 117797518e11SKevin Wolf # But removing the backing file should always work 117897518e11SKevin Wolf self.reopen(opts, {'backing': None}) 117997518e11SKevin Wolf 118097518e11SKevin Wolf self.vm.shutdown() 118197518e11SKevin Wolf 118297518e11SKevin Wolf # We don't allow setting a backing file that uses a different AioContext if 118397518e11SKevin Wolf # neither of them can switch to the other AioContext 118497518e11SKevin Wolf def test_iothreads_error(self): 118597518e11SKevin Wolf self.run_test_iothreads('iothread0', 'iothread1', 11861de6b45fSKevin Wolf "Cannot change iothread of active block backend") 118797518e11SKevin Wolf 118897518e11SKevin Wolf def test_iothreads_compatible_users(self): 118997518e11SKevin Wolf self.run_test_iothreads('iothread0', 'iothread0') 119097518e11SKevin Wolf 119197518e11SKevin Wolf def test_iothreads_switch_backing(self): 1192e2c8eb14SKevin Wolf self.run_test_iothreads('iothread0', '') 119397518e11SKevin Wolf 119497518e11SKevin Wolf def test_iothreads_switch_overlay(self): 1195e2c8eb14SKevin Wolf self.run_test_iothreads('', 'iothread0') 1196bf3e50f6SAlberto Garcia 1197bf3e50f6SAlberto Garciaif __name__ == '__main__': 119852ea799eSJohn Snow iotests.activate_logging() 1199103cbc77SMax Reitz iotests.main(supported_fmts=["qcow2"], 1200103cbc77SMax Reitz supported_protocols=["file"]) 1201