xref: /qemu/tests/qemu-iotests/245 (revision 52ea799e)
1903cb1bfSPhilippe Mathieu-Daudé#!/usr/bin/env python3
2bf3e50f6SAlberto Garcia#
3bf3e50f6SAlberto Garcia# Test cases for the QMP 'x-blockdev-reopen' command
4bf3e50f6SAlberto Garcia#
5bf3e50f6SAlberto Garcia# Copyright (C) 2018-2019 Igalia, S.L.
6bf3e50f6SAlberto Garcia# Author: Alberto Garcia <berto@igalia.com>
7bf3e50f6SAlberto Garcia#
8bf3e50f6SAlberto Garcia# This program is free software; you can redistribute it and/or modify
9bf3e50f6SAlberto Garcia# it under the terms of the GNU General Public License as published by
10bf3e50f6SAlberto Garcia# the Free Software Foundation; either version 2 of the License, or
11bf3e50f6SAlberto Garcia# (at your option) any later version.
12bf3e50f6SAlberto Garcia#
13bf3e50f6SAlberto Garcia# This program is distributed in the hope that it will be useful,
14bf3e50f6SAlberto Garcia# but WITHOUT ANY WARRANTY; without even the implied warranty of
15bf3e50f6SAlberto Garcia# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16bf3e50f6SAlberto Garcia# GNU General Public License for more details.
17bf3e50f6SAlberto Garcia#
18bf3e50f6SAlberto Garcia# You should have received a copy of the GNU General Public License
19bf3e50f6SAlberto Garcia# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20bf3e50f6SAlberto Garcia#
21bf3e50f6SAlberto Garcia
22bf3e50f6SAlberto Garciaimport os
23bf3e50f6SAlberto Garciaimport re
24bf3e50f6SAlberto Garciaimport iotests
25bf3e50f6SAlberto Garciaimport copy
26bf3e50f6SAlberto Garciaimport json
27bf3e50f6SAlberto Garciafrom iotests import qemu_img, qemu_io
28bf3e50f6SAlberto Garcia
29bf3e50f6SAlberto Garciahd_path = [
30bf3e50f6SAlberto Garcia    os.path.join(iotests.test_dir, 'hd0.img'),
31bf3e50f6SAlberto Garcia    os.path.join(iotests.test_dir, 'hd1.img'),
32bf3e50f6SAlberto Garcia    os.path.join(iotests.test_dir, 'hd2.img')
33bf3e50f6SAlberto Garcia]
34bf3e50f6SAlberto Garcia
35bf3e50f6SAlberto Garciadef hd_opts(idx):
36bf3e50f6SAlberto Garcia    return {'driver': iotests.imgfmt,
37bf3e50f6SAlberto Garcia            'node-name': 'hd%d' % idx,
38bf3e50f6SAlberto Garcia            'file': {'driver': 'file',
39bf3e50f6SAlberto Garcia                     'node-name': 'hd%d-file' % idx,
40bf3e50f6SAlberto Garcia                     'filename':  hd_path[idx] } }
41bf3e50f6SAlberto Garcia
42bf3e50f6SAlberto Garciaclass TestBlockdevReopen(iotests.QMPTestCase):
43bf3e50f6SAlberto Garcia    total_io_cmds = 0
44bf3e50f6SAlberto Garcia
45bf3e50f6SAlberto Garcia    def setUp(self):
46bf3e50f6SAlberto Garcia        qemu_img('create', '-f', iotests.imgfmt, hd_path[0], '3M')
47bf3e50f6SAlberto Garcia        qemu_img('create', '-f', iotests.imgfmt, '-b', hd_path[0], hd_path[1])
48bf3e50f6SAlberto Garcia        qemu_img('create', '-f', iotests.imgfmt, hd_path[2], '3M')
49bf3e50f6SAlberto Garcia        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xa0  0 1M', hd_path[0])
50bf3e50f6SAlberto Garcia        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xa1 1M 1M', hd_path[1])
51bf3e50f6SAlberto Garcia        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xa2 2M 1M', hd_path[2])
52bf3e50f6SAlberto Garcia        self.vm = iotests.VM()
53bf3e50f6SAlberto Garcia        self.vm.launch()
54bf3e50f6SAlberto Garcia
55bf3e50f6SAlberto Garcia    def tearDown(self):
56bf3e50f6SAlberto Garcia        self.vm.shutdown()
57bf3e50f6SAlberto Garcia        self.check_qemu_io_errors()
58bf3e50f6SAlberto Garcia        os.remove(hd_path[0])
59bf3e50f6SAlberto Garcia        os.remove(hd_path[1])
60bf3e50f6SAlberto Garcia        os.remove(hd_path[2])
61bf3e50f6SAlberto Garcia
62bf3e50f6SAlberto Garcia    # The output of qemu-io is not returned by vm.hmp_qemu_io() but
63bf3e50f6SAlberto Garcia    # it's stored in the log and can only be read when the VM has been
64bf3e50f6SAlberto Garcia    # shut down. This function runs qemu-io and keeps track of the
65bf3e50f6SAlberto Garcia    # number of times it's been called.
66bf3e50f6SAlberto Garcia    def run_qemu_io(self, img, cmd):
67bf3e50f6SAlberto Garcia        result = self.vm.hmp_qemu_io(img, cmd)
68bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', '')
69bf3e50f6SAlberto Garcia        self.total_io_cmds += 1
70bf3e50f6SAlberto Garcia
71bf3e50f6SAlberto Garcia    # Once the VM is shut down we can parse the log and see if qemu-io
72bf3e50f6SAlberto Garcia    # ran without errors.
73bf3e50f6SAlberto Garcia    def check_qemu_io_errors(self):
74bf3e50f6SAlberto Garcia        self.assertFalse(self.vm.is_running())
75bf3e50f6SAlberto Garcia        found = 0
76bf3e50f6SAlberto Garcia        log = self.vm.get_log()
77bf3e50f6SAlberto Garcia        for line in log.split("\n"):
78bf3e50f6SAlberto Garcia            if line.startswith("Pattern verification failed"):
79bf3e50f6SAlberto Garcia                raise Exception("%s (command #%d)" % (line, found))
80bf3e50f6SAlberto Garcia            if re.match("read .*/.* bytes at offset", line):
81bf3e50f6SAlberto Garcia                found += 1
82bf3e50f6SAlberto Garcia        self.assertEqual(found, self.total_io_cmds,
83bf3e50f6SAlberto Garcia                         "Expected output of %d qemu-io commands, found %d" %
84bf3e50f6SAlberto Garcia                         (found, self.total_io_cmds))
85bf3e50f6SAlberto Garcia
86bf3e50f6SAlberto Garcia    # Run x-blockdev-reopen with 'opts' but applying 'newopts'
87bf3e50f6SAlberto Garcia    # on top of it. The original 'opts' dict is unmodified
88bf3e50f6SAlberto Garcia    def reopen(self, opts, newopts = {}, errmsg = None):
89bf3e50f6SAlberto Garcia        opts = copy.deepcopy(opts)
90bf3e50f6SAlberto Garcia
91bf3e50f6SAlberto Garcia        # Apply the changes from 'newopts' on top of 'opts'
92bf3e50f6SAlberto Garcia        for key in newopts:
93bf3e50f6SAlberto Garcia            value = newopts[key]
94bf3e50f6SAlberto Garcia            # If key has the form "foo.bar" then we need to do
95bf3e50f6SAlberto Garcia            # opts["foo"]["bar"] = value, not opts["foo.bar"] = value
96bf3e50f6SAlberto Garcia            subdict = opts
97bf3e50f6SAlberto Garcia            while key.find('.') != -1:
98bf3e50f6SAlberto Garcia                [prefix, key] = key.split('.', 1)
99bf3e50f6SAlberto Garcia                subdict = opts[prefix]
100bf3e50f6SAlberto Garcia            subdict[key] = value
101bf3e50f6SAlberto Garcia
102bf3e50f6SAlberto Garcia        result = self.vm.qmp('x-blockdev-reopen', conv_keys = False, **opts)
103bf3e50f6SAlberto Garcia        if errmsg:
104bf3e50f6SAlberto Garcia            self.assert_qmp(result, 'error/class', 'GenericError')
105bf3e50f6SAlberto Garcia            self.assert_qmp(result, 'error/desc', errmsg)
106bf3e50f6SAlberto Garcia        else:
107bf3e50f6SAlberto Garcia            self.assert_qmp(result, 'return', {})
108bf3e50f6SAlberto Garcia
109bf3e50f6SAlberto Garcia
110bf3e50f6SAlberto Garcia    # Run query-named-block-nodes and return the specified entry
111bf3e50f6SAlberto Garcia    def get_node(self, node_name):
112bf3e50f6SAlberto Garcia        result = self.vm.qmp('query-named-block-nodes')
113bf3e50f6SAlberto Garcia        for node in result['return']:
114bf3e50f6SAlberto Garcia            if node['node-name'] == node_name:
115bf3e50f6SAlberto Garcia                return node
116bf3e50f6SAlberto Garcia        return None
117bf3e50f6SAlberto Garcia
118bf3e50f6SAlberto Garcia    # Run 'query-named-block-nodes' and compare its output with the
119bf3e50f6SAlberto Garcia    # value passed by the user in 'graph'
120bf3e50f6SAlberto Garcia    def check_node_graph(self, graph):
121bf3e50f6SAlberto Garcia        result = self.vm.qmp('query-named-block-nodes')
122bf3e50f6SAlberto Garcia        self.assertEqual(json.dumps(graph,  sort_keys=True),
123bf3e50f6SAlberto Garcia                         json.dumps(result, sort_keys=True))
124bf3e50f6SAlberto Garcia
125bf3e50f6SAlberto Garcia    # This test opens one single disk image (without backing files)
126bf3e50f6SAlberto Garcia    # and tries to reopen it with illegal / incorrect parameters.
127bf3e50f6SAlberto Garcia    def test_incorrect_parameters_single_file(self):
128bf3e50f6SAlberto Garcia        # Open 'hd0' only (no backing files)
129bf3e50f6SAlberto Garcia        opts = hd_opts(0)
130bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
131bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
132bf3e50f6SAlberto Garcia        original_graph = self.vm.qmp('query-named-block-nodes')
133bf3e50f6SAlberto Garcia
134bf3e50f6SAlberto Garcia        # We can reopen the image passing the same options
135bf3e50f6SAlberto Garcia        self.reopen(opts)
136bf3e50f6SAlberto Garcia
137bf3e50f6SAlberto Garcia        # We can also reopen passing a child reference in 'file'
138bf3e50f6SAlberto Garcia        self.reopen(opts, {'file': 'hd0-file'})
139bf3e50f6SAlberto Garcia
140bf3e50f6SAlberto Garcia        # We cannot change any of these
141bf3e50f6SAlberto Garcia        self.reopen(opts, {'node-name': 'not-found'}, "Cannot find node named 'not-found'")
142bf3e50f6SAlberto Garcia        self.reopen(opts, {'node-name': ''}, "Cannot find node named ''")
143bf3e50f6SAlberto Garcia        self.reopen(opts, {'node-name': None}, "Invalid parameter type for 'node-name', expected: string")
144bf3e50f6SAlberto Garcia        self.reopen(opts, {'driver': 'raw'}, "Cannot change the option 'driver'")
145bf3e50f6SAlberto Garcia        self.reopen(opts, {'driver': ''}, "Invalid parameter ''")
146bf3e50f6SAlberto Garcia        self.reopen(opts, {'driver': None}, "Invalid parameter type for 'driver', expected: string")
147bf3e50f6SAlberto Garcia        self.reopen(opts, {'file': 'not-found'}, "Cannot change the option 'file'")
148bf3e50f6SAlberto Garcia        self.reopen(opts, {'file': ''}, "Cannot change the option 'file'")
149bf3e50f6SAlberto Garcia        self.reopen(opts, {'file': None}, "Invalid parameter type for 'file', expected: BlockdevRef")
150bf3e50f6SAlberto Garcia        self.reopen(opts, {'file.node-name': 'newname'}, "Cannot change the option 'node-name'")
151bf3e50f6SAlberto Garcia        self.reopen(opts, {'file.driver': 'host_device'}, "Cannot change the option 'driver'")
152bf3e50f6SAlberto Garcia        self.reopen(opts, {'file.filename': hd_path[1]}, "Cannot change the option 'filename'")
153bf3e50f6SAlberto Garcia        self.reopen(opts, {'file.aio': 'native'}, "Cannot change the option 'aio'")
154bf3e50f6SAlberto Garcia        self.reopen(opts, {'file.locking': 'off'}, "Cannot change the option 'locking'")
155bf3e50f6SAlberto Garcia        self.reopen(opts, {'file.filename': None}, "Invalid parameter type for 'file.filename', expected: string")
156bf3e50f6SAlberto Garcia
157bf3e50f6SAlberto Garcia        # node-name is optional in BlockdevOptions, but x-blockdev-reopen needs it
158bf3e50f6SAlberto Garcia        del opts['node-name']
159bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "Node name not specified")
160bf3e50f6SAlberto Garcia
161bf3e50f6SAlberto Garcia        # Check that nothing has changed
162bf3e50f6SAlberto Garcia        self.check_node_graph(original_graph)
163bf3e50f6SAlberto Garcia
164bf3e50f6SAlberto Garcia        # Remove the node
165bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
166bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
167bf3e50f6SAlberto Garcia
168bf3e50f6SAlberto Garcia    # This test opens an image with a backing file and tries to reopen
169bf3e50f6SAlberto Garcia    # it with illegal / incorrect parameters.
170bf3e50f6SAlberto Garcia    def test_incorrect_parameters_backing_file(self):
171bf3e50f6SAlberto Garcia        # Open hd1 omitting the backing options (hd0 will be opened
172bf3e50f6SAlberto Garcia        # with the default options)
173bf3e50f6SAlberto Garcia        opts = hd_opts(1)
174bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
175bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
176bf3e50f6SAlberto Garcia        original_graph = self.vm.qmp('query-named-block-nodes')
177bf3e50f6SAlberto Garcia
178bf3e50f6SAlberto Garcia        # We can't reopen the image passing the same options, 'backing' is mandatory
179bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "backing is missing for 'hd1'")
180bf3e50f6SAlberto Garcia
181bf3e50f6SAlberto Garcia        # Everything works if we pass 'backing' using the existing node name
182bf3e50f6SAlberto Garcia        for node in original_graph['return']:
183bf3e50f6SAlberto Garcia            if node['drv'] == iotests.imgfmt and node['file'] == hd_path[0]:
184bf3e50f6SAlberto Garcia                backing_node_name = node['node-name']
185bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing': backing_node_name})
186bf3e50f6SAlberto Garcia
187bf3e50f6SAlberto Garcia        # We can't use a non-existing or empty (non-NULL) node as the backing image
188bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing': 'not-found'}, "Cannot find device= nor node_name=not-found")
189bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing': ''}, "Cannot find device= nor node_name=")
190bf3e50f6SAlberto Garcia
191bf3e50f6SAlberto Garcia        # We can reopen the image just fine if we specify the backing options
192bf3e50f6SAlberto Garcia        opts['backing'] = {'driver': iotests.imgfmt,
193bf3e50f6SAlberto Garcia                           'file': {'driver': 'file',
194bf3e50f6SAlberto Garcia                                    'filename': hd_path[0]}}
195bf3e50f6SAlberto Garcia        self.reopen(opts)
196bf3e50f6SAlberto Garcia
197bf3e50f6SAlberto Garcia        # We cannot change any of these options
198bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing.node-name': 'newname'}, "Cannot change the option 'node-name'")
199bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing.driver': 'raw'}, "Cannot change the option 'driver'")
200bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing.file.node-name': 'newname'}, "Cannot change the option 'node-name'")
201bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing.file.driver': 'host_device'}, "Cannot change the option 'driver'")
202bf3e50f6SAlberto Garcia
203bf3e50f6SAlberto Garcia        # Check that nothing has changed since the beginning
204bf3e50f6SAlberto Garcia        self.check_node_graph(original_graph)
205bf3e50f6SAlberto Garcia
206bf3e50f6SAlberto Garcia        # Remove the node
207bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd1')
208bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
209bf3e50f6SAlberto Garcia
210bf3e50f6SAlberto Garcia    # Reopen an image several times changing some of its options
211bf3e50f6SAlberto Garcia    def test_reopen(self):
21223e1d054SMax Reitz        # Check whether the filesystem supports O_DIRECT
21323e1d054SMax Reitz        if 'O_DIRECT' in qemu_io('-f', 'raw', '-t', 'none', '-c', 'quit', hd_path[0]):
21423e1d054SMax Reitz            supports_direct = False
21523e1d054SMax Reitz        else:
21623e1d054SMax Reitz            supports_direct = True
21723e1d054SMax Reitz
218bf3e50f6SAlberto Garcia        # Open the hd1 image passing all backing options
219bf3e50f6SAlberto Garcia        opts = hd_opts(1)
220bf3e50f6SAlberto Garcia        opts['backing'] = hd_opts(0)
221bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
222bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
223bf3e50f6SAlberto Garcia        original_graph = self.vm.qmp('query-named-block-nodes')
224bf3e50f6SAlberto Garcia
225bf3e50f6SAlberto Garcia        # We can reopen the image passing the same options
226bf3e50f6SAlberto Garcia        self.reopen(opts)
227bf3e50f6SAlberto Garcia
228bf3e50f6SAlberto Garcia        # Reopen in read-only mode
229bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'ro', False)
230bf3e50f6SAlberto Garcia
231bf3e50f6SAlberto Garcia        self.reopen(opts, {'read-only': True})
232bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'ro', True)
233bf3e50f6SAlberto Garcia        self.reopen(opts)
234bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'ro', False)
235bf3e50f6SAlberto Garcia
236bf3e50f6SAlberto Garcia        # Change the cache options
237bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'cache/writeback', True)
238bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'cache/direct', False)
239bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'cache/no-flush', False)
24023e1d054SMax Reitz        self.reopen(opts, {'cache': { 'direct': supports_direct, 'no-flush': True }})
241bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'cache/writeback', True)
24223e1d054SMax Reitz        self.assert_qmp(self.get_node('hd1'), 'cache/direct', supports_direct)
243bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'cache/no-flush', True)
244bf3e50f6SAlberto Garcia
245bf3e50f6SAlberto Garcia        # Reopen again with the original options
246bf3e50f6SAlberto Garcia        self.reopen(opts)
247bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'cache/writeback', True)
248bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'cache/direct', False)
249bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'cache/no-flush', False)
250bf3e50f6SAlberto Garcia
251bf3e50f6SAlberto Garcia        # Change 'detect-zeroes' and 'discard'
252bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'off')
253bf3e50f6SAlberto Garcia        self.reopen(opts, {'detect-zeroes': 'on'})
254bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'on')
255bf3e50f6SAlberto Garcia        self.reopen(opts, {'detect-zeroes': 'unmap'},
256bf3e50f6SAlberto Garcia                    "setting detect-zeroes to unmap is not allowed " +
257bf3e50f6SAlberto Garcia                    "without setting discard operation to unmap")
258bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'on')
259bf3e50f6SAlberto Garcia        self.reopen(opts, {'detect-zeroes': 'unmap', 'discard': 'unmap'})
260bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'unmap')
261bf3e50f6SAlberto Garcia        self.reopen(opts)
262bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'off')
263bf3e50f6SAlberto Garcia
264bf3e50f6SAlberto Garcia        # Changing 'force-share' is currently not supported
265bf3e50f6SAlberto Garcia        self.reopen(opts, {'force-share': True}, "Cannot change the option 'force-share'")
266bf3e50f6SAlberto Garcia
267bf3e50f6SAlberto Garcia        # Change some qcow2-specific options
268bf3e50f6SAlberto Garcia        # No way to test for success other than checking the return message
269bf3e50f6SAlberto Garcia        if iotests.imgfmt == 'qcow2':
270bf3e50f6SAlberto Garcia            self.reopen(opts, {'l2-cache-entry-size': 128 * 1024},
271bf3e50f6SAlberto Garcia                        "L2 cache entry size must be a power of two "+
272bf3e50f6SAlberto Garcia                        "between 512 and the cluster size (65536)")
273bf3e50f6SAlberto Garcia            self.reopen(opts, {'l2-cache-size': 1024 * 1024,
274bf3e50f6SAlberto Garcia                               'cache-size':     512 * 1024},
275bf3e50f6SAlberto Garcia                        "l2-cache-size may not exceed cache-size")
276bf3e50f6SAlberto Garcia            self.reopen(opts, {'l2-cache-size':        4 * 1024 * 1024,
277bf3e50f6SAlberto Garcia                               'refcount-cache-size':  4 * 1024 * 1024,
278bf3e50f6SAlberto Garcia                               'l2-cache-entry-size': 32 * 1024})
279bf3e50f6SAlberto Garcia            self.reopen(opts, {'pass-discard-request': True})
280bf3e50f6SAlberto Garcia
281bf3e50f6SAlberto Garcia        # Check that nothing has changed since the beginning
282bf3e50f6SAlberto Garcia        # (from the parameters that we can check)
283bf3e50f6SAlberto Garcia        self.check_node_graph(original_graph)
284bf3e50f6SAlberto Garcia
285bf3e50f6SAlberto Garcia        # Check that the node names (other than the top-level one) are optional
286bf3e50f6SAlberto Garcia        del opts['file']['node-name']
287bf3e50f6SAlberto Garcia        del opts['backing']['node-name']
288bf3e50f6SAlberto Garcia        del opts['backing']['file']['node-name']
289bf3e50f6SAlberto Garcia        self.reopen(opts)
290bf3e50f6SAlberto Garcia        self.check_node_graph(original_graph)
291bf3e50f6SAlberto Garcia
292bf3e50f6SAlberto Garcia        # Reopen setting backing = null, this removes the backing image from the chain
293bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing': None})
294bf3e50f6SAlberto Garcia        self.assert_qmp_absent(self.get_node('hd1'), 'image/backing-image')
295bf3e50f6SAlberto Garcia
296bf3e50f6SAlberto Garcia        # Open the 'hd0' image
297bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **hd_opts(0))
298bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
299bf3e50f6SAlberto Garcia
300bf3e50f6SAlberto Garcia        # Reopen the hd1 image setting 'hd0' as its backing image
301bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing': 'hd0'})
302bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'image/backing-image/filename', hd_path[0])
303bf3e50f6SAlberto Garcia
304bf3e50f6SAlberto Garcia        # Check that nothing has changed since the beginning
305bf3e50f6SAlberto Garcia        self.reopen(hd_opts(0), {'read-only': True})
306bf3e50f6SAlberto Garcia        self.check_node_graph(original_graph)
307bf3e50f6SAlberto Garcia
308bf3e50f6SAlberto Garcia        # The backing file (hd0) is now a reference, we cannot change backing.* anymore
309bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "Cannot change the option 'backing.driver'")
310bf3e50f6SAlberto Garcia
311bf3e50f6SAlberto Garcia        # We can't remove 'hd0' while it's a backing image of 'hd1'
312bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
313bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'error/class', 'GenericError')
314bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'error/desc', "Node 'hd0' is busy: node is used as backing hd of 'hd1'")
315bf3e50f6SAlberto Garcia
316bf3e50f6SAlberto Garcia        # But we can remove both nodes if done in the proper order
317bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd1')
318bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
319bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
320bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
321bf3e50f6SAlberto Garcia
322bf3e50f6SAlberto Garcia    # Reopen a raw image and see the effect of changing the 'offset' option
323bf3e50f6SAlberto Garcia    def test_reopen_raw(self):
324bf3e50f6SAlberto Garcia        opts = {'driver': 'raw', 'node-name': 'hd0',
325bf3e50f6SAlberto Garcia                'file': { 'driver': 'file',
326bf3e50f6SAlberto Garcia                          'filename': hd_path[0],
327bf3e50f6SAlberto Garcia                          'node-name': 'hd0-file' } }
328bf3e50f6SAlberto Garcia
329bf3e50f6SAlberto Garcia        # First we create a 2MB raw file, and fill each half with a
330bf3e50f6SAlberto Garcia        # different value
331bf3e50f6SAlberto Garcia        qemu_img('create', '-f', 'raw', hd_path[0], '2M')
332bf3e50f6SAlberto Garcia        qemu_io('-f', 'raw', '-c', 'write -P 0xa0  0 1M', hd_path[0])
333bf3e50f6SAlberto Garcia        qemu_io('-f', 'raw', '-c', 'write -P 0xa1 1M 1M', hd_path[0])
334bf3e50f6SAlberto Garcia
335bf3e50f6SAlberto Garcia        # Open the raw file with QEMU
336bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
337bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
338bf3e50f6SAlberto Garcia
339bf3e50f6SAlberto Garcia        # Read 1MB from offset 0
340bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa0  0 1M")
341bf3e50f6SAlberto Garcia
342bf3e50f6SAlberto Garcia        # Reopen the image with a 1MB offset.
343bf3e50f6SAlberto Garcia        # Now the results are different
344bf3e50f6SAlberto Garcia        self.reopen(opts, {'offset': 1024*1024})
345bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa1  0 1M")
346bf3e50f6SAlberto Garcia
347bf3e50f6SAlberto Garcia        # Reopen again with the original options.
348bf3e50f6SAlberto Garcia        # We get the original results again
349bf3e50f6SAlberto Garcia        self.reopen(opts)
350bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa0  0 1M")
351bf3e50f6SAlberto Garcia
352bf3e50f6SAlberto Garcia        # Remove the block device
353bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
354bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
355bf3e50f6SAlberto Garcia
356bf3e50f6SAlberto Garcia    # Omitting an option should reset it to the default value, but if
357bf3e50f6SAlberto Garcia    # an option cannot be changed it shouldn't be possible to reset it
358bf3e50f6SAlberto Garcia    # to its default value either
359bf3e50f6SAlberto Garcia    def test_reset_default_values(self):
360bf3e50f6SAlberto Garcia        opts = {'driver': 'qcow2', 'node-name': 'hd0',
361bf3e50f6SAlberto Garcia                'file': { 'driver': 'file',
362bf3e50f6SAlberto Garcia                          'filename': hd_path[0],
363bf3e50f6SAlberto Garcia                          'x-check-cache-dropped': True, # This one can be changed
364bf3e50f6SAlberto Garcia                          'locking': 'off',              # This one can NOT be changed
365bf3e50f6SAlberto Garcia                          'node-name': 'hd0-file' } }
366bf3e50f6SAlberto Garcia
367bf3e50f6SAlberto Garcia        # Open the file with QEMU
368bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
369bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
370bf3e50f6SAlberto Garcia
371bf3e50f6SAlberto Garcia        # file.x-check-cache-dropped can be changed...
372bf3e50f6SAlberto Garcia        self.reopen(opts, { 'file.x-check-cache-dropped': False })
373bf3e50f6SAlberto Garcia        # ...and dropped completely (resetting to the default value)
374bf3e50f6SAlberto Garcia        del opts['file']['x-check-cache-dropped']
375bf3e50f6SAlberto Garcia        self.reopen(opts)
376bf3e50f6SAlberto Garcia
377bf3e50f6SAlberto Garcia        # file.locking cannot be changed nor reset to the default value
378bf3e50f6SAlberto Garcia        self.reopen(opts, { 'file.locking': 'on' }, "Cannot change the option 'locking'")
379bf3e50f6SAlberto Garcia        del opts['file']['locking']
380bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "Option 'locking' cannot be reset to its default value")
381bf3e50f6SAlberto Garcia        # But we can reopen it if we maintain its previous value
382bf3e50f6SAlberto Garcia        self.reopen(opts, { 'file.locking': 'off' })
383bf3e50f6SAlberto Garcia
384bf3e50f6SAlberto Garcia        # Remove the block device
385bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
386bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
387bf3e50f6SAlberto Garcia
388bf3e50f6SAlberto Garcia    # This test modifies the node graph a few times by changing the
389bf3e50f6SAlberto Garcia    # 'backing' option on reopen and verifies that the guest data that
390bf3e50f6SAlberto Garcia    # is read afterwards is consistent with the graph changes.
391bf3e50f6SAlberto Garcia    def test_io_with_graph_changes(self):
392bf3e50f6SAlberto Garcia        opts = []
393bf3e50f6SAlberto Garcia
394bf3e50f6SAlberto Garcia        # Open hd0, hd1 and hd2 without any backing image
395bf3e50f6SAlberto Garcia        for i in range(3):
396bf3e50f6SAlberto Garcia            opts.append(hd_opts(i))
397bf3e50f6SAlberto Garcia            opts[i]['backing'] = None
398bf3e50f6SAlberto Garcia            result = self.vm.qmp('blockdev-add', conv_keys = False, **opts[i])
399bf3e50f6SAlberto Garcia            self.assert_qmp(result, 'return', {})
400bf3e50f6SAlberto Garcia
401bf3e50f6SAlberto Garcia        # hd0
402bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa0  0 1M")
403bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0    1M 1M")
404bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0    2M 1M")
405bf3e50f6SAlberto Garcia
406bf3e50f6SAlberto Garcia        # hd1 <- hd0
407bf3e50f6SAlberto Garcia        self.reopen(opts[0], {'backing': 'hd1'})
408bf3e50f6SAlberto Garcia
409bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa0  0 1M")
410bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa1 1M 1M")
411bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0    2M 1M")
412bf3e50f6SAlberto Garcia
413bf3e50f6SAlberto Garcia        # hd1 <- hd0 , hd1 <- hd2
414bf3e50f6SAlberto Garcia        self.reopen(opts[2], {'backing': 'hd1'})
415bf3e50f6SAlberto Garcia
416bf3e50f6SAlberto Garcia        self.run_qemu_io("hd2", "read -P 0     0 1M")
417bf3e50f6SAlberto Garcia        self.run_qemu_io("hd2", "read -P 0xa1 1M 1M")
418bf3e50f6SAlberto Garcia        self.run_qemu_io("hd2", "read -P 0xa2 2M 1M")
419bf3e50f6SAlberto Garcia
420bf3e50f6SAlberto Garcia        # hd1 <- hd2 <- hd0
421bf3e50f6SAlberto Garcia        self.reopen(opts[0], {'backing': 'hd2'})
422bf3e50f6SAlberto Garcia
423bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa0  0 1M")
424bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa1 1M 1M")
425bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa2 2M 1M")
426bf3e50f6SAlberto Garcia
427bf3e50f6SAlberto Garcia        # hd2 <- hd0
428bf3e50f6SAlberto Garcia        self.reopen(opts[2], {'backing': None})
429bf3e50f6SAlberto Garcia
430bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa0  0 1M")
431bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0    1M 1M")
432bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa2 2M 1M")
433bf3e50f6SAlberto Garcia
434bf3e50f6SAlberto Garcia        # hd2 <- hd1 <- hd0
435bf3e50f6SAlberto Garcia        self.reopen(opts[1], {'backing': 'hd2'})
436bf3e50f6SAlberto Garcia        self.reopen(opts[0], {'backing': 'hd1'})
437bf3e50f6SAlberto Garcia
438bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa0  0 1M")
439bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa1 1M 1M")
440bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa2 2M 1M")
441bf3e50f6SAlberto Garcia
442bf3e50f6SAlberto Garcia        # Illegal operation: hd2 is a child of hd1
443bf3e50f6SAlberto Garcia        self.reopen(opts[2], {'backing': 'hd1'},
444bf3e50f6SAlberto Garcia                    "Making 'hd1' a backing file of 'hd2' would create a cycle")
445bf3e50f6SAlberto Garcia
446bf3e50f6SAlberto Garcia        # hd2 <- hd0, hd2 <- hd1
447bf3e50f6SAlberto Garcia        self.reopen(opts[0], {'backing': 'hd2'})
448bf3e50f6SAlberto Garcia
449bf3e50f6SAlberto Garcia        self.run_qemu_io("hd1", "read -P 0     0 1M")
450bf3e50f6SAlberto Garcia        self.run_qemu_io("hd1", "read -P 0xa1 1M 1M")
451bf3e50f6SAlberto Garcia        self.run_qemu_io("hd1", "read -P 0xa2 2M 1M")
452bf3e50f6SAlberto Garcia
453bf3e50f6SAlberto Garcia        # More illegal operations
454bf3e50f6SAlberto Garcia        self.reopen(opts[2], {'backing': 'hd1'},
455bf3e50f6SAlberto Garcia                    "Making 'hd1' a backing file of 'hd2' would create a cycle")
456bf3e50f6SAlberto Garcia        self.reopen(opts[2], {'file': 'hd0-file'}, "Cannot change the option 'file'")
457bf3e50f6SAlberto Garcia
458bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd2')
459bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'error/class', 'GenericError')
460bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'error/desc', "Node 'hd2' is busy: node is used as backing hd of 'hd0'")
461bf3e50f6SAlberto Garcia
462bf3e50f6SAlberto Garcia        # hd1 doesn't have a backing file now
463bf3e50f6SAlberto Garcia        self.reopen(opts[1], {'backing': None})
464bf3e50f6SAlberto Garcia
465bf3e50f6SAlberto Garcia        self.run_qemu_io("hd1", "read -P 0     0 1M")
466bf3e50f6SAlberto Garcia        self.run_qemu_io("hd1", "read -P 0xa1 1M 1M")
467bf3e50f6SAlberto Garcia        self.run_qemu_io("hd1", "read -P 0    2M 1M")
468bf3e50f6SAlberto Garcia
469bf3e50f6SAlberto Garcia        # We can't remove the 'backing' option if the image has a
470bf3e50f6SAlberto Garcia        # default backing file
471bf3e50f6SAlberto Garcia        del opts[1]['backing']
472bf3e50f6SAlberto Garcia        self.reopen(opts[1], {}, "backing is missing for 'hd1'")
473bf3e50f6SAlberto Garcia
474bf3e50f6SAlberto Garcia        self.run_qemu_io("hd1", "read -P 0     0 1M")
475bf3e50f6SAlberto Garcia        self.run_qemu_io("hd1", "read -P 0xa1 1M 1M")
476bf3e50f6SAlberto Garcia        self.run_qemu_io("hd1", "read -P 0    2M 1M")
477bf3e50f6SAlberto Garcia
478bf3e50f6SAlberto Garcia    # This test verifies that we can't change the children of a block
479bf3e50f6SAlberto Garcia    # device during a reopen operation in a way that would create
480bf3e50f6SAlberto Garcia    # cycles in the node graph
4819442bebeSThomas Huth    @iotests.skip_if_unsupported(['blkverify'])
482bf3e50f6SAlberto Garcia    def test_graph_cycles(self):
483bf3e50f6SAlberto Garcia        opts = []
484bf3e50f6SAlberto Garcia
485bf3e50f6SAlberto Garcia        # Open all three images without backing file
486bf3e50f6SAlberto Garcia        for i in range(3):
487bf3e50f6SAlberto Garcia            opts.append(hd_opts(i))
488bf3e50f6SAlberto Garcia            opts[i]['backing'] = None
489bf3e50f6SAlberto Garcia            result = self.vm.qmp('blockdev-add', conv_keys = False, **opts[i])
490bf3e50f6SAlberto Garcia            self.assert_qmp(result, 'return', {})
491bf3e50f6SAlberto Garcia
492bf3e50f6SAlberto Garcia        # hd1 <- hd0, hd1 <- hd2
493bf3e50f6SAlberto Garcia        self.reopen(opts[0], {'backing': 'hd1'})
494bf3e50f6SAlberto Garcia        self.reopen(opts[2], {'backing': 'hd1'})
495bf3e50f6SAlberto Garcia
496bf3e50f6SAlberto Garcia        # Illegal: hd2 is backed by hd1
497bf3e50f6SAlberto Garcia        self.reopen(opts[1], {'backing': 'hd2'},
498bf3e50f6SAlberto Garcia                    "Making 'hd2' a backing file of 'hd1' would create a cycle")
499bf3e50f6SAlberto Garcia
500bf3e50f6SAlberto Garcia        # hd1 <- hd0 <- hd2
501bf3e50f6SAlberto Garcia        self.reopen(opts[2], {'backing': 'hd0'})
502bf3e50f6SAlberto Garcia
503bf3e50f6SAlberto Garcia        # Illegal: hd2 is backed by hd0, which is backed by hd1
504bf3e50f6SAlberto Garcia        self.reopen(opts[1], {'backing': 'hd2'},
505bf3e50f6SAlberto Garcia                    "Making 'hd2' a backing file of 'hd1' would create a cycle")
506bf3e50f6SAlberto Garcia
507bf3e50f6SAlberto Garcia        # Illegal: hd1 cannot point to itself
508bf3e50f6SAlberto Garcia        self.reopen(opts[1], {'backing': 'hd1'},
509bf3e50f6SAlberto Garcia                    "Making 'hd1' a backing file of 'hd1' would create a cycle")
510bf3e50f6SAlberto Garcia
511bf3e50f6SAlberto Garcia        # Remove all backing files
512bf3e50f6SAlberto Garcia        self.reopen(opts[0])
513bf3e50f6SAlberto Garcia        self.reopen(opts[2])
514bf3e50f6SAlberto Garcia
515bf3e50f6SAlberto Garcia        ##########################################
516bf3e50f6SAlberto Garcia        # Add a blkverify node using hd0 and hd1 #
517bf3e50f6SAlberto Garcia        ##########################################
518bf3e50f6SAlberto Garcia        bvopts = {'driver': 'blkverify',
519bf3e50f6SAlberto Garcia                  'node-name': 'bv',
520bf3e50f6SAlberto Garcia                  'test': 'hd0',
521bf3e50f6SAlberto Garcia                  'raw': 'hd1'}
522bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **bvopts)
523bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
524bf3e50f6SAlberto Garcia
525bf3e50f6SAlberto Garcia        # blkverify doesn't currently allow reopening. TODO: implement this
526bf3e50f6SAlberto Garcia        self.reopen(bvopts, {}, "Block format 'blkverify' used by node 'bv'" +
527bf3e50f6SAlberto Garcia                    " does not support reopening files")
528bf3e50f6SAlberto Garcia
529bf3e50f6SAlberto Garcia        # Illegal: hd0 is a child of the blkverify node
530bf3e50f6SAlberto Garcia        self.reopen(opts[0], {'backing': 'bv'},
531bf3e50f6SAlberto Garcia                    "Making 'bv' a backing file of 'hd0' would create a cycle")
532bf3e50f6SAlberto Garcia
533bf3e50f6SAlberto Garcia        # Delete the blkverify node
534bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'bv')
535bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
536bf3e50f6SAlberto Garcia
537bf3e50f6SAlberto Garcia    # Misc reopen tests with different block drivers
5389442bebeSThomas Huth    @iotests.skip_if_unsupported(['quorum', 'throttle'])
539bf3e50f6SAlberto Garcia    def test_misc_drivers(self):
540bf3e50f6SAlberto Garcia        ####################
541bf3e50f6SAlberto Garcia        ###### quorum ######
542bf3e50f6SAlberto Garcia        ####################
543bf3e50f6SAlberto Garcia        for i in range(3):
544bf3e50f6SAlberto Garcia            opts = hd_opts(i)
545bf3e50f6SAlberto Garcia            # Open all three images without backing file
546bf3e50f6SAlberto Garcia            opts['backing'] = None
547bf3e50f6SAlberto Garcia            result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
548bf3e50f6SAlberto Garcia            self.assert_qmp(result, 'return', {})
549bf3e50f6SAlberto Garcia
550bf3e50f6SAlberto Garcia        opts = {'driver': 'quorum',
551bf3e50f6SAlberto Garcia                'node-name': 'quorum0',
552bf3e50f6SAlberto Garcia                'children': ['hd0', 'hd1', 'hd2'],
553bf3e50f6SAlberto Garcia                'vote-threshold': 2}
554bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
555bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
556bf3e50f6SAlberto Garcia
557bf3e50f6SAlberto Garcia        # Quorum doesn't currently allow reopening. TODO: implement this
558bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "Block format 'quorum' used by node 'quorum0'" +
559bf3e50f6SAlberto Garcia                    " does not support reopening files")
560bf3e50f6SAlberto Garcia
561bf3e50f6SAlberto Garcia        # You can't make quorum0 a backing file of hd0:
562bf3e50f6SAlberto Garcia        # hd0 is already a child of quorum0.
563bf3e50f6SAlberto Garcia        self.reopen(hd_opts(0), {'backing': 'quorum0'},
564bf3e50f6SAlberto Garcia                    "Making 'quorum0' a backing file of 'hd0' would create a cycle")
565bf3e50f6SAlberto Garcia
566bf3e50f6SAlberto Garcia        # Delete quorum0
567bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'quorum0')
568bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
569bf3e50f6SAlberto Garcia
570bf3e50f6SAlberto Garcia        # Delete hd0, hd1 and hd2
571bf3e50f6SAlberto Garcia        for i in range(3):
572bf3e50f6SAlberto Garcia            result = self.vm.qmp('blockdev-del', conv_keys = True,
573bf3e50f6SAlberto Garcia                                 node_name = 'hd%d' % i)
574bf3e50f6SAlberto Garcia            self.assert_qmp(result, 'return', {})
575bf3e50f6SAlberto Garcia
576bf3e50f6SAlberto Garcia        ######################
577bf3e50f6SAlberto Garcia        ###### blkdebug ######
578bf3e50f6SAlberto Garcia        ######################
579bf3e50f6SAlberto Garcia        opts = {'driver': 'blkdebug',
580bf3e50f6SAlberto Garcia                'node-name': 'bd',
581bf3e50f6SAlberto Garcia                'config': '/dev/null',
582bf3e50f6SAlberto Garcia                'image': hd_opts(0)}
583bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
584bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
585bf3e50f6SAlberto Garcia
586bf3e50f6SAlberto Garcia        # blkdebug allows reopening if we keep the same options
587bf3e50f6SAlberto Garcia        self.reopen(opts)
588bf3e50f6SAlberto Garcia
589bf3e50f6SAlberto Garcia        # but it currently does not allow changes
590bf3e50f6SAlberto Garcia        self.reopen(opts, {'image': 'hd1'}, "Cannot change the option 'image'")
591bf3e50f6SAlberto Garcia        self.reopen(opts, {'align': 33554432}, "Cannot change the option 'align'")
592bf3e50f6SAlberto Garcia        self.reopen(opts, {'config': '/non/existent'}, "Cannot change the option 'config'")
593bf3e50f6SAlberto Garcia        del opts['config']
594bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "Option 'config' cannot be reset to its default value")
595bf3e50f6SAlberto Garcia
596bf3e50f6SAlberto Garcia        # Delete the blkdebug node
597bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'bd')
598bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
599bf3e50f6SAlberto Garcia
600bf3e50f6SAlberto Garcia        ##################
601bf3e50f6SAlberto Garcia        ###### null ######
602bf3e50f6SAlberto Garcia        ##################
603a6f8f9f8SMax Reitz        opts = {'driver': 'null-co', 'node-name': 'root', 'size': 1024}
604bf3e50f6SAlberto Garcia
605bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
606bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
607bf3e50f6SAlberto Garcia
608bf3e50f6SAlberto Garcia        # 1 << 30 is the default value, but we cannot change it explicitly
609bf3e50f6SAlberto Garcia        self.reopen(opts, {'size': (1 << 30)}, "Cannot change the option 'size'")
610bf3e50f6SAlberto Garcia
611bf3e50f6SAlberto Garcia        # We cannot change 'size' back to its default value either
612bf3e50f6SAlberto Garcia        del opts['size']
613bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "Option 'size' cannot be reset to its default value")
614bf3e50f6SAlberto Garcia
615bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'root')
616bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
617bf3e50f6SAlberto Garcia
618bf3e50f6SAlberto Garcia        ##################
619bf3e50f6SAlberto Garcia        ###### file ######
620bf3e50f6SAlberto Garcia        ##################
621bf3e50f6SAlberto Garcia        opts = hd_opts(0)
622bf3e50f6SAlberto Garcia        opts['file']['locking'] = 'on'
623bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
624bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
625bf3e50f6SAlberto Garcia
626bf3e50f6SAlberto Garcia        # 'locking' cannot be changed
627bf3e50f6SAlberto Garcia        del opts['file']['locking']
628bf3e50f6SAlberto Garcia        self.reopen(opts, {'file.locking': 'on'})
629bf3e50f6SAlberto Garcia        self.reopen(opts, {'file.locking': 'off'}, "Cannot change the option 'locking'")
630bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "Option 'locking' cannot be reset to its default value")
631bf3e50f6SAlberto Garcia
632bf3e50f6SAlberto Garcia        # Trying to reopen the 'file' node directly does not make a difference
633bf3e50f6SAlberto Garcia        opts = opts['file']
634bf3e50f6SAlberto Garcia        self.reopen(opts, {'locking': 'on'})
635bf3e50f6SAlberto Garcia        self.reopen(opts, {'locking': 'off'}, "Cannot change the option 'locking'")
636bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "Option 'locking' cannot be reset to its default value")
637bf3e50f6SAlberto Garcia
638bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
639bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
640bf3e50f6SAlberto Garcia
641bf3e50f6SAlberto Garcia        ######################
642bf3e50f6SAlberto Garcia        ###### throttle ######
643bf3e50f6SAlberto Garcia        ######################
644bf3e50f6SAlberto Garcia        opts = { 'qom-type': 'throttle-group', 'id': 'group0',
645bf3e50f6SAlberto Garcia                 'props': { 'limits': { 'iops-total': 1000 } } }
646bf3e50f6SAlberto Garcia        result = self.vm.qmp('object-add', conv_keys = False, **opts)
647bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
648bf3e50f6SAlberto Garcia
649bf3e50f6SAlberto Garcia        opts = { 'qom-type': 'throttle-group', 'id': 'group1',
650bf3e50f6SAlberto Garcia                 'props': { 'limits': { 'iops-total': 2000 } } }
651bf3e50f6SAlberto Garcia        result = self.vm.qmp('object-add', conv_keys = False, **opts)
652bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
653bf3e50f6SAlberto Garcia
654bf3e50f6SAlberto Garcia        # Add a throttle filter with group = group0
655bf3e50f6SAlberto Garcia        opts = { 'driver': 'throttle', 'node-name': 'throttle0',
656bf3e50f6SAlberto Garcia                 'throttle-group': 'group0', 'file': hd_opts(0) }
657bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
658bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
659bf3e50f6SAlberto Garcia
660bf3e50f6SAlberto Garcia        # We can reopen it if we keep the same options
661bf3e50f6SAlberto Garcia        self.reopen(opts)
662bf3e50f6SAlberto Garcia
663bf3e50f6SAlberto Garcia        # We can also reopen if 'file' is a reference to the child
664bf3e50f6SAlberto Garcia        self.reopen(opts, {'file': 'hd0'})
665bf3e50f6SAlberto Garcia
666bf3e50f6SAlberto Garcia        # This is illegal
667bf3e50f6SAlberto Garcia        self.reopen(opts, {'throttle-group': 'notfound'}, "Throttle group 'notfound' does not exist")
668bf3e50f6SAlberto Garcia
669bf3e50f6SAlberto Garcia        # But it's possible to change the group to group1
670bf3e50f6SAlberto Garcia        self.reopen(opts, {'throttle-group': 'group1'})
671bf3e50f6SAlberto Garcia
672bf3e50f6SAlberto Garcia        # Now group1 is in use, it cannot be deleted
673bf3e50f6SAlberto Garcia        result = self.vm.qmp('object-del', id = 'group1')
674bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'error/class', 'GenericError')
675bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'error/desc', "object 'group1' is in use, can not be deleted")
676bf3e50f6SAlberto Garcia
677bf3e50f6SAlberto Garcia        # Default options, this switches the group back to group0
678bf3e50f6SAlberto Garcia        self.reopen(opts)
679bf3e50f6SAlberto Garcia
680bf3e50f6SAlberto Garcia        # So now we cannot delete group0
681bf3e50f6SAlberto Garcia        result = self.vm.qmp('object-del', id = 'group0')
682bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'error/class', 'GenericError')
683bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'error/desc', "object 'group0' is in use, can not be deleted")
684bf3e50f6SAlberto Garcia
685bf3e50f6SAlberto Garcia        # But group1 is free this time, and it can be deleted
686bf3e50f6SAlberto Garcia        result = self.vm.qmp('object-del', id = 'group1')
687bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
688bf3e50f6SAlberto Garcia
689bf3e50f6SAlberto Garcia        # Let's delete the filter node
690bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'throttle0')
691bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
692bf3e50f6SAlberto Garcia
693bf3e50f6SAlberto Garcia        # And we can finally get rid of group0
694bf3e50f6SAlberto Garcia        result = self.vm.qmp('object-del', id = 'group0')
695bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
696bf3e50f6SAlberto Garcia
697bf3e50f6SAlberto Garcia    # If an image has a backing file then the 'backing' option must be
698bf3e50f6SAlberto Garcia    # passed on reopen. We don't allow leaving the option out in this
699bf3e50f6SAlberto Garcia    # case because it's unclear what the correct semantics would be.
700bf3e50f6SAlberto Garcia    def test_missing_backing_options_1(self):
701bf3e50f6SAlberto Garcia        # hd2
702bf3e50f6SAlberto Garcia        opts = hd_opts(2)
703bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
704bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
705bf3e50f6SAlberto Garcia
706bf3e50f6SAlberto Garcia        # hd0
707bf3e50f6SAlberto Garcia        opts = hd_opts(0)
708bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
709bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
710bf3e50f6SAlberto Garcia
711bf3e50f6SAlberto Garcia        # hd0 has no backing file: we can omit the 'backing' option
712bf3e50f6SAlberto Garcia        self.reopen(opts)
713bf3e50f6SAlberto Garcia
714bf3e50f6SAlberto Garcia        # hd2 <- hd0
715bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing': 'hd2'})
716bf3e50f6SAlberto Garcia
717bf3e50f6SAlberto Garcia        # hd0 has a backing file: we must set the 'backing' option
718bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "backing is missing for 'hd0'")
719bf3e50f6SAlberto Garcia
720bf3e50f6SAlberto Garcia        # hd2 can't be removed because it's the backing file of hd0
721bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd2')
722bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'error/class', 'GenericError')
723bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'error/desc', "Node 'hd2' is busy: node is used as backing hd of 'hd0'")
724bf3e50f6SAlberto Garcia
725bf3e50f6SAlberto Garcia        # Detach hd2 from hd0.
726bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing': None})
727bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "backing is missing for 'hd0'")
728bf3e50f6SAlberto Garcia
729bf3e50f6SAlberto Garcia        # Remove both hd0 and hd2
730bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
731bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
732bf3e50f6SAlberto Garcia
733bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd2')
734bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
735bf3e50f6SAlberto Garcia
736bf3e50f6SAlberto Garcia    # If an image has default backing file (as part of its metadata)
737bf3e50f6SAlberto Garcia    # then the 'backing' option must be passed on reopen. We don't
738bf3e50f6SAlberto Garcia    # allow leaving the option out in this case because it's unclear
739bf3e50f6SAlberto Garcia    # what the correct semantics would be.
740bf3e50f6SAlberto Garcia    def test_missing_backing_options_2(self):
741bf3e50f6SAlberto Garcia        # hd0 <- hd1
742bf3e50f6SAlberto Garcia        # (hd0 is hd1's default backing file)
743bf3e50f6SAlberto Garcia        opts = hd_opts(1)
744bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
745bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
746bf3e50f6SAlberto Garcia
747bf3e50f6SAlberto Garcia        # hd1 has a backing file: we can't omit the 'backing' option
748bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "backing is missing for 'hd1'")
749bf3e50f6SAlberto Garcia
750bf3e50f6SAlberto Garcia        # Let's detach the backing file
751bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing': None})
752bf3e50f6SAlberto Garcia
753bf3e50f6SAlberto Garcia        # No backing file attached to hd1 now, but we still can't omit the 'backing' option
754bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "backing is missing for 'hd1'")
755bf3e50f6SAlberto Garcia
756bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd1')
757bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
758bf3e50f6SAlberto Garcia
759bf3e50f6SAlberto Garcia    # Test that making 'backing' a reference to an existing child
760bf3e50f6SAlberto Garcia    # keeps its current options
761bf3e50f6SAlberto Garcia    def test_backing_reference(self):
762bf3e50f6SAlberto Garcia        # hd2 <- hd1 <- hd0
763bf3e50f6SAlberto Garcia        opts = hd_opts(0)
764bf3e50f6SAlberto Garcia        opts['backing'] = hd_opts(1)
765bf3e50f6SAlberto Garcia        opts['backing']['backing'] = hd_opts(2)
766bf3e50f6SAlberto Garcia        # Enable 'detect-zeroes' on all three nodes
767bf3e50f6SAlberto Garcia        opts['detect-zeroes'] = 'on'
768bf3e50f6SAlberto Garcia        opts['backing']['detect-zeroes'] = 'on'
769bf3e50f6SAlberto Garcia        opts['backing']['backing']['detect-zeroes'] = 'on'
770bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
771bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
772bf3e50f6SAlberto Garcia
773bf3e50f6SAlberto Garcia        # Reopen the chain passing the minimum amount of required options.
774bf3e50f6SAlberto Garcia        # By making 'backing' a reference to hd1 (instead of a sub-dict)
775bf3e50f6SAlberto Garcia        # we tell QEMU to keep its current set of options.
776bf3e50f6SAlberto Garcia        opts = {'driver': iotests.imgfmt,
777bf3e50f6SAlberto Garcia                'node-name': 'hd0',
778bf3e50f6SAlberto Garcia                'file': 'hd0-file',
779bf3e50f6SAlberto Garcia                'backing': 'hd1' }
780bf3e50f6SAlberto Garcia        self.reopen(opts)
781bf3e50f6SAlberto Garcia
782bf3e50f6SAlberto Garcia        # This has reset 'detect-zeroes' on hd0, but not on hd1 and hd2.
783bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd0'), 'detect_zeroes', 'off')
784bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'on')
785bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd2'), 'detect_zeroes', 'on')
786bf3e50f6SAlberto Garcia
787bf3e50f6SAlberto Garcia    # Test what happens if the graph changes due to other operations
788bf3e50f6SAlberto Garcia    # such as block-stream
789bf3e50f6SAlberto Garcia    def test_block_stream_1(self):
790bf3e50f6SAlberto Garcia        # hd1 <- hd0
791bf3e50f6SAlberto Garcia        opts = hd_opts(0)
792bf3e50f6SAlberto Garcia        opts['backing'] = hd_opts(1)
793bf3e50f6SAlberto Garcia        opts['backing']['backing'] = None
794bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
795bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
796bf3e50f6SAlberto Garcia
797bf3e50f6SAlberto Garcia        # Stream hd1 into hd0 and wait until it's done
798bf3e50f6SAlberto Garcia        result = self.vm.qmp('block-stream', conv_keys = True, job_id = 'stream0', device = 'hd0')
799bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
800bf3e50f6SAlberto Garcia        self.wait_until_completed(drive = 'stream0')
801bf3e50f6SAlberto Garcia
802bf3e50f6SAlberto Garcia        # Now we have only hd0
803bf3e50f6SAlberto Garcia        self.assertEqual(self.get_node('hd1'), None)
804bf3e50f6SAlberto Garcia
805bf3e50f6SAlberto Garcia        # We have backing.* options but there's no backing file anymore
806bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "Cannot change the option 'backing.driver'")
807bf3e50f6SAlberto Garcia
808bf3e50f6SAlberto Garcia        # If we remove the 'backing' option then we can reopen hd0 just fine
809bf3e50f6SAlberto Garcia        del opts['backing']
810bf3e50f6SAlberto Garcia        self.reopen(opts)
811bf3e50f6SAlberto Garcia
812bf3e50f6SAlberto Garcia        # We can also reopen hd0 if we set 'backing' to null
813bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing': None})
814bf3e50f6SAlberto Garcia
815bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
816bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
817bf3e50f6SAlberto Garcia
818bf3e50f6SAlberto Garcia    # Another block_stream test
819bf3e50f6SAlberto Garcia    def test_block_stream_2(self):
820bf3e50f6SAlberto Garcia        # hd2 <- hd1 <- hd0
821bf3e50f6SAlberto Garcia        opts = hd_opts(0)
822bf3e50f6SAlberto Garcia        opts['backing'] = hd_opts(1)
823bf3e50f6SAlberto Garcia        opts['backing']['backing'] = hd_opts(2)
824bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
825bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
826bf3e50f6SAlberto Garcia
827bf3e50f6SAlberto Garcia        # Stream hd1 into hd0 and wait until it's done
828bf3e50f6SAlberto Garcia        result = self.vm.qmp('block-stream', conv_keys = True, job_id = 'stream0',
829bf3e50f6SAlberto Garcia                             device = 'hd0', base_node = 'hd2')
830bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
831bf3e50f6SAlberto Garcia        self.wait_until_completed(drive = 'stream0')
832bf3e50f6SAlberto Garcia
833bf3e50f6SAlberto Garcia        # The chain is hd2 <- hd0 now. hd1 is missing
834bf3e50f6SAlberto Garcia        self.assertEqual(self.get_node('hd1'), None)
835bf3e50f6SAlberto Garcia
836bf3e50f6SAlberto Garcia        # The backing options in the dict were meant for hd1, but we cannot
837bf3e50f6SAlberto Garcia        # use them with hd2 because hd1 had a backing file while hd2 does not.
838bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "Cannot change the option 'backing.driver'")
839bf3e50f6SAlberto Garcia
840bf3e50f6SAlberto Garcia        # If we remove hd1's options from the dict then things work fine
841bf3e50f6SAlberto Garcia        opts['backing'] = opts['backing']['backing']
842bf3e50f6SAlberto Garcia        self.reopen(opts)
843bf3e50f6SAlberto Garcia
844bf3e50f6SAlberto Garcia        # We can also reopen hd0 if we use a reference to the backing file
845bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing': 'hd2'})
846bf3e50f6SAlberto Garcia
847bf3e50f6SAlberto Garcia        # But we cannot leave the option out
848bf3e50f6SAlberto Garcia        del opts['backing']
849bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "backing is missing for 'hd0'")
850bf3e50f6SAlberto Garcia
851bf3e50f6SAlberto Garcia        # Now we can delete hd0 (and hd2)
852bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
853bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
854bf3e50f6SAlberto Garcia        self.assertEqual(self.get_node('hd2'), None)
855bf3e50f6SAlberto Garcia
856bf3e50f6SAlberto Garcia    # Reopen the chain during a block-stream job (from hd1 to hd0)
857bf3e50f6SAlberto Garcia    def test_block_stream_3(self):
858bf3e50f6SAlberto Garcia        # hd2 <- hd1 <- hd0
859bf3e50f6SAlberto Garcia        opts = hd_opts(0)
860bf3e50f6SAlberto Garcia        opts['backing'] = hd_opts(1)
861bf3e50f6SAlberto Garcia        opts['backing']['backing'] = hd_opts(2)
862bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
863bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
864bf3e50f6SAlberto Garcia
865bf3e50f6SAlberto Garcia        # hd2 <- hd0
866bf3e50f6SAlberto Garcia        result = self.vm.qmp('block-stream', conv_keys = True, job_id = 'stream0',
867c423a6afSMax Reitz                             device = 'hd0', base_node = 'hd2',
868c423a6afSMax Reitz                             auto_finalize = False)
869bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
870bf3e50f6SAlberto Garcia
871c624b015SAndrey Shinkevich        # We can remove hd2 while the stream job is ongoing
872bf3e50f6SAlberto Garcia        opts['backing']['backing'] = None
873c624b015SAndrey Shinkevich        self.reopen(opts, {})
874bf3e50f6SAlberto Garcia
875bf3e50f6SAlberto Garcia        # We can't remove hd1 while the stream job is ongoing
876bf3e50f6SAlberto Garcia        opts['backing'] = None
877bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "Cannot change 'backing' link from 'hd0' to 'hd1'")
878bf3e50f6SAlberto Garcia
879c423a6afSMax Reitz        self.vm.run_job('stream0', auto_finalize = False, auto_dismiss = True)
880bf3e50f6SAlberto Garcia
881bf3e50f6SAlberto Garcia    # Reopen the chain during a block-stream job (from hd2 to hd1)
882bf3e50f6SAlberto Garcia    def test_block_stream_4(self):
883bf3e50f6SAlberto Garcia        # hd2 <- hd1 <- hd0
884bf3e50f6SAlberto Garcia        opts = hd_opts(0)
885bf3e50f6SAlberto Garcia        opts['backing'] = hd_opts(1)
886bf3e50f6SAlberto Garcia        opts['backing']['backing'] = hd_opts(2)
887bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
888bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
889bf3e50f6SAlberto Garcia
890bf3e50f6SAlberto Garcia        # hd1 <- hd0
891bf3e50f6SAlberto Garcia        result = self.vm.qmp('block-stream', conv_keys = True, job_id = 'stream0',
892c423a6afSMax Reitz                             device = 'hd1', auto_finalize = False)
893bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
894bf3e50f6SAlberto Garcia
895bf3e50f6SAlberto Garcia        # We can't reopen with the original options because that would
896bf3e50f6SAlberto Garcia        # make hd1 read-only and block-stream requires it to be read-write
897c423a6afSMax Reitz        # (Which error message appears depends on whether the stream job is
898c423a6afSMax Reitz        # already done with copying at this point.)
899c423a6afSMax Reitz        self.reopen(opts, {},
900c423a6afSMax Reitz            ["Can't set node 'hd1' to r/o with copy-on-read enabled",
901c423a6afSMax Reitz             "Cannot make block node read-only, there is a writer on it"])
902bf3e50f6SAlberto Garcia
903bf3e50f6SAlberto Garcia        # We can't remove hd2 while the stream job is ongoing
904bf3e50f6SAlberto Garcia        opts['backing']['backing'] = None
905bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing.read-only': False}, "Cannot change 'backing' link from 'hd1' to 'hd2'")
906bf3e50f6SAlberto Garcia
907bf3e50f6SAlberto Garcia        # We can detach hd1 from hd0 because it doesn't affect the stream job
908bf3e50f6SAlberto Garcia        opts['backing'] = None
909bf3e50f6SAlberto Garcia        self.reopen(opts)
910bf3e50f6SAlberto Garcia
911c423a6afSMax Reitz        self.vm.run_job('stream0', auto_finalize = False, auto_dismiss = True)
912bf3e50f6SAlberto Garcia
913bf3e50f6SAlberto Garcia    # Reopen the chain during a block-commit job (from hd0 to hd2)
914bf3e50f6SAlberto Garcia    def test_block_commit_1(self):
915bf3e50f6SAlberto Garcia        # hd2 <- hd1 <- hd0
916bf3e50f6SAlberto Garcia        opts = hd_opts(0)
917bf3e50f6SAlberto Garcia        opts['backing'] = hd_opts(1)
918bf3e50f6SAlberto Garcia        opts['backing']['backing'] = hd_opts(2)
919bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
920bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
921bf3e50f6SAlberto Garcia
922bf3e50f6SAlberto Garcia        result = self.vm.qmp('block-commit', conv_keys = True, job_id = 'commit0',
923c423a6afSMax Reitz                             device = 'hd0')
924bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
925bf3e50f6SAlberto Garcia
926bf3e50f6SAlberto Garcia        # We can't remove hd2 while the commit job is ongoing
927bf3e50f6SAlberto Garcia        opts['backing']['backing'] = None
928bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "Cannot change 'backing' link from 'hd1' to 'hd2'")
929bf3e50f6SAlberto Garcia
930bf3e50f6SAlberto Garcia        # We can't remove hd1 while the commit job is ongoing
931bf3e50f6SAlberto Garcia        opts['backing'] = None
932bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "Cannot change 'backing' link from 'hd0' to 'hd1'")
933bf3e50f6SAlberto Garcia
934bf3e50f6SAlberto Garcia        event = self.vm.event_wait(name='BLOCK_JOB_READY')
935bf3e50f6SAlberto Garcia        self.assert_qmp(event, 'data/device', 'commit0')
936bf3e50f6SAlberto Garcia        self.assert_qmp(event, 'data/type', 'commit')
937bf3e50f6SAlberto Garcia        self.assert_qmp_absent(event, 'data/error')
938bf3e50f6SAlberto Garcia
939bf3e50f6SAlberto Garcia        result = self.vm.qmp('block-job-complete', device='commit0')
940bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
941bf3e50f6SAlberto Garcia
942bf3e50f6SAlberto Garcia        self.wait_until_completed(drive = 'commit0')
943bf3e50f6SAlberto Garcia
944bf3e50f6SAlberto Garcia    # Reopen the chain during a block-commit job (from hd1 to hd2)
945bf3e50f6SAlberto Garcia    def test_block_commit_2(self):
946bf3e50f6SAlberto Garcia        # hd2 <- hd1 <- hd0
947bf3e50f6SAlberto Garcia        opts = hd_opts(0)
948bf3e50f6SAlberto Garcia        opts['backing'] = hd_opts(1)
949bf3e50f6SAlberto Garcia        opts['backing']['backing'] = hd_opts(2)
950bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
951bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
952bf3e50f6SAlberto Garcia
953bf3e50f6SAlberto Garcia        result = self.vm.qmp('block-commit', conv_keys = True, job_id = 'commit0',
954c423a6afSMax Reitz                             device = 'hd0', top_node = 'hd1',
955c423a6afSMax Reitz                             auto_finalize = False)
956bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
957bf3e50f6SAlberto Garcia
958bf3e50f6SAlberto Garcia        # We can't remove hd2 while the commit job is ongoing
959bf3e50f6SAlberto Garcia        opts['backing']['backing'] = None
960bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "Cannot change the option 'backing.driver'")
961bf3e50f6SAlberto Garcia
962bf3e50f6SAlberto Garcia        # We can't remove hd1 while the commit job is ongoing
963bf3e50f6SAlberto Garcia        opts['backing'] = None
964bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "Cannot change backing link if 'hd0' has an implicit backing file")
965bf3e50f6SAlberto Garcia
966bf3e50f6SAlberto Garcia        # hd2 <- hd0
967c423a6afSMax Reitz        self.vm.run_job('commit0', auto_finalize = False, auto_dismiss = True)
968bf3e50f6SAlberto Garcia
969bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd0'), 'ro', False)
970bf3e50f6SAlberto Garcia        self.assertEqual(self.get_node('hd1'), None)
971bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd2'), 'ro', True)
972bf3e50f6SAlberto Garcia
97397518e11SKevin Wolf    def run_test_iothreads(self, iothread_a, iothread_b, errmsg = None):
974bf3e50f6SAlberto Garcia        opts = hd_opts(0)
975bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts)
976bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
977bf3e50f6SAlberto Garcia
978bf3e50f6SAlberto Garcia        opts2 = hd_opts(2)
979bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-add', conv_keys = False, **opts2)
980bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
981bf3e50f6SAlberto Garcia
982bf3e50f6SAlberto Garcia        result = self.vm.qmp('object-add', qom_type='iothread', id='iothread0')
983bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
984bf3e50f6SAlberto Garcia
985bf3e50f6SAlberto Garcia        result = self.vm.qmp('object-add', qom_type='iothread', id='iothread1')
986bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
987bf3e50f6SAlberto Garcia
98897518e11SKevin Wolf        result = self.vm.qmp('device_add', driver='virtio-scsi', id='scsi0',
98997518e11SKevin Wolf                             iothread=iothread_a)
990bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
991bf3e50f6SAlberto Garcia
99297518e11SKevin Wolf        result = self.vm.qmp('device_add', driver='virtio-scsi', id='scsi1',
99397518e11SKevin Wolf                             iothread=iothread_b)
994bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', {})
995bf3e50f6SAlberto Garcia
99697518e11SKevin Wolf        if iothread_a:
99797518e11SKevin Wolf            result = self.vm.qmp('device_add', driver='scsi-hd', drive='hd0',
99897518e11SKevin Wolf                                 share_rw=True, bus="scsi0.0")
999bf3e50f6SAlberto Garcia            self.assert_qmp(result, 'return', {})
1000bf3e50f6SAlberto Garcia
100197518e11SKevin Wolf        if iothread_b:
100297518e11SKevin Wolf            result = self.vm.qmp('device_add', driver='scsi-hd', drive='hd2',
100397518e11SKevin Wolf                                 share_rw=True, bus="scsi1.0")
100497518e11SKevin Wolf            self.assert_qmp(result, 'return', {})
100597518e11SKevin Wolf
100697518e11SKevin Wolf        # Attaching the backing file may or may not work
100797518e11SKevin Wolf        self.reopen(opts, {'backing': 'hd2'}, errmsg)
100897518e11SKevin Wolf
100997518e11SKevin Wolf        # But removing the backing file should always work
101097518e11SKevin Wolf        self.reopen(opts, {'backing': None})
101197518e11SKevin Wolf
101297518e11SKevin Wolf        self.vm.shutdown()
101397518e11SKevin Wolf
101497518e11SKevin Wolf    # We don't allow setting a backing file that uses a different AioContext if
101597518e11SKevin Wolf    # neither of them can switch to the other AioContext
101697518e11SKevin Wolf    def test_iothreads_error(self):
101797518e11SKevin Wolf        self.run_test_iothreads('iothread0', 'iothread1',
10181de6b45fSKevin Wolf                                "Cannot change iothread of active block backend")
101997518e11SKevin Wolf
102097518e11SKevin Wolf    def test_iothreads_compatible_users(self):
102197518e11SKevin Wolf        self.run_test_iothreads('iothread0', 'iothread0')
102297518e11SKevin Wolf
102397518e11SKevin Wolf    def test_iothreads_switch_backing(self):
10241de6b45fSKevin Wolf        self.run_test_iothreads('iothread0', None)
102597518e11SKevin Wolf
102697518e11SKevin Wolf    def test_iothreads_switch_overlay(self):
10271de6b45fSKevin Wolf        self.run_test_iothreads(None, 'iothread0')
1028bf3e50f6SAlberto Garcia
1029bf3e50f6SAlberto Garciaif __name__ == '__main__':
1030*52ea799eSJohn Snow    iotests.activate_logging()
1031103cbc77SMax Reitz    iotests.main(supported_fmts=["qcow2"],
1032103cbc77SMax Reitz                 supported_protocols=["file"])
1033