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 self.vm.cmd('blockdev-add', options)
117 os.remove(source_img)
122 os.remove(target_img)
126 def findBlockNode(self, node_name, qdev=None):
128 result = self.vm.qmp('query-block')
129 for device in result['return']:
130 if device['qdev'] == qdev:
132 self.assert_qmp(device, 'inserted/node-name', node_name)
133 return device['inserted']
135 result = self.vm.qmp('query-named-block-nodes')
136 for node in result['return']:
137 if node['node-name'] == node_name:
140 self.fail('Cannot find node %s/%s' % (qdev, node_name))
142 def assertIntactSourceBackingChain(self):
143 node = self.findBlockNode('source')
145 self.assert_qmp(node, 'image' + '/backing-image' * 0 + '/filename',
147 self.assert_qmp(node, 'image' + '/backing-image' * 1 + '/filename',
149 self.assert_qmp(node, 'image' + '/backing-image' * 2 + '/filename',
151 self.assert_qmp(node, 'image' + '/backing-image' * 3 + '/filename',
153 self.assert_qmp_absent(node, 'image' + '/backing-image' * 4)
155 def assertCorrectBackingImage(self, node, default_image):
157 if self.target_real_backing:
158 image = self.target_real_backing
160 image = self.target_backing
162 image = default_image
165 self.assert_qmp(node, 'image/backing-image/filename', image)
167 self.assert_qmp_absent(node, 'image/backing-image')
170 # Class variables for controlling its behavior:
172 # cmd: Mirroring command to execute, either drive-mirror or blockdev-mirror
174 class MirrorBaseClass(BaseClass):
175 def openBacking(self):
178 def runMirror(self, sync):
179 if self.cmd == 'blockdev-mirror':
180 self.vm.cmd(self.cmd, job_id='mirror-job', device='source',
181 sync=sync, target='target',
187 mode = 'absolute-paths'
188 self.vm.cmd(self.cmd, job_id='mirror-job', device='source',
189 sync=sync, target=target_img,
190 format=iotests.imgfmt, mode=mode,
191 node_name='target', auto_finalize=False)
193 self.vm.run_job('mirror-job', auto_finalize=False,
194 pre_finalize=self.openBacking, auto_dismiss=True)
197 self.runMirror('full')
199 node = self.findBlockNode('target', 'qdev0')
200 self.assertCorrectBackingImage(node, None)
201 self.assertIntactSourceBackingChain()
204 self.runMirror('top')
206 node = self.findBlockNode('target', 'qdev0')
207 self.assertCorrectBackingImage(node, back2_img)
208 self.assertIntactSourceBackingChain()
211 self.runMirror('none')
213 node = self.findBlockNode('target', 'qdev0')
214 self.assertCorrectBackingImage(node, source_img)
215 self.assertIntactSourceBackingChain()
218 class TestDriveMirrorAbsolutePaths(MirrorBaseClass):
222 class TestDriveMirrorExistingNoBacking(MirrorBaseClass):
225 target_backing = None
227 class TestDriveMirrorExistingBacking(MirrorBaseClass):
230 target_backing = 'null-co://'
232 class TestBlockdevMirrorNoBacking(MirrorBaseClass):
233 cmd = 'blockdev-mirror'
235 target_backing = None
237 class TestBlockdevMirrorBacking(MirrorBaseClass):
238 cmd = 'blockdev-mirror'
240 target_backing = 'null-co://'
242 class TestBlockdevMirrorForcedBacking(MirrorBaseClass):
243 cmd = 'blockdev-mirror'
245 target_backing = None
246 target_blockdev_backing = { 'driver': 'null-co' }
247 target_real_backing = 'null-co://'
249 # Attach the backing chain only during completion, with blockdev-reopen
250 class TestBlockdevMirrorReopen(MirrorBaseClass):
251 cmd = 'blockdev-mirror'
253 target_backing = 'null-co://'
254 target_open_with_backing = False
256 def openBacking(self):
257 if not self.target_open_with_backing:
258 self.vm.cmd('blockdev-add', node_name="backing",
260 self.vm.cmd('blockdev-reopen', options=[{
261 'node-name': "target",
262 'driver': iotests.imgfmt,
263 'file': "target-file",
267 class TestBlockdevMirrorReopenIothread(TestBlockdevMirrorReopen):
270 # Attach the backing chain only during completion, with blockdev-snapshot
271 class TestBlockdevMirrorSnapshot(MirrorBaseClass):
272 cmd = 'blockdev-mirror'
274 target_backing = 'null-co://'
275 target_open_with_backing = False
277 def openBacking(self):
278 if not self.target_open_with_backing:
279 self.vm.cmd('blockdev-add', node_name="backing",
281 self.vm.cmd('blockdev-snapshot', node="backing",
284 class TestBlockdevMirrorSnapshotIothread(TestBlockdevMirrorSnapshot):
287 class TestCommit(BaseClass):
290 def testCommit(self):
291 self.vm.cmd('block-commit', job_id='commit-job',
292 device='source', base=back1_img)
294 self.vm.event_wait('BLOCK_JOB_READY')
296 self.vm.cmd('block-job-complete', device='commit-job')
298 self.vm.event_wait('BLOCK_JOB_COMPLETED')
300 node = self.findBlockNode(None, 'qdev0')
301 self.assert_qmp(node, 'image' + '/backing-image' * 0 + '/filename',
303 self.assert_qmp(node, 'image' + '/backing-image' * 1 + '/filename',
305 self.assert_qmp_absent(node, 'image' + '/backing-image' * 2 +
308 self.assertIntactSourceBackingChain()
312 MirrorBaseClass = None
314 if __name__ == '__main__':
315 iotests.main(supported_fmts=['qcow2'],
316 supported_protocols=['file'])