xref: /openbmc/qemu/tests/qemu-iotests/216 (revision 4a4ff4c5)
1#!/usr/bin/env python
2#
3# Copy-on-read tests using a COR filter node
4#
5# Copyright (C) 2018 Red Hat, Inc.
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19#
20# Creator/Owner: Max Reitz <mreitz@redhat.com>
21
22import iotests
23from iotests import log, qemu_img_pipe, qemu_io, filter_qemu_io
24
25# Need backing file support
26iotests.verify_image_format(supported_fmts=['qcow2', 'qcow', 'qed', 'vmdk'])
27iotests.verify_platform(['linux'])
28
29log('')
30log('=== Copy-on-read across nodes ===')
31log('')
32
33# The old copy-on-read mechanism without a filter node cannot request
34# WRITE_UNCHANGED permissions for its child.  Therefore it just tries
35# to sneak its write by the usual permission system and holds its
36# fingers crossed.  However, that sneaking does not work so well when
37# there is a filter node in the way: That will receive the write
38# request and re-issue a new one to its child, which this time is a
39# proper write request that will make the permission system cough --
40# unless there is someone at the top (like a guest device) that has
41# requested write permissions.
42#
43# A COR filter node, however, can request the proper permissions for
44# its child and therefore is not hit by this issue.
45
46with iotests.FilePath('base.img') as base_img_path, \
47     iotests.FilePath('top.img') as top_img_path, \
48     iotests.VM() as vm:
49
50    log('--- Setting up images ---')
51    log('')
52
53    qemu_img_pipe('create', '-f', iotests.imgfmt, base_img_path, '64M')
54
55    log(filter_qemu_io(qemu_io(base_img_path, '-c', 'write -P 1 0M 1M')))
56
57    qemu_img_pipe('create', '-f', iotests.imgfmt, '-b', base_img_path,
58                  top_img_path)
59
60    log(filter_qemu_io(qemu_io(top_img_path,  '-c', 'write -P 2 1M 1M')))
61
62    log('')
63    log('--- Doing COR ---')
64    log('')
65
66    # Compare with e.g. the following:
67    #   vm.add_drive_raw('if=none,node-name=node0,copy-on-read=on,driver=raw,' \
68    #                    'file.driver=%s,file.file.filename=%s' %
69    #                       (iotests.imgfmt, top_img_path))
70    # (Remove the blockdev-add instead.)
71    # ((Not tested here because it hits an assertion in the permission
72    #   system.))
73
74    vm.launch()
75
76    log(vm.qmp('blockdev-add',
77                    node_name='node0',
78                    driver='copy-on-read',
79                    file={
80                        'driver': 'raw',
81                        'file': {
82                            'driver': 'copy-on-read',
83                            'file': {
84                                'driver': 'raw',
85                                'file': {
86                                    'driver': iotests.imgfmt,
87                                    'file': {
88                                        'driver': 'file',
89                                        'filename': top_img_path
90                                    },
91                                    'backing': {
92                                        'driver': iotests.imgfmt,
93                                        'file': {
94                                            'driver': 'file',
95                                            'filename': base_img_path
96                                        }
97                                    }
98                                }
99                            }
100                        }
101                    }))
102
103    # Trigger COR
104    log(vm.qmp('human-monitor-command',
105               command_line='qemu-io node0 "read 0 64M"'))
106
107    vm.shutdown()
108
109    log('')
110    log('--- Checking COR result ---')
111    log('')
112
113    log(filter_qemu_io(qemu_io(base_img_path, '-c', 'discard 0 64M')))
114    log(filter_qemu_io(qemu_io(top_img_path,  '-c', 'read -P 1 0M 1M')))
115    log(filter_qemu_io(qemu_io(top_img_path,  '-c', 'read -P 2 1M 1M')))
116