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 VmdkExtent
{
64 BlockDriverState
*file
;
68 int64_t flat_start_offset
;
69 int64_t l1_table_offset
;
70 int64_t l1_backup_table_offset
;
72 uint32_t *l1_backup_table
;
74 uint32_t l1_entry_sectors
;
78 uint32_t l2_cache_offsets
[L2_CACHE_SIZE
];
79 uint32_t l2_cache_counts
[L2_CACHE_SIZE
];
81 unsigned int cluster_sectors
;
84 typedef struct BDRVVmdkState
{
89 /* Extent array with num_extents entries, ascend ordered by address */
93 typedef struct VmdkMetaData
{
95 unsigned int l1_index
;
96 unsigned int l2_index
;
97 unsigned int l2_offset
;
101 static int vmdk_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
108 magic
= be32_to_cpu(*(uint32_t *)buf
);
109 if (magic
== VMDK3_MAGIC
||
110 magic
== VMDK4_MAGIC
) {
113 const char *p
= (const char *)buf
;
114 const char *end
= p
+ buf_size
;
117 /* skip comment line */
118 while (p
< end
&& *p
!= '\n') {
125 while (p
< end
&& *p
== ' ') {
128 /* skip '\r' if windows line endings used. */
129 if (p
< end
&& *p
== '\r') {
132 /* only accept blank lines before 'version=' line */
133 if (p
== end
|| *p
!= '\n') {
139 if (end
- p
>= strlen("version=X\n")) {
140 if (strncmp("version=1\n", p
, strlen("version=1\n")) == 0 ||
141 strncmp("version=2\n", p
, strlen("version=2\n")) == 0) {
145 if (end
- p
>= strlen("version=X\r\n")) {
146 if (strncmp("version=1\r\n", p
, strlen("version=1\r\n")) == 0 ||
147 strncmp("version=2\r\n", p
, strlen("version=2\r\n")) == 0) {
159 #define SECTOR_SIZE 512
160 #define DESC_SIZE (20 * SECTOR_SIZE) /* 20 sectors of 512 bytes each */
161 #define BUF_SIZE 4096
162 #define HEADER_SIZE 512 /* first sector of 512 bytes */
164 static void vmdk_free_extents(BlockDriverState
*bs
)
167 BDRVVmdkState
*s
= bs
->opaque
;
169 for (i
= 0; i
< s
->num_extents
; i
++) {
170 g_free(s
->extents
[i
].l1_table
);
171 g_free(s
->extents
[i
].l2_cache
);
172 g_free(s
->extents
[i
].l1_backup_table
);
177 static uint32_t vmdk_read_cid(BlockDriverState
*bs
, int parent
)
179 char desc
[DESC_SIZE
];
181 const char *p_name
, *cid_str
;
183 BDRVVmdkState
*s
= bs
->opaque
;
185 if (bdrv_pread(bs
->file
, s
->desc_offset
, desc
, DESC_SIZE
) != DESC_SIZE
) {
190 cid_str
= "parentCID";
191 cid_str_size
= sizeof("parentCID");
194 cid_str_size
= sizeof("CID");
197 p_name
= strstr(desc
, cid_str
);
198 if (p_name
!= NULL
) {
199 p_name
+= cid_str_size
;
200 sscanf(p_name
, "%x", &cid
);
206 static int vmdk_write_cid(BlockDriverState
*bs
, uint32_t cid
)
208 char desc
[DESC_SIZE
], tmp_desc
[DESC_SIZE
];
209 char *p_name
, *tmp_str
;
210 BDRVVmdkState
*s
= bs
->opaque
;
212 memset(desc
, 0, sizeof(desc
));
213 if (bdrv_pread(bs
->file
, s
->desc_offset
, desc
, DESC_SIZE
) != DESC_SIZE
) {
217 tmp_str
= strstr(desc
, "parentCID");
218 pstrcpy(tmp_desc
, sizeof(tmp_desc
), tmp_str
);
219 p_name
= strstr(desc
, "CID");
220 if (p_name
!= NULL
) {
221 p_name
+= sizeof("CID");
222 snprintf(p_name
, sizeof(desc
) - (p_name
- desc
), "%x\n", cid
);
223 pstrcat(desc
, sizeof(desc
), tmp_desc
);
226 if (bdrv_pwrite_sync(bs
->file
, s
->desc_offset
, desc
, DESC_SIZE
) < 0) {
232 static int vmdk_is_cid_valid(BlockDriverState
*bs
)
235 BDRVVmdkState
*s
= bs
->opaque
;
236 BlockDriverState
*p_bs
= bs
->backing_hd
;
240 cur_pcid
= vmdk_read_cid(p_bs
, 0);
241 if (s
->parent_cid
!= cur_pcid
) {
251 static int vmdk_parent_open(BlockDriverState
*bs
)
254 char desc
[DESC_SIZE
+ 1];
255 BDRVVmdkState
*s
= bs
->opaque
;
257 desc
[DESC_SIZE
] = '\0';
258 if (bdrv_pread(bs
->file
, s
->desc_offset
, desc
, DESC_SIZE
) != DESC_SIZE
) {
262 p_name
= strstr(desc
, "parentFileNameHint");
263 if (p_name
!= NULL
) {
266 p_name
+= sizeof("parentFileNameHint") + 1;
267 end_name
= strchr(p_name
, '\"');
268 if (end_name
== NULL
) {
271 if ((end_name
- p_name
) > sizeof(bs
->backing_file
) - 1) {
275 pstrcpy(bs
->backing_file
, end_name
- p_name
+ 1, p_name
);
281 /* Create and append extent to the extent array. Return the added VmdkExtent
282 * address. return NULL if allocation failed. */
283 static VmdkExtent
*vmdk_add_extent(BlockDriverState
*bs
,
284 BlockDriverState
*file
, bool flat
, int64_t sectors
,
285 int64_t l1_offset
, int64_t l1_backup_offset
,
287 int l2_size
, unsigned int cluster_sectors
)
290 BDRVVmdkState
*s
= bs
->opaque
;
292 s
->extents
= g_realloc(s
->extents
,
293 (s
->num_extents
+ 1) * sizeof(VmdkExtent
));
294 extent
= &s
->extents
[s
->num_extents
];
297 memset(extent
, 0, sizeof(VmdkExtent
));
300 extent
->sectors
= sectors
;
301 extent
->l1_table_offset
= l1_offset
;
302 extent
->l1_backup_table_offset
= l1_backup_offset
;
303 extent
->l1_size
= l1_size
;
304 extent
->l1_entry_sectors
= l2_size
* cluster_sectors
;
305 extent
->l2_size
= l2_size
;
306 extent
->cluster_sectors
= cluster_sectors
;
308 if (s
->num_extents
> 1) {
309 extent
->end_sector
= (*(extent
- 1)).end_sector
+ extent
->sectors
;
311 extent
->end_sector
= extent
->sectors
;
313 bs
->total_sectors
= extent
->end_sector
;
317 static int vmdk_init_tables(BlockDriverState
*bs
, VmdkExtent
*extent
)
322 /* read the L1 table */
323 l1_size
= extent
->l1_size
* sizeof(uint32_t);
324 extent
->l1_table
= g_malloc(l1_size
);
325 ret
= bdrv_pread(extent
->file
,
326 extent
->l1_table_offset
,
332 for (i
= 0; i
< extent
->l1_size
; i
++) {
333 le32_to_cpus(&extent
->l1_table
[i
]);
336 if (extent
->l1_backup_table_offset
) {
337 extent
->l1_backup_table
= g_malloc(l1_size
);
338 ret
= bdrv_pread(extent
->file
,
339 extent
->l1_backup_table_offset
,
340 extent
->l1_backup_table
,
345 for (i
= 0; i
< extent
->l1_size
; i
++) {
346 le32_to_cpus(&extent
->l1_backup_table
[i
]);
351 g_malloc(extent
->l2_size
* L2_CACHE_SIZE
* sizeof(uint32_t));
354 g_free(extent
->l1_backup_table
);
356 g_free(extent
->l1_table
);
360 static int vmdk_open_vmdk3(BlockDriverState
*bs
, int flags
)
365 BDRVVmdkState
*s
= bs
->opaque
;
368 s
->desc_offset
= 0x200;
369 ret
= bdrv_pread(bs
->file
, sizeof(magic
), &header
, sizeof(header
));
373 extent
= vmdk_add_extent(bs
,
375 le32_to_cpu(header
.disk_sectors
),
376 le32_to_cpu(header
.l1dir_offset
) << 9,
378 le32_to_cpu(header
.granularity
));
379 ret
= vmdk_init_tables(bs
, extent
);
381 /* vmdk_init_tables cleans up on fail, so only free allocation of
382 * vmdk_add_extent here. */
387 vmdk_free_extents(bs
);
391 static int vmdk_open_vmdk4(BlockDriverState
*bs
, int flags
)
395 uint32_t l1_size
, l1_entry_sectors
;
397 BDRVVmdkState
*s
= bs
->opaque
;
400 s
->desc_offset
= 0x200;
401 ret
= bdrv_pread(bs
->file
, sizeof(magic
), &header
, sizeof(header
));
405 l1_entry_sectors
= le32_to_cpu(header
.num_gtes_per_gte
)
406 * le64_to_cpu(header
.granularity
);
407 l1_size
= (le64_to_cpu(header
.capacity
) + l1_entry_sectors
- 1)
409 extent
= vmdk_add_extent(bs
, bs
->file
, false,
410 le64_to_cpu(header
.capacity
),
411 le64_to_cpu(header
.gd_offset
) << 9,
412 le64_to_cpu(header
.rgd_offset
) << 9,
414 le32_to_cpu(header
.num_gtes_per_gte
),
415 le64_to_cpu(header
.granularity
));
416 if (extent
->l1_entry_sectors
<= 0) {
420 /* try to open parent images, if exist */
421 ret
= vmdk_parent_open(bs
);
425 s
->parent_cid
= vmdk_read_cid(bs
, 1);
426 ret
= vmdk_init_tables(bs
, extent
);
432 vmdk_free_extents(bs
);
436 /* find an option value out of descriptor file */
437 static int vmdk_parse_description(const char *desc
, const char *opt_name
,
438 char *buf
, int buf_size
)
440 char *opt_pos
, *opt_end
;
441 const char *end
= desc
+ strlen(desc
);
443 opt_pos
= strstr(desc
, opt_name
);
447 /* Skip "=\"" following opt_name */
448 opt_pos
+= strlen(opt_name
) + 2;
449 if (opt_pos
>= end
) {
453 while (opt_end
< end
&& *opt_end
!= '"') {
456 if (opt_end
== end
|| buf_size
< opt_end
- opt_pos
+ 1) {
459 pstrcpy(buf
, opt_end
- opt_pos
+ 1, opt_pos
);
463 static int vmdk_parse_extents(const char *desc
, BlockDriverState
*bs
,
464 const char *desc_file_path
)
470 const char *p
= desc
;
475 /* parse extent line:
476 * RW [size in sectors] FLAT "file-name.vmdk" OFFSET
478 * RW [size in sectors] SPARSE "file-name.vmdk"
481 ret
= sscanf(p
, "%10s %" SCNd64
" %10s %511s %" SCNd64
,
482 access
, §ors
, type
, fname
, &flat_offset
);
483 if (ret
< 4 || strcmp(access
, "RW")) {
485 } else if (!strcmp(type
, "FLAT")) {
486 if (ret
!= 5 || flat_offset
< 0) {
489 } else if (ret
!= 4) {
493 /* trim the quotation marks around */
494 if (fname
[0] == '"') {
495 memmove(fname
, fname
+ 1, strlen(fname
));
496 if (strlen(fname
) <= 1 || fname
[strlen(fname
) - 1] != '"') {
499 fname
[strlen(fname
) - 1] = '\0';
502 (strcmp(type
, "FLAT") && strcmp(type
, "SPARSE")) ||
503 (strcmp(access
, "RW"))) {
507 /* save to extents array */
508 if (!strcmp(type
, "FLAT")) {
510 char extent_path
[PATH_MAX
];
511 BlockDriverState
*extent_file
;
514 path_combine(extent_path
, sizeof(extent_path
),
515 desc_file_path
, fname
);
516 ret
= bdrv_file_open(&extent_file
, extent_path
, bs
->open_flags
);
520 extent
= vmdk_add_extent(bs
, extent_file
, true, sectors
,
521 0, 0, 0, 0, sectors
);
522 extent
->flat_start_offset
= flat_offset
;
524 /* SPARSE extent, not supported for now */
526 "VMDK: Not supported extent type \"%s\""".\n", type
);
530 /* move to next line */
531 while (*p
&& *p
!= '\n') {
539 static int vmdk_open_desc_file(BlockDriverState
*bs
, int flags
)
544 BDRVVmdkState
*s
= bs
->opaque
;
546 ret
= bdrv_pread(bs
->file
, 0, buf
, sizeof(buf
));
551 if (vmdk_parse_description(buf
, "createType", ct
, sizeof(ct
))) {
554 if (strcmp(ct
, "monolithicFlat")) {
556 "VMDK: Not supported image type \"%s\""".\n", ct
);
560 ret
= vmdk_parse_extents(buf
, bs
, bs
->file
->filename
);
565 /* try to open parent images, if exist */
566 if (vmdk_parent_open(bs
)) {
570 s
->parent_cid
= vmdk_read_cid(bs
, 1);
574 static int vmdk_open(BlockDriverState
*bs
, int flags
)
578 if (bdrv_pread(bs
->file
, 0, &magic
, sizeof(magic
)) != sizeof(magic
)) {
582 magic
= be32_to_cpu(magic
);
583 if (magic
== VMDK3_MAGIC
) {
584 return vmdk_open_vmdk3(bs
, flags
);
585 } else if (magic
== VMDK4_MAGIC
) {
586 return vmdk_open_vmdk4(bs
, flags
);
588 return vmdk_open_desc_file(bs
, flags
);
592 static int get_whole_cluster(BlockDriverState
*bs
,
594 uint64_t cluster_offset
,
598 /* 128 sectors * 512 bytes each = grain size 64KB */
599 uint8_t whole_grain
[extent
->cluster_sectors
* 512];
601 /* we will be here if it's first write on non-exist grain(cluster).
602 * try to read from parent image, if exist */
603 if (bs
->backing_hd
) {
606 if (!vmdk_is_cid_valid(bs
)) {
610 /* floor offset to cluster */
611 offset
-= offset
% (extent
->cluster_sectors
* 512);
612 ret
= bdrv_read(bs
->backing_hd
, offset
>> 9, whole_grain
,
613 extent
->cluster_sectors
);
618 /* Write grain only into the active image */
619 ret
= bdrv_write(extent
->file
, cluster_offset
, whole_grain
,
620 extent
->cluster_sectors
);
628 static int vmdk_L2update(VmdkExtent
*extent
, VmdkMetaData
*m_data
)
630 /* update L2 table */
631 if (bdrv_pwrite_sync(
633 ((int64_t)m_data
->l2_offset
* 512)
634 + (m_data
->l2_index
* sizeof(m_data
->offset
)),
636 sizeof(m_data
->offset
)
640 /* update backup L2 table */
641 if (extent
->l1_backup_table_offset
!= 0) {
642 m_data
->l2_offset
= extent
->l1_backup_table
[m_data
->l1_index
];
643 if (bdrv_pwrite_sync(
645 ((int64_t)m_data
->l2_offset
* 512)
646 + (m_data
->l2_index
* sizeof(m_data
->offset
)),
647 &(m_data
->offset
), sizeof(m_data
->offset
)
656 static int get_cluster_offset(BlockDriverState
*bs
,
658 VmdkMetaData
*m_data
,
661 uint64_t *cluster_offset
)
663 unsigned int l1_index
, l2_offset
, l2_index
;
665 uint32_t min_count
, *l2_table
, tmp
= 0;
671 *cluster_offset
= extent
->flat_start_offset
;
675 l1_index
= (offset
>> 9) / extent
->l1_entry_sectors
;
676 if (l1_index
>= extent
->l1_size
) {
679 l2_offset
= extent
->l1_table
[l1_index
];
683 for (i
= 0; i
< L2_CACHE_SIZE
; i
++) {
684 if (l2_offset
== extent
->l2_cache_offsets
[i
]) {
685 /* increment the hit count */
686 if (++extent
->l2_cache_counts
[i
] == 0xffffffff) {
687 for (j
= 0; j
< L2_CACHE_SIZE
; j
++) {
688 extent
->l2_cache_counts
[j
] >>= 1;
691 l2_table
= extent
->l2_cache
+ (i
* extent
->l2_size
);
695 /* not found: load a new entry in the least used one */
697 min_count
= 0xffffffff;
698 for (i
= 0; i
< L2_CACHE_SIZE
; i
++) {
699 if (extent
->l2_cache_counts
[i
] < min_count
) {
700 min_count
= extent
->l2_cache_counts
[i
];
704 l2_table
= extent
->l2_cache
+ (min_index
* extent
->l2_size
);
707 (int64_t)l2_offset
* 512,
709 extent
->l2_size
* sizeof(uint32_t)
710 ) != extent
->l2_size
* sizeof(uint32_t)) {
714 extent
->l2_cache_offsets
[min_index
] = l2_offset
;
715 extent
->l2_cache_counts
[min_index
] = 1;
717 l2_index
= ((offset
>> 9) / extent
->cluster_sectors
) % extent
->l2_size
;
718 *cluster_offset
= le32_to_cpu(l2_table
[l2_index
]);
720 if (!*cluster_offset
) {
725 /* Avoid the L2 tables update for the images that have snapshots. */
726 *cluster_offset
= bdrv_getlength(extent
->file
);
729 *cluster_offset
+ (extent
->cluster_sectors
<< 9)
732 *cluster_offset
>>= 9;
733 tmp
= cpu_to_le32(*cluster_offset
);
734 l2_table
[l2_index
] = tmp
;
736 /* First of all we write grain itself, to avoid race condition
737 * that may to corrupt the image.
738 * This problem may occur because of insufficient space on host disk
739 * or inappropriate VM shutdown.
741 if (get_whole_cluster(
742 bs
, extent
, *cluster_offset
, offset
, allocate
) == -1) {
747 m_data
->offset
= tmp
;
748 m_data
->l1_index
= l1_index
;
749 m_data
->l2_index
= l2_index
;
750 m_data
->l2_offset
= l2_offset
;
754 *cluster_offset
<<= 9;
758 static VmdkExtent
*find_extent(BDRVVmdkState
*s
,
759 int64_t sector_num
, VmdkExtent
*start_hint
)
761 VmdkExtent
*extent
= start_hint
;
764 extent
= &s
->extents
[0];
766 while (extent
< &s
->extents
[s
->num_extents
]) {
767 if (sector_num
< extent
->end_sector
) {
775 static int vmdk_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
776 int nb_sectors
, int *pnum
)
778 BDRVVmdkState
*s
= bs
->opaque
;
779 int64_t index_in_cluster
, n
, ret
;
783 extent
= find_extent(s
, sector_num
, NULL
);
787 ret
= get_cluster_offset(bs
, extent
, NULL
,
788 sector_num
* 512, 0, &offset
);
789 /* get_cluster_offset returning 0 means success */
792 index_in_cluster
= sector_num
% extent
->cluster_sectors
;
793 n
= extent
->cluster_sectors
- index_in_cluster
;
794 if (n
> nb_sectors
) {
801 static int vmdk_read(BlockDriverState
*bs
, int64_t sector_num
,
802 uint8_t *buf
, int nb_sectors
)
804 BDRVVmdkState
*s
= bs
->opaque
;
806 uint64_t n
, index_in_cluster
;
807 VmdkExtent
*extent
= NULL
;
808 uint64_t cluster_offset
;
810 while (nb_sectors
> 0) {
811 extent
= find_extent(s
, sector_num
, extent
);
815 ret
= get_cluster_offset(
817 sector_num
<< 9, 0, &cluster_offset
);
818 index_in_cluster
= sector_num
% extent
->cluster_sectors
;
819 n
= extent
->cluster_sectors
- index_in_cluster
;
820 if (n
> nb_sectors
) {
824 /* if not allocated, try to read from parent image, if exist */
825 if (bs
->backing_hd
) {
826 if (!vmdk_is_cid_valid(bs
)) {
829 ret
= bdrv_read(bs
->backing_hd
, sector_num
, buf
, n
);
834 memset(buf
, 0, 512 * n
);
837 ret
= bdrv_pread(extent
->file
,
838 cluster_offset
+ index_in_cluster
* 512,
851 static int vmdk_write(BlockDriverState
*bs
, int64_t sector_num
,
852 const uint8_t *buf
, int nb_sectors
)
854 BDRVVmdkState
*s
= bs
->opaque
;
855 VmdkExtent
*extent
= NULL
;
857 int64_t index_in_cluster
;
858 uint64_t cluster_offset
;
861 if (sector_num
> bs
->total_sectors
) {
863 "(VMDK) Wrong offset: sector_num=0x%" PRIx64
864 " total_sectors=0x%" PRIx64
"\n",
865 sector_num
, bs
->total_sectors
);
869 while (nb_sectors
> 0) {
870 extent
= find_extent(s
, sector_num
, extent
);
874 ret
= get_cluster_offset(
883 index_in_cluster
= sector_num
% extent
->cluster_sectors
;
884 n
= extent
->cluster_sectors
- index_in_cluster
;
885 if (n
> nb_sectors
) {
889 ret
= bdrv_pwrite(extent
->file
,
890 cluster_offset
+ index_in_cluster
* 512,
897 /* update L2 tables */
898 if (vmdk_L2update(extent
, &m_data
) == -1) {
906 /* update CID on the first write every time the virtual disk is
908 if (!s
->cid_updated
) {
909 vmdk_write_cid(bs
, time(NULL
));
910 s
->cid_updated
= true;
917 static int vmdk_create_extent(const char *filename
, int64_t filesize
, bool flat
)
922 uint32_t tmp
, magic
, grains
, gd_size
, gt_size
, gt_count
;
926 O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
| O_LARGEFILE
,
932 ret
= ftruncate(fd
, filesize
);
938 magic
= cpu_to_be32(VMDK4_MAGIC
);
939 memset(&header
, 0, sizeof(header
));
941 header
.flags
= 3; /* ?? */
942 header
.capacity
= filesize
/ 512;
943 header
.granularity
= 128;
944 header
.num_gtes_per_gte
= 512;
946 grains
= (filesize
/ 512 + header
.granularity
- 1) / header
.granularity
;
947 gt_size
= ((header
.num_gtes_per_gte
* sizeof(uint32_t)) + 511) >> 9;
949 (grains
+ header
.num_gtes_per_gte
- 1) / header
.num_gtes_per_gte
;
950 gd_size
= (gt_count
* sizeof(uint32_t) + 511) >> 9;
952 header
.desc_offset
= 1;
953 header
.desc_size
= 20;
954 header
.rgd_offset
= header
.desc_offset
+ header
.desc_size
;
955 header
.gd_offset
= header
.rgd_offset
+ gd_size
+ (gt_size
* gt_count
);
956 header
.grain_offset
=
957 ((header
.gd_offset
+ gd_size
+ (gt_size
* gt_count
) +
958 header
.granularity
- 1) / header
.granularity
) *
960 /* swap endianness for all header fields */
961 header
.version
= cpu_to_le32(header
.version
);
962 header
.flags
= cpu_to_le32(header
.flags
);
963 header
.capacity
= cpu_to_le64(header
.capacity
);
964 header
.granularity
= cpu_to_le64(header
.granularity
);
965 header
.num_gtes_per_gte
= cpu_to_le32(header
.num_gtes_per_gte
);
966 header
.desc_offset
= cpu_to_le64(header
.desc_offset
);
967 header
.desc_size
= cpu_to_le64(header
.desc_size
);
968 header
.rgd_offset
= cpu_to_le64(header
.rgd_offset
);
969 header
.gd_offset
= cpu_to_le64(header
.gd_offset
);
970 header
.grain_offset
= cpu_to_le64(header
.grain_offset
);
972 header
.check_bytes
[0] = 0xa;
973 header
.check_bytes
[1] = 0x20;
974 header
.check_bytes
[2] = 0xd;
975 header
.check_bytes
[3] = 0xa;
977 /* write all the data */
978 ret
= qemu_write_full(fd
, &magic
, sizeof(magic
));
979 if (ret
!= sizeof(magic
)) {
983 ret
= qemu_write_full(fd
, &header
, sizeof(header
));
984 if (ret
!= sizeof(header
)) {
989 ret
= ftruncate(fd
, le64_to_cpu(header
.grain_offset
) << 9);
995 /* write grain directory */
996 lseek(fd
, le64_to_cpu(header
.rgd_offset
) << 9, SEEK_SET
);
997 for (i
= 0, tmp
= le64_to_cpu(header
.rgd_offset
) + gd_size
;
998 i
< gt_count
; i
++, tmp
+= gt_size
) {
999 ret
= qemu_write_full(fd
, &tmp
, sizeof(tmp
));
1000 if (ret
!= sizeof(tmp
)) {
1006 /* write backup grain directory */
1007 lseek(fd
, le64_to_cpu(header
.gd_offset
) << 9, SEEK_SET
);
1008 for (i
= 0, tmp
= le64_to_cpu(header
.gd_offset
) + gd_size
;
1009 i
< gt_count
; i
++, tmp
+= gt_size
) {
1010 ret
= qemu_write_full(fd
, &tmp
, sizeof(tmp
));
1011 if (ret
!= sizeof(tmp
)) {
1023 static int filename_decompose(const char *filename
, char *path
, char *prefix
,
1024 char *postfix
, size_t buf_len
)
1028 if (filename
== NULL
|| !strlen(filename
)) {
1029 fprintf(stderr
, "Vmdk: no filename provided.\n");
1032 p
= strrchr(filename
, '/');
1034 p
= strrchr(filename
, '\\');
1037 p
= strrchr(filename
, ':');
1041 if (p
- filename
>= buf_len
) {
1044 pstrcpy(path
, p
- filename
+ 1, filename
);
1049 q
= strrchr(p
, '.');
1051 pstrcpy(prefix
, buf_len
, p
);
1054 if (q
- p
>= buf_len
) {
1057 pstrcpy(prefix
, q
- p
+ 1, p
);
1058 pstrcpy(postfix
, buf_len
, q
);
1063 static int relative_path(char *dest
, int dest_size
,
1064 const char *base
, const char *target
)
1070 const char *sep
= "\\";
1072 const char *sep
= "/";
1075 if (!(dest
&& base
&& target
)) {
1078 if (path_is_absolute(target
)) {
1079 dest
[dest_size
- 1] = '\0';
1080 strncpy(dest
, target
, dest_size
- 1);
1083 while (base
[i
] == target
[i
]) {
1096 pstrcat(dest
, dest_size
, "..");
1097 pstrcat(dest
, dest_size
, sep
);
1099 pstrcat(dest
, dest_size
, q
);
1103 static int vmdk_create(const char *filename
, QEMUOptionParameter
*options
)
1106 char desc
[BUF_SIZE
];
1107 int64_t total_size
= 0, filesize
;
1108 const char *backing_file
= NULL
;
1109 const char *fmt
= NULL
;
1113 char ext_desc_lines
[BUF_SIZE
] = "";
1114 char path
[PATH_MAX
], prefix
[PATH_MAX
], postfix
[PATH_MAX
];
1115 const int64_t split_size
= 0x80000000; /* VMDK has constant split size */
1116 const char *desc_extent_line
;
1117 char parent_desc_line
[BUF_SIZE
] = "";
1118 uint32_t parent_cid
= 0xffffffff;
1119 const char desc_template
[] =
1120 "# Disk DescriptorFile\n"
1124 "createType=\"%s\"\n"
1127 "# Extent description\n"
1130 "# The Disk Data Base\n"
1133 "ddb.virtualHWVersion = \"%d\"\n"
1134 "ddb.geometry.cylinders = \"%" PRId64
"\"\n"
1135 "ddb.geometry.heads = \"16\"\n"
1136 "ddb.geometry.sectors = \"63\"\n"
1137 "ddb.adapterType = \"ide\"\n";
1139 if (filename_decompose(filename
, path
, prefix
, postfix
, PATH_MAX
)) {
1142 /* Read out options */
1143 while (options
&& options
->name
) {
1144 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
1145 total_size
= options
->value
.n
;
1146 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FILE
)) {
1147 backing_file
= options
->value
.s
;
1148 } else if (!strcmp(options
->name
, BLOCK_OPT_COMPAT6
)) {
1149 flags
|= options
->value
.n
? BLOCK_FLAG_COMPAT6
: 0;
1150 } else if (!strcmp(options
->name
, BLOCK_OPT_SUBFMT
)) {
1151 fmt
= options
->value
.s
;
1156 /* Default format to monolithicSparse */
1157 fmt
= "monolithicSparse";
1158 } else if (strcmp(fmt
, "monolithicFlat") &&
1159 strcmp(fmt
, "monolithicSparse") &&
1160 strcmp(fmt
, "twoGbMaxExtentSparse") &&
1161 strcmp(fmt
, "twoGbMaxExtentFlat")) {
1162 fprintf(stderr
, "VMDK: Unknown subformat: %s\n", fmt
);
1165 split
= !(strcmp(fmt
, "twoGbMaxExtentFlat") &&
1166 strcmp(fmt
, "twoGbMaxExtentSparse"));
1167 flat
= !(strcmp(fmt
, "monolithicFlat") &&
1168 strcmp(fmt
, "twoGbMaxExtentFlat"));
1170 desc_extent_line
= "RW %lld FLAT \"%s\" 0\n";
1172 desc_extent_line
= "RW %lld SPARSE \"%s\"\n";
1174 if (flat
&& backing_file
) {
1175 /* not supporting backing file for flat image */
1179 char parent_filename
[PATH_MAX
];
1180 BlockDriverState
*bs
= bdrv_new("");
1181 ret
= bdrv_open(bs
, backing_file
, 0, NULL
);
1186 if (strcmp(bs
->drv
->format_name
, "vmdk")) {
1190 filesize
= bdrv_getlength(bs
);
1191 parent_cid
= vmdk_read_cid(bs
, 0);
1193 relative_path(parent_filename
, sizeof(parent_filename
),
1194 filename
, backing_file
);
1195 snprintf(parent_desc_line
, sizeof(parent_desc_line
),
1196 "parentFileNameHint=\"%s\"", parent_filename
);
1199 /* Create extents */
1200 filesize
= total_size
;
1201 while (filesize
> 0) {
1202 char desc_line
[BUF_SIZE
];
1203 char ext_filename
[PATH_MAX
];
1204 char desc_filename
[PATH_MAX
];
1205 int64_t size
= filesize
;
1207 if (split
&& size
> split_size
) {
1211 snprintf(desc_filename
, sizeof(desc_filename
), "%s-%c%03d%s",
1212 prefix
, flat
? 'f' : 's', ++idx
, postfix
);
1214 snprintf(desc_filename
, sizeof(desc_filename
), "%s-flat%s",
1217 snprintf(desc_filename
, sizeof(desc_filename
), "%s%s",
1220 snprintf(ext_filename
, sizeof(ext_filename
), "%s%s",
1221 path
, desc_filename
);
1223 if (vmdk_create_extent(ext_filename
, size
, flat
)) {
1228 /* Format description line */
1229 snprintf(desc_line
, sizeof(desc_line
),
1230 desc_extent_line
, size
/ 512, desc_filename
);
1231 pstrcat(ext_desc_lines
, sizeof(ext_desc_lines
), desc_line
);
1233 /* generate descriptor file */
1234 snprintf(desc
, sizeof(desc
), desc_template
,
1235 (unsigned int)time(NULL
),
1240 (flags
& BLOCK_FLAG_COMPAT6
? 6 : 4),
1241 total_size
/ (int64_t)(63 * 16 * 512));
1242 if (split
|| flat
) {
1245 O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
| O_LARGEFILE
,
1250 O_WRONLY
| O_BINARY
| O_LARGEFILE
,
1256 /* the descriptor offset = 0x200 */
1257 if (!split
&& !flat
&& 0x200 != lseek(fd
, 0x200, SEEK_SET
)) {
1261 ret
= qemu_write_full(fd
, desc
, strlen(desc
));
1262 if (ret
!= strlen(desc
)) {
1272 static void vmdk_close(BlockDriverState
*bs
)
1274 vmdk_free_extents(bs
);
1277 static int vmdk_flush(BlockDriverState
*bs
)
1280 BDRVVmdkState
*s
= bs
->opaque
;
1282 ret
= bdrv_flush(bs
->file
);
1283 for (i
= 0; i
< s
->num_extents
; i
++) {
1284 err
= bdrv_flush(s
->extents
[i
].file
);
1292 static int64_t vmdk_get_allocated_file_size(BlockDriverState
*bs
)
1297 BDRVVmdkState
*s
= bs
->opaque
;
1299 ret
= bdrv_get_allocated_file_size(bs
->file
);
1303 for (i
= 0; i
< s
->num_extents
; i
++) {
1304 if (s
->extents
[i
].file
== bs
->file
) {
1307 r
= bdrv_get_allocated_file_size(s
->extents
[i
].file
);
1316 static QEMUOptionParameter vmdk_create_options
[] = {
1318 .name
= BLOCK_OPT_SIZE
,
1320 .help
= "Virtual disk size"
1323 .name
= BLOCK_OPT_BACKING_FILE
,
1325 .help
= "File name of a base image"
1328 .name
= BLOCK_OPT_COMPAT6
,
1330 .help
= "VMDK version 6 image"
1333 .name
= BLOCK_OPT_SUBFMT
,
1336 "VMDK flat extent format, can be one of "
1337 "{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse | twoGbMaxExtentFlat} "
1342 static BlockDriver bdrv_vmdk
= {
1343 .format_name
= "vmdk",
1344 .instance_size
= sizeof(BDRVVmdkState
),
1345 .bdrv_probe
= vmdk_probe
,
1346 .bdrv_open
= vmdk_open
,
1347 .bdrv_read
= vmdk_read
,
1348 .bdrv_write
= vmdk_write
,
1349 .bdrv_close
= vmdk_close
,
1350 .bdrv_create
= vmdk_create
,
1351 .bdrv_flush
= vmdk_flush
,
1352 .bdrv_is_allocated
= vmdk_is_allocated
,
1353 .bdrv_get_allocated_file_size
= vmdk_get_allocated_file_size
,
1355 .create_options
= vmdk_create_options
,
1358 static void bdrv_vmdk_init(void)
1360 bdrv_register(&bdrv_vmdk
);
1363 block_init(bdrv_vmdk_init
);