1 From 0d0f2ba577bd05491b5954751787f8b969ca1ec3 Mon Sep 17 00:00:00 2001
2 From: Fam Zheng <famcool@gmail.com>
3 Date: Tue, 19 Jul 2011 08:45:23 +0800
4 Subject: [PATCH 11/12] VMDK: create different subformats
6 Add create option 'format', with enums:
11 Each creates a subformat image file. The default is monolithicSparse.
13 Signed-off-by: Fam Zheng <famcool@gmail.com>
14 Reviewed-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
15 Signed-off-by: Kevin Wolf <kwolf@redhat.com>
17 block/vmdk.c | 503 ++++++++++++++++++++++++++++++++---------------------------
19 2 files changed, 275 insertions(+), 229 deletions(-)
23 @@ -156,8 +156,9 @@ static int vmdk_probe(const uint8_t *buf
26 #define SECTOR_SIZE 512
27 -#define DESC_SIZE 20*SECTOR_SIZE // 20 sectors of 512 bytes each
28 -#define HEADER_SIZE 512 // first sector of 512 bytes
29 +#define DESC_SIZE (20 * SECTOR_SIZE) /* 20 sectors of 512 bytes each */
30 +#define BUF_SIZE 4096
31 +#define HEADER_SIZE 512 /* first sector of 512 bytes */
33 static void vmdk_free_extents(BlockDriverState *bs)
35 @@ -243,168 +244,6 @@ static int vmdk_is_cid_valid(BlockDriver
39 -static int vmdk_snapshot_create(const char *filename, const char *backing_file)
44 - char *p_name, *gd_buf, *rgd_buf;
45 - const char *real_filename, *temp_str;
47 - uint32_t gde_entries, gd_size;
48 - int64_t gd_offset, rgd_offset, capacity, gt_size;
49 - char p_desc[DESC_SIZE], s_desc[DESC_SIZE], hdr[HEADER_SIZE];
50 - static const char desc_template[] =
51 - "# Disk DescriptorFile\n"
55 - "createType=\"monolithicSparse\"\n"
56 - "parentFileNameHint=\"%s\"\n"
58 - "# Extent description\n"
59 - "RW %u SPARSE \"%s\"\n"
61 - "# The Disk Data Base \n"
65 - snp_fd = open(filename, O_RDWR | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, 0644);
68 - p_fd = open(backing_file, O_RDONLY | O_BINARY | O_LARGEFILE);
74 - /* read the header */
75 - if (lseek(p_fd, 0x0, SEEK_SET) == -1) {
79 - if (read(p_fd, hdr, HEADER_SIZE) != HEADER_SIZE) {
84 - /* write the header */
85 - if (lseek(snp_fd, 0x0, SEEK_SET) == -1) {
89 - if (write(snp_fd, hdr, HEADER_SIZE) == -1) {
94 - memset(&header, 0, sizeof(header));
95 - memcpy(&header,&hdr[4], sizeof(header)); // skip the VMDK4_MAGIC
97 - if (ftruncate(snp_fd, header.grain_offset << 9)) {
101 - /* the descriptor offset = 0x200 */
102 - if (lseek(p_fd, 0x200, SEEK_SET) == -1) {
106 - if (read(p_fd, p_desc, DESC_SIZE) != DESC_SIZE) {
111 - if ((p_name = strstr(p_desc,"CID")) != NULL) {
112 - p_name += sizeof("CID");
113 - sscanf(p_name,"%x",&p_cid);
116 - real_filename = filename;
117 - if ((temp_str = strrchr(real_filename, '\\')) != NULL)
118 - real_filename = temp_str + 1;
119 - if ((temp_str = strrchr(real_filename, '/')) != NULL)
120 - real_filename = temp_str + 1;
121 - if ((temp_str = strrchr(real_filename, ':')) != NULL)
122 - real_filename = temp_str + 1;
124 - snprintf(s_desc, sizeof(s_desc), desc_template, p_cid, p_cid, backing_file,
125 - (uint32_t)header.capacity, real_filename);
127 - /* write the descriptor */
128 - if (lseek(snp_fd, 0x200, SEEK_SET) == -1) {
132 - if (write(snp_fd, s_desc, strlen(s_desc)) == -1) {
137 - gd_offset = header.gd_offset * SECTOR_SIZE; // offset of GD table
138 - rgd_offset = header.rgd_offset * SECTOR_SIZE; // offset of RGD table
139 - capacity = header.capacity * SECTOR_SIZE; // Extent size
141 - * Each GDE span 32M disk, means:
142 - * 512 GTE per GT, each GTE points to grain
144 - gt_size = (int64_t)header.num_gtes_per_gte * header.granularity * SECTOR_SIZE;
149 - gde_entries = (uint32_t)(capacity / gt_size); // number of gde/rgde
150 - gd_size = gde_entries * sizeof(uint32_t);
153 - rgd_buf = qemu_malloc(gd_size);
154 - if (lseek(p_fd, rgd_offset, SEEK_SET) == -1) {
158 - if (read(p_fd, rgd_buf, gd_size) != gd_size) {
162 - if (lseek(snp_fd, rgd_offset, SEEK_SET) == -1) {
166 - if (write(snp_fd, rgd_buf, gd_size) == -1) {
172 - gd_buf = qemu_malloc(gd_size);
173 - if (lseek(p_fd, gd_offset, SEEK_SET) == -1) {
177 - if (read(p_fd, gd_buf, gd_size) != gd_size) {
181 - if (lseek(snp_fd, gd_offset, SEEK_SET) == -1) {
185 - if (write(snp_fd, gd_buf, gd_size) == -1) {
194 - qemu_free(rgd_buf);
201 static int vmdk_parent_open(BlockDriverState *bs)
204 @@ -1058,68 +897,40 @@ static int vmdk_write(BlockDriverState *
208 -static int vmdk_create(const char *filename, QEMUOptionParameter *options)
210 +static int vmdk_create_extent(const char *filename, int64_t filesize, bool flat)
216 uint32_t tmp, magic, grains, gd_size, gt_size, gt_count;
217 - static const char desc_template[] =
218 - "# Disk DescriptorFile\n"
221 - "parentCID=ffffffff\n"
222 - "createType=\"monolithicSparse\"\n"
224 - "# Extent description\n"
225 - "RW %" PRId64 " SPARSE \"%s\"\n"
227 - "# The Disk Data Base \n"
230 - "ddb.virtualHWVersion = \"%d\"\n"
231 - "ddb.geometry.cylinders = \"%" PRId64 "\"\n"
232 - "ddb.geometry.heads = \"16\"\n"
233 - "ddb.geometry.sectors = \"63\"\n"
234 - "ddb.adapterType = \"ide\"\n";
236 - const char *real_filename, *temp_str;
237 - int64_t total_size = 0;
238 - const char *backing_file = NULL;
242 - // Read out options
243 - while (options && options->name) {
244 - if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
245 - total_size = options->value.n / 512;
246 - } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
247 - backing_file = options->value.s;
248 - } else if (!strcmp(options->name, BLOCK_OPT_COMPAT6)) {
249 - flags |= options->value.n ? BLOCK_FLAG_COMPAT6: 0;
254 + O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
260 - /* XXX: add support for backing file */
261 - if (backing_file) {
262 - return vmdk_snapshot_create(filename, backing_file);
264 + ret = ftruncate(fd, filesize);
271 - fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
275 magic = cpu_to_be32(VMDK4_MAGIC);
276 memset(&header, 0, sizeof(header));
278 header.flags = 3; /* ?? */
279 - header.capacity = total_size;
280 + header.capacity = filesize / 512;
281 header.granularity = 128;
282 header.num_gtes_per_gte = 512;
284 - grains = (total_size + header.granularity - 1) / header.granularity;
285 + grains = (filesize / 512 + header.granularity - 1) / header.granularity;
286 gt_size = ((header.num_gtes_per_gte * sizeof(uint32_t)) + 511) >> 9;
287 - gt_count = (grains + header.num_gtes_per_gte - 1) / header.num_gtes_per_gte;
289 + (grains + header.num_gtes_per_gte - 1) / header.num_gtes_per_gte;
290 gd_size = (gt_count * sizeof(uint32_t) + 511) >> 9;
292 header.desc_offset = 1;
293 @@ -1130,7 +941,6 @@ static int vmdk_create(const char *filen
294 ((header.gd_offset + gd_size + (gt_size * gt_count) +
295 header.granularity - 1) / header.granularity) *
298 /* swap endianness for all header fields */
299 header.version = cpu_to_le32(header.version);
300 header.flags = cpu_to_le32(header.flags);
301 @@ -1188,27 +998,255 @@ static int vmdk_create(const char *filen
305 - /* compose the descriptor */
306 - real_filename = filename;
307 - if ((temp_str = strrchr(real_filename, '\\')) != NULL)
308 - real_filename = temp_str + 1;
309 - if ((temp_str = strrchr(real_filename, '/')) != NULL)
310 - real_filename = temp_str + 1;
311 - if ((temp_str = strrchr(real_filename, ':')) != NULL)
312 - real_filename = temp_str + 1;
313 - snprintf(desc, sizeof(desc), desc_template, (unsigned int)time(NULL),
314 - total_size, real_filename,
315 - (flags & BLOCK_FLAG_COMPAT6 ? 6 : 4),
316 - total_size / (int64_t)(63 * 16));
323 - /* write the descriptor */
324 - lseek(fd, le64_to_cpu(header.desc_offset) << 9, SEEK_SET);
325 +static int filename_decompose(const char *filename, char *path, char *prefix,
326 + char *postfix, size_t buf_len)
330 + if (filename == NULL || !strlen(filename)) {
331 + fprintf(stderr, "Vmdk: no filename provided.\n");
334 + p = strrchr(filename, '/');
336 + p = strrchr(filename, '\\');
339 + p = strrchr(filename, ':');
343 + if (p - filename >= buf_len) {
346 + pstrcpy(path, p - filename + 1, filename);
351 + q = strrchr(p, '.');
353 + pstrcpy(prefix, buf_len, p);
356 + if (q - p >= buf_len) {
359 + pstrcpy(prefix, q - p + 1, p);
360 + pstrcpy(postfix, buf_len, q);
365 +static int relative_path(char *dest, int dest_size,
366 + const char *base, const char *target)
372 + const char *sep = "\\";
374 + const char *sep = "/";
377 + if (!(dest && base && target)) {
380 + if (path_is_absolute(target)) {
381 + dest[dest_size - 1] = '\0';
382 + strncpy(dest, target, dest_size - 1);
385 + while (base[i] == target[i]) {
398 + pstrcat(dest, dest_size, "..");
399 + pstrcat(dest, dest_size, sep);
401 + pstrcat(dest, dest_size, q);
405 +static int vmdk_create(const char *filename, QEMUOptionParameter *options)
408 + char desc[BUF_SIZE];
409 + int64_t total_size = 0, filesize;
410 + const char *backing_file = NULL;
411 + const char *fmt = NULL;
415 + char ext_desc_lines[BUF_SIZE] = "";
416 + char path[PATH_MAX], prefix[PATH_MAX], postfix[PATH_MAX];
417 + const int64_t split_size = 0x80000000; /* VMDK has constant split size */
418 + const char *desc_extent_line;
419 + char parent_desc_line[BUF_SIZE] = "";
420 + uint32_t parent_cid = 0xffffffff;
421 + const char desc_template[] =
422 + "# Disk DescriptorFile\n"
426 + "createType=\"%s\"\n"
429 + "# Extent description\n"
432 + "# The Disk Data Base\n"
435 + "ddb.virtualHWVersion = \"%d\"\n"
436 + "ddb.geometry.cylinders = \"%" PRId64 "\"\n"
437 + "ddb.geometry.heads = \"16\"\n"
438 + "ddb.geometry.sectors = \"63\"\n"
439 + "ddb.adapterType = \"ide\"\n";
441 + if (filename_decompose(filename, path, prefix, postfix, PATH_MAX)) {
444 + /* Read out options */
445 + while (options && options->name) {
446 + if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
447 + total_size = options->value.n;
448 + } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
449 + backing_file = options->value.s;
450 + } else if (!strcmp(options->name, BLOCK_OPT_COMPAT6)) {
451 + flags |= options->value.n ? BLOCK_FLAG_COMPAT6 : 0;
452 + } else if (!strcmp(options->name, BLOCK_OPT_SUBFMT)) {
453 + fmt = options->value.s;
458 + /* Default format to monolithicSparse */
459 + fmt = "monolithicSparse";
460 + } else if (strcmp(fmt, "monolithicFlat") &&
461 + strcmp(fmt, "monolithicSparse") &&
462 + strcmp(fmt, "twoGbMaxExtentSparse") &&
463 + strcmp(fmt, "twoGbMaxExtentFlat")) {
464 + fprintf(stderr, "VMDK: Unknown subformat: %s\n", fmt);
467 + split = !(strcmp(fmt, "twoGbMaxExtentFlat") &&
468 + strcmp(fmt, "twoGbMaxExtentSparse"));
469 + flat = !(strcmp(fmt, "monolithicFlat") &&
470 + strcmp(fmt, "twoGbMaxExtentFlat"));
472 + desc_extent_line = "RW %lld FLAT \"%s\" 0\n";
474 + desc_extent_line = "RW %lld SPARSE \"%s\"\n";
476 + if (flat && backing_file) {
477 + /* not supporting backing file for flat image */
480 + if (backing_file) {
481 + char parent_filename[PATH_MAX];
482 + BlockDriverState *bs = bdrv_new("");
483 + ret = bdrv_open(bs, backing_file, 0, NULL);
488 + if (strcmp(bs->drv->format_name, "vmdk")) {
492 + filesize = bdrv_getlength(bs);
493 + parent_cid = vmdk_read_cid(bs, 0);
495 + relative_path(parent_filename, sizeof(parent_filename),
496 + filename, backing_file);
497 + snprintf(parent_desc_line, sizeof(parent_desc_line),
498 + "parentFileNameHint=\"%s\"", parent_filename);
501 + /* Create extents */
502 + filesize = total_size;
503 + while (filesize > 0) {
504 + char desc_line[BUF_SIZE];
505 + char ext_filename[PATH_MAX];
506 + char desc_filename[PATH_MAX];
507 + int64_t size = filesize;
509 + if (split && size > split_size) {
513 + snprintf(desc_filename, sizeof(desc_filename), "%s-%c%03d%s",
514 + prefix, flat ? 'f' : 's', ++idx, postfix);
516 + snprintf(desc_filename, sizeof(desc_filename), "%s-flat%s",
519 + snprintf(desc_filename, sizeof(desc_filename), "%s%s",
522 + snprintf(ext_filename, sizeof(ext_filename), "%s%s",
523 + path, desc_filename);
525 + if (vmdk_create_extent(ext_filename, size, flat)) {
530 + /* Format description line */
531 + snprintf(desc_line, sizeof(desc_line),
532 + desc_extent_line, size / 512, desc_filename);
533 + pstrcat(ext_desc_lines, sizeof(ext_desc_lines), desc_line);
535 + /* generate descriptor file */
536 + snprintf(desc, sizeof(desc), desc_template,
537 + (unsigned int)time(NULL),
542 + (flags & BLOCK_FLAG_COMPAT6 ? 6 : 4),
543 + total_size / (int64_t)(63 * 16 * 512));
544 + if (split || flat) {
547 + O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
552 + O_WRONLY | O_BINARY | O_LARGEFILE,
558 + /* the descriptor offset = 0x200 */
559 + if (!split && !flat && 0x200 != lseek(fd, 0x200, SEEK_SET)) {
563 ret = qemu_write_full(fd, desc, strlen(desc));
564 if (ret != strlen(desc)) {
572 @@ -1252,6 +1290,13 @@ static QEMUOptionParameter vmdk_create_o
574 .help = "VMDK version 6 image"
577 + .name = BLOCK_OPT_SUBFMT,
578 + .type = OPT_STRING,
580 + "VMDK flat extent format, can be one of "
581 + "{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse | twoGbMaxExtentFlat} "
589 #define BLOCK_OPT_CLUSTER_SIZE "cluster_size"
590 #define BLOCK_OPT_TABLE_SIZE "table_size"
591 #define BLOCK_OPT_PREALLOC "preallocation"
592 +#define BLOCK_OPT_SUBFMT "subformat"
594 typedef struct AIOPool {
595 void (*cancel)(BlockDriverAIOCB *acb);