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'], 26*b30b8077SVladimir Sementsov-Ogievskiy supported_platforms=['linux'], 27*b30b8077SVladimir 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: 34bf03dedeSKevin Wolf iotests.qemu_img_log('create', '-f', iotests.imgfmt, base, 35bf03dedeSKevin Wolf str(size_long)) 36b66ff2c2SEric Blake iotests.qemu_img_log('create', '-f', iotests.imgfmt, '-b', base, 37b66ff2c2SEric Blake '-F', iotests.imgfmt, mid, str(size_short)) 38b66ff2c2SEric Blake iotests.qemu_img_log('create', '-f', iotests.imgfmt, '-b', mid, 39b66ff2c2SEric Blake '-F', iotests.imgfmt, top, str(size_long)) 40bf03dedeSKevin Wolf 41bf03dedeSKevin Wolf iotests.qemu_io_log('-c', 'write -P 1 0 %d' % size_long, base) 42bf03dedeSKevin Wolf 43bf03dedeSKevin Wolfdef create_vm() -> iotests.VM: 44bf03dedeSKevin Wolf vm = iotests.VM() 45bf03dedeSKevin Wolf vm.add_blockdev('file,filename=%s,node-name=base-file' % base) 46bf03dedeSKevin Wolf vm.add_blockdev('%s,file=base-file,node-name=base' % iotests.imgfmt) 47bf03dedeSKevin Wolf vm.add_blockdev('file,filename=%s,node-name=mid-file' % mid) 48bf03dedeSKevin Wolf vm.add_blockdev('%s,file=mid-file,node-name=mid,backing=base' 49bf03dedeSKevin Wolf % iotests.imgfmt) 50bf03dedeSKevin Wolf vm.add_drive(top, 'backing=mid,node-name=top') 51bf03dedeSKevin Wolf return vm 52bf03dedeSKevin Wolf 53bf03dedeSKevin Wolfwith iotests.FilePath('base') as base, \ 54bf03dedeSKevin Wolf iotests.FilePath('mid') as mid, \ 55bf03dedeSKevin Wolf iotests.FilePath('top') as top: 56bf03dedeSKevin Wolf 57bf03dedeSKevin Wolf iotests.log('== Commit tests ==') 58bf03dedeSKevin Wolf 59bf03dedeSKevin Wolf create_chain() 60bf03dedeSKevin Wolf 61bf03dedeSKevin Wolf iotests.log('=== Check visible data ===') 62bf03dedeSKevin Wolf 63bf03dedeSKevin Wolf iotests.qemu_io_log('-c', 'read -P 1 0 %d' % size_short, top) 64bf03dedeSKevin Wolf iotests.qemu_io_log('-c', 'read -P 0 %d %d' % (size_short, size_diff), top) 65bf03dedeSKevin Wolf 66bf03dedeSKevin Wolf iotests.log('=== Checking allocation status ===') 67bf03dedeSKevin Wolf 68bf03dedeSKevin Wolf iotests.qemu_io_log('-c', 'alloc 0 %d' % size_short, 69bf03dedeSKevin Wolf '-c', 'alloc %d %d' % (size_short, size_diff), 70bf03dedeSKevin Wolf base) 71bf03dedeSKevin Wolf 72bf03dedeSKevin Wolf iotests.qemu_io_log('-c', 'alloc 0 %d' % size_short, 73bf03dedeSKevin Wolf '-c', 'alloc %d %d' % (size_short, size_diff), 74bf03dedeSKevin Wolf mid) 75bf03dedeSKevin Wolf 76bf03dedeSKevin Wolf iotests.qemu_io_log('-c', 'alloc 0 %d' % size_short, 77bf03dedeSKevin Wolf '-c', 'alloc %d %d' % (size_short, size_diff), 78bf03dedeSKevin Wolf top) 79bf03dedeSKevin Wolf 80bf03dedeSKevin Wolf iotests.log('=== Checking map ===') 81bf03dedeSKevin Wolf 82bf03dedeSKevin Wolf iotests.qemu_img_log('map', '--output=json', base) 83bf03dedeSKevin Wolf iotests.qemu_img_log('map', '--output=human', base) 84bf03dedeSKevin Wolf iotests.qemu_img_log('map', '--output=json', mid) 85bf03dedeSKevin Wolf iotests.qemu_img_log('map', '--output=human', mid) 86bf03dedeSKevin Wolf iotests.qemu_img_log('map', '--output=json', top) 87bf03dedeSKevin Wolf iotests.qemu_img_log('map', '--output=human', top) 88bf03dedeSKevin Wolf 89bf03dedeSKevin Wolf iotests.log('=== Testing qemu-img commit (top -> mid) ===') 90bf03dedeSKevin Wolf 91bf03dedeSKevin Wolf iotests.qemu_img_log('commit', top) 92bf03dedeSKevin Wolf iotests.img_info_log(mid) 93bf03dedeSKevin Wolf iotests.qemu_io_log('-c', 'read -P 1 0 %d' % size_short, mid) 94bf03dedeSKevin Wolf iotests.qemu_io_log('-c', 'read -P 0 %d %d' % (size_short, size_diff), mid) 95bf03dedeSKevin Wolf 96bf03dedeSKevin Wolf iotests.log('=== Testing HMP commit (top -> mid) ===') 97bf03dedeSKevin Wolf 98bf03dedeSKevin Wolf create_chain() 99bf03dedeSKevin Wolf with create_vm() as vm: 100bf03dedeSKevin Wolf vm.launch() 101bf03dedeSKevin Wolf vm.qmp_log('human-monitor-command', command_line='commit drive0') 102bf03dedeSKevin Wolf 103bf03dedeSKevin Wolf iotests.img_info_log(mid) 104bf03dedeSKevin Wolf iotests.qemu_io_log('-c', 'read -P 1 0 %d' % size_short, mid) 105bf03dedeSKevin Wolf iotests.qemu_io_log('-c', 'read -P 0 %d %d' % (size_short, size_diff), mid) 106bf03dedeSKevin Wolf 107bf03dedeSKevin Wolf iotests.log('=== Testing QMP active commit (top -> mid) ===') 108bf03dedeSKevin Wolf 109bf03dedeSKevin Wolf create_chain() 110bf03dedeSKevin Wolf with create_vm() as vm: 111bf03dedeSKevin Wolf vm.launch() 112bf03dedeSKevin Wolf vm.qmp_log('block-commit', device='top', base_node='mid', 113bf03dedeSKevin Wolf job_id='job0', auto_dismiss=False) 114bf03dedeSKevin Wolf vm.run_job('job0', wait=5) 115bf03dedeSKevin Wolf 116bf03dedeSKevin Wolf iotests.img_info_log(mid) 117bf03dedeSKevin Wolf iotests.qemu_io_log('-c', 'read -P 1 0 %d' % size_short, mid) 118bf03dedeSKevin Wolf iotests.qemu_io_log('-c', 'read -P 0 %d %d' % (size_short, size_diff), mid) 119bf03dedeSKevin Wolf 1204f193168SVladimir Sementsov-Ogievskiy iotests.log('=== Testing qemu-img commit (top -> base) ===') 1214f193168SVladimir Sementsov-Ogievskiy 1224f193168SVladimir Sementsov-Ogievskiy create_chain() 1234f193168SVladimir Sementsov-Ogievskiy iotests.qemu_img_log('commit', '-b', base, top) 1244f193168SVladimir Sementsov-Ogievskiy iotests.img_info_log(base) 1254f193168SVladimir Sementsov-Ogievskiy iotests.qemu_io_log('-c', 'read -P 1 0 %d' % size_short, base) 1264f193168SVladimir Sementsov-Ogievskiy iotests.qemu_io_log('-c', 'read -P 0 %d %d' % (size_short, size_diff), base) 1274f193168SVladimir Sementsov-Ogievskiy 1284f193168SVladimir Sementsov-Ogievskiy iotests.log('=== Testing QMP active commit (top -> base) ===') 1294f193168SVladimir Sementsov-Ogievskiy 1304f193168SVladimir Sementsov-Ogievskiy create_chain() 1314f193168SVladimir Sementsov-Ogievskiy with create_vm() as vm: 1324f193168SVladimir Sementsov-Ogievskiy vm.launch() 1334f193168SVladimir Sementsov-Ogievskiy vm.qmp_log('block-commit', device='top', base_node='base', 1344f193168SVladimir Sementsov-Ogievskiy job_id='job0', auto_dismiss=False) 1354f193168SVladimir Sementsov-Ogievskiy vm.run_job('job0', wait=5) 1364f193168SVladimir Sementsov-Ogievskiy 1374f193168SVladimir Sementsov-Ogievskiy iotests.img_info_log(mid) 1384f193168SVladimir Sementsov-Ogievskiy iotests.qemu_io_log('-c', 'read -P 1 0 %d' % size_short, base) 1394f193168SVladimir Sementsov-Ogievskiy iotests.qemu_io_log('-c', 'read -P 0 %d %d' % (size_short, size_diff), base) 140bf03dedeSKevin Wolf 141bf03dedeSKevin Wolf iotests.log('== Resize tests ==') 142bf03dedeSKevin Wolf 143bf03dedeSKevin Wolf # Use different sizes for different allocation modes: 144bf03dedeSKevin Wolf # 145bf03dedeSKevin Wolf # We want to have at least one test where 32 bit truncation in the size of 146bf03dedeSKevin Wolf # the overlapping area becomes visible. This is covered by the 147bf03dedeSKevin Wolf # prealloc='off' case (1G to 6G is an overlap of 5G). 148bf03dedeSKevin Wolf # 149bf03dedeSKevin Wolf # However, we can only do this for modes that don't preallocate data 150bf03dedeSKevin Wolf # because otherwise we might run out of space on the test host. 151bf03dedeSKevin Wolf # 152bf03dedeSKevin Wolf # We also want to test some unaligned combinations. 153bf03dedeSKevin Wolf for (prealloc, base_size, top_size_old, top_size_new, off) in [ 154bf03dedeSKevin Wolf ('off', '6G', '1G', '8G', '5G'), 155bf03dedeSKevin Wolf ('metadata', '32G', '30G', '33G', '31G'), 156bf03dedeSKevin Wolf ('falloc', '10M', '5M', '15M', '9M'), 157bf03dedeSKevin Wolf ('full', '16M', '8M', '12M', '11M'), 158bf03dedeSKevin Wolf ('off', '384k', '253k', '512k', '253k'), 159bf03dedeSKevin Wolf ('off', '400k', '256k', '512k', '336k'), 160bf03dedeSKevin Wolf ('off', '512k', '256k', '500k', '436k')]: 161bf03dedeSKevin Wolf 162bf03dedeSKevin Wolf iotests.log('=== preallocation=%s ===' % prealloc) 163bf03dedeSKevin Wolf iotests.qemu_img_log('create', '-f', iotests.imgfmt, base, base_size) 164b66ff2c2SEric Blake iotests.qemu_img_log('create', '-f', iotests.imgfmt, '-b', base, 165b66ff2c2SEric Blake '-F', iotests.imgfmt, top, top_size_old) 166bf03dedeSKevin Wolf iotests.qemu_io_log('-c', 'write -P 1 %s 64k' % off, base) 167bf03dedeSKevin Wolf 168bf03dedeSKevin Wolf # After this, top_size_old to base_size should be allocated/zeroed. 169bf03dedeSKevin Wolf # 170bf03dedeSKevin Wolf # In theory, leaving base_size to top_size_new unallocated would be 171bf03dedeSKevin Wolf # correct, but in practice, if we zero out anything, we zero out 172bf03dedeSKevin Wolf # everything up to top_size_new. 173bf03dedeSKevin Wolf iotests.qemu_img_log('resize', '-f', iotests.imgfmt, 174bf03dedeSKevin Wolf '--preallocation', prealloc, top, top_size_new) 175bf03dedeSKevin Wolf iotests.qemu_io_log('-c', 'read -P 0 %s 64k' % off, top) 176bf03dedeSKevin Wolf iotests.qemu_io_log('-c', 'map', top) 177bf03dedeSKevin Wolf iotests.qemu_img_log('map', '--output=json', top) 178