qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Dong Xu Wang <wdongxu@linux.vnet.ibm.com>
To: qemu-devel@nongnu.org
Cc: kwolf@redhat.com, wdongxu@cn.ibm.com, stefanha@redhat.com,
	Dong Xu Wang <wdongxu@linux.vnet.ibm.com>
Subject: [Qemu-devel] [PATCH V18 21/25] block: add QemuOpts support for vpc.c
Date: Tue, 13 Aug 2013 12:32:02 +0800	[thread overview]
Message-ID: <1376368326-7433-22-git-send-email-wdongxu@linux.vnet.ibm.com> (raw)
In-Reply-To: <1376368326-7433-1-git-send-email-wdongxu@linux.vnet.ibm.com>

Signed-off-by: Dong Xu Wang <wdongxu@linux.vnet.ibm.com>
---
 block/vpc.c | 125 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 125 insertions(+)

diff --git a/block/vpc.c b/block/vpc.c
index fe4f311..aa1263a 100644
--- a/block/vpc.c
+++ b/block/vpc.c
@@ -786,6 +786,109 @@ static int vpc_create(const char *filename, QEMUOptionParameter *options)
     return ret;
 }
 
+static int vpc_create_new(const char *filename, QemuOpts *opts)
+{
+    uint8_t buf[1024];
+    struct vhd_footer *footer = (struct vhd_footer *) buf;
+    char *disk_type_param = NULL;
+    int fd, i;
+    uint16_t cyls = 0;
+    uint8_t heads = 0;
+    uint8_t secs_per_cyl = 0;
+    int64_t total_sectors;
+    int64_t total_size = 0;
+    int disk_type = VHD_DYNAMIC;
+    int ret = -EIO;
+
+    /* Read out opts */
+    total_size = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0);
+    disk_type_param = qemu_opt_get_del(opts, BLOCK_OPT_SUBFMT);
+    if (disk_type_param) {
+        if (!strcmp(disk_type_param, "dynamic")) {
+            disk_type = VHD_DYNAMIC;
+        } else if (!strcmp(disk_type_param, "fixed")) {
+            disk_type = VHD_FIXED;
+        } else {
+            ret = -EINVAL;
+            goto finish;
+        }
+    }
+
+    /* Create the file */
+    fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
+    if (fd < 0) {
+        ret = -EIO;
+        goto finish;
+    }
+
+    /*
+     * Calculate matching total_size and geometry. Increase the number of
+     * sectors requested until we get enough (or fail). This ensures that
+     * qemu-img convert doesn't truncate images, but rather rounds up.
+     */
+    total_sectors = total_size / BDRV_SECTOR_SIZE;
+    for (i = 0; total_sectors > (int64_t)cyls * heads * secs_per_cyl; i++) {
+        if (calculate_geometry(total_sectors + i, &cyls, &heads,
+                               &secs_per_cyl)) {
+            ret = -EFBIG;
+            goto fail;
+        }
+    }
+
+    total_sectors = (int64_t) cyls * heads * secs_per_cyl;
+
+    /* Prepare the Hard Disk Footer */
+    memset(buf, 0, 1024);
+
+    memcpy(footer->creator, "conectix", 8);
+    /* TODO Check if "qemu" creator_app is ok for VPC */
+    memcpy(footer->creator_app, "qemu", 4);
+    memcpy(footer->creator_os, "Wi2k", 4);
+
+    footer->features = be32_to_cpu(0x02);
+    footer->version = be32_to_cpu(0x00010000);
+    if (disk_type == VHD_DYNAMIC) {
+        footer->data_offset = be64_to_cpu(HEADER_SIZE);
+    } else {
+        footer->data_offset = be64_to_cpu(0xFFFFFFFFFFFFFFFFULL);
+    }
+    footer->timestamp = be32_to_cpu(time(NULL) - VHD_TIMESTAMP_BASE);
+
+    /* Version of Virtual PC 2007 */
+    footer->major = be16_to_cpu(0x0005);
+    footer->minor = be16_to_cpu(0x0003);
+    if (disk_type == VHD_DYNAMIC) {
+        footer->orig_size = be64_to_cpu(total_sectors * 512);
+        footer->size = be64_to_cpu(total_sectors * 512);
+    } else {
+        footer->orig_size = be64_to_cpu(total_size);
+        footer->size = be64_to_cpu(total_size);
+    }
+    footer->cyls = be16_to_cpu(cyls);
+    footer->heads = heads;
+    footer->secs_per_cyl = secs_per_cyl;
+
+    footer->type = be32_to_cpu(disk_type);
+
+#if defined(CONFIG_UUID)
+    uuid_generate(footer->uuid);
+#endif
+
+    footer->checksum = be32_to_cpu(vpc_checksum(buf, HEADER_SIZE));
+
+    if (disk_type == VHD_DYNAMIC) {
+        ret = create_dynamic_disk(fd, buf, total_sectors);
+    } else {
+        ret = create_fixed_disk(fd, buf, total_size);
+    }
+
+ fail:
+    qemu_close(fd);
+finish:
+    g_free(disk_type_param);
+    return ret;
+}
+
 static int vpc_has_zero_init(BlockDriverState *bs)
 {
     BDRVVPCState *s = bs->opaque;
@@ -826,6 +929,26 @@ static QEMUOptionParameter vpc_create_options[] = {
     { NULL }
 };
 
+static QemuOptsList vpc_create_opts = {
+    .name = "vpc-create-opts",
+    .head = QTAILQ_HEAD_INITIALIZER(vpc_create_opts.head),
+    .desc = {
+        {
+            .name = BLOCK_OPT_SIZE,
+            .type = QEMU_OPT_SIZE,
+            .help = "Virtual disk size"
+        },
+        {
+            .name = BLOCK_OPT_SUBFMT,
+            .type = QEMU_OPT_STRING,
+            .help =
+                "Type of virtual hard disk format. Supported formats are "
+                "{dynamic (default) | fixed} "
+        },
+        { /* end of list */ }
+    }
+};
+
 static BlockDriver bdrv_vpc = {
     .format_name    = "vpc",
     .instance_size  = sizeof(BDRVVPCState),
@@ -835,11 +958,13 @@ static BlockDriver bdrv_vpc = {
     .bdrv_close             = vpc_close,
     .bdrv_reopen_prepare    = vpc_reopen_prepare,
     .bdrv_create            = vpc_create,
+    .bdrv_create_new        = vpc_create_new,
 
     .bdrv_read              = vpc_co_read,
     .bdrv_write             = vpc_co_write,
 
     .create_options         = vpc_create_options,
+    .bdrv_create_opts       = &vpc_create_opts,
     .bdrv_has_zero_init     = vpc_has_zero_init,
 };
 
-- 
1.7.11.7

  parent reply	other threads:[~2013-08-13  4:34 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-13  4:31 [Qemu-devel] [PATCH V18 00/25] replace QEMUOptionParameter with QemuOpts parser Dong Xu Wang
2013-08-13  4:31 ` [Qemu-devel] [PATCH V18 01/25] qemu-option: add def_value_str in QemuOptDesc struct and rewrite qemu_opts_print Dong Xu Wang
2013-08-27 13:49   ` Kevin Wolf
2013-08-28  8:55     ` Dong Xu Wang
2013-08-28 12:53   ` Eric Blake
2013-08-29  1:49     ` Dong Xu Wang
2013-08-13  4:31 ` [Qemu-devel] [PATCH V18 02/25] qemu-option: avoid duplication of default value in QemuOpts Dong Xu Wang
2013-08-28 12:57   ` Eric Blake
2013-08-29  1:49     ` Dong Xu Wang
2013-08-13  4:31 ` [Qemu-devel] [PATCH V18 03/25] qemu-option: create four QemuOptsList related functions Dong Xu Wang
2013-08-27 13:54   ` Kevin Wolf
2013-08-13  4:31 ` [Qemu-devel] [PATCH V18 04/25] qemu-option: create some QemuOpts functons Dong Xu Wang
2013-08-27 13:58   ` Kevin Wolf
2013-08-13  4:31 ` [Qemu-devel] [PATCH V18 05/25] qemu-option: opt->str store digit, without suffixes Dong Xu Wang
2013-08-27 14:01   ` Kevin Wolf
2013-08-13  4:31 ` [Qemu-devel] [PATCH V18 06/25] add interface to block Dong Xu Wang
2013-08-27 14:08   ` Kevin Wolf
2013-08-13  4:31 ` [Qemu-devel] [PATCH V18 07/25] block: add QemuOpts support for cow.c Dong Xu Wang
2013-08-13  4:31 ` [Qemu-devel] [PATCH V18 08/25] block: add QemuOpts support for gluster.c Dong Xu Wang
2013-08-13  4:31 ` [Qemu-devel] [PATCH V18 09/25] block: add QemuOpts support for iscsi.c Dong Xu Wang
2013-08-13  4:31 ` [Qemu-devel] [PATCH V18 10/25] block: add QemuOpts support for qcow.c Dong Xu Wang
2013-08-13  4:31 ` [Qemu-devel] [PATCH V18 11/25] block: add QemuOpts support for qcow2.c Dong Xu Wang
2013-08-13  4:31 ` [Qemu-devel] [PATCH V18 12/25] block: add QemuOpts support for qed.c Dong Xu Wang
2013-08-13  4:31 ` [Qemu-devel] [PATCH V18 13/25] block: add QemuOpts support for raw-posix.c Dong Xu Wang
2013-08-13  4:31 ` [Qemu-devel] [PATCH V18 14/25] block: add QemuOpts support for raw-win32.c Dong Xu Wang
2013-08-13  4:31 ` [Qemu-devel] [PATCH V18 15/25] block: add QemuOpts support for raw.c Dong Xu Wang
2013-08-13  4:31 ` [Qemu-devel] [PATCH V18 16/25] block: add QemuOpts support for rbd.c Dong Xu Wang
2013-08-13  4:31 ` [Qemu-devel] [PATCH V18 17/25] block: add QemuOpts support for sheepdog.c Dong Xu Wang
2013-08-13  4:31 ` [Qemu-devel] [PATCH V18 18/25] block: add QemuOpts support for ssh.c Dong Xu Wang
2013-08-13  4:32 ` [Qemu-devel] [PATCH V18 19/25] block: add QemuOpts support for vdi.c Dong Xu Wang
2013-08-13  4:32 ` [Qemu-devel] [PATCH V18 20/25] block: add QemuOpts support for vmdk.c Dong Xu Wang
2013-08-13  4:32 ` Dong Xu Wang [this message]
2013-08-13  4:32 ` [Qemu-devel] [PATCH V18 22/25] block: add QemuOpts support for block.c Dong Xu Wang
2013-08-13  4:32 ` [Qemu-devel] [PATCH V18 23/25] block: clean temp code and use QemuOpts in block Dong Xu Wang
2013-08-13  4:32 ` [Qemu-devel] [PATCH V18 24/25] qapi: query-command-line-options outputs def_value_str Dong Xu Wang
2013-08-27 14:12   ` Kevin Wolf
2013-08-27 23:13     ` Eric Blake
2013-08-28  8:57       ` Dong Xu Wang
2013-08-13  4:32 ` [Qemu-devel] [PATCH V18 25/25] qemu-option: remove QEMUOptionParameter related functions and struct Dong Xu Wang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1376368326-7433-22-git-send-email-wdongxu@linux.vnet.ibm.com \
    --to=wdongxu@linux.vnet.ibm.com \
    --cc=kwolf@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    --cc=wdongxu@cn.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).