2 * Block driver for the VMDK format
4 * Copyright (c) 2004 Fabrice Bellard
5 * Copyright (c) 2005 Filip Navara
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "qemu-common.h"
27 #include "block_int.h"
30 #define VMDK3_MAGIC (('C' << 24) | ('O' << 16) | ('W' << 8) | 'D')
31 #define VMDK4_MAGIC (('K' << 24) | ('D' << 16) | ('M' << 8) | 'V')
36 uint32_t disk_sectors
;
38 uint32_t l1dir_offset
;
40 uint32_t file_sectors
;
43 uint32_t sectors_per_track
;
53 int32_t num_gtes_per_gte
;
59 } __attribute__((packed
)) VMDK4Header
;
61 #define L2_CACHE_SIZE 16
63 typedef struct BDRVVmdkState
{
65 int64_t l1_table_offset
;
66 int64_t l1_backup_table_offset
;
68 uint32_t *l1_backup_table
;
70 uint32_t l1_entry_sectors
;
74 uint32_t l2_cache_offsets
[L2_CACHE_SIZE
];
75 uint32_t l2_cache_counts
[L2_CACHE_SIZE
];
77 unsigned int cluster_sectors
;
82 typedef struct VmdkMetaData
{
84 unsigned int l1_index
;
85 unsigned int l2_index
;
86 unsigned int l2_offset
;
90 static int vmdk_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
96 magic
= be32_to_cpu(*(uint32_t *)buf
);
97 if (magic
== VMDK3_MAGIC
||
106 #define SECTOR_SIZE 512
107 #define DESC_SIZE 20*SECTOR_SIZE // 20 sectors of 512 bytes each
108 #define HEADER_SIZE 512 // first sector of 512 bytes
110 static uint32_t vmdk_read_cid(BlockDriverState
*bs
, int parent
)
112 BDRVVmdkState
*s
= bs
->opaque
;
113 char desc
[DESC_SIZE
];
115 const char *p_name
, *cid_str
;
118 /* the descriptor offset = 0x200 */
119 if (bdrv_pread(s
->hd
, 0x200, desc
, DESC_SIZE
) != DESC_SIZE
)
123 cid_str
= "parentCID";
124 cid_str_size
= sizeof("parentCID");
127 cid_str_size
= sizeof("CID");
130 if ((p_name
= strstr(desc
,cid_str
)) != NULL
) {
131 p_name
+= cid_str_size
;
132 sscanf(p_name
,"%x",&cid
);
138 static int vmdk_write_cid(BlockDriverState
*bs
, uint32_t cid
)
140 BDRVVmdkState
*s
= bs
->opaque
;
141 char desc
[DESC_SIZE
], tmp_desc
[DESC_SIZE
];
142 char *p_name
, *tmp_str
;
144 /* the descriptor offset = 0x200 */
145 if (bdrv_pread(s
->hd
, 0x200, desc
, DESC_SIZE
) != DESC_SIZE
)
148 tmp_str
= strstr(desc
,"parentCID");
149 pstrcpy(tmp_desc
, sizeof(tmp_desc
), tmp_str
);
150 if ((p_name
= strstr(desc
,"CID")) != NULL
) {
151 p_name
+= sizeof("CID");
152 snprintf(p_name
, sizeof(desc
) - (p_name
- desc
), "%x\n", cid
);
153 pstrcat(desc
, sizeof(desc
), tmp_desc
);
156 if (bdrv_pwrite(s
->hd
, 0x200, desc
, DESC_SIZE
) != DESC_SIZE
)
161 static int vmdk_is_cid_valid(BlockDriverState
*bs
)
164 BDRVVmdkState
*s
= bs
->opaque
;
165 BlockDriverState
*p_bs
= bs
->backing_hd
;
169 cur_pcid
= vmdk_read_cid(p_bs
,0);
170 if (s
->parent_cid
!= cur_pcid
)
179 static int vmdk_snapshot_create(const char *filename
, const char *backing_file
)
183 char *p_name
, *gd_buf
, *rgd_buf
;
184 const char *real_filename
, *temp_str
;
186 uint32_t gde_entries
, gd_size
;
187 int64_t gd_offset
, rgd_offset
, capacity
, gt_size
;
188 char p_desc
[DESC_SIZE
], s_desc
[DESC_SIZE
], hdr
[HEADER_SIZE
];
189 static const char desc_template
[] =
190 "# Disk DescriptorFile\n"
194 "createType=\"monolithicSparse\"\n"
195 "parentFileNameHint=\"%s\"\n"
197 "# Extent description\n"
198 "RW %u SPARSE \"%s\"\n"
200 "# The Disk Data Base \n"
204 snp_fd
= open(filename
, O_RDWR
| O_CREAT
| O_TRUNC
| O_BINARY
| O_LARGEFILE
, 0644);
207 p_fd
= open(backing_file
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
213 /* read the header */
214 if (lseek(p_fd
, 0x0, SEEK_SET
) == -1)
216 if (read(p_fd
, hdr
, HEADER_SIZE
) != HEADER_SIZE
)
219 /* write the header */
220 if (lseek(snp_fd
, 0x0, SEEK_SET
) == -1)
222 if (write(snp_fd
, hdr
, HEADER_SIZE
) == -1)
225 memset(&header
, 0, sizeof(header
));
226 memcpy(&header
,&hdr
[4], sizeof(header
)); // skip the VMDK4_MAGIC
228 ftruncate(snp_fd
, header
.grain_offset
<< 9);
229 /* the descriptor offset = 0x200 */
230 if (lseek(p_fd
, 0x200, SEEK_SET
) == -1)
232 if (read(p_fd
, p_desc
, DESC_SIZE
) != DESC_SIZE
)
235 if ((p_name
= strstr(p_desc
,"CID")) != NULL
) {
236 p_name
+= sizeof("CID");
237 sscanf(p_name
,"%x",&p_cid
);
240 real_filename
= filename
;
241 if ((temp_str
= strrchr(real_filename
, '\\')) != NULL
)
242 real_filename
= temp_str
+ 1;
243 if ((temp_str
= strrchr(real_filename
, '/')) != NULL
)
244 real_filename
= temp_str
+ 1;
245 if ((temp_str
= strrchr(real_filename
, ':')) != NULL
)
246 real_filename
= temp_str
+ 1;
248 snprintf(s_desc
, sizeof(s_desc
), desc_template
, p_cid
, p_cid
, backing_file
,
249 (uint32_t)header
.capacity
, real_filename
);
251 /* write the descriptor */
252 if (lseek(snp_fd
, 0x200, SEEK_SET
) == -1)
254 if (write(snp_fd
, s_desc
, strlen(s_desc
)) == -1)
257 gd_offset
= header
.gd_offset
* SECTOR_SIZE
; // offset of GD table
258 rgd_offset
= header
.rgd_offset
* SECTOR_SIZE
; // offset of RGD table
259 capacity
= header
.capacity
* SECTOR_SIZE
; // Extent size
261 * Each GDE span 32M disk, means:
262 * 512 GTE per GT, each GTE points to grain
264 gt_size
= (int64_t)header
.num_gtes_per_gte
* header
.granularity
* SECTOR_SIZE
;
267 gde_entries
= (uint32_t)(capacity
/ gt_size
); // number of gde/rgde
268 gd_size
= gde_entries
* sizeof(uint32_t);
271 rgd_buf
= qemu_malloc(gd_size
);
272 if (lseek(p_fd
, rgd_offset
, SEEK_SET
) == -1)
274 if (read(p_fd
, rgd_buf
, gd_size
) != gd_size
)
276 if (lseek(snp_fd
, rgd_offset
, SEEK_SET
) == -1)
278 if (write(snp_fd
, rgd_buf
, gd_size
) == -1)
282 gd_buf
= qemu_malloc(gd_size
);
283 if (lseek(p_fd
, gd_offset
, SEEK_SET
) == -1)
285 if (read(p_fd
, gd_buf
, gd_size
) != gd_size
)
287 if (lseek(snp_fd
, gd_offset
, SEEK_SET
) == -1)
289 if (write(snp_fd
, gd_buf
, gd_size
) == -1)
308 static void vmdk_parent_close(BlockDriverState
*bs
)
311 bdrv_close(bs
->backing_hd
);
314 static int parent_open
= 0;
315 static int vmdk_parent_open(BlockDriverState
*bs
, const char * filename
)
317 BDRVVmdkState
*s
= bs
->opaque
;
319 char desc
[DESC_SIZE
];
320 char parent_img_name
[1024];
322 /* the descriptor offset = 0x200 */
323 if (bdrv_pread(s
->hd
, 0x200, desc
, DESC_SIZE
) != DESC_SIZE
)
326 if ((p_name
= strstr(desc
,"parentFileNameHint")) != NULL
) {
328 struct stat file_buf
;
330 p_name
+= sizeof("parentFileNameHint") + 1;
331 if ((end_name
= strchr(p_name
,'\"')) == NULL
)
333 if ((end_name
- p_name
) > sizeof (bs
->backing_file
) - 1)
336 pstrcpy(bs
->backing_file
, end_name
- p_name
+ 1, p_name
);
337 if (stat(bs
->backing_file
, &file_buf
) != 0) {
338 path_combine(parent_img_name
, sizeof(parent_img_name
),
339 filename
, bs
->backing_file
);
341 pstrcpy(parent_img_name
, sizeof(parent_img_name
),
345 bs
->backing_hd
= bdrv_new("");
346 if (!bs
->backing_hd
) {
352 if (bdrv_open(bs
->backing_hd
, parent_img_name
, BDRV_O_RDONLY
) < 0)
360 static int vmdk_open(BlockDriverState
*bs
, const char *filename
, int flags
)
362 BDRVVmdkState
*s
= bs
->opaque
;
367 // Parent must be opened as RO.
368 flags
= BDRV_O_RDONLY
;
370 ret
= bdrv_file_open(&s
->hd
, filename
, flags
);
373 if (bdrv_pread(s
->hd
, 0, &magic
, sizeof(magic
)) != sizeof(magic
))
376 magic
= be32_to_cpu(magic
);
377 if (magic
== VMDK3_MAGIC
) {
380 if (bdrv_pread(s
->hd
, sizeof(magic
), &header
, sizeof(header
)) != sizeof(header
))
382 s
->cluster_sectors
= le32_to_cpu(header
.granularity
);
385 bs
->total_sectors
= le32_to_cpu(header
.disk_sectors
);
386 s
->l1_table_offset
= le32_to_cpu(header
.l1dir_offset
) << 9;
387 s
->l1_backup_table_offset
= 0;
388 s
->l1_entry_sectors
= s
->l2_size
* s
->cluster_sectors
;
389 } else if (magic
== VMDK4_MAGIC
) {
392 if (bdrv_pread(s
->hd
, sizeof(magic
), &header
, sizeof(header
)) != sizeof(header
))
394 bs
->total_sectors
= le64_to_cpu(header
.capacity
);
395 s
->cluster_sectors
= le64_to_cpu(header
.granularity
);
396 s
->l2_size
= le32_to_cpu(header
.num_gtes_per_gte
);
397 s
->l1_entry_sectors
= s
->l2_size
* s
->cluster_sectors
;
398 if (s
->l1_entry_sectors
<= 0)
400 s
->l1_size
= (bs
->total_sectors
+ s
->l1_entry_sectors
- 1)
401 / s
->l1_entry_sectors
;
402 s
->l1_table_offset
= le64_to_cpu(header
.rgd_offset
) << 9;
403 s
->l1_backup_table_offset
= le64_to_cpu(header
.gd_offset
) << 9;
410 // try to open parent images, if exist
411 if (vmdk_parent_open(bs
, filename
) != 0)
413 // write the CID once after the image creation
414 s
->parent_cid
= vmdk_read_cid(bs
,1);
419 /* read the L1 table */
420 l1_size
= s
->l1_size
* sizeof(uint32_t);
421 s
->l1_table
= qemu_malloc(l1_size
);
422 if (bdrv_pread(s
->hd
, s
->l1_table_offset
, s
->l1_table
, l1_size
) != l1_size
)
424 for(i
= 0; i
< s
->l1_size
; i
++) {
425 le32_to_cpus(&s
->l1_table
[i
]);
428 if (s
->l1_backup_table_offset
) {
429 s
->l1_backup_table
= qemu_malloc(l1_size
);
430 if (bdrv_pread(s
->hd
, s
->l1_backup_table_offset
, s
->l1_backup_table
, l1_size
) != l1_size
)
432 for(i
= 0; i
< s
->l1_size
; i
++) {
433 le32_to_cpus(&s
->l1_backup_table
[i
]);
437 s
->l2_cache
= qemu_malloc(s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint32_t));
440 qemu_free(s
->l1_backup_table
);
441 qemu_free(s
->l1_table
);
442 qemu_free(s
->l2_cache
);
447 static uint64_t get_cluster_offset(BlockDriverState
*bs
, VmdkMetaData
*m_data
,
448 uint64_t offset
, int allocate
);
450 static int get_whole_cluster(BlockDriverState
*bs
, uint64_t cluster_offset
,
451 uint64_t offset
, int allocate
)
453 BDRVVmdkState
*s
= bs
->opaque
;
454 uint8_t whole_grain
[s
->cluster_sectors
*512]; // 128 sectors * 512 bytes each = grain size 64KB
456 // we will be here if it's first write on non-exist grain(cluster).
457 // try to read from parent image, if exist
458 if (bs
->backing_hd
) {
461 if (!vmdk_is_cid_valid(bs
))
464 ret
= bdrv_read(bs
->backing_hd
, offset
>> 9, whole_grain
,
470 //Write grain only into the active image
471 ret
= bdrv_write(s
->hd
, cluster_offset
, whole_grain
,
480 static int vmdk_L2update(BlockDriverState
*bs
, VmdkMetaData
*m_data
)
482 BDRVVmdkState
*s
= bs
->opaque
;
484 /* update L2 table */
485 if (bdrv_pwrite(s
->hd
, ((int64_t)m_data
->l2_offset
* 512) + (m_data
->l2_index
* sizeof(m_data
->offset
)),
486 &(m_data
->offset
), sizeof(m_data
->offset
)) != sizeof(m_data
->offset
))
488 /* update backup L2 table */
489 if (s
->l1_backup_table_offset
!= 0) {
490 m_data
->l2_offset
= s
->l1_backup_table
[m_data
->l1_index
];
491 if (bdrv_pwrite(s
->hd
, ((int64_t)m_data
->l2_offset
* 512) + (m_data
->l2_index
* sizeof(m_data
->offset
)),
492 &(m_data
->offset
), sizeof(m_data
->offset
)) != sizeof(m_data
->offset
))
499 static uint64_t get_cluster_offset(BlockDriverState
*bs
, VmdkMetaData
*m_data
,
500 uint64_t offset
, int allocate
)
502 BDRVVmdkState
*s
= bs
->opaque
;
503 unsigned int l1_index
, l2_offset
, l2_index
;
505 uint32_t min_count
, *l2_table
, tmp
= 0;
506 uint64_t cluster_offset
;
511 l1_index
= (offset
>> 9) / s
->l1_entry_sectors
;
512 if (l1_index
>= s
->l1_size
)
514 l2_offset
= s
->l1_table
[l1_index
];
517 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
518 if (l2_offset
== s
->l2_cache_offsets
[i
]) {
519 /* increment the hit count */
520 if (++s
->l2_cache_counts
[i
] == 0xffffffff) {
521 for(j
= 0; j
< L2_CACHE_SIZE
; j
++) {
522 s
->l2_cache_counts
[j
] >>= 1;
525 l2_table
= s
->l2_cache
+ (i
* s
->l2_size
);
529 /* not found: load a new entry in the least used one */
531 min_count
= 0xffffffff;
532 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
533 if (s
->l2_cache_counts
[i
] < min_count
) {
534 min_count
= s
->l2_cache_counts
[i
];
538 l2_table
= s
->l2_cache
+ (min_index
* s
->l2_size
);
539 if (bdrv_pread(s
->hd
, (int64_t)l2_offset
* 512, l2_table
, s
->l2_size
* sizeof(uint32_t)) !=
540 s
->l2_size
* sizeof(uint32_t))
543 s
->l2_cache_offsets
[min_index
] = l2_offset
;
544 s
->l2_cache_counts
[min_index
] = 1;
546 l2_index
= ((offset
>> 9) / s
->cluster_sectors
) % s
->l2_size
;
547 cluster_offset
= le32_to_cpu(l2_table
[l2_index
]);
549 if (!cluster_offset
) {
552 // Avoid the L2 tables update for the images that have snapshots.
554 cluster_offset
= bdrv_getlength(s
->hd
);
555 bdrv_truncate(s
->hd
, cluster_offset
+ (s
->cluster_sectors
<< 9));
557 cluster_offset
>>= 9;
558 tmp
= cpu_to_le32(cluster_offset
);
559 l2_table
[l2_index
] = tmp
;
561 /* First of all we write grain itself, to avoid race condition
562 * that may to corrupt the image.
563 * This problem may occur because of insufficient space on host disk
564 * or inappropriate VM shutdown.
566 if (get_whole_cluster(bs
, cluster_offset
, offset
, allocate
) == -1)
570 m_data
->offset
= tmp
;
571 m_data
->l1_index
= l1_index
;
572 m_data
->l2_index
= l2_index
;
573 m_data
->l2_offset
= l2_offset
;
577 cluster_offset
<<= 9;
578 return cluster_offset
;
581 static int vmdk_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
582 int nb_sectors
, int *pnum
)
584 BDRVVmdkState
*s
= bs
->opaque
;
585 int index_in_cluster
, n
;
586 uint64_t cluster_offset
;
588 cluster_offset
= get_cluster_offset(bs
, NULL
, sector_num
<< 9, 0);
589 index_in_cluster
= sector_num
% s
->cluster_sectors
;
590 n
= s
->cluster_sectors
- index_in_cluster
;
594 return (cluster_offset
!= 0);
597 static int vmdk_read(BlockDriverState
*bs
, int64_t sector_num
,
598 uint8_t *buf
, int nb_sectors
)
600 BDRVVmdkState
*s
= bs
->opaque
;
601 int index_in_cluster
, n
, ret
;
602 uint64_t cluster_offset
;
604 while (nb_sectors
> 0) {
605 cluster_offset
= get_cluster_offset(bs
, NULL
, sector_num
<< 9, 0);
606 index_in_cluster
= sector_num
% s
->cluster_sectors
;
607 n
= s
->cluster_sectors
- index_in_cluster
;
610 if (!cluster_offset
) {
611 // try to read from parent image, if exist
612 if (bs
->backing_hd
) {
613 if (!vmdk_is_cid_valid(bs
))
615 ret
= bdrv_read(bs
->backing_hd
, sector_num
, buf
, n
);
619 memset(buf
, 0, 512 * n
);
622 if(bdrv_pread(s
->hd
, cluster_offset
+ index_in_cluster
* 512, buf
, n
* 512) != n
* 512)
632 static int vmdk_write(BlockDriverState
*bs
, int64_t sector_num
,
633 const uint8_t *buf
, int nb_sectors
)
635 BDRVVmdkState
*s
= bs
->opaque
;
637 int index_in_cluster
, n
;
638 uint64_t cluster_offset
;
639 static int cid_update
= 0;
641 if (sector_num
> bs
->total_sectors
) {
643 "(VMDK) Wrong offset: sector_num=0x%" PRIx64
644 " total_sectors=0x%" PRIx64
"\n",
645 sector_num
, bs
->total_sectors
);
649 while (nb_sectors
> 0) {
650 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
651 n
= s
->cluster_sectors
- index_in_cluster
;
654 cluster_offset
= get_cluster_offset(bs
, &m_data
, sector_num
<< 9, 1);
658 if (bdrv_pwrite(s
->hd
, cluster_offset
+ index_in_cluster
* 512, buf
, n
* 512) != n
* 512)
661 /* update L2 tables */
662 if (vmdk_L2update(bs
, &m_data
) == -1)
669 // update CID on the first write every time the virtual disk is opened
671 vmdk_write_cid(bs
, time(NULL
));
678 static int vmdk_create(const char *filename
, QEMUOptionParameter
*options
)
682 uint32_t tmp
, magic
, grains
, gd_size
, gt_size
, gt_count
;
683 static const char desc_template
[] =
684 "# Disk DescriptorFile\n"
687 "parentCID=ffffffff\n"
688 "createType=\"monolithicSparse\"\n"
690 "# Extent description\n"
691 "RW %" PRId64
" SPARSE \"%s\"\n"
693 "# The Disk Data Base \n"
696 "ddb.virtualHWVersion = \"%d\"\n"
697 "ddb.geometry.cylinders = \"%" PRId64
"\"\n"
698 "ddb.geometry.heads = \"16\"\n"
699 "ddb.geometry.sectors = \"63\"\n"
700 "ddb.adapterType = \"ide\"\n";
702 const char *real_filename
, *temp_str
;
703 int64_t total_size
= 0;
704 const char *backing_file
= NULL
;
708 while (options
&& options
->name
) {
709 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
710 total_size
= options
->value
.n
/ 512;
711 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FILE
)) {
712 backing_file
= options
->value
.s
;
713 } else if (!strcmp(options
->name
, BLOCK_OPT_COMPAT6
)) {
714 flags
|= options
->value
.n
? BLOCK_FLAG_COMPAT6
: 0;
719 /* XXX: add support for backing file */
721 return vmdk_snapshot_create(filename
, backing_file
);
724 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
| O_LARGEFILE
,
728 magic
= cpu_to_be32(VMDK4_MAGIC
);
729 memset(&header
, 0, sizeof(header
));
730 header
.version
= cpu_to_le32(1);
731 header
.flags
= cpu_to_le32(3); /* ?? */
732 header
.capacity
= cpu_to_le64(total_size
);
733 header
.granularity
= cpu_to_le64(128);
734 header
.num_gtes_per_gte
= cpu_to_le32(512);
736 grains
= (total_size
+ header
.granularity
- 1) / header
.granularity
;
737 gt_size
= ((header
.num_gtes_per_gte
* sizeof(uint32_t)) + 511) >> 9;
738 gt_count
= (grains
+ header
.num_gtes_per_gte
- 1) / header
.num_gtes_per_gte
;
739 gd_size
= (gt_count
* sizeof(uint32_t) + 511) >> 9;
741 header
.desc_offset
= 1;
742 header
.desc_size
= 20;
743 header
.rgd_offset
= header
.desc_offset
+ header
.desc_size
;
744 header
.gd_offset
= header
.rgd_offset
+ gd_size
+ (gt_size
* gt_count
);
745 header
.grain_offset
=
746 ((header
.gd_offset
+ gd_size
+ (gt_size
* gt_count
) +
747 header
.granularity
- 1) / header
.granularity
) *
750 header
.desc_offset
= cpu_to_le64(header
.desc_offset
);
751 header
.desc_size
= cpu_to_le64(header
.desc_size
);
752 header
.rgd_offset
= cpu_to_le64(header
.rgd_offset
);
753 header
.gd_offset
= cpu_to_le64(header
.gd_offset
);
754 header
.grain_offset
= cpu_to_le64(header
.grain_offset
);
756 header
.check_bytes
[0] = 0xa;
757 header
.check_bytes
[1] = 0x20;
758 header
.check_bytes
[2] = 0xd;
759 header
.check_bytes
[3] = 0xa;
761 /* write all the data */
762 write(fd
, &magic
, sizeof(magic
));
763 write(fd
, &header
, sizeof(header
));
765 ftruncate(fd
, header
.grain_offset
<< 9);
767 /* write grain directory */
768 lseek(fd
, le64_to_cpu(header
.rgd_offset
) << 9, SEEK_SET
);
769 for (i
= 0, tmp
= header
.rgd_offset
+ gd_size
;
770 i
< gt_count
; i
++, tmp
+= gt_size
)
771 write(fd
, &tmp
, sizeof(tmp
));
773 /* write backup grain directory */
774 lseek(fd
, le64_to_cpu(header
.gd_offset
) << 9, SEEK_SET
);
775 for (i
= 0, tmp
= header
.gd_offset
+ gd_size
;
776 i
< gt_count
; i
++, tmp
+= gt_size
)
777 write(fd
, &tmp
, sizeof(tmp
));
779 /* compose the descriptor */
780 real_filename
= filename
;
781 if ((temp_str
= strrchr(real_filename
, '\\')) != NULL
)
782 real_filename
= temp_str
+ 1;
783 if ((temp_str
= strrchr(real_filename
, '/')) != NULL
)
784 real_filename
= temp_str
+ 1;
785 if ((temp_str
= strrchr(real_filename
, ':')) != NULL
)
786 real_filename
= temp_str
+ 1;
787 snprintf(desc
, sizeof(desc
), desc_template
, (unsigned int)time(NULL
),
788 total_size
, real_filename
,
789 (flags
& BLOCK_FLAG_COMPAT6
? 6 : 4),
790 total_size
/ (int64_t)(63 * 16));
792 /* write the descriptor */
793 lseek(fd
, le64_to_cpu(header
.desc_offset
) << 9, SEEK_SET
);
794 write(fd
, desc
, strlen(desc
));
800 static void vmdk_close(BlockDriverState
*bs
)
802 BDRVVmdkState
*s
= bs
->opaque
;
804 qemu_free(s
->l1_table
);
805 qemu_free(s
->l2_cache
);
806 // try to close parent image, if exist
807 vmdk_parent_close(s
->hd
);
811 static void vmdk_flush(BlockDriverState
*bs
)
813 BDRVVmdkState
*s
= bs
->opaque
;
818 static QEMUOptionParameter vmdk_create_options
[] = {
820 .name
= BLOCK_OPT_SIZE
,
822 .help
= "Virtual disk size"
825 .name
= BLOCK_OPT_BACKING_FILE
,
827 .help
= "File name of a base image"
830 .name
= BLOCK_OPT_COMPAT6
,
832 .help
= "VMDK version 6 image"
837 static BlockDriver bdrv_vmdk
= {
838 .format_name
= "vmdk",
839 .instance_size
= sizeof(BDRVVmdkState
),
840 .bdrv_probe
= vmdk_probe
,
841 .bdrv_open
= vmdk_open
,
842 .bdrv_read
= vmdk_read
,
843 .bdrv_write
= vmdk_write
,
844 .bdrv_close
= vmdk_close
,
845 .bdrv_create
= vmdk_create
,
846 .bdrv_flush
= vmdk_flush
,
847 .bdrv_is_allocated
= vmdk_is_allocated
,
849 .create_options
= vmdk_create_options
,
852 static void bdrv_vmdk_init(void)
854 bdrv_register(&bdrv_vmdk
);
857 block_init(bdrv_vmdk_init
);