1903cb1bfSPhilippe Mathieu-Daudé#!/usr/bin/env python3 29dd003a9SVladimir 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 113*b6aed193SVladimir Sementsov-Ogievskiy self.vm.cmd('blockdev-add', options) 114298c6009SMax Reitz 115298c6009SMax Reitz def tearDown(self): 116298c6009SMax Reitz self.vm.shutdown() 117298c6009SMax Reitz os.remove(source_img) 118298c6009SMax Reitz os.remove(back2_img) 119298c6009SMax Reitz os.remove(back1_img) 120298c6009SMax Reitz os.remove(back0_img) 121298c6009SMax Reitz try: 122298c6009SMax Reitz os.remove(target_img) 123298c6009SMax Reitz except OSError: 124298c6009SMax Reitz pass 125298c6009SMax Reitz 1261d701e0eSMax Reitz def findBlockNode(self, node_name, qdev=None): 1271d701e0eSMax Reitz if qdev: 128298c6009SMax Reitz result = self.vm.qmp('query-block') 129298c6009SMax Reitz for device in result['return']: 1301d701e0eSMax Reitz if device['qdev'] == qdev: 131298c6009SMax Reitz if node_name: 132298c6009SMax Reitz self.assert_qmp(device, 'inserted/node-name', node_name) 133298c6009SMax Reitz return device['inserted'] 134298c6009SMax Reitz else: 135298c6009SMax Reitz result = self.vm.qmp('query-named-block-nodes') 136298c6009SMax Reitz for node in result['return']: 137298c6009SMax Reitz if node['node-name'] == node_name: 138298c6009SMax Reitz return node 139298c6009SMax Reitz 1401d701e0eSMax Reitz self.fail('Cannot find node %s/%s' % (qdev, node_name)) 141298c6009SMax Reitz 142298c6009SMax Reitz def assertIntactSourceBackingChain(self): 143298c6009SMax Reitz node = self.findBlockNode('source') 144298c6009SMax Reitz 145298c6009SMax Reitz self.assert_qmp(node, 'image' + '/backing-image' * 0 + '/filename', 146298c6009SMax Reitz source_img) 147298c6009SMax Reitz self.assert_qmp(node, 'image' + '/backing-image' * 1 + '/filename', 148298c6009SMax Reitz back2_img) 149298c6009SMax Reitz self.assert_qmp(node, 'image' + '/backing-image' * 2 + '/filename', 150298c6009SMax Reitz back1_img) 151298c6009SMax Reitz self.assert_qmp(node, 'image' + '/backing-image' * 3 + '/filename', 152298c6009SMax Reitz back0_img) 153298c6009SMax Reitz self.assert_qmp_absent(node, 'image' + '/backing-image' * 4) 154298c6009SMax Reitz 155298c6009SMax Reitz def assertCorrectBackingImage(self, node, default_image): 156298c6009SMax Reitz if self.existing: 157298c6009SMax Reitz if self.target_real_backing: 158298c6009SMax Reitz image = self.target_real_backing 159298c6009SMax Reitz else: 160298c6009SMax Reitz image = self.target_backing 161298c6009SMax Reitz else: 162298c6009SMax Reitz image = default_image 163298c6009SMax Reitz 164298c6009SMax Reitz if image: 165298c6009SMax Reitz self.assert_qmp(node, 'image/backing-image/filename', image) 166298c6009SMax Reitz else: 167298c6009SMax Reitz self.assert_qmp_absent(node, 'image/backing-image') 168298c6009SMax Reitz 169298c6009SMax Reitz 170298c6009SMax Reitz# Class variables for controlling its behavior: 171298c6009SMax Reitz# 172298c6009SMax Reitz# cmd: Mirroring command to execute, either drive-mirror or blockdev-mirror 173298c6009SMax Reitz 174298c6009SMax Reitzclass MirrorBaseClass(BaseClass): 1758bdee9f1SKevin Wolf def openBacking(self): 1768bdee9f1SKevin Wolf pass 1778bdee9f1SKevin Wolf 178298c6009SMax Reitz def runMirror(self, sync): 179298c6009SMax Reitz if self.cmd == 'blockdev-mirror': 180*b6aed193SVladimir Sementsov-Ogievskiy self.vm.cmd(self.cmd, job_id='mirror-job', device='source', 1818bdee9f1SKevin Wolf sync=sync, target='target', 1828bdee9f1SKevin Wolf auto_finalize=False) 183298c6009SMax Reitz else: 184298c6009SMax Reitz if self.existing: 185298c6009SMax Reitz mode = 'existing' 186298c6009SMax Reitz else: 187298c6009SMax Reitz mode = 'absolute-paths' 188*b6aed193SVladimir Sementsov-Ogievskiy self.vm.cmd(self.cmd, job_id='mirror-job', device='source', 1891d701e0eSMax Reitz sync=sync, target=target_img, 1901d701e0eSMax Reitz format=iotests.imgfmt, mode=mode, 1918bdee9f1SKevin Wolf node_name='target', auto_finalize=False) 192298c6009SMax Reitz 19352ea799eSJohn Snow self.vm.run_job('mirror-job', auto_finalize=False, 1948bdee9f1SKevin Wolf pre_finalize=self.openBacking, auto_dismiss=True) 195298c6009SMax Reitz 196298c6009SMax Reitz def testFull(self): 197298c6009SMax Reitz self.runMirror('full') 198298c6009SMax Reitz 1996a5f6403SKevin Wolf node = self.findBlockNode('target', 'qdev0') 200298c6009SMax Reitz self.assertCorrectBackingImage(node, None) 201298c6009SMax Reitz self.assertIntactSourceBackingChain() 202298c6009SMax Reitz 203298c6009SMax Reitz def testTop(self): 204298c6009SMax Reitz self.runMirror('top') 205298c6009SMax Reitz 2066a5f6403SKevin Wolf node = self.findBlockNode('target', 'qdev0') 207298c6009SMax Reitz self.assertCorrectBackingImage(node, back2_img) 208298c6009SMax Reitz self.assertIntactSourceBackingChain() 209298c6009SMax Reitz 210298c6009SMax Reitz def testNone(self): 211298c6009SMax Reitz self.runMirror('none') 212298c6009SMax Reitz 2136a5f6403SKevin Wolf node = self.findBlockNode('target', 'qdev0') 214298c6009SMax Reitz self.assertCorrectBackingImage(node, source_img) 215298c6009SMax Reitz self.assertIntactSourceBackingChain() 216298c6009SMax Reitz 217298c6009SMax Reitz 218298c6009SMax Reitzclass TestDriveMirrorAbsolutePaths(MirrorBaseClass): 219298c6009SMax Reitz cmd = 'drive-mirror' 220298c6009SMax Reitz existing = False 221298c6009SMax Reitz 222298c6009SMax Reitzclass TestDriveMirrorExistingNoBacking(MirrorBaseClass): 223298c6009SMax Reitz cmd = 'drive-mirror' 224298c6009SMax Reitz existing = True 225298c6009SMax Reitz target_backing = None 226298c6009SMax Reitz 227298c6009SMax Reitzclass TestDriveMirrorExistingBacking(MirrorBaseClass): 228298c6009SMax Reitz cmd = 'drive-mirror' 229298c6009SMax Reitz existing = True 230298c6009SMax Reitz target_backing = 'null-co://' 231298c6009SMax Reitz 232298c6009SMax Reitzclass TestBlockdevMirrorNoBacking(MirrorBaseClass): 233298c6009SMax Reitz cmd = 'blockdev-mirror' 234298c6009SMax Reitz existing = True 235298c6009SMax Reitz target_backing = None 236298c6009SMax Reitz 237298c6009SMax Reitzclass TestBlockdevMirrorBacking(MirrorBaseClass): 238298c6009SMax Reitz cmd = 'blockdev-mirror' 239298c6009SMax Reitz existing = True 240298c6009SMax Reitz target_backing = 'null-co://' 241298c6009SMax Reitz 242298c6009SMax Reitzclass TestBlockdevMirrorForcedBacking(MirrorBaseClass): 243298c6009SMax Reitz cmd = 'blockdev-mirror' 244298c6009SMax Reitz existing = True 245298c6009SMax Reitz target_backing = None 246298c6009SMax Reitz target_blockdev_backing = { 'driver': 'null-co' } 247298c6009SMax Reitz target_real_backing = 'null-co://' 248298c6009SMax Reitz 2498bdee9f1SKevin Wolf# Attach the backing chain only during completion, with blockdev-reopen 2508bdee9f1SKevin Wolfclass TestBlockdevMirrorReopen(MirrorBaseClass): 2518bdee9f1SKevin Wolf cmd = 'blockdev-mirror' 2528bdee9f1SKevin Wolf existing = True 2538bdee9f1SKevin Wolf target_backing = 'null-co://' 2548bdee9f1SKevin Wolf target_open_with_backing = False 2558bdee9f1SKevin Wolf 2568bdee9f1SKevin Wolf def openBacking(self): 2578bdee9f1SKevin Wolf if not self.target_open_with_backing: 258*b6aed193SVladimir Sementsov-Ogievskiy self.vm.cmd('blockdev-add', node_name="backing", 2598bdee9f1SKevin Wolf driver="null-co") 260*b6aed193SVladimir Sementsov-Ogievskiy self.vm.cmd('blockdev-reopen', options=[{ 2613908b7a8SAlberto Garcia 'node-name': "target", 2623908b7a8SAlberto Garcia 'driver': iotests.imgfmt, 2633908b7a8SAlberto Garcia 'file': "target-file", 2643908b7a8SAlberto Garcia 'backing': "backing" 2653908b7a8SAlberto Garcia }]) 2668bdee9f1SKevin Wolf 2676a5f6403SKevin Wolfclass TestBlockdevMirrorReopenIothread(TestBlockdevMirrorReopen): 2686a5f6403SKevin Wolf use_iothread = True 2696a5f6403SKevin Wolf 2708bdee9f1SKevin Wolf# Attach the backing chain only during completion, with blockdev-snapshot 2718bdee9f1SKevin Wolfclass TestBlockdevMirrorSnapshot(MirrorBaseClass): 2728bdee9f1SKevin Wolf cmd = 'blockdev-mirror' 2738bdee9f1SKevin Wolf existing = True 2748bdee9f1SKevin Wolf target_backing = 'null-co://' 2758bdee9f1SKevin Wolf target_open_with_backing = False 2768bdee9f1SKevin Wolf 2778bdee9f1SKevin Wolf def openBacking(self): 2788bdee9f1SKevin Wolf if not self.target_open_with_backing: 279*b6aed193SVladimir Sementsov-Ogievskiy self.vm.cmd('blockdev-add', node_name="backing", 2808bdee9f1SKevin Wolf driver="null-co") 281*b6aed193SVladimir Sementsov-Ogievskiy self.vm.cmd('blockdev-snapshot', node="backing", 2828bdee9f1SKevin Wolf overlay="target") 283298c6009SMax Reitz 2846a5f6403SKevin Wolfclass TestBlockdevMirrorSnapshotIothread(TestBlockdevMirrorSnapshot): 2856a5f6403SKevin Wolf use_iothread = True 2866a5f6403SKevin Wolf 287298c6009SMax Reitzclass TestCommit(BaseClass): 288298c6009SMax Reitz existing = False 289298c6009SMax Reitz 290298c6009SMax Reitz def testCommit(self): 291*b6aed193SVladimir Sementsov-Ogievskiy self.vm.cmd('block-commit', job_id='commit-job', 2921d701e0eSMax Reitz device='source', base=back1_img) 293298c6009SMax Reitz 294298c6009SMax Reitz self.vm.event_wait('BLOCK_JOB_READY') 295298c6009SMax Reitz 296*b6aed193SVladimir Sementsov-Ogievskiy self.vm.cmd('block-job-complete', device='commit-job') 297298c6009SMax Reitz 298298c6009SMax Reitz self.vm.event_wait('BLOCK_JOB_COMPLETED') 299298c6009SMax Reitz 3006a5f6403SKevin Wolf node = self.findBlockNode(None, 'qdev0') 301298c6009SMax Reitz self.assert_qmp(node, 'image' + '/backing-image' * 0 + '/filename', 302298c6009SMax Reitz back1_img) 303298c6009SMax Reitz self.assert_qmp(node, 'image' + '/backing-image' * 1 + '/filename', 304298c6009SMax Reitz back0_img) 305298c6009SMax Reitz self.assert_qmp_absent(node, 'image' + '/backing-image' * 2 + 306298c6009SMax Reitz '/filename') 307298c6009SMax Reitz 308298c6009SMax Reitz self.assertIntactSourceBackingChain() 309298c6009SMax Reitz 310298c6009SMax Reitz 311298c6009SMax ReitzBaseClass = None 312298c6009SMax ReitzMirrorBaseClass = None 313298c6009SMax Reitz 314298c6009SMax Reitzif __name__ == '__main__': 315103cbc77SMax Reitz iotests.main(supported_fmts=['qcow2'], 316103cbc77SMax Reitz supported_protocols=['file']) 317