From patchwork Tue Aug 13 04:32:00 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robert Wang X-Patchwork-Id: 266698 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (unknown [IPv6:2001:4830:134:3::12]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 01EEC2C00B6 for ; Tue, 13 Aug 2013 14:40:13 +1000 (EST) Received: from localhost ([::1]:33052 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V96Ox-0005WZ-UZ for incoming@patchwork.ozlabs.org; Tue, 13 Aug 2013 00:40:11 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55140) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V96Ix-000850-7z for qemu-devel@nongnu.org; Tue, 13 Aug 2013 00:34:08 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1V96Io-0000b8-QO for qemu-devel@nongnu.org; Tue, 13 Aug 2013 00:33:59 -0400 Received: from mail-pb0-x22b.google.com ([2607:f8b0:400e:c01::22b]:61836) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V96Io-0000ay-C6 for qemu-devel@nongnu.org; Tue, 13 Aug 2013 00:33:50 -0400 Received: by mail-pb0-f43.google.com with SMTP id md4so7510506pbc.16 for ; Mon, 12 Aug 2013 21:33:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=k5m7fOhgGJf1kycTFunVZ1wV99Anvzp+sqnkb3LsegA=; b=CvFQZlaP2/iBHC9kXH+is3+kHOkas924fZTpYzD5HO2al3/vzxD67L9QFYFVokETIR ozjWOWwtYp+tzOe427QugJvPbctP5NQ87/mZS3CJj46WFIxFuXtpYIxt2Kj0IykPnNm0 O2LlctMZ90yQq9vBg1xeurxzJ7W5pVfZmYPtm6tJtXffwqfJGAuuu2uAK8TwXRm/xp7D 0YtWFsktu46KYL9GfqTavbS0aM/2TOCMv6e35bQ8LD+hmi/YJ22Ja3OW25ZnP/8i6Cpf ARv1K/q8Mqb4/1Objc7BGC203BQTGjn1wxgprOWuAWenydweiTROlHb5LoE1/b+lwRhd 1n1A== X-Received: by 10.68.115.15 with SMTP id jk15mr2492110pbb.36.1376368429597; Mon, 12 Aug 2013 21:33:49 -0700 (PDT) Received: from 11.wdongxu.kvm58 ([202.108.130.153]) by mx.google.com with ESMTPSA id mz5sm10285023pbc.18.2013.08.12.21.33.46 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Mon, 12 Aug 2013 21:33:49 -0700 (PDT) From: Dong Xu Wang To: qemu-devel@nongnu.org Date: Tue, 13 Aug 2013 12:32:00 +0800 Message-Id: <1376368326-7433-20-git-send-email-wdongxu@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.11.7 In-Reply-To: <1376368326-7433-1-git-send-email-wdongxu@linux.vnet.ibm.com> References: <1376368326-7433-1-git-send-email-wdongxu@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:400e:c01::22b Cc: kwolf@redhat.com, wdongxu@cn.ibm.com, stefanha@redhat.com, Dong Xu Wang Subject: [Qemu-devel] [PATCH V18 19/25] block: add QemuOpts support for vdi.c X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Signed-off-by: Dong Xu Wang --- 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, };