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
25 #include <uuid/uuid.h>
28 #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
;
80 static int vmdk_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
86 magic
= be32_to_cpu(*(uint32_t *)buf
);
87 if (magic
== VMDK3_MAGIC
||
96 #define SECTOR_SIZE 512
97 #define DESC_SIZE 20*SECTOR_SIZE // 20 sectors of 512 bytes each
98 #define HEADER_SIZE 512 // first sector of 512 bytes
100 static uint32_t vmdk_read_cid(int fd
, int parent
)
102 char desc
[DESC_SIZE
];
104 char *p_name
, *cid_str
;
108 /* the descriptor offset = 0x200 */
109 if (lseek(fd
, 0x200, SEEK_SET
) == -1)
111 if (read(fd
, desc
, DESC_SIZE
) != DESC_SIZE
)
115 cid_str
= "parentCID";
116 cid_str_size
= sizeof("parentCID");
119 cid_str_size
= sizeof("CID");
122 if ((p_name
= strstr(desc
,cid_str
)) != 0) {
123 p_name
+= cid_str_size
;
124 sscanf(p_name
,"%x",&cid
);
132 static int vmdk_write_cid(BlockDriverState
*bs
, uint32_t cid
)
134 BDRVVmdkState
*s
= bs
->opaque
;
135 char desc
[DESC_SIZE
], tmp_desc
[DESC_SIZE
];
136 char *p_name
, *tmp_str
;
139 /* the descriptor offset = 0x200 */
140 if (lseek(s
->fd
, 0x200, SEEK_SET
) == -1)
142 if (read(s
->fd
, desc
, DESC_SIZE
) != DESC_SIZE
)
145 tmp_str
= strstr(desc
,"parentCID");
146 strcpy(tmp_desc
, tmp_str
);
147 if ((p_name
= strstr(desc
,"CID")) != 0) {
148 p_name
+= sizeof("CID");
149 sprintf(p_name
,"%x\n",cid
);
150 strcat(desc
,tmp_desc
);
153 if (lseek(s
->fd
, 0x200, SEEK_SET
) == -1)
155 if (write(s
->fd
, &desc
, DESC_SIZE
) != DESC_SIZE
)
164 static int vmdk_is_cid_valid(BlockDriverState
*bs
)
167 BlockDriverState
*p_bs
= bs
->bs_par_table
;
171 BDRVVmdkState
*s
= p_bs
->opaque
;
173 cur_pcid
= vmdk_read_cid(s
->fd
,0);
174 if (bs
->parent_cid
!= cur_pcid
)
183 int vmdk_snapshot_create(BlockDriverState
*bs
)
187 char *p_name
, *gd_buf
, *rgd_buf
;
189 uint32_t gde_entries
, gd_size
;
190 int64_t gd_offset
, rgd_offset
, capacity
, gt_size
;
191 char p_desc
[DESC_SIZE
], s_desc
[DESC_SIZE
], hdr
[HEADER_SIZE
];
192 char parent_filename
[1024];
193 char snapshot_filename
[1024];
195 char *desc_template
=
196 "# Disk DescriptorFile\n"
200 "createType=\"monolithicSparse\"\n"
201 "parentFileNameHint=\"%s\"\n"
203 "# Extent description\n"
204 "RW %lu SPARSE \"%s\"\n"
206 "# The Disk Data Base \n"
210 strcpy(parent_filename
, bs
->filename
);
211 uuid_generate(name
); // it should be unique filename
212 uuid_unparse(name
, snapshot_filename
);
213 strcat(snapshot_filename
,".vmdk");
215 snp_fd
= open(snapshot_filename
, O_RDWR
| O_CREAT
| O_TRUNC
| O_BINARY
| O_LARGEFILE
, 0644);
218 p_fd
= open(parent_filename
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
224 if (lseek(p_fd
, 0x0, SEEK_SET
) == -1)
226 if (read(p_fd
, hdr
, HEADER_SIZE
) != HEADER_SIZE
)
229 /* write the header */
230 if (lseek(snp_fd
, 0x0, SEEK_SET
) == -1)
232 if (write(snp_fd
, hdr
, HEADER_SIZE
) == -1)
235 memset(&header
, 0, sizeof(header
));
236 memcpy(&header
,&hdr
[4], sizeof(header
)); // skip the VMDK4_MAGIC
238 ftruncate(snp_fd
, header
.grain_offset
<< 9);
239 /* the descriptor offset = 0x200 */
240 if (lseek(p_fd
, 0x200, SEEK_SET
) == -1)
242 if (read(p_fd
, p_desc
, DESC_SIZE
) != DESC_SIZE
)
245 if ((p_name
= strstr(p_desc
,"CID")) != 0) {
246 p_name
+= sizeof("CID");
247 sscanf(p_name
,"%x",&p_cid
);
249 sprintf(s_desc
, desc_template
, p_cid
, p_cid
, parent_filename
250 , (uint32_t)header
.capacity
, snapshot_filename
);
252 bs
->parent_cid
= p_cid
;
254 /* write the descriptor */
255 if (lseek(snp_fd
, 0x200, SEEK_SET
) == -1)
257 if (write(snp_fd
, s_desc
, strlen(s_desc
)) == -1)
260 gd_offset
= header
.gd_offset
* SECTOR_SIZE
; // offset of GD table
261 rgd_offset
= header
.rgd_offset
* SECTOR_SIZE
; // offset of RGD table
262 capacity
= header
.capacity
* SECTOR_SIZE
; // Extent size
264 * Each GDE span 32M disk, means:
265 * 512 GTE per GT, each GTE points to grain
267 gt_size
= (int64_t)header
.num_gtes_per_gte
* header
.granularity
* SECTOR_SIZE
;
270 gde_entries
= (uint32_t)(capacity
/ gt_size
); // number of gde/rgde
271 gd_size
= gde_entries
* sizeof(uint32_t);
274 rgd_buf
= qemu_malloc(gd_size
);
277 if (lseek(p_fd
, rgd_offset
, SEEK_SET
) == -1)
279 if (read(p_fd
, rgd_buf
, gd_size
) != gd_size
)
281 if (lseek(snp_fd
, rgd_offset
, SEEK_SET
) == -1)
283 if (write(snp_fd
, rgd_buf
, gd_size
) == -1)
288 gd_buf
= qemu_malloc(gd_size
);
291 if (lseek(p_fd
, gd_offset
, SEEK_SET
) == -1)
293 if (read(p_fd
, gd_buf
, gd_size
) != gd_size
)
295 if (lseek(snp_fd
, gd_offset
, SEEK_SET
) == -1)
297 if (write(snp_fd
, gd_buf
, gd_size
) == -1)
315 static void vmdk_parent_close(BlockDriverState
*bs
)
317 if (bs
->bs_par_table
)
318 bdrv_close(bs
->bs_par_table
);
322 static int vmdk_parent_open(BlockDriverState
*bs
, int fd
, char * dir_name
)
325 char desc
[DESC_SIZE
];
327 char parent_img_name
[1024];
329 /* the descriptor offset = 0x200 */
330 if (lseek(fd
, 0x200, SEEK_SET
) == -1)
332 if (read(fd
, desc
, DESC_SIZE
) != DESC_SIZE
)
335 if ((p_name
= strstr(desc
,"parentFileNameHint")) != 0) {
336 char *end_name
, *tmp_name
;
337 char name
[256], buf
[128];
339 struct stat file_buf
;
341 p_name
+= sizeof("parentFileNameHint") + 1;
342 if ((end_name
= strchr(p_name
,'\"')) == 0)
345 bs
->parent_img_name
= qemu_mallocz(end_name
- p_name
+ 2);
346 strncpy(bs
->parent_img_name
, p_name
, end_name
- p_name
);
347 if (stat(bs
->parent_img_name
, &file_buf
) != 0) {
348 strcpy(parent_img_name
, dir_name
);
349 strcat(parent_img_name
, basename(bs
->parent_img_name
));
351 strcpy(parent_img_name
, bs
->parent_img_name
);
354 tmp_name
= strstr(bs
->device_name
,"_QEMU");
355 name_size
= tmp_name
? (tmp_name
- bs
->device_name
) : sizeof(bs
->device_name
);
356 strncpy(name
,bs
->device_name
,name_size
);
357 sprintf(buf
, "_QEMU_%d", ++idx
);
359 bs
->bs_par_table
= bdrv_new(strcat(name
, buf
));
360 if (!bs
->bs_par_table
) {
366 if (bdrv_open(bs
->bs_par_table
, parent_img_name
, 0) < 0)
373 static int vmdk_open(BlockDriverState
*bs
, const char *filename
)
375 BDRVVmdkState
*s
= bs
->opaque
;
380 fd
= open(filename
, O_RDWR
| O_BINARY
| O_LARGEFILE
);
382 fd
= open(filename
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
387 if (read(fd
, &magic
, sizeof(magic
)) != sizeof(magic
))
389 magic
= be32_to_cpu(magic
);
390 if (magic
== VMDK3_MAGIC
) {
392 if (read(fd
, &header
, sizeof(header
)) !=
395 s
->cluster_sectors
= le32_to_cpu(header
.granularity
);
398 bs
->total_sectors
= le32_to_cpu(header
.disk_sectors
);
399 s
->l1_table_offset
= le32_to_cpu(header
.l1dir_offset
) << 9;
400 s
->l1_backup_table_offset
= 0;
401 s
->l1_entry_sectors
= s
->l2_size
* s
->cluster_sectors
;
402 } else if (magic
== VMDK4_MAGIC
) {
406 if (read(fd
, &header
, sizeof(header
)) != sizeof(header
))
408 bs
->total_sectors
= le64_to_cpu(header
.capacity
);
409 s
->cluster_sectors
= le64_to_cpu(header
.granularity
);
410 s
->l2_size
= le32_to_cpu(header
.num_gtes_per_gte
);
411 s
->l1_entry_sectors
= s
->l2_size
* s
->cluster_sectors
;
412 if (s
->l1_entry_sectors
<= 0)
414 s
->l1_size
= (bs
->total_sectors
+ s
->l1_entry_sectors
- 1)
415 / s
->l1_entry_sectors
;
416 s
->l1_table_offset
= le64_to_cpu(header
.rgd_offset
) << 9;
417 s
->l1_backup_table_offset
= le64_to_cpu(header
.gd_offset
) << 9;
419 // try to open parent images, if exist
420 strcpy(dir_name
, dirname(filename
));
421 strcat(dir_name
,"/");
422 if (vmdk_parent_open(bs
, fd
, dir_name
) != 0)
424 // write the CID once after the image creation
425 bs
->cid
= vmdk_read_cid(fd
,0);
426 bs
->parent_cid
= vmdk_read_cid(fd
,1);
430 /* read the L1 table */
431 l1_size
= s
->l1_size
* sizeof(uint32_t);
432 s
->l1_table
= qemu_malloc(l1_size
);
435 if (lseek(fd
, s
->l1_table_offset
, SEEK_SET
) == -1)
437 if (read(fd
, s
->l1_table
, l1_size
) != l1_size
)
439 for(i
= 0; i
< s
->l1_size
; i
++) {
440 le32_to_cpus(&s
->l1_table
[i
]);
443 if (s
->l1_backup_table_offset
) {
444 s
->l1_backup_table
= qemu_malloc(l1_size
);
445 if (!s
->l1_backup_table
)
447 if (lseek(fd
, s
->l1_backup_table_offset
, SEEK_SET
) == -1)
449 if (read(fd
, s
->l1_backup_table
, l1_size
) != l1_size
)
451 for(i
= 0; i
< s
->l1_size
; i
++) {
452 le32_to_cpus(&s
->l1_backup_table
[i
]);
456 s
->l2_cache
= qemu_malloc(s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint32_t));
462 qemu_free(s
->l1_backup_table
);
463 qemu_free(s
->l1_table
);
464 qemu_free(s
->l2_cache
);
469 static uint64_t get_cluster_offset(BlockDriverState
*bs
, uint64_t offset
, int allocate
);
471 static int get_whole_cluster(BlockDriverState
*bs
, uint64_t cluster_offset
,
472 uint64_t offset
, int allocate
)
475 uint64_t parent_cluster_offset
;
476 BDRVVmdkState
*s
= bs
->opaque
;
477 uint8_t whole_grain
[s
->cluster_sectors
*512]; // 128 sectors * 512 bytes each = grain size 64KB
479 // we will be here if it's first write on non-exist grain(cluster).
480 // try to read from parent image, if exist
481 if (bs
->bs_par_table
) {
482 BDRVVmdkState
*ps
= bs
->bs_par_table
->opaque
;
484 if (!vmdk_is_cid_valid(bs
))
487 parent_cluster_offset
= get_cluster_offset(bs
->bs_par_table
, offset
, allocate
);
489 lseek(ps
->fd
, parent_cluster_offset
, SEEK_SET
);
490 ret
= read(ps
->fd
, whole_grain
, ps
->cluster_sectors
*512);
491 if (ret
!= ps
->cluster_sectors
*512)
494 lseek(s
->fd
, cluster_offset
<< 9, SEEK_SET
);
495 ret
= write(s
->fd
, whole_grain
, sizeof(whole_grain
));
496 if (ret
!= sizeof(whole_grain
))
502 static uint64_t get_cluster_offset(BlockDriverState
*bs
,
503 uint64_t offset
, int allocate
)
505 BDRVVmdkState
*s
= bs
->opaque
;
506 unsigned int l1_index
, l2_offset
, l2_index
;
508 uint32_t min_count
, *l2_table
, tmp
;
509 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 lseek(s
->fd
, (int64_t)l2_offset
* 512, SEEK_SET
);
540 if (read(s
->fd
, l2_table
, s
->l2_size
* sizeof(uint32_t)) !=
541 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
]);
548 if (!cluster_offset
) {
551 cluster_offset
= lseek(s
->fd
, 0, SEEK_END
);
552 ftruncate(s
->fd
, cluster_offset
+ (s
->cluster_sectors
<< 9));
553 cluster_offset
>>= 9;
554 /* update L2 table */
555 tmp
= cpu_to_le32(cluster_offset
);
556 l2_table
[l2_index
] = tmp
;
557 lseek(s
->fd
, ((int64_t)l2_offset
* 512) + (l2_index
* sizeof(tmp
)), SEEK_SET
);
558 if (write(s
->fd
, &tmp
, sizeof(tmp
)) != sizeof(tmp
))
560 /* update backup L2 table */
561 if (s
->l1_backup_table_offset
!= 0) {
562 l2_offset
= s
->l1_backup_table
[l1_index
];
563 lseek(s
->fd
, ((int64_t)l2_offset
* 512) + (l2_index
* sizeof(tmp
)), SEEK_SET
);
564 if (write(s
->fd
, &tmp
, sizeof(tmp
)) != sizeof(tmp
))
568 if (get_whole_cluster(bs
, cluster_offset
, offset
, allocate
) == -1)
571 cluster_offset
<<= 9;
572 return cluster_offset
;
575 static int vmdk_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
576 int nb_sectors
, int *pnum
)
578 BDRVVmdkState
*s
= bs
->opaque
;
579 int index_in_cluster
, n
;
580 uint64_t cluster_offset
;
582 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 0);
583 index_in_cluster
= sector_num
% s
->cluster_sectors
;
584 n
= s
->cluster_sectors
- index_in_cluster
;
588 return (cluster_offset
!= 0);
591 static int vmdk_read(BlockDriverState
*bs
, int64_t sector_num
,
592 uint8_t *buf
, int nb_sectors
)
594 BDRVVmdkState
*s
= bs
->opaque
;
595 int ret
, index_in_cluster
, n
;
596 uint64_t cluster_offset
;
598 while (nb_sectors
> 0) {
599 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 0);
600 index_in_cluster
= sector_num
% s
->cluster_sectors
;
601 n
= s
->cluster_sectors
- index_in_cluster
;
604 if (!cluster_offset
) {
605 // try to read from parent image, if exist
606 if (bs
->bs_par_table
) {
607 if (!vmdk_is_cid_valid(bs
))
609 if (vmdk_read(bs
->bs_par_table
, sector_num
, buf
, nb_sectors
) == -1)
612 memset(buf
, 0, 512 * n
);
615 lseek(s
->fd
, cluster_offset
+ index_in_cluster
* 512, SEEK_SET
);
616 ret
= read(s
->fd
, buf
, n
* 512);
627 static int vmdk_write(BlockDriverState
*bs
, int64_t sector_num
,
628 const uint8_t *buf
, int nb_sectors
)
630 BDRVVmdkState
*s
= bs
->opaque
;
631 int ret
, index_in_cluster
, n
;
632 uint64_t cluster_offset
;
633 static int cid_update
= 0;
635 while (nb_sectors
> 0) {
636 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
637 n
= s
->cluster_sectors
- index_in_cluster
;
640 cluster_offset
= get_cluster_offset(bs
, sector_num
<< 9, 1);
643 lseek(s
->fd
, cluster_offset
+ index_in_cluster
* 512, SEEK_SET
);
644 ret
= write(s
->fd
, buf
, n
* 512);
651 // update CID on the first write every time the virtual disk is opened
653 vmdk_write_cid(bs
, time(NULL
));
660 static int vmdk_create(const char *filename
, int64_t total_size
,
661 const char *backing_file
, int flags
)
665 uint32_t tmp
, magic
, grains
, gd_size
, gt_size
, gt_count
;
666 char *desc_template
=
667 "# Disk DescriptorFile\n"
670 "parentCID=ffffffff\n"
671 "createType=\"monolithicSparse\"\n"
673 "# Extent description\n"
674 "RW %lu SPARSE \"%s\"\n"
676 "# The Disk Data Base \n"
679 "ddb.virtualHWVersion = \"4\"\n"
680 "ddb.geometry.cylinders = \"%lu\"\n"
681 "ddb.geometry.heads = \"16\"\n"
682 "ddb.geometry.sectors = \"63\"\n"
683 "ddb.adapterType = \"ide\"\n";
685 const char *real_filename
, *temp_str
;
687 /* XXX: add support for backing file */
689 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
| O_LARGEFILE
,
693 magic
= cpu_to_be32(VMDK4_MAGIC
);
694 memset(&header
, 0, sizeof(header
));
695 header
.version
= cpu_to_le32(1);
696 header
.flags
= cpu_to_le32(3); /* ?? */
697 header
.capacity
= cpu_to_le64(total_size
);
698 header
.granularity
= cpu_to_le64(128);
699 header
.num_gtes_per_gte
= cpu_to_le32(512);
701 grains
= (total_size
+ header
.granularity
- 1) / header
.granularity
;
702 gt_size
= ((header
.num_gtes_per_gte
* sizeof(uint32_t)) + 511) >> 9;
703 gt_count
= (grains
+ header
.num_gtes_per_gte
- 1) / header
.num_gtes_per_gte
;
704 gd_size
= (gt_count
* sizeof(uint32_t) + 511) >> 9;
706 header
.desc_offset
= 1;
707 header
.desc_size
= 20;
708 header
.rgd_offset
= header
.desc_offset
+ header
.desc_size
;
709 header
.gd_offset
= header
.rgd_offset
+ gd_size
+ (gt_size
* gt_count
);
710 header
.grain_offset
=
711 ((header
.gd_offset
+ gd_size
+ (gt_size
* gt_count
) +
712 header
.granularity
- 1) / header
.granularity
) *
715 header
.desc_offset
= cpu_to_le64(header
.desc_offset
);
716 header
.desc_size
= cpu_to_le64(header
.desc_size
);
717 header
.rgd_offset
= cpu_to_le64(header
.rgd_offset
);
718 header
.gd_offset
= cpu_to_le64(header
.gd_offset
);
719 header
.grain_offset
= cpu_to_le64(header
.grain_offset
);
721 header
.check_bytes
[0] = 0xa;
722 header
.check_bytes
[1] = 0x20;
723 header
.check_bytes
[2] = 0xd;
724 header
.check_bytes
[3] = 0xa;
726 /* write all the data */
727 write(fd
, &magic
, sizeof(magic
));
728 write(fd
, &header
, sizeof(header
));
730 ftruncate(fd
, header
.grain_offset
<< 9);
732 /* write grain directory */
733 lseek(fd
, le64_to_cpu(header
.rgd_offset
) << 9, SEEK_SET
);
734 for (i
= 0, tmp
= header
.rgd_offset
+ gd_size
;
735 i
< gt_count
; i
++, tmp
+= gt_size
)
736 write(fd
, &tmp
, sizeof(tmp
));
738 /* write backup grain directory */
739 lseek(fd
, le64_to_cpu(header
.gd_offset
) << 9, SEEK_SET
);
740 for (i
= 0, tmp
= header
.gd_offset
+ gd_size
;
741 i
< gt_count
; i
++, tmp
+= gt_size
)
742 write(fd
, &tmp
, sizeof(tmp
));
744 /* compose the descriptor */
745 real_filename
= filename
;
746 if ((temp_str
= strrchr(real_filename
, '\\')) != NULL
)
747 real_filename
= temp_str
+ 1;
748 if ((temp_str
= strrchr(real_filename
, '/')) != NULL
)
749 real_filename
= temp_str
+ 1;
750 if ((temp_str
= strrchr(real_filename
, ':')) != NULL
)
751 real_filename
= temp_str
+ 1;
752 sprintf(desc
, desc_template
, time(NULL
), (unsigned long)total_size
,
753 real_filename
, total_size
/ (63 * 16));
755 /* write the descriptor */
756 lseek(fd
, le64_to_cpu(header
.desc_offset
) << 9, SEEK_SET
);
757 write(fd
, desc
, strlen(desc
));
763 static void vmdk_close(BlockDriverState
*bs
)
765 BDRVVmdkState
*s
= bs
->opaque
;
767 qemu_free(s
->l1_table
);
768 qemu_free(s
->l2_cache
);
770 // try to close parent image, if exist
771 vmdk_parent_close(bs
);
774 static void vmdk_flush(BlockDriverState
*bs
)
776 BDRVVmdkState
*s
= bs
->opaque
;
780 BlockDriver bdrv_vmdk
= {
782 sizeof(BDRVVmdkState
),