1#!/usr/bin/env python3 2# group: rw quick 3# 4# Copyright (C) Red Hat, Inc. 5# 6# This program is free software; you can redistribute it and/or modify 7# it under the terms of the GNU General Public License as published by 8# the Free Software Foundation; either version 2 of the License, or 9# (at your option) any later version. 10# 11# This program is distributed in the hope that it will be useful, 12# but WITHOUT ANY WARRANTY; without even the implied warranty of 13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14# GNU General Public License for more details. 15# 16# You should have received a copy of the GNU General Public License 17# along with this program. If not, see <http://www.gnu.org/licenses/>. 18# 19# Creator/Owner: Kevin Wolf <kwolf@redhat.com> 20 21import iotests 22 23from iotests import QemuIoInteractive 24from iotests import filter_qemu_io, filter_qtest, filter_qmp_testfiles 25 26iotests.script_initialize(supported_fmts=['generic'], 27 supported_protocols=['file'], 28 supported_platforms=['linux']) 29 30def get_export(node_name='disk-fmt', allow_inactive=None): 31 exp = { 32 'id': 'exp0', 33 'type': 'nbd', 34 'node-name': node_name, 35 'writable': True, 36 } 37 38 if allow_inactive is not None: 39 exp['allow-inactive'] = allow_inactive 40 41 return exp 42 43def node_is_active(_vm, node_name): 44 nodes = _vm.cmd('query-named-block-nodes', flat=True) 45 node = next(n for n in nodes if n['node-name'] == node_name) 46 return node['active'] 47 48with iotests.FilePath('disk.img') as path, \ 49 iotests.FilePath('snap.qcow2') as snap_path, \ 50 iotests.FilePath('snap2.qcow2') as snap2_path, \ 51 iotests.FilePath('target.img') as target_path, \ 52 iotests.FilePath('nbd.sock', base_dir=iotests.sock_dir) as nbd_sock, \ 53 iotests.VM() as vm: 54 55 img_size = '10M' 56 57 iotests.log('Preparing disk...') 58 iotests.qemu_img_create('-f', iotests.imgfmt, path, img_size) 59 iotests.qemu_img_create('-f', iotests.imgfmt, target_path, img_size) 60 61 iotests.qemu_img_create('-f', 'qcow2', '-b', path, '-F', iotests.imgfmt, 62 snap_path) 63 iotests.qemu_img_create('-f', 'qcow2', '-b', snap_path, '-F', 'qcow2', 64 snap2_path) 65 66 iotests.log('Launching VM...') 67 vm.add_blockdev(f'file,node-name=disk-file,filename={path}') 68 vm.add_blockdev(f'{iotests.imgfmt},file=disk-file,node-name=disk-fmt,' 69 'active=off') 70 vm.add_blockdev(f'file,node-name=target-file,filename={target_path}') 71 vm.add_blockdev(f'{iotests.imgfmt},file=target-file,node-name=target-fmt') 72 vm.add_blockdev(f'file,node-name=snap-file,filename={snap_path}') 73 vm.add_blockdev(f'file,node-name=snap2-file,filename={snap2_path}') 74 75 # Actually running the VM activates all images 76 vm.add_paused() 77 78 vm.launch() 79 vm.qmp_log('nbd-server-start', 80 addr={'type': 'unix', 'data':{'path': nbd_sock}}, 81 filters=[filter_qmp_testfiles]) 82 83 iotests.log('\n=== Creating export of inactive node ===') 84 85 iotests.log('\nExports activate nodes without allow-inactive') 86 iotests.log('disk-fmt active: %s' % node_is_active(vm, 'disk-fmt')) 87 vm.qmp_log('block-export-add', **get_export()) 88 iotests.log('disk-fmt active: %s' % node_is_active(vm, 'disk-fmt')) 89 vm.qmp_log('query-block-exports') 90 vm.qmp_log('block-export-del', id='exp0') 91 vm.event_wait('BLOCK_EXPORT_DELETED') 92 vm.qmp_log('query-block-exports') 93 94 iotests.log('\nExports activate nodes with allow-inactive=false') 95 vm.qmp_log('blockdev-set-active', node_name='disk-fmt', active=False) 96 iotests.log('disk-fmt active: %s' % node_is_active(vm, 'disk-fmt')) 97 vm.qmp_log('block-export-add', **get_export(allow_inactive=False)) 98 iotests.log('disk-fmt active: %s' % node_is_active(vm, 'disk-fmt')) 99 vm.qmp_log('query-block-exports') 100 vm.qmp_log('block-export-del', id='exp0') 101 vm.event_wait('BLOCK_EXPORT_DELETED') 102 vm.qmp_log('query-block-exports') 103 104 iotests.log('\nExport leaves nodes inactive with allow-inactive=true') 105 vm.qmp_log('blockdev-set-active', node_name='disk-fmt', active=False) 106 iotests.log('disk-fmt active: %s' % node_is_active(vm, 'disk-fmt')) 107 vm.qmp_log('block-export-add', **get_export(allow_inactive=True)) 108 iotests.log('disk-fmt active: %s' % node_is_active(vm, 'disk-fmt')) 109 vm.qmp_log('query-block-exports') 110 vm.qmp_log('block-export-del', id='exp0') 111 vm.event_wait('BLOCK_EXPORT_DELETED') 112 vm.qmp_log('query-block-exports') 113 114 iotests.log('\n=== Inactivating node with existing export ===') 115 116 iotests.log('\nInactivating nodes with an export fails without ' 117 'allow-inactive') 118 vm.qmp_log('blockdev-set-active', node_name='disk-fmt', active=True) 119 vm.qmp_log('block-export-add', **get_export(node_name='disk-fmt')) 120 vm.qmp_log('blockdev-set-active', node_name='disk-fmt', active=False) 121 iotests.log('disk-fmt active: %s' % node_is_active(vm, 'disk-fmt')) 122 vm.qmp_log('query-block-exports') 123 vm.qmp_log('block-export-del', id='exp0') 124 vm.event_wait('BLOCK_EXPORT_DELETED') 125 vm.qmp_log('query-block-exports') 126 127 iotests.log('\nInactivating nodes with an export fails with ' 128 'allow-inactive=false') 129 vm.qmp_log('blockdev-set-active', node_name='disk-fmt', active=True) 130 vm.qmp_log('block-export-add', 131 **get_export(node_name='disk-fmt', allow_inactive=False)) 132 vm.qmp_log('blockdev-set-active', node_name='disk-fmt', active=False) 133 iotests.log('disk-fmt active: %s' % node_is_active(vm, 'disk-fmt')) 134 vm.qmp_log('query-block-exports') 135 vm.qmp_log('block-export-del', id='exp0') 136 vm.event_wait('BLOCK_EXPORT_DELETED') 137 vm.qmp_log('query-block-exports') 138 139 iotests.log('\nInactivating nodes with an export works with ' 140 'allow-inactive=true') 141 vm.qmp_log('blockdev-set-active', node_name='disk-fmt', active=True) 142 vm.qmp_log('block-export-add', 143 **get_export(node_name='disk-fmt', allow_inactive=True)) 144 vm.qmp_log('blockdev-set-active', node_name='disk-fmt', active=False) 145 iotests.log('disk-fmt active: %s' % node_is_active(vm, 'disk-fmt')) 146 vm.qmp_log('query-block-exports') 147 vm.qmp_log('block-export-del', id='exp0') 148 vm.event_wait('BLOCK_EXPORT_DELETED') 149 vm.qmp_log('query-block-exports') 150 151 iotests.log('\n=== Inactive nodes with parent ===') 152 153 iotests.log('\nInactivating nodes with an active parent fails') 154 vm.qmp_log('blockdev-set-active', node_name='disk-fmt', active=True) 155 vm.qmp_log('blockdev-set-active', node_name='disk-file', active=False) 156 iotests.log('disk-file active: %s' % node_is_active(vm, 'disk-file')) 157 iotests.log('disk-fmt active: %s' % node_is_active(vm, 'disk-fmt')) 158 159 iotests.log('\nInactivating nodes with an inactive parent works') 160 vm.qmp_log('blockdev-set-active', node_name='disk-fmt', active=False) 161 vm.qmp_log('blockdev-set-active', node_name='disk-file', active=False) 162 iotests.log('disk-file active: %s' % node_is_active(vm, 'disk-file')) 163 iotests.log('disk-fmt active: %s' % node_is_active(vm, 'disk-fmt')) 164 165 iotests.log('\nCreating active parent node with an inactive child fails') 166 vm.qmp_log('blockdev-add', driver='raw', file='disk-fmt', 167 node_name='disk-filter') 168 vm.qmp_log('blockdev-add', driver='raw', file='disk-fmt', 169 node_name='disk-filter', active=True) 170 171 iotests.log('\nCreating inactive parent node with an inactive child works') 172 vm.qmp_log('blockdev-add', driver='raw', file='disk-fmt', 173 node_name='disk-filter', active=False) 174 vm.qmp_log('blockdev-del', node_name='disk-filter') 175 176 iotests.log('\n=== Resizing an inactive node ===') 177 vm.qmp_log('block_resize', node_name='disk-fmt', size=16*1024*1024) 178 179 iotests.log('\n=== Taking a snapshot of an inactive node ===') 180 181 iotests.log('\nActive overlay over inactive backing file automatically ' 182 'makes both inactive for compatibility') 183 vm.qmp_log('blockdev-add', driver='qcow2', node_name='snap-fmt', 184 file='snap-file', backing=None) 185 iotests.log('disk-fmt active: %s' % node_is_active(vm, 'disk-fmt')) 186 iotests.log('snap-fmt active: %s' % node_is_active(vm, 'snap-fmt')) 187 vm.qmp_log('blockdev-snapshot', node='disk-fmt', overlay='snap-fmt') 188 iotests.log('disk-fmt active: %s' % node_is_active(vm, 'disk-fmt')) 189 iotests.log('snap-fmt active: %s' % node_is_active(vm, 'snap-fmt')) 190 vm.qmp_log('blockdev-del', node_name='snap-fmt') 191 192 iotests.log('\nInactive overlay over inactive backing file just works') 193 vm.qmp_log('blockdev-add', driver='qcow2', node_name='snap-fmt', 194 file='snap-file', backing=None, active=False) 195 vm.qmp_log('blockdev-snapshot', node='disk-fmt', overlay='snap-fmt') 196 197 iotests.log('\n=== Block jobs with inactive nodes ===') 198 199 iotests.log('\nStreaming into an inactive node') 200 vm.qmp_log('block-stream', device='snap-fmt', 201 filters=[iotests.filter_qmp_generated_node_ids]) 202 203 iotests.log('\nCommitting an inactive root node (active commit)') 204 vm.qmp_log('block-commit', job_id='job0', device='snap-fmt', 205 filters=[iotests.filter_qmp_generated_node_ids]) 206 207 iotests.log('\nCommitting an inactive intermediate node to inactive base') 208 vm.qmp_log('blockdev-add', driver='qcow2', node_name='snap2-fmt', 209 file='snap2-file', backing='snap-fmt', active=False) 210 211 iotests.log('disk-fmt active: %s' % node_is_active(vm, 'disk-fmt')) 212 iotests.log('snap-fmt active: %s' % node_is_active(vm, 'snap-fmt')) 213 iotests.log('snap2-fmt active: %s' % node_is_active(vm, 'snap2-fmt')) 214 215 vm.qmp_log('block-commit', job_id='job0', device='snap2-fmt', 216 top_node='snap-fmt', 217 filters=[iotests.filter_qmp_generated_node_ids]) 218 219 iotests.log('\nCommitting an inactive intermediate node to active base') 220 vm.qmp_log('blockdev-set-active', node_name='disk-fmt', active=True) 221 vm.qmp_log('block-commit', job_id='job0', device='snap2-fmt', 222 top_node='snap-fmt', 223 filters=[iotests.filter_qmp_generated_node_ids]) 224 225 iotests.log('\nMirror from inactive source to active target') 226 vm.qmp_log('blockdev-mirror', job_id='job0', device='snap2-fmt', 227 target='target-fmt', sync='full', 228 filters=[iotests.filter_qmp_generated_node_ids]) 229 230 iotests.log('\nMirror from active source to inactive target') 231 232 iotests.log('disk-fmt active: %s' % node_is_active(vm, 'disk-fmt')) 233 iotests.log('snap-fmt active: %s' % node_is_active(vm, 'snap-fmt')) 234 iotests.log('snap2-fmt active: %s' % node_is_active(vm, 'snap2-fmt')) 235 iotests.log('target-fmt active: %s' % node_is_active(vm, 'target-fmt')) 236 237 # Activating snap2-fmt recursively activates the whole backing chain 238 vm.qmp_log('blockdev-set-active', node_name='snap2-fmt', active=True) 239 vm.qmp_log('blockdev-set-active', node_name='target-fmt', active=False) 240 241 iotests.log('disk-fmt active: %s' % node_is_active(vm, 'disk-fmt')) 242 iotests.log('snap-fmt active: %s' % node_is_active(vm, 'snap-fmt')) 243 iotests.log('snap2-fmt active: %s' % node_is_active(vm, 'snap2-fmt')) 244 iotests.log('target-fmt active: %s' % node_is_active(vm, 'target-fmt')) 245 246 vm.qmp_log('blockdev-mirror', job_id='job0', device='snap2-fmt', 247 target='target-fmt', sync='full', 248 filters=[iotests.filter_qmp_generated_node_ids]) 249 250 iotests.log('\nBackup from active source to inactive target') 251 252 vm.qmp_log('blockdev-backup', job_id='job0', device='snap2-fmt', 253 target='target-fmt', sync='full', 254 filters=[iotests.filter_qmp_generated_node_ids]) 255 256 iotests.log('\nBackup from inactive source to active target') 257 258 # Inactivating snap2-fmt recursively inactivates the whole backing chain 259 vm.qmp_log('blockdev-set-active', node_name='snap2-fmt', active=False) 260 vm.qmp_log('blockdev-set-active', node_name='target-fmt', active=True) 261 262 iotests.log('disk-fmt active: %s' % node_is_active(vm, 'disk-fmt')) 263 iotests.log('snap-fmt active: %s' % node_is_active(vm, 'snap-fmt')) 264 iotests.log('snap2-fmt active: %s' % node_is_active(vm, 'snap2-fmt')) 265 iotests.log('target-fmt active: %s' % node_is_active(vm, 'target-fmt')) 266 267 vm.qmp_log('blockdev-backup', job_id='job0', device='snap2-fmt', 268 target='target-fmt', sync='full', 269 filters=[iotests.filter_qmp_generated_node_ids]) 270 271 iotests.log('\n=== Accessing export on inactive node ===') 272 273 # Use the target node because it has the right image format and isn't the 274 # (read-only) backing file of a qcow2 node 275 vm.qmp_log('blockdev-set-active', node_name='target-fmt', active=False) 276 vm.qmp_log('block-export-add', 277 **get_export(node_name='target-fmt', allow_inactive=True)) 278 279 # The read should succeed, everything else should fail gracefully 280 qemu_io = QemuIoInteractive('-f', 'raw', 281 f'nbd+unix:///target-fmt?socket={nbd_sock}') 282 iotests.log(qemu_io.cmd('read 0 64k'), filters=[filter_qemu_io]) 283 iotests.log(qemu_io.cmd('write 0 64k'), filters=[filter_qemu_io]) 284 iotests.log(qemu_io.cmd('write -z 0 64k'), filters=[filter_qemu_io]) 285 iotests.log(qemu_io.cmd('write -zu 0 64k'), filters=[filter_qemu_io]) 286 iotests.log(qemu_io.cmd('discard 0 64k'), filters=[filter_qemu_io]) 287 iotests.log(qemu_io.cmd('flush'), filters=[filter_qemu_io]) 288 iotests.log(qemu_io.cmd('map'), filters=[filter_qemu_io]) 289 qemu_io.close() 290 291 iotests.log('\n=== Resuming VM activates all images ===') 292 vm.qmp_log('cont') 293 294 iotests.log('disk-fmt active: %s' % node_is_active(vm, 'disk-fmt')) 295 iotests.log('snap-fmt active: %s' % node_is_active(vm, 'snap-fmt')) 296 iotests.log('snap2-fmt active: %s' % node_is_active(vm, 'snap2-fmt')) 297 iotests.log('target-fmt active: %s' % node_is_active(vm, 'target-fmt')) 298 299 iotests.log('\nShutting down...') 300 vm.shutdown() 301 log = vm.get_log() 302 if log: 303 iotests.log(log, [filter_qtest, filter_qemu_io]) 304