xref: /qemu/tests/qemu-iotests/155 (revision 9dd003a9)
1903cb1bfSPhilippe Mathieu-Daudé#!/usr/bin/env python3
2*9dd003a9SVladimir Sementsov-Ogievskiy# group: rw
3298c6009SMax Reitz#
4298c6009SMax Reitz# Test whether the backing BDSs are correct after completion of a
5298c6009SMax Reitz# mirror block job; in "existing" modes (drive-mirror with
6298c6009SMax Reitz# mode=existing and blockdev-mirror) the backing chain should not be
7298c6009SMax Reitz# overridden.
8298c6009SMax Reitz#
9298c6009SMax Reitz# Copyright (C) 2016 Red Hat, Inc.
10298c6009SMax Reitz#
11298c6009SMax Reitz# This program is free software; you can redistribute it and/or modify
12298c6009SMax Reitz# it under the terms of the GNU General Public License as published by
13298c6009SMax Reitz# the Free Software Foundation; either version 2 of the License, or
14298c6009SMax Reitz# (at your option) any later version.
15298c6009SMax Reitz#
16298c6009SMax Reitz# This program is distributed in the hope that it will be useful,
17298c6009SMax Reitz# but WITHOUT ANY WARRANTY; without even the implied warranty of
18298c6009SMax Reitz# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19298c6009SMax Reitz# GNU General Public License for more details.
20298c6009SMax Reitz#
21298c6009SMax Reitz# You should have received a copy of the GNU General Public License
22298c6009SMax Reitz# along with this program.  If not, see <http://www.gnu.org/licenses/>.
23298c6009SMax Reitz#
24298c6009SMax Reitz
25298c6009SMax Reitzimport os
26298c6009SMax Reitzimport iotests
27298c6009SMax Reitzfrom iotests import qemu_img
28298c6009SMax Reitz
29298c6009SMax Reitzback0_img = os.path.join(iotests.test_dir, 'back0.' + iotests.imgfmt)
30298c6009SMax Reitzback1_img = os.path.join(iotests.test_dir, 'back1.' + iotests.imgfmt)
31298c6009SMax Reitzback2_img = os.path.join(iotests.test_dir, 'back2.' + iotests.imgfmt)
32298c6009SMax Reitzsource_img = os.path.join(iotests.test_dir, 'source.' + iotests.imgfmt)
33298c6009SMax Reitztarget_img = os.path.join(iotests.test_dir, 'target.' + iotests.imgfmt)
34298c6009SMax Reitz
35298c6009SMax Reitz
36298c6009SMax Reitz# Class variables for controlling its behavior:
37298c6009SMax Reitz#
38298c6009SMax Reitz# existing: If True, explicitly create the target image and blockdev-add it
39298c6009SMax Reitz# target_backing: If existing is True: Use this filename as the backing file
40298c6009SMax Reitz#                 of the target image
41298c6009SMax Reitz#                 (None: no backing file)
42298c6009SMax Reitz# target_blockdev_backing: If existing is True: Pass this dict as "backing"
43298c6009SMax Reitz#                          for the blockdev-add command
44298c6009SMax Reitz#                          (None: do not pass "backing")
45298c6009SMax Reitz# target_real_backing: If existing is True: The real filename of the backing
46298c6009SMax Reitz#                      image during runtime, only makes sense if
47298c6009SMax Reitz#                      target_blockdev_backing is not None
48298c6009SMax Reitz#                      (None: same as target_backing)
498bdee9f1SKevin Wolf# target_open_with_backing: If True, the target image is added with its backing
508bdee9f1SKevin Wolf#                           chain opened right away. If False, blockdev-add
518bdee9f1SKevin Wolf#                           opens it without a backing file and job completion
528bdee9f1SKevin Wolf#                           is supposed to open the backing chain.
536a5f6403SKevin Wolf# use_iothread: If True, an iothread is configured for the virtio-blk device
546a5f6403SKevin Wolf#               that uses the image being mirrored
55298c6009SMax Reitz
56298c6009SMax Reitzclass BaseClass(iotests.QMPTestCase):
57298c6009SMax Reitz    target_blockdev_backing = None
58298c6009SMax Reitz    target_real_backing = None
598bdee9f1SKevin Wolf    target_open_with_backing = True
606a5f6403SKevin Wolf    use_iothread = False
61298c6009SMax Reitz
62298c6009SMax Reitz    def setUp(self):
631d701e0eSMax Reitz        qemu_img('create', '-f', iotests.imgfmt, back0_img, '1440K')
64b66ff2c2SEric Blake        qemu_img('create', '-f', iotests.imgfmt, '-b', back0_img,
65b66ff2c2SEric Blake                 '-F', iotests.imgfmt, back1_img)
66b66ff2c2SEric Blake        qemu_img('create', '-f', iotests.imgfmt, '-b', back1_img,
67b66ff2c2SEric Blake                 '-F', iotests.imgfmt, back2_img)
68b66ff2c2SEric Blake        qemu_img('create', '-f', iotests.imgfmt, '-b', back2_img,
69b66ff2c2SEric Blake                 '-F', iotests.imgfmt, source_img)
70298c6009SMax Reitz
71298c6009SMax Reitz        self.vm = iotests.VM()
72298c6009SMax Reitz        # Add the BDS via blockdev-add so it stays around after the mirror block
73298c6009SMax Reitz        # job has been completed
741d701e0eSMax Reitz        blockdev = {'node-name': 'source',
751d701e0eSMax Reitz                    'driver': iotests.imgfmt,
761d701e0eSMax Reitz                    'file': {'driver': 'file',
771d701e0eSMax Reitz                             'filename': source_img}}
7862a94288SKevin Wolf        self.vm.add_blockdev(self.vm.qmp_to_opts(blockdev))
796a5f6403SKevin Wolf
806a5f6403SKevin Wolf        if self.use_iothread:
816a5f6403SKevin Wolf            self.vm.add_object('iothread,id=iothread0')
826a5f6403SKevin Wolf            iothread = ",iothread=iothread0"
836a5f6403SKevin Wolf        else:
846a5f6403SKevin Wolf            iothread = ""
856a5f6403SKevin Wolf
866a5f6403SKevin Wolf        self.vm.add_device('virtio-scsi%s' % iothread)
876a5f6403SKevin Wolf        self.vm.add_device('scsi-hd,id=qdev0,drive=source')
886a5f6403SKevin Wolf
891d701e0eSMax Reitz        self.vm.launch()
90298c6009SMax Reitz
91298c6009SMax Reitz        self.assertIntactSourceBackingChain()
92298c6009SMax Reitz
93298c6009SMax Reitz        if self.existing:
94298c6009SMax Reitz            if self.target_backing:
95298c6009SMax Reitz                qemu_img('create', '-f', iotests.imgfmt,
96b66ff2c2SEric Blake                         '-b', self.target_backing, '-F', 'raw',
97b66ff2c2SEric Blake                         target_img, '1440K')
98298c6009SMax Reitz            else:
991d701e0eSMax Reitz                qemu_img('create', '-f', iotests.imgfmt, target_img, '1440K')
100298c6009SMax Reitz
101298c6009SMax Reitz            if self.cmd == 'blockdev-mirror':
102298c6009SMax Reitz                options = { 'node-name': 'target',
103298c6009SMax Reitz                            'driver': iotests.imgfmt,
104298c6009SMax Reitz                            'file': { 'driver': 'file',
1058bdee9f1SKevin Wolf                                      'node-name': 'target-file',
106298c6009SMax Reitz                                      'filename': target_img } }
1078bdee9f1SKevin Wolf
1088bdee9f1SKevin Wolf                if not self.target_open_with_backing:
1098bdee9f1SKevin Wolf                        options['backing'] = None
1108bdee9f1SKevin Wolf                elif self.target_blockdev_backing:
111298c6009SMax Reitz                        options['backing'] = self.target_blockdev_backing
112298c6009SMax Reitz
1130153d2f5SKevin Wolf                result = self.vm.qmp('blockdev-add', **options)
114298c6009SMax Reitz                self.assert_qmp(result, 'return', {})
115298c6009SMax Reitz
116298c6009SMax Reitz    def tearDown(self):
117298c6009SMax Reitz        self.vm.shutdown()
118298c6009SMax Reitz        os.remove(source_img)
119298c6009SMax Reitz        os.remove(back2_img)
120298c6009SMax Reitz        os.remove(back1_img)
121298c6009SMax Reitz        os.remove(back0_img)
122298c6009SMax Reitz        try:
123298c6009SMax Reitz            os.remove(target_img)
124298c6009SMax Reitz        except OSError:
125298c6009SMax Reitz            pass
126298c6009SMax Reitz
1271d701e0eSMax Reitz    def findBlockNode(self, node_name, qdev=None):
1281d701e0eSMax Reitz        if qdev:
129298c6009SMax Reitz            result = self.vm.qmp('query-block')
130298c6009SMax Reitz            for device in result['return']:
1311d701e0eSMax Reitz                if device['qdev'] == qdev:
132298c6009SMax Reitz                    if node_name:
133298c6009SMax Reitz                        self.assert_qmp(device, 'inserted/node-name', node_name)
134298c6009SMax Reitz                    return device['inserted']
135298c6009SMax Reitz        else:
136298c6009SMax Reitz            result = self.vm.qmp('query-named-block-nodes')
137298c6009SMax Reitz            for node in result['return']:
138298c6009SMax Reitz                if node['node-name'] == node_name:
139298c6009SMax Reitz                    return node
140298c6009SMax Reitz
1411d701e0eSMax Reitz        self.fail('Cannot find node %s/%s' % (qdev, node_name))
142298c6009SMax Reitz
143298c6009SMax Reitz    def assertIntactSourceBackingChain(self):
144298c6009SMax Reitz        node = self.findBlockNode('source')
145298c6009SMax Reitz
146298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 0 + '/filename',
147298c6009SMax Reitz                        source_img)
148298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 1 + '/filename',
149298c6009SMax Reitz                        back2_img)
150298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 2 + '/filename',
151298c6009SMax Reitz                        back1_img)
152298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 3 + '/filename',
153298c6009SMax Reitz                        back0_img)
154298c6009SMax Reitz        self.assert_qmp_absent(node, 'image' + '/backing-image' * 4)
155298c6009SMax Reitz
156298c6009SMax Reitz    def assertCorrectBackingImage(self, node, default_image):
157298c6009SMax Reitz        if self.existing:
158298c6009SMax Reitz            if self.target_real_backing:
159298c6009SMax Reitz                image = self.target_real_backing
160298c6009SMax Reitz            else:
161298c6009SMax Reitz                image = self.target_backing
162298c6009SMax Reitz        else:
163298c6009SMax Reitz            image = default_image
164298c6009SMax Reitz
165298c6009SMax Reitz        if image:
166298c6009SMax Reitz            self.assert_qmp(node, 'image/backing-image/filename', image)
167298c6009SMax Reitz        else:
168298c6009SMax Reitz            self.assert_qmp_absent(node, 'image/backing-image')
169298c6009SMax Reitz
170298c6009SMax Reitz
171298c6009SMax Reitz# Class variables for controlling its behavior:
172298c6009SMax Reitz#
173298c6009SMax Reitz# cmd: Mirroring command to execute, either drive-mirror or blockdev-mirror
174298c6009SMax Reitz
175298c6009SMax Reitzclass MirrorBaseClass(BaseClass):
1768bdee9f1SKevin Wolf    def openBacking(self):
1778bdee9f1SKevin Wolf        pass
1788bdee9f1SKevin Wolf
179298c6009SMax Reitz    def runMirror(self, sync):
180298c6009SMax Reitz        if self.cmd == 'blockdev-mirror':
1811d701e0eSMax Reitz            result = self.vm.qmp(self.cmd, job_id='mirror-job', device='source',
1828bdee9f1SKevin Wolf                                 sync=sync, target='target',
1838bdee9f1SKevin Wolf                                 auto_finalize=False)
184298c6009SMax Reitz        else:
185298c6009SMax Reitz            if self.existing:
186298c6009SMax Reitz                mode = 'existing'
187298c6009SMax Reitz            else:
188298c6009SMax Reitz                mode = 'absolute-paths'
1891d701e0eSMax Reitz            result = self.vm.qmp(self.cmd, job_id='mirror-job', device='source',
1901d701e0eSMax Reitz                                 sync=sync, target=target_img,
1911d701e0eSMax Reitz                                 format=iotests.imgfmt, mode=mode,
1928bdee9f1SKevin Wolf                                 node_name='target', auto_finalize=False)
193298c6009SMax Reitz
194298c6009SMax Reitz        self.assert_qmp(result, 'return', {})
195298c6009SMax Reitz
19652ea799eSJohn Snow        self.vm.run_job('mirror-job', auto_finalize=False,
1978bdee9f1SKevin Wolf                        pre_finalize=self.openBacking, auto_dismiss=True)
198298c6009SMax Reitz
199298c6009SMax Reitz    def testFull(self):
200298c6009SMax Reitz        self.runMirror('full')
201298c6009SMax Reitz
2026a5f6403SKevin Wolf        node = self.findBlockNode('target', 'qdev0')
203298c6009SMax Reitz        self.assertCorrectBackingImage(node, None)
204298c6009SMax Reitz        self.assertIntactSourceBackingChain()
205298c6009SMax Reitz
206298c6009SMax Reitz    def testTop(self):
207298c6009SMax Reitz        self.runMirror('top')
208298c6009SMax Reitz
2096a5f6403SKevin Wolf        node = self.findBlockNode('target', 'qdev0')
210298c6009SMax Reitz        self.assertCorrectBackingImage(node, back2_img)
211298c6009SMax Reitz        self.assertIntactSourceBackingChain()
212298c6009SMax Reitz
213298c6009SMax Reitz    def testNone(self):
214298c6009SMax Reitz        self.runMirror('none')
215298c6009SMax Reitz
2166a5f6403SKevin Wolf        node = self.findBlockNode('target', 'qdev0')
217298c6009SMax Reitz        self.assertCorrectBackingImage(node, source_img)
218298c6009SMax Reitz        self.assertIntactSourceBackingChain()
219298c6009SMax Reitz
220298c6009SMax Reitz
221298c6009SMax Reitzclass TestDriveMirrorAbsolutePaths(MirrorBaseClass):
222298c6009SMax Reitz    cmd = 'drive-mirror'
223298c6009SMax Reitz    existing = False
224298c6009SMax Reitz
225298c6009SMax Reitzclass TestDriveMirrorExistingNoBacking(MirrorBaseClass):
226298c6009SMax Reitz    cmd = 'drive-mirror'
227298c6009SMax Reitz    existing = True
228298c6009SMax Reitz    target_backing = None
229298c6009SMax Reitz
230298c6009SMax Reitzclass TestDriveMirrorExistingBacking(MirrorBaseClass):
231298c6009SMax Reitz    cmd = 'drive-mirror'
232298c6009SMax Reitz    existing = True
233298c6009SMax Reitz    target_backing = 'null-co://'
234298c6009SMax Reitz
235298c6009SMax Reitzclass TestBlockdevMirrorNoBacking(MirrorBaseClass):
236298c6009SMax Reitz    cmd = 'blockdev-mirror'
237298c6009SMax Reitz    existing = True
238298c6009SMax Reitz    target_backing = None
239298c6009SMax Reitz
240298c6009SMax Reitzclass TestBlockdevMirrorBacking(MirrorBaseClass):
241298c6009SMax Reitz    cmd = 'blockdev-mirror'
242298c6009SMax Reitz    existing = True
243298c6009SMax Reitz    target_backing = 'null-co://'
244298c6009SMax Reitz
245298c6009SMax Reitzclass TestBlockdevMirrorForcedBacking(MirrorBaseClass):
246298c6009SMax Reitz    cmd = 'blockdev-mirror'
247298c6009SMax Reitz    existing = True
248298c6009SMax Reitz    target_backing = None
249298c6009SMax Reitz    target_blockdev_backing = { 'driver': 'null-co' }
250298c6009SMax Reitz    target_real_backing = 'null-co://'
251298c6009SMax Reitz
2528bdee9f1SKevin Wolf# Attach the backing chain only during completion, with blockdev-reopen
2538bdee9f1SKevin Wolfclass TestBlockdevMirrorReopen(MirrorBaseClass):
2548bdee9f1SKevin Wolf    cmd = 'blockdev-mirror'
2558bdee9f1SKevin Wolf    existing = True
2568bdee9f1SKevin Wolf    target_backing = 'null-co://'
2578bdee9f1SKevin Wolf    target_open_with_backing = False
2588bdee9f1SKevin Wolf
2598bdee9f1SKevin Wolf    def openBacking(self):
2608bdee9f1SKevin Wolf        if not self.target_open_with_backing:
2618bdee9f1SKevin Wolf            result = self.vm.qmp('blockdev-add', node_name="backing",
2628bdee9f1SKevin Wolf                                 driver="null-co")
2638bdee9f1SKevin Wolf            self.assert_qmp(result, 'return', {})
2648bdee9f1SKevin Wolf            result = self.vm.qmp('x-blockdev-reopen', node_name="target",
2658bdee9f1SKevin Wolf                                 driver=iotests.imgfmt, file="target-file",
2668bdee9f1SKevin Wolf                                 backing="backing")
2678bdee9f1SKevin Wolf            self.assert_qmp(result, 'return', {})
2688bdee9f1SKevin Wolf
2696a5f6403SKevin Wolfclass TestBlockdevMirrorReopenIothread(TestBlockdevMirrorReopen):
2706a5f6403SKevin Wolf    use_iothread = True
2716a5f6403SKevin Wolf
2728bdee9f1SKevin Wolf# Attach the backing chain only during completion, with blockdev-snapshot
2738bdee9f1SKevin Wolfclass TestBlockdevMirrorSnapshot(MirrorBaseClass):
2748bdee9f1SKevin Wolf    cmd = 'blockdev-mirror'
2758bdee9f1SKevin Wolf    existing = True
2768bdee9f1SKevin Wolf    target_backing = 'null-co://'
2778bdee9f1SKevin Wolf    target_open_with_backing = False
2788bdee9f1SKevin Wolf
2798bdee9f1SKevin Wolf    def openBacking(self):
2808bdee9f1SKevin Wolf        if not self.target_open_with_backing:
2818bdee9f1SKevin Wolf            result = self.vm.qmp('blockdev-add', node_name="backing",
2828bdee9f1SKevin Wolf                                 driver="null-co")
2838bdee9f1SKevin Wolf            self.assert_qmp(result, 'return', {})
2848bdee9f1SKevin Wolf            result = self.vm.qmp('blockdev-snapshot', node="backing",
2858bdee9f1SKevin Wolf                                 overlay="target")
2868bdee9f1SKevin Wolf            self.assert_qmp(result, 'return', {})
287298c6009SMax Reitz
2886a5f6403SKevin Wolfclass TestBlockdevMirrorSnapshotIothread(TestBlockdevMirrorSnapshot):
2896a5f6403SKevin Wolf    use_iothread = True
2906a5f6403SKevin Wolf
291298c6009SMax Reitzclass TestCommit(BaseClass):
292298c6009SMax Reitz    existing = False
293298c6009SMax Reitz
294298c6009SMax Reitz    def testCommit(self):
2951d701e0eSMax Reitz        result = self.vm.qmp('block-commit', job_id='commit-job',
2961d701e0eSMax Reitz                             device='source', base=back1_img)
297298c6009SMax Reitz        self.assert_qmp(result, 'return', {})
298298c6009SMax Reitz
299298c6009SMax Reitz        self.vm.event_wait('BLOCK_JOB_READY')
300298c6009SMax Reitz
3011d701e0eSMax Reitz        result = self.vm.qmp('block-job-complete', device='commit-job')
302298c6009SMax Reitz        self.assert_qmp(result, 'return', {})
303298c6009SMax Reitz
304298c6009SMax Reitz        self.vm.event_wait('BLOCK_JOB_COMPLETED')
305298c6009SMax Reitz
3066a5f6403SKevin Wolf        node = self.findBlockNode(None, 'qdev0')
307298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 0 + '/filename',
308298c6009SMax Reitz                        back1_img)
309298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 1 + '/filename',
310298c6009SMax Reitz                        back0_img)
311298c6009SMax Reitz        self.assert_qmp_absent(node, 'image' + '/backing-image' * 2 +
312298c6009SMax Reitz                               '/filename')
313298c6009SMax Reitz
314298c6009SMax Reitz        self.assertIntactSourceBackingChain()
315298c6009SMax Reitz
316298c6009SMax Reitz
317298c6009SMax ReitzBaseClass = None
318298c6009SMax ReitzMirrorBaseClass = None
319298c6009SMax Reitz
320298c6009SMax Reitzif __name__ == '__main__':
321103cbc77SMax Reitz    iotests.main(supported_fmts=['qcow2'],
322103cbc77SMax Reitz                 supported_protocols=['file'])
323