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
,
64 '-F', iotests
.imgfmt
, back1_img
)
65 qemu_img('create', '-f', iotests
.imgfmt
, '-b', back1_img
,
66 '-F', iotests
.imgfmt
, back2_img
)
67 qemu_img('create', '-f', iotests
.imgfmt
, '-b', back2_img
,
68 '-F', iotests
.imgfmt
, source_img
)
70 self
.vm
= iotests
.VM()
71 # Add the BDS via blockdev-add so it stays around after the mirror block
72 # job has been completed
73 blockdev
= {'node-name': 'source',
74 'driver': iotests
.imgfmt
,
75 'file': {'driver': 'file',
76 'filename': source_img
}}
77 self
.vm
.add_blockdev(self
.vm
.qmp_to_opts(blockdev
))
80 self
.vm
.add_object('iothread,id=iothread0')
81 iothread
= ",iothread=iothread0"
85 self
.vm
.add_device('virtio-scsi%s' % iothread
)
86 self
.vm
.add_device('scsi-hd,id=qdev0,drive=source')
90 self
.assertIntactSourceBackingChain()
93 if self
.target_backing
:
94 qemu_img('create', '-f', iotests
.imgfmt
,
95 '-b', self
.target_backing
, '-F', 'raw',
98 qemu_img('create', '-f', iotests
.imgfmt
, target_img
, '1440K')
100 if self
.cmd
== 'blockdev-mirror':
101 options
= { 'node-name': 'target',
102 'driver': iotests
.imgfmt
,
103 'file': { 'driver': 'file',
104 'node-name': 'target-file',
105 'filename': target_img
} }
107 if not self
.target_open_with_backing
:
108 options
['backing'] = None
109 elif self
.target_blockdev_backing
:
110 options
['backing'] = self
.target_blockdev_backing
112 result
= self
.vm
.qmp('blockdev-add', **options
)
113 self
.assert_qmp(result
, 'return', {})
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 result
= self
.vm
.qmp(self
.cmd
, job_id
='mirror-job', device
='source',
181 sync
=sync
, target
='target',
187 mode
= 'absolute-paths'
188 result
= self
.vm
.qmp(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
.assert_qmp(result
, 'return', {})
195 self
.vm
.run_job('mirror-job', auto_finalize
=False,
196 pre_finalize
=self
.openBacking
, auto_dismiss
=True)
199 self
.runMirror('full')
201 node
= self
.findBlockNode('target', 'qdev0')
202 self
.assertCorrectBackingImage(node
, None)
203 self
.assertIntactSourceBackingChain()
206 self
.runMirror('top')
208 node
= self
.findBlockNode('target', 'qdev0')
209 self
.assertCorrectBackingImage(node
, back2_img
)
210 self
.assertIntactSourceBackingChain()
213 self
.runMirror('none')
215 node
= self
.findBlockNode('target', 'qdev0')
216 self
.assertCorrectBackingImage(node
, source_img
)
217 self
.assertIntactSourceBackingChain()
220 class TestDriveMirrorAbsolutePaths(MirrorBaseClass
):
224 class TestDriveMirrorExistingNoBacking(MirrorBaseClass
):
227 target_backing
= None
229 class TestDriveMirrorExistingBacking(MirrorBaseClass
):
232 target_backing
= 'null-co://'
234 class TestBlockdevMirrorNoBacking(MirrorBaseClass
):
235 cmd
= 'blockdev-mirror'
237 target_backing
= None
239 class TestBlockdevMirrorBacking(MirrorBaseClass
):
240 cmd
= 'blockdev-mirror'
242 target_backing
= 'null-co://'
244 class TestBlockdevMirrorForcedBacking(MirrorBaseClass
):
245 cmd
= 'blockdev-mirror'
247 target_backing
= None
248 target_blockdev_backing
= { 'driver': 'null-co' }
249 target_real_backing
= 'null-co://'
251 # Attach the backing chain only during completion, with blockdev-reopen
252 class TestBlockdevMirrorReopen(MirrorBaseClass
):
253 cmd
= 'blockdev-mirror'
255 target_backing
= 'null-co://'
256 target_open_with_backing
= False
258 def openBacking(self
):
259 if not self
.target_open_with_backing
:
260 result
= self
.vm
.qmp('blockdev-add', node_name
="backing",
262 self
.assert_qmp(result
, 'return', {})
263 result
= self
.vm
.qmp('x-blockdev-reopen', node_name
="target",
264 driver
=iotests
.imgfmt
, file="target-file",
266 self
.assert_qmp(result
, 'return', {})
268 class TestBlockdevMirrorReopenIothread(TestBlockdevMirrorReopen
):
271 # Attach the backing chain only during completion, with blockdev-snapshot
272 class TestBlockdevMirrorSnapshot(MirrorBaseClass
):
273 cmd
= 'blockdev-mirror'
275 target_backing
= 'null-co://'
276 target_open_with_backing
= False
278 def openBacking(self
):
279 if not self
.target_open_with_backing
:
280 result
= self
.vm
.qmp('blockdev-add', node_name
="backing",
282 self
.assert_qmp(result
, 'return', {})
283 result
= self
.vm
.qmp('blockdev-snapshot', node
="backing",
285 self
.assert_qmp(result
, 'return', {})
287 class TestBlockdevMirrorSnapshotIothread(TestBlockdevMirrorSnapshot
):
290 class TestCommit(BaseClass
):
293 def testCommit(self
):
294 result
= self
.vm
.qmp('block-commit', job_id
='commit-job',
295 device
='source', base
=back1_img
)
296 self
.assert_qmp(result
, 'return', {})
298 self
.vm
.event_wait('BLOCK_JOB_READY')
300 result
= self
.vm
.qmp('block-job-complete', device
='commit-job')
301 self
.assert_qmp(result
, 'return', {})
303 self
.vm
.event_wait('BLOCK_JOB_COMPLETED')
305 node
= self
.findBlockNode(None, 'qdev0')
306 self
.assert_qmp(node
, 'image' + '/backing-image' * 0 + '/filename',
308 self
.assert_qmp(node
, 'image' + '/backing-image' * 1 + '/filename',
310 self
.assert_qmp_absent(node
, 'image' + '/backing-image' * 2 +
313 self
.assertIntactSourceBackingChain()
317 MirrorBaseClass
= None
319 if __name__
== '__main__':
320 iotests
.main(supported_fmts
=['qcow2'],
321 supported_protocols
=['file'])