xref: /openbmc/qemu/tests/qemu-iotests/274 (revision 7d87775f)
1#!/usr/bin/env python3
2# group: rw backing
3#
4# Copyright (C) 2019 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#
21# Some tests for short backing files and short overlays
22
23import iotests
24
25iotests.script_initialize(supported_fmts=['qcow2'],
26                          supported_platforms=['linux'],
27                          unsupported_imgopts=['refcount_bits', 'compat'])
28
29size_short = 1 * 1024 * 1024
30size_long = 2 * 1024 * 1024
31size_diff = size_long - size_short
32
33def create_chain() -> None:
34    iotests.qemu_img_create('-f', iotests.imgfmt, base, str(size_long))
35    iotests.qemu_img_create('-f', iotests.imgfmt, '-b', base,
36                            '-F', iotests.imgfmt, mid, str(size_short))
37    iotests.qemu_img_create('-f', iotests.imgfmt, '-b', mid,
38                            '-F', iotests.imgfmt, top, str(size_long))
39
40    iotests.qemu_io_log('-c', 'write -P 1 0 %d' % size_long, base)
41
42def create_vm() -> iotests.VM:
43    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'
48                    % iotests.imgfmt)
49    vm.add_drive(top, 'backing=mid,node-name=top')
50    return vm
51
52with iotests.FilePath('base') as base, \
53     iotests.FilePath('mid') as mid, \
54     iotests.FilePath('top') as top:
55
56    iotests.log('== Commit tests ==')
57
58    create_chain()
59
60    iotests.log('=== Check visible data ===')
61
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)
64
65    iotests.log('=== Checking allocation status ===')
66
67    iotests.qemu_io_log('-c', 'alloc 0 %d' % size_short,
68                        '-c', 'alloc %d %d' % (size_short, size_diff),
69                        base)
70
71    iotests.qemu_io_log('-c', 'alloc 0 %d' % size_short,
72                        '-c', 'alloc %d %d' % (size_short, size_diff),
73                        mid)
74
75    iotests.qemu_io_log('-c', 'alloc 0 %d' % size_short,
76                        '-c', 'alloc %d %d' % (size_short, size_diff),
77                        top)
78
79    iotests.log('=== Checking map ===')
80
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)
87
88    iotests.log('=== Testing qemu-img commit (top -> mid) ===')
89
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)
94
95    iotests.log('=== Testing HMP commit (top -> mid) ===')
96
97    create_chain()
98    with create_vm() as vm:
99        vm.launch()
100        vm.qmp_log('human-monitor-command', command_line='commit drive0')
101
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)
105
106    iotests.log('=== Testing QMP active commit (top -> mid) ===')
107
108    create_chain()
109    with create_vm() as vm:
110        vm.launch()
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)
114
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)
118
119    iotests.log('=== Testing qemu-img commit (top -> base) ===')
120
121    create_chain()
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)
126
127    iotests.log('=== Testing QMP active commit (top -> base) ===')
128
129    create_chain()
130    with create_vm() as vm:
131        vm.launch()
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)
135
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)
139
140    iotests.log('== Resize tests ==')
141
142    # Use different sizes for different allocation modes:
143    #
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).
147    #
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.
150    #
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')]:
160
161        iotests.log('=== preallocation=%s ===' % prealloc)
162        iotests.qemu_img_create('-f', iotests.imgfmt, base, base_size)
163        iotests.qemu_img_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)
166
167        # After this, top_size_old to base_size should be allocated/zeroed.
168        #
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)
177