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