xref: /openbmc/qemu/tests/qemu-iotests/044 (revision 14a650ec)
1#!/usr/bin/env python
2#
3# Tests growing a large refcount table.
4#
5# Copyright (C) 2012 Red Hat, Inc.
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 time
22import os
23import qcow2
24from qcow2 import QcowHeader
25import iotests
26from iotests import qemu_img, qemu_img_verbose, qemu_io
27import struct
28import subprocess
29
30test_img = os.path.join(iotests.test_dir, 'test.img')
31
32class TestRefcountTableGrowth(iotests.QMPTestCase):
33    '''Abstract base class for image mirroring test cases'''
34
35    def preallocate(self, name):
36        fd = open(name, "r+b")
37        try:
38            off_reftable = 512
39            off_refblock = off_reftable + (512 * 512)
40            off_l1       = off_refblock + (512 * 512 * 64)
41            off_l2       = off_l1 + (512 * 512 * 4 * 8)
42            off_data     = off_l2 + (512 * 512 * 4 * 512)
43
44            # Write a new header
45            h = QcowHeader(fd)
46            h.refcount_table_offset = off_reftable
47            h.refcount_table_clusters = 512
48            h.l1_table_offset = off_l1
49            h.l1_size = 512 * 512 * 4
50            h.update(fd)
51
52            # Write a refcount table
53            fd.seek(off_reftable)
54
55            for i in xrange(0, h.refcount_table_clusters):
56                sector = ''.join(struct.pack('>Q',
57                    off_refblock + i * 64 * 512 + j * 512)
58                    for j in xrange(0, 64))
59                fd.write(sector)
60
61            # Write the refcount blocks
62            assert(fd.tell() == off_refblock)
63            sector = ''.join(struct.pack('>H', 1) for j in xrange(0, 64 * 256))
64            for block in xrange(0, h.refcount_table_clusters):
65                fd.write(sector)
66
67            # Write the L1 table
68            assert(fd.tell() == off_l1)
69            assert(off_l2 + 512 * h.l1_size == off_data)
70            table = ''.join(struct.pack('>Q', (1 << 63) | off_l2 + 512 * j)
71                for j in xrange(0, h.l1_size))
72            fd.write(table)
73
74            # Write the L2 tables
75            assert(fd.tell() == off_l2)
76            img_file_size = h.refcount_table_clusters * 64 * 256 * 512
77            remaining = img_file_size - off_data
78
79            off = off_data
80            while remaining > 1024 * 512:
81                pytable = list((1 << 63) | off + 512 * j
82                    for j in xrange(0, 1024))
83                table = struct.pack('>1024Q', *pytable)
84                fd.write(table)
85                remaining = remaining - 1024 * 512
86                off = off + 1024 * 512
87
88            table = ''.join(struct.pack('>Q', (1 << 63) | off + 512 * j)
89                for j in xrange(0, remaining / 512))
90            fd.write(table)
91
92
93            # Data
94            fd.truncate(img_file_size)
95
96
97        finally:
98            fd.close()
99
100
101    def setUp(self):
102        qemu_img('create', '-f', iotests.imgfmt, '-o', 'cluster_size=512', test_img, '16G')
103        self.preallocate(test_img)
104        pass
105
106
107    def tearDown(self):
108        os.remove(test_img)
109        pass
110
111    def test_grow_refcount_table(self):
112        qemu_io('-c', 'write 3800M 1M', test_img)
113        qemu_img_verbose('check' , test_img)
114        pass
115
116if __name__ == '__main__':
117    iotests.main(supported_fmts=['qcow2'])
118