3 # Test whether the backing BDSs are correct after completion of a
4 # mirror block job; in "existing" modes (drive-mirror with
5 # mode=existing and blockdev-mirror) the backing chain should not be
8 # Copyright (C) 2016 Red Hat, Inc.
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program. If not, see <http://www.gnu.org/licenses/>.
26 from iotests import qemu_img
28 back0_img = os.path.join(iotests.test_dir, 'back0.' + iotests.imgfmt)
29 back1_img = os.path.join(iotests.test_dir, 'back1.' + iotests.imgfmt)
30 back2_img = os.path.join(iotests.test_dir, 'back2.' + iotests.imgfmt)
31 source_img = os.path.join(iotests.test_dir, 'source.' + iotests.imgfmt)
32 target_img = os.path.join(iotests.test_dir, 'target.' + iotests.imgfmt)
35 # Class variables for controlling its behavior:
37 # existing: If True, explicitly create the target image and blockdev-add it
38 # target_backing: If existing is True: Use this filename as the backing file
40 # (None: no backing file)
41 # target_blockdev_backing: If existing is True: Pass this dict as "backing"
42 # for the blockdev-add command
43 # (None: do not pass "backing")
44 # target_real_backing: If existing is True: The real filename of the backing
45 # image during runtime, only makes sense if
46 # target_blockdev_backing is not None
47 # (None: same as target_backing)
48 # target_open_with_backing: If True, the target image is added with its backing
49 # chain opened right away. If False, blockdev-add
50 # opens it without a backing file and job completion
51 # is supposed to open the backing chain.
52 # use_iothread: If True, an iothread is configured for the virtio-blk device
53 # that uses the image being mirrored
55 class BaseClass(iotests.QMPTestCase):
56 target_blockdev_backing = None
57 target_real_backing = None
58 target_open_with_backing = True
62 qemu_img('create', '-f', iotests.imgfmt, back0_img, '1440K')
63 qemu_img('create', '-f', iotests.imgfmt, '-b', back0_img, back1_img)
64 qemu_img('create', '-f', iotests.imgfmt, '-b', back1_img, back2_img)
65 qemu_img('create', '-f', iotests.imgfmt, '-b', back2_img, source_img)
67 self.vm = iotests.VM()
68 # Add the BDS via blockdev-add so it stays around after the mirror block
69 # job has been completed
70 blockdev = {'node-name': 'source',
71 'driver': iotests.imgfmt,
72 'file': {'driver': 'file',
73 'filename': source_img}}
74 self.vm.add_blockdev(self.vm.qmp_to_opts(blockdev))
77 self.vm.add_object('iothread,id=iothread0')
78 iothread = ",iothread=iothread0"
82 self.vm.add_device('virtio-scsi%s' % iothread)
83 self.vm.add_device('scsi-hd,id=qdev0,drive=source')
87 self.assertIntactSourceBackingChain()
90 if self.target_backing:
91 qemu_img('create', '-f', iotests.imgfmt,
92 '-b', self.target_backing, target_img, '1440K')
94 qemu_img('create', '-f', iotests.imgfmt, target_img, '1440K')
96 if self.cmd == 'blockdev-mirror':
97 options = { 'node-name': 'target',
98 'driver': iotests.imgfmt,
99 'file': { 'driver': 'file',
100 'node-name': 'target-file',
101 'filename': target_img } }
103 if not self.target_open_with_backing:
104 options['backing'] = None
105 elif self.target_blockdev_backing:
106 options['backing'] = self.target_blockdev_backing
108 result = self.vm.qmp('blockdev-add', **options)
109 self.assert_qmp(result, 'return', {})
113 os.remove(source_img)
118 os.remove(target_img)
122 def findBlockNode(self, node_name, qdev=None):
124 result = self.vm.qmp('query-block')
125 for device in result['return']:
126 if device['qdev'] == qdev:
128 self.assert_qmp(device, 'inserted/node-name', node_name)
129 return device['inserted']
131 result = self.vm.qmp('query-named-block-nodes')
132 for node in result['return']:
133 if node['node-name'] == node_name:
136 self.fail('Cannot find node %s/%s' % (qdev, node_name))
138 def assertIntactSourceBackingChain(self):
139 node = self.findBlockNode('source')
141 self.assert_qmp(node, 'image' + '/backing-image' * 0 + '/filename',
143 self.assert_qmp(node, 'image' + '/backing-image' * 1 + '/filename',
145 self.assert_qmp(node, 'image' + '/backing-image' * 2 + '/filename',
147 self.assert_qmp(node, 'image' + '/backing-image' * 3 + '/filename',
149 self.assert_qmp_absent(node, 'image' + '/backing-image' * 4)
151 def assertCorrectBackingImage(self, node, default_image):
153 if self.target_real_backing:
154 image = self.target_real_backing
156 image = self.target_backing
158 image = default_image
161 self.assert_qmp(node, 'image/backing-image/filename', image)
163 self.assert_qmp_absent(node, 'image/backing-image')
166 # Class variables for controlling its behavior:
168 # cmd: Mirroring command to execute, either drive-mirror or blockdev-mirror
170 class MirrorBaseClass(BaseClass):
171 def openBacking(self):
174 def runMirror(self, sync):
175 if self.cmd == 'blockdev-mirror':
176 result = self.vm.qmp(self.cmd, job_id='mirror-job', device='source',
177 sync=sync, target='target',
183 mode = 'absolute-paths'
184 result = self.vm.qmp(self.cmd, job_id='mirror-job', device='source',
185 sync=sync, target=target_img,
186 format=iotests.imgfmt, mode=mode,
187 node_name='target', auto_finalize=False)
189 self.assert_qmp(result, 'return', {})
191 self.vm.run_job('mirror-job', use_log=False, auto_finalize=False,
192 pre_finalize=self.openBacking, auto_dismiss=True)
195 self.runMirror('full')
197 node = self.findBlockNode('target', 'qdev0')
198 self.assertCorrectBackingImage(node, None)
199 self.assertIntactSourceBackingChain()
202 self.runMirror('top')
204 node = self.findBlockNode('target', 'qdev0')
205 self.assertCorrectBackingImage(node, back2_img)
206 self.assertIntactSourceBackingChain()
209 self.runMirror('none')
211 node = self.findBlockNode('target', 'qdev0')
212 self.assertCorrectBackingImage(node, source_img)
213 self.assertIntactSourceBackingChain()
216 class TestDriveMirrorAbsolutePaths(MirrorBaseClass):
220 class TestDriveMirrorExistingNoBacking(MirrorBaseClass):
223 target_backing = None
225 class TestDriveMirrorExistingBacking(MirrorBaseClass):
228 target_backing = 'null-co://'
230 class TestBlockdevMirrorNoBacking(MirrorBaseClass):
231 cmd = 'blockdev-mirror'
233 target_backing = None
235 class TestBlockdevMirrorBacking(MirrorBaseClass):
236 cmd = 'blockdev-mirror'
238 target_backing = 'null-co://'
240 class TestBlockdevMirrorForcedBacking(MirrorBaseClass):
241 cmd = 'blockdev-mirror'
243 target_backing = None
244 target_blockdev_backing = { 'driver': 'null-co' }
245 target_real_backing = 'null-co://'
247 # Attach the backing chain only during completion, with blockdev-reopen
248 class TestBlockdevMirrorReopen(MirrorBaseClass):
249 cmd = 'blockdev-mirror'
251 target_backing = 'null-co://'
252 target_open_with_backing = False
254 def openBacking(self):
255 if not self.target_open_with_backing:
256 result = self.vm.qmp('blockdev-add', node_name="backing",
258 self.assert_qmp(result, 'return', {})
259 result = self.vm.qmp('x-blockdev-reopen', node_name="target",
260 driver=iotests.imgfmt, file="target-file",
262 self.assert_qmp(result, 'return', {})
264 class TestBlockdevMirrorReopenIothread(TestBlockdevMirrorReopen):
267 # Attach the backing chain only during completion, with blockdev-snapshot
268 class TestBlockdevMirrorSnapshot(MirrorBaseClass):
269 cmd = 'blockdev-mirror'
271 target_backing = 'null-co://'
272 target_open_with_backing = False
274 def openBacking(self):
275 if not self.target_open_with_backing:
276 result = self.vm.qmp('blockdev-add', node_name="backing",
278 self.assert_qmp(result, 'return', {})
279 result = self.vm.qmp('blockdev-snapshot', node="backing",
281 self.assert_qmp(result, 'return', {})
283 class TestBlockdevMirrorSnapshotIothread(TestBlockdevMirrorSnapshot):
286 class TestCommit(BaseClass):
289 def testCommit(self):
290 result = self.vm.qmp('block-commit', job_id='commit-job',
291 device='source', base=back1_img)
292 self.assert_qmp(result, 'return', {})
294 self.vm.event_wait('BLOCK_JOB_READY')
296 result = self.vm.qmp('block-job-complete', device='commit-job')
297 self.assert_qmp(result, 'return', {})
299 self.vm.event_wait('BLOCK_JOB_COMPLETED')
301 node = self.findBlockNode(None, 'qdev0')
302 self.assert_qmp(node, 'image' + '/backing-image' * 0 + '/filename',
304 self.assert_qmp(node, 'image' + '/backing-image' * 1 + '/filename',
306 self.assert_qmp_absent(node, 'image' + '/backing-image' * 2 +
309 self.assertIntactSourceBackingChain()
313 MirrorBaseClass = None
315 if __name__ == '__main__':
316 iotests.main(supported_fmts=['qcow2'],
317 supported_protocols=['file'])