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