From patchwork Tue Aug 13 04:31:48 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robert Wang X-Patchwork-Id: 266709 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 8259A2C00F2 for ; Tue, 13 Aug 2013 14:53:00 +1000 (EST) Received: from localhost ([::1]:56412 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V96Mz-0002ri-QN for incoming@patchwork.ozlabs.org; Tue, 13 Aug 2013 00:38:09 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54788) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V96IG-0006pS-9t for qemu-devel@nongnu.org; Tue, 13 Aug 2013 00:33:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1V96I7-0000KV-TI for qemu-devel@nongnu.org; Tue, 13 Aug 2013 00:33:16 -0400 Received: from mail-pd0-x22d.google.com ([2607:f8b0:400e:c02::22d]:65502) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V96I7-0000KA-KV for qemu-devel@nongnu.org; Tue, 13 Aug 2013 00:33:07 -0400 Received: by mail-pd0-f173.google.com with SMTP id p10so4349254pdj.32 for ; Mon, 12 Aug 2013 21:33:06 -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=mfjs3yaHBI1WCSr8vft5e6epO5pATwm34/8wLeK9cNI=; b=DuSx34ChbTsKD90FsIRs52+jioIAwkDfoZMy2Sse8niFdbtP7AZ9kLmHg6m6YRzdiV i2bedjz1jZjM+SqPrzpDd2fGmeuZVpO251Y5PF3XBTCzk/6Of2G0s1vtZhYyEp9lsyVK JuqT4y0F/W1YS9N1wYQjB9VfxkfihGW3IP2Uc07YK+98PCCu/C2zhxngM4j+7wL9thWE e33u/LyZq4wMCuL/dp9JLKycf0IBUlUfRAQRmtCwlvfDpfFW9P8UiTNt5qoQy9GtamuF wG4FeSbKw2AT9JeQqI67+ftNQ2GYsJjYonn7+CsaNdVOhO1SGCywEnT4LQxfLS8Zqu2x s2uQ== X-Received: by 10.68.20.200 with SMTP id p8mr2474110pbe.122.1376368386900; Mon, 12 Aug 2013 21:33:06 -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.03 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Mon, 12 Aug 2013 21:33:06 -0700 (PDT) From: Dong Xu Wang To: qemu-devel@nongnu.org Date: Tue, 13 Aug 2013 12:31:48 +0800 Message-Id: <1376368326-7433-8-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:c02::22d Cc: kwolf@redhat.com, wdongxu@cn.ibm.com, stefanha@redhat.com, Dong Xu Wang Subject: [Qemu-devel] [PATCH V18 07/25] block: add QemuOpts support for cow.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/cow.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/block/cow.c b/block/cow.c index 1cc2e89..3a0d450 100644 --- a/block/cow.c +++ b/block/cow.c @@ -332,6 +332,83 @@ static QEMUOptionParameter cow_create_options[] = { { NULL } }; +static int cow_create_new(const char *filename, QemuOpts *opts) +{ + struct cow_header_v2 cow_header; + struct stat st; + int64_t image_sectors = 0; + char *image_filename = NULL; + int ret; + BlockDriverState *cow_bs; + + /* Read out opts */ + image_sectors = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0) / 512; + image_filename = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE); + + ret = bdrv_create_file_new(filename, opts); + if (ret < 0) { + goto finish; + } + + ret = bdrv_file_open(&cow_bs, filename, NULL, BDRV_O_RDWR); + if (ret < 0) { + goto finish; + } + + memset(&cow_header, 0, sizeof(cow_header)); + cow_header.magic = cpu_to_be32(COW_MAGIC); + cow_header.version = cpu_to_be32(COW_VERSION); + if (image_filename) { + /* Note: if no file, we put a dummy mtime */ + cow_header.mtime = cpu_to_be32(0); + + if (stat(image_filename, &st) != 0) { + goto mtime_fail; + } + cow_header.mtime = cpu_to_be32(st.st_mtime); + mtime_fail: + pstrcpy(cow_header.backing_file, sizeof(cow_header.backing_file), + image_filename); + } + cow_header.sectorsize = cpu_to_be32(512); + cow_header.size = cpu_to_be64(image_sectors * 512); + ret = bdrv_pwrite(cow_bs, 0, &cow_header, sizeof(cow_header)); + if (ret < 0) { + goto exit; + } + + /* resize to include at least all the bitmap */ + ret = bdrv_truncate(cow_bs, + sizeof(cow_header) + ((image_sectors + 7) >> 3)); + if (ret < 0) { + goto exit; + } + +exit: + bdrv_delete(cow_bs); +finish: + g_free(image_filename); + return ret; +} + +static QemuOptsList cow_create_opts = { + .name = "cow-create-opts", + .head = QTAILQ_HEAD_INITIALIZER(cow_create_opts.head), + .desc = { + { + .name = BLOCK_OPT_SIZE, + .type = QEMU_OPT_SIZE, + .help = "Virtual disk size" + }, + { + .name = BLOCK_OPT_BACKING_FILE, + .type = QEMU_OPT_STRING, + .help = "File name of a base image" + }, + { /* end of list */ } + } +}; + static BlockDriver bdrv_cow = { .format_name = "cow", .instance_size = sizeof(BDRVCowState), @@ -340,6 +417,7 @@ static BlockDriver bdrv_cow = { .bdrv_open = cow_open, .bdrv_close = cow_close, .bdrv_create = cow_create, + .bdrv_create_new = cow_create_new, .bdrv_has_zero_init = bdrv_has_zero_init_1, .bdrv_read = cow_co_read, @@ -347,6 +425,7 @@ static BlockDriver bdrv_cow = { .bdrv_co_is_allocated = cow_co_is_allocated, .create_options = cow_create_options, + .bdrv_create_opts = &cow_create_opts, }; static void bdrv_cow_init(void)