3 # Test for preallocate filter
5 # Copyright (c) 2020 Virtuozzo International GmbH.
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.
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.
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/>.
25 disk = os.path.join(iotests.test_dir, 'disk')
26 overlay = os.path.join(iotests.test_dir, 'overlay')
27 refdisk = os.path.join(iotests.test_dir, 'refdisk')
28 drive_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}'
33 class TestPreallocateBase(iotests.QMPTestCase):
35 iotests.qemu_img_create('-f', iotests.imgfmt, disk, str(10 * MiB))
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)
48 self.assertTrue(os.path.getsize(disk) > 100 * MiB)
50 def check_small(self):
51 self.assertTrue(os.path.getsize(disk) < 10 * MiB)
54 class TestQemuImg(TestPreallocateBase):
55 def test_qemu_img(self):
56 p = iotests.QemuIoInteractive('--image-opts', drive_opts)
66 class TestPreallocateFilter(TestPreallocateBase):
69 self.vm = iotests.VM().add_drive(path=None, opts=drive_opts)
76 def test_prealloc(self):
77 self.vm.hmp_qemu_io('drive0', 'write 0 1M')
80 def test_external_snapshot(self):
83 result = self.vm.qmp('blockdev-snapshot-sync', node_name='disk',
84 snapshot_file=overlay,
85 snapshot_node_name='overlay')
86 self.assert_qmp(result, 'return', {})
88 # on reopen to r-o base preallocation should be dropped
91 self.vm.hmp_qemu_io('drive0', 'write 1M 1M')
93 result = self.vm.qmp('block-commit', device='overlay')
94 self.assert_qmp(result, 'return', {})
95 self.complete_and_wait()
97 # commit of new megabyte should trigger preallocation
100 def test_reopen_opts(self):
101 result = self.vm.qmp('blockdev-reopen', options=[{
103 'driver': iotests.imgfmt,
105 'node-name': 'filter',
106 'driver': 'preallocate',
107 'prealloc-size': 20 * MiB,
108 'prealloc-align': 5 * MiB,
116 self.assert_qmp(result, 'return', {})
118 self.vm.hmp_qemu_io('drive0', 'write 0 1M')
119 self.assertTrue(os.path.getsize(disk) == 25 * MiB)
122 class TestTruncate(iotests.QMPTestCase):
124 iotests.qemu_img_create('-f', iotests.imgfmt, disk, str(10 * MiB))
125 iotests.qemu_img_create('-f', iotests.imgfmt, refdisk, str(10 * MiB))
131 def do_test(self, prealloc_mode, new_size):
132 ret = iotests.qemu_io_silent('--image-opts', '-c', 'write 0 10M', '-c',
133 f'truncate -m {prealloc_mode} {new_size}',
135 self.assertEqual(ret, 0)
137 ret = iotests.qemu_io_silent('-f', iotests.imgfmt, '-c', 'write 0 10M',
139 f'truncate -m {prealloc_mode} {new_size}',
141 self.assertEqual(ret, 0)
144 refstat = os.stat(refdisk)
146 # Probably we'll want preallocate filter to keep align to cluster when
147 # shrink preallocation, so, ignore small differece
148 self.assertLess(abs(stat.st_size - refstat.st_size), 64 * 1024)
150 # Preallocate filter may leak some internal clusters (for example, if
151 # guest write far over EOF, skipping some clusters - they will remain
152 # fallocated, preallocate filter don't care about such leaks, it drops
153 # only trailing preallocation.
154 self.assertLess(abs(stat.st_blocks - refstat.st_blocks) * 512,
157 def test_real_shrink(self):
158 self.do_test('off', '5M')
160 def test_truncate_inside_preallocated_area__falloc(self):
161 self.do_test('falloc', '50M')
163 def test_truncate_inside_preallocated_area__metadata(self):
164 self.do_test('metadata', '50M')
166 def test_truncate_inside_preallocated_area__full(self):
167 self.do_test('full', '50M')
169 def test_truncate_inside_preallocated_area__off(self):
170 self.do_test('off', '50M')
172 def test_truncate_over_preallocated_area__falloc(self):
173 self.do_test('falloc', '150M')
175 def test_truncate_over_preallocated_area__metadata(self):
176 self.do_test('metadata', '150M')
178 def test_truncate_over_preallocated_area__full(self):
179 self.do_test('full', '150M')
181 def test_truncate_over_preallocated_area__off(self):
182 self.do_test('off', '150M')
185 if __name__ == '__main__':
186 iotests.main(supported_fmts=['qcow2'], required_fmts=['preallocate'])