From f67503e5bd8997ea7ec3f4bfa0af0e06321771a6 Mon Sep 17 00:00:00 2001 From: Max Reitz Date: Tue, 18 Feb 2014 18:33:05 +0100 Subject: block: Change BDS parameter of bdrv_open() to ** Make bdrv_open() take a pointer to a BDS pointer, similarly to bdrv_file_open(). If a pointer to a NULL pointer is given, bdrv_open() will create a new BDS with an empty name; if the BDS pointer is not NULL, that existing BDS will be reused (in the same way as bdrv_open() already did). Signed-off-by: Max Reitz Reviewed-by: Kevin Wolf Signed-off-by: Kevin Wolf --- qemu-img.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'qemu-img.c') diff --git a/qemu-img.c b/qemu-img.c index 0927b090de..59b101347c 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -289,7 +289,7 @@ static BlockDriverState *bdrv_new_open(const char *filename, drv = NULL; } - ret = bdrv_open(bs, filename, NULL, flags, drv, &local_err); + ret = bdrv_open(&bs, filename, NULL, flags, drv, &local_err); if (ret < 0) { error_report("Could not open '%s': %s", filename, error_get_pretty(local_err)); @@ -310,9 +310,7 @@ static BlockDriverState *bdrv_new_open(const char *filename, } return bs; fail: - if (bs) { - bdrv_unref(bs); - } + bdrv_unref(bs); return NULL; } @@ -2314,7 +2312,7 @@ static int img_rebase(int argc, char **argv) bs_old_backing = bdrv_new("old_backing"); bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name)); - ret = bdrv_open(bs_old_backing, backing_name, NULL, BDRV_O_FLAGS, + ret = bdrv_open(&bs_old_backing, backing_name, NULL, BDRV_O_FLAGS, old_backing_drv, &local_err); if (ret) { error_report("Could not open old backing file '%s': %s", @@ -2324,7 +2322,7 @@ static int img_rebase(int argc, char **argv) } if (out_baseimg[0]) { bs_new_backing = bdrv_new("new_backing"); - ret = bdrv_open(bs_new_backing, out_baseimg, NULL, BDRV_O_FLAGS, + ret = bdrv_open(&bs_new_backing, out_baseimg, NULL, BDRV_O_FLAGS, new_backing_drv, &local_err); if (ret) { error_report("Could not open new backing file '%s': %s", -- cgit v1.2.1 From ddf5636dc9e4be894f2ab4a5f803d915478b5099 Mon Sep 17 00:00:00 2001 From: Max Reitz Date: Tue, 18 Feb 2014 18:33:06 +0100 Subject: block: Add reference parameter to bdrv_open() Allow bdrv_open() to handle references to existing block devices just as bdrv_file_open() is already capable of. Signed-off-by: Max Reitz Signed-off-by: Kevin Wolf --- qemu-img.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'qemu-img.c') diff --git a/qemu-img.c b/qemu-img.c index 59b101347c..79ab3e8cbe 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -289,7 +289,7 @@ static BlockDriverState *bdrv_new_open(const char *filename, drv = NULL; } - ret = bdrv_open(&bs, filename, NULL, flags, drv, &local_err); + ret = bdrv_open(&bs, filename, NULL, NULL, flags, drv, &local_err); if (ret < 0) { error_report("Could not open '%s': %s", filename, error_get_pretty(local_err)); @@ -2312,7 +2312,7 @@ static int img_rebase(int argc, char **argv) bs_old_backing = bdrv_new("old_backing"); bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name)); - ret = bdrv_open(&bs_old_backing, backing_name, NULL, BDRV_O_FLAGS, + ret = bdrv_open(&bs_old_backing, backing_name, NULL, NULL, BDRV_O_FLAGS, old_backing_drv, &local_err); if (ret) { error_report("Could not open old backing file '%s': %s", @@ -2322,8 +2322,8 @@ static int img_rebase(int argc, char **argv) } if (out_baseimg[0]) { bs_new_backing = bdrv_new("new_backing"); - ret = bdrv_open(&bs_new_backing, out_baseimg, NULL, BDRV_O_FLAGS, - new_backing_drv, &local_err); + ret = bdrv_open(&bs_new_backing, out_baseimg, NULL, NULL, + BDRV_O_FLAGS, new_backing_drv, &local_err); if (ret) { error_report("Could not open new backing file '%s': %s", out_baseimg, error_get_pretty(local_err)); -- cgit v1.2.1 From 77386bf6ebe67164a2d102b207fb3bc11af8c1e8 Mon Sep 17 00:00:00 2001 From: Kevin Wolf Date: Fri, 21 Feb 2014 16:24:04 +0100 Subject: qemu-img create: Support multiple -o options If you specified multiple -o options for qemu-img create, it would silently ignore all but the last one. This patch fixes the problem. Now multiple -o options has the same meaning as having a single option with all settings in the order of their respective -o options. Signed-off-by: Kevin Wolf Reviewed-by: Jeff Cody Reviewed-by: Eric Blake --- qemu-img.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) (limited to 'qemu-img.c') diff --git a/qemu-img.c b/qemu-img.c index 79ab3e8cbe..9c1643d0c1 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -369,13 +369,23 @@ static int img_create(int argc, char **argv) case 'e': error_report("option -e is deprecated, please use \'-o " "encryption\' instead!"); - return 1; + goto fail; case '6': error_report("option -6 is deprecated, please use \'-o " "compat6\' instead!"); - return 1; + goto fail; case 'o': - options = optarg; + if (!is_valid_option_list(optarg)) { + error_report("Invalid option list: %s", optarg); + goto fail; + } + if (!options) { + options = g_strdup(optarg); + } else { + char *old_options = options; + options = g_strdup_printf("%s,%s", options, optarg); + g_free(old_options); + } break; case 'q': quiet = true; @@ -403,7 +413,7 @@ static int img_create(int argc, char **argv) error_report("kilobytes, megabytes, gigabytes, terabytes, " "petabytes and exabytes."); } - return 1; + goto fail; } img_size = (uint64_t)sval; } @@ -411,7 +421,8 @@ static int img_create(int argc, char **argv) help(); } - if (options && is_help_option(options)) { + if (options && has_help_option(options)) { + g_free(options); return print_block_option_help(filename, fmt); } @@ -420,10 +431,15 @@ static int img_create(int argc, char **argv) if (local_err) { error_report("%s: %s", filename, error_get_pretty(local_err)); error_free(local_err); - return 1; + goto fail; } + g_free(options); return 0; + +fail: + g_free(options); + return 1; } static void dump_json_image_check(ImageCheck *check, bool quiet) -- cgit v1.2.1 From 2dc8328b4c6aba60f4ad543186f4e8aec2e9287e Mon Sep 17 00:00:00 2001 From: Kevin Wolf Date: Fri, 21 Feb 2014 16:24:05 +0100 Subject: qemu-img convert: Support multiple -o options Instead of ignoring all option values but the last one, multiple -o options now have the same meaning as having a single option with all settings in the order of their respective -o options. Signed-off-by: Kevin Wolf Reviewed-by: Jeff Cody Reviewed-by: Eric Blake --- qemu-img.c | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) (limited to 'qemu-img.c') diff --git a/qemu-img.c b/qemu-img.c index 9c1643d0c1..3fd2168679 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -1164,6 +1164,9 @@ static int img_convert(int argc, char **argv) Error *local_err = NULL; QemuOpts *sn_opts = NULL; + /* Initialize before goto out */ + qemu_progress_init(progress, 1.0); + fmt = NULL; out_fmt = "raw"; cache = "unsafe"; @@ -1195,13 +1198,26 @@ static int img_convert(int argc, char **argv) case 'e': error_report("option -e is deprecated, please use \'-o " "encryption\' instead!"); - return 1; + ret = -1; + goto out; case '6': error_report("option -6 is deprecated, please use \'-o " "compat6\' instead!"); - return 1; + ret = -1; + goto out; case 'o': - options = optarg; + if (!is_valid_option_list(optarg)) { + error_report("Invalid option list: %s", optarg); + ret = -1; + goto out; + } + if (!options) { + options = g_strdup(optarg); + } else { + char *old_options = options; + options = g_strdup_printf("%s,%s", options, optarg); + g_free(old_options); + } break; case 's': snapshot_name = optarg; @@ -1212,7 +1228,8 @@ static int img_convert(int argc, char **argv) if (!sn_opts) { error_report("Failed in parsing snapshot param '%s'", optarg); - return 1; + ret = -1; + goto out; } } else { snapshot_name = optarg; @@ -1225,7 +1242,8 @@ static int img_convert(int argc, char **argv) sval = strtosz_suffix(optarg, &end, STRTOSZ_DEFSUFFIX_B); if (sval < 0 || *end) { error_report("Invalid minimum zero buffer size for sparse output specified"); - return 1; + ret = -1; + goto out; } min_sparse = sval / BDRV_SECTOR_SIZE; @@ -1257,10 +1275,7 @@ static int img_convert(int argc, char **argv) out_filename = argv[argc - 1]; - /* Initialize before goto out */ - qemu_progress_init(progress, 1.0); - - if (options && is_help_option(options)) { + if (options && has_help_option(options)) { ret = print_block_option_help(out_filename, out_fmt); goto out; } @@ -1653,6 +1668,7 @@ out: free_option_parameters(create_options); free_option_parameters(param); qemu_vfree(buf); + g_free(options); if (sn_opts) { qemu_opts_del(sn_opts); } -- cgit v1.2.1 From 626f84f39d4ae365a44dbbc0d0dd3c7739c3971a Mon Sep 17 00:00:00 2001 From: Kevin Wolf Date: Fri, 21 Feb 2014 16:24:06 +0100 Subject: qemu-img amend: Support multiple -o options Instead of ignoring all option values but the last one, multiple -o options now have the same meaning as having a single option with all settings in the order of their respective -o options. Signed-off-by: Kevin Wolf Reviewed-by: Jeff Cody Reviewed-by: Eric Blake --- qemu-img.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'qemu-img.c') diff --git a/qemu-img.c b/qemu-img.c index 3fd2168679..6ceaeb244a 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -2667,7 +2667,18 @@ static int img_amend(int argc, char **argv) help(); break; case 'o': - options = optarg; + if (!is_valid_option_list(optarg)) { + error_report("Invalid option list: %s", optarg); + ret = -1; + goto out; + } + if (!options) { + options = g_strdup(optarg); + } else { + char *old_options = options; + options = g_strdup_printf("%s,%s", options, optarg); + g_free(old_options); + } break; case 'f': fmt = optarg; @@ -2697,7 +2708,7 @@ static int img_amend(int argc, char **argv) fmt = bs->drv->format_name; - if (is_help_option(options)) { + if (has_help_option(options)) { ret = print_block_option_help(filename, fmt); goto out; } @@ -2724,6 +2735,8 @@ out: } free_option_parameters(create_options); free_option_parameters(options_param); + g_free(options); + if (ret) { return 1; } -- cgit v1.2.1 From a283cb6e58fca846c658360971d23fdd1129db65 Mon Sep 17 00:00:00 2001 From: Kevin Wolf Date: Fri, 21 Feb 2014 16:24:07 +0100 Subject: qemu-img: Allow -o help with incomplete argument list This patch allows using 'qemu-img $subcmd -o help' for the create, convert and amend subcommands, without specifying the previously required filename arguments. Note that it's still allowed and meaningful to specify a filename: An invocation like 'qemu-img create -o help sheepdog:foo' will also display options that are provided by the Sheepdog driver. Signed-off-by: Kevin Wolf Reviewed-by: Jeff Cody Reviewed-by: Eric Blake --- qemu-img.c | 58 +++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 23 deletions(-) (limited to 'qemu-img.c') diff --git a/qemu-img.c b/qemu-img.c index 6ceaeb244a..5512f5bfe4 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -250,16 +250,19 @@ static int print_block_option_help(const char *filename, const char *fmt) return 1; } - proto_drv = bdrv_find_protocol(filename, true); - if (!proto_drv) { - error_report("Unknown protocol '%s'", filename); - return 1; - } - create_options = append_option_parameters(create_options, drv->create_options); - create_options = append_option_parameters(create_options, - proto_drv->create_options); + + if (filename) { + proto_drv = bdrv_find_protocol(filename, true); + if (!proto_drv) { + error_report("Unknown protocol '%s'", filename); + return 1; + } + create_options = append_option_parameters(create_options, + proto_drv->create_options); + } + print_option_help(create_options); free_option_parameters(create_options); return 0; @@ -394,10 +397,16 @@ static int img_create(int argc, char **argv) } /* Get the filename */ + filename = (optind < argc) ? argv[optind] : NULL; + if (options && has_help_option(options)) { + g_free(options); + return print_block_option_help(filename, fmt); + } + if (optind >= argc) { help(); } - filename = argv[optind++]; + optind++; /* Get image size, if specified */ if (optind < argc) { @@ -421,11 +430,6 @@ static int img_create(int argc, char **argv) help(); } - if (options && has_help_option(options)) { - g_free(options); - return print_block_option_help(filename, fmt); - } - bdrv_img_create(filename, fmt, base_filename, base_fmt, options, img_size, BDRV_O_FLAGS, &local_err, quiet); if (local_err) { @@ -1269,17 +1273,18 @@ static int img_convert(int argc, char **argv) } bs_n = argc - optind - 1; - if (bs_n < 1) { - help(); - } - - out_filename = argv[argc - 1]; + out_filename = bs_n >= 1 ? argv[argc - 1] : NULL; if (options && has_help_option(options)) { ret = print_block_option_help(out_filename, out_fmt); goto out; } + if (bs_n < 1) { + help(); + } + + if (bs_n > 1 && out_baseimg) { error_report("-B makes no sense when concatenating multiple input " "images"); @@ -2689,15 +2694,21 @@ static int img_amend(int argc, char **argv) } } - if (optind != argc - 1) { + if (!options) { help(); } - if (!options) { - help(); + filename = (optind == argc - 1) ? argv[argc - 1] : NULL; + if (fmt && has_help_option(options)) { + /* If a format is explicitly specified (and possibly no filename is + * given), print option help here */ + ret = print_block_option_help(filename, fmt); + goto out; } - filename = argv[argc - 1]; + if (optind != argc - 1) { + help(); + } bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR, true, quiet); if (!bs) { @@ -2709,6 +2720,7 @@ static int img_amend(int argc, char **argv) fmt = bs->drv->format_name; if (has_help_option(options)) { + /* If the format was auto-detected, print option help here */ ret = print_block_option_help(filename, fmt); goto out; } -- cgit v1.2.1