4 # Copyright (C) 2019 Red Hat, Inc.
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.
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.
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/>.
19 # Creator/Owner: Kevin Wolf <kwolf@redhat.com>
21 # Some tests for short backing files and short overlays
25 iotests.script_initialize(supported_fmts=['qcow2'],
26 supported_platforms=['linux'])
28 size_short = 1 * 1024 * 1024
29 size_long = 2 * 1024 * 1024
30 size_diff = size_long - size_short
32 def create_chain() -> None:
33 iotests.qemu_img_log('create', '-f', iotests.imgfmt, base,
35 iotests.qemu_img_log('create', '-f', iotests.imgfmt, '-b', base,
36 '-F', iotests.imgfmt, mid, str(size_short))
37 iotests.qemu_img_log('create', '-f', iotests.imgfmt, '-b', mid,
38 '-F', iotests.imgfmt, top, str(size_long))
40 iotests.qemu_io_log('-c', 'write -P 1 0 %d' % size_long, base)
42 def create_vm() -> iotests.VM:
44 vm.add_blockdev('file,filename=%s,node-name=base-file' % base)
45 vm.add_blockdev('%s,file=base-file,node-name=base' % iotests.imgfmt)
46 vm.add_blockdev('file,filename=%s,node-name=mid-file' % mid)
47 vm.add_blockdev('%s,file=mid-file,node-name=mid,backing=base'
49 vm.add_drive(top, 'backing=mid,node-name=top')
52 with iotests.FilePath('base') as base, \
53 iotests.FilePath('mid') as mid, \
54 iotests.FilePath('top') as top:
56 iotests.log('== Commit tests ==')
60 iotests.log('=== Check visible data ===')
62 iotests.qemu_io_log('-c', 'read -P 1 0 %d' % size_short, top)
63 iotests.qemu_io_log('-c', 'read -P 0 %d %d' % (size_short, size_diff), top)
65 iotests.log('=== Checking allocation status ===')
67 iotests.qemu_io_log('-c', 'alloc 0 %d' % size_short,
68 '-c', 'alloc %d %d' % (size_short, size_diff),
71 iotests.qemu_io_log('-c', 'alloc 0 %d' % size_short,
72 '-c', 'alloc %d %d' % (size_short, size_diff),
75 iotests.qemu_io_log('-c', 'alloc 0 %d' % size_short,
76 '-c', 'alloc %d %d' % (size_short, size_diff),
79 iotests.log('=== Checking map ===')
81 iotests.qemu_img_log('map', '--output=json', base)
82 iotests.qemu_img_log('map', '--output=human', base)
83 iotests.qemu_img_log('map', '--output=json', mid)
84 iotests.qemu_img_log('map', '--output=human', mid)
85 iotests.qemu_img_log('map', '--output=json', top)
86 iotests.qemu_img_log('map', '--output=human', top)
88 iotests.log('=== Testing qemu-img commit (top -> mid) ===')
90 iotests.qemu_img_log('commit', top)
91 iotests.img_info_log(mid)
92 iotests.qemu_io_log('-c', 'read -P 1 0 %d' % size_short, mid)
93 iotests.qemu_io_log('-c', 'read -P 0 %d %d' % (size_short, size_diff), mid)
95 iotests.log('=== Testing HMP commit (top -> mid) ===')
98 with create_vm() as vm:
100 vm.qmp_log('human-monitor-command', command_line='commit drive0')
102 iotests.img_info_log(mid)
103 iotests.qemu_io_log('-c', 'read -P 1 0 %d' % size_short, mid)
104 iotests.qemu_io_log('-c', 'read -P 0 %d %d' % (size_short, size_diff), mid)
106 iotests.log('=== Testing QMP active commit (top -> mid) ===')
109 with create_vm() as vm:
111 vm.qmp_log('block-commit', device='top', base_node='mid',
112 job_id='job0', auto_dismiss=False)
113 vm.run_job('job0', wait=5)
115 iotests.img_info_log(mid)
116 iotests.qemu_io_log('-c', 'read -P 1 0 %d' % size_short, mid)
117 iotests.qemu_io_log('-c', 'read -P 0 %d %d' % (size_short, size_diff), mid)
119 iotests.log('=== Testing qemu-img commit (top -> base) ===')
122 iotests.qemu_img_log('commit', '-b', base, top)
123 iotests.img_info_log(base)
124 iotests.qemu_io_log('-c', 'read -P 1 0 %d' % size_short, base)
125 iotests.qemu_io_log('-c', 'read -P 0 %d %d' % (size_short, size_diff), base)
127 iotests.log('=== Testing QMP active commit (top -> base) ===')
130 with create_vm() as vm:
132 vm.qmp_log('block-commit', device='top', base_node='base',
133 job_id='job0', auto_dismiss=False)
134 vm.run_job('job0', wait=5)
136 iotests.img_info_log(mid)
137 iotests.qemu_io_log('-c', 'read -P 1 0 %d' % size_short, base)
138 iotests.qemu_io_log('-c', 'read -P 0 %d %d' % (size_short, size_diff), base)
140 iotests.log('== Resize tests ==')
142 # Use different sizes for different allocation modes:
144 # We want to have at least one test where 32 bit truncation in the size of
145 # the overlapping area becomes visible. This is covered by the
146 # prealloc='off' case (1G to 6G is an overlap of 5G).
148 # However, we can only do this for modes that don't preallocate data
149 # because otherwise we might run out of space on the test host.
151 # We also want to test some unaligned combinations.
152 for (prealloc, base_size, top_size_old, top_size_new, off) in [
153 ('off', '6G', '1G', '8G', '5G'),
154 ('metadata', '32G', '30G', '33G', '31G'),
155 ('falloc', '10M', '5M', '15M', '9M'),
156 ('full', '16M', '8M', '12M', '11M'),
157 ('off', '384k', '253k', '512k', '253k'),
158 ('off', '400k', '256k', '512k', '336k'),
159 ('off', '512k', '256k', '500k', '436k')]:
161 iotests.log('=== preallocation=%s ===' % prealloc)
162 iotests.qemu_img_log('create', '-f', iotests.imgfmt, base, base_size)
163 iotests.qemu_img_log('create', '-f', iotests.imgfmt, '-b', base,
164 '-F', iotests.imgfmt, top, top_size_old)
165 iotests.qemu_io_log('-c', 'write -P 1 %s 64k' % off, base)
167 # After this, top_size_old to base_size should be allocated/zeroed.
169 # In theory, leaving base_size to top_size_new unallocated would be
170 # correct, but in practice, if we zero out anything, we zero out
171 # everything up to top_size_new.
172 iotests.qemu_img_log('resize', '-f', iotests.imgfmt,
173 '--preallocation', prealloc, top, top_size_new)
174 iotests.qemu_io_log('-c', 'read -P 0 %s 64k' % off, top)
175 iotests.qemu_io_log('-c', 'map', top)
176 iotests.qemu_img_log('map', '--output=json', top)