2020-01-30 19:32:23 +03:00
|
|
|
#!/usr/bin/env python3
|
2021-01-16 16:44:19 +03:00
|
|
|
# group: rw
|
2016-06-10 21:57:49 +03:00
|
|
|
#
|
|
|
|
# Test whether the backing BDSs are correct after completion of a
|
|
|
|
# mirror block job; in "existing" modes (drive-mirror with
|
|
|
|
# mode=existing and blockdev-mirror) the backing chain should not be
|
|
|
|
# overridden.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2016 Red Hat, Inc.
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
|
|
|
|
import os
|
|
|
|
import iotests
|
|
|
|
from iotests import qemu_img
|
|
|
|
|
|
|
|
back0_img = os.path.join(iotests.test_dir, 'back0.' + iotests.imgfmt)
|
|
|
|
back1_img = os.path.join(iotests.test_dir, 'back1.' + iotests.imgfmt)
|
|
|
|
back2_img = os.path.join(iotests.test_dir, 'back2.' + iotests.imgfmt)
|
|
|
|
source_img = os.path.join(iotests.test_dir, 'source.' + iotests.imgfmt)
|
|
|
|
target_img = os.path.join(iotests.test_dir, 'target.' + iotests.imgfmt)
|
|
|
|
|
|
|
|
|
|
|
|
# Class variables for controlling its behavior:
|
|
|
|
#
|
|
|
|
# existing: If True, explicitly create the target image and blockdev-add it
|
|
|
|
# target_backing: If existing is True: Use this filename as the backing file
|
|
|
|
# of the target image
|
|
|
|
# (None: no backing file)
|
|
|
|
# target_blockdev_backing: If existing is True: Pass this dict as "backing"
|
|
|
|
# for the blockdev-add command
|
|
|
|
# (None: do not pass "backing")
|
|
|
|
# target_real_backing: If existing is True: The real filename of the backing
|
|
|
|
# image during runtime, only makes sense if
|
|
|
|
# target_blockdev_backing is not None
|
|
|
|
# (None: same as target_backing)
|
2020-03-10 14:38:28 +03:00
|
|
|
# target_open_with_backing: If True, the target image is added with its backing
|
|
|
|
# chain opened right away. If False, blockdev-add
|
|
|
|
# opens it without a backing file and job completion
|
|
|
|
# is supposed to open the backing chain.
|
2020-03-10 14:38:30 +03:00
|
|
|
# use_iothread: If True, an iothread is configured for the virtio-blk device
|
|
|
|
# that uses the image being mirrored
|
2016-06-10 21:57:49 +03:00
|
|
|
|
|
|
|
class BaseClass(iotests.QMPTestCase):
|
|
|
|
target_blockdev_backing = None
|
|
|
|
target_real_backing = None
|
2020-03-10 14:38:28 +03:00
|
|
|
target_open_with_backing = True
|
2020-03-10 14:38:30 +03:00
|
|
|
use_iothread = False
|
2016-06-10 21:57:49 +03:00
|
|
|
|
|
|
|
def setUp(self):
|
2017-11-11 01:42:59 +03:00
|
|
|
qemu_img('create', '-f', iotests.imgfmt, back0_img, '1440K')
|
iotests: Specify explicit backing format where sensible
There are many existing qcow2 images that specify a backing file but
no format. This has been the source of CVEs in the past, but has
become more prominent of a problem now that libvirt has switched to
-blockdev. With older -drive, at least the probing was always done by
qemu (so the only risk of a changed format between successive boots of
a guest was if qemu was upgraded and probed differently). But with
newer -blockdev, libvirt must specify a format; if libvirt guesses raw
where the image was formatted, this results in data corruption visible
to the guest; conversely, if libvirt guesses qcow2 where qemu was
using raw, this can result in potential security holes, so modern
libvirt instead refuses to use images without explicit backing format.
The change in libvirt to reject images without explicit backing format
has pointed out that a number of tools have been far too reliant on
probing in the past. It's time to set a better example in our own
iotests of properly setting this parameter.
iotest calls to create, rebase, and convert are all impacted to some
degree. It's a bit annoying that we are inconsistent on command line
- while all of those accept -o backing_file=...,backing_fmt=..., the
shortcuts are different: create and rebase have -b and -F, while
convert has -B but no -F. (amend has no shortcuts, but the previous
patch just deprecated the use of amend to change backing chains).
Signed-off-by: Eric Blake <eblake@redhat.com>
Message-Id: <20200706203954.341758-9-eblake@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2020-07-06 23:39:52 +03:00
|
|
|
qemu_img('create', '-f', iotests.imgfmt, '-b', back0_img,
|
|
|
|
'-F', iotests.imgfmt, back1_img)
|
|
|
|
qemu_img('create', '-f', iotests.imgfmt, '-b', back1_img,
|
|
|
|
'-F', iotests.imgfmt, back2_img)
|
|
|
|
qemu_img('create', '-f', iotests.imgfmt, '-b', back2_img,
|
|
|
|
'-F', iotests.imgfmt, source_img)
|
2016-06-10 21:57:49 +03:00
|
|
|
|
|
|
|
self.vm = iotests.VM()
|
|
|
|
# Add the BDS via blockdev-add so it stays around after the mirror block
|
|
|
|
# job has been completed
|
2017-11-11 01:42:59 +03:00
|
|
|
blockdev = {'node-name': 'source',
|
|
|
|
'driver': iotests.imgfmt,
|
|
|
|
'file': {'driver': 'file',
|
|
|
|
'filename': source_img}}
|
2018-05-08 19:10:16 +03:00
|
|
|
self.vm.add_blockdev(self.vm.qmp_to_opts(blockdev))
|
2020-03-10 14:38:30 +03:00
|
|
|
|
|
|
|
if self.use_iothread:
|
|
|
|
self.vm.add_object('iothread,id=iothread0')
|
|
|
|
iothread = ",iothread=iothread0"
|
|
|
|
else:
|
|
|
|
iothread = ""
|
|
|
|
|
|
|
|
self.vm.add_device('virtio-scsi%s' % iothread)
|
|
|
|
self.vm.add_device('scsi-hd,id=qdev0,drive=source')
|
|
|
|
|
2017-11-11 01:42:59 +03:00
|
|
|
self.vm.launch()
|
2016-06-10 21:57:49 +03:00
|
|
|
|
|
|
|
self.assertIntactSourceBackingChain()
|
|
|
|
|
|
|
|
if self.existing:
|
|
|
|
if self.target_backing:
|
|
|
|
qemu_img('create', '-f', iotests.imgfmt,
|
iotests: Specify explicit backing format where sensible
There are many existing qcow2 images that specify a backing file but
no format. This has been the source of CVEs in the past, but has
become more prominent of a problem now that libvirt has switched to
-blockdev. With older -drive, at least the probing was always done by
qemu (so the only risk of a changed format between successive boots of
a guest was if qemu was upgraded and probed differently). But with
newer -blockdev, libvirt must specify a format; if libvirt guesses raw
where the image was formatted, this results in data corruption visible
to the guest; conversely, if libvirt guesses qcow2 where qemu was
using raw, this can result in potential security holes, so modern
libvirt instead refuses to use images without explicit backing format.
The change in libvirt to reject images without explicit backing format
has pointed out that a number of tools have been far too reliant on
probing in the past. It's time to set a better example in our own
iotests of properly setting this parameter.
iotest calls to create, rebase, and convert are all impacted to some
degree. It's a bit annoying that we are inconsistent on command line
- while all of those accept -o backing_file=...,backing_fmt=..., the
shortcuts are different: create and rebase have -b and -F, while
convert has -B but no -F. (amend has no shortcuts, but the previous
patch just deprecated the use of amend to change backing chains).
Signed-off-by: Eric Blake <eblake@redhat.com>
Message-Id: <20200706203954.341758-9-eblake@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2020-07-06 23:39:52 +03:00
|
|
|
'-b', self.target_backing, '-F', 'raw',
|
|
|
|
target_img, '1440K')
|
2016-06-10 21:57:49 +03:00
|
|
|
else:
|
2017-11-11 01:42:59 +03:00
|
|
|
qemu_img('create', '-f', iotests.imgfmt, target_img, '1440K')
|
2016-06-10 21:57:49 +03:00
|
|
|
|
|
|
|
if self.cmd == 'blockdev-mirror':
|
|
|
|
options = { 'node-name': 'target',
|
|
|
|
'driver': iotests.imgfmt,
|
|
|
|
'file': { 'driver': 'file',
|
2020-03-10 14:38:28 +03:00
|
|
|
'node-name': 'target-file',
|
2016-06-10 21:57:49 +03:00
|
|
|
'filename': target_img } }
|
2020-03-10 14:38:28 +03:00
|
|
|
|
|
|
|
if not self.target_open_with_backing:
|
|
|
|
options['backing'] = None
|
|
|
|
elif self.target_blockdev_backing:
|
|
|
|
options['backing'] = self.target_blockdev_backing
|
2016-06-10 21:57:49 +03:00
|
|
|
|
2023-10-06 18:41:25 +03:00
|
|
|
self.vm.cmd('blockdev-add', options)
|
2016-06-10 21:57:49 +03:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.vm.shutdown()
|
|
|
|
os.remove(source_img)
|
|
|
|
os.remove(back2_img)
|
|
|
|
os.remove(back1_img)
|
|
|
|
os.remove(back0_img)
|
|
|
|
try:
|
|
|
|
os.remove(target_img)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
2017-11-11 01:42:59 +03:00
|
|
|
def findBlockNode(self, node_name, qdev=None):
|
|
|
|
if qdev:
|
2016-06-10 21:57:49 +03:00
|
|
|
result = self.vm.qmp('query-block')
|
|
|
|
for device in result['return']:
|
2017-11-11 01:42:59 +03:00
|
|
|
if device['qdev'] == qdev:
|
2016-06-10 21:57:49 +03:00
|
|
|
if node_name:
|
|
|
|
self.assert_qmp(device, 'inserted/node-name', node_name)
|
|
|
|
return device['inserted']
|
|
|
|
else:
|
|
|
|
result = self.vm.qmp('query-named-block-nodes')
|
|
|
|
for node in result['return']:
|
|
|
|
if node['node-name'] == node_name:
|
|
|
|
return node
|
|
|
|
|
2017-11-11 01:42:59 +03:00
|
|
|
self.fail('Cannot find node %s/%s' % (qdev, node_name))
|
2016-06-10 21:57:49 +03:00
|
|
|
|
|
|
|
def assertIntactSourceBackingChain(self):
|
|
|
|
node = self.findBlockNode('source')
|
|
|
|
|
|
|
|
self.assert_qmp(node, 'image' + '/backing-image' * 0 + '/filename',
|
|
|
|
source_img)
|
|
|
|
self.assert_qmp(node, 'image' + '/backing-image' * 1 + '/filename',
|
|
|
|
back2_img)
|
|
|
|
self.assert_qmp(node, 'image' + '/backing-image' * 2 + '/filename',
|
|
|
|
back1_img)
|
|
|
|
self.assert_qmp(node, 'image' + '/backing-image' * 3 + '/filename',
|
|
|
|
back0_img)
|
|
|
|
self.assert_qmp_absent(node, 'image' + '/backing-image' * 4)
|
|
|
|
|
|
|
|
def assertCorrectBackingImage(self, node, default_image):
|
|
|
|
if self.existing:
|
|
|
|
if self.target_real_backing:
|
|
|
|
image = self.target_real_backing
|
|
|
|
else:
|
|
|
|
image = self.target_backing
|
|
|
|
else:
|
|
|
|
image = default_image
|
|
|
|
|
|
|
|
if image:
|
|
|
|
self.assert_qmp(node, 'image/backing-image/filename', image)
|
|
|
|
else:
|
|
|
|
self.assert_qmp_absent(node, 'image/backing-image')
|
|
|
|
|
|
|
|
|
|
|
|
# Class variables for controlling its behavior:
|
|
|
|
#
|
|
|
|
# cmd: Mirroring command to execute, either drive-mirror or blockdev-mirror
|
|
|
|
|
|
|
|
class MirrorBaseClass(BaseClass):
|
2020-03-10 14:38:28 +03:00
|
|
|
def openBacking(self):
|
|
|
|
pass
|
|
|
|
|
2016-06-10 21:57:49 +03:00
|
|
|
def runMirror(self, sync):
|
|
|
|
if self.cmd == 'blockdev-mirror':
|
2023-10-06 18:41:25 +03:00
|
|
|
self.vm.cmd(self.cmd, job_id='mirror-job', device='source',
|
|
|
|
sync=sync, target='target',
|
|
|
|
auto_finalize=False)
|
2016-06-10 21:57:49 +03:00
|
|
|
else:
|
|
|
|
if self.existing:
|
|
|
|
mode = 'existing'
|
|
|
|
else:
|
|
|
|
mode = 'absolute-paths'
|
2023-10-06 18:41:25 +03:00
|
|
|
self.vm.cmd(self.cmd, job_id='mirror-job', device='source',
|
|
|
|
sync=sync, target=target_img,
|
|
|
|
format=iotests.imgfmt, mode=mode,
|
|
|
|
node_name='target', auto_finalize=False)
|
2016-06-10 21:57:49 +03:00
|
|
|
|
2020-03-31 03:00:14 +03:00
|
|
|
self.vm.run_job('mirror-job', auto_finalize=False,
|
2020-03-10 14:38:28 +03:00
|
|
|
pre_finalize=self.openBacking, auto_dismiss=True)
|
2016-06-10 21:57:49 +03:00
|
|
|
|
|
|
|
def testFull(self):
|
|
|
|
self.runMirror('full')
|
|
|
|
|
2020-03-10 14:38:30 +03:00
|
|
|
node = self.findBlockNode('target', 'qdev0')
|
2016-06-10 21:57:49 +03:00
|
|
|
self.assertCorrectBackingImage(node, None)
|
|
|
|
self.assertIntactSourceBackingChain()
|
|
|
|
|
|
|
|
def testTop(self):
|
|
|
|
self.runMirror('top')
|
|
|
|
|
2020-03-10 14:38:30 +03:00
|
|
|
node = self.findBlockNode('target', 'qdev0')
|
2016-06-10 21:57:49 +03:00
|
|
|
self.assertCorrectBackingImage(node, back2_img)
|
|
|
|
self.assertIntactSourceBackingChain()
|
|
|
|
|
|
|
|
def testNone(self):
|
|
|
|
self.runMirror('none')
|
|
|
|
|
2020-03-10 14:38:30 +03:00
|
|
|
node = self.findBlockNode('target', 'qdev0')
|
2016-06-10 21:57:49 +03:00
|
|
|
self.assertCorrectBackingImage(node, source_img)
|
|
|
|
self.assertIntactSourceBackingChain()
|
|
|
|
|
|
|
|
|
|
|
|
class TestDriveMirrorAbsolutePaths(MirrorBaseClass):
|
|
|
|
cmd = 'drive-mirror'
|
|
|
|
existing = False
|
|
|
|
|
|
|
|
class TestDriveMirrorExistingNoBacking(MirrorBaseClass):
|
|
|
|
cmd = 'drive-mirror'
|
|
|
|
existing = True
|
|
|
|
target_backing = None
|
|
|
|
|
|
|
|
class TestDriveMirrorExistingBacking(MirrorBaseClass):
|
|
|
|
cmd = 'drive-mirror'
|
|
|
|
existing = True
|
|
|
|
target_backing = 'null-co://'
|
|
|
|
|
|
|
|
class TestBlockdevMirrorNoBacking(MirrorBaseClass):
|
|
|
|
cmd = 'blockdev-mirror'
|
|
|
|
existing = True
|
|
|
|
target_backing = None
|
|
|
|
|
|
|
|
class TestBlockdevMirrorBacking(MirrorBaseClass):
|
|
|
|
cmd = 'blockdev-mirror'
|
|
|
|
existing = True
|
|
|
|
target_backing = 'null-co://'
|
|
|
|
|
|
|
|
class TestBlockdevMirrorForcedBacking(MirrorBaseClass):
|
|
|
|
cmd = 'blockdev-mirror'
|
|
|
|
existing = True
|
|
|
|
target_backing = None
|
|
|
|
target_blockdev_backing = { 'driver': 'null-co' }
|
|
|
|
target_real_backing = 'null-co://'
|
|
|
|
|
2020-03-10 14:38:28 +03:00
|
|
|
# Attach the backing chain only during completion, with blockdev-reopen
|
|
|
|
class TestBlockdevMirrorReopen(MirrorBaseClass):
|
|
|
|
cmd = 'blockdev-mirror'
|
|
|
|
existing = True
|
|
|
|
target_backing = 'null-co://'
|
|
|
|
target_open_with_backing = False
|
|
|
|
|
|
|
|
def openBacking(self):
|
|
|
|
if not self.target_open_with_backing:
|
2023-10-06 18:41:25 +03:00
|
|
|
self.vm.cmd('blockdev-add', node_name="backing",
|
|
|
|
driver="null-co")
|
|
|
|
self.vm.cmd('blockdev-reopen', options=[{
|
|
|
|
'node-name': "target",
|
|
|
|
'driver': iotests.imgfmt,
|
|
|
|
'file': "target-file",
|
|
|
|
'backing': "backing"
|
|
|
|
}])
|
2020-03-10 14:38:28 +03:00
|
|
|
|
2020-03-10 14:38:30 +03:00
|
|
|
class TestBlockdevMirrorReopenIothread(TestBlockdevMirrorReopen):
|
|
|
|
use_iothread = True
|
|
|
|
|
2020-03-10 14:38:28 +03:00
|
|
|
# Attach the backing chain only during completion, with blockdev-snapshot
|
|
|
|
class TestBlockdevMirrorSnapshot(MirrorBaseClass):
|
|
|
|
cmd = 'blockdev-mirror'
|
|
|
|
existing = True
|
|
|
|
target_backing = 'null-co://'
|
|
|
|
target_open_with_backing = False
|
|
|
|
|
|
|
|
def openBacking(self):
|
|
|
|
if not self.target_open_with_backing:
|
2023-10-06 18:41:25 +03:00
|
|
|
self.vm.cmd('blockdev-add', node_name="backing",
|
|
|
|
driver="null-co")
|
|
|
|
self.vm.cmd('blockdev-snapshot', node="backing",
|
|
|
|
overlay="target")
|
2016-06-10 21:57:49 +03:00
|
|
|
|
2020-03-10 14:38:30 +03:00
|
|
|
class TestBlockdevMirrorSnapshotIothread(TestBlockdevMirrorSnapshot):
|
|
|
|
use_iothread = True
|
|
|
|
|
2016-06-10 21:57:49 +03:00
|
|
|
class TestCommit(BaseClass):
|
|
|
|
existing = False
|
|
|
|
|
|
|
|
def testCommit(self):
|
2023-10-06 18:41:25 +03:00
|
|
|
self.vm.cmd('block-commit', job_id='commit-job',
|
|
|
|
device='source', base=back1_img)
|
2016-06-10 21:57:49 +03:00
|
|
|
|
|
|
|
self.vm.event_wait('BLOCK_JOB_READY')
|
|
|
|
|
2023-10-06 18:41:25 +03:00
|
|
|
self.vm.cmd('block-job-complete', device='commit-job')
|
2016-06-10 21:57:49 +03:00
|
|
|
|
|
|
|
self.vm.event_wait('BLOCK_JOB_COMPLETED')
|
|
|
|
|
2020-03-10 14:38:30 +03:00
|
|
|
node = self.findBlockNode(None, 'qdev0')
|
2016-06-10 21:57:49 +03:00
|
|
|
self.assert_qmp(node, 'image' + '/backing-image' * 0 + '/filename',
|
|
|
|
back1_img)
|
|
|
|
self.assert_qmp(node, 'image' + '/backing-image' * 1 + '/filename',
|
|
|
|
back0_img)
|
|
|
|
self.assert_qmp_absent(node, 'image' + '/backing-image' * 2 +
|
|
|
|
'/filename')
|
|
|
|
|
|
|
|
self.assertIntactSourceBackingChain()
|
|
|
|
|
|
|
|
|
|
|
|
BaseClass = None
|
|
|
|
MirrorBaseClass = None
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2019-09-02 22:33:18 +03:00
|
|
|
iotests.main(supported_fmts=['qcow2'],
|
|
|
|
supported_protocols=['file'])
|