root / block-vmdk.c @ 6cc721cf
History | View | Annotate | Download (13.9 kB)
1 | ea2384d3 | bellard | /*
|
---|---|---|---|
2 | ea2384d3 | bellard | * Block driver for the VMDK format
|
3 | ea2384d3 | bellard | *
|
4 | ea2384d3 | bellard | * Copyright (c) 2004 Fabrice Bellard
|
5 | ff1afc72 | bellard | * Copyright (c) 2005 Filip Navara
|
6 | ea2384d3 | bellard | *
|
7 | ea2384d3 | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
8 | ea2384d3 | bellard | * of this software and associated documentation files (the "Software"), to deal
|
9 | ea2384d3 | bellard | * in the Software without restriction, including without limitation the rights
|
10 | ea2384d3 | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11 | ea2384d3 | bellard | * copies of the Software, and to permit persons to whom the Software is
|
12 | ea2384d3 | bellard | * furnished to do so, subject to the following conditions:
|
13 | ea2384d3 | bellard | *
|
14 | ea2384d3 | bellard | * The above copyright notice and this permission notice shall be included in
|
15 | ea2384d3 | bellard | * all copies or substantial portions of the Software.
|
16 | ea2384d3 | bellard | *
|
17 | ea2384d3 | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18 | ea2384d3 | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19 | ea2384d3 | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
20 | ea2384d3 | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21 | ea2384d3 | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22 | ea2384d3 | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
23 | ea2384d3 | bellard | * THE SOFTWARE.
|
24 | ea2384d3 | bellard | */
|
25 | ea2384d3 | bellard | #include "vl.h" |
26 | ea2384d3 | bellard | #include "block_int.h" |
27 | ea2384d3 | bellard | |
28 | ea2384d3 | bellard | #define VMDK3_MAGIC (('C' << 24) | ('O' << 16) | ('W' << 8) | 'D') |
29 | ea2384d3 | bellard | #define VMDK4_MAGIC (('K' << 24) | ('D' << 16) | ('M' << 8) | 'V') |
30 | ea2384d3 | bellard | |
31 | ea2384d3 | bellard | typedef struct { |
32 | ea2384d3 | bellard | uint32_t version; |
33 | ea2384d3 | bellard | uint32_t flags; |
34 | ea2384d3 | bellard | uint32_t disk_sectors; |
35 | ea2384d3 | bellard | uint32_t granularity; |
36 | ea2384d3 | bellard | uint32_t l1dir_offset; |
37 | ea2384d3 | bellard | uint32_t l1dir_size; |
38 | ea2384d3 | bellard | uint32_t file_sectors; |
39 | ea2384d3 | bellard | uint32_t cylinders; |
40 | ea2384d3 | bellard | uint32_t heads; |
41 | ea2384d3 | bellard | uint32_t sectors_per_track; |
42 | ea2384d3 | bellard | } VMDK3Header; |
43 | ea2384d3 | bellard | |
44 | ea2384d3 | bellard | typedef struct { |
45 | ea2384d3 | bellard | uint32_t version; |
46 | ea2384d3 | bellard | uint32_t flags; |
47 | ea2384d3 | bellard | int64_t capacity; |
48 | ea2384d3 | bellard | int64_t granularity; |
49 | ea2384d3 | bellard | int64_t desc_offset; |
50 | ea2384d3 | bellard | int64_t desc_size; |
51 | ea2384d3 | bellard | int32_t num_gtes_per_gte; |
52 | ea2384d3 | bellard | int64_t rgd_offset; |
53 | ea2384d3 | bellard | int64_t gd_offset; |
54 | ea2384d3 | bellard | int64_t grain_offset; |
55 | ea2384d3 | bellard | char filler[1]; |
56 | ea2384d3 | bellard | char check_bytes[4]; |
57 | ff1afc72 | bellard | } __attribute__((packed)) VMDK4Header; |
58 | ea2384d3 | bellard | |
59 | ea2384d3 | bellard | #define L2_CACHE_SIZE 16 |
60 | ea2384d3 | bellard | |
61 | ea2384d3 | bellard | typedef struct BDRVVmdkState { |
62 | ea2384d3 | bellard | int fd;
|
63 | ea2384d3 | bellard | int64_t l1_table_offset; |
64 | ff1afc72 | bellard | int64_t l1_backup_table_offset; |
65 | ea2384d3 | bellard | uint32_t *l1_table; |
66 | ff1afc72 | bellard | uint32_t *l1_backup_table; |
67 | ea2384d3 | bellard | unsigned int l1_size; |
68 | ea2384d3 | bellard | uint32_t l1_entry_sectors; |
69 | ea2384d3 | bellard | |
70 | ea2384d3 | bellard | unsigned int l2_size; |
71 | ea2384d3 | bellard | uint32_t *l2_cache; |
72 | ea2384d3 | bellard | uint32_t l2_cache_offsets[L2_CACHE_SIZE]; |
73 | ea2384d3 | bellard | uint32_t l2_cache_counts[L2_CACHE_SIZE]; |
74 | ea2384d3 | bellard | |
75 | ea2384d3 | bellard | unsigned int cluster_sectors; |
76 | ea2384d3 | bellard | } BDRVVmdkState; |
77 | ea2384d3 | bellard | |
78 | ea2384d3 | bellard | static int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename) |
79 | ea2384d3 | bellard | { |
80 | ea2384d3 | bellard | uint32_t magic; |
81 | ea2384d3 | bellard | |
82 | ea2384d3 | bellard | if (buf_size < 4) |
83 | ea2384d3 | bellard | return 0; |
84 | ea2384d3 | bellard | magic = be32_to_cpu(*(uint32_t *)buf); |
85 | ea2384d3 | bellard | if (magic == VMDK3_MAGIC ||
|
86 | ea2384d3 | bellard | magic == VMDK4_MAGIC) |
87 | ea2384d3 | bellard | return 100; |
88 | ea2384d3 | bellard | else
|
89 | ea2384d3 | bellard | return 0; |
90 | ea2384d3 | bellard | } |
91 | ea2384d3 | bellard | |
92 | ea2384d3 | bellard | static int vmdk_open(BlockDriverState *bs, const char *filename) |
93 | ea2384d3 | bellard | { |
94 | ea2384d3 | bellard | BDRVVmdkState *s = bs->opaque; |
95 | ea2384d3 | bellard | int fd, i;
|
96 | ea2384d3 | bellard | uint32_t magic; |
97 | ea2384d3 | bellard | int l1_size;
|
98 | ea2384d3 | bellard | |
99 | ff1afc72 | bellard | fd = open(filename, O_RDWR | O_BINARY | O_LARGEFILE); |
100 | ff1afc72 | bellard | if (fd < 0) { |
101 | ff1afc72 | bellard | fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE); |
102 | ff1afc72 | bellard | if (fd < 0) |
103 | ff1afc72 | bellard | return -1; |
104 | ff1afc72 | bellard | bs->read_only = 1;
|
105 | ff1afc72 | bellard | } |
106 | ea2384d3 | bellard | if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) |
107 | ea2384d3 | bellard | goto fail;
|
108 | 7143c62c | bellard | magic = be32_to_cpu(magic); |
109 | ea2384d3 | bellard | if (magic == VMDK3_MAGIC) {
|
110 | ea2384d3 | bellard | VMDK3Header header; |
111 | ea2384d3 | bellard | if (read(fd, &header, sizeof(header)) != |
112 | ea2384d3 | bellard | sizeof(header))
|
113 | ea2384d3 | bellard | goto fail;
|
114 | ea2384d3 | bellard | s->cluster_sectors = le32_to_cpu(header.granularity); |
115 | ea2384d3 | bellard | s->l2_size = 1 << 9; |
116 | ea2384d3 | bellard | s->l1_size = 1 << 6; |
117 | ea2384d3 | bellard | bs->total_sectors = le32_to_cpu(header.disk_sectors); |
118 | ff1afc72 | bellard | s->l1_table_offset = le32_to_cpu(header.l1dir_offset) << 9;
|
119 | ff1afc72 | bellard | s->l1_backup_table_offset = 0;
|
120 | ea2384d3 | bellard | s->l1_entry_sectors = s->l2_size * s->cluster_sectors; |
121 | ea2384d3 | bellard | } else if (magic == VMDK4_MAGIC) { |
122 | ea2384d3 | bellard | VMDK4Header header; |
123 | ea2384d3 | bellard | |
124 | ea2384d3 | bellard | if (read(fd, &header, sizeof(header)) != sizeof(header)) |
125 | ea2384d3 | bellard | goto fail;
|
126 | ea2384d3 | bellard | bs->total_sectors = le32_to_cpu(header.capacity); |
127 | ea2384d3 | bellard | s->cluster_sectors = le32_to_cpu(header.granularity); |
128 | ea2384d3 | bellard | s->l2_size = le32_to_cpu(header.num_gtes_per_gte); |
129 | ea2384d3 | bellard | s->l1_entry_sectors = s->l2_size * s->cluster_sectors; |
130 | ea2384d3 | bellard | if (s->l1_entry_sectors <= 0) |
131 | ea2384d3 | bellard | goto fail;
|
132 | ea2384d3 | bellard | s->l1_size = (bs->total_sectors + s->l1_entry_sectors - 1)
|
133 | ea2384d3 | bellard | / s->l1_entry_sectors; |
134 | ff1afc72 | bellard | s->l1_table_offset = le64_to_cpu(header.rgd_offset) << 9;
|
135 | ff1afc72 | bellard | s->l1_backup_table_offset = le64_to_cpu(header.gd_offset) << 9;
|
136 | ea2384d3 | bellard | } else {
|
137 | ea2384d3 | bellard | goto fail;
|
138 | ea2384d3 | bellard | } |
139 | ea2384d3 | bellard | /* read the L1 table */
|
140 | ea2384d3 | bellard | l1_size = s->l1_size * sizeof(uint32_t);
|
141 | ea2384d3 | bellard | s->l1_table = qemu_malloc(l1_size); |
142 | ea2384d3 | bellard | if (!s->l1_table)
|
143 | ea2384d3 | bellard | goto fail;
|
144 | 7143c62c | bellard | if (lseek(fd, s->l1_table_offset, SEEK_SET) == -1) |
145 | 7143c62c | bellard | goto fail;
|
146 | 7143c62c | bellard | if (read(fd, s->l1_table, l1_size) != l1_size)
|
147 | ea2384d3 | bellard | goto fail;
|
148 | ea2384d3 | bellard | for(i = 0; i < s->l1_size; i++) { |
149 | ea2384d3 | bellard | le32_to_cpus(&s->l1_table[i]); |
150 | ea2384d3 | bellard | } |
151 | ea2384d3 | bellard | |
152 | ff1afc72 | bellard | if (s->l1_backup_table_offset) {
|
153 | ff1afc72 | bellard | s->l1_backup_table = qemu_malloc(l1_size); |
154 | ff1afc72 | bellard | if (!s->l1_backup_table)
|
155 | ff1afc72 | bellard | goto fail;
|
156 | ff1afc72 | bellard | if (lseek(fd, s->l1_backup_table_offset, SEEK_SET) == -1) |
157 | ff1afc72 | bellard | goto fail;
|
158 | ff1afc72 | bellard | if (read(fd, s->l1_backup_table, l1_size) != l1_size)
|
159 | ff1afc72 | bellard | goto fail;
|
160 | ff1afc72 | bellard | for(i = 0; i < s->l1_size; i++) { |
161 | ff1afc72 | bellard | le32_to_cpus(&s->l1_backup_table[i]); |
162 | ff1afc72 | bellard | } |
163 | ff1afc72 | bellard | } |
164 | ff1afc72 | bellard | |
165 | ea2384d3 | bellard | s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint32_t));
|
166 | ea2384d3 | bellard | if (!s->l2_cache)
|
167 | ea2384d3 | bellard | goto fail;
|
168 | ea2384d3 | bellard | s->fd = fd; |
169 | ea2384d3 | bellard | return 0; |
170 | ea2384d3 | bellard | fail:
|
171 | ff1afc72 | bellard | qemu_free(s->l1_backup_table); |
172 | ea2384d3 | bellard | qemu_free(s->l1_table); |
173 | ea2384d3 | bellard | qemu_free(s->l2_cache); |
174 | ea2384d3 | bellard | close(fd); |
175 | ea2384d3 | bellard | return -1; |
176 | ea2384d3 | bellard | } |
177 | ea2384d3 | bellard | |
178 | ea2384d3 | bellard | static uint64_t get_cluster_offset(BlockDriverState *bs,
|
179 | ff1afc72 | bellard | uint64_t offset, int allocate)
|
180 | ea2384d3 | bellard | { |
181 | ea2384d3 | bellard | BDRVVmdkState *s = bs->opaque; |
182 | ea2384d3 | bellard | unsigned int l1_index, l2_offset, l2_index; |
183 | ea2384d3 | bellard | int min_index, i, j;
|
184 | ff1afc72 | bellard | uint32_t min_count, *l2_table, tmp; |
185 | ea2384d3 | bellard | uint64_t cluster_offset; |
186 | ea2384d3 | bellard | |
187 | ea2384d3 | bellard | l1_index = (offset >> 9) / s->l1_entry_sectors;
|
188 | ea2384d3 | bellard | if (l1_index >= s->l1_size)
|
189 | ea2384d3 | bellard | return 0; |
190 | ea2384d3 | bellard | l2_offset = s->l1_table[l1_index]; |
191 | ea2384d3 | bellard | if (!l2_offset)
|
192 | ea2384d3 | bellard | return 0; |
193 | ea2384d3 | bellard | for(i = 0; i < L2_CACHE_SIZE; i++) { |
194 | ea2384d3 | bellard | if (l2_offset == s->l2_cache_offsets[i]) {
|
195 | ea2384d3 | bellard | /* increment the hit count */
|
196 | ea2384d3 | bellard | if (++s->l2_cache_counts[i] == 0xffffffff) { |
197 | ea2384d3 | bellard | for(j = 0; j < L2_CACHE_SIZE; j++) { |
198 | ea2384d3 | bellard | s->l2_cache_counts[j] >>= 1;
|
199 | ea2384d3 | bellard | } |
200 | ea2384d3 | bellard | } |
201 | ea2384d3 | bellard | l2_table = s->l2_cache + (i * s->l2_size); |
202 | ea2384d3 | bellard | goto found;
|
203 | ea2384d3 | bellard | } |
204 | ea2384d3 | bellard | } |
205 | ea2384d3 | bellard | /* not found: load a new entry in the least used one */
|
206 | ea2384d3 | bellard | min_index = 0;
|
207 | ea2384d3 | bellard | min_count = 0xffffffff;
|
208 | ea2384d3 | bellard | for(i = 0; i < L2_CACHE_SIZE; i++) { |
209 | ea2384d3 | bellard | if (s->l2_cache_counts[i] < min_count) {
|
210 | ea2384d3 | bellard | min_count = s->l2_cache_counts[i]; |
211 | ea2384d3 | bellard | min_index = i; |
212 | ea2384d3 | bellard | } |
213 | ea2384d3 | bellard | } |
214 | ea2384d3 | bellard | l2_table = s->l2_cache + (min_index * s->l2_size); |
215 | ea2384d3 | bellard | lseek(s->fd, (int64_t)l2_offset * 512, SEEK_SET);
|
216 | ea2384d3 | bellard | if (read(s->fd, l2_table, s->l2_size * sizeof(uint32_t)) != |
217 | ea2384d3 | bellard | s->l2_size * sizeof(uint32_t))
|
218 | ea2384d3 | bellard | return 0; |
219 | ea2384d3 | bellard | s->l2_cache_offsets[min_index] = l2_offset; |
220 | ea2384d3 | bellard | s->l2_cache_counts[min_index] = 1;
|
221 | ea2384d3 | bellard | found:
|
222 | ea2384d3 | bellard | l2_index = ((offset >> 9) / s->cluster_sectors) % s->l2_size;
|
223 | ea2384d3 | bellard | cluster_offset = le32_to_cpu(l2_table[l2_index]); |
224 | ff1afc72 | bellard | if (!cluster_offset) {
|
225 | ff1afc72 | bellard | if (!allocate)
|
226 | ff1afc72 | bellard | return 0; |
227 | ff1afc72 | bellard | cluster_offset = lseek(s->fd, 0, SEEK_END);
|
228 | ff1afc72 | bellard | ftruncate(s->fd, cluster_offset + (s->cluster_sectors << 9));
|
229 | ff1afc72 | bellard | cluster_offset >>= 9;
|
230 | ff1afc72 | bellard | /* update L2 table */
|
231 | ff1afc72 | bellard | tmp = cpu_to_le32(cluster_offset); |
232 | ff1afc72 | bellard | l2_table[l2_index] = tmp; |
233 | ff1afc72 | bellard | lseek(s->fd, ((int64_t)l2_offset * 512) + (l2_index * sizeof(tmp)), SEEK_SET); |
234 | ff1afc72 | bellard | if (write(s->fd, &tmp, sizeof(tmp)) != sizeof(tmp)) |
235 | ff1afc72 | bellard | return 0; |
236 | ff1afc72 | bellard | /* update backup L2 table */
|
237 | ff1afc72 | bellard | if (s->l1_backup_table_offset != 0) { |
238 | ff1afc72 | bellard | l2_offset = s->l1_backup_table[l1_index]; |
239 | ff1afc72 | bellard | lseek(s->fd, ((int64_t)l2_offset * 512) + (l2_index * sizeof(tmp)), SEEK_SET); |
240 | ff1afc72 | bellard | if (write(s->fd, &tmp, sizeof(tmp)) != sizeof(tmp)) |
241 | ff1afc72 | bellard | return 0; |
242 | ff1afc72 | bellard | } |
243 | ff1afc72 | bellard | } |
244 | ea2384d3 | bellard | cluster_offset <<= 9;
|
245 | ea2384d3 | bellard | return cluster_offset;
|
246 | ea2384d3 | bellard | } |
247 | ea2384d3 | bellard | |
248 | ea2384d3 | bellard | static int vmdk_is_allocated(BlockDriverState *bs, int64_t sector_num, |
249 | ea2384d3 | bellard | int nb_sectors, int *pnum) |
250 | ea2384d3 | bellard | { |
251 | ea2384d3 | bellard | BDRVVmdkState *s = bs->opaque; |
252 | ea2384d3 | bellard | int index_in_cluster, n;
|
253 | ea2384d3 | bellard | uint64_t cluster_offset; |
254 | ea2384d3 | bellard | |
255 | ff1afc72 | bellard | cluster_offset = get_cluster_offset(bs, sector_num << 9, 0); |
256 | ea2384d3 | bellard | index_in_cluster = sector_num % s->cluster_sectors; |
257 | ea2384d3 | bellard | n = s->cluster_sectors - index_in_cluster; |
258 | ea2384d3 | bellard | if (n > nb_sectors)
|
259 | ea2384d3 | bellard | n = nb_sectors; |
260 | ea2384d3 | bellard | *pnum = n; |
261 | ea2384d3 | bellard | return (cluster_offset != 0); |
262 | ea2384d3 | bellard | } |
263 | ea2384d3 | bellard | |
264 | ea2384d3 | bellard | static int vmdk_read(BlockDriverState *bs, int64_t sector_num, |
265 | ea2384d3 | bellard | uint8_t *buf, int nb_sectors)
|
266 | ea2384d3 | bellard | { |
267 | ea2384d3 | bellard | BDRVVmdkState *s = bs->opaque; |
268 | ea2384d3 | bellard | int ret, index_in_cluster, n;
|
269 | ea2384d3 | bellard | uint64_t cluster_offset; |
270 | ea2384d3 | bellard | |
271 | ea2384d3 | bellard | while (nb_sectors > 0) { |
272 | ff1afc72 | bellard | cluster_offset = get_cluster_offset(bs, sector_num << 9, 0); |
273 | ea2384d3 | bellard | index_in_cluster = sector_num % s->cluster_sectors; |
274 | ea2384d3 | bellard | n = s->cluster_sectors - index_in_cluster; |
275 | ea2384d3 | bellard | if (n > nb_sectors)
|
276 | ea2384d3 | bellard | n = nb_sectors; |
277 | ea2384d3 | bellard | if (!cluster_offset) {
|
278 | ea2384d3 | bellard | memset(buf, 0, 512 * n); |
279 | ea2384d3 | bellard | } else {
|
280 | d5249393 | bellard | lseek(s->fd, cluster_offset + index_in_cluster * 512, SEEK_SET);
|
281 | ea2384d3 | bellard | ret = read(s->fd, buf, n * 512);
|
282 | ea2384d3 | bellard | if (ret != n * 512) |
283 | ea2384d3 | bellard | return -1; |
284 | ea2384d3 | bellard | } |
285 | ea2384d3 | bellard | nb_sectors -= n; |
286 | ea2384d3 | bellard | sector_num += n; |
287 | ea2384d3 | bellard | buf += n * 512;
|
288 | ea2384d3 | bellard | } |
289 | ea2384d3 | bellard | return 0; |
290 | ea2384d3 | bellard | } |
291 | ea2384d3 | bellard | |
292 | ea2384d3 | bellard | static int vmdk_write(BlockDriverState *bs, int64_t sector_num, |
293 | ea2384d3 | bellard | const uint8_t *buf, int nb_sectors) |
294 | ea2384d3 | bellard | { |
295 | ff1afc72 | bellard | BDRVVmdkState *s = bs->opaque; |
296 | ff1afc72 | bellard | int ret, index_in_cluster, n;
|
297 | ff1afc72 | bellard | uint64_t cluster_offset; |
298 | ff1afc72 | bellard | |
299 | ff1afc72 | bellard | while (nb_sectors > 0) { |
300 | ff1afc72 | bellard | index_in_cluster = sector_num & (s->cluster_sectors - 1);
|
301 | ff1afc72 | bellard | n = s->cluster_sectors - index_in_cluster; |
302 | ff1afc72 | bellard | if (n > nb_sectors)
|
303 | ff1afc72 | bellard | n = nb_sectors; |
304 | ff1afc72 | bellard | cluster_offset = get_cluster_offset(bs, sector_num << 9, 1); |
305 | ff1afc72 | bellard | if (!cluster_offset)
|
306 | ff1afc72 | bellard | return -1; |
307 | ff1afc72 | bellard | lseek(s->fd, cluster_offset + index_in_cluster * 512, SEEK_SET);
|
308 | ff1afc72 | bellard | ret = write(s->fd, buf, n * 512);
|
309 | ff1afc72 | bellard | if (ret != n * 512) |
310 | ff1afc72 | bellard | return -1; |
311 | ff1afc72 | bellard | nb_sectors -= n; |
312 | ff1afc72 | bellard | sector_num += n; |
313 | ff1afc72 | bellard | buf += n * 512;
|
314 | ff1afc72 | bellard | } |
315 | ff1afc72 | bellard | return 0; |
316 | ea2384d3 | bellard | } |
317 | ea2384d3 | bellard | |
318 | 8979b227 | bellard | static int vmdk_create(const char *filename, int64_t total_size, |
319 | 8979b227 | bellard | const char *backing_file, int flags) |
320 | 8979b227 | bellard | { |
321 | 8979b227 | bellard | int fd, i;
|
322 | 8979b227 | bellard | VMDK4Header header; |
323 | 8979b227 | bellard | uint32_t tmp, magic, grains, gd_size, gt_size, gt_count; |
324 | 8979b227 | bellard | char *desc_template =
|
325 | 8979b227 | bellard | "# Disk DescriptorFile\n"
|
326 | 8979b227 | bellard | "version=1\n"
|
327 | 8979b227 | bellard | "CID=%x\n"
|
328 | 8979b227 | bellard | "parentCID=ffffffff\n"
|
329 | 8979b227 | bellard | "createType=\"monolithicSparse\"\n"
|
330 | 8979b227 | bellard | "\n"
|
331 | 8979b227 | bellard | "# Extent description\n"
|
332 | 8979b227 | bellard | "RW %lu SPARSE \"%s\"\n"
|
333 | 8979b227 | bellard | "\n"
|
334 | 8979b227 | bellard | "# The Disk Data Base \n"
|
335 | 8979b227 | bellard | "#DDB\n"
|
336 | 8979b227 | bellard | "\n"
|
337 | 8979b227 | bellard | "ddb.virtualHWVersion = \"3\"\n"
|
338 | 8979b227 | bellard | "ddb.geometry.cylinders = \"%lu\"\n"
|
339 | 8979b227 | bellard | "ddb.geometry.heads = \"16\"\n"
|
340 | 8979b227 | bellard | "ddb.geometry.sectors = \"63\"\n"
|
341 | 8979b227 | bellard | "ddb.adapterType = \"ide\"\n";
|
342 | 8979b227 | bellard | char desc[1024]; |
343 | 8979b227 | bellard | const char *real_filename, *temp_str; |
344 | 8979b227 | bellard | |
345 | 8979b227 | bellard | /* XXX: add support for backing file */
|
346 | 8979b227 | bellard | |
347 | 8979b227 | bellard | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, |
348 | 8979b227 | bellard | 0644);
|
349 | 8979b227 | bellard | if (fd < 0) |
350 | 8979b227 | bellard | return -1; |
351 | 8979b227 | bellard | magic = cpu_to_be32(VMDK4_MAGIC); |
352 | 8979b227 | bellard | memset(&header, 0, sizeof(header)); |
353 | 8979b227 | bellard | header.version = cpu_to_le32(1);
|
354 | 8979b227 | bellard | header.flags = cpu_to_le32(3); /* ?? */ |
355 | 8979b227 | bellard | header.capacity = cpu_to_le64(total_size); |
356 | 8979b227 | bellard | header.granularity = cpu_to_le64(128);
|
357 | 8979b227 | bellard | header.num_gtes_per_gte = cpu_to_le32(512);
|
358 | 8979b227 | bellard | |
359 | 8979b227 | bellard | grains = (total_size + header.granularity - 1) / header.granularity;
|
360 | 8979b227 | bellard | gt_size = ((header.num_gtes_per_gte * sizeof(uint32_t)) + 511) >> 9; |
361 | 8979b227 | bellard | gt_count = (grains + header.num_gtes_per_gte - 1) / header.num_gtes_per_gte;
|
362 | 8979b227 | bellard | gd_size = (gt_count * sizeof(uint32_t) + 511) >> 9; |
363 | 8979b227 | bellard | |
364 | 8979b227 | bellard | header.desc_offset = 1;
|
365 | 8979b227 | bellard | header.desc_size = 20;
|
366 | 8979b227 | bellard | header.rgd_offset = header.desc_offset + header.desc_size; |
367 | 8979b227 | bellard | header.gd_offset = header.rgd_offset + gd_size + (gt_size * gt_count); |
368 | 8979b227 | bellard | header.grain_offset = |
369 | 8979b227 | bellard | ((header.gd_offset + gd_size + (gt_size * gt_count) + |
370 | 8979b227 | bellard | header.granularity - 1) / header.granularity) *
|
371 | 8979b227 | bellard | header.granularity; |
372 | 8979b227 | bellard | |
373 | 8979b227 | bellard | header.desc_offset = cpu_to_le64(header.desc_offset); |
374 | 8979b227 | bellard | header.desc_size = cpu_to_le64(header.desc_size); |
375 | 8979b227 | bellard | header.rgd_offset = cpu_to_le64(header.rgd_offset); |
376 | 8979b227 | bellard | header.gd_offset = cpu_to_le64(header.gd_offset); |
377 | 8979b227 | bellard | header.grain_offset = cpu_to_le64(header.grain_offset); |
378 | 8979b227 | bellard | |
379 | 8979b227 | bellard | header.check_bytes[0] = 0xa; |
380 | 8979b227 | bellard | header.check_bytes[1] = 0x20; |
381 | 8979b227 | bellard | header.check_bytes[2] = 0xd; |
382 | 8979b227 | bellard | header.check_bytes[3] = 0xa; |
383 | 8979b227 | bellard | |
384 | 8979b227 | bellard | /* write all the data */
|
385 | 8979b227 | bellard | write(fd, &magic, sizeof(magic));
|
386 | 8979b227 | bellard | write(fd, &header, sizeof(header));
|
387 | 8979b227 | bellard | |
388 | 8979b227 | bellard | ftruncate(fd, header.grain_offset << 9);
|
389 | 8979b227 | bellard | |
390 | 8979b227 | bellard | /* write grain directory */
|
391 | 8979b227 | bellard | lseek(fd, le64_to_cpu(header.rgd_offset) << 9, SEEK_SET);
|
392 | 8979b227 | bellard | for (i = 0, tmp = header.rgd_offset + gd_size; |
393 | 8979b227 | bellard | i < gt_count; i++, tmp += gt_size) |
394 | 8979b227 | bellard | write(fd, &tmp, sizeof(tmp));
|
395 | 8979b227 | bellard | |
396 | 8979b227 | bellard | /* write backup grain directory */
|
397 | 8979b227 | bellard | lseek(fd, le64_to_cpu(header.gd_offset) << 9, SEEK_SET);
|
398 | 8979b227 | bellard | for (i = 0, tmp = header.gd_offset + gd_size; |
399 | 8979b227 | bellard | i < gt_count; i++, tmp += gt_size) |
400 | 8979b227 | bellard | write(fd, &tmp, sizeof(tmp));
|
401 | 8979b227 | bellard | |
402 | 8979b227 | bellard | /* compose the descriptor */
|
403 | 8979b227 | bellard | real_filename = filename; |
404 | 8979b227 | bellard | if ((temp_str = strrchr(real_filename, '\\')) != NULL) |
405 | 8979b227 | bellard | real_filename = temp_str + 1;
|
406 | 8979b227 | bellard | if ((temp_str = strrchr(real_filename, '/')) != NULL) |
407 | 8979b227 | bellard | real_filename = temp_str + 1;
|
408 | 8979b227 | bellard | if ((temp_str = strrchr(real_filename, ':')) != NULL) |
409 | 8979b227 | bellard | real_filename = temp_str + 1;
|
410 | 8979b227 | bellard | sprintf(desc, desc_template, time(NULL), (unsigned long)total_size, |
411 | 8979b227 | bellard | real_filename, total_size / (63 * 16)); |
412 | 8979b227 | bellard | |
413 | 8979b227 | bellard | /* write the descriptor */
|
414 | 8979b227 | bellard | lseek(fd, le64_to_cpu(header.desc_offset) << 9, SEEK_SET);
|
415 | 8979b227 | bellard | write(fd, desc, strlen(desc)); |
416 | 8979b227 | bellard | |
417 | 8979b227 | bellard | close(fd); |
418 | 8979b227 | bellard | return 0; |
419 | 8979b227 | bellard | } |
420 | 8979b227 | bellard | |
421 | e2731add | bellard | static void vmdk_close(BlockDriverState *bs) |
422 | ea2384d3 | bellard | { |
423 | ea2384d3 | bellard | BDRVVmdkState *s = bs->opaque; |
424 | ea2384d3 | bellard | qemu_free(s->l1_table); |
425 | ea2384d3 | bellard | qemu_free(s->l2_cache); |
426 | ea2384d3 | bellard | close(s->fd); |
427 | ea2384d3 | bellard | } |
428 | ea2384d3 | bellard | |
429 | ea2384d3 | bellard | BlockDriver bdrv_vmdk = { |
430 | ea2384d3 | bellard | "vmdk",
|
431 | ea2384d3 | bellard | sizeof(BDRVVmdkState),
|
432 | ea2384d3 | bellard | vmdk_probe, |
433 | ea2384d3 | bellard | vmdk_open, |
434 | ea2384d3 | bellard | vmdk_read, |
435 | ea2384d3 | bellard | vmdk_write, |
436 | ea2384d3 | bellard | vmdk_close, |
437 | 8979b227 | bellard | vmdk_create, |
438 | ea2384d3 | bellard | vmdk_is_allocated, |
439 | ea2384d3 | bellard | }; |