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 19/25] block: add QemuOpts support for vdi.c
Date: Tue, 13 Aug 2013 12:32:00 +0800	[thread overview]
Message-ID: <1376368326-7433-20-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/vdi.c | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 128 insertions(+)

diff --git a/block/vdi.c b/block/vdi.c
index 8a91525..50bf24f 100644
--- a/block/vdi.c
+++ b/block/vdi.c
@@ -736,6 +736,102 @@ static int vdi_create(const char *filename, QEMUOptionParameter *options)
     return result;
 }
 
+static int vdi_create_new(const char *filename, QemuOpts *opts)
+{
+    int fd;
+    int result = 0;
+    uint64_t bytes = 0;
+    uint32_t blocks;
+    size_t block_size = DEFAULT_CLUSTER_SIZE;
+    uint32_t image_type = VDI_TYPE_DYNAMIC;
+    VdiHeader header;
+    size_t i;
+    size_t bmap_size;
+
+    logout("\n");
+
+    /* Read out options. */
+    bytes = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0);
+#if defined(CONFIG_VDI_BLOCK_SIZE)
+    /* TODO: Additional checks (SECTOR_SIZE * 2^n, ...). */
+    block_size = qemu_opt_get_size_del(opts,
+                                       BLOCK_OPT_CLUSTER_SIZE,
+                                       DEFAULT_CLUSTER_SIZE);
+#endif
+#if defined(CONFIG_VDI_STATIC_IMAGE)
+    if (qemu_opt_get_bool_del(opts, BLOCK_OPT_STATIC, false)) {
+        image_type = VDI_TYPE_STATIC;
+    }
+#endif
+
+    fd = qemu_open(filename,
+                   O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
+                   0644);
+    if (fd < 0) {
+        return -errno;
+    }
+
+    /* We need enough blocks to store the given disk size,
+       so always round up. */
+    blocks = (bytes + block_size - 1) / block_size;
+
+    bmap_size = blocks * sizeof(uint32_t);
+    bmap_size = ((bmap_size + SECTOR_SIZE - 1) & ~(SECTOR_SIZE - 1));
+
+    memset(&header, 0, sizeof(header));
+    pstrcpy(header.text, sizeof(header.text), VDI_TEXT);
+    header.signature = VDI_SIGNATURE;
+    header.version = VDI_VERSION_1_1;
+    header.header_size = 0x180;
+    header.image_type = image_type;
+    header.offset_bmap = 0x200;
+    header.offset_data = 0x200 + bmap_size;
+    header.sector_size = SECTOR_SIZE;
+    header.disk_size = bytes;
+    header.block_size = block_size;
+    header.blocks_in_image = blocks;
+    if (image_type == VDI_TYPE_STATIC) {
+        header.blocks_allocated = blocks;
+    }
+    uuid_generate(header.uuid_image);
+    uuid_generate(header.uuid_last_snap);
+    /* There is no need to set header.uuid_link or header.uuid_parent here. */
+#if defined(CONFIG_VDI_DEBUG)
+    vdi_header_print(&header);
+#endif
+    vdi_header_to_le(&header);
+    if (write(fd, &header, sizeof(header)) < 0) {
+        result = -errno;
+    }
+
+    if (bmap_size > 0) {
+        uint32_t *bmap = g_malloc0(bmap_size);
+        for (i = 0; i < blocks; i++) {
+            if (image_type == VDI_TYPE_STATIC) {
+                bmap[i] = i;
+            } else {
+                bmap[i] = VDI_UNALLOCATED;
+            }
+        }
+        if (write(fd, bmap, bmap_size) < 0) {
+            result = -errno;
+        }
+        g_free(bmap);
+    }
+
+    if (image_type == VDI_TYPE_STATIC) {
+        if (ftruncate(fd, sizeof(header) + bmap_size + blocks * block_size)) {
+            result = -errno;
+        }
+    }
+
+    if (close(fd) < 0) {
+        result = -errno;
+    }
+
+    return result;
+}
+
 static void vdi_close(BlockDriverState *bs)
 {
     BDRVVdiState *s = bs->opaque;
@@ -771,6 +867,36 @@ static QEMUOptionParameter vdi_create_options[] = {
     { NULL }
 };
 
+static QemuOptsList vdi_create_opts = {
+    .name = "vdi-create-opts",
+    .head = QTAILQ_HEAD_INITIALIZER(vdi_create_opts.head),
+    .desc = {
+        {
+            .name = BLOCK_OPT_SIZE,
+            .type = QEMU_OPT_SIZE,
+            .help = "Virtual disk size"
+        },
+#if defined(CONFIG_VDI_BLOCK_SIZE)
+        {
+            .name = BLOCK_OPT_CLUSTER_SIZE,
+            .type = QEMU_OPT_SIZE,
+            .help = "VDI cluster (block) size",
+            .def_value_str = stringify(DEFAULT_CLUSTER_SIZE)
+        },
+#endif
+#if defined(CONFIG_VDI_STATIC_IMAGE)
+        {
+            .name = BLOCK_OPT_STATIC,
+            .type = QEMU_OPT_BOOL,
+            .help = "VDI static (pre-allocated) image",
+            .def_value_str = "off"
+        },
+#endif
+        /* TODO: An additional option to set UUID values might be useful. */
+        { /* end of list */ }
+    }
+};
+
 static BlockDriver bdrv_vdi = {
     .format_name = "vdi",
     .instance_size = sizeof(BDRVVdiState),
@@ -779,6 +905,7 @@ static BlockDriver bdrv_vdi = {
     .bdrv_close = vdi_close,
     .bdrv_reopen_prepare = vdi_reopen_prepare,
     .bdrv_create = vdi_create,
+    .bdrv_create_new = vdi_create_new,
     .bdrv_has_zero_init = bdrv_has_zero_init_1,
     .bdrv_co_is_allocated = vdi_co_is_allocated,
     .bdrv_make_empty = vdi_make_empty,
@@ -791,6 +918,7 @@ static BlockDriver bdrv_vdi = {
     .bdrv_get_info = vdi_get_info,
 
     .create_options = vdi_create_options,
+    .bdrv_create_opts = &vdi_create_opts,
     .bdrv_check = vdi_check,
 };
 
-- 
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 ` Dong Xu Wang [this message]
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 ` [Qemu-devel] [PATCH V18 21/25] block: add QemuOpts support for vpc.c Dong Xu Wang
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-20-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).