#include "block/block_int.h"
#include "block/qapi.h"
#include <getopt.h>
-#include <stdio.h>
-#include <stdarg.h>
+#include <glib.h>
-#ifdef _WIN32
-#include <windows.h>
-#endif
+#define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION \
+ ", Copyright (c) 2004-2008 Fabrice Bellard\n"
typedef struct img_cmd_t {
const char *name;
#define BDRV_O_FLAGS BDRV_O_CACHE_WB
#define BDRV_DEFAULT_CACHE "writeback"
-static void format_print(void *opaque, const char *name)
+static gint compare_data(gconstpointer a, gconstpointer b, gpointer user)
+{
+ return g_strcmp0(a, b);
+}
+
+static void print_format(gpointer data, gpointer user)
+{
+ printf(" %s", (char *)data);
+}
+
+static void add_format_to_seq(void *opaque, const char *fmt_name)
+{
+ GSequence *seq = opaque;
+
+ g_sequence_insert_sorted(seq, (gpointer)fmt_name,
+ compare_data, NULL);
+}
+
+static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
{
- printf(" %s", name);
+ va_list ap;
+
+ error_printf("qemu-img: ");
+
+ va_start(ap, fmt);
+ error_vprintf(fmt, ap);
+ va_end(ap);
+
+ error_printf("\nTry 'qemu-img --help' for more information\n");
+ exit(EXIT_FAILURE);
}
/* Please keep in synch with qemu-img.texi */
-static void help(void)
+static void QEMU_NORETURN help(void)
{
const char *help_msg =
- "qemu-img version " QEMU_VERSION ", Copyright (c) 2004-2008 Fabrice Bellard\n"
+ QEMU_IMG_VERSION
"usage: qemu-img command [command options]\n"
"QEMU disk image utility\n"
"\n"
" '-f' first image format\n"
" '-F' second image format\n"
" '-s' run in Strict mode - fail on different image size or sector allocation\n";
+ GSequence *seq;
printf("%s\nSupported formats:", help_msg);
- bdrv_iterate_format(format_print, NULL);
+ seq = g_sequence_new(NULL);
+ bdrv_iterate_format(add_format_to_seq, seq);
+ g_sequence_foreach(seq, print_format, NULL);
printf("\n");
- exit(1);
+ g_sequence_free(seq);
+
+ exit(EXIT_SUCCESS);
}
static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, 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;
}
-static BlockDriverState *bdrv_new_open(const char *filename,
+static BlockDriverState *bdrv_new_open(const char *id,
+ const char *filename,
const char *fmt,
int flags,
bool require_io,
Error *local_err = NULL;
int ret;
- bs = bdrv_new("image");
+ bs = bdrv_new(id, &error_abort);
if (fmt) {
drv = bdrv_find_format(fmt);
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));
}
return bs;
fail:
- if (bs) {
- bdrv_unref(bs);
- }
+ bdrv_unref(bs);
return NULL;
}
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;
}
/* 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();
+ error_exit("Expecting image file name");
}
- filename = argv[optind++];
+ optind++;
/* Get image size, if specified */
if (optind < argc) {
error_report("kilobytes, megabytes, gigabytes, terabytes, "
"petabytes and exabytes.");
}
- return 1;
+ goto fail;
}
img_size = (uint64_t)sval;
}
if (optind != argc) {
- help();
- }
-
- if (options && is_help_option(options)) {
- return print_block_option_help(filename, fmt);
+ error_exit("Unexpected argument: %s", argv[optind]);
}
bdrv_img_create(filename, fmt, base_filename, base_fmt,
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)
{
- Error *errp = NULL;
+ Error *local_err = NULL;
QString *str;
QmpOutputVisitor *ov = qmp_output_visitor_new();
QObject *obj;
visit_type_ImageCheck(qmp_output_get_visitor(ov),
- &check, NULL, &errp);
+ &check, NULL, &local_err);
obj = qmp_output_get_qobject(ov);
str = qobject_to_json_pretty(obj);
assert(str != NULL);
static const struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"format", required_argument, 0, 'f'},
- {"repair", no_argument, 0, 'r'},
+ {"repair", required_argument, 0, 'r'},
{"output", required_argument, 0, OPTION_OUTPUT},
{0, 0, 0, 0}
};
} else if (!strcmp(optarg, "all")) {
fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
} else {
- help();
+ error_exit("Unknown option value for -r "
+ "(expecting 'leaks' or 'all'): %s", optarg);
}
break;
case OPTION_OUTPUT:
}
}
if (optind != argc - 1) {
- help();
+ error_exit("Expecting one image file name");
}
filename = argv[optind++];
return 1;
}
- bs = bdrv_new_open(filename, fmt, flags, true, quiet);
+ bs = bdrv_new_open("image", filename, fmt, flags, true, quiet);
if (!bs) {
return 1;
}
}
}
if (optind != argc - 1) {
- help();
+ error_exit("Expecting one image file name");
}
filename = argv[optind++];
return -1;
}
- bs = bdrv_new_open(filename, fmt, flags, true, quiet);
+ bs = bdrv_new_open("image", filename, fmt, flags, true, quiet);
if (!bs) {
return 1;
}
if (optind != argc - 2) {
- help();
+ error_exit("Expecting two image file names");
}
filename1 = argv[optind++];
filename2 = argv[optind++];
/* Initialize before goto out */
qemu_progress_init(progress, 2.0);
- bs1 = bdrv_new_open(filename1, fmt1, BDRV_O_FLAGS, true, quiet);
+ bs1 = bdrv_new_open("image 1", filename1, fmt1, BDRV_O_FLAGS, true, quiet);
if (!bs1) {
error_report("Can't open file %s", filename1);
ret = 2;
goto out3;
}
- bs2 = bdrv_new_open(filename2, fmt2, BDRV_O_FLAGS, true, quiet);
+ bs2 = bdrv_new_open("image 2", filename2, fmt2, BDRV_O_FLAGS, true, quiet);
if (!bs2) {
error_report("Can't open file %s", filename2);
ret = 2;
case 'e':
error_report("option -e is deprecated, please use \'-o "
"encryption\' instead!");
- return 1;
+ ret = -1;
+ goto fail_getopt;
case '6':
error_report("option -6 is deprecated, please use \'-o "
"compat6\' instead!");
- return 1;
+ ret = -1;
+ goto fail_getopt;
case 'o':
- options = optarg;
+ if (!is_valid_option_list(optarg)) {
+ error_report("Invalid option list: %s", optarg);
+ ret = -1;
+ goto fail_getopt;
+ }
+ 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;
if (!sn_opts) {
error_report("Failed in parsing snapshot param '%s'",
optarg);
- return 1;
+ ret = -1;
+ goto fail_getopt;
}
} else {
snapshot_name = optarg;
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 fail_getopt;
}
min_sparse = sval / BDRV_SECTOR_SIZE;
}
}
+ /* Initialize before goto out */
if (quiet) {
progress = 0;
}
+ qemu_progress_init(progress, 1.0);
- bs_n = argc - optind - 1;
- if (bs_n < 1) {
- help();
- }
-
- out_filename = argv[argc - 1];
- /* Initialize before goto out */
- qemu_progress_init(progress, 1.0);
+ bs_n = argc - optind - 1;
+ out_filename = bs_n >= 1 ? argv[argc - 1] : NULL;
- if (options && is_help_option(options)) {
+ if (options && has_help_option(options)) {
ret = print_block_option_help(out_filename, out_fmt);
goto out;
}
+ if (bs_n < 1) {
+ error_exit("Must specify image file name");
+ }
+
+
if (bs_n > 1 && out_baseimg) {
error_report("-B makes no sense when concatenating multiple input "
"images");
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,
- quiet);
+ char *id = bs_n > 1 ? g_strdup_printf("source %d", bs_i)
+ : g_strdup("source");
+ bs[bs_i] = bdrv_new_open(id, argv[optind + bs_i], fmt, BDRV_O_FLAGS,
+ true, quiet);
+ g_free(id);
if (!bs[bs_i]) {
error_report("Could not open '%s'", argv[optind + bs_i]);
ret = -1;
return -1;
}
- out_bs = bdrv_new_open(out_filename, out_fmt, flags, true, quiet);
+ out_bs = bdrv_new_open("target", out_filename, out_fmt, flags, true, quiet);
if (!out_bs) {
ret = -1;
goto out;
goto out;
}
} else {
+ compress = compress || bdi.needs_compressed_writes;
cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
}
}
g_free(bs);
}
+fail_getopt:
+ g_free(options);
+
if (ret) {
return 1;
}
static void dump_json_image_info_list(ImageInfoList *list)
{
- Error *errp = NULL;
+ Error *local_err = NULL;
QString *str;
QmpOutputVisitor *ov = qmp_output_visitor_new();
QObject *obj;
visit_type_ImageInfoList(qmp_output_get_visitor(ov),
- &list, NULL, &errp);
+ &list, NULL, &local_err);
obj = qmp_output_get_qobject(ov);
str = qobject_to_json_pretty(obj);
assert(str != NULL);
static void dump_json_image_info(ImageInfo *info)
{
- Error *errp = NULL;
+ Error *local_err = NULL;
QString *str;
QmpOutputVisitor *ov = qmp_output_visitor_new();
QObject *obj;
visit_type_ImageInfo(qmp_output_get_visitor(ov),
- &info, NULL, &errp);
+ &info, NULL, &local_err);
obj = qmp_output_get_qobject(ov);
str = qobject_to_json_pretty(obj);
assert(str != NULL);
}
g_hash_table_insert(filenames, (gpointer)filename, NULL);
- bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_NO_BACKING,
- false, false);
+ bs = bdrv_new_open("image", filename, fmt,
+ BDRV_O_FLAGS | BDRV_O_NO_BACKING, false, false);
if (!bs) {
goto err;
}
if (err) {
error_report("%s", error_get_pretty(err));
error_free(err);
+ bdrv_unref(bs);
goto err;
}
}
}
if (optind != argc - 1) {
- help();
+ error_exit("Expecting one image file name");
}
filename = argv[optind++];
break;
}
}
- if (optind >= argc) {
- help();
+ if (optind != argc - 1) {
+ error_exit("Expecting one image file name");
}
- filename = argv[optind++];
+ filename = argv[optind];
if (output && !strcmp(output, "json")) {
output_format = OFORMAT_JSON;
return 1;
}
- bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS, true, false);
+ bs = bdrv_new_open("image", filename, fmt, BDRV_O_FLAGS, true, false);
if (!bs) {
return 1;
}
return 0;
case 'l':
if (action) {
- help();
+ error_exit("Cannot mix '-l', '-a', '-c', '-d'");
return 0;
}
action = SNAPSHOT_LIST;
break;
case 'a':
if (action) {
- help();
+ error_exit("Cannot mix '-l', '-a', '-c', '-d'");
return 0;
}
action = SNAPSHOT_APPLY;
break;
case 'c':
if (action) {
- help();
+ error_exit("Cannot mix '-l', '-a', '-c', '-d'");
return 0;
}
action = SNAPSHOT_CREATE;
break;
case 'd':
if (action) {
- help();
+ error_exit("Cannot mix '-l', '-a', '-c', '-d'");
return 0;
}
action = SNAPSHOT_DELETE;
}
if (optind != argc - 1) {
- help();
+ error_exit("Expecting one image file name");
}
filename = argv[optind++];
/* Open the image */
- bs = bdrv_new_open(filename, NULL, bdrv_oflags, true, quiet);
+ bs = bdrv_new_open("image", filename, NULL, bdrv_oflags, true, quiet);
if (!bs) {
return 1;
}
progress = 0;
}
- if ((optind != argc - 1) || (!unsafe && !out_baseimg)) {
- help();
+ if (optind != argc - 1) {
+ error_exit("Expecting one image file name");
+ }
+ if (!unsafe && !out_baseimg) {
+ error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
}
filename = argv[optind++];
* 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, quiet);
+ bs = bdrv_new_open("image", filename, fmt, flags, true, quiet);
if (!bs) {
return 1;
}
} else {
char backing_name[1024];
- bs_old_backing = bdrv_new("old_backing");
+ bs_old_backing = bdrv_new("old_backing", &error_abort);
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",
goto out;
}
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);
+ bs_new_backing = bdrv_new("new_backing", &error_abort);
+ 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));
/* Remove size from argv manually so that negative numbers are not treated
* as options by getopt. */
if (argc < 3) {
- help();
+ error_exit("Not enough arguments");
return 1;
}
}
}
if (optind != argc - 1) {
- help();
+ error_exit("Expecting one image file name");
}
filename = argv[optind++];
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, quiet);
+ bs = bdrv_new_open("image", filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR,
+ true, quiet);
if (!bs) {
ret = -1;
goto out;
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;
}
}
- if (optind != argc - 1) {
- help();
+ if (!options) {
+ error_exit("Must specify options (-o)");
}
- 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) {
+ error_exit("Expecting one image file name");
+ }
- bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR, true, quiet);
+ bs = bdrv_new_open("image", filename, fmt,
+ BDRV_O_FLAGS | BDRV_O_RDWR, true, quiet);
if (!bs) {
error_report("Could not open image '%s'", filename);
ret = -1;
fmt = bs->drv->format_name;
- if (is_help_option(options)) {
+ if (has_help_option(options)) {
+ /* If the format was auto-detected, print option help here */
ret = print_block_option_help(filename, fmt);
goto out;
}
}
free_option_parameters(create_options);
free_option_parameters(options_param);
+ g_free(options);
+
if (ret) {
return 1;
}
{
const img_cmd_t *cmd;
const char *cmdname;
+ int c;
+ static const struct option long_options[] = {
+ {"help", no_argument, 0, 'h'},
+ {"version", no_argument, 0, 'v'},
+ {0, 0, 0, 0}
+ };
#ifdef CONFIG_POSIX
signal(SIGPIPE, SIG_IGN);
#endif
error_set_progname(argv[0]);
+ qemu_init_exec_dir(argv[0]);
qemu_init_main_loop();
bdrv_init();
- if (argc < 2)
- help();
+ if (argc < 2) {
+ error_exit("Not enough arguments");
+ }
cmdname = argv[1];
- argc--; argv++;
/* find the command */
- for(cmd = img_cmds; cmd->name != NULL; cmd++) {
+ for (cmd = img_cmds; cmd->name != NULL; cmd++) {
if (!strcmp(cmdname, cmd->name)) {
- return cmd->handler(argc, argv);
+ return cmd->handler(argc - 1, argv + 1);
}
}
+ c = getopt_long(argc, argv, "h", long_options, NULL);
+
+ if (c == 'h') {
+ help();
+ }
+ if (c == 'v') {
+ printf(QEMU_IMG_VERSION);
+ return 0;
+ }
+
/* not found */
- help();
- return 0;
+ error_exit("Command not found: %s", cmdname);
}