4 # Test whether the backing BDSs are correct after completion of a
5 # mirror block job; in "existing" modes (drive-mirror with
6 # mode=existing and blockdev-mirror) the backing chain should not be
9 # Copyright (C) 2016 Red Hat, Inc.
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with this program. If not, see <http://www.gnu.org/licenses/>.
27 from iotests import qemu_img
29 back0_img = os.path.join(iotests.test_dir, 'back0.' + iotests.imgfmt)
30 back1_img = os.path.join(iotests.test_dir, 'back1.' + iotests.imgfmt)
31 back2_img = os.path.join(iotests.test_dir, 'back2.' + iotests.imgfmt)
32 source_img = os.path.join(iotests.test_dir, 'source.' + iotests.imgfmt)
33 target_img = os.path.join(iotests.test_dir, 'target.' + iotests.imgfmt)
36 # Class variables for controlling its behavior:
38 # existing: If True, explicitly create the target image and blockdev-add it
39 # target_backing: If existing is True: Use this filename as the backing file
41 # (None: no backing file)
42 # target_blockdev_backing: If existing is True: Pass this dict as "backing"
43 # for the blockdev-add command
44 # (None: do not pass "backing")
45 # target_real_backing: If existing is True: The real filename of the backing
46 # image during runtime, only makes sense if
47 # target_blockdev_backing is not None
48 # (None: same as target_backing)
49 # target_open_with_backing: If True, the target image is added with its backing
50 # chain opened right away. If False, blockdev-add
51 # opens it without a backing file and job completion
52 # is supposed to open the backing chain.
53 # use_iothread: If True, an iothread is configured for the virtio-blk device
54 # that uses the image being mirrored
56 class BaseClass(iotests.QMPTestCase):
57 target_blockdev_backing = None
58 target_real_backing = None
59 target_open_with_backing = True
63 qemu_img('create', '-f', iotests.imgfmt, back0_img, '1440K')
64 qemu_img('create', '-f', iotests.imgfmt, '-b', back0_img,
65 '-F', iotests.imgfmt, back1_img)
66 qemu_img('create', '-f', iotests.imgfmt, '-b', back1_img,
67 '-F', iotests.imgfmt, back2_img)
68 qemu_img('create', '-f', iotests.imgfmt, '-b', back2_img,
69 '-F', iotests.imgfmt, source_img)
71 self.vm = iotests.VM()
72 # Add the BDS via blockdev-add so it stays around after the mirror block
73 # job has been completed
74 blockdev = {'node-name': 'source',
75 'driver': iotests.imgfmt,
76 'file': {'driver': 'file',
77 'filename': source_img}}
78 self.vm.add_blockdev(self.vm.qmp_to_opts(blockdev))
81 self.vm.add_object('iothread,id=iothread0')
82 iothread = ",iothread=iothread0"
86 self.vm.add_device('virtio-scsi%s' % iothread)
87 self.vm.add_device('scsi-hd,id=qdev0,drive=source')
91 self.assertIntactSourceBackingChain()
94 if self.target_backing:
95 qemu_img('create', '-f', iotests.imgfmt,
96 '-b', self.target_backing, '-F', 'raw',
99 qemu_img('create', '-f', iotests.imgfmt, target_img, '1440K')
101 if self.cmd == 'blockdev-mirror':
102 options = { 'node-name': 'target',
103 'driver': iotests.imgfmt,
104 'file': { 'driver': 'file',
105 'node-name': 'target-file',
106 'filename': target_img } }
108 if not self.target_open_with_backing:
109 options['backing'] = None
110 elif self.target_blockdev_backing:
111 options['backing'] = self.target_blockdev_backing
113 result = self.vm.qmp('blockdev-add', **options)
114 self.assert_qmp(result, 'return', {})
118 os.remove(source_img)
123 os.remove(target_img)
127 def findBlockNode(self, node_name, qdev=None):
129 result = self.vm.qmp('query-block')
130 for device in result['return']:
131 if device['qdev'] == qdev:
133 self.assert_qmp(device, 'inserted/node-name', node_name)
134 return device['inserted']
136 result = self.vm.qmp('query-named-block-nodes')
137 for node in result['return']:
138 if node['node-name'] == node_name:
141 self.fail('Cannot find node %s/%s' % (qdev, node_name))
143 def assertIntactSourceBackingChain(self):
144 node = self.findBlockNode('source')
146 self.assert_qmp(node, 'image' + '/backing-image' * 0 + '/filename',
148 self.assert_qmp(node, 'image' + '/backing-image' * 1 + '/filename',
150 self.assert_qmp(node, 'image' + '/backing-image' * 2 + '/filename',
152 self.assert_qmp(node, 'image' + '/backing-image' * 3 + '/filename',
154 self.assert_qmp_absent(node, 'image' + '/backing-image' * 4)
156 def assertCorrectBackingImage(self, node, default_image):
158 if self.target_real_backing:
159 image = self.target_real_backing
161 image = self.target_backing
163 image = default_image
166 self.assert_qmp(node, 'image/backing-image/filename', image)
168 self.assert_qmp_absent(node, 'image/backing-image')
171 # Class variables for controlling its behavior:
173 # cmd: Mirroring command to execute, either drive-mirror or blockdev-mirror
175 class MirrorBaseClass(BaseClass):
176 def openBacking(self):
179 def runMirror(self, sync):
180 if self.cmd == 'blockdev-mirror':
181 result = self.vm.qmp(self.cmd, job_id='mirror-job', device='source',
182 sync=sync, target='target',
188 mode = 'absolute-paths'
189 result = self.vm.qmp(self.cmd, job_id='mirror-job', device='source',
190 sync=sync, target=target_img,
191 format=iotests.imgfmt, mode=mode,
192 node_name='target', auto_finalize=False)
194 self.assert_qmp(result, 'return', {})
196 self.vm.run_job('mirror-job', auto_finalize=False,
197 pre_finalize=self.openBacking, auto_dismiss=True)
200 self.runMirror('full')
202 node = self.findBlockNode('target', 'qdev0')
203 self.assertCorrectBackingImage(node, None)
204 self.assertIntactSourceBackingChain()
207 self.runMirror('top')
209 node = self.findBlockNode('target', 'qdev0')
210 self.assertCorrectBackingImage(node, back2_img)
211 self.assertIntactSourceBackingChain()
214 self.runMirror('none')
216 node = self.findBlockNode('target', 'qdev0')
217 self.assertCorrectBackingImage(node, source_img)
218 self.assertIntactSourceBackingChain()
221 class TestDriveMirrorAbsolutePaths(MirrorBaseClass):
225 class TestDriveMirrorExistingNoBacking(MirrorBaseClass):
228 target_backing = None
230 class TestDriveMirrorExistingBacking(MirrorBaseClass):
233 target_backing = 'null-co://'
235 class TestBlockdevMirrorNoBacking(MirrorBaseClass):
236 cmd = 'blockdev-mirror'
238 target_backing = None
240 class TestBlockdevMirrorBacking(MirrorBaseClass):
241 cmd = 'blockdev-mirror'
243 target_backing = 'null-co://'
245 class TestBlockdevMirrorForcedBacking(MirrorBaseClass):
246 cmd = 'blockdev-mirror'
248 target_backing = None
249 target_blockdev_backing = { 'driver': 'null-co' }
250 target_real_backing = 'null-co://'
252 # Attach the backing chain only during completion, with blockdev-reopen
253 class TestBlockdevMirrorReopen(MirrorBaseClass):
254 cmd = 'blockdev-mirror'
256 target_backing = 'null-co://'
257 target_open_with_backing = False
259 def openBacking(self):
260 if not self.target_open_with_backing:
261 result = self.vm.qmp('blockdev-add', node_name="backing",
263 self.assert_qmp(result, 'return', {})
264 result = self.vm.qmp('x-blockdev-reopen', node_name="target",
265 driver=iotests.imgfmt, file="target-file",
267 self.assert_qmp(result, 'return', {})
269 class TestBlockdevMirrorReopenIothread(TestBlockdevMirrorReopen):
272 # Attach the backing chain only during completion, with blockdev-snapshot
273 class TestBlockdevMirrorSnapshot(MirrorBaseClass):
274 cmd = 'blockdev-mirror'
276 target_backing = 'null-co://'
277 target_open_with_backing = False
279 def openBacking(self):
280 if not self.target_open_with_backing:
281 result = self.vm.qmp('blockdev-add', node_name="backing",
283 self.assert_qmp(result, 'return', {})
284 result = self.vm.qmp('blockdev-snapshot', node="backing",
286 self.assert_qmp(result, 'return', {})
288 class TestBlockdevMirrorSnapshotIothread(TestBlockdevMirrorSnapshot):
291 class TestCommit(BaseClass):
294 def testCommit(self):
295 result = self.vm.qmp('block-commit', job_id='commit-job',
296 device='source', base=back1_img)
297 self.assert_qmp(result, 'return', {})
299 self.vm.event_wait('BLOCK_JOB_READY')
301 result = self.vm.qmp('block-job-complete', device='commit-job')
302 self.assert_qmp(result, 'return', {})
304 self.vm.event_wait('BLOCK_JOB_COMPLETED')
306 node = self.findBlockNode(None, 'qdev0')
307 self.assert_qmp(node, 'image' + '/backing-image' * 0 + '/filename',
309 self.assert_qmp(node, 'image' + '/backing-image' * 1 + '/filename',
311 self.assert_qmp_absent(node, 'image' + '/backing-image' * 2 +
314 self.assertIntactSourceBackingChain()
318 MirrorBaseClass = None
320 if __name__ == '__main__':
321 iotests.main(supported_fmts=['qcow2'],
322 supported_protocols=['file'])