1bf03dedeSKevin Wolf#!/usr/bin/env python3 29dd003a9SVladimir Sementsov-Ogievskiy# group: rw backing 3bf03dedeSKevin Wolf# 4bf03dedeSKevin Wolf# Copyright (C) 2019 Red Hat, Inc. 5bf03dedeSKevin Wolf# 6bf03dedeSKevin Wolf# This program is free software; you can redistribute it and/or modify 7bf03dedeSKevin Wolf# it under the terms of the GNU General Public License as published by 8bf03dedeSKevin Wolf# the Free Software Foundation; either version 2 of the License, or 9bf03dedeSKevin Wolf# (at your option) any later version. 10bf03dedeSKevin Wolf# 11bf03dedeSKevin Wolf# This program is distributed in the hope that it will be useful, 12bf03dedeSKevin Wolf# but WITHOUT ANY WARRANTY; without even the implied warranty of 13bf03dedeSKevin Wolf# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14bf03dedeSKevin Wolf# GNU General Public License for more details. 15bf03dedeSKevin Wolf# 16bf03dedeSKevin Wolf# You should have received a copy of the GNU General Public License 17bf03dedeSKevin Wolf# along with this program. If not, see <http://www.gnu.org/licenses/>. 18bf03dedeSKevin Wolf# 19bf03dedeSKevin Wolf# Creator/Owner: Kevin Wolf <kwolf@redhat.com> 20bf03dedeSKevin Wolf# 21bf03dedeSKevin Wolf# Some tests for short backing files and short overlays 22bf03dedeSKevin Wolf 23bf03dedeSKevin Wolfimport iotests 24bf03dedeSKevin Wolf 257d814059SJohn Snowiotests.script_initialize(supported_fmts=['qcow2'], 26b30b8077SVladimir Sementsov-Ogievskiy supported_platforms=['linux'], 27b30b8077SVladimir Sementsov-Ogievskiy unsupported_imgopts=['refcount_bits', 'compat']) 28bf03dedeSKevin Wolf 29bf03dedeSKevin Wolfsize_short = 1 * 1024 * 1024 30bf03dedeSKevin Wolfsize_long = 2 * 1024 * 1024 31bf03dedeSKevin Wolfsize_diff = size_long - size_short 32bf03dedeSKevin Wolf 33bf03dedeSKevin Wolfdef create_chain() -> None: 34*3c8b7358SJohn Snow iotests.qemu_img_create('-f', iotests.imgfmt, base, str(size_long)) 35*3c8b7358SJohn Snow iotests.qemu_img_create('-f', iotests.imgfmt, '-b', base, 36b66ff2c2SEric Blake '-F', iotests.imgfmt, mid, str(size_short)) 37*3c8b7358SJohn Snow iotests.qemu_img_create('-f', iotests.imgfmt, '-b', mid, 38b66ff2c2SEric Blake '-F', iotests.imgfmt, top, str(size_long)) 39bf03dedeSKevin Wolf 40bf03dedeSKevin Wolf iotests.qemu_io_log('-c', 'write -P 1 0 %d' % size_long, base) 41bf03dedeSKevin Wolf 42bf03dedeSKevin Wolfdef create_vm() -> iotests.VM: 43bf03dedeSKevin Wolf vm = iotests.VM() 44bf03dedeSKevin Wolf vm.add_blockdev('file,filename=%s,node-name=base-file' % base) 45bf03dedeSKevin Wolf vm.add_blockdev('%s,file=base-file,node-name=base' % iotests.imgfmt) 46bf03dedeSKevin Wolf vm.add_blockdev('file,filename=%s,node-name=mid-file' % mid) 47bf03dedeSKevin Wolf vm.add_blockdev('%s,file=mid-file,node-name=mid,backing=base' 48bf03dedeSKevin Wolf % iotests.imgfmt) 49bf03dedeSKevin Wolf vm.add_drive(top, 'backing=mid,node-name=top') 50bf03dedeSKevin Wolf return vm 51bf03dedeSKevin Wolf 52bf03dedeSKevin Wolfwith iotests.FilePath('base') as base, \ 53bf03dedeSKevin Wolf iotests.FilePath('mid') as mid, \ 54bf03dedeSKevin Wolf iotests.FilePath('top') as top: 55bf03dedeSKevin Wolf 56bf03dedeSKevin Wolf iotests.log('== Commit tests ==') 57bf03dedeSKevin Wolf 58bf03dedeSKevin Wolf create_chain() 59bf03dedeSKevin Wolf 60bf03dedeSKevin Wolf iotests.log('=== Check visible data ===') 61bf03dedeSKevin Wolf 62bf03dedeSKevin Wolf iotests.qemu_io_log('-c', 'read -P 1 0 %d' % size_short, top) 63bf03dedeSKevin Wolf iotests.qemu_io_log('-c', 'read -P 0 %d %d' % (size_short, size_diff), top) 64bf03dedeSKevin Wolf 65bf03dedeSKevin Wolf iotests.log('=== Checking allocation status ===') 66bf03dedeSKevin Wolf 67bf03dedeSKevin Wolf iotests.qemu_io_log('-c', 'alloc 0 %d' % size_short, 68bf03dedeSKevin Wolf '-c', 'alloc %d %d' % (size_short, size_diff), 69bf03dedeSKevin Wolf base) 70bf03dedeSKevin Wolf 71bf03dedeSKevin Wolf iotests.qemu_io_log('-c', 'alloc 0 %d' % size_short, 72bf03dedeSKevin Wolf '-c', 'alloc %d %d' % (size_short, size_diff), 73bf03dedeSKevin Wolf mid) 74bf03dedeSKevin Wolf 75bf03dedeSKevin Wolf iotests.qemu_io_log('-c', 'alloc 0 %d' % size_short, 76bf03dedeSKevin Wolf '-c', 'alloc %d %d' % (size_short, size_diff), 77bf03dedeSKevin Wolf top) 78bf03dedeSKevin Wolf 79bf03dedeSKevin Wolf iotests.log('=== Checking map ===') 80bf03dedeSKevin Wolf 81bf03dedeSKevin Wolf iotests.qemu_img_log('map', '--output=json', base) 82bf03dedeSKevin Wolf iotests.qemu_img_log('map', '--output=human', base) 83bf03dedeSKevin Wolf iotests.qemu_img_log('map', '--output=json', mid) 84bf03dedeSKevin Wolf iotests.qemu_img_log('map', '--output=human', mid) 85bf03dedeSKevin Wolf iotests.qemu_img_log('map', '--output=json', top) 86bf03dedeSKevin Wolf iotests.qemu_img_log('map', '--output=human', top) 87bf03dedeSKevin Wolf 88bf03dedeSKevin Wolf iotests.log('=== Testing qemu-img commit (top -> mid) ===') 89bf03dedeSKevin Wolf 90bf03dedeSKevin Wolf iotests.qemu_img_log('commit', top) 91bf03dedeSKevin Wolf iotests.img_info_log(mid) 92bf03dedeSKevin Wolf iotests.qemu_io_log('-c', 'read -P 1 0 %d' % size_short, mid) 93bf03dedeSKevin Wolf iotests.qemu_io_log('-c', 'read -P 0 %d %d' % (size_short, size_diff), mid) 94bf03dedeSKevin Wolf 95bf03dedeSKevin Wolf iotests.log('=== Testing HMP commit (top -> mid) ===') 96bf03dedeSKevin Wolf 97bf03dedeSKevin Wolf create_chain() 98bf03dedeSKevin Wolf with create_vm() as vm: 99bf03dedeSKevin Wolf vm.launch() 100bf03dedeSKevin Wolf vm.qmp_log('human-monitor-command', command_line='commit drive0') 101bf03dedeSKevin Wolf 102bf03dedeSKevin Wolf iotests.img_info_log(mid) 103bf03dedeSKevin Wolf iotests.qemu_io_log('-c', 'read -P 1 0 %d' % size_short, mid) 104bf03dedeSKevin Wolf iotests.qemu_io_log('-c', 'read -P 0 %d %d' % (size_short, size_diff), mid) 105bf03dedeSKevin Wolf 106bf03dedeSKevin Wolf iotests.log('=== Testing QMP active commit (top -> mid) ===') 107bf03dedeSKevin Wolf 108bf03dedeSKevin Wolf create_chain() 109bf03dedeSKevin Wolf with create_vm() as vm: 110bf03dedeSKevin Wolf vm.launch() 111bf03dedeSKevin Wolf vm.qmp_log('block-commit', device='top', base_node='mid', 112bf03dedeSKevin Wolf job_id='job0', auto_dismiss=False) 113bf03dedeSKevin Wolf vm.run_job('job0', wait=5) 114bf03dedeSKevin Wolf 115bf03dedeSKevin Wolf iotests.img_info_log(mid) 116bf03dedeSKevin Wolf iotests.qemu_io_log('-c', 'read -P 1 0 %d' % size_short, mid) 117bf03dedeSKevin Wolf iotests.qemu_io_log('-c', 'read -P 0 %d %d' % (size_short, size_diff), mid) 118bf03dedeSKevin Wolf 1194f193168SVladimir Sementsov-Ogievskiy iotests.log('=== Testing qemu-img commit (top -> base) ===') 1204f193168SVladimir Sementsov-Ogievskiy 1214f193168SVladimir Sementsov-Ogievskiy create_chain() 1224f193168SVladimir Sementsov-Ogievskiy iotests.qemu_img_log('commit', '-b', base, top) 1234f193168SVladimir Sementsov-Ogievskiy iotests.img_info_log(base) 1244f193168SVladimir Sementsov-Ogievskiy iotests.qemu_io_log('-c', 'read -P 1 0 %d' % size_short, base) 1254f193168SVladimir Sementsov-Ogievskiy iotests.qemu_io_log('-c', 'read -P 0 %d %d' % (size_short, size_diff), base) 1264f193168SVladimir Sementsov-Ogievskiy 1274f193168SVladimir Sementsov-Ogievskiy iotests.log('=== Testing QMP active commit (top -> base) ===') 1284f193168SVladimir Sementsov-Ogievskiy 1294f193168SVladimir Sementsov-Ogievskiy create_chain() 1304f193168SVladimir Sementsov-Ogievskiy with create_vm() as vm: 1314f193168SVladimir Sementsov-Ogievskiy vm.launch() 1324f193168SVladimir Sementsov-Ogievskiy vm.qmp_log('block-commit', device='top', base_node='base', 1334f193168SVladimir Sementsov-Ogievskiy job_id='job0', auto_dismiss=False) 1344f193168SVladimir Sementsov-Ogievskiy vm.run_job('job0', wait=5) 1354f193168SVladimir Sementsov-Ogievskiy 1364f193168SVladimir Sementsov-Ogievskiy iotests.img_info_log(mid) 1374f193168SVladimir Sementsov-Ogievskiy iotests.qemu_io_log('-c', 'read -P 1 0 %d' % size_short, base) 1384f193168SVladimir Sementsov-Ogievskiy iotests.qemu_io_log('-c', 'read -P 0 %d %d' % (size_short, size_diff), base) 139bf03dedeSKevin Wolf 140bf03dedeSKevin Wolf iotests.log('== Resize tests ==') 141bf03dedeSKevin Wolf 142bf03dedeSKevin Wolf # Use different sizes for different allocation modes: 143bf03dedeSKevin Wolf # 144bf03dedeSKevin Wolf # We want to have at least one test where 32 bit truncation in the size of 145bf03dedeSKevin Wolf # the overlapping area becomes visible. This is covered by the 146bf03dedeSKevin Wolf # prealloc='off' case (1G to 6G is an overlap of 5G). 147bf03dedeSKevin Wolf # 148bf03dedeSKevin Wolf # However, we can only do this for modes that don't preallocate data 149bf03dedeSKevin Wolf # because otherwise we might run out of space on the test host. 150bf03dedeSKevin Wolf # 151bf03dedeSKevin Wolf # We also want to test some unaligned combinations. 152bf03dedeSKevin Wolf for (prealloc, base_size, top_size_old, top_size_new, off) in [ 153bf03dedeSKevin Wolf ('off', '6G', '1G', '8G', '5G'), 154bf03dedeSKevin Wolf ('metadata', '32G', '30G', '33G', '31G'), 155bf03dedeSKevin Wolf ('falloc', '10M', '5M', '15M', '9M'), 156bf03dedeSKevin Wolf ('full', '16M', '8M', '12M', '11M'), 157bf03dedeSKevin Wolf ('off', '384k', '253k', '512k', '253k'), 158bf03dedeSKevin Wolf ('off', '400k', '256k', '512k', '336k'), 159bf03dedeSKevin Wolf ('off', '512k', '256k', '500k', '436k')]: 160bf03dedeSKevin Wolf 161bf03dedeSKevin Wolf iotests.log('=== preallocation=%s ===' % prealloc) 162*3c8b7358SJohn Snow iotests.qemu_img_create('-f', iotests.imgfmt, base, base_size) 163*3c8b7358SJohn Snow iotests.qemu_img_create('-f', iotests.imgfmt, '-b', base, 164b66ff2c2SEric Blake '-F', iotests.imgfmt, top, top_size_old) 165bf03dedeSKevin Wolf iotests.qemu_io_log('-c', 'write -P 1 %s 64k' % off, base) 166bf03dedeSKevin Wolf 167bf03dedeSKevin Wolf # After this, top_size_old to base_size should be allocated/zeroed. 168bf03dedeSKevin Wolf # 169bf03dedeSKevin Wolf # In theory, leaving base_size to top_size_new unallocated would be 170bf03dedeSKevin Wolf # correct, but in practice, if we zero out anything, we zero out 171bf03dedeSKevin Wolf # everything up to top_size_new. 172bf03dedeSKevin Wolf iotests.qemu_img_log('resize', '-f', iotests.imgfmt, 173bf03dedeSKevin Wolf '--preallocation', prealloc, top, top_size_new) 174bf03dedeSKevin Wolf iotests.qemu_io_log('-c', 'read -P 0 %s 64k' % off, top) 175bf03dedeSKevin Wolf iotests.qemu_io_log('-c', 'map', top) 176bf03dedeSKevin Wolf iotests.qemu_img_log('map', '--output=json', top) 177