xref: /openbmc/qemu/tests/qemu-iotests/298 (revision 2e1cacfb)
1#!/usr/bin/env python3
2#
3# Test for preallocate filter
4#
5# Copyright (c) 2020 Virtuozzo International GmbH.
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19#
20
21import os
22import iotests
23
24MiB = 1024 * 1024
25disk = os.path.join(iotests.test_dir, 'disk')
26overlay = os.path.join(iotests.test_dir, 'overlay')
27refdisk = os.path.join(iotests.test_dir, 'refdisk')
28drive_opts = f'node-name=disk,driver={iotests.imgfmt},' \
29    f'file.node-name=filter,file.driver=preallocate,' \
30    f'file.file.node-name=file,file.file.filename={disk}'
31
32
33class TestPreallocateBase(iotests.QMPTestCase):
34    def setUp(self):
35        iotests.qemu_img_create('-f', iotests.imgfmt, disk, str(10 * MiB))
36
37    def tearDown(self):
38        try:
39            self.check_small()
40            check = iotests.qemu_img_check(disk)
41            self.assertFalse('leaks' in check)
42            self.assertFalse('corruptions' in check)
43            self.assertEqual(check['check-errors'], 0)
44        finally:
45            os.remove(disk)
46
47    def check_big(self):
48        self.assertTrue(os.path.getsize(disk) > 100 * MiB)
49
50    def check_small(self):
51        self.assertTrue(os.path.getsize(disk) < 10 * MiB)
52
53
54class TestQemuImg(TestPreallocateBase):
55    def test_qemu_img(self):
56        p = iotests.QemuIoInteractive('--image-opts', drive_opts)
57
58        p.cmd('write 0 1M')
59        p.cmd('flush')
60
61        self.check_big()
62
63        p.close()
64
65
66class TestPreallocateFilter(TestPreallocateBase):
67    def setUp(self):
68        super().setUp()
69        self.vm = iotests.VM().add_drive(path=None, opts=drive_opts)
70        self.vm.launch()
71
72    def tearDown(self):
73        self.vm.shutdown()
74        super().tearDown()
75
76    def test_prealloc(self):
77        self.vm.hmp_qemu_io('drive0', 'write 0 1M')
78        self.check_big()
79
80    def test_external_snapshot(self):
81        self.test_prealloc()
82
83        self.vm.cmd('blockdev-snapshot-sync', node_name='disk',
84                    snapshot_file=overlay,
85                    snapshot_node_name='overlay')
86
87        # on reopen to  r-o base preallocation should be dropped
88        self.check_small()
89
90        self.vm.hmp_qemu_io('drive0', 'write 1M 1M')
91
92        self.vm.cmd('block-commit', device='overlay')
93        self.complete_and_wait()
94
95        # commit of new megabyte should trigger preallocation
96        self.check_big()
97
98    def test_reopen_opts(self):
99        self.vm.cmd('blockdev-reopen', options=[{
100            'node-name': 'disk',
101            'driver': iotests.imgfmt,
102            'file': {
103                'node-name': 'filter',
104                'driver': 'preallocate',
105                'prealloc-size': 20 * MiB,
106                'prealloc-align': 5 * MiB,
107                'file': {
108                    'node-name': 'file',
109                    'driver': 'file',
110                    'filename': disk
111                }
112            }
113        }])
114
115        self.vm.hmp_qemu_io('drive0', 'write 0 1M')
116        self.assertTrue(os.path.getsize(disk) == 25 * MiB)
117
118
119class TestTruncate(iotests.QMPTestCase):
120    def setUp(self):
121        iotests.qemu_img_create('-f', iotests.imgfmt, disk, str(10 * MiB))
122        iotests.qemu_img_create('-f', iotests.imgfmt, refdisk, str(10 * MiB))
123
124    def tearDown(self):
125        os.remove(disk)
126        os.remove(refdisk)
127
128    def do_test(self, prealloc_mode, new_size):
129        iotests.qemu_io('--image-opts', '-c', 'write 0 10M', '-c',
130                        f'truncate -m {prealloc_mode} {new_size}',
131                        drive_opts)
132
133        iotests.qemu_io('-f', iotests.imgfmt, '-c', 'write 0 10M',
134                        '-c', f'truncate -m {prealloc_mode} {new_size}',
135                        refdisk)
136
137        stat = os.stat(disk)
138        refstat = os.stat(refdisk)
139
140        # The preallocate filter may keep cluster alignment when shrinking,
141        # so ignore small differences
142        self.assertLess(abs(stat.st_size - refstat.st_size), 64 * 1024)
143
144        # Preallocate filter may leak some internal clusters (for example, if
145        # guest write far over EOF, skipping some clusters - they will remain
146        # fallocated, preallocate filter don't care about such leaks, it drops
147        # only trailing preallocation.
148        self.assertLess(abs(stat.st_blocks - refstat.st_blocks) * 512,
149                        1024 * 1024)
150
151    def test_real_shrink(self):
152        self.do_test('off', '5M')
153
154    def test_truncate_inside_preallocated_area__falloc(self):
155        self.do_test('falloc', '50M')
156
157    def test_truncate_inside_preallocated_area__metadata(self):
158        self.do_test('metadata', '50M')
159
160    def test_truncate_inside_preallocated_area__full(self):
161        self.do_test('full', '50M')
162
163    def test_truncate_inside_preallocated_area__off(self):
164        self.do_test('off', '50M')
165
166    def test_truncate_over_preallocated_area__falloc(self):
167        self.do_test('falloc', '150M')
168
169    def test_truncate_over_preallocated_area__metadata(self):
170        self.do_test('metadata', '150M')
171
172    def test_truncate_over_preallocated_area__full(self):
173        self.do_test('full', '150M')
174
175    def test_truncate_over_preallocated_area__off(self):
176        self.do_test('off', '150M')
177
178
179if __name__ == '__main__':
180    iotests.main(supported_fmts=['qcow2'], required_fmts=['preallocate'])
181