From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1LuoeD-0001CY-Nb for qemu-devel@nongnu.org; Fri, 17 Apr 2009 10:02:29 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Luoe8-00018j-9S for qemu-devel@nongnu.org; Fri, 17 Apr 2009 10:02:28 -0400 Received: from [199.232.76.173] (port=47598 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Luoe7-00018G-HB for qemu-devel@nongnu.org; Fri, 17 Apr 2009 10:02:23 -0400 Received: from mo-p05-ob.rzone.de ([81.169.146.181]:44048) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1Luoe6-0005os-SB for qemu-devel@nongnu.org; Fri, 17 Apr 2009 10:02:23 -0400 From: kwolf@redhat.com Date: Fri, 17 Apr 2009 16:01:58 +0200 Message-Id: <1239976920-4912-3-git-send-email-kwolf@redhat.com> In-Reply-To: <1239969879-5611-1-git-send-email-kwolf@redhat.com> References: <1239969879-5611-1-git-send-email-kwolf@redhat.com> Subject: [Qemu-devel] [PATCH 3/5] Introduce qemu-img check subcommand Reply-To: qemu-devel@nongnu.org List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Kevin Wolf From: Kevin Wolf Now that block drivers can provide check functions, expose them through qemu-img. Signed-off-by: Kevin Wolf --- qemu-img.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 62 insertions(+), 0 deletions(-) diff --git a/qemu-img.c b/qemu-img.c index ccf4a6f..29149a2 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -58,6 +58,7 @@ static void help(void) "QEMU disk image utility\n" "\n" "Command syntax:\n" + " check [-f fmt] filename\n" " create [-e] [-6] [-F fmt] [-b base_image] [-f fmt] filename [size]\n" " commit [-f fmt] filename\n" " convert [-c] [-e] [-6] [-f fmt] [-O output_fmt] [-B output_base_image] filename [filename2 [...]] output_filename\n" @@ -315,6 +316,65 @@ static int img_create(int argc, char **argv) return 0; } +static int img_check(int argc, char **argv) +{ + int c, ret; + const char *filename, *fmt; + BlockDriver *drv; + BlockDriverState *bs; + + fmt = NULL; + for(;;) { + c = getopt(argc, argv, "f:h"); + if (c == -1) + break; + switch(c) { + case 'h': + help(); + break; + case 'f': + fmt = optarg; + break; + } + } + if (optind >= argc) + help(); + filename = argv[optind++]; + + bs = bdrv_new(""); + if (!bs) + error("Not enough memory"); + if (fmt) { + drv = bdrv_find_format(fmt); + if (!drv) + error("Unknown file format '%s'", fmt); + } else { + drv = NULL; + } + if (bdrv_open2(bs, filename, BRDV_O_FLAGS, drv) < 0) { + error("Could not open '%s'", filename); + } + ret = bdrv_check(bs); + switch(ret) { + case 0: + printf("No errors were found on the image.\n"); + break; + case -ENOTSUP: + error("This image format does not support checks"); + break; + default: + if (ret < 0) { + error("An error occurred during the check"); + } else { + printf("%d errors were found on the image.\n", ret); + } + break; + } + + bdrv_delete(bs); + return 0; +} + static int img_commit(int argc, char **argv) { int c, ret; @@ -888,6 +948,8 @@ int main(int argc, char **argv) argc--; argv++; if (!strcmp(cmd, "create")) { img_create(argc, argv); + } else if (!strcmp(cmd, "check")) { + img_check(argc, argv); } else if (!strcmp(cmd, "commit")) { img_commit(argc, argv); } else if (!strcmp(cmd, "convert")) { -- 1.6.0.6