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
{
64 int64_t l1_table_offset
;
65 int64_t l1_backup_table_offset
;
67 uint32_t *l1_backup_table
;
69 uint32_t l1_entry_sectors
;
73 uint32_t l2_cache_offsets
[L2_CACHE_SIZE
];
74 uint32_t l2_cache_counts
[L2_CACHE_SIZE
];
76 unsigned int cluster_sectors
;
80 typedef struct VmdkMetaData
{
82 unsigned int l1_index
;
83 unsigned int l2_index
;
84 unsigned int l2_offset
;
88 static int vmdk_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
94 magic
= be32_to_cpu(*(uint32_t *)buf
);
95 if (magic
== VMDK3_MAGIC
||
104 #define SECTOR_SIZE 512
105 #define DESC_SIZE 20*SECTOR_SIZE // 20 sectors of 512 bytes each
106 #define HEADER_SIZE 512 // first sector of 512 bytes
108 static uint32_t vmdk_read_cid(BlockDriverState
*bs
, int parent
)
110 char desc
[DESC_SIZE
];
112 const char *p_name
, *cid_str
;
115 /* the descriptor offset = 0x200 */
116 if (bdrv_pread(bs
->file
, 0x200, desc
, DESC_SIZE
) != DESC_SIZE
)
120 cid_str
= "parentCID";
121 cid_str_size
= sizeof("parentCID");
124 cid_str_size
= sizeof("CID");
127 if ((p_name
= strstr(desc
,cid_str
)) != NULL
) {
128 p_name
+= cid_str_size
;
129 sscanf(p_name
,"%x",&cid
);
135 static int vmdk_write_cid(BlockDriverState
*bs
, uint32_t cid
)
137 char desc
[DESC_SIZE
], tmp_desc
[DESC_SIZE
];
138 char *p_name
, *tmp_str
;
140 /* the descriptor offset = 0x200 */
141 if (bdrv_pread(bs
->file
, 0x200, desc
, DESC_SIZE
) != DESC_SIZE
)
144 tmp_str
= strstr(desc
,"parentCID");
145 pstrcpy(tmp_desc
, sizeof(tmp_desc
), tmp_str
);
146 if ((p_name
= strstr(desc
,"CID")) != NULL
) {
147 p_name
+= sizeof("CID");
148 snprintf(p_name
, sizeof(desc
) - (p_name
- desc
), "%x\n", cid
);
149 pstrcat(desc
, sizeof(desc
), tmp_desc
);
152 if (bdrv_pwrite_sync(bs
->file
, 0x200, desc
, DESC_SIZE
) < 0)
157 static int vmdk_is_cid_valid(BlockDriverState
*bs
)
160 BDRVVmdkState
*s
= bs
->opaque
;
161 BlockDriverState
*p_bs
= bs
->backing_hd
;
165 cur_pcid
= vmdk_read_cid(p_bs
,0);
166 if (s
->parent_cid
!= cur_pcid
)
175 static int vmdk_snapshot_create(const char *filename
, const char *backing_file
)
180 char *p_name
, *gd_buf
, *rgd_buf
;
181 const char *real_filename
, *temp_str
;
183 uint32_t gde_entries
, gd_size
;
184 int64_t gd_offset
, rgd_offset
, capacity
, gt_size
;
185 char p_desc
[DESC_SIZE
], s_desc
[DESC_SIZE
], hdr
[HEADER_SIZE
];
186 static const char desc_template
[] =
187 "# Disk DescriptorFile\n"
191 "createType=\"monolithicSparse\"\n"
192 "parentFileNameHint=\"%s\"\n"
194 "# Extent description\n"
195 "RW %u SPARSE \"%s\"\n"
197 "# The Disk Data Base \n"
201 snp_fd
= open(filename
, O_RDWR
| O_CREAT
| O_TRUNC
| O_BINARY
| O_LARGEFILE
, 0644);
204 p_fd
= open(backing_file
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
210 /* read the header */
211 if (lseek(p_fd
, 0x0, SEEK_SET
) == -1) {
215 if (read(p_fd
, hdr
, HEADER_SIZE
) != HEADER_SIZE
) {
220 /* write the header */
221 if (lseek(snp_fd
, 0x0, SEEK_SET
) == -1) {
225 if (write(snp_fd
, hdr
, HEADER_SIZE
) == -1) {
230 memset(&header
, 0, sizeof(header
));
231 memcpy(&header
,&hdr
[4], sizeof(header
)); // skip the VMDK4_MAGIC
233 if (ftruncate(snp_fd
, header
.grain_offset
<< 9)) {
237 /* the descriptor offset = 0x200 */
238 if (lseek(p_fd
, 0x200, SEEK_SET
) == -1) {
242 if (read(p_fd
, p_desc
, DESC_SIZE
) != DESC_SIZE
) {
247 if ((p_name
= strstr(p_desc
,"CID")) != NULL
) {
248 p_name
+= sizeof("CID");
249 sscanf(p_name
,"%x",&p_cid
);
252 real_filename
= filename
;
253 if ((temp_str
= strrchr(real_filename
, '\\')) != NULL
)
254 real_filename
= temp_str
+ 1;
255 if ((temp_str
= strrchr(real_filename
, '/')) != NULL
)
256 real_filename
= temp_str
+ 1;
257 if ((temp_str
= strrchr(real_filename
, ':')) != NULL
)
258 real_filename
= temp_str
+ 1;
260 snprintf(s_desc
, sizeof(s_desc
), desc_template
, p_cid
, p_cid
, backing_file
,
261 (uint32_t)header
.capacity
, real_filename
);
263 /* write the descriptor */
264 if (lseek(snp_fd
, 0x200, SEEK_SET
) == -1) {
268 if (write(snp_fd
, s_desc
, strlen(s_desc
)) == -1) {
273 gd_offset
= header
.gd_offset
* SECTOR_SIZE
; // offset of GD table
274 rgd_offset
= header
.rgd_offset
* SECTOR_SIZE
; // offset of RGD table
275 capacity
= header
.capacity
* SECTOR_SIZE
; // Extent size
277 * Each GDE span 32M disk, means:
278 * 512 GTE per GT, each GTE points to grain
280 gt_size
= (int64_t)header
.num_gtes_per_gte
* header
.granularity
* SECTOR_SIZE
;
285 gde_entries
= (uint32_t)(capacity
/ gt_size
); // number of gde/rgde
286 gd_size
= gde_entries
* sizeof(uint32_t);
289 rgd_buf
= qemu_malloc(gd_size
);
290 if (lseek(p_fd
, rgd_offset
, SEEK_SET
) == -1) {
294 if (read(p_fd
, rgd_buf
, gd_size
) != gd_size
) {
298 if (lseek(snp_fd
, rgd_offset
, SEEK_SET
) == -1) {
302 if (write(snp_fd
, rgd_buf
, gd_size
) == -1) {
308 gd_buf
= qemu_malloc(gd_size
);
309 if (lseek(p_fd
, gd_offset
, SEEK_SET
) == -1) {
313 if (read(p_fd
, gd_buf
, gd_size
) != gd_size
) {
317 if (lseek(snp_fd
, gd_offset
, SEEK_SET
) == -1) {
321 if (write(snp_fd
, gd_buf
, gd_size
) == -1) {
337 static int vmdk_parent_open(BlockDriverState
*bs
)
340 char desc
[DESC_SIZE
];
342 /* the descriptor offset = 0x200 */
343 if (bdrv_pread(bs
->file
, 0x200, desc
, DESC_SIZE
) != DESC_SIZE
)
346 if ((p_name
= strstr(desc
,"parentFileNameHint")) != NULL
) {
349 p_name
+= sizeof("parentFileNameHint") + 1;
350 if ((end_name
= strchr(p_name
,'\"')) == NULL
)
352 if ((end_name
- p_name
) > sizeof (bs
->backing_file
) - 1)
355 pstrcpy(bs
->backing_file
, end_name
- p_name
+ 1, p_name
);
361 static int vmdk_open(BlockDriverState
*bs
, int flags
)
363 BDRVVmdkState
*s
= bs
->opaque
;
367 if (bdrv_pread(bs
->file
, 0, &magic
, sizeof(magic
)) != sizeof(magic
))
370 magic
= be32_to_cpu(magic
);
371 if (magic
== VMDK3_MAGIC
) {
374 if (bdrv_pread(bs
->file
, sizeof(magic
), &header
, sizeof(header
)) != sizeof(header
))
376 s
->cluster_sectors
= le32_to_cpu(header
.granularity
);
379 bs
->total_sectors
= le32_to_cpu(header
.disk_sectors
);
380 s
->l1_table_offset
= le32_to_cpu(header
.l1dir_offset
) << 9;
381 s
->l1_backup_table_offset
= 0;
382 s
->l1_entry_sectors
= s
->l2_size
* s
->cluster_sectors
;
383 } else if (magic
== VMDK4_MAGIC
) {
386 if (bdrv_pread(bs
->file
, sizeof(magic
), &header
, sizeof(header
)) != sizeof(header
))
388 bs
->total_sectors
= le64_to_cpu(header
.capacity
);
389 s
->cluster_sectors
= le64_to_cpu(header
.granularity
);
390 s
->l2_size
= le32_to_cpu(header
.num_gtes_per_gte
);
391 s
->l1_entry_sectors
= s
->l2_size
* s
->cluster_sectors
;
392 if (s
->l1_entry_sectors
<= 0)
394 s
->l1_size
= (bs
->total_sectors
+ s
->l1_entry_sectors
- 1)
395 / s
->l1_entry_sectors
;
396 s
->l1_table_offset
= le64_to_cpu(header
.rgd_offset
) << 9;
397 s
->l1_backup_table_offset
= le64_to_cpu(header
.gd_offset
) << 9;
399 // try to open parent images, if exist
400 if (vmdk_parent_open(bs
) != 0)
402 // write the CID once after the image creation
403 s
->parent_cid
= vmdk_read_cid(bs
,1);
408 /* read the L1 table */
409 l1_size
= s
->l1_size
* sizeof(uint32_t);
410 s
->l1_table
= qemu_malloc(l1_size
);
411 if (bdrv_pread(bs
->file
, s
->l1_table_offset
, s
->l1_table
, l1_size
) != l1_size
)
413 for(i
= 0; i
< s
->l1_size
; i
++) {
414 le32_to_cpus(&s
->l1_table
[i
]);
417 if (s
->l1_backup_table_offset
) {
418 s
->l1_backup_table
= qemu_malloc(l1_size
);
419 if (bdrv_pread(bs
->file
, s
->l1_backup_table_offset
, s
->l1_backup_table
, l1_size
) != l1_size
)
421 for(i
= 0; i
< s
->l1_size
; i
++) {
422 le32_to_cpus(&s
->l1_backup_table
[i
]);
426 s
->l2_cache
= qemu_malloc(s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint32_t));
429 qemu_free(s
->l1_backup_table
);
430 qemu_free(s
->l1_table
);
431 qemu_free(s
->l2_cache
);
435 static uint64_t get_cluster_offset(BlockDriverState
*bs
, VmdkMetaData
*m_data
,
436 uint64_t offset
, int allocate
);
438 static int get_whole_cluster(BlockDriverState
*bs
, uint64_t cluster_offset
,
439 uint64_t offset
, int allocate
)
441 BDRVVmdkState
*s
= bs
->opaque
;
442 uint8_t whole_grain
[s
->cluster_sectors
*512]; // 128 sectors * 512 bytes each = grain size 64KB
444 // we will be here if it's first write on non-exist grain(cluster).
445 // try to read from parent image, if exist
446 if (bs
->backing_hd
) {
449 if (!vmdk_is_cid_valid(bs
))
452 ret
= bdrv_read(bs
->backing_hd
, offset
>> 9, whole_grain
,
458 //Write grain only into the active image
459 ret
= bdrv_write(bs
->file
, cluster_offset
, whole_grain
,
468 static int vmdk_L2update(BlockDriverState
*bs
, VmdkMetaData
*m_data
)
470 BDRVVmdkState
*s
= bs
->opaque
;
472 /* update L2 table */
473 if (bdrv_pwrite_sync(bs
->file
, ((int64_t)m_data
->l2_offset
* 512) + (m_data
->l2_index
* sizeof(m_data
->offset
)),
474 &(m_data
->offset
), sizeof(m_data
->offset
)) < 0)
476 /* update backup L2 table */
477 if (s
->l1_backup_table_offset
!= 0) {
478 m_data
->l2_offset
= s
->l1_backup_table
[m_data
->l1_index
];
479 if (bdrv_pwrite_sync(bs
->file
, ((int64_t)m_data
->l2_offset
* 512) + (m_data
->l2_index
* sizeof(m_data
->offset
)),
480 &(m_data
->offset
), sizeof(m_data
->offset
)) < 0)
487 static uint64_t get_cluster_offset(BlockDriverState
*bs
, VmdkMetaData
*m_data
,
488 uint64_t offset
, int allocate
)
490 BDRVVmdkState
*s
= bs
->opaque
;
491 unsigned int l1_index
, l2_offset
, l2_index
;
493 uint32_t min_count
, *l2_table
, tmp
= 0;
494 uint64_t cluster_offset
;
499 l1_index
= (offset
>> 9) / s
->l1_entry_sectors
;
500 if (l1_index
>= s
->l1_size
)
502 l2_offset
= s
->l1_table
[l1_index
];
505 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
506 if (l2_offset
== s
->l2_cache_offsets
[i
]) {
507 /* increment the hit count */
508 if (++s
->l2_cache_counts
[i
] == 0xffffffff) {
509 for(j
= 0; j
< L2_CACHE_SIZE
; j
++) {
510 s
->l2_cache_counts
[j
] >>= 1;
513 l2_table
= s
->l2_cache
+ (i
* s
->l2_size
);
517 /* not found: load a new entry in the least used one */
519 min_count
= 0xffffffff;
520 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
521 if (s
->l2_cache_counts
[i
] < min_count
) {
522 min_count
= s
->l2_cache_counts
[i
];
526 l2_table
= s
->l2_cache
+ (min_index
* s
->l2_size
);
527 if (bdrv_pread(bs
->file
, (int64_t)l2_offset
* 512, l2_table
, s
->l2_size
* sizeof(uint32_t)) !=
528 s
->l2_size
* sizeof(uint32_t))
531 s
->l2_cache_offsets
[min_index
] = l2_offset
;
532 s
->l2_cache_counts
[min_index
] = 1;
534 l2_index
= ((offset
>> 9) / s
->cluster_sectors
) % s
->l2_size
;
535 cluster_offset
= le32_to_cpu(l2_table
[l2_index
]);
537 if (!cluster_offset
) {
541 // Avoid the L2 tables update for the images that have snapshots.
542 cluster_offset
= bdrv_getlength(bs
->file
);
543 bdrv_truncate(bs
->file
, cluster_offset
+ (s
->cluster_sectors
<< 9));
545 cluster_offset
>>= 9;
546 tmp
= cpu_to_le32(cluster_offset
);
547 l2_table
[l2_index
] = tmp
;
549 /* First of all we write grain itself, to avoid race condition
550 * that may to corrupt the image.
551 * This problem may occur because of insufficient space on host disk
552 * or inappropriate VM shutdown.
554 if (get_whole_cluster(bs
, cluster_offset
, offset
, allocate
) == -1)
558 m_data
->offset
= tmp
;
559 m_data
->l1_index
= l1_index
;
560 m_data
->l2_index
= l2_index
;
561 m_data
->l2_offset
= l2_offset
;
565 cluster_offset
<<= 9;
566 return cluster_offset
;
569 static int vmdk_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
570 int nb_sectors
, int *pnum
)
572 BDRVVmdkState
*s
= bs
->opaque
;
573 int index_in_cluster
, n
;
574 uint64_t cluster_offset
;
576 cluster_offset
= get_cluster_offset(bs
, NULL
, sector_num
<< 9, 0);
577 index_in_cluster
= sector_num
% s
->cluster_sectors
;
578 n
= s
->cluster_sectors
- index_in_cluster
;
582 return (cluster_offset
!= 0);
585 static int vmdk_read(BlockDriverState
*bs
, int64_t sector_num
,
586 uint8_t *buf
, int nb_sectors
)
588 BDRVVmdkState
*s
= bs
->opaque
;
589 int index_in_cluster
, n
, ret
;
590 uint64_t cluster_offset
;
592 while (nb_sectors
> 0) {
593 cluster_offset
= get_cluster_offset(bs
, NULL
, sector_num
<< 9, 0);
594 index_in_cluster
= sector_num
% s
->cluster_sectors
;
595 n
= s
->cluster_sectors
- index_in_cluster
;
598 if (!cluster_offset
) {
599 // try to read from parent image, if exist
600 if (bs
->backing_hd
) {
601 if (!vmdk_is_cid_valid(bs
))
603 ret
= bdrv_read(bs
->backing_hd
, sector_num
, buf
, n
);
607 memset(buf
, 0, 512 * n
);
610 if(bdrv_pread(bs
->file
, cluster_offset
+ index_in_cluster
* 512, buf
, n
* 512) != n
* 512)
620 static int vmdk_write(BlockDriverState
*bs
, int64_t sector_num
,
621 const uint8_t *buf
, int nb_sectors
)
623 BDRVVmdkState
*s
= bs
->opaque
;
625 int index_in_cluster
, n
;
626 uint64_t cluster_offset
;
627 static int cid_update
= 0;
629 if (sector_num
> bs
->total_sectors
) {
631 "(VMDK) Wrong offset: sector_num=0x%" PRIx64
632 " total_sectors=0x%" PRIx64
"\n",
633 sector_num
, bs
->total_sectors
);
637 while (nb_sectors
> 0) {
638 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
639 n
= s
->cluster_sectors
- index_in_cluster
;
642 cluster_offset
= get_cluster_offset(bs
, &m_data
, sector_num
<< 9, 1);
646 if (bdrv_pwrite(bs
->file
, cluster_offset
+ index_in_cluster
* 512, buf
, n
* 512) != n
* 512)
649 /* update L2 tables */
650 if (vmdk_L2update(bs
, &m_data
) == -1)
657 // update CID on the first write every time the virtual disk is opened
659 vmdk_write_cid(bs
, time(NULL
));
666 static int vmdk_create(const char *filename
, QEMUOptionParameter
*options
)
670 uint32_t tmp
, magic
, grains
, gd_size
, gt_size
, gt_count
;
671 static const char desc_template
[] =
672 "# Disk DescriptorFile\n"
675 "parentCID=ffffffff\n"
676 "createType=\"monolithicSparse\"\n"
678 "# Extent description\n"
679 "RW %" PRId64
" SPARSE \"%s\"\n"
681 "# The Disk Data Base \n"
684 "ddb.virtualHWVersion = \"%d\"\n"
685 "ddb.geometry.cylinders = \"%" PRId64
"\"\n"
686 "ddb.geometry.heads = \"16\"\n"
687 "ddb.geometry.sectors = \"63\"\n"
688 "ddb.adapterType = \"ide\"\n";
690 const char *real_filename
, *temp_str
;
691 int64_t total_size
= 0;
692 const char *backing_file
= NULL
;
697 while (options
&& options
->name
) {
698 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
699 total_size
= options
->value
.n
/ 512;
700 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FILE
)) {
701 backing_file
= options
->value
.s
;
702 } else if (!strcmp(options
->name
, BLOCK_OPT_COMPAT6
)) {
703 flags
|= options
->value
.n
? BLOCK_FLAG_COMPAT6
: 0;
708 /* XXX: add support for backing file */
710 return vmdk_snapshot_create(filename
, backing_file
);
713 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
| O_LARGEFILE
,
717 magic
= cpu_to_be32(VMDK4_MAGIC
);
718 memset(&header
, 0, sizeof(header
));
719 header
.version
= cpu_to_le32(1);
720 header
.flags
= cpu_to_le32(3); /* ?? */
721 header
.capacity
= cpu_to_le64(total_size
);
722 header
.granularity
= cpu_to_le64(128);
723 header
.num_gtes_per_gte
= cpu_to_le32(512);
725 grains
= (total_size
+ header
.granularity
- 1) / header
.granularity
;
726 gt_size
= ((header
.num_gtes_per_gte
* sizeof(uint32_t)) + 511) >> 9;
727 gt_count
= (grains
+ header
.num_gtes_per_gte
- 1) / header
.num_gtes_per_gte
;
728 gd_size
= (gt_count
* sizeof(uint32_t) + 511) >> 9;
730 header
.desc_offset
= 1;
731 header
.desc_size
= 20;
732 header
.rgd_offset
= header
.desc_offset
+ header
.desc_size
;
733 header
.gd_offset
= header
.rgd_offset
+ gd_size
+ (gt_size
* gt_count
);
734 header
.grain_offset
=
735 ((header
.gd_offset
+ gd_size
+ (gt_size
* gt_count
) +
736 header
.granularity
- 1) / header
.granularity
) *
739 header
.desc_offset
= cpu_to_le64(header
.desc_offset
);
740 header
.desc_size
= cpu_to_le64(header
.desc_size
);
741 header
.rgd_offset
= cpu_to_le64(header
.rgd_offset
);
742 header
.gd_offset
= cpu_to_le64(header
.gd_offset
);
743 header
.grain_offset
= cpu_to_le64(header
.grain_offset
);
745 header
.check_bytes
[0] = 0xa;
746 header
.check_bytes
[1] = 0x20;
747 header
.check_bytes
[2] = 0xd;
748 header
.check_bytes
[3] = 0xa;
750 /* write all the data */
751 ret
= qemu_write_full(fd
, &magic
, sizeof(magic
));
752 if (ret
!= sizeof(magic
)) {
756 ret
= qemu_write_full(fd
, &header
, sizeof(header
));
757 if (ret
!= sizeof(header
)) {
762 ret
= ftruncate(fd
, header
.grain_offset
<< 9);
768 /* write grain directory */
769 lseek(fd
, le64_to_cpu(header
.rgd_offset
) << 9, SEEK_SET
);
770 for (i
= 0, tmp
= header
.rgd_offset
+ gd_size
;
771 i
< gt_count
; i
++, tmp
+= gt_size
) {
772 ret
= qemu_write_full(fd
, &tmp
, sizeof(tmp
));
773 if (ret
!= sizeof(tmp
)) {
779 /* write backup grain directory */
780 lseek(fd
, le64_to_cpu(header
.gd_offset
) << 9, SEEK_SET
);
781 for (i
= 0, tmp
= header
.gd_offset
+ gd_size
;
782 i
< gt_count
; i
++, tmp
+= gt_size
) {
783 ret
= qemu_write_full(fd
, &tmp
, sizeof(tmp
));
784 if (ret
!= sizeof(tmp
)) {
790 /* compose the descriptor */
791 real_filename
= filename
;
792 if ((temp_str
= strrchr(real_filename
, '\\')) != NULL
)
793 real_filename
= temp_str
+ 1;
794 if ((temp_str
= strrchr(real_filename
, '/')) != NULL
)
795 real_filename
= temp_str
+ 1;
796 if ((temp_str
= strrchr(real_filename
, ':')) != NULL
)
797 real_filename
= temp_str
+ 1;
798 snprintf(desc
, sizeof(desc
), desc_template
, (unsigned int)time(NULL
),
799 total_size
, real_filename
,
800 (flags
& BLOCK_FLAG_COMPAT6
? 6 : 4),
801 total_size
/ (int64_t)(63 * 16));
803 /* write the descriptor */
804 lseek(fd
, le64_to_cpu(header
.desc_offset
) << 9, SEEK_SET
);
805 ret
= qemu_write_full(fd
, desc
, strlen(desc
));
806 if (ret
!= strlen(desc
)) {
817 static void vmdk_close(BlockDriverState
*bs
)
819 BDRVVmdkState
*s
= bs
->opaque
;
821 qemu_free(s
->l1_table
);
822 qemu_free(s
->l2_cache
);
825 static int vmdk_flush(BlockDriverState
*bs
)
827 return bdrv_flush(bs
->file
);
831 static QEMUOptionParameter vmdk_create_options
[] = {
833 .name
= BLOCK_OPT_SIZE
,
835 .help
= "Virtual disk size"
838 .name
= BLOCK_OPT_BACKING_FILE
,
840 .help
= "File name of a base image"
843 .name
= BLOCK_OPT_COMPAT6
,
845 .help
= "VMDK version 6 image"
850 static BlockDriver bdrv_vmdk
= {
851 .format_name
= "vmdk",
852 .instance_size
= sizeof(BDRVVmdkState
),
853 .bdrv_probe
= vmdk_probe
,
854 .bdrv_open
= vmdk_open
,
855 .bdrv_read
= vmdk_read
,
856 .bdrv_write
= vmdk_write
,
857 .bdrv_close
= vmdk_close
,
858 .bdrv_create
= vmdk_create
,
859 .bdrv_flush
= vmdk_flush
,
860 .bdrv_is_allocated
= vmdk_is_allocated
,
862 .create_options
= vmdk_create_options
,
865 static void bdrv_vmdk_init(void)
867 bdrv_register(&bdrv_vmdk
);
870 block_init(bdrv_vmdk_init
);