cachepc-qemu

Fork of AMDESE/qemu with changes for cachepc side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-qemu
Log | Files | Refs | Submodules | LICENSE | sfeed.txt

298 (6219B)


      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        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', {})
     87
     88        # on reopen to  r-o base preallocation should be dropped
     89        self.check_small()
     90
     91        self.vm.hmp_qemu_io('drive0', 'write 1M 1M')
     92
     93        result = self.vm.qmp('block-commit', device='overlay')
     94        self.assert_qmp(result, 'return', {})
     95        self.complete_and_wait()
     96
     97        # commit of new megabyte should trigger preallocation
     98        self.check_big()
     99
    100    def test_reopen_opts(self):
    101        result = self.vm.qmp('blockdev-reopen', options=[{
    102            'node-name': 'disk',
    103            'driver': iotests.imgfmt,
    104            'file': {
    105                'node-name': 'filter',
    106                'driver': 'preallocate',
    107                'prealloc-size': 20 * MiB,
    108                'prealloc-align': 5 * MiB,
    109                'file': {
    110                    'node-name': 'file',
    111                    'driver': 'file',
    112                    'filename': disk
    113                }
    114            }
    115        }])
    116        self.assert_qmp(result, 'return', {})
    117
    118        self.vm.hmp_qemu_io('drive0', 'write 0 1M')
    119        self.assertTrue(os.path.getsize(disk) == 25 * MiB)
    120
    121
    122class TestTruncate(iotests.QMPTestCase):
    123    def setUp(self):
    124        iotests.qemu_img_create('-f', iotests.imgfmt, disk, str(10 * MiB))
    125        iotests.qemu_img_create('-f', iotests.imgfmt, refdisk, str(10 * MiB))
    126
    127    def tearDown(self):
    128        os.remove(disk)
    129        os.remove(refdisk)
    130
    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}',
    134                                     drive_opts)
    135        self.assertEqual(ret, 0)
    136
    137        ret = iotests.qemu_io_silent('-f', iotests.imgfmt, '-c', 'write 0 10M',
    138                                     '-c',
    139                                     f'truncate -m {prealloc_mode} {new_size}',
    140                                     refdisk)
    141        self.assertEqual(ret, 0)
    142
    143        stat = os.stat(disk)
    144        refstat = os.stat(refdisk)
    145
    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)
    149
    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,
    155                        1024 * 1024)
    156
    157    def test_real_shrink(self):
    158        self.do_test('off', '5M')
    159
    160    def test_truncate_inside_preallocated_area__falloc(self):
    161        self.do_test('falloc', '50M')
    162
    163    def test_truncate_inside_preallocated_area__metadata(self):
    164        self.do_test('metadata', '50M')
    165
    166    def test_truncate_inside_preallocated_area__full(self):
    167        self.do_test('full', '50M')
    168
    169    def test_truncate_inside_preallocated_area__off(self):
    170        self.do_test('off', '50M')
    171
    172    def test_truncate_over_preallocated_area__falloc(self):
    173        self.do_test('falloc', '150M')
    174
    175    def test_truncate_over_preallocated_area__metadata(self):
    176        self.do_test('metadata', '150M')
    177
    178    def test_truncate_over_preallocated_area__full(self):
    179        self.do_test('full', '150M')
    180
    181    def test_truncate_over_preallocated_area__off(self):
    182        self.do_test('off', '150M')
    183
    184
    185if __name__ == '__main__':
    186    iotests.main(supported_fmts=['qcow2'], required_fmts=['preallocate'])