From patchwork Tue Sep 11 08:37:06 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: mahaocong X-Patchwork-Id: 10595615 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 445396CB for ; Tue, 11 Sep 2018 12:22:44 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3425828D6C for ; Tue, 11 Sep 2018 12:22:44 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 280FE291B7; Tue, 11 Sep 2018 12:22:44 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.4 required=2.0 tests=BAYES_00,DKIM_SIGNED, FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_BL_SPAMCOP_NET,RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 7A5D528D6C for ; Tue, 11 Sep 2018 12:22:43 +0000 (UTC) Received: from localhost ([::1]:57431 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fzhgo-0000VM-H8 for patchwork-qemu-devel@patchwork.kernel.org; Tue, 11 Sep 2018 08:22:42 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42055) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fzeBG-0003YI-Iw for qemu-devel@nongnu.org; Tue, 11 Sep 2018 04:37:55 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fzeBD-000698-CE for qemu-devel@nongnu.org; Tue, 11 Sep 2018 04:37:54 -0400 Received: from m50-132.163.com ([123.125.50.132]:42706) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fzeBC-00066T-Ns; Tue, 11 Sep 2018 04:37:51 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-Id; bh=QPuzT4M1foRDbeP9kx vReOGNXNB2KPs3fnWA9Urv99U=; b=ZsH/ul1Hv6AHf0mwL0tko8nOjrEbcoExMm wjV/FxN3M1BiwJuhd8FgTdx7Qgb5utoFomB8oeqKuX+XRL1PInDf42pBuCR8TSRn 1/ZpRnzCERRGKxVwJHDloi8eOJfD6g1N7YN4Mp/6ZDVtj7vEWj9sv41b9AXTC6l9 EEQMsDdLQ= Received: from chishui0007.ys (unknown [111.202.144.35]) by smtp2 (Coremail) with SMTP id DNGowACnxgjUfpdbq4kPAA--.179S4; Tue, 11 Sep 2018 16:37:42 +0800 (CST) From: Ma Haocong To: qemu-devel@nongnu.org, qemu-block@nongnu.org Date: Tue, 11 Sep 2018 16:37:06 +0800 Message-Id: <20180911083706.5378-2-mahaocong_work@163.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20180911083706.5378-1-mahaocong_work@163.com> References: <20180911083706.5378-1-mahaocong_work@163.com> X-CM-TRANSID: DNGowACnxgjUfpdbq4kPAA--.179S4 X-Coremail-Antispam: 1Uf129KBjvJXoWxZr18XryxtFW5Cw1DZFW5Jrb_yoW5KFy8pF n3JrySgrnYya13Zr4fAa4kJw1ruwn5Xr1DJryxZ343tF4aqrn8X340kF1avFZrJF4UJa13 ZF4YyryDGa1YvFJanT9S1TB71UUUUUDqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07jCc_-UUUUU= X-Originating-IP: [111.202.144.35] X-CM-SenderInfo: 5pdkt05frqwspzruyqqrwthudrp/1tbiJxa+ClXloCgI+AAAsN X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 123.125.50.132 X-Mailman-Approved-At: Tue, 11 Sep 2018 08:21:43 -0400 Subject: [Qemu-devel] [PATCH v1 1/1] qemu-img: add new function to remove bitmap in image X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Ma Haocong , Max Reitz Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Signed-off-by: Ma Haocong --- qemu-img-cmds.hx | 6 +++ qemu-img.c | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) diff --git a/qemu-img-cmds.hx b/qemu-img-cmds.hx index 1526f327a5..cc397b64e4 100644 --- a/qemu-img-cmds.hx +++ b/qemu-img-cmds.hx @@ -97,6 +97,12 @@ STEXI @item resize [--object @var{objectdef}] [--image-opts] [-f @var{fmt}] [--preallocation=@var{prealloc}] [-q] [--shrink] @var{filename} [+ | -]@var{size} ETEXI +DEF("removebmp", img_removebmp, + "removebmp [--object objectdef] [--image-opts] [-q] [-f fmt] filename dirtybitmap") +STEXI +@item removebmp [--object @var{objectdef}] [--image-opts] [-q] [-f @var{fmt}] @var{filename} @var{dirtybitmap} +ETEXI + STEXI @end table ETEXI diff --git a/qemu-img.c b/qemu-img.c index b12f4cd19b..fdafb4a131 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -835,6 +835,125 @@ fail: return ret; } +/* + * Remove a named dirty bitmap in image. + * This command should be used with no other QEMU process + * open the image at the same time. Otherwise it may be + * croputed the bitmap even the image. + */ +static int img_removebmp(int argc, char **argv) +{ + int c, ret; + const char *filename, *fmt, *bitmapname; + bool quiet = false; + BlockBackend *blk; + BlockDriverState *bs; + BdrvDirtyBitmap *bitmap; + Error *local_err = NULL; + bool image_opts = false; + fmt = NULL; + + for (;;) { + int option_index = 0; + static const struct option long_options[] = { + {"help", no_argument, 0, 'h'}, + {"format", required_argument, 0, 'f'}, + {"object", required_argument, 0, OPTION_OBJECT}, + {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, + {0, 0, 0, 0} + }; + c = getopt_long(argc, argv, ":hf:q", + long_options, &option_index); + if (c == -1) { + break; + } + switch (c) { + case ':': + missing_argument(argv[optind - 1]); + break; + case '?': + unrecognized_option(argv[optind - 1]); + break; + case 'h': + help(); + break; + case 'f': + fmt = optarg; + break; + case 'q': + quiet = true; + break; + case OPTION_OBJECT: { + QemuOpts *opts; + opts = qemu_opts_parse_noisily(&qemu_object_opts, + optarg, true); + if (!opts) { + return 1; + } + } break; + case OPTION_IMAGE_OPTS: + image_opts = true; + break; + } + } + + if (optind != argc - 2) { + error_exit("Expecting two parameters"); + } + filename = argv[optind++]; + bitmapname = argv[optind++]; + + if (qemu_opts_foreach(&qemu_object_opts, + user_creatable_add_opts_foreach, + NULL, NULL)) { + return 1; + } + + blk = img_open(image_opts, filename, fmt, + BDRV_O_RDWR, false, quiet, false); + if (!blk) { + ret = -1; + goto out; + } + bs = blk_bs(blk); + if (!bs) { + ret = -1; + goto out; + } + + bitmap = bdrv_find_dirty_bitmap(bs, bitmapname); + + /* + * Dirty bitmap may not be load if the 'IN_USE' flag is set (e.g. the + * qemu thread is corrupted and the 'IN_USE' flag is not be cleared), + * so the result of bdrv_find_dirty_bitmap is null. In this case, + * we delete bitmap in qcow2 file directly. + */ + if (!bitmap) { + bdrv_remove_persistent_dirty_bitmap(bs, bitmapname, &local_err); + if (local_err != NULL) { + ret = -1; + goto out; + } + } else { + if (bdrv_dirty_bitmap_get_persistance(bitmap)) { + bdrv_remove_persistent_dirty_bitmap(bs, bitmapname, &local_err); + if (local_err != NULL) { + ret = -1; + goto out; + } + } + bdrv_release_dirty_bitmap(bs, bitmap); + } + +out: + blk_unref(blk); + if (ret) { + return 1; + } + return 0; +} + typedef struct CommonBlockJobCBInfo { BlockDriverState *bs; Error **errp;