xref: /qemu/tests/qemu-iotests/245 (revision b6aed193)
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 copy
24bf3e50f6SAlberto Garciaimport json
256dede6a4SJohn Snowimport os
266dede6a4SJohn Snowimport re
276dede6a4SJohn Snowfrom subprocess import CalledProcessError
286dede6a4SJohn Snow
296dede6a4SJohn Snowimport iotests
30bf3e50f6SAlberto Garciafrom iotests import qemu_img, qemu_io
31bf3e50f6SAlberto Garcia
32bf3e50f6SAlberto Garciahd_path = [
33bf3e50f6SAlberto Garcia    os.path.join(iotests.test_dir, 'hd0.img'),
34bf3e50f6SAlberto Garcia    os.path.join(iotests.test_dir, 'hd1.img'),
35bf3e50f6SAlberto Garcia    os.path.join(iotests.test_dir, 'hd2.img')
36bf3e50f6SAlberto Garcia]
37bf3e50f6SAlberto Garcia
38bf3e50f6SAlberto Garciadef hd_opts(idx):
39bf3e50f6SAlberto Garcia    return {'driver': iotests.imgfmt,
40bf3e50f6SAlberto Garcia            'node-name': 'hd%d' % idx,
41bf3e50f6SAlberto Garcia            'file': {'driver': 'file',
42bf3e50f6SAlberto Garcia                     'node-name': 'hd%d-file' % idx,
43bf3e50f6SAlberto Garcia                     'filename':  hd_path[idx] } }
44bf3e50f6SAlberto Garcia
45bf3e50f6SAlberto Garciaclass TestBlockdevReopen(iotests.QMPTestCase):
46bf3e50f6SAlberto Garcia    total_io_cmds = 0
47bf3e50f6SAlberto Garcia
48bf3e50f6SAlberto Garcia    def setUp(self):
49bf3e50f6SAlberto Garcia        qemu_img('create', '-f', iotests.imgfmt, hd_path[0], '3M')
50b66ff2c2SEric Blake        qemu_img('create', '-f', iotests.imgfmt, '-b', hd_path[0],
51b66ff2c2SEric Blake                 '-F', iotests.imgfmt, hd_path[1])
52bf3e50f6SAlberto Garcia        qemu_img('create', '-f', iotests.imgfmt, hd_path[2], '3M')
53bf3e50f6SAlberto Garcia        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xa0  0 1M', hd_path[0])
54bf3e50f6SAlberto Garcia        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xa1 1M 1M', hd_path[1])
55bf3e50f6SAlberto Garcia        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xa2 2M 1M', hd_path[2])
56bf3e50f6SAlberto Garcia        self.vm = iotests.VM()
57bf3e50f6SAlberto Garcia        self.vm.launch()
58bf3e50f6SAlberto Garcia
59bf3e50f6SAlberto Garcia    def tearDown(self):
60bf3e50f6SAlberto Garcia        self.vm.shutdown()
61bf3e50f6SAlberto Garcia        self.check_qemu_io_errors()
62bf3e50f6SAlberto Garcia        os.remove(hd_path[0])
63bf3e50f6SAlberto Garcia        os.remove(hd_path[1])
64bf3e50f6SAlberto Garcia        os.remove(hd_path[2])
65bf3e50f6SAlberto Garcia
66bf3e50f6SAlberto Garcia    # The output of qemu-io is not returned by vm.hmp_qemu_io() but
67bf3e50f6SAlberto Garcia    # it's stored in the log and can only be read when the VM has been
68bf3e50f6SAlberto Garcia    # shut down. This function runs qemu-io and keeps track of the
69bf3e50f6SAlberto Garcia    # number of times it's been called.
70bf3e50f6SAlberto Garcia    def run_qemu_io(self, img, cmd):
71bf3e50f6SAlberto Garcia        result = self.vm.hmp_qemu_io(img, cmd)
72bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'return', '')
73bf3e50f6SAlberto Garcia        self.total_io_cmds += 1
74bf3e50f6SAlberto Garcia
75bf3e50f6SAlberto Garcia    # Once the VM is shut down we can parse the log and see if qemu-io
76bf3e50f6SAlberto Garcia    # ran without errors.
77bf3e50f6SAlberto Garcia    def check_qemu_io_errors(self):
78bf3e50f6SAlberto Garcia        self.assertFalse(self.vm.is_running())
79bf3e50f6SAlberto Garcia        found = 0
80bf3e50f6SAlberto Garcia        log = self.vm.get_log()
81bf3e50f6SAlberto Garcia        for line in log.split("\n"):
82bf3e50f6SAlberto Garcia            if line.startswith("Pattern verification failed"):
83bf3e50f6SAlberto Garcia                raise Exception("%s (command #%d)" % (line, found))
844c5393f1SAlberto Garcia            if re.match("(read|wrote) .*/.* bytes at offset", line):
85bf3e50f6SAlberto Garcia                found += 1
86bf3e50f6SAlberto Garcia        self.assertEqual(found, self.total_io_cmds,
87bf3e50f6SAlberto Garcia                         "Expected output of %d qemu-io commands, found %d" %
88bf3e50f6SAlberto Garcia                         (found, self.total_io_cmds))
89bf3e50f6SAlberto Garcia
90e60edf69SAlberto Garcia    # Run blockdev-reopen on a list of block devices
913908b7a8SAlberto Garcia    def reopenMultiple(self, opts, errmsg = None):
92e60edf69SAlberto Garcia        result = self.vm.qmp('blockdev-reopen', conv_keys=False, options=opts)
933908b7a8SAlberto Garcia        if errmsg:
943908b7a8SAlberto Garcia            self.assert_qmp(result, 'error/class', 'GenericError')
953908b7a8SAlberto Garcia            self.assert_qmp(result, 'error/desc', errmsg)
963908b7a8SAlberto Garcia        else:
973908b7a8SAlberto Garcia            self.assert_qmp(result, 'return', {})
983908b7a8SAlberto Garcia
99e60edf69SAlberto Garcia    # Run blockdev-reopen on a single block device (specified by
1003908b7a8SAlberto Garcia    # 'opts') but applying 'newopts' on top of it. The original 'opts'
1013908b7a8SAlberto Garcia    # dict is unmodified
102bf3e50f6SAlberto Garcia    def reopen(self, opts, newopts = {}, errmsg = None):
103bf3e50f6SAlberto Garcia        opts = copy.deepcopy(opts)
104bf3e50f6SAlberto Garcia
105bf3e50f6SAlberto Garcia        # Apply the changes from 'newopts' on top of 'opts'
106bf3e50f6SAlberto Garcia        for key in newopts:
107bf3e50f6SAlberto Garcia            value = newopts[key]
108bf3e50f6SAlberto Garcia            # If key has the form "foo.bar" then we need to do
109bf3e50f6SAlberto Garcia            # opts["foo"]["bar"] = value, not opts["foo.bar"] = value
110bf3e50f6SAlberto Garcia            subdict = opts
111bf3e50f6SAlberto Garcia            while key.find('.') != -1:
112bf3e50f6SAlberto Garcia                [prefix, key] = key.split('.', 1)
113bf3e50f6SAlberto Garcia                subdict = opts[prefix]
114bf3e50f6SAlberto Garcia            subdict[key] = value
115bf3e50f6SAlberto Garcia
1163908b7a8SAlberto Garcia        self.reopenMultiple([ opts ], errmsg)
117bf3e50f6SAlberto Garcia
118bf3e50f6SAlberto Garcia
119bf3e50f6SAlberto Garcia    # Run query-named-block-nodes and return the specified entry
120bf3e50f6SAlberto Garcia    def get_node(self, node_name):
121bf3e50f6SAlberto Garcia        result = self.vm.qmp('query-named-block-nodes')
122bf3e50f6SAlberto Garcia        for node in result['return']:
123bf3e50f6SAlberto Garcia            if node['node-name'] == node_name:
124bf3e50f6SAlberto Garcia                return node
125bf3e50f6SAlberto Garcia        return None
126bf3e50f6SAlberto Garcia
127bf3e50f6SAlberto Garcia    # Run 'query-named-block-nodes' and compare its output with the
128bf3e50f6SAlberto Garcia    # value passed by the user in 'graph'
129bf3e50f6SAlberto Garcia    def check_node_graph(self, graph):
130bf3e50f6SAlberto Garcia        result = self.vm.qmp('query-named-block-nodes')
131bf3e50f6SAlberto Garcia        self.assertEqual(json.dumps(graph,  sort_keys=True),
132bf3e50f6SAlberto Garcia                         json.dumps(result, sort_keys=True))
133bf3e50f6SAlberto Garcia
134bf3e50f6SAlberto Garcia    # This test opens one single disk image (without backing files)
135bf3e50f6SAlberto Garcia    # and tries to reopen it with illegal / incorrect parameters.
136bf3e50f6SAlberto Garcia    def test_incorrect_parameters_single_file(self):
137bf3e50f6SAlberto Garcia        # Open 'hd0' only (no backing files)
138bf3e50f6SAlberto Garcia        opts = hd_opts(0)
139b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-add', conv_keys = False, **opts)
140bf3e50f6SAlberto Garcia        original_graph = self.vm.qmp('query-named-block-nodes')
141bf3e50f6SAlberto Garcia
142bf3e50f6SAlberto Garcia        # We can reopen the image passing the same options
143bf3e50f6SAlberto Garcia        self.reopen(opts)
144bf3e50f6SAlberto Garcia
145bf3e50f6SAlberto Garcia        # We can also reopen passing a child reference in 'file'
146bf3e50f6SAlberto Garcia        self.reopen(opts, {'file': 'hd0-file'})
147bf3e50f6SAlberto Garcia
148bf3e50f6SAlberto Garcia        # We cannot change any of these
149ef2e38a1SConnor Kuehl        self.reopen(opts, {'node-name': 'not-found'}, "Failed to find node with node-name='not-found'")
150ef2e38a1SConnor Kuehl        self.reopen(opts, {'node-name': ''}, "Failed to find node with node-name=''")
1513908b7a8SAlberto Garcia        self.reopen(opts, {'node-name': None}, "Invalid parameter type for 'options[0].node-name', expected: string")
152bf3e50f6SAlberto Garcia        self.reopen(opts, {'driver': 'raw'}, "Cannot change the option 'driver'")
153ea29331bSMarkus Armbruster        self.reopen(opts, {'driver': ''}, "Parameter 'driver' does not accept value ''")
1543908b7a8SAlberto Garcia        self.reopen(opts, {'driver': None}, "Invalid parameter type for 'options[0].driver', expected: string")
155ecd30d2dSAlberto Garcia        self.reopen(opts, {'file': 'not-found'}, "Cannot find device='' nor node-name='not-found'")
156ecd30d2dSAlberto Garcia        self.reopen(opts, {'file': ''}, "Cannot find device='' nor node-name=''")
157bf3e50f6SAlberto Garcia        self.reopen(opts, {'file': None}, "Invalid parameter type for 'file', expected: BlockdevRef")
158bf3e50f6SAlberto Garcia        self.reopen(opts, {'file.node-name': 'newname'}, "Cannot change the option 'node-name'")
159bf3e50f6SAlberto Garcia        self.reopen(opts, {'file.driver': 'host_device'}, "Cannot change the option 'driver'")
160bf3e50f6SAlberto Garcia        self.reopen(opts, {'file.filename': hd_path[1]}, "Cannot change the option 'filename'")
161bf3e50f6SAlberto Garcia        self.reopen(opts, {'file.aio': 'native'}, "Cannot change the option 'aio'")
162bf3e50f6SAlberto Garcia        self.reopen(opts, {'file.locking': 'off'}, "Cannot change the option 'locking'")
1633908b7a8SAlberto Garcia        self.reopen(opts, {'file.filename': None}, "Invalid parameter type for 'options[0].file.filename', expected: string")
164bf3e50f6SAlberto Garcia
165e60edf69SAlberto Garcia        # node-name is optional in BlockdevOptions, but blockdev-reopen needs it
166bf3e50f6SAlberto Garcia        del opts['node-name']
167ef2e38a1SConnor Kuehl        self.reopen(opts, {}, "node-name not specified")
168bf3e50f6SAlberto Garcia
169bf3e50f6SAlberto Garcia        # Check that nothing has changed
170bf3e50f6SAlberto Garcia        self.check_node_graph(original_graph)
171bf3e50f6SAlberto Garcia
172bf3e50f6SAlberto Garcia        # Remove the node
173b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-del', conv_keys = True, node_name = 'hd0')
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)
181b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-add', conv_keys = False, **opts)
182bf3e50f6SAlberto Garcia        original_graph = self.vm.qmp('query-named-block-nodes')
183bf3e50f6SAlberto Garcia
184bf3e50f6SAlberto Garcia        # We can't reopen the image passing the same options, 'backing' is mandatory
185bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "backing is missing for 'hd1'")
186bf3e50f6SAlberto Garcia
187bf3e50f6SAlberto Garcia        # Everything works if we pass 'backing' using the existing node name
188bf3e50f6SAlberto Garcia        for node in original_graph['return']:
189bf3e50f6SAlberto Garcia            if node['drv'] == iotests.imgfmt and node['file'] == hd_path[0]:
190bf3e50f6SAlberto Garcia                backing_node_name = node['node-name']
191bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing': backing_node_name})
192bf3e50f6SAlberto Garcia
193bf3e50f6SAlberto Garcia        # We can't use a non-existing or empty (non-NULL) node as the backing image
194785ec4b1SConnor Kuehl        self.reopen(opts, {'backing': 'not-found'}, "Cannot find device=\'\' nor node-name=\'not-found\'")
195785ec4b1SConnor Kuehl        self.reopen(opts, {'backing': ''}, "Cannot find device=\'\' nor node-name=\'\'")
196bf3e50f6SAlberto Garcia
197bf3e50f6SAlberto Garcia        # We can reopen the image just fine if we specify the backing options
198bf3e50f6SAlberto Garcia        opts['backing'] = {'driver': iotests.imgfmt,
199bf3e50f6SAlberto Garcia                           'file': {'driver': 'file',
200bf3e50f6SAlberto Garcia                                    'filename': hd_path[0]}}
201bf3e50f6SAlberto Garcia        self.reopen(opts)
202bf3e50f6SAlberto Garcia
203bf3e50f6SAlberto Garcia        # We cannot change any of these options
204bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing.node-name': 'newname'}, "Cannot change the option 'node-name'")
205bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing.driver': 'raw'}, "Cannot change the option 'driver'")
206bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing.file.node-name': 'newname'}, "Cannot change the option 'node-name'")
207bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing.file.driver': 'host_device'}, "Cannot change the option 'driver'")
208bf3e50f6SAlberto Garcia
209bf3e50f6SAlberto Garcia        # Check that nothing has changed since the beginning
210bf3e50f6SAlberto Garcia        self.check_node_graph(original_graph)
211bf3e50f6SAlberto Garcia
212bf3e50f6SAlberto Garcia        # Remove the node
213b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-del', conv_keys = True, node_name = 'hd1')
214bf3e50f6SAlberto Garcia
215bf3e50f6SAlberto Garcia    # Reopen an image several times changing some of its options
216bf3e50f6SAlberto Garcia    def test_reopen(self):
2176dede6a4SJohn Snow        try:
2186dede6a4SJohn Snow            qemu_io('-f', 'raw', '-t', 'none', '-c', 'quit', hd_path[0])
2196dede6a4SJohn Snow            supports_direct = True
2206dede6a4SJohn Snow        except CalledProcessError as exc:
2216dede6a4SJohn Snow            if 'O_DIRECT' in exc.stdout:
22223e1d054SMax Reitz                supports_direct = False
22323e1d054SMax Reitz            else:
2246dede6a4SJohn Snow                raise
22523e1d054SMax Reitz
226bf3e50f6SAlberto Garcia        # Open the hd1 image passing all backing options
227bf3e50f6SAlberto Garcia        opts = hd_opts(1)
228bf3e50f6SAlberto Garcia        opts['backing'] = hd_opts(0)
229b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-add', conv_keys = False, **opts)
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
304b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-add', conv_keys = False, **hd_opts(0))
305bf3e50f6SAlberto Garcia
306bf3e50f6SAlberto Garcia        # Reopen the hd1 image setting 'hd0' as its backing image
307bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing': 'hd0'})
308bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'image/backing-image/filename', hd_path[0])
309bf3e50f6SAlberto Garcia
310bf3e50f6SAlberto Garcia        # Check that nothing has changed since the beginning
311bf3e50f6SAlberto Garcia        self.reopen(hd_opts(0), {'read-only': True})
312bf3e50f6SAlberto Garcia        self.check_node_graph(original_graph)
313bf3e50f6SAlberto Garcia
314bf3e50f6SAlberto Garcia        # The backing file (hd0) is now a reference, we cannot change backing.* anymore
315bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "Cannot change the option 'backing.driver'")
316bf3e50f6SAlberto Garcia
317bf3e50f6SAlberto Garcia        # We can't remove 'hd0' while it's a backing image of 'hd1'
318bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd0')
319bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'error/class', 'GenericError')
320bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'error/desc', "Node 'hd0' is busy: node is used as backing hd of 'hd1'")
321bf3e50f6SAlberto Garcia
322bf3e50f6SAlberto Garcia        # But we can remove both nodes if done in the proper order
323b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-del', conv_keys = True, node_name = 'hd1')
324b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-del', conv_keys = True, node_name = 'hd0')
325bf3e50f6SAlberto Garcia
326bf3e50f6SAlberto Garcia    # Reopen a raw image and see the effect of changing the 'offset' option
327bf3e50f6SAlberto Garcia    def test_reopen_raw(self):
328bf3e50f6SAlberto Garcia        opts = {'driver': 'raw', 'node-name': 'hd0',
329bf3e50f6SAlberto Garcia                'file': { 'driver': 'file',
330bf3e50f6SAlberto Garcia                          'filename': hd_path[0],
331bf3e50f6SAlberto Garcia                          'node-name': 'hd0-file' } }
332bf3e50f6SAlberto Garcia
333bf3e50f6SAlberto Garcia        # First we create a 2MB raw file, and fill each half with a
334bf3e50f6SAlberto Garcia        # different value
335bf3e50f6SAlberto Garcia        qemu_img('create', '-f', 'raw', hd_path[0], '2M')
336bf3e50f6SAlberto Garcia        qemu_io('-f', 'raw', '-c', 'write -P 0xa0  0 1M', hd_path[0])
337bf3e50f6SAlberto Garcia        qemu_io('-f', 'raw', '-c', 'write -P 0xa1 1M 1M', hd_path[0])
338bf3e50f6SAlberto Garcia
339bf3e50f6SAlberto Garcia        # Open the raw file with QEMU
340b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-add', conv_keys = False, **opts)
341bf3e50f6SAlberto Garcia
342bf3e50f6SAlberto Garcia        # Read 1MB from offset 0
343bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa0  0 1M")
344bf3e50f6SAlberto Garcia
345bf3e50f6SAlberto Garcia        # Reopen the image with a 1MB offset.
346bf3e50f6SAlberto Garcia        # Now the results are different
347bf3e50f6SAlberto Garcia        self.reopen(opts, {'offset': 1024*1024})
348bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa1  0 1M")
349bf3e50f6SAlberto Garcia
350bf3e50f6SAlberto Garcia        # Reopen again with the original options.
351bf3e50f6SAlberto Garcia        # We get the original results again
352bf3e50f6SAlberto Garcia        self.reopen(opts)
353bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa0  0 1M")
354bf3e50f6SAlberto Garcia
355bf3e50f6SAlberto Garcia        # Remove the block device
356b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-del', conv_keys = True, node_name = 'hd0')
357bf3e50f6SAlberto Garcia
358bf3e50f6SAlberto Garcia    # Omitting an option should reset it to the default value, but if
359bf3e50f6SAlberto Garcia    # an option cannot be changed it shouldn't be possible to reset it
360bf3e50f6SAlberto Garcia    # to its default value either
361bf3e50f6SAlberto Garcia    def test_reset_default_values(self):
362bf3e50f6SAlberto Garcia        opts = {'driver': 'qcow2', 'node-name': 'hd0',
363bf3e50f6SAlberto Garcia                'file': { 'driver': 'file',
364bf3e50f6SAlberto Garcia                          'filename': hd_path[0],
365bf3e50f6SAlberto Garcia                          'x-check-cache-dropped': True, # This one can be changed
366bf3e50f6SAlberto Garcia                          'locking': 'off',              # This one can NOT be changed
367bf3e50f6SAlberto Garcia                          'node-name': 'hd0-file' } }
368bf3e50f6SAlberto Garcia
369bf3e50f6SAlberto Garcia        # Open the file with QEMU
370b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-add', conv_keys = False, **opts)
371bf3e50f6SAlberto Garcia
372bf3e50f6SAlberto Garcia        # file.x-check-cache-dropped can be changed...
373bf3e50f6SAlberto Garcia        self.reopen(opts, { 'file.x-check-cache-dropped': False })
374bf3e50f6SAlberto Garcia        # ...and dropped completely (resetting to the default value)
375bf3e50f6SAlberto Garcia        del opts['file']['x-check-cache-dropped']
376bf3e50f6SAlberto Garcia        self.reopen(opts)
377bf3e50f6SAlberto Garcia
378bf3e50f6SAlberto Garcia        # file.locking cannot be changed nor reset to the default value
379bf3e50f6SAlberto Garcia        self.reopen(opts, { 'file.locking': 'on' }, "Cannot change the option 'locking'")
380bf3e50f6SAlberto Garcia        del opts['file']['locking']
381bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "Option 'locking' cannot be reset to its default value")
382bf3e50f6SAlberto Garcia        # But we can reopen it if we maintain its previous value
383bf3e50f6SAlberto Garcia        self.reopen(opts, { 'file.locking': 'off' })
384bf3e50f6SAlberto Garcia
385bf3e50f6SAlberto Garcia        # Remove the block device
386b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-del', conv_keys = True, node_name = 'hd0')
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
398b6aed193SVladimir Sementsov-Ogievskiy            self.vm.cmd('blockdev-add', conv_keys = False, **opts[i])
399bf3e50f6SAlberto Garcia
400bf3e50f6SAlberto Garcia        # hd0
401bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa0  0 1M")
402bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0    1M 1M")
403bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0    2M 1M")
404bf3e50f6SAlberto Garcia
405bf3e50f6SAlberto Garcia        # hd1 <- hd0
406bf3e50f6SAlberto Garcia        self.reopen(opts[0], {'backing': 'hd1'})
407bf3e50f6SAlberto Garcia
408bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa0  0 1M")
409bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa1 1M 1M")
410bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0    2M 1M")
411bf3e50f6SAlberto Garcia
412bf3e50f6SAlberto Garcia        # hd1 <- hd0 , hd1 <- hd2
413bf3e50f6SAlberto Garcia        self.reopen(opts[2], {'backing': 'hd1'})
414bf3e50f6SAlberto Garcia
415bf3e50f6SAlberto Garcia        self.run_qemu_io("hd2", "read -P 0     0 1M")
416bf3e50f6SAlberto Garcia        self.run_qemu_io("hd2", "read -P 0xa1 1M 1M")
417bf3e50f6SAlberto Garcia        self.run_qemu_io("hd2", "read -P 0xa2 2M 1M")
418bf3e50f6SAlberto Garcia
419bf3e50f6SAlberto Garcia        # hd1 <- hd2 <- hd0
420bf3e50f6SAlberto Garcia        self.reopen(opts[0], {'backing': 'hd2'})
421bf3e50f6SAlberto Garcia
422bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa0  0 1M")
423bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa1 1M 1M")
424bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa2 2M 1M")
425bf3e50f6SAlberto Garcia
426bf3e50f6SAlberto Garcia        # hd2 <- hd0
427bf3e50f6SAlberto Garcia        self.reopen(opts[2], {'backing': None})
428bf3e50f6SAlberto Garcia
429bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa0  0 1M")
430bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0    1M 1M")
431bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa2 2M 1M")
432bf3e50f6SAlberto Garcia
433bf3e50f6SAlberto Garcia        # hd2 <- hd1 <- hd0
434bf3e50f6SAlberto Garcia        self.reopen(opts[1], {'backing': 'hd2'})
435bf3e50f6SAlberto Garcia        self.reopen(opts[0], {'backing': 'hd1'})
436bf3e50f6SAlberto Garcia
437bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa0  0 1M")
438bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa1 1M 1M")
439bf3e50f6SAlberto Garcia        self.run_qemu_io("hd0", "read -P 0xa2 2M 1M")
440bf3e50f6SAlberto Garcia
441bf3e50f6SAlberto Garcia        # Illegal operation: hd2 is a child of hd1
442bf3e50f6SAlberto Garcia        self.reopen(opts[2], {'backing': 'hd1'},
443ecd30d2dSAlberto Garcia                    "Making 'hd1' a backing child of 'hd2' would create a cycle")
444bf3e50f6SAlberto Garcia
445bf3e50f6SAlberto Garcia        # hd2 <- hd0, hd2 <- hd1
446bf3e50f6SAlberto Garcia        self.reopen(opts[0], {'backing': 'hd2'})
447bf3e50f6SAlberto Garcia
448bf3e50f6SAlberto Garcia        self.run_qemu_io("hd1", "read -P 0     0 1M")
449bf3e50f6SAlberto Garcia        self.run_qemu_io("hd1", "read -P 0xa1 1M 1M")
450bf3e50f6SAlberto Garcia        self.run_qemu_io("hd1", "read -P 0xa2 2M 1M")
451bf3e50f6SAlberto Garcia
452bf3e50f6SAlberto Garcia        # More illegal operations
453bf3e50f6SAlberto Garcia        self.reopen(opts[2], {'backing': 'hd1'},
454ecd30d2dSAlberto Garcia                    "Making 'hd1' a backing child of 'hd2' would create a cycle")
455ecd30d2dSAlberto Garcia        self.reopen(opts[2], {'file': 'hd0-file'},
456ecd30d2dSAlberto 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).")
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
489b6aed193SVladimir Sementsov-Ogievskiy            self.vm.cmd('blockdev-add', conv_keys = False, **opts[i])
490bf3e50f6SAlberto Garcia
491bf3e50f6SAlberto Garcia        # hd1 <- hd0, hd1 <- hd2
492bf3e50f6SAlberto Garcia        self.reopen(opts[0], {'backing': 'hd1'})
493bf3e50f6SAlberto Garcia        self.reopen(opts[2], {'backing': 'hd1'})
494bf3e50f6SAlberto Garcia
495bf3e50f6SAlberto Garcia        # Illegal: hd2 is backed by hd1
496bf3e50f6SAlberto Garcia        self.reopen(opts[1], {'backing': 'hd2'},
497ecd30d2dSAlberto Garcia                    "Making 'hd2' a backing child of 'hd1' would create a cycle")
498bf3e50f6SAlberto Garcia
499bf3e50f6SAlberto Garcia        # hd1 <- hd0 <- hd2
500bf3e50f6SAlberto Garcia        self.reopen(opts[2], {'backing': 'hd0'})
501bf3e50f6SAlberto Garcia
502bf3e50f6SAlberto Garcia        # Illegal: hd2 is backed by hd0, which is backed by hd1
503bf3e50f6SAlberto Garcia        self.reopen(opts[1], {'backing': 'hd2'},
504ecd30d2dSAlberto Garcia                    "Making 'hd2' a backing child of 'hd1' would create a cycle")
505bf3e50f6SAlberto Garcia
506bf3e50f6SAlberto Garcia        # Illegal: hd1 cannot point to itself
507bf3e50f6SAlberto Garcia        self.reopen(opts[1], {'backing': 'hd1'},
508ecd30d2dSAlberto Garcia                    "Making 'hd1' a backing child of 'hd1' would create a cycle")
509bf3e50f6SAlberto Garcia
510bf3e50f6SAlberto Garcia        # Remove all backing files
511bf3e50f6SAlberto Garcia        self.reopen(opts[0])
512bf3e50f6SAlberto Garcia        self.reopen(opts[2])
513bf3e50f6SAlberto Garcia
514bf3e50f6SAlberto Garcia        ##########################################
515bf3e50f6SAlberto Garcia        # Add a blkverify node using hd0 and hd1 #
516bf3e50f6SAlberto Garcia        ##########################################
517bf3e50f6SAlberto Garcia        bvopts = {'driver': 'blkverify',
518bf3e50f6SAlberto Garcia                  'node-name': 'bv',
519bf3e50f6SAlberto Garcia                  'test': 'hd0',
520bf3e50f6SAlberto Garcia                  'raw': 'hd1'}
521b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-add', conv_keys = False, **bvopts)
522bf3e50f6SAlberto Garcia
523bf3e50f6SAlberto Garcia        # blkverify doesn't currently allow reopening. TODO: implement this
524bf3e50f6SAlberto Garcia        self.reopen(bvopts, {}, "Block format 'blkverify' used by node 'bv'" +
525bf3e50f6SAlberto Garcia                    " does not support reopening files")
526bf3e50f6SAlberto Garcia
527bf3e50f6SAlberto Garcia        # Illegal: hd0 is a child of the blkverify node
528bf3e50f6SAlberto Garcia        self.reopen(opts[0], {'backing': 'bv'},
529ecd30d2dSAlberto Garcia                    "Making 'bv' a backing child of 'hd0' would create a cycle")
530bf3e50f6SAlberto Garcia
531bf3e50f6SAlberto Garcia        # Delete the blkverify node
532b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-del', conv_keys = True, node_name = 'bv')
533bf3e50f6SAlberto Garcia
5344c5393f1SAlberto Garcia    # Replace the protocol layer ('file' parameter) of a disk image
5354c5393f1SAlberto Garcia    def test_replace_file(self):
5364c5393f1SAlberto Garcia        # Create two small raw images and add them to a running VM
5374c5393f1SAlberto Garcia        qemu_img('create', '-f', 'raw', hd_path[0], '10k')
5384c5393f1SAlberto Garcia        qemu_img('create', '-f', 'raw', hd_path[1], '10k')
5394c5393f1SAlberto Garcia
5404c5393f1SAlberto Garcia        hd0_opts = {'driver': 'file', 'node-name': 'hd0-file', 'filename':  hd_path[0] }
5414c5393f1SAlberto Garcia        hd1_opts = {'driver': 'file', 'node-name': 'hd1-file', 'filename':  hd_path[1] }
5424c5393f1SAlberto Garcia
543b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-add', conv_keys = False, **hd0_opts)
544b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-add', conv_keys = False, **hd1_opts)
5454c5393f1SAlberto Garcia
5464c5393f1SAlberto Garcia        # Add a raw format layer that uses hd0-file as its protocol layer
5474c5393f1SAlberto Garcia        opts = {'driver': 'raw', 'node-name': 'hd', 'file': 'hd0-file'}
5484c5393f1SAlberto Garcia
549b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-add', conv_keys = False, **opts)
5504c5393f1SAlberto Garcia
5514c5393f1SAlberto Garcia        # Fill the image with data
5524c5393f1SAlberto Garcia        self.run_qemu_io("hd", "read  -P 0 0 10k")
5534c5393f1SAlberto Garcia        self.run_qemu_io("hd", "write -P 0xa0 0 10k")
5544c5393f1SAlberto Garcia
5554c5393f1SAlberto Garcia        # Replace hd0-file with hd1-file and fill it with (different) data
5564c5393f1SAlberto Garcia        self.reopen(opts, {'file': 'hd1-file'})
5574c5393f1SAlberto Garcia        self.run_qemu_io("hd", "read  -P 0 0 10k")
5584c5393f1SAlberto Garcia        self.run_qemu_io("hd", "write -P 0xa1 0 10k")
5594c5393f1SAlberto Garcia
5604c5393f1SAlberto Garcia        # Use hd0-file again and check that it contains the expected data
5614c5393f1SAlberto Garcia        self.reopen(opts, {'file': 'hd0-file'})
5624c5393f1SAlberto Garcia        self.run_qemu_io("hd", "read  -P 0xa0 0 10k")
5634c5393f1SAlberto Garcia
5644c5393f1SAlberto Garcia        # And finally do the same with hd1-file
5654c5393f1SAlberto Garcia        self.reopen(opts, {'file': 'hd1-file'})
5664c5393f1SAlberto Garcia        self.run_qemu_io("hd", "read  -P 0xa1 0 10k")
5674c5393f1SAlberto Garcia
5684c5393f1SAlberto Garcia    # Insert (and remove) a throttle filter
5694c5393f1SAlberto Garcia    def test_insert_throttle_filter(self):
5704c5393f1SAlberto Garcia        # Add an image to the VM
5714c5393f1SAlberto Garcia        hd0_opts = hd_opts(0)
572b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-add', conv_keys = False, **hd0_opts)
5734c5393f1SAlberto Garcia
5744c5393f1SAlberto Garcia        # Create a throttle-group object
5754c5393f1SAlberto Garcia        opts = { 'qom-type': 'throttle-group', 'id': 'group0',
5764c5393f1SAlberto Garcia                 'limits': { 'iops-total': 1000 } }
577b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('object-add', conv_keys = False, **opts)
5784c5393f1SAlberto Garcia
5794c5393f1SAlberto Garcia        # Add a throttle filter with the group that we just created.
5804c5393f1SAlberto Garcia        # The filter is not used by anyone yet
5814c5393f1SAlberto Garcia        opts = { 'driver': 'throttle', 'node-name': 'throttle0',
5824c5393f1SAlberto Garcia                 'throttle-group': 'group0', 'file': 'hd0-file' }
583b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-add', conv_keys = False, **opts)
5844c5393f1SAlberto Garcia
5854c5393f1SAlberto Garcia        # Insert the throttle filter between hd0 and hd0-file
5864c5393f1SAlberto Garcia        self.reopen(hd0_opts, {'file': 'throttle0'})
5874c5393f1SAlberto Garcia
5884c5393f1SAlberto Garcia        # Remove the throttle filter from hd0
5894c5393f1SAlberto Garcia        self.reopen(hd0_opts, {'file': 'hd0-file'})
5904c5393f1SAlberto Garcia
5914c5393f1SAlberto Garcia    # Insert (and remove) a compress filter
59278935fcdSKevin Wolf    @iotests.skip_if_unsupported(['compress'])
5934c5393f1SAlberto Garcia    def test_insert_compress_filter(self):
5944c5393f1SAlberto Garcia        # Add an image to the VM: hd (raw) -> hd0 (qcow2) -> hd0-file (file)
5954c5393f1SAlberto Garcia        opts = {'driver': 'raw', 'node-name': 'hd', 'file': hd_opts(0)}
596b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-add', conv_keys = False, **opts)
5974c5393f1SAlberto Garcia
5984c5393f1SAlberto Garcia        # Add a 'compress' filter
5994c5393f1SAlberto Garcia        filter_opts = {'driver': 'compress',
6004c5393f1SAlberto Garcia                       'node-name': 'compress0',
6014c5393f1SAlberto Garcia                       'file': 'hd0'}
602b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-add', conv_keys = False, **filter_opts)
6034c5393f1SAlberto Garcia
6044c5393f1SAlberto Garcia        # Unmap the beginning of the image (we cannot write compressed
6054c5393f1SAlberto Garcia        # data to an allocated cluster)
6064c5393f1SAlberto Garcia        self.run_qemu_io("hd", "write -z -u 0 128k")
6074c5393f1SAlberto Garcia
6084c5393f1SAlberto Garcia        # Write data to the first cluster
6094c5393f1SAlberto Garcia        self.run_qemu_io("hd", "write -P 0xa0 0 64k")
6104c5393f1SAlberto Garcia
6114c5393f1SAlberto Garcia        # Insert the filter then write to the second cluster
6124c5393f1SAlberto Garcia        # hd -> compress0 -> hd0 -> hd0-file
6134c5393f1SAlberto Garcia        self.reopen(opts, {'file': 'compress0'})
6144c5393f1SAlberto Garcia        self.run_qemu_io("hd", "write -P 0xa1 64k 64k")
6154c5393f1SAlberto Garcia
6164c5393f1SAlberto Garcia        # Remove the filter then write to the third cluster
6174c5393f1SAlberto Garcia        # hd -> hd0 -> hd0-file
6184c5393f1SAlberto Garcia        self.reopen(opts, {'file': 'hd0'})
6194c5393f1SAlberto Garcia        self.run_qemu_io("hd", "write -P 0xa2 128k 64k")
6204c5393f1SAlberto Garcia
6214c5393f1SAlberto Garcia        # Verify the data that we just wrote
6224c5393f1SAlberto Garcia        self.run_qemu_io("hd", "read -P 0xa0    0 64k")
6234c5393f1SAlberto Garcia        self.run_qemu_io("hd", "read -P 0xa1  64k 64k")
6244c5393f1SAlberto Garcia        self.run_qemu_io("hd", "read -P 0xa2 128k 64k")
6254c5393f1SAlberto Garcia
6264c5393f1SAlberto Garcia        self.vm.shutdown()
6274c5393f1SAlberto Garcia
6284c5393f1SAlberto Garcia        # Check the first byte of the first three L2 entries and verify that
6294c5393f1SAlberto Garcia        # the second one is compressed (0x40) while the others are not (0x80)
63078935fcdSKevin Wolf        iotests.qemu_io('-f', 'raw', '-c', 'read -P 0x80 0x40000 1',
6314c5393f1SAlberto Garcia                                     '-c', 'read -P 0x40 0x40008 1',
6324c5393f1SAlberto Garcia                                     '-c', 'read -P 0x80 0x40010 1', hd_path[0])
6334c5393f1SAlberto Garcia
634246ebc2dSAlberto Garcia    # Swap the disk images of two active block devices
635246ebc2dSAlberto Garcia    def test_swap_files(self):
636246ebc2dSAlberto Garcia        # Add hd0 and hd2 (none of them with backing files)
637246ebc2dSAlberto Garcia        opts0 = hd_opts(0)
638b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-add', conv_keys = False, **opts0)
639246ebc2dSAlberto Garcia
640246ebc2dSAlberto Garcia        opts2 = hd_opts(2)
641b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-add', conv_keys = False, **opts2)
642246ebc2dSAlberto Garcia
643246ebc2dSAlberto Garcia        # Write different data to both block devices
644246ebc2dSAlberto Garcia        self.run_qemu_io("hd0", "write -P 0xa0 0 1k")
645246ebc2dSAlberto Garcia        self.run_qemu_io("hd2", "write -P 0xa2 0 1k")
646246ebc2dSAlberto Garcia
647246ebc2dSAlberto Garcia        # Check that the data reads correctly
648246ebc2dSAlberto Garcia        self.run_qemu_io("hd0", "read  -P 0xa0 0 1k")
649246ebc2dSAlberto Garcia        self.run_qemu_io("hd2", "read  -P 0xa2 0 1k")
650246ebc2dSAlberto Garcia
651246ebc2dSAlberto Garcia        # It's not possible to make a block device use an image that
652246ebc2dSAlberto Garcia        # is already being used by the other device.
653246ebc2dSAlberto Garcia        self.reopen(opts0, {'file': 'hd2-file'},
654246ebc2dSAlberto Garcia                    "Permission conflict on node 'hd2-file': permissions "
655246ebc2dSAlberto Garcia                    "'write, resize' are both required by node 'hd2' (uses "
656246ebc2dSAlberto Garcia                    "node 'hd2-file' as 'file' child) and unshared by node "
657246ebc2dSAlberto Garcia                    "'hd0' (uses node 'hd2-file' as 'file' child).")
658246ebc2dSAlberto Garcia        self.reopen(opts2, {'file': 'hd0-file'},
659246ebc2dSAlberto Garcia                    "Permission conflict on node 'hd0-file': permissions "
660246ebc2dSAlberto Garcia                    "'write, resize' are both required by node 'hd0' (uses "
661246ebc2dSAlberto Garcia                    "node 'hd0-file' as 'file' child) and unshared by node "
662246ebc2dSAlberto Garcia                    "'hd2' (uses node 'hd0-file' as 'file' child).")
663246ebc2dSAlberto Garcia
664246ebc2dSAlberto Garcia        # But we can swap the images if we reopen both devices at the
665246ebc2dSAlberto Garcia        # same time
666246ebc2dSAlberto Garcia        opts0['file'] = 'hd2-file'
667246ebc2dSAlberto Garcia        opts2['file'] = 'hd0-file'
668246ebc2dSAlberto Garcia        self.reopenMultiple([opts0, opts2])
669246ebc2dSAlberto Garcia        self.run_qemu_io("hd0", "read  -P 0xa2 0 1k")
670246ebc2dSAlberto Garcia        self.run_qemu_io("hd2", "read  -P 0xa0 0 1k")
671246ebc2dSAlberto Garcia
672246ebc2dSAlberto Garcia        # And we can of course come back to the original state
673246ebc2dSAlberto Garcia        opts0['file'] = 'hd0-file'
674246ebc2dSAlberto Garcia        opts2['file'] = 'hd2-file'
675246ebc2dSAlberto Garcia        self.reopenMultiple([opts0, opts2])
676246ebc2dSAlberto Garcia        self.run_qemu_io("hd0", "read  -P 0xa0 0 1k")
677246ebc2dSAlberto Garcia        self.run_qemu_io("hd2", "read  -P 0xa2 0 1k")
678246ebc2dSAlberto Garcia
679bf3e50f6SAlberto Garcia    # Misc reopen tests with different block drivers
6809442bebeSThomas Huth    @iotests.skip_if_unsupported(['quorum', 'throttle'])
681bf3e50f6SAlberto Garcia    def test_misc_drivers(self):
682bf3e50f6SAlberto Garcia        ####################
683bf3e50f6SAlberto Garcia        ###### quorum ######
684bf3e50f6SAlberto Garcia        ####################
685bf3e50f6SAlberto Garcia        for i in range(3):
686bf3e50f6SAlberto Garcia            opts = hd_opts(i)
687bf3e50f6SAlberto Garcia            # Open all three images without backing file
688bf3e50f6SAlberto Garcia            opts['backing'] = None
689b6aed193SVladimir Sementsov-Ogievskiy            self.vm.cmd('blockdev-add', conv_keys = False, **opts)
690bf3e50f6SAlberto Garcia
691bf3e50f6SAlberto Garcia        opts = {'driver': 'quorum',
692bf3e50f6SAlberto Garcia                'node-name': 'quorum0',
693bf3e50f6SAlberto Garcia                'children': ['hd0', 'hd1', 'hd2'],
694bf3e50f6SAlberto Garcia                'vote-threshold': 2}
695b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-add', conv_keys = False, **opts)
696bf3e50f6SAlberto Garcia
697bf3e50f6SAlberto Garcia        # Quorum doesn't currently allow reopening. TODO: implement this
698bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "Block format 'quorum' used by node 'quorum0'" +
699bf3e50f6SAlberto Garcia                    " does not support reopening files")
700bf3e50f6SAlberto Garcia
701bf3e50f6SAlberto Garcia        # You can't make quorum0 a backing file of hd0:
702bf3e50f6SAlberto Garcia        # hd0 is already a child of quorum0.
703bf3e50f6SAlberto Garcia        self.reopen(hd_opts(0), {'backing': 'quorum0'},
704ecd30d2dSAlberto Garcia                    "Making 'quorum0' a backing child of 'hd0' would create a cycle")
705bf3e50f6SAlberto Garcia
706bf3e50f6SAlberto Garcia        # Delete quorum0
707b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-del', conv_keys = True, node_name = 'quorum0')
708bf3e50f6SAlberto Garcia
709bf3e50f6SAlberto Garcia        # Delete hd0, hd1 and hd2
710bf3e50f6SAlberto Garcia        for i in range(3):
711b6aed193SVladimir Sementsov-Ogievskiy            self.vm.cmd('blockdev-del', conv_keys = True,
712bf3e50f6SAlberto Garcia                        node_name = 'hd%d' % i)
713bf3e50f6SAlberto Garcia
714bf3e50f6SAlberto Garcia        ######################
715bf3e50f6SAlberto Garcia        ###### blkdebug ######
716bf3e50f6SAlberto Garcia        ######################
717bf3e50f6SAlberto Garcia        opts = {'driver': 'blkdebug',
718bf3e50f6SAlberto Garcia                'node-name': 'bd',
719bf3e50f6SAlberto Garcia                'config': '/dev/null',
720bf3e50f6SAlberto Garcia                'image': hd_opts(0)}
721b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-add', conv_keys = False, **opts)
722bf3e50f6SAlberto Garcia
723bf3e50f6SAlberto Garcia        # blkdebug allows reopening if we keep the same options
724bf3e50f6SAlberto Garcia        self.reopen(opts)
725bf3e50f6SAlberto Garcia
726bf3e50f6SAlberto Garcia        # but it currently does not allow changes
727bf3e50f6SAlberto Garcia        self.reopen(opts, {'image': 'hd1'}, "Cannot change the option 'image'")
728bf3e50f6SAlberto Garcia        self.reopen(opts, {'align': 33554432}, "Cannot change the option 'align'")
729bf3e50f6SAlberto Garcia        self.reopen(opts, {'config': '/non/existent'}, "Cannot change the option 'config'")
730bf3e50f6SAlberto Garcia        del opts['config']
731bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "Option 'config' cannot be reset to its default value")
732bf3e50f6SAlberto Garcia
733bf3e50f6SAlberto Garcia        # Delete the blkdebug node
734b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-del', conv_keys = True, node_name = 'bd')
735bf3e50f6SAlberto Garcia
736bf3e50f6SAlberto Garcia        ##################
737bf3e50f6SAlberto Garcia        ###### null ######
738bf3e50f6SAlberto Garcia        ##################
739a6f8f9f8SMax Reitz        opts = {'driver': 'null-co', 'node-name': 'root', 'size': 1024}
740bf3e50f6SAlberto Garcia
741b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-add', conv_keys = False, **opts)
742bf3e50f6SAlberto Garcia
743bf3e50f6SAlberto Garcia        # 1 << 30 is the default value, but we cannot change it explicitly
744bf3e50f6SAlberto Garcia        self.reopen(opts, {'size': (1 << 30)}, "Cannot change the option 'size'")
745bf3e50f6SAlberto Garcia
746bf3e50f6SAlberto Garcia        # We cannot change 'size' back to its default value either
747bf3e50f6SAlberto Garcia        del opts['size']
748bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "Option 'size' cannot be reset to its default value")
749bf3e50f6SAlberto Garcia
750b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-del', conv_keys = True, node_name = 'root')
751bf3e50f6SAlberto Garcia
752bf3e50f6SAlberto Garcia        ##################
753bf3e50f6SAlberto Garcia        ###### file ######
754bf3e50f6SAlberto Garcia        ##################
755bf3e50f6SAlberto Garcia        opts = hd_opts(0)
756bf3e50f6SAlberto Garcia        opts['file']['locking'] = 'on'
757b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-add', conv_keys = False, **opts)
758bf3e50f6SAlberto Garcia
759bf3e50f6SAlberto Garcia        # 'locking' cannot be changed
760bf3e50f6SAlberto Garcia        del opts['file']['locking']
761bf3e50f6SAlberto Garcia        self.reopen(opts, {'file.locking': 'on'})
762bf3e50f6SAlberto Garcia        self.reopen(opts, {'file.locking': 'off'}, "Cannot change the option 'locking'")
763bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "Option 'locking' cannot be reset to its default value")
764bf3e50f6SAlberto Garcia
765bf3e50f6SAlberto Garcia        # Trying to reopen the 'file' node directly does not make a difference
766bf3e50f6SAlberto Garcia        opts = opts['file']
767bf3e50f6SAlberto Garcia        self.reopen(opts, {'locking': 'on'})
768bf3e50f6SAlberto Garcia        self.reopen(opts, {'locking': 'off'}, "Cannot change the option 'locking'")
769bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "Option 'locking' cannot be reset to its default value")
770bf3e50f6SAlberto Garcia
771b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-del', conv_keys = True, node_name = 'hd0')
772bf3e50f6SAlberto Garcia
773bf3e50f6SAlberto Garcia        ######################
774bf3e50f6SAlberto Garcia        ###### throttle ######
775bf3e50f6SAlberto Garcia        ######################
776bf3e50f6SAlberto Garcia        opts = { 'qom-type': 'throttle-group', 'id': 'group0',
777fa818b2fSAlberto Garcia                 'limits': { 'iops-total': 1000 } }
778b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('object-add', conv_keys = False, **opts)
779bf3e50f6SAlberto Garcia
780bf3e50f6SAlberto Garcia        opts = { 'qom-type': 'throttle-group', 'id': 'group1',
781fa818b2fSAlberto Garcia                 'limits': { 'iops-total': 2000 } }
782b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('object-add', conv_keys = False, **opts)
783bf3e50f6SAlberto Garcia
784bf3e50f6SAlberto Garcia        # Add a throttle filter with group = group0
785bf3e50f6SAlberto Garcia        opts = { 'driver': 'throttle', 'node-name': 'throttle0',
786bf3e50f6SAlberto Garcia                 'throttle-group': 'group0', 'file': hd_opts(0) }
787b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-add', conv_keys = False, **opts)
788bf3e50f6SAlberto Garcia
789bf3e50f6SAlberto Garcia        # We can reopen it if we keep the same options
790bf3e50f6SAlberto Garcia        self.reopen(opts)
791bf3e50f6SAlberto Garcia
792bf3e50f6SAlberto Garcia        # We can also reopen if 'file' is a reference to the child
793bf3e50f6SAlberto Garcia        self.reopen(opts, {'file': 'hd0'})
794bf3e50f6SAlberto Garcia
795bf3e50f6SAlberto Garcia        # This is illegal
796bf3e50f6SAlberto Garcia        self.reopen(opts, {'throttle-group': 'notfound'}, "Throttle group 'notfound' does not exist")
797bf3e50f6SAlberto Garcia
798bf3e50f6SAlberto Garcia        # But it's possible to change the group to group1
799bf3e50f6SAlberto Garcia        self.reopen(opts, {'throttle-group': 'group1'})
800bf3e50f6SAlberto Garcia
801bf3e50f6SAlberto Garcia        # Now group1 is in use, it cannot be deleted
802bf3e50f6SAlberto Garcia        result = self.vm.qmp('object-del', id = 'group1')
803bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'error/class', 'GenericError')
804bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'error/desc', "object 'group1' is in use, can not be deleted")
805bf3e50f6SAlberto Garcia
806bf3e50f6SAlberto Garcia        # Default options, this switches the group back to group0
807bf3e50f6SAlberto Garcia        self.reopen(opts)
808bf3e50f6SAlberto Garcia
809bf3e50f6SAlberto Garcia        # So now we cannot delete group0
810bf3e50f6SAlberto Garcia        result = self.vm.qmp('object-del', id = 'group0')
811bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'error/class', 'GenericError')
812bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'error/desc', "object 'group0' is in use, can not be deleted")
813bf3e50f6SAlberto Garcia
814bf3e50f6SAlberto Garcia        # But group1 is free this time, and it can be deleted
815b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('object-del', id = 'group1')
816bf3e50f6SAlberto Garcia
817bf3e50f6SAlberto Garcia        # Let's delete the filter node
818b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-del', conv_keys = True, node_name = 'throttle0')
819bf3e50f6SAlberto Garcia
820bf3e50f6SAlberto Garcia        # And we can finally get rid of group0
821b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('object-del', id = 'group0')
822bf3e50f6SAlberto Garcia
823bf3e50f6SAlberto Garcia    # If an image has a backing file then the 'backing' option must be
824bf3e50f6SAlberto Garcia    # passed on reopen. We don't allow leaving the option out in this
825bf3e50f6SAlberto Garcia    # case because it's unclear what the correct semantics would be.
826bf3e50f6SAlberto Garcia    def test_missing_backing_options_1(self):
827bf3e50f6SAlberto Garcia        # hd2
828bf3e50f6SAlberto Garcia        opts = hd_opts(2)
829b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-add', conv_keys = False, **opts)
830bf3e50f6SAlberto Garcia
831bf3e50f6SAlberto Garcia        # hd0
832bf3e50f6SAlberto Garcia        opts = hd_opts(0)
833b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-add', conv_keys = False, **opts)
834bf3e50f6SAlberto Garcia
835bf3e50f6SAlberto Garcia        # hd0 has no backing file: we can omit the 'backing' option
836bf3e50f6SAlberto Garcia        self.reopen(opts)
837bf3e50f6SAlberto Garcia
838bf3e50f6SAlberto Garcia        # hd2 <- hd0
839bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing': 'hd2'})
840bf3e50f6SAlberto Garcia
841bf3e50f6SAlberto Garcia        # hd0 has a backing file: we must set the 'backing' option
842bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "backing is missing for 'hd0'")
843bf3e50f6SAlberto Garcia
844bf3e50f6SAlberto Garcia        # hd2 can't be removed because it's the backing file of hd0
845bf3e50f6SAlberto Garcia        result = self.vm.qmp('blockdev-del', conv_keys = True, node_name = 'hd2')
846bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'error/class', 'GenericError')
847bf3e50f6SAlberto Garcia        self.assert_qmp(result, 'error/desc', "Node 'hd2' is busy: node is used as backing hd of 'hd0'")
848bf3e50f6SAlberto Garcia
849bf3e50f6SAlberto Garcia        # Detach hd2 from hd0.
850bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing': None})
8510b877d09SMax Reitz
8520b877d09SMax Reitz        # Without a backing file, we can omit 'backing' again
8530b877d09SMax Reitz        self.reopen(opts)
854bf3e50f6SAlberto Garcia
855bf3e50f6SAlberto Garcia        # Remove both hd0 and hd2
856b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-del', conv_keys = True, node_name = 'hd0')
857bf3e50f6SAlberto Garcia
858b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-del', conv_keys = True, node_name = 'hd2')
859bf3e50f6SAlberto Garcia
860bf3e50f6SAlberto Garcia    # If an image has default backing file (as part of its metadata)
861bf3e50f6SAlberto Garcia    # then the 'backing' option must be passed on reopen. We don't
862bf3e50f6SAlberto Garcia    # allow leaving the option out in this case because it's unclear
863bf3e50f6SAlberto Garcia    # what the correct semantics would be.
864bf3e50f6SAlberto Garcia    def test_missing_backing_options_2(self):
865bf3e50f6SAlberto Garcia        # hd0 <- hd1
866bf3e50f6SAlberto Garcia        # (hd0 is hd1's default backing file)
867bf3e50f6SAlberto Garcia        opts = hd_opts(1)
868b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-add', conv_keys = False, **opts)
869bf3e50f6SAlberto Garcia
870bf3e50f6SAlberto Garcia        # hd1 has a backing file: we can't omit the 'backing' option
871bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "backing is missing for 'hd1'")
872bf3e50f6SAlberto Garcia
873bf3e50f6SAlberto Garcia        # Let's detach the backing file
874bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing': None})
875bf3e50f6SAlberto Garcia
876bf3e50f6SAlberto Garcia        # No backing file attached to hd1 now, but we still can't omit the 'backing' option
877bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "backing is missing for 'hd1'")
878bf3e50f6SAlberto Garcia
879b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-del', conv_keys = True, node_name = 'hd1')
880bf3e50f6SAlberto Garcia
881bf3e50f6SAlberto Garcia    # Test that making 'backing' a reference to an existing child
882bf3e50f6SAlberto Garcia    # keeps its current options
883bf3e50f6SAlberto Garcia    def test_backing_reference(self):
884bf3e50f6SAlberto Garcia        # hd2 <- hd1 <- hd0
885bf3e50f6SAlberto Garcia        opts = hd_opts(0)
886bf3e50f6SAlberto Garcia        opts['backing'] = hd_opts(1)
887bf3e50f6SAlberto Garcia        opts['backing']['backing'] = hd_opts(2)
888bf3e50f6SAlberto Garcia        # Enable 'detect-zeroes' on all three nodes
889bf3e50f6SAlberto Garcia        opts['detect-zeroes'] = 'on'
890bf3e50f6SAlberto Garcia        opts['backing']['detect-zeroes'] = 'on'
891bf3e50f6SAlberto Garcia        opts['backing']['backing']['detect-zeroes'] = 'on'
892b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-add', conv_keys = False, **opts)
893bf3e50f6SAlberto Garcia
894bf3e50f6SAlberto Garcia        # Reopen the chain passing the minimum amount of required options.
895bf3e50f6SAlberto Garcia        # By making 'backing' a reference to hd1 (instead of a sub-dict)
896bf3e50f6SAlberto Garcia        # we tell QEMU to keep its current set of options.
897bf3e50f6SAlberto Garcia        opts = {'driver': iotests.imgfmt,
898bf3e50f6SAlberto Garcia                'node-name': 'hd0',
899bf3e50f6SAlberto Garcia                'file': 'hd0-file',
900bf3e50f6SAlberto Garcia                'backing': 'hd1' }
901bf3e50f6SAlberto Garcia        self.reopen(opts)
902bf3e50f6SAlberto Garcia
903bf3e50f6SAlberto Garcia        # This has reset 'detect-zeroes' on hd0, but not on hd1 and hd2.
904bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd0'), 'detect_zeroes', 'off')
905bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd1'), 'detect_zeroes', 'on')
906bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd2'), 'detect_zeroes', 'on')
907bf3e50f6SAlberto Garcia
908bf3e50f6SAlberto Garcia    # Test what happens if the graph changes due to other operations
909bf3e50f6SAlberto Garcia    # such as block-stream
910bf3e50f6SAlberto Garcia    def test_block_stream_1(self):
911bf3e50f6SAlberto Garcia        # hd1 <- hd0
912bf3e50f6SAlberto Garcia        opts = hd_opts(0)
913bf3e50f6SAlberto Garcia        opts['backing'] = hd_opts(1)
914bf3e50f6SAlberto Garcia        opts['backing']['backing'] = None
915b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-add', conv_keys = False, **opts)
916bf3e50f6SAlberto Garcia
917bf3e50f6SAlberto Garcia        # Stream hd1 into hd0 and wait until it's done
918b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('block-stream', conv_keys = True, job_id = 'stream0', device = 'hd0')
919bf3e50f6SAlberto Garcia        self.wait_until_completed(drive = 'stream0')
920bf3e50f6SAlberto Garcia
921bf3e50f6SAlberto Garcia        # Now we have only hd0
922bf3e50f6SAlberto Garcia        self.assertEqual(self.get_node('hd1'), None)
923bf3e50f6SAlberto Garcia
924bf3e50f6SAlberto Garcia        # We have backing.* options but there's no backing file anymore
925bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "Cannot change the option 'backing.driver'")
926bf3e50f6SAlberto Garcia
927bf3e50f6SAlberto Garcia        # If we remove the 'backing' option then we can reopen hd0 just fine
928bf3e50f6SAlberto Garcia        del opts['backing']
929bf3e50f6SAlberto Garcia        self.reopen(opts)
930bf3e50f6SAlberto Garcia
931bf3e50f6SAlberto Garcia        # We can also reopen hd0 if we set 'backing' to null
932bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing': None})
933bf3e50f6SAlberto Garcia
934b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-del', conv_keys = True, node_name = 'hd0')
935bf3e50f6SAlberto Garcia
936bf3e50f6SAlberto Garcia    # Another block_stream test
937bf3e50f6SAlberto Garcia    def test_block_stream_2(self):
938bf3e50f6SAlberto Garcia        # hd2 <- hd1 <- hd0
939bf3e50f6SAlberto Garcia        opts = hd_opts(0)
940bf3e50f6SAlberto Garcia        opts['backing'] = hd_opts(1)
941bf3e50f6SAlberto Garcia        opts['backing']['backing'] = hd_opts(2)
942b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-add', conv_keys = False, **opts)
943bf3e50f6SAlberto Garcia
944bf3e50f6SAlberto Garcia        # Stream hd1 into hd0 and wait until it's done
945b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('block-stream', conv_keys = True, job_id = 'stream0',
946bf3e50f6SAlberto Garcia                    device = 'hd0', base_node = 'hd2')
947bf3e50f6SAlberto Garcia        self.wait_until_completed(drive = 'stream0')
948bf3e50f6SAlberto Garcia
949bf3e50f6SAlberto Garcia        # The chain is hd2 <- hd0 now. hd1 is missing
950bf3e50f6SAlberto Garcia        self.assertEqual(self.get_node('hd1'), None)
951bf3e50f6SAlberto Garcia
952bf3e50f6SAlberto Garcia        # The backing options in the dict were meant for hd1, but we cannot
953bf3e50f6SAlberto Garcia        # use them with hd2 because hd1 had a backing file while hd2 does not.
954bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "Cannot change the option 'backing.driver'")
955bf3e50f6SAlberto Garcia
956bf3e50f6SAlberto Garcia        # If we remove hd1's options from the dict then things work fine
957bf3e50f6SAlberto Garcia        opts['backing'] = opts['backing']['backing']
958bf3e50f6SAlberto Garcia        self.reopen(opts)
959bf3e50f6SAlberto Garcia
960bf3e50f6SAlberto Garcia        # We can also reopen hd0 if we use a reference to the backing file
961bf3e50f6SAlberto Garcia        self.reopen(opts, {'backing': 'hd2'})
962bf3e50f6SAlberto Garcia
963bf3e50f6SAlberto Garcia        # But we cannot leave the option out
964bf3e50f6SAlberto Garcia        del opts['backing']
965bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "backing is missing for 'hd0'")
966bf3e50f6SAlberto Garcia
967bf3e50f6SAlberto Garcia        # Now we can delete hd0 (and hd2)
968b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-del', conv_keys = True, node_name = 'hd0')
969bf3e50f6SAlberto Garcia        self.assertEqual(self.get_node('hd2'), None)
970bf3e50f6SAlberto Garcia
971bf3e50f6SAlberto Garcia    # Reopen the chain during a block-stream job (from hd1 to hd0)
972bf3e50f6SAlberto Garcia    def test_block_stream_3(self):
973bf3e50f6SAlberto Garcia        # hd2 <- hd1 <- hd0
974bf3e50f6SAlberto Garcia        opts = hd_opts(0)
975bf3e50f6SAlberto Garcia        opts['backing'] = hd_opts(1)
976bf3e50f6SAlberto Garcia        opts['backing']['backing'] = hd_opts(2)
977b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-add', conv_keys = False, **opts)
978bf3e50f6SAlberto Garcia
979bf3e50f6SAlberto Garcia        # hd2 <- hd0
980b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('block-stream', conv_keys = True, job_id = 'stream0',
981c423a6afSMax Reitz                    device = 'hd0', base_node = 'hd2',
982c423a6afSMax Reitz                    auto_finalize = False)
983bf3e50f6SAlberto Garcia
984c624b015SAndrey Shinkevich        # We can remove hd2 while the stream job is ongoing
985bf3e50f6SAlberto Garcia        opts['backing']['backing'] = None
986c624b015SAndrey Shinkevich        self.reopen(opts, {})
987bf3e50f6SAlberto Garcia
988bf3e50f6SAlberto Garcia        # We can't remove hd1 while the stream job is ongoing
989bf3e50f6SAlberto Garcia        opts['backing'] = None
990bfae052aSVladimir Sementsov-Ogievskiy        self.reopen(opts, {}, "Cannot change frozen 'backing' link from 'hd0' to 'hd1'")
991bf3e50f6SAlberto Garcia
992c423a6afSMax Reitz        self.vm.run_job('stream0', auto_finalize = False, auto_dismiss = True)
993bf3e50f6SAlberto Garcia
994bf3e50f6SAlberto Garcia    # Reopen the chain during a block-stream job (from hd2 to hd1)
995bf3e50f6SAlberto Garcia    def test_block_stream_4(self):
996bf3e50f6SAlberto Garcia        # hd2 <- hd1 <- hd0
997bf3e50f6SAlberto Garcia        opts = hd_opts(0)
998bf3e50f6SAlberto Garcia        opts['backing'] = hd_opts(1)
999bf3e50f6SAlberto Garcia        opts['backing']['backing'] = hd_opts(2)
1000b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-add', conv_keys = False, **opts)
1001bf3e50f6SAlberto Garcia
1002bf3e50f6SAlberto Garcia        # hd1 <- hd0
1003b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('block-stream', conv_keys = True, job_id = 'stream0',
1004205736f4SAndrey Shinkevich                    device = 'hd1', filter_node_name='cor',
1005205736f4SAndrey Shinkevich                    auto_finalize = False)
1006bf3e50f6SAlberto Garcia
1007205736f4SAndrey Shinkevich        # We can't reopen with the original options because there is a filter
1008205736f4SAndrey Shinkevich        # inserted by stream job above hd1.
1009c423a6afSMax Reitz        self.reopen(opts, {},
1010205736f4SAndrey Shinkevich                    "Cannot change the option 'backing.backing.file.node-name'")
1011205736f4SAndrey Shinkevich
1012205736f4SAndrey Shinkevich        # We can't reopen hd1 to read-only, as block-stream requires it to be
1013205736f4SAndrey Shinkevich        # read-write
1014205736f4SAndrey Shinkevich        self.reopen(opts['backing'], {'read-only': True},
1015c20555e1SVladimir Sementsov-Ogievskiy                    "Read-only block node 'hd1' cannot support read-write users")
1016bf3e50f6SAlberto Garcia
1017bf3e50f6SAlberto Garcia        # We can't remove hd2 while the stream job is ongoing
1018bf3e50f6SAlberto Garcia        opts['backing']['backing'] = None
1019205736f4SAndrey Shinkevich        self.reopen(opts['backing'], {'read-only': False},
1020bfae052aSVladimir Sementsov-Ogievskiy                    "Cannot change frozen 'backing' link from 'hd1' to 'hd2'")
1021bf3e50f6SAlberto Garcia
1022bf3e50f6SAlberto Garcia        # We can detach hd1 from hd0 because it doesn't affect the stream job
1023bf3e50f6SAlberto Garcia        opts['backing'] = None
1024bf3e50f6SAlberto Garcia        self.reopen(opts)
1025bf3e50f6SAlberto Garcia
1026c423a6afSMax Reitz        self.vm.run_job('stream0', auto_finalize = False, auto_dismiss = True)
1027bf3e50f6SAlberto Garcia
1028bf3e50f6SAlberto Garcia    # Reopen the chain during a block-commit job (from hd0 to hd2)
1029bf3e50f6SAlberto Garcia    def test_block_commit_1(self):
1030bf3e50f6SAlberto Garcia        # hd2 <- hd1 <- hd0
1031bf3e50f6SAlberto Garcia        opts = hd_opts(0)
1032bf3e50f6SAlberto Garcia        opts['backing'] = hd_opts(1)
1033bf3e50f6SAlberto Garcia        opts['backing']['backing'] = hd_opts(2)
1034b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-add', conv_keys = False, **opts)
1035bf3e50f6SAlberto Garcia
1036b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('block-commit', conv_keys = True, job_id = 'commit0',
1037c423a6afSMax Reitz                    device = 'hd0')
1038bf3e50f6SAlberto Garcia
1039bf3e50f6SAlberto Garcia        # We can't remove hd2 while the commit job is ongoing
1040bf3e50f6SAlberto Garcia        opts['backing']['backing'] = None
1041bfae052aSVladimir Sementsov-Ogievskiy        self.reopen(opts, {}, "Cannot change frozen 'backing' link from 'hd1' to 'hd2'")
1042bf3e50f6SAlberto Garcia
1043bf3e50f6SAlberto Garcia        # We can't remove hd1 while the commit job is ongoing
1044bf3e50f6SAlberto Garcia        opts['backing'] = None
1045bfae052aSVladimir Sementsov-Ogievskiy        self.reopen(opts, {}, "Cannot change frozen 'backing' link from 'hd0' to 'hd1'")
1046bf3e50f6SAlberto Garcia
1047bf3e50f6SAlberto Garcia        event = self.vm.event_wait(name='BLOCK_JOB_READY')
1048bf3e50f6SAlberto Garcia        self.assert_qmp(event, 'data/device', 'commit0')
1049bf3e50f6SAlberto Garcia        self.assert_qmp(event, 'data/type', 'commit')
1050bf3e50f6SAlberto Garcia        self.assert_qmp_absent(event, 'data/error')
1051bf3e50f6SAlberto Garcia
1052b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('block-job-complete', device='commit0')
1053bf3e50f6SAlberto Garcia
1054bf3e50f6SAlberto Garcia        self.wait_until_completed(drive = 'commit0')
1055bf3e50f6SAlberto Garcia
1056bf3e50f6SAlberto Garcia    # Reopen the chain during a block-commit job (from hd1 to hd2)
1057bf3e50f6SAlberto Garcia    def test_block_commit_2(self):
1058bf3e50f6SAlberto Garcia        # hd2 <- hd1 <- hd0
1059bf3e50f6SAlberto Garcia        opts = hd_opts(0)
1060bf3e50f6SAlberto Garcia        opts['backing'] = hd_opts(1)
1061bf3e50f6SAlberto Garcia        opts['backing']['backing'] = hd_opts(2)
1062b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-add', conv_keys = False, **opts)
1063bf3e50f6SAlberto Garcia
1064b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('block-commit', conv_keys = True, job_id = 'commit0',
1065c423a6afSMax Reitz                    device = 'hd0', top_node = 'hd1',
1066c423a6afSMax Reitz                    auto_finalize = False)
1067bf3e50f6SAlberto Garcia
1068bf3e50f6SAlberto Garcia        # We can't remove hd2 while the commit job is ongoing
1069bf3e50f6SAlberto Garcia        opts['backing']['backing'] = None
1070bf3e50f6SAlberto Garcia        self.reopen(opts, {}, "Cannot change the option 'backing.driver'")
1071bf3e50f6SAlberto Garcia
1072bf3e50f6SAlberto Garcia        # We can't remove hd1 while the commit job is ongoing
1073bf3e50f6SAlberto Garcia        opts['backing'] = None
1074ecd30d2dSAlberto Garcia        self.reopen(opts, {}, "Cannot replace implicit backing child of hd0")
1075bf3e50f6SAlberto Garcia
1076bf3e50f6SAlberto Garcia        # hd2 <- hd0
1077c423a6afSMax Reitz        self.vm.run_job('commit0', auto_finalize = False, auto_dismiss = True)
1078bf3e50f6SAlberto Garcia
1079bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd0'), 'ro', False)
1080bf3e50f6SAlberto Garcia        self.assertEqual(self.get_node('hd1'), None)
1081bf3e50f6SAlberto Garcia        self.assert_qmp(self.get_node('hd2'), 'ro', True)
1082bf3e50f6SAlberto Garcia
1083ee810602SKevin Wolf    def run_test_iothreads(self, iothread_a, iothread_b, errmsg = None,
1084ee810602SKevin Wolf                           opts_a = None, opts_b = None):
1085ee810602SKevin Wolf        opts = opts_a or hd_opts(0)
1086b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-add', conv_keys = False, **opts)
1087bf3e50f6SAlberto Garcia
1088ee810602SKevin Wolf        opts2 = opts_b or hd_opts(2)
1089b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('blockdev-add', conv_keys = False, **opts2)
1090bf3e50f6SAlberto Garcia
1091b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('object-add', qom_type='iothread', id='iothread0')
1092bf3e50f6SAlberto Garcia
1093b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('object-add', qom_type='iothread', id='iothread1')
1094bf3e50f6SAlberto Garcia
1095b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('device_add', driver='virtio-scsi', id='scsi0',
109697518e11SKevin Wolf                    iothread=iothread_a)
1097bf3e50f6SAlberto Garcia
1098b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('device_add', driver='virtio-scsi', id='scsi1',
109997518e11SKevin Wolf                    iothread=iothread_b)
1100bf3e50f6SAlberto Garcia
110197518e11SKevin Wolf        if iothread_a:
1102b6aed193SVladimir Sementsov-Ogievskiy            self.vm.cmd('device_add', driver='scsi-hd', drive='hd0',
110397518e11SKevin Wolf                        share_rw=True, bus="scsi0.0")
1104bf3e50f6SAlberto Garcia
110597518e11SKevin Wolf        if iothread_b:
1106b6aed193SVladimir Sementsov-Ogievskiy            self.vm.cmd('device_add', driver='scsi-hd', drive='hd2',
110797518e11SKevin Wolf                        share_rw=True, bus="scsi1.0")
110897518e11SKevin Wolf
110997518e11SKevin Wolf        # Attaching the backing file may or may not work
111097518e11SKevin Wolf        self.reopen(opts, {'backing': 'hd2'}, errmsg)
111197518e11SKevin Wolf
111297518e11SKevin Wolf        # But removing the backing file should always work
111397518e11SKevin Wolf        self.reopen(opts, {'backing': None})
111497518e11SKevin Wolf
111597518e11SKevin Wolf        self.vm.shutdown()
111697518e11SKevin Wolf
111797518e11SKevin Wolf    # We don't allow setting a backing file that uses a different AioContext if
111897518e11SKevin Wolf    # neither of them can switch to the other AioContext
111997518e11SKevin Wolf    def test_iothreads_error(self):
112097518e11SKevin Wolf        self.run_test_iothreads('iothread0', 'iothread1',
11211de6b45fSKevin Wolf                                "Cannot change iothread of active block backend")
112297518e11SKevin Wolf
112397518e11SKevin Wolf    def test_iothreads_compatible_users(self):
112497518e11SKevin Wolf        self.run_test_iothreads('iothread0', 'iothread0')
112597518e11SKevin Wolf
112697518e11SKevin Wolf    def test_iothreads_switch_backing(self):
1127e2c8eb14SKevin Wolf        self.run_test_iothreads('iothread0', '')
112897518e11SKevin Wolf
112997518e11SKevin Wolf    def test_iothreads_switch_overlay(self):
1130e2c8eb14SKevin Wolf        self.run_test_iothreads('', 'iothread0')
1131bf3e50f6SAlberto Garcia
1132ee810602SKevin Wolf    def test_iothreads_with_throttling(self):
1133ee810602SKevin Wolf        # Create a throttle-group object
1134ee810602SKevin Wolf        opts = { 'qom-type': 'throttle-group', 'id': 'group0',
1135ee810602SKevin Wolf                 'limits': { 'iops-total': 1000 } }
1136b6aed193SVladimir Sementsov-Ogievskiy        self.vm.cmd('object-add', conv_keys = False, **opts)
1137ee810602SKevin Wolf
1138ee810602SKevin Wolf        # Options with a throttle filter between format and protocol
1139ee810602SKevin Wolf        opts = [
1140ee810602SKevin Wolf            {
1141ee810602SKevin Wolf                'driver': iotests.imgfmt,
1142ee810602SKevin Wolf                'node-name': f'hd{idx}',
1143ee810602SKevin Wolf                'file' : {
1144ee810602SKevin Wolf                    'node-name': f'hd{idx}-throttle',
1145ee810602SKevin Wolf                    'driver': 'throttle',
1146ee810602SKevin Wolf                    'throttle-group': 'group0',
1147ee810602SKevin Wolf                    'file': {
1148ee810602SKevin Wolf                        'driver': 'file',
1149ee810602SKevin Wolf                        'node-name': f'hd{idx}-file',
1150ee810602SKevin Wolf                        'filename': hd_path[idx],
1151ee810602SKevin Wolf                    },
1152ee810602SKevin Wolf                },
1153ee810602SKevin Wolf            }
1154ee810602SKevin Wolf            for idx in (0, 2)
1155ee810602SKevin Wolf        ]
1156ee810602SKevin Wolf
1157ee810602SKevin Wolf        self.run_test_iothreads('iothread0', 'iothread0', None,
1158ee810602SKevin Wolf                                opts[0], opts[1])
1159ee810602SKevin Wolf
1160bf3e50f6SAlberto Garciaif __name__ == '__main__':
116152ea799eSJohn Snow    iotests.activate_logging()
1162103cbc77SMax Reitz    iotests.main(supported_fmts=["qcow2"],
1163103cbc77SMax Reitz                 supported_protocols=["file"])
1164