From patchwork Mon Dec 17 13:39:51 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miroslav Rezanina X-Patchwork-Id: 206882 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id E422B2C0092 for ; Tue, 18 Dec 2012 01:33:40 +1100 (EST) Received: from localhost ([::1]:60100 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TkbVl-0008UT-OF for incoming@patchwork.ozlabs.org; Mon, 17 Dec 2012 09:17:41 -0500 Received: from eggs.gnu.org ([208.118.235.92]:45311) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TkbV9-0006oW-VR for qemu-devel@nongnu.org; Mon, 17 Dec 2012 09:17:07 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TkbV2-0007Xv-EI for qemu-devel@nongnu.org; Mon, 17 Dec 2012 09:17:03 -0500 Received: from 212-4-128-36.cust.selfnet.cz ([212.4.128.36]:46974 helo=lws-ntb.brq.redhat.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TkbV0-0007T3-Cg for qemu-devel@nongnu.org; Mon, 17 Dec 2012 09:16:56 -0500 Received: from lws-ntb.brq.redhat.com (localhost [127.0.0.1]) by lws-ntb.brq.redhat.com (8.14.5/8.14.5) with ESMTP id qBHDeMQo031588; Mon, 17 Dec 2012 14:40:24 +0100 Received: (from root@localhost) by lws-ntb.brq.redhat.com (8.14.5/8.14.5/Submit) id qBHDeJVn031587; Mon, 17 Dec 2012 14:40:19 +0100 From: mrezanin@redhat.com To: qemu-devel@nongnu.org Date: Mon, 17 Dec 2012 14:39:51 +0100 Message-Id: <1355751593-31535-3-git-send-email-mrezanin@redhat.com> X-Mailer: git-send-email 1.7.11.7 In-Reply-To: <1355751593-31535-2-git-send-email-mrezanin@redhat.com> References: <1355751593-31535-1-git-send-email-mrezanin@redhat.com> <1355751593-31535-2-git-send-email-mrezanin@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 212.4.128.36 Cc: kwolf@redhat.com, pbonzini@redhat.com, Miroslav Rezanina , stefanha@redhat.com Subject: [Qemu-devel] [PATCH v7 2/4] qemu-img: Add "Quiet mode" option 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 From: Miroslav Rezanina There can be need to turn output to stdout off. This patch adds a -q option that enable "Quiet mode". In Quiet mode, only errors are printed out. Signed-off-by: Miroslav Rezanina --- block.c | 11 +++--- block.h | 2 +- blockdev.c | 6 ++-- qemu-img-cmds.hx | 28 +++++++-------- qemu-img.c | 108 +++++++++++++++++++++++++++++++++++++++++-------------- qemu-img.texi | 3 ++ 6 files changed, 108 insertions(+), 50 deletions(-) diff --git a/block.c b/block.c index 24c06ab..5450ff9 100644 --- a/block.c +++ b/block.c @@ -4449,7 +4449,7 @@ bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) int bdrv_img_create(const char *filename, const char *fmt, const char *base_filename, const char *base_fmt, - char *options, uint64_t img_size, int flags) + char *options, uint64_t img_size, int flags, bool quiet) { QEMUOptionParameter *param = NULL, *create_options = NULL; QEMUOptionParameter *backing_fmt, *backing_file, *size; @@ -4565,10 +4565,11 @@ int bdrv_img_create(const char *filename, const char *fmt, } } - printf("Formatting '%s', fmt=%s ", filename, fmt); - print_option_parameters(param); - puts(""); - + if (!quiet) { + printf("Formatting '%s', fmt=%s ", filename, fmt); + print_option_parameters(param); + puts(""); + } ret = bdrv_create(drv, filename, param); if (ret < 0) { diff --git a/block.h b/block.h index 2cb8d71..a7e7220 100644 --- a/block.h +++ b/block.h @@ -347,7 +347,7 @@ int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf, int bdrv_img_create(const char *filename, const char *fmt, const char *base_filename, const char *base_fmt, - char *options, uint64_t img_size, int flags); + char *options, uint64_t img_size, int flags, bool quiet); void bdrv_set_buffer_alignment(BlockDriverState *bs, int align); void *qemu_blockalign(BlockDriverState *bs, size_t size); diff --git a/blockdev.c b/blockdev.c index e73fd6e..b2fb7f7 100644 --- a/blockdev.c +++ b/blockdev.c @@ -789,7 +789,7 @@ void qmp_transaction(BlockdevActionList *dev_list, Error **errp) ret = bdrv_img_create(new_image_file, format, states->old_bs->filename, states->old_bs->drv->format_name, - NULL, -1, flags); + NULL, -1, flags, false); if (ret) { error_set(errp, QERR_OPEN_FILE_FAILED, new_image_file); goto delete_and_fail; @@ -1264,7 +1264,7 @@ void qmp_drive_mirror(const char *device, const char *target, bdrv_get_geometry(bs, &size); size *= 512; ret = bdrv_img_create(target, format, - NULL, NULL, NULL, size, flags); + NULL, NULL, NULL, size, flags, false); } else { switch (mode) { case NEW_IMAGE_MODE_EXISTING: @@ -1275,7 +1275,7 @@ void qmp_drive_mirror(const char *device, const char *target, ret = bdrv_img_create(target, format, source->filename, source->drv->format_name, - NULL, -1, flags); + NULL, -1, flags,false); break; default: abort(); diff --git a/qemu-img-cmds.hx b/qemu-img-cmds.hx index a181363..90b93e0 100644 --- a/qemu-img-cmds.hx +++ b/qemu-img-cmds.hx @@ -10,27 +10,27 @@ STEXI ETEXI DEF("check", img_check, - "check [-f fmt] [-r [leaks | all]] filename") + "check [-q] [-f fmt] [-r [leaks | all]] filename") STEXI -@item check [-f @var{fmt}] [-r [leaks | all]] @var{filename} +@item check [-q] [-f @var{fmt}] [-r [leaks | all]] @var{filename} ETEXI DEF("create", img_create, - "create [-f fmt] [-o options] filename [size]") + "create [-q] [-f fmt] [-o options] filename [size]") STEXI -@item create [-f @var{fmt}] [-o @var{options}] @var{filename} [@var{size}] +@item create [-q] [-f @var{fmt}] [-o @var{options}] @var{filename} [@var{size}] ETEXI DEF("commit", img_commit, - "commit [-f fmt] [-t cache] filename") + "commit [-q] [-f fmt] [-t cache] filename") STEXI -@item commit [-f @var{fmt}] [-t @var{cache}] @var{filename} +@item commit [-q] [-f @var{fmt}] [-t @var{cache}] @var{filename} ETEXI DEF("convert", img_convert, - "convert [-c] [-p] [-f fmt] [-t cache] [-O output_fmt] [-o options] [-s snapshot_name] [-S sparse_size] filename [filename2 [...]] output_filename") + "convert [-c] [-p] [-q] [-f fmt] [-t cache] [-O output_fmt] [-o options] [-s snapshot_name] [-S sparse_size] filename [filename2 [...]] output_filename") STEXI -@item convert [-c] [-p] [-f @var{fmt}] [-t @var{cache}] [-O @var{output_fmt}] [-o @var{options}] [-s @var{snapshot_name}] [-S @var{sparse_size}] @var{filename} [@var{filename2} [...]] @var{output_filename} +@item convert [-c] [-p] [-q] [-f @var{fmt}] [-t @var{cache}] [-O @var{output_fmt}] [-o @var{options}] [-s @var{snapshot_name}] [-S @var{sparse_size}] @var{filename} [@var{filename2} [...]] @var{output_filename} ETEXI DEF("info", img_info, @@ -40,20 +40,20 @@ STEXI ETEXI DEF("snapshot", img_snapshot, - "snapshot [-l | -a snapshot | -c snapshot | -d snapshot] filename") + "snapshot [-q] [-l | -a snapshot | -c snapshot | -d snapshot] filename") STEXI -@item snapshot [-l | -a @var{snapshot} | -c @var{snapshot} | -d @var{snapshot}] @var{filename} +@item snapshot [-q] [-l | -a @var{snapshot} | -c @var{snapshot} | -d @var{snapshot}] @var{filename} ETEXI DEF("rebase", img_rebase, - "rebase [-f fmt] [-t cache] [-p] [-u] -b backing_file [-F backing_fmt] filename") + "rebase [-q] [-f fmt] [-t cache] [-p] [-u] -b backing_file [-F backing_fmt] filename") STEXI -@item rebase [-f @var{fmt}] [-t @var{cache}] [-p] [-u] -b @var{backing_file} [-F @var{backing_fmt}] @var{filename} +@item rebase [-q] [-f @var{fmt}] [-t @var{cache}] [-p] [-u] -b @var{backing_file} [-F @var{backing_fmt}] @var{filename} ETEXI DEF("resize", img_resize, - "resize filename [+ | -]size") + "resize [-q] filename [+ | -]size") STEXI -@item resize @var{filename} [+ | -]@var{size} +@item resize [-q] @var{filename} [+ | -]@var{size} @end table ETEXI diff --git a/qemu-img.c b/qemu-img.c index e29e01b..b9a45f1 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -32,6 +32,7 @@ #include "block_int.h" #include #include +#include #ifdef _WIN32 #include @@ -86,6 +87,7 @@ static void help(void) " rebasing in this case (useful for renaming the backing file)\n" " '-h' with or without a command shows this help and lists the supported formats\n" " '-p' show progress of command (only certain commands)\n" + " '-q' Quiet mode - do not print any output (except errors)\n" " '-S' indicates the consecutive number of bytes that must contain only zeros\n" " for qemu-img to create a sparse image during conversion\n" " '--output' takes the format in which the output must be done (human or json)\n" @@ -109,6 +111,18 @@ static void help(void) exit(1); } +static int qprintf(bool quiet, const char* fmt, ...) +{ + int ret = 0; + if (!quiet) { + va_list args; + va_start(args, fmt); + ret = vprintf(fmt, args); + va_end(args); + } + return ret; +} + #if defined(WIN32) /* XXX: put correct support for win32 */ static int read_password(char *buf, int buf_size) @@ -227,7 +241,8 @@ static int print_block_option_help(const char *filename, const char *fmt) static BlockDriverState *bdrv_new_open(const char *filename, const char *fmt, int flags, - bool require_io) + bool require_io, + bool quiet) { BlockDriverState *bs; BlockDriver *drv; @@ -253,7 +268,7 @@ static BlockDriverState *bdrv_new_open(const char *filename, } if (bdrv_is_encrypted(bs) && require_io) { - printf("Disk image '%s' is encrypted.\n", filename); + qprintf(quiet, "Disk image '%s' is encrypted.\n", filename); if (read_password(password, sizeof(password)) < 0) { error_report("No password given"); goto fail; @@ -301,9 +316,10 @@ static int img_create(int argc, char **argv) const char *filename; const char *base_filename = NULL; char *options = NULL; + bool quiet = false; for(;;) { - c = getopt(argc, argv, "F:b:f:he6o:"); + c = getopt(argc, argv, "F:b:f:he6o:q"); if (c == -1) { break; } @@ -332,6 +348,9 @@ static int img_create(int argc, char **argv) case 'o': options = optarg; break; + case 'q': + quiet = true; + break; } } @@ -362,7 +381,7 @@ static int img_create(int argc, char **argv) } ret = bdrv_img_create(filename, fmt, base_filename, base_fmt, - options, img_size, BDRV_O_FLAGS); + options, img_size, BDRV_O_FLAGS, quiet); out: if (ret) { return 1; @@ -386,10 +405,11 @@ static int img_check(int argc, char **argv) BdrvCheckResult result; int fix = 0; int flags = BDRV_O_FLAGS | BDRV_O_CHECK; + bool quiet = false; fmt = NULL; for(;;) { - c = getopt(argc, argv, "f:hr:"); + c = getopt(argc, argv, "f:hr:q"); if (c == -1) { break; } @@ -412,6 +432,9 @@ static int img_check(int argc, char **argv) help(); } break; + case 'q': + quiet = true; + break; } } if (optind >= argc) { @@ -419,7 +442,7 @@ static int img_check(int argc, char **argv) } filename = argv[optind++]; - bs = bdrv_new_open(filename, fmt, flags, true); + bs = bdrv_new_open(filename, fmt, flags, true, quiet); if (!bs) { return 1; } @@ -432,7 +455,8 @@ static int img_check(int argc, char **argv) } if (result.corruptions_fixed || result.leaks_fixed) { - printf("The following inconsistencies were found and repaired:\n\n" + qprintf(quiet, + "The following inconsistencies were found and repaired:\n\n" " %d leaked clusters\n" " %d corruptions\n\n" "Double checking the fixed image now...\n", @@ -442,29 +466,31 @@ static int img_check(int argc, char **argv) } if (!(result.corruptions || result.leaks || result.check_errors)) { - printf("No errors were found on the image.\n"); + qprintf(quiet, "No errors were found on the image.\n"); } else { if (result.corruptions) { - printf("\n%d errors were found on the image.\n" + qprintf(quiet, "\n%d errors were found on the image.\n" "Data may be corrupted, or further writes to the image " "may corrupt it.\n", result.corruptions); } if (result.leaks) { - printf("\n%d leaked clusters were found on the image.\n" + qprintf(quiet, "\n%d leaked clusters were found on the image.\n" "This means waste of disk space, but no harm to data.\n", result.leaks); } if (result.check_errors) { - printf("\n%d internal errors have occurred during the check.\n", + qprintf(quiet, + "\n%d internal errors have occurred during the check.\n", result.check_errors); } } if (result.bfi.total_clusters != 0 && result.bfi.allocated_clusters != 0) { - printf("%" PRId64 "/%" PRId64 "= %0.2f%% allocated, %0.2f%% fragmented\n", + qprintf(quiet, + "%" PRId64 "/%" PRId64 "= %0.2f%% allocated, %0.2f%% fragmented\n", result.bfi.allocated_clusters, result.bfi.total_clusters, result.bfi.allocated_clusters * 100.0 / result.bfi.total_clusters, result.bfi.fragmented_clusters * 100.0 / result.bfi.allocated_clusters); @@ -473,7 +499,7 @@ static int img_check(int argc, char **argv) bdrv_delete(bs); if (ret < 0 || result.check_errors) { - printf("\nAn error has occurred during the check: %s\n" + qprintf(quiet, "\nAn error has occurred during the check: %s\n" "The check is not complete and may have missed error.\n", strerror(-ret)); return 1; @@ -493,11 +519,12 @@ static int img_commit(int argc, char **argv) int c, ret, flags; const char *filename, *fmt, *cache; BlockDriverState *bs; + bool quiet = false; fmt = NULL; cache = BDRV_DEFAULT_CACHE; for(;;) { - c = getopt(argc, argv, "f:ht:"); + c = getopt(argc, argv, "f:ht:q"); if (c == -1) { break; } @@ -512,6 +539,9 @@ static int img_commit(int argc, char **argv) case 't': cache = optarg; break; + case 'q': + quiet = true; + break; } } if (optind >= argc) { @@ -526,14 +556,14 @@ static int img_commit(int argc, char **argv) return -1; } - bs = bdrv_new_open(filename, fmt, flags, true); + bs = bdrv_new_open(filename, fmt, flags, true, quiet); if (!bs) { return 1; } ret = bdrv_commit(bs); switch(ret) { case 0: - printf("Image committed.\n"); + qprintf(quiet, "Image committed.\n"); break; case -ENOENT: error_report("No disk inserted"); @@ -676,6 +706,7 @@ static int img_convert(int argc, char **argv) const char *snapshot_name = NULL; float local_progress = 0; int min_sparse = 8; /* Need at least 4k of zeros for sparse detection */ + bool quiet = false; fmt = NULL; out_fmt = "raw"; @@ -683,7 +714,7 @@ static int img_convert(int argc, char **argv) out_baseimg = NULL; compress = 0; for(;;) { - c = getopt(argc, argv, "f:O:B:s:hce6o:pS:t:"); + c = getopt(argc, argv, "f:O:B:s:hce6o:pS:t:q"); if (c == -1) { break; } @@ -737,9 +768,16 @@ static int img_convert(int argc, char **argv) case 't': cache = optarg; break; + case 'q': + quiet = true; + break; } } + if (quiet) { + progress = 0; + } + bs_n = argc - optind - 1; if (bs_n < 1) { help(); @@ -768,7 +806,7 @@ static int img_convert(int argc, char **argv) total_sectors = 0; for (bs_i = 0; bs_i < bs_n; bs_i++) { - bs[bs_i] = bdrv_new_open(argv[optind + bs_i], fmt, BDRV_O_FLAGS, true); + bs[bs_i] = bdrv_new_open(argv[optind + bs_i], fmt, BDRV_O_FLAGS, true, quiet); if (!bs[bs_i]) { error_report("Could not open '%s'", argv[optind + bs_i]); ret = -1; @@ -887,7 +925,7 @@ static int img_convert(int argc, char **argv) return -1; } - out_bs = bdrv_new_open(out_filename, out_fmt, flags, true); + out_bs = bdrv_new_open(out_filename, out_fmt, flags, true, quiet); if (!out_bs) { ret = -1; goto out; @@ -1350,7 +1388,7 @@ static ImageInfoList *collect_image_info_list(const char *filename, g_hash_table_insert(filenames, (gpointer)filename, NULL); bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_NO_BACKING, - false); + false, false); if (!bs) { goto err; } @@ -1486,11 +1524,12 @@ static int img_snapshot(int argc, char **argv) int c, ret = 0, bdrv_oflags; int action = 0; qemu_timeval tv; + bool quiet = false; bdrv_oflags = BDRV_O_FLAGS | BDRV_O_RDWR; /* Parse commandline parameters */ for(;;) { - c = getopt(argc, argv, "la:c:d:h"); + c = getopt(argc, argv, "la:c:d:hq"); if (c == -1) { break; } @@ -1531,6 +1570,9 @@ static int img_snapshot(int argc, char **argv) action = SNAPSHOT_DELETE; snapshot_name = optarg; break; + case 'q': + quiet = true; + break; } } @@ -1540,7 +1582,7 @@ static int img_snapshot(int argc, char **argv) filename = argv[optind++]; /* Open the image */ - bs = bdrv_new_open(filename, NULL, bdrv_oflags, true); + bs = bdrv_new_open(filename, NULL, bdrv_oflags, true, quiet); if (!bs) { return 1; } @@ -1600,6 +1642,7 @@ static int img_rebase(int argc, char **argv) int c, flags, ret; int unsafe = 0; int progress = 0; + bool quiet = false; /* Parse commandline parameters */ fmt = NULL; @@ -1607,7 +1650,7 @@ static int img_rebase(int argc, char **argv) out_baseimg = NULL; out_basefmt = NULL; for(;;) { - c = getopt(argc, argv, "uhf:F:b:pt:"); + c = getopt(argc, argv, "uhf:F:b:pt:q"); if (c == -1) { break; } @@ -1634,9 +1677,16 @@ static int img_rebase(int argc, char **argv) case 't': cache = optarg; break; + case 'q': + quiet = true; + break; } } + if (quiet) { + progress = 0; + } + if ((optind >= argc) || (!unsafe && !out_baseimg)) { help(); } @@ -1658,7 +1708,7 @@ static int img_rebase(int argc, char **argv) * Ignore the old backing file for unsafe rebase in case we want to correct * the reference to a renamed or moved backing file. */ - bs = bdrv_new_open(filename, fmt, flags, true); + bs = bdrv_new_open(filename, fmt, flags, true, quiet); if (!bs) { return 1; } @@ -1870,6 +1920,7 @@ static int img_resize(int argc, char **argv) int c, ret, relative; const char *filename, *fmt, *size; int64_t n, total_size; + bool quiet = false; BlockDriverState *bs = NULL; QemuOpts *param; static QemuOptsList resize_options = { @@ -1898,7 +1949,7 @@ static int img_resize(int argc, char **argv) /* Parse getopt arguments */ fmt = NULL; for(;;) { - c = getopt(argc, argv, "f:h"); + c = getopt(argc, argv, "f:hq"); if (c == -1) { break; } @@ -1910,6 +1961,9 @@ static int img_resize(int argc, char **argv) case 'f': fmt = optarg; break; + case 'q': + quiet = true; + break; } } if (optind >= argc) { @@ -1943,7 +1997,7 @@ static int img_resize(int argc, char **argv) n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0); qemu_opts_del(param); - bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR, true); + bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR, true, quiet); if (!bs) { ret = -1; goto out; @@ -1963,7 +2017,7 @@ static int img_resize(int argc, char **argv) ret = bdrv_truncate(bs, total_size); switch (ret) { case 0: - printf("Image resized.\n"); + qprintf(quiet, "Image resized.\n"); break; case -ENOTSUP: error_report("This image does not support resize"); diff --git a/qemu-img.texi b/qemu-img.texi index 00fca8d..bb82a3d 100644 --- a/qemu-img.texi +++ b/qemu-img.texi @@ -54,6 +54,9 @@ indicates that target image must be compressed (qcow format only) with or without a command shows help and lists the supported formats @item -p display progress bar (convert and rebase commands only) +@item -q +Quiet mode - do not print any output (except errors). There's no progres bar +in case both @var{-q} and @var{-p} options are used. @item -S @var{size} indicates the consecutive number of bytes that must contain only zeros for qemu-img to create a sparse image during conversion. This value is rounded