xref: /openbmc/qemu/tests/qemu-iotests/216 (revision 7d814059)
17c477526SPhilippe Mathieu-Daudé#!/usr/bin/env python3
23e7a95feSMax Reitz#
33e7a95feSMax Reitz# Copy-on-read tests using a COR filter node
43e7a95feSMax Reitz#
53e7a95feSMax Reitz# Copyright (C) 2018 Red Hat, Inc.
63e7a95feSMax Reitz#
73e7a95feSMax Reitz# This program is free software; you can redistribute it and/or modify
83e7a95feSMax Reitz# it under the terms of the GNU General Public License as published by
93e7a95feSMax Reitz# the Free Software Foundation; either version 2 of the License, or
103e7a95feSMax Reitz# (at your option) any later version.
113e7a95feSMax Reitz#
123e7a95feSMax Reitz# This program is distributed in the hope that it will be useful,
133e7a95feSMax Reitz# but WITHOUT ANY WARRANTY; without even the implied warranty of
143e7a95feSMax Reitz# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
153e7a95feSMax Reitz# GNU General Public License for more details.
163e7a95feSMax Reitz#
173e7a95feSMax Reitz# You should have received a copy of the GNU General Public License
183e7a95feSMax Reitz# along with this program.  If not, see <http://www.gnu.org/licenses/>.
193e7a95feSMax Reitz#
203e7a95feSMax Reitz# Creator/Owner: Max Reitz <mreitz@redhat.com>
213e7a95feSMax Reitz
223e7a95feSMax Reitzimport iotests
23e4ca4e98SMax Reitzfrom iotests import log, qemu_img, qemu_io_silent
243e7a95feSMax Reitz
253e7a95feSMax Reitz# Need backing file support
26*7d814059SJohn Snowiotests.script_initialize(supported_fmts=['qcow2', 'qcow', 'qed', 'vmdk'],
27*7d814059SJohn Snow                          supported_platforms=['linux'])
283e7a95feSMax Reitz
293e7a95feSMax Reitzlog('')
303e7a95feSMax Reitzlog('=== Copy-on-read across nodes ===')
313e7a95feSMax Reitzlog('')
323e7a95feSMax Reitz
333e7a95feSMax Reitz# The old copy-on-read mechanism without a filter node cannot request
343e7a95feSMax Reitz# WRITE_UNCHANGED permissions for its child.  Therefore it just tries
353e7a95feSMax Reitz# to sneak its write by the usual permission system and holds its
363e7a95feSMax Reitz# fingers crossed.  However, that sneaking does not work so well when
373e7a95feSMax Reitz# there is a filter node in the way: That will receive the write
383e7a95feSMax Reitz# request and re-issue a new one to its child, which this time is a
393e7a95feSMax Reitz# proper write request that will make the permission system cough --
403e7a95feSMax Reitz# unless there is someone at the top (like a guest device) that has
413e7a95feSMax Reitz# requested write permissions.
423e7a95feSMax Reitz#
433e7a95feSMax Reitz# A COR filter node, however, can request the proper permissions for
443e7a95feSMax Reitz# its child and therefore is not hit by this issue.
453e7a95feSMax Reitz
463e7a95feSMax Reitzwith iotests.FilePath('base.img') as base_img_path, \
473e7a95feSMax Reitz     iotests.FilePath('top.img') as top_img_path, \
483e7a95feSMax Reitz     iotests.VM() as vm:
493e7a95feSMax Reitz
503e7a95feSMax Reitz    log('--- Setting up images ---')
513e7a95feSMax Reitz    log('')
523e7a95feSMax Reitz
53e4ca4e98SMax Reitz    assert qemu_img('create', '-f', iotests.imgfmt, base_img_path, '64M') == 0
54e4ca4e98SMax Reitz    assert qemu_io_silent(base_img_path, '-c', 'write -P 1 0M 1M') == 0
55e4ca4e98SMax Reitz    assert qemu_img('create', '-f', iotests.imgfmt, '-b', base_img_path,
56e4ca4e98SMax Reitz                    top_img_path) == 0
57e4ca4e98SMax Reitz    assert qemu_io_silent(top_img_path,  '-c', 'write -P 2 1M 1M') == 0
583e7a95feSMax Reitz
59e4ca4e98SMax Reitz    log('Done')
603e7a95feSMax Reitz
613e7a95feSMax Reitz    log('')
623e7a95feSMax Reitz    log('--- Doing COR ---')
633e7a95feSMax Reitz    log('')
643e7a95feSMax Reitz
653e7a95feSMax Reitz    # Compare with e.g. the following:
663e7a95feSMax Reitz    #   vm.add_drive_raw('if=none,node-name=node0,copy-on-read=on,driver=raw,' \
673e7a95feSMax Reitz    #                    'file.driver=%s,file.file.filename=%s' %
683e7a95feSMax Reitz    #                       (iotests.imgfmt, top_img_path))
693e7a95feSMax Reitz    # (Remove the blockdev-add instead.)
703e7a95feSMax Reitz    # ((Not tested here because it hits an assertion in the permission
713e7a95feSMax Reitz    #   system.))
723e7a95feSMax Reitz
733e7a95feSMax Reitz    vm.launch()
743e7a95feSMax Reitz
753e7a95feSMax Reitz    log(vm.qmp('blockdev-add',
763e7a95feSMax Reitz                    node_name='node0',
773e7a95feSMax Reitz                    driver='copy-on-read',
783e7a95feSMax Reitz                    file={
793e7a95feSMax Reitz                        'driver': 'raw',
803e7a95feSMax Reitz                        'file': {
813e7a95feSMax Reitz                            'driver': 'copy-on-read',
823e7a95feSMax Reitz                            'file': {
833e7a95feSMax Reitz                                'driver': 'raw',
843e7a95feSMax Reitz                                'file': {
853e7a95feSMax Reitz                                    'driver': iotests.imgfmt,
863e7a95feSMax Reitz                                    'file': {
873e7a95feSMax Reitz                                        'driver': 'file',
883e7a95feSMax Reitz                                        'filename': top_img_path
893e7a95feSMax Reitz                                    },
903e7a95feSMax Reitz                                    'backing': {
913e7a95feSMax Reitz                                        'driver': iotests.imgfmt,
923e7a95feSMax Reitz                                        'file': {
933e7a95feSMax Reitz                                            'driver': 'file',
943e7a95feSMax Reitz                                            'filename': base_img_path
953e7a95feSMax Reitz                                        }
963e7a95feSMax Reitz                                    }
973e7a95feSMax Reitz                                }
983e7a95feSMax Reitz                            }
993e7a95feSMax Reitz                        }
1003e7a95feSMax Reitz                    }))
1013e7a95feSMax Reitz
1023e7a95feSMax Reitz    # Trigger COR
1033e7a95feSMax Reitz    log(vm.qmp('human-monitor-command',
1043e7a95feSMax Reitz               command_line='qemu-io node0 "read 0 64M"'))
1053e7a95feSMax Reitz
1063e7a95feSMax Reitz    vm.shutdown()
1073e7a95feSMax Reitz
1083e7a95feSMax Reitz    log('')
1093e7a95feSMax Reitz    log('--- Checking COR result ---')
1103e7a95feSMax Reitz    log('')
1113e7a95feSMax Reitz
112e4ca4e98SMax Reitz    assert qemu_io_silent(base_img_path, '-c', 'discard 0 64M') == 0
113e4ca4e98SMax Reitz    assert qemu_io_silent(top_img_path,  '-c', 'read -P 1 0M 1M') == 0
114e4ca4e98SMax Reitz    assert qemu_io_silent(top_img_path,  '-c', 'read -P 2 1M 1M') == 0
115e4ca4e98SMax Reitz
116e4ca4e98SMax Reitz    log('Done')
117