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