xref: /qemu/qemu-img.c (revision fc81fa1e)
1 /*
2  * QEMU disk image utility
3  *
4  * Copyright (c) 2003-2008 Fabrice Bellard
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #include "qemu/osdep.h"
26 #include <getopt.h>
27 
28 #include "qemu-version.h"
29 #include "qapi/error.h"
30 #include "qapi-visit.h"
31 #include "qapi/qobject-output-visitor.h"
32 #include "qapi/qmp/qjson.h"
33 #include "qapi/qmp/qbool.h"
34 #include "qapi/qmp/qdict.h"
35 #include "qapi/qmp/qstring.h"
36 #include "qemu/cutils.h"
37 #include "qemu/config-file.h"
38 #include "qemu/option.h"
39 #include "qemu/error-report.h"
40 #include "qemu/log.h"
41 #include "qom/object_interfaces.h"
42 #include "sysemu/sysemu.h"
43 #include "sysemu/block-backend.h"
44 #include "block/block_int.h"
45 #include "block/blockjob.h"
46 #include "block/qapi.h"
47 #include "crypto/init.h"
48 #include "trace/control.h"
49 
50 #define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION QEMU_PKGVERSION \
51                           "\n" QEMU_COPYRIGHT "\n"
52 
53 typedef struct img_cmd_t {
54     const char *name;
55     int (*handler)(int argc, char **argv);
56 } img_cmd_t;
57 
58 enum {
59     OPTION_OUTPUT = 256,
60     OPTION_BACKING_CHAIN = 257,
61     OPTION_OBJECT = 258,
62     OPTION_IMAGE_OPTS = 259,
63     OPTION_PATTERN = 260,
64     OPTION_FLUSH_INTERVAL = 261,
65     OPTION_NO_DRAIN = 262,
66     OPTION_TARGET_IMAGE_OPTS = 263,
67     OPTION_SIZE = 264,
68     OPTION_PREALLOCATION = 265,
69     OPTION_SHRINK = 266,
70 };
71 
72 typedef enum OutputFormat {
73     OFORMAT_JSON,
74     OFORMAT_HUMAN,
75 } OutputFormat;
76 
77 /* Default to cache=writeback as data integrity is not important for qemu-img */
78 #define BDRV_DEFAULT_CACHE "writeback"
79 
80 static void format_print(void *opaque, const char *name)
81 {
82     printf(" %s", name);
83 }
84 
85 static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
86 {
87     va_list ap;
88 
89     error_printf("qemu-img: ");
90 
91     va_start(ap, fmt);
92     error_vprintf(fmt, ap);
93     va_end(ap);
94 
95     error_printf("\nTry 'qemu-img --help' for more information\n");
96     exit(EXIT_FAILURE);
97 }
98 
99 static void QEMU_NORETURN missing_argument(const char *option)
100 {
101     error_exit("missing argument for option '%s'", option);
102 }
103 
104 static void QEMU_NORETURN unrecognized_option(const char *option)
105 {
106     error_exit("unrecognized option '%s'", option);
107 }
108 
109 /* Please keep in synch with qemu-img.texi */
110 static void QEMU_NORETURN help(void)
111 {
112     const char *help_msg =
113            QEMU_IMG_VERSION
114            "usage: qemu-img [standard options] command [command options]\n"
115            "QEMU disk image utility\n"
116            "\n"
117            "    '-h', '--help'       display this help and exit\n"
118            "    '-V', '--version'    output version information and exit\n"
119            "    '-T', '--trace'      [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
120            "                         specify tracing options\n"
121            "\n"
122            "Command syntax:\n"
123 #define DEF(option, callback, arg_string)        \
124            "  " arg_string "\n"
125 #include "qemu-img-cmds.h"
126 #undef DEF
127 #undef GEN_DOCS
128            "\n"
129            "Command parameters:\n"
130            "  'filename' is a disk image filename\n"
131            "  'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
132            "    manual page for a description of the object properties. The most common\n"
133            "    object type is a 'secret', which is used to supply passwords and/or\n"
134            "    encryption keys.\n"
135            "  'fmt' is the disk image format. It is guessed automatically in most cases\n"
136            "  'cache' is the cache mode used to write the output disk image, the valid\n"
137            "    options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
138            "    'directsync' and 'unsafe' (default for convert)\n"
139            "  'src_cache' is the cache mode used to read input disk images, the valid\n"
140            "    options are the same as for the 'cache' option\n"
141            "  'size' is the disk image size in bytes. Optional suffixes\n"
142            "    'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
143            "    'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P)  are\n"
144            "    supported. 'b' is ignored.\n"
145            "  'output_filename' is the destination disk image filename\n"
146            "  'output_fmt' is the destination format\n"
147            "  'options' is a comma separated list of format specific options in a\n"
148            "    name=value format. Use -o ? for an overview of the options supported by the\n"
149            "    used format\n"
150            "  'snapshot_param' is param used for internal snapshot, format\n"
151            "    is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
152            "    '[ID_OR_NAME]'\n"
153            "  'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n"
154            "    instead\n"
155            "  '-c' indicates that target image must be compressed (qcow format only)\n"
156            "  '-u' allows unsafe backing chains. For rebasing, it is assumed that old and\n"
157            "       new backing file match exactly. The image doesn't need a working\n"
158            "       backing file before rebasing in this case (useful for renaming the\n"
159            "       backing file). For image creation, allow creating without attempting\n"
160            "       to open the backing file.\n"
161            "  '-h' with or without a command shows this help and lists the supported formats\n"
162            "  '-p' show progress of command (only certain commands)\n"
163            "  '-q' use Quiet mode - do not print any output (except errors)\n"
164            "  '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
165            "       contain only zeros for qemu-img to create a sparse image during\n"
166            "       conversion. If the number of bytes is 0, the source will not be scanned for\n"
167            "       unallocated or zero sectors, and the destination image will always be\n"
168            "       fully allocated\n"
169            "  '--output' takes the format in which the output must be done (human or json)\n"
170            "  '-n' skips the target volume creation (useful if the volume is created\n"
171            "       prior to running qemu-img)\n"
172            "\n"
173            "Parameters to check subcommand:\n"
174            "  '-r' tries to repair any inconsistencies that are found during the check.\n"
175            "       '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
176            "       kinds of errors, with a higher risk of choosing the wrong fix or\n"
177            "       hiding corruption that has already occurred.\n"
178            "\n"
179            "Parameters to convert subcommand:\n"
180            "  '-m' specifies how many coroutines work in parallel during the convert\n"
181            "       process (defaults to 8)\n"
182            "  '-W' allow to write to the target out of order rather than sequential\n"
183            "\n"
184            "Parameters to snapshot subcommand:\n"
185            "  'snapshot' is the name of the snapshot to create, apply or delete\n"
186            "  '-a' applies a snapshot (revert disk to saved state)\n"
187            "  '-c' creates a snapshot\n"
188            "  '-d' deletes a snapshot\n"
189            "  '-l' lists all snapshots in the given image\n"
190            "\n"
191            "Parameters to compare subcommand:\n"
192            "  '-f' first image format\n"
193            "  '-F' second image format\n"
194            "  '-s' run in Strict mode - fail on different image size or sector allocation\n"
195            "\n"
196            "Parameters to dd subcommand:\n"
197            "  'bs=BYTES' read and write up to BYTES bytes at a time "
198            "(default: 512)\n"
199            "  'count=N' copy only N input blocks\n"
200            "  'if=FILE' read from FILE\n"
201            "  'of=FILE' write to FILE\n"
202            "  'skip=N' skip N bs-sized blocks at the start of input\n";
203 
204     printf("%s\nSupported formats:", help_msg);
205     bdrv_iterate_format(format_print, NULL);
206     printf("\n\n" QEMU_HELP_BOTTOM "\n");
207     exit(EXIT_SUCCESS);
208 }
209 
210 static QemuOptsList qemu_object_opts = {
211     .name = "object",
212     .implied_opt_name = "qom-type",
213     .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
214     .desc = {
215         { }
216     },
217 };
218 
219 static QemuOptsList qemu_source_opts = {
220     .name = "source",
221     .implied_opt_name = "file",
222     .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head),
223     .desc = {
224         { }
225     },
226 };
227 
228 static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
229 {
230     int ret = 0;
231     if (!quiet) {
232         va_list args;
233         va_start(args, fmt);
234         ret = vprintf(fmt, args);
235         va_end(args);
236     }
237     return ret;
238 }
239 
240 
241 static int print_block_option_help(const char *filename, const char *fmt)
242 {
243     BlockDriver *drv, *proto_drv;
244     QemuOptsList *create_opts = NULL;
245     Error *local_err = NULL;
246 
247     /* Find driver and parse its options */
248     drv = bdrv_find_format(fmt);
249     if (!drv) {
250         error_report("Unknown file format '%s'", fmt);
251         return 1;
252     }
253 
254     create_opts = qemu_opts_append(create_opts, drv->create_opts);
255     if (filename) {
256         proto_drv = bdrv_find_protocol(filename, true, &local_err);
257         if (!proto_drv) {
258             error_report_err(local_err);
259             qemu_opts_free(create_opts);
260             return 1;
261         }
262         create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
263     }
264 
265     qemu_opts_print_help(create_opts);
266     qemu_opts_free(create_opts);
267     return 0;
268 }
269 
270 
271 static BlockBackend *img_open_opts(const char *optstr,
272                                    QemuOpts *opts, int flags, bool writethrough,
273                                    bool quiet, bool force_share)
274 {
275     QDict *options;
276     Error *local_err = NULL;
277     BlockBackend *blk;
278     options = qemu_opts_to_qdict(opts, NULL);
279     if (force_share) {
280         if (qdict_haskey(options, BDRV_OPT_FORCE_SHARE)
281             && !qdict_get_bool(options, BDRV_OPT_FORCE_SHARE)) {
282             error_report("--force-share/-U conflicts with image options");
283             QDECREF(options);
284             return NULL;
285         }
286         qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
287     }
288     blk = blk_new_open(NULL, NULL, options, flags, &local_err);
289     if (!blk) {
290         error_reportf_err(local_err, "Could not open '%s': ", optstr);
291         return NULL;
292     }
293     blk_set_enable_write_cache(blk, !writethrough);
294 
295     return blk;
296 }
297 
298 static BlockBackend *img_open_file(const char *filename,
299                                    QDict *options,
300                                    const char *fmt, int flags,
301                                    bool writethrough, bool quiet,
302                                    bool force_share)
303 {
304     BlockBackend *blk;
305     Error *local_err = NULL;
306 
307     if (!options) {
308         options = qdict_new();
309     }
310     if (fmt) {
311         qdict_put_str(options, "driver", fmt);
312     }
313 
314     if (force_share) {
315         qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
316     }
317     blk = blk_new_open(filename, NULL, options, flags, &local_err);
318     if (!blk) {
319         error_reportf_err(local_err, "Could not open '%s': ", filename);
320         return NULL;
321     }
322     blk_set_enable_write_cache(blk, !writethrough);
323 
324     return blk;
325 }
326 
327 
328 static int img_add_key_secrets(void *opaque,
329                                const char *name, const char *value,
330                                Error **errp)
331 {
332     QDict *options = opaque;
333 
334     if (g_str_has_suffix(name, "key-secret")) {
335         qdict_put_str(options, name, value);
336     }
337 
338     return 0;
339 }
340 
341 static BlockBackend *img_open_new_file(const char *filename,
342                                        QemuOpts *create_opts,
343                                        const char *fmt, int flags,
344                                        bool writethrough, bool quiet,
345                                        bool force_share)
346 {
347     QDict *options = NULL;
348 
349     options = qdict_new();
350     qemu_opt_foreach(create_opts, img_add_key_secrets, options, &error_abort);
351 
352     return img_open_file(filename, options, fmt, flags, writethrough, quiet,
353                          force_share);
354 }
355 
356 
357 static BlockBackend *img_open(bool image_opts,
358                               const char *filename,
359                               const char *fmt, int flags, bool writethrough,
360                               bool quiet, bool force_share)
361 {
362     BlockBackend *blk;
363     if (image_opts) {
364         QemuOpts *opts;
365         if (fmt) {
366             error_report("--image-opts and --format are mutually exclusive");
367             return NULL;
368         }
369         opts = qemu_opts_parse_noisily(qemu_find_opts("source"),
370                                        filename, true);
371         if (!opts) {
372             return NULL;
373         }
374         blk = img_open_opts(filename, opts, flags, writethrough, quiet,
375                             force_share);
376     } else {
377         blk = img_open_file(filename, NULL, fmt, flags, writethrough, quiet,
378                             force_share);
379     }
380     return blk;
381 }
382 
383 
384 static int add_old_style_options(const char *fmt, QemuOpts *opts,
385                                  const char *base_filename,
386                                  const char *base_fmt)
387 {
388     Error *err = NULL;
389 
390     if (base_filename) {
391         qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err);
392         if (err) {
393             error_report("Backing file not supported for file format '%s'",
394                          fmt);
395             error_free(err);
396             return -1;
397         }
398     }
399     if (base_fmt) {
400         qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err);
401         if (err) {
402             error_report("Backing file format not supported for file "
403                          "format '%s'", fmt);
404             error_free(err);
405             return -1;
406         }
407     }
408     return 0;
409 }
410 
411 static int64_t cvtnum(const char *s)
412 {
413     int err;
414     uint64_t value;
415 
416     err = qemu_strtosz(s, NULL, &value);
417     if (err < 0) {
418         return err;
419     }
420     if (value > INT64_MAX) {
421         return -ERANGE;
422     }
423     return value;
424 }
425 
426 static int img_create(int argc, char **argv)
427 {
428     int c;
429     uint64_t img_size = -1;
430     const char *fmt = "raw";
431     const char *base_fmt = NULL;
432     const char *filename;
433     const char *base_filename = NULL;
434     char *options = NULL;
435     Error *local_err = NULL;
436     bool quiet = false;
437     int flags = 0;
438 
439     for(;;) {
440         static const struct option long_options[] = {
441             {"help", no_argument, 0, 'h'},
442             {"object", required_argument, 0, OPTION_OBJECT},
443             {0, 0, 0, 0}
444         };
445         c = getopt_long(argc, argv, ":F:b:f:ho:qu",
446                         long_options, NULL);
447         if (c == -1) {
448             break;
449         }
450         switch(c) {
451         case ':':
452             missing_argument(argv[optind - 1]);
453             break;
454         case '?':
455             unrecognized_option(argv[optind - 1]);
456             break;
457         case 'h':
458             help();
459             break;
460         case 'F':
461             base_fmt = optarg;
462             break;
463         case 'b':
464             base_filename = optarg;
465             break;
466         case 'f':
467             fmt = optarg;
468             break;
469         case 'o':
470             if (!is_valid_option_list(optarg)) {
471                 error_report("Invalid option list: %s", optarg);
472                 goto fail;
473             }
474             if (!options) {
475                 options = g_strdup(optarg);
476             } else {
477                 char *old_options = options;
478                 options = g_strdup_printf("%s,%s", options, optarg);
479                 g_free(old_options);
480             }
481             break;
482         case 'q':
483             quiet = true;
484             break;
485         case 'u':
486             flags |= BDRV_O_NO_BACKING;
487             break;
488         case OPTION_OBJECT: {
489             QemuOpts *opts;
490             opts = qemu_opts_parse_noisily(&qemu_object_opts,
491                                            optarg, true);
492             if (!opts) {
493                 goto fail;
494             }
495         }   break;
496         }
497     }
498 
499     /* Get the filename */
500     filename = (optind < argc) ? argv[optind] : NULL;
501     if (options && has_help_option(options)) {
502         g_free(options);
503         return print_block_option_help(filename, fmt);
504     }
505 
506     if (optind >= argc) {
507         error_exit("Expecting image file name");
508     }
509     optind++;
510 
511     if (qemu_opts_foreach(&qemu_object_opts,
512                           user_creatable_add_opts_foreach,
513                           NULL, NULL)) {
514         goto fail;
515     }
516 
517     /* Get image size, if specified */
518     if (optind < argc) {
519         int64_t sval;
520 
521         sval = cvtnum(argv[optind++]);
522         if (sval < 0) {
523             if (sval == -ERANGE) {
524                 error_report("Image size must be less than 8 EiB!");
525             } else {
526                 error_report("Invalid image size specified! You may use k, M, "
527                       "G, T, P or E suffixes for ");
528                 error_report("kilobytes, megabytes, gigabytes, terabytes, "
529                              "petabytes and exabytes.");
530             }
531             goto fail;
532         }
533         img_size = (uint64_t)sval;
534     }
535     if (optind != argc) {
536         error_exit("Unexpected argument: %s", argv[optind]);
537     }
538 
539     bdrv_img_create(filename, fmt, base_filename, base_fmt,
540                     options, img_size, flags, quiet, &local_err);
541     if (local_err) {
542         error_reportf_err(local_err, "%s: ", filename);
543         goto fail;
544     }
545 
546     g_free(options);
547     return 0;
548 
549 fail:
550     g_free(options);
551     return 1;
552 }
553 
554 static void dump_json_image_check(ImageCheck *check, bool quiet)
555 {
556     QString *str;
557     QObject *obj;
558     Visitor *v = qobject_output_visitor_new(&obj);
559 
560     visit_type_ImageCheck(v, NULL, &check, &error_abort);
561     visit_complete(v, &obj);
562     str = qobject_to_json_pretty(obj);
563     assert(str != NULL);
564     qprintf(quiet, "%s\n", qstring_get_str(str));
565     qobject_decref(obj);
566     visit_free(v);
567     QDECREF(str);
568 }
569 
570 static void dump_human_image_check(ImageCheck *check, bool quiet)
571 {
572     if (!(check->corruptions || check->leaks || check->check_errors)) {
573         qprintf(quiet, "No errors were found on the image.\n");
574     } else {
575         if (check->corruptions) {
576             qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
577                     "Data may be corrupted, or further writes to the image "
578                     "may corrupt it.\n",
579                     check->corruptions);
580         }
581 
582         if (check->leaks) {
583             qprintf(quiet,
584                     "\n%" PRId64 " leaked clusters were found on the image.\n"
585                     "This means waste of disk space, but no harm to data.\n",
586                     check->leaks);
587         }
588 
589         if (check->check_errors) {
590             qprintf(quiet,
591                     "\n%" PRId64
592                     " internal errors have occurred during the check.\n",
593                     check->check_errors);
594         }
595     }
596 
597     if (check->total_clusters != 0 && check->allocated_clusters != 0) {
598         qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
599                 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
600                 check->allocated_clusters, check->total_clusters,
601                 check->allocated_clusters * 100.0 / check->total_clusters,
602                 check->fragmented_clusters * 100.0 / check->allocated_clusters,
603                 check->compressed_clusters * 100.0 /
604                 check->allocated_clusters);
605     }
606 
607     if (check->image_end_offset) {
608         qprintf(quiet,
609                 "Image end offset: %" PRId64 "\n", check->image_end_offset);
610     }
611 }
612 
613 static int collect_image_check(BlockDriverState *bs,
614                    ImageCheck *check,
615                    const char *filename,
616                    const char *fmt,
617                    int fix)
618 {
619     int ret;
620     BdrvCheckResult result;
621 
622     ret = bdrv_check(bs, &result, fix);
623     if (ret < 0) {
624         return ret;
625     }
626 
627     check->filename                 = g_strdup(filename);
628     check->format                   = g_strdup(bdrv_get_format_name(bs));
629     check->check_errors             = result.check_errors;
630     check->corruptions              = result.corruptions;
631     check->has_corruptions          = result.corruptions != 0;
632     check->leaks                    = result.leaks;
633     check->has_leaks                = result.leaks != 0;
634     check->corruptions_fixed        = result.corruptions_fixed;
635     check->has_corruptions_fixed    = result.corruptions != 0;
636     check->leaks_fixed              = result.leaks_fixed;
637     check->has_leaks_fixed          = result.leaks != 0;
638     check->image_end_offset         = result.image_end_offset;
639     check->has_image_end_offset     = result.image_end_offset != 0;
640     check->total_clusters           = result.bfi.total_clusters;
641     check->has_total_clusters       = result.bfi.total_clusters != 0;
642     check->allocated_clusters       = result.bfi.allocated_clusters;
643     check->has_allocated_clusters   = result.bfi.allocated_clusters != 0;
644     check->fragmented_clusters      = result.bfi.fragmented_clusters;
645     check->has_fragmented_clusters  = result.bfi.fragmented_clusters != 0;
646     check->compressed_clusters      = result.bfi.compressed_clusters;
647     check->has_compressed_clusters  = result.bfi.compressed_clusters != 0;
648 
649     return 0;
650 }
651 
652 /*
653  * Checks an image for consistency. Exit codes:
654  *
655  *  0 - Check completed, image is good
656  *  1 - Check not completed because of internal errors
657  *  2 - Check completed, image is corrupted
658  *  3 - Check completed, image has leaked clusters, but is good otherwise
659  * 63 - Checks are not supported by the image format
660  */
661 static int img_check(int argc, char **argv)
662 {
663     int c, ret;
664     OutputFormat output_format = OFORMAT_HUMAN;
665     const char *filename, *fmt, *output, *cache;
666     BlockBackend *blk;
667     BlockDriverState *bs;
668     int fix = 0;
669     int flags = BDRV_O_CHECK;
670     bool writethrough;
671     ImageCheck *check;
672     bool quiet = false;
673     bool image_opts = false;
674     bool force_share = false;
675 
676     fmt = NULL;
677     output = NULL;
678     cache = BDRV_DEFAULT_CACHE;
679 
680     for(;;) {
681         int option_index = 0;
682         static const struct option long_options[] = {
683             {"help", no_argument, 0, 'h'},
684             {"format", required_argument, 0, 'f'},
685             {"repair", required_argument, 0, 'r'},
686             {"output", required_argument, 0, OPTION_OUTPUT},
687             {"object", required_argument, 0, OPTION_OBJECT},
688             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
689             {"force-share", no_argument, 0, 'U'},
690             {0, 0, 0, 0}
691         };
692         c = getopt_long(argc, argv, ":hf:r:T:qU",
693                         long_options, &option_index);
694         if (c == -1) {
695             break;
696         }
697         switch(c) {
698         case ':':
699             missing_argument(argv[optind - 1]);
700             break;
701         case '?':
702             unrecognized_option(argv[optind - 1]);
703             break;
704         case 'h':
705             help();
706             break;
707         case 'f':
708             fmt = optarg;
709             break;
710         case 'r':
711             flags |= BDRV_O_RDWR;
712 
713             if (!strcmp(optarg, "leaks")) {
714                 fix = BDRV_FIX_LEAKS;
715             } else if (!strcmp(optarg, "all")) {
716                 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
717             } else {
718                 error_exit("Unknown option value for -r "
719                            "(expecting 'leaks' or 'all'): %s", optarg);
720             }
721             break;
722         case OPTION_OUTPUT:
723             output = optarg;
724             break;
725         case 'T':
726             cache = optarg;
727             break;
728         case 'q':
729             quiet = true;
730             break;
731         case 'U':
732             force_share = true;
733             break;
734         case OPTION_OBJECT: {
735             QemuOpts *opts;
736             opts = qemu_opts_parse_noisily(&qemu_object_opts,
737                                            optarg, true);
738             if (!opts) {
739                 return 1;
740             }
741         }   break;
742         case OPTION_IMAGE_OPTS:
743             image_opts = true;
744             break;
745         }
746     }
747     if (optind != argc - 1) {
748         error_exit("Expecting one image file name");
749     }
750     filename = argv[optind++];
751 
752     if (output && !strcmp(output, "json")) {
753         output_format = OFORMAT_JSON;
754     } else if (output && !strcmp(output, "human")) {
755         output_format = OFORMAT_HUMAN;
756     } else if (output) {
757         error_report("--output must be used with human or json as argument.");
758         return 1;
759     }
760 
761     if (qemu_opts_foreach(&qemu_object_opts,
762                           user_creatable_add_opts_foreach,
763                           NULL, NULL)) {
764         return 1;
765     }
766 
767     ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
768     if (ret < 0) {
769         error_report("Invalid source cache option: %s", cache);
770         return 1;
771     }
772 
773     blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
774                    force_share);
775     if (!blk) {
776         return 1;
777     }
778     bs = blk_bs(blk);
779 
780     check = g_new0(ImageCheck, 1);
781     ret = collect_image_check(bs, check, filename, fmt, fix);
782 
783     if (ret == -ENOTSUP) {
784         error_report("This image format does not support checks");
785         ret = 63;
786         goto fail;
787     }
788 
789     if (check->corruptions_fixed || check->leaks_fixed) {
790         int corruptions_fixed, leaks_fixed;
791 
792         leaks_fixed         = check->leaks_fixed;
793         corruptions_fixed   = check->corruptions_fixed;
794 
795         if (output_format == OFORMAT_HUMAN) {
796             qprintf(quiet,
797                     "The following inconsistencies were found and repaired:\n\n"
798                     "    %" PRId64 " leaked clusters\n"
799                     "    %" PRId64 " corruptions\n\n"
800                     "Double checking the fixed image now...\n",
801                     check->leaks_fixed,
802                     check->corruptions_fixed);
803         }
804 
805         ret = collect_image_check(bs, check, filename, fmt, 0);
806 
807         check->leaks_fixed          = leaks_fixed;
808         check->corruptions_fixed    = corruptions_fixed;
809     }
810 
811     if (!ret) {
812         switch (output_format) {
813         case OFORMAT_HUMAN:
814             dump_human_image_check(check, quiet);
815             break;
816         case OFORMAT_JSON:
817             dump_json_image_check(check, quiet);
818             break;
819         }
820     }
821 
822     if (ret || check->check_errors) {
823         if (ret) {
824             error_report("Check failed: %s", strerror(-ret));
825         } else {
826             error_report("Check failed");
827         }
828         ret = 1;
829         goto fail;
830     }
831 
832     if (check->corruptions) {
833         ret = 2;
834     } else if (check->leaks) {
835         ret = 3;
836     } else {
837         ret = 0;
838     }
839 
840 fail:
841     qapi_free_ImageCheck(check);
842     blk_unref(blk);
843     return ret;
844 }
845 
846 typedef struct CommonBlockJobCBInfo {
847     BlockDriverState *bs;
848     Error **errp;
849 } CommonBlockJobCBInfo;
850 
851 static void common_block_job_cb(void *opaque, int ret)
852 {
853     CommonBlockJobCBInfo *cbi = opaque;
854 
855     if (ret < 0) {
856         error_setg_errno(cbi->errp, -ret, "Block job failed");
857     }
858 }
859 
860 static void run_block_job(BlockJob *job, Error **errp)
861 {
862     AioContext *aio_context = blk_get_aio_context(job->blk);
863     int ret = 0;
864 
865     aio_context_acquire(aio_context);
866     block_job_ref(job);
867     do {
868         aio_poll(aio_context, true);
869         qemu_progress_print(job->len ?
870                             ((float)job->offset / job->len * 100.f) : 0.0f, 0);
871     } while (!job->ready && !job->completed);
872 
873     if (!job->completed) {
874         ret = block_job_complete_sync(job, errp);
875     } else {
876         ret = job->ret;
877     }
878     block_job_unref(job);
879     aio_context_release(aio_context);
880 
881     /* publish completion progress only when success */
882     if (!ret) {
883         qemu_progress_print(100.f, 0);
884     }
885 }
886 
887 static int img_commit(int argc, char **argv)
888 {
889     int c, ret, flags;
890     const char *filename, *fmt, *cache, *base;
891     BlockBackend *blk;
892     BlockDriverState *bs, *base_bs;
893     BlockJob *job;
894     bool progress = false, quiet = false, drop = false;
895     bool writethrough;
896     Error *local_err = NULL;
897     CommonBlockJobCBInfo cbi;
898     bool image_opts = false;
899     AioContext *aio_context;
900 
901     fmt = NULL;
902     cache = BDRV_DEFAULT_CACHE;
903     base = NULL;
904     for(;;) {
905         static const struct option long_options[] = {
906             {"help", no_argument, 0, 'h'},
907             {"object", required_argument, 0, OPTION_OBJECT},
908             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
909             {0, 0, 0, 0}
910         };
911         c = getopt_long(argc, argv, ":f:ht:b:dpq",
912                         long_options, NULL);
913         if (c == -1) {
914             break;
915         }
916         switch(c) {
917         case ':':
918             missing_argument(argv[optind - 1]);
919             break;
920         case '?':
921             unrecognized_option(argv[optind - 1]);
922             break;
923         case 'h':
924             help();
925             break;
926         case 'f':
927             fmt = optarg;
928             break;
929         case 't':
930             cache = optarg;
931             break;
932         case 'b':
933             base = optarg;
934             /* -b implies -d */
935             drop = true;
936             break;
937         case 'd':
938             drop = true;
939             break;
940         case 'p':
941             progress = true;
942             break;
943         case 'q':
944             quiet = true;
945             break;
946         case OPTION_OBJECT: {
947             QemuOpts *opts;
948             opts = qemu_opts_parse_noisily(&qemu_object_opts,
949                                            optarg, true);
950             if (!opts) {
951                 return 1;
952             }
953         }   break;
954         case OPTION_IMAGE_OPTS:
955             image_opts = true;
956             break;
957         }
958     }
959 
960     /* Progress is not shown in Quiet mode */
961     if (quiet) {
962         progress = false;
963     }
964 
965     if (optind != argc - 1) {
966         error_exit("Expecting one image file name");
967     }
968     filename = argv[optind++];
969 
970     if (qemu_opts_foreach(&qemu_object_opts,
971                           user_creatable_add_opts_foreach,
972                           NULL, NULL)) {
973         return 1;
974     }
975 
976     flags = BDRV_O_RDWR | BDRV_O_UNMAP;
977     ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
978     if (ret < 0) {
979         error_report("Invalid cache option: %s", cache);
980         return 1;
981     }
982 
983     blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
984                    false);
985     if (!blk) {
986         return 1;
987     }
988     bs = blk_bs(blk);
989 
990     qemu_progress_init(progress, 1.f);
991     qemu_progress_print(0.f, 100);
992 
993     if (base) {
994         base_bs = bdrv_find_backing_image(bs, base);
995         if (!base_bs) {
996             error_setg(&local_err,
997                        "Did not find '%s' in the backing chain of '%s'",
998                        base, filename);
999             goto done;
1000         }
1001     } else {
1002         /* This is different from QMP, which by default uses the deepest file in
1003          * the backing chain (i.e., the very base); however, the traditional
1004          * behavior of qemu-img commit is using the immediate backing file. */
1005         base_bs = backing_bs(bs);
1006         if (!base_bs) {
1007             error_setg(&local_err, "Image does not have a backing file");
1008             goto done;
1009         }
1010     }
1011 
1012     cbi = (CommonBlockJobCBInfo){
1013         .errp = &local_err,
1014         .bs   = bs,
1015     };
1016 
1017     aio_context = bdrv_get_aio_context(bs);
1018     aio_context_acquire(aio_context);
1019     commit_active_start("commit", bs, base_bs, BLOCK_JOB_DEFAULT, 0,
1020                         BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb,
1021                         &cbi, false, &local_err);
1022     aio_context_release(aio_context);
1023     if (local_err) {
1024         goto done;
1025     }
1026 
1027     /* When the block job completes, the BlockBackend reference will point to
1028      * the old backing file. In order to avoid that the top image is already
1029      * deleted, so we can still empty it afterwards, increment the reference
1030      * counter here preemptively. */
1031     if (!drop) {
1032         bdrv_ref(bs);
1033     }
1034 
1035     job = block_job_get("commit");
1036     run_block_job(job, &local_err);
1037     if (local_err) {
1038         goto unref_backing;
1039     }
1040 
1041     if (!drop && bs->drv->bdrv_make_empty) {
1042         ret = bs->drv->bdrv_make_empty(bs);
1043         if (ret) {
1044             error_setg_errno(&local_err, -ret, "Could not empty %s",
1045                              filename);
1046             goto unref_backing;
1047         }
1048     }
1049 
1050 unref_backing:
1051     if (!drop) {
1052         bdrv_unref(bs);
1053     }
1054 
1055 done:
1056     qemu_progress_end();
1057 
1058     blk_unref(blk);
1059 
1060     if (local_err) {
1061         error_report_err(local_err);
1062         return 1;
1063     }
1064 
1065     qprintf(quiet, "Image committed.\n");
1066     return 0;
1067 }
1068 
1069 /*
1070  * Returns -1 if 'buf' contains only zeroes, otherwise the byte index
1071  * of the first sector boundary within buf where the sector contains a
1072  * non-zero byte.  This function is robust to a buffer that is not
1073  * sector-aligned.
1074  */
1075 static int64_t find_nonzero(const uint8_t *buf, int64_t n)
1076 {
1077     int64_t i;
1078     int64_t end = QEMU_ALIGN_DOWN(n, BDRV_SECTOR_SIZE);
1079 
1080     for (i = 0; i < end; i += BDRV_SECTOR_SIZE) {
1081         if (!buffer_is_zero(buf + i, BDRV_SECTOR_SIZE)) {
1082             return i;
1083         }
1084     }
1085     if (i < n && !buffer_is_zero(buf + i, n - end)) {
1086         return i;
1087     }
1088     return -1;
1089 }
1090 
1091 /*
1092  * Returns true iff the first sector pointed to by 'buf' contains at least
1093  * a non-NUL byte.
1094  *
1095  * 'pnum' is set to the number of sectors (including and immediately following
1096  * the first one) that are known to be in the same allocated/unallocated state.
1097  */
1098 static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
1099 {
1100     bool is_zero;
1101     int i;
1102 
1103     if (n <= 0) {
1104         *pnum = 0;
1105         return 0;
1106     }
1107     is_zero = buffer_is_zero(buf, 512);
1108     for(i = 1; i < n; i++) {
1109         buf += 512;
1110         if (is_zero != buffer_is_zero(buf, 512)) {
1111             break;
1112         }
1113     }
1114     *pnum = i;
1115     return !is_zero;
1116 }
1117 
1118 /*
1119  * Like is_allocated_sectors, but if the buffer starts with a used sector,
1120  * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1121  * breaking up write requests for only small sparse areas.
1122  */
1123 static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
1124     int min)
1125 {
1126     int ret;
1127     int num_checked, num_used;
1128 
1129     if (n < min) {
1130         min = n;
1131     }
1132 
1133     ret = is_allocated_sectors(buf, n, pnum);
1134     if (!ret) {
1135         return ret;
1136     }
1137 
1138     num_used = *pnum;
1139     buf += BDRV_SECTOR_SIZE * *pnum;
1140     n -= *pnum;
1141     num_checked = num_used;
1142 
1143     while (n > 0) {
1144         ret = is_allocated_sectors(buf, n, pnum);
1145 
1146         buf += BDRV_SECTOR_SIZE * *pnum;
1147         n -= *pnum;
1148         num_checked += *pnum;
1149         if (ret) {
1150             num_used = num_checked;
1151         } else if (*pnum >= min) {
1152             break;
1153         }
1154     }
1155 
1156     *pnum = num_used;
1157     return 1;
1158 }
1159 
1160 /*
1161  * Compares two buffers sector by sector. Returns 0 if the first
1162  * sector of each buffer matches, non-zero otherwise.
1163  *
1164  * pnum is set to the sector-aligned size of the buffer prefix that
1165  * has the same matching status as the first sector.
1166  */
1167 static int compare_buffers(const uint8_t *buf1, const uint8_t *buf2,
1168                            int64_t bytes, int64_t *pnum)
1169 {
1170     bool res;
1171     int64_t i = MIN(bytes, BDRV_SECTOR_SIZE);
1172 
1173     assert(bytes > 0);
1174 
1175     res = !!memcmp(buf1, buf2, i);
1176     while (i < bytes) {
1177         int64_t len = MIN(bytes - i, BDRV_SECTOR_SIZE);
1178 
1179         if (!!memcmp(buf1 + i, buf2 + i, len) != res) {
1180             break;
1181         }
1182         i += len;
1183     }
1184 
1185     *pnum = i;
1186     return res;
1187 }
1188 
1189 #define IO_BUF_SIZE (2 * 1024 * 1024)
1190 
1191 /*
1192  * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1193  *
1194  * Intended for use by 'qemu-img compare': Returns 0 in case sectors are
1195  * filled with 0, 1 if sectors contain non-zero data (this is a comparison
1196  * failure), and 4 on error (the exit status for read errors), after emitting
1197  * an error message.
1198  *
1199  * @param blk:  BlockBackend for the image
1200  * @param offset: Starting offset to check
1201  * @param bytes: Number of bytes to check
1202  * @param filename: Name of disk file we are checking (logging purpose)
1203  * @param buffer: Allocated buffer for storing read data
1204  * @param quiet: Flag for quiet mode
1205  */
1206 static int check_empty_sectors(BlockBackend *blk, int64_t offset,
1207                                int64_t bytes, const char *filename,
1208                                uint8_t *buffer, bool quiet)
1209 {
1210     int ret = 0;
1211     int64_t idx;
1212 
1213     ret = blk_pread(blk, offset, buffer, bytes);
1214     if (ret < 0) {
1215         error_report("Error while reading offset %" PRId64 " of %s: %s",
1216                      offset, filename, strerror(-ret));
1217         return 4;
1218     }
1219     idx = find_nonzero(buffer, bytes);
1220     if (idx >= 0) {
1221         qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1222                 offset + idx);
1223         return 1;
1224     }
1225 
1226     return 0;
1227 }
1228 
1229 /*
1230  * Compares two images. Exit codes:
1231  *
1232  * 0 - Images are identical
1233  * 1 - Images differ
1234  * >1 - Error occurred
1235  */
1236 static int img_compare(int argc, char **argv)
1237 {
1238     const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
1239     BlockBackend *blk1, *blk2;
1240     BlockDriverState *bs1, *bs2;
1241     int64_t total_size1, total_size2;
1242     uint8_t *buf1 = NULL, *buf2 = NULL;
1243     int64_t pnum1, pnum2;
1244     int allocated1, allocated2;
1245     int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1246     bool progress = false, quiet = false, strict = false;
1247     int flags;
1248     bool writethrough;
1249     int64_t total_size;
1250     int64_t offset = 0;
1251     int64_t chunk;
1252     int c;
1253     uint64_t progress_base;
1254     bool image_opts = false;
1255     bool force_share = false;
1256 
1257     cache = BDRV_DEFAULT_CACHE;
1258     for (;;) {
1259         static const struct option long_options[] = {
1260             {"help", no_argument, 0, 'h'},
1261             {"object", required_argument, 0, OPTION_OBJECT},
1262             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
1263             {"force-share", no_argument, 0, 'U'},
1264             {0, 0, 0, 0}
1265         };
1266         c = getopt_long(argc, argv, ":hf:F:T:pqsU",
1267                         long_options, NULL);
1268         if (c == -1) {
1269             break;
1270         }
1271         switch (c) {
1272         case ':':
1273             missing_argument(argv[optind - 1]);
1274             break;
1275         case '?':
1276             unrecognized_option(argv[optind - 1]);
1277             break;
1278         case 'h':
1279             help();
1280             break;
1281         case 'f':
1282             fmt1 = optarg;
1283             break;
1284         case 'F':
1285             fmt2 = optarg;
1286             break;
1287         case 'T':
1288             cache = optarg;
1289             break;
1290         case 'p':
1291             progress = true;
1292             break;
1293         case 'q':
1294             quiet = true;
1295             break;
1296         case 's':
1297             strict = true;
1298             break;
1299         case 'U':
1300             force_share = true;
1301             break;
1302         case OPTION_OBJECT: {
1303             QemuOpts *opts;
1304             opts = qemu_opts_parse_noisily(&qemu_object_opts,
1305                                            optarg, true);
1306             if (!opts) {
1307                 ret = 2;
1308                 goto out4;
1309             }
1310         }   break;
1311         case OPTION_IMAGE_OPTS:
1312             image_opts = true;
1313             break;
1314         }
1315     }
1316 
1317     /* Progress is not shown in Quiet mode */
1318     if (quiet) {
1319         progress = false;
1320     }
1321 
1322 
1323     if (optind != argc - 2) {
1324         error_exit("Expecting two image file names");
1325     }
1326     filename1 = argv[optind++];
1327     filename2 = argv[optind++];
1328 
1329     if (qemu_opts_foreach(&qemu_object_opts,
1330                           user_creatable_add_opts_foreach,
1331                           NULL, NULL)) {
1332         ret = 2;
1333         goto out4;
1334     }
1335 
1336     /* Initialize before goto out */
1337     qemu_progress_init(progress, 2.0);
1338 
1339     flags = 0;
1340     ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
1341     if (ret < 0) {
1342         error_report("Invalid source cache option: %s", cache);
1343         ret = 2;
1344         goto out3;
1345     }
1346 
1347     blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet,
1348                     force_share);
1349     if (!blk1) {
1350         ret = 2;
1351         goto out3;
1352     }
1353 
1354     blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet,
1355                     force_share);
1356     if (!blk2) {
1357         ret = 2;
1358         goto out2;
1359     }
1360     bs1 = blk_bs(blk1);
1361     bs2 = blk_bs(blk2);
1362 
1363     buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1364     buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
1365     total_size1 = blk_getlength(blk1);
1366     if (total_size1 < 0) {
1367         error_report("Can't get size of %s: %s",
1368                      filename1, strerror(-total_size1));
1369         ret = 4;
1370         goto out;
1371     }
1372     total_size2 = blk_getlength(blk2);
1373     if (total_size2 < 0) {
1374         error_report("Can't get size of %s: %s",
1375                      filename2, strerror(-total_size2));
1376         ret = 4;
1377         goto out;
1378     }
1379     total_size = MIN(total_size1, total_size2);
1380     progress_base = MAX(total_size1, total_size2);
1381 
1382     qemu_progress_print(0, 100);
1383 
1384     if (strict && total_size1 != total_size2) {
1385         ret = 1;
1386         qprintf(quiet, "Strict mode: Image size mismatch!\n");
1387         goto out;
1388     }
1389 
1390     while (offset < total_size) {
1391         int status1, status2;
1392 
1393         status1 = bdrv_block_status_above(bs1, NULL, offset,
1394                                           total_size1 - offset, &pnum1, NULL,
1395                                           NULL);
1396         if (status1 < 0) {
1397             ret = 3;
1398             error_report("Sector allocation test failed for %s", filename1);
1399             goto out;
1400         }
1401         allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
1402 
1403         status2 = bdrv_block_status_above(bs2, NULL, offset,
1404                                           total_size2 - offset, &pnum2, NULL,
1405                                           NULL);
1406         if (status2 < 0) {
1407             ret = 3;
1408             error_report("Sector allocation test failed for %s", filename2);
1409             goto out;
1410         }
1411         allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
1412 
1413         assert(pnum1 && pnum2);
1414         chunk = MIN(pnum1, pnum2);
1415 
1416         if (strict) {
1417             if (status1 != status2) {
1418                 ret = 1;
1419                 qprintf(quiet, "Strict mode: Offset %" PRId64
1420                         " block status mismatch!\n", offset);
1421                 goto out;
1422             }
1423         }
1424         if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
1425             /* nothing to do */
1426         } else if (allocated1 == allocated2) {
1427             if (allocated1) {
1428                 int64_t pnum;
1429 
1430                 chunk = MIN(chunk, IO_BUF_SIZE);
1431                 ret = blk_pread(blk1, offset, buf1, chunk);
1432                 if (ret < 0) {
1433                     error_report("Error while reading offset %" PRId64
1434                                  " of %s: %s",
1435                                  offset, filename1, strerror(-ret));
1436                     ret = 4;
1437                     goto out;
1438                 }
1439                 ret = blk_pread(blk2, offset, buf2, chunk);
1440                 if (ret < 0) {
1441                     error_report("Error while reading offset %" PRId64
1442                                  " of %s: %s",
1443                                  offset, filename2, strerror(-ret));
1444                     ret = 4;
1445                     goto out;
1446                 }
1447                 ret = compare_buffers(buf1, buf2, chunk, &pnum);
1448                 if (ret || pnum != chunk) {
1449                     qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1450                             offset + (ret ? 0 : pnum));
1451                     ret = 1;
1452                     goto out;
1453                 }
1454             }
1455         } else {
1456             chunk = MIN(chunk, IO_BUF_SIZE);
1457             if (allocated1) {
1458                 ret = check_empty_sectors(blk1, offset, chunk,
1459                                           filename1, buf1, quiet);
1460             } else {
1461                 ret = check_empty_sectors(blk2, offset, chunk,
1462                                           filename2, buf1, quiet);
1463             }
1464             if (ret) {
1465                 goto out;
1466             }
1467         }
1468         offset += chunk;
1469         qemu_progress_print(((float) chunk / progress_base) * 100, 100);
1470     }
1471 
1472     if (total_size1 != total_size2) {
1473         BlockBackend *blk_over;
1474         const char *filename_over;
1475 
1476         qprintf(quiet, "Warning: Image size mismatch!\n");
1477         if (total_size1 > total_size2) {
1478             blk_over = blk1;
1479             filename_over = filename1;
1480         } else {
1481             blk_over = blk2;
1482             filename_over = filename2;
1483         }
1484 
1485         while (offset < progress_base) {
1486             ret = bdrv_block_status_above(blk_bs(blk_over), NULL, offset,
1487                                           progress_base - offset, &chunk,
1488                                           NULL, NULL);
1489             if (ret < 0) {
1490                 ret = 3;
1491                 error_report("Sector allocation test failed for %s",
1492                              filename_over);
1493                 goto out;
1494 
1495             }
1496             if (ret & BDRV_BLOCK_ALLOCATED && !(ret & BDRV_BLOCK_ZERO)) {
1497                 chunk = MIN(chunk, IO_BUF_SIZE);
1498                 ret = check_empty_sectors(blk_over, offset, chunk,
1499                                           filename_over, buf1, quiet);
1500                 if (ret) {
1501                     goto out;
1502                 }
1503             }
1504             offset += chunk;
1505             qemu_progress_print(((float) chunk / progress_base) * 100, 100);
1506         }
1507     }
1508 
1509     qprintf(quiet, "Images are identical.\n");
1510     ret = 0;
1511 
1512 out:
1513     qemu_vfree(buf1);
1514     qemu_vfree(buf2);
1515     blk_unref(blk2);
1516 out2:
1517     blk_unref(blk1);
1518 out3:
1519     qemu_progress_end();
1520 out4:
1521     return ret;
1522 }
1523 
1524 enum ImgConvertBlockStatus {
1525     BLK_DATA,
1526     BLK_ZERO,
1527     BLK_BACKING_FILE,
1528 };
1529 
1530 #define MAX_COROUTINES 16
1531 
1532 typedef struct ImgConvertState {
1533     BlockBackend **src;
1534     int64_t *src_sectors;
1535     int src_num;
1536     int64_t total_sectors;
1537     int64_t allocated_sectors;
1538     int64_t allocated_done;
1539     int64_t sector_num;
1540     int64_t wr_offs;
1541     enum ImgConvertBlockStatus status;
1542     int64_t sector_next_status;
1543     BlockBackend *target;
1544     bool has_zero_init;
1545     bool compressed;
1546     bool target_has_backing;
1547     bool wr_in_order;
1548     int min_sparse;
1549     size_t cluster_sectors;
1550     size_t buf_sectors;
1551     long num_coroutines;
1552     int running_coroutines;
1553     Coroutine *co[MAX_COROUTINES];
1554     int64_t wait_sector_num[MAX_COROUTINES];
1555     CoMutex lock;
1556     int ret;
1557 } ImgConvertState;
1558 
1559 static void convert_select_part(ImgConvertState *s, int64_t sector_num,
1560                                 int *src_cur, int64_t *src_cur_offset)
1561 {
1562     *src_cur = 0;
1563     *src_cur_offset = 0;
1564     while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) {
1565         *src_cur_offset += s->src_sectors[*src_cur];
1566         (*src_cur)++;
1567         assert(*src_cur < s->src_num);
1568     }
1569 }
1570 
1571 static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1572 {
1573     int64_t src_cur_offset;
1574     int ret, n, src_cur;
1575 
1576     convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1577 
1578     assert(s->total_sectors > sector_num);
1579     n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1580 
1581     if (s->sector_next_status <= sector_num) {
1582         int64_t count = n * BDRV_SECTOR_SIZE;
1583 
1584         if (s->target_has_backing) {
1585 
1586             ret = bdrv_block_status(blk_bs(s->src[src_cur]),
1587                                     (sector_num - src_cur_offset) *
1588                                     BDRV_SECTOR_SIZE,
1589                                     count, &count, NULL, NULL);
1590         } else {
1591             ret = bdrv_block_status_above(blk_bs(s->src[src_cur]), NULL,
1592                                           (sector_num - src_cur_offset) *
1593                                           BDRV_SECTOR_SIZE,
1594                                           count, &count, NULL, NULL);
1595         }
1596         if (ret < 0) {
1597             return ret;
1598         }
1599         n = DIV_ROUND_UP(count, BDRV_SECTOR_SIZE);
1600 
1601         if (ret & BDRV_BLOCK_ZERO) {
1602             s->status = BLK_ZERO;
1603         } else if (ret & BDRV_BLOCK_DATA) {
1604             s->status = BLK_DATA;
1605         } else {
1606             s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA;
1607         }
1608 
1609         s->sector_next_status = sector_num + n;
1610     }
1611 
1612     n = MIN(n, s->sector_next_status - sector_num);
1613     if (s->status == BLK_DATA) {
1614         n = MIN(n, s->buf_sectors);
1615     }
1616 
1617     /* We need to write complete clusters for compressed images, so if an
1618      * unallocated area is shorter than that, we must consider the whole
1619      * cluster allocated. */
1620     if (s->compressed) {
1621         if (n < s->cluster_sectors) {
1622             n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1623             s->status = BLK_DATA;
1624         } else {
1625             n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1626         }
1627     }
1628 
1629     return n;
1630 }
1631 
1632 static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num,
1633                                         int nb_sectors, uint8_t *buf)
1634 {
1635     int n, ret;
1636     QEMUIOVector qiov;
1637     struct iovec iov;
1638 
1639     assert(nb_sectors <= s->buf_sectors);
1640     while (nb_sectors > 0) {
1641         BlockBackend *blk;
1642         int src_cur;
1643         int64_t bs_sectors, src_cur_offset;
1644 
1645         /* In the case of compression with multiple source files, we can get a
1646          * nb_sectors that spreads into the next part. So we must be able to
1647          * read across multiple BDSes for one convert_read() call. */
1648         convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1649         blk = s->src[src_cur];
1650         bs_sectors = s->src_sectors[src_cur];
1651 
1652         n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1653         iov.iov_base = buf;
1654         iov.iov_len = n << BDRV_SECTOR_BITS;
1655         qemu_iovec_init_external(&qiov, &iov, 1);
1656 
1657         ret = blk_co_preadv(
1658                 blk, (sector_num - src_cur_offset) << BDRV_SECTOR_BITS,
1659                 n << BDRV_SECTOR_BITS, &qiov, 0);
1660         if (ret < 0) {
1661             return ret;
1662         }
1663 
1664         sector_num += n;
1665         nb_sectors -= n;
1666         buf += n * BDRV_SECTOR_SIZE;
1667     }
1668 
1669     return 0;
1670 }
1671 
1672 
1673 static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
1674                                          int nb_sectors, uint8_t *buf,
1675                                          enum ImgConvertBlockStatus status)
1676 {
1677     int ret;
1678     QEMUIOVector qiov;
1679     struct iovec iov;
1680 
1681     while (nb_sectors > 0) {
1682         int n = nb_sectors;
1683         BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0;
1684 
1685         switch (status) {
1686         case BLK_BACKING_FILE:
1687             /* If we have a backing file, leave clusters unallocated that are
1688              * unallocated in the source image, so that the backing file is
1689              * visible at the respective offset. */
1690             assert(s->target_has_backing);
1691             break;
1692 
1693         case BLK_DATA:
1694             /* If we're told to keep the target fully allocated (-S 0) or there
1695              * is real non-zero data, we must write it. Otherwise we can treat
1696              * it as zero sectors.
1697              * Compressed clusters need to be written as a whole, so in that
1698              * case we can only save the write if the buffer is completely
1699              * zeroed. */
1700             if (!s->min_sparse ||
1701                 (!s->compressed &&
1702                  is_allocated_sectors_min(buf, n, &n, s->min_sparse)) ||
1703                 (s->compressed &&
1704                  !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)))
1705             {
1706                 iov.iov_base = buf;
1707                 iov.iov_len = n << BDRV_SECTOR_BITS;
1708                 qemu_iovec_init_external(&qiov, &iov, 1);
1709 
1710                 ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS,
1711                                      n << BDRV_SECTOR_BITS, &qiov, flags);
1712                 if (ret < 0) {
1713                     return ret;
1714                 }
1715                 break;
1716             }
1717             /* fall-through */
1718 
1719         case BLK_ZERO:
1720             if (s->has_zero_init) {
1721                 assert(!s->target_has_backing);
1722                 break;
1723             }
1724             ret = blk_co_pwrite_zeroes(s->target,
1725                                        sector_num << BDRV_SECTOR_BITS,
1726                                        n << BDRV_SECTOR_BITS, 0);
1727             if (ret < 0) {
1728                 return ret;
1729             }
1730             break;
1731         }
1732 
1733         sector_num += n;
1734         nb_sectors -= n;
1735         buf += n * BDRV_SECTOR_SIZE;
1736     }
1737 
1738     return 0;
1739 }
1740 
1741 static void coroutine_fn convert_co_do_copy(void *opaque)
1742 {
1743     ImgConvertState *s = opaque;
1744     uint8_t *buf = NULL;
1745     int ret, i;
1746     int index = -1;
1747 
1748     for (i = 0; i < s->num_coroutines; i++) {
1749         if (s->co[i] == qemu_coroutine_self()) {
1750             index = i;
1751             break;
1752         }
1753     }
1754     assert(index >= 0);
1755 
1756     s->running_coroutines++;
1757     buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1758 
1759     while (1) {
1760         int n;
1761         int64_t sector_num;
1762         enum ImgConvertBlockStatus status;
1763 
1764         qemu_co_mutex_lock(&s->lock);
1765         if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) {
1766             qemu_co_mutex_unlock(&s->lock);
1767             break;
1768         }
1769         n = convert_iteration_sectors(s, s->sector_num);
1770         if (n < 0) {
1771             qemu_co_mutex_unlock(&s->lock);
1772             s->ret = n;
1773             break;
1774         }
1775         /* save current sector and allocation status to local variables */
1776         sector_num = s->sector_num;
1777         status = s->status;
1778         if (!s->min_sparse && s->status == BLK_ZERO) {
1779             n = MIN(n, s->buf_sectors);
1780         }
1781         /* increment global sector counter so that other coroutines can
1782          * already continue reading beyond this request */
1783         s->sector_num += n;
1784         qemu_co_mutex_unlock(&s->lock);
1785 
1786         if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) {
1787             s->allocated_done += n;
1788             qemu_progress_print(100.0 * s->allocated_done /
1789                                         s->allocated_sectors, 0);
1790         }
1791 
1792         if (status == BLK_DATA) {
1793             ret = convert_co_read(s, sector_num, n, buf);
1794             if (ret < 0) {
1795                 error_report("error while reading sector %" PRId64
1796                              ": %s", sector_num, strerror(-ret));
1797                 s->ret = ret;
1798             }
1799         } else if (!s->min_sparse && status == BLK_ZERO) {
1800             status = BLK_DATA;
1801             memset(buf, 0x00, n * BDRV_SECTOR_SIZE);
1802         }
1803 
1804         if (s->wr_in_order) {
1805             /* keep writes in order */
1806             while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) {
1807                 s->wait_sector_num[index] = sector_num;
1808                 qemu_coroutine_yield();
1809             }
1810             s->wait_sector_num[index] = -1;
1811         }
1812 
1813         if (s->ret == -EINPROGRESS) {
1814             ret = convert_co_write(s, sector_num, n, buf, status);
1815             if (ret < 0) {
1816                 error_report("error while writing sector %" PRId64
1817                              ": %s", sector_num, strerror(-ret));
1818                 s->ret = ret;
1819             }
1820         }
1821 
1822         if (s->wr_in_order) {
1823             /* reenter the coroutine that might have waited
1824              * for this write to complete */
1825             s->wr_offs = sector_num + n;
1826             for (i = 0; i < s->num_coroutines; i++) {
1827                 if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) {
1828                     /*
1829                      * A -> B -> A cannot occur because A has
1830                      * s->wait_sector_num[i] == -1 during A -> B.  Therefore
1831                      * B will never enter A during this time window.
1832                      */
1833                     qemu_coroutine_enter(s->co[i]);
1834                     break;
1835                 }
1836             }
1837         }
1838     }
1839 
1840     qemu_vfree(buf);
1841     s->co[index] = NULL;
1842     s->running_coroutines--;
1843     if (!s->running_coroutines && s->ret == -EINPROGRESS) {
1844         /* the convert job finished successfully */
1845         s->ret = 0;
1846     }
1847 }
1848 
1849 static int convert_do_copy(ImgConvertState *s)
1850 {
1851     int ret, i, n;
1852     int64_t sector_num = 0;
1853 
1854     /* Check whether we have zero initialisation or can get it efficiently */
1855     s->has_zero_init = s->min_sparse && !s->target_has_backing
1856                      ? bdrv_has_zero_init(blk_bs(s->target))
1857                      : false;
1858 
1859     if (!s->has_zero_init && !s->target_has_backing &&
1860         bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
1861     {
1862         ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP);
1863         if (ret == 0) {
1864             s->has_zero_init = true;
1865         }
1866     }
1867 
1868     /* Allocate buffer for copied data. For compressed images, only one cluster
1869      * can be copied at a time. */
1870     if (s->compressed) {
1871         if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
1872             error_report("invalid cluster size");
1873             return -EINVAL;
1874         }
1875         s->buf_sectors = s->cluster_sectors;
1876     }
1877 
1878     while (sector_num < s->total_sectors) {
1879         n = convert_iteration_sectors(s, sector_num);
1880         if (n < 0) {
1881             return n;
1882         }
1883         if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
1884         {
1885             s->allocated_sectors += n;
1886         }
1887         sector_num += n;
1888     }
1889 
1890     /* Do the copy */
1891     s->sector_next_status = 0;
1892     s->ret = -EINPROGRESS;
1893 
1894     qemu_co_mutex_init(&s->lock);
1895     for (i = 0; i < s->num_coroutines; i++) {
1896         s->co[i] = qemu_coroutine_create(convert_co_do_copy, s);
1897         s->wait_sector_num[i] = -1;
1898         qemu_coroutine_enter(s->co[i]);
1899     }
1900 
1901     while (s->running_coroutines) {
1902         main_loop_wait(false);
1903     }
1904 
1905     if (s->compressed && !s->ret) {
1906         /* signal EOF to align */
1907         ret = blk_pwrite_compressed(s->target, 0, NULL, 0);
1908         if (ret < 0) {
1909             return ret;
1910         }
1911     }
1912 
1913     return s->ret;
1914 }
1915 
1916 static int img_convert(int argc, char **argv)
1917 {
1918     int c, bs_i, flags, src_flags = 0;
1919     const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe",
1920                *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL,
1921                *out_filename, *out_baseimg_param, *snapshot_name = NULL;
1922     BlockDriver *drv = NULL, *proto_drv = NULL;
1923     BlockDriverInfo bdi;
1924     BlockDriverState *out_bs;
1925     QemuOpts *opts = NULL, *sn_opts = NULL;
1926     QemuOptsList *create_opts = NULL;
1927     char *options = NULL;
1928     Error *local_err = NULL;
1929     bool writethrough, src_writethrough, quiet = false, image_opts = false,
1930          skip_create = false, progress = false, tgt_image_opts = false;
1931     int64_t ret = -EINVAL;
1932     bool force_share = false;
1933 
1934     ImgConvertState s = (ImgConvertState) {
1935         /* Need at least 4k of zeros for sparse detection */
1936         .min_sparse         = 8,
1937         .buf_sectors        = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
1938         .wr_in_order        = true,
1939         .num_coroutines     = 8,
1940     };
1941 
1942     for(;;) {
1943         static const struct option long_options[] = {
1944             {"help", no_argument, 0, 'h'},
1945             {"object", required_argument, 0, OPTION_OBJECT},
1946             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
1947             {"force-share", no_argument, 0, 'U'},
1948             {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
1949             {0, 0, 0, 0}
1950         };
1951         c = getopt_long(argc, argv, ":hf:O:B:co:s:l:S:pt:T:qnm:WU",
1952                         long_options, NULL);
1953         if (c == -1) {
1954             break;
1955         }
1956         switch(c) {
1957         case ':':
1958             missing_argument(argv[optind - 1]);
1959             break;
1960         case '?':
1961             unrecognized_option(argv[optind - 1]);
1962             break;
1963         case 'h':
1964             help();
1965             break;
1966         case 'f':
1967             fmt = optarg;
1968             break;
1969         case 'O':
1970             out_fmt = optarg;
1971             break;
1972         case 'B':
1973             out_baseimg = optarg;
1974             break;
1975         case 'c':
1976             s.compressed = true;
1977             break;
1978         case 'o':
1979             if (!is_valid_option_list(optarg)) {
1980                 error_report("Invalid option list: %s", optarg);
1981                 goto fail_getopt;
1982             }
1983             if (!options) {
1984                 options = g_strdup(optarg);
1985             } else {
1986                 char *old_options = options;
1987                 options = g_strdup_printf("%s,%s", options, optarg);
1988                 g_free(old_options);
1989             }
1990             break;
1991         case 's':
1992             snapshot_name = optarg;
1993             break;
1994         case 'l':
1995             if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
1996                 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
1997                                                   optarg, false);
1998                 if (!sn_opts) {
1999                     error_report("Failed in parsing snapshot param '%s'",
2000                                  optarg);
2001                     goto fail_getopt;
2002                 }
2003             } else {
2004                 snapshot_name = optarg;
2005             }
2006             break;
2007         case 'S':
2008         {
2009             int64_t sval;
2010 
2011             sval = cvtnum(optarg);
2012             if (sval < 0) {
2013                 error_report("Invalid minimum zero buffer size for sparse output specified");
2014                 goto fail_getopt;
2015             }
2016 
2017             s.min_sparse = sval / BDRV_SECTOR_SIZE;
2018             break;
2019         }
2020         case 'p':
2021             progress = true;
2022             break;
2023         case 't':
2024             cache = optarg;
2025             break;
2026         case 'T':
2027             src_cache = optarg;
2028             break;
2029         case 'q':
2030             quiet = true;
2031             break;
2032         case 'n':
2033             skip_create = true;
2034             break;
2035         case 'm':
2036             if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) ||
2037                 s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) {
2038                 error_report("Invalid number of coroutines. Allowed number of"
2039                              " coroutines is between 1 and %d", MAX_COROUTINES);
2040                 goto fail_getopt;
2041             }
2042             break;
2043         case 'W':
2044             s.wr_in_order = false;
2045             break;
2046         case 'U':
2047             force_share = true;
2048             break;
2049         case OPTION_OBJECT: {
2050             QemuOpts *object_opts;
2051             object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
2052                                                   optarg, true);
2053             if (!object_opts) {
2054                 goto fail_getopt;
2055             }
2056             break;
2057         }
2058         case OPTION_IMAGE_OPTS:
2059             image_opts = true;
2060             break;
2061         case OPTION_TARGET_IMAGE_OPTS:
2062             tgt_image_opts = true;
2063             break;
2064         }
2065     }
2066 
2067     if (!out_fmt && !tgt_image_opts) {
2068         out_fmt = "raw";
2069     }
2070 
2071     if (qemu_opts_foreach(&qemu_object_opts,
2072                           user_creatable_add_opts_foreach,
2073                           NULL, NULL)) {
2074         goto fail_getopt;
2075     }
2076 
2077     if (!s.wr_in_order && s.compressed) {
2078         error_report("Out of order write and compress are mutually exclusive");
2079         goto fail_getopt;
2080     }
2081 
2082     if (tgt_image_opts && !skip_create) {
2083         error_report("--target-image-opts requires use of -n flag");
2084         goto fail_getopt;
2085     }
2086 
2087     s.src_num = argc - optind - 1;
2088     out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;
2089 
2090     if (options && has_help_option(options)) {
2091         if (out_fmt) {
2092             ret = print_block_option_help(out_filename, out_fmt);
2093             goto fail_getopt;
2094         } else {
2095             error_report("Option help requires a format be specified");
2096             goto fail_getopt;
2097         }
2098     }
2099 
2100     if (s.src_num < 1) {
2101         error_report("Must specify image file name");
2102         goto fail_getopt;
2103     }
2104 
2105 
2106     /* ret is still -EINVAL until here */
2107     ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
2108     if (ret < 0) {
2109         error_report("Invalid source cache option: %s", src_cache);
2110         goto fail_getopt;
2111     }
2112 
2113     /* Initialize before goto out */
2114     if (quiet) {
2115         progress = false;
2116     }
2117     qemu_progress_init(progress, 1.0);
2118     qemu_progress_print(0, 100);
2119 
2120     s.src = g_new0(BlockBackend *, s.src_num);
2121     s.src_sectors = g_new(int64_t, s.src_num);
2122 
2123     for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2124         s.src[bs_i] = img_open(image_opts, argv[optind + bs_i],
2125                                fmt, src_flags, src_writethrough, quiet,
2126                                force_share);
2127         if (!s.src[bs_i]) {
2128             ret = -1;
2129             goto out;
2130         }
2131         s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]);
2132         if (s.src_sectors[bs_i] < 0) {
2133             error_report("Could not get size of %s: %s",
2134                          argv[optind + bs_i], strerror(-s.src_sectors[bs_i]));
2135             ret = -1;
2136             goto out;
2137         }
2138         s.total_sectors += s.src_sectors[bs_i];
2139     }
2140 
2141     if (sn_opts) {
2142         bdrv_snapshot_load_tmp(blk_bs(s.src[0]),
2143                                qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
2144                                qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
2145                                &local_err);
2146     } else if (snapshot_name != NULL) {
2147         if (s.src_num > 1) {
2148             error_report("No support for concatenating multiple snapshot");
2149             ret = -1;
2150             goto out;
2151         }
2152 
2153         bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name,
2154                                              &local_err);
2155     }
2156     if (local_err) {
2157         error_reportf_err(local_err, "Failed to load snapshot: ");
2158         ret = -1;
2159         goto out;
2160     }
2161 
2162     if (!skip_create) {
2163         /* Find driver and parse its options */
2164         drv = bdrv_find_format(out_fmt);
2165         if (!drv) {
2166             error_report("Unknown file format '%s'", out_fmt);
2167             ret = -1;
2168             goto out;
2169         }
2170 
2171         proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
2172         if (!proto_drv) {
2173             error_report_err(local_err);
2174             ret = -1;
2175             goto out;
2176         }
2177 
2178         if (!drv->create_opts) {
2179             error_report("Format driver '%s' does not support image creation",
2180                          drv->format_name);
2181             ret = -1;
2182             goto out;
2183         }
2184 
2185         if (!proto_drv->create_opts) {
2186             error_report("Protocol driver '%s' does not support image creation",
2187                          proto_drv->format_name);
2188             ret = -1;
2189             goto out;
2190         }
2191 
2192         create_opts = qemu_opts_append(create_opts, drv->create_opts);
2193         create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
2194 
2195         opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
2196         if (options) {
2197             qemu_opts_do_parse(opts, options, NULL, &local_err);
2198             if (local_err) {
2199                 error_report_err(local_err);
2200                 ret = -1;
2201                 goto out;
2202             }
2203         }
2204 
2205         qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s.total_sectors * 512,
2206                             &error_abort);
2207         ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
2208         if (ret < 0) {
2209             goto out;
2210         }
2211     }
2212 
2213     /* Get backing file name if -o backing_file was used */
2214     out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
2215     if (out_baseimg_param) {
2216         out_baseimg = out_baseimg_param;
2217     }
2218     s.target_has_backing = (bool) out_baseimg;
2219 
2220     if (s.src_num > 1 && out_baseimg) {
2221         error_report("Having a backing file for the target makes no sense when "
2222                      "concatenating multiple input images");
2223         ret = -1;
2224         goto out;
2225     }
2226 
2227     /* Check if compression is supported */
2228     if (s.compressed) {
2229         bool encryption =
2230             qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
2231         const char *encryptfmt =
2232             qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT);
2233         const char *preallocation =
2234             qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
2235 
2236         if (drv && !drv->bdrv_co_pwritev_compressed) {
2237             error_report("Compression not supported for this file format");
2238             ret = -1;
2239             goto out;
2240         }
2241 
2242         if (encryption || encryptfmt) {
2243             error_report("Compression and encryption not supported at "
2244                          "the same time");
2245             ret = -1;
2246             goto out;
2247         }
2248 
2249         if (preallocation
2250             && strcmp(preallocation, "off"))
2251         {
2252             error_report("Compression and preallocation not supported at "
2253                          "the same time");
2254             ret = -1;
2255             goto out;
2256         }
2257     }
2258 
2259     if (!skip_create) {
2260         /* Create the new image */
2261         ret = bdrv_create(drv, out_filename, opts, &local_err);
2262         if (ret < 0) {
2263             error_reportf_err(local_err, "%s: error while converting %s: ",
2264                               out_filename, out_fmt);
2265             goto out;
2266         }
2267     }
2268 
2269     flags = s.min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
2270     ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
2271     if (ret < 0) {
2272         error_report("Invalid cache option: %s", cache);
2273         goto out;
2274     }
2275 
2276     if (skip_create) {
2277         s.target = img_open(tgt_image_opts, out_filename, out_fmt,
2278                             flags, writethrough, quiet, false);
2279     } else {
2280         /* TODO ultimately we should allow --target-image-opts
2281          * to be used even when -n is not given.
2282          * That has to wait for bdrv_create to be improved
2283          * to allow filenames in option syntax
2284          */
2285         s.target = img_open_new_file(out_filename, opts, out_fmt,
2286                                      flags, writethrough, quiet, false);
2287     }
2288     if (!s.target) {
2289         ret = -1;
2290         goto out;
2291     }
2292     out_bs = blk_bs(s.target);
2293 
2294     if (s.compressed && !out_bs->drv->bdrv_co_pwritev_compressed) {
2295         error_report("Compression not supported for this file format");
2296         ret = -1;
2297         goto out;
2298     }
2299 
2300     /* increase bufsectors from the default 4096 (2M) if opt_transfer
2301      * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
2302      * as maximum. */
2303     s.buf_sectors = MIN(32768,
2304                         MAX(s.buf_sectors,
2305                             MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS,
2306                                 out_bs->bl.pdiscard_alignment >>
2307                                 BDRV_SECTOR_BITS)));
2308 
2309     if (skip_create) {
2310         int64_t output_sectors = blk_nb_sectors(s.target);
2311         if (output_sectors < 0) {
2312             error_report("unable to get output image length: %s",
2313                          strerror(-output_sectors));
2314             ret = -1;
2315             goto out;
2316         } else if (output_sectors < s.total_sectors) {
2317             error_report("output file is smaller than input file");
2318             ret = -1;
2319             goto out;
2320         }
2321     }
2322 
2323     ret = bdrv_get_info(out_bs, &bdi);
2324     if (ret < 0) {
2325         if (s.compressed) {
2326             error_report("could not get block driver info");
2327             goto out;
2328         }
2329     } else {
2330         s.compressed = s.compressed || bdi.needs_compressed_writes;
2331         s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
2332     }
2333 
2334     ret = convert_do_copy(&s);
2335 out:
2336     if (!ret) {
2337         qemu_progress_print(100, 0);
2338     }
2339     qemu_progress_end();
2340     qemu_opts_del(opts);
2341     qemu_opts_free(create_opts);
2342     qemu_opts_del(sn_opts);
2343     blk_unref(s.target);
2344     if (s.src) {
2345         for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2346             blk_unref(s.src[bs_i]);
2347         }
2348         g_free(s.src);
2349     }
2350     g_free(s.src_sectors);
2351 fail_getopt:
2352     g_free(options);
2353 
2354     return !!ret;
2355 }
2356 
2357 
2358 static void dump_snapshots(BlockDriverState *bs)
2359 {
2360     QEMUSnapshotInfo *sn_tab, *sn;
2361     int nb_sns, i;
2362 
2363     nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2364     if (nb_sns <= 0)
2365         return;
2366     printf("Snapshot list:\n");
2367     bdrv_snapshot_dump(fprintf, stdout, NULL);
2368     printf("\n");
2369     for(i = 0; i < nb_sns; i++) {
2370         sn = &sn_tab[i];
2371         bdrv_snapshot_dump(fprintf, stdout, sn);
2372         printf("\n");
2373     }
2374     g_free(sn_tab);
2375 }
2376 
2377 static void dump_json_image_info_list(ImageInfoList *list)
2378 {
2379     QString *str;
2380     QObject *obj;
2381     Visitor *v = qobject_output_visitor_new(&obj);
2382 
2383     visit_type_ImageInfoList(v, NULL, &list, &error_abort);
2384     visit_complete(v, &obj);
2385     str = qobject_to_json_pretty(obj);
2386     assert(str != NULL);
2387     printf("%s\n", qstring_get_str(str));
2388     qobject_decref(obj);
2389     visit_free(v);
2390     QDECREF(str);
2391 }
2392 
2393 static void dump_json_image_info(ImageInfo *info)
2394 {
2395     QString *str;
2396     QObject *obj;
2397     Visitor *v = qobject_output_visitor_new(&obj);
2398 
2399     visit_type_ImageInfo(v, NULL, &info, &error_abort);
2400     visit_complete(v, &obj);
2401     str = qobject_to_json_pretty(obj);
2402     assert(str != NULL);
2403     printf("%s\n", qstring_get_str(str));
2404     qobject_decref(obj);
2405     visit_free(v);
2406     QDECREF(str);
2407 }
2408 
2409 static void dump_human_image_info_list(ImageInfoList *list)
2410 {
2411     ImageInfoList *elem;
2412     bool delim = false;
2413 
2414     for (elem = list; elem; elem = elem->next) {
2415         if (delim) {
2416             printf("\n");
2417         }
2418         delim = true;
2419 
2420         bdrv_image_info_dump(fprintf, stdout, elem->value);
2421     }
2422 }
2423 
2424 static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2425 {
2426     return strcmp(a, b) == 0;
2427 }
2428 
2429 /**
2430  * Open an image file chain and return an ImageInfoList
2431  *
2432  * @filename: topmost image filename
2433  * @fmt: topmost image format (may be NULL to autodetect)
2434  * @chain: true  - enumerate entire backing file chain
2435  *         false - only topmost image file
2436  *
2437  * Returns a list of ImageInfo objects or NULL if there was an error opening an
2438  * image file.  If there was an error a message will have been printed to
2439  * stderr.
2440  */
2441 static ImageInfoList *collect_image_info_list(bool image_opts,
2442                                               const char *filename,
2443                                               const char *fmt,
2444                                               bool chain, bool force_share)
2445 {
2446     ImageInfoList *head = NULL;
2447     ImageInfoList **last = &head;
2448     GHashTable *filenames;
2449     Error *err = NULL;
2450 
2451     filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2452 
2453     while (filename) {
2454         BlockBackend *blk;
2455         BlockDriverState *bs;
2456         ImageInfo *info;
2457         ImageInfoList *elem;
2458 
2459         if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2460             error_report("Backing file '%s' creates an infinite loop.",
2461                          filename);
2462             goto err;
2463         }
2464         g_hash_table_insert(filenames, (gpointer)filename, NULL);
2465 
2466         blk = img_open(image_opts, filename, fmt,
2467                        BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false,
2468                        force_share);
2469         if (!blk) {
2470             goto err;
2471         }
2472         bs = blk_bs(blk);
2473 
2474         bdrv_query_image_info(bs, &info, &err);
2475         if (err) {
2476             error_report_err(err);
2477             blk_unref(blk);
2478             goto err;
2479         }
2480 
2481         elem = g_new0(ImageInfoList, 1);
2482         elem->value = info;
2483         *last = elem;
2484         last = &elem->next;
2485 
2486         blk_unref(blk);
2487 
2488         filename = fmt = NULL;
2489         if (chain) {
2490             if (info->has_full_backing_filename) {
2491                 filename = info->full_backing_filename;
2492             } else if (info->has_backing_filename) {
2493                 error_report("Could not determine absolute backing filename,"
2494                              " but backing filename '%s' present",
2495                              info->backing_filename);
2496                 goto err;
2497             }
2498             if (info->has_backing_filename_format) {
2499                 fmt = info->backing_filename_format;
2500             }
2501         }
2502     }
2503     g_hash_table_destroy(filenames);
2504     return head;
2505 
2506 err:
2507     qapi_free_ImageInfoList(head);
2508     g_hash_table_destroy(filenames);
2509     return NULL;
2510 }
2511 
2512 static int img_info(int argc, char **argv)
2513 {
2514     int c;
2515     OutputFormat output_format = OFORMAT_HUMAN;
2516     bool chain = false;
2517     const char *filename, *fmt, *output;
2518     ImageInfoList *list;
2519     bool image_opts = false;
2520     bool force_share = false;
2521 
2522     fmt = NULL;
2523     output = NULL;
2524     for(;;) {
2525         int option_index = 0;
2526         static const struct option long_options[] = {
2527             {"help", no_argument, 0, 'h'},
2528             {"format", required_argument, 0, 'f'},
2529             {"output", required_argument, 0, OPTION_OUTPUT},
2530             {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
2531             {"object", required_argument, 0, OPTION_OBJECT},
2532             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
2533             {"force-share", no_argument, 0, 'U'},
2534             {0, 0, 0, 0}
2535         };
2536         c = getopt_long(argc, argv, ":f:hU",
2537                         long_options, &option_index);
2538         if (c == -1) {
2539             break;
2540         }
2541         switch(c) {
2542         case ':':
2543             missing_argument(argv[optind - 1]);
2544             break;
2545         case '?':
2546             unrecognized_option(argv[optind - 1]);
2547             break;
2548         case 'h':
2549             help();
2550             break;
2551         case 'f':
2552             fmt = optarg;
2553             break;
2554         case 'U':
2555             force_share = true;
2556             break;
2557         case OPTION_OUTPUT:
2558             output = optarg;
2559             break;
2560         case OPTION_BACKING_CHAIN:
2561             chain = true;
2562             break;
2563         case OPTION_OBJECT: {
2564             QemuOpts *opts;
2565             opts = qemu_opts_parse_noisily(&qemu_object_opts,
2566                                            optarg, true);
2567             if (!opts) {
2568                 return 1;
2569             }
2570         }   break;
2571         case OPTION_IMAGE_OPTS:
2572             image_opts = true;
2573             break;
2574         }
2575     }
2576     if (optind != argc - 1) {
2577         error_exit("Expecting one image file name");
2578     }
2579     filename = argv[optind++];
2580 
2581     if (output && !strcmp(output, "json")) {
2582         output_format = OFORMAT_JSON;
2583     } else if (output && !strcmp(output, "human")) {
2584         output_format = OFORMAT_HUMAN;
2585     } else if (output) {
2586         error_report("--output must be used with human or json as argument.");
2587         return 1;
2588     }
2589 
2590     if (qemu_opts_foreach(&qemu_object_opts,
2591                           user_creatable_add_opts_foreach,
2592                           NULL, NULL)) {
2593         return 1;
2594     }
2595 
2596     list = collect_image_info_list(image_opts, filename, fmt, chain,
2597                                    force_share);
2598     if (!list) {
2599         return 1;
2600     }
2601 
2602     switch (output_format) {
2603     case OFORMAT_HUMAN:
2604         dump_human_image_info_list(list);
2605         break;
2606     case OFORMAT_JSON:
2607         if (chain) {
2608             dump_json_image_info_list(list);
2609         } else {
2610             dump_json_image_info(list->value);
2611         }
2612         break;
2613     }
2614 
2615     qapi_free_ImageInfoList(list);
2616     return 0;
2617 }
2618 
2619 static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2620                            MapEntry *next)
2621 {
2622     switch (output_format) {
2623     case OFORMAT_HUMAN:
2624         if (e->data && !e->has_offset) {
2625             error_report("File contains external, encrypted or compressed clusters.");
2626             exit(1);
2627         }
2628         if (e->data && !e->zero) {
2629             printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
2630                    e->start, e->length,
2631                    e->has_offset ? e->offset : 0,
2632                    e->has_filename ? e->filename : "");
2633         }
2634         /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2635          * Modify the flags here to allow more coalescing.
2636          */
2637         if (next && (!next->data || next->zero)) {
2638             next->data = false;
2639             next->zero = true;
2640         }
2641         break;
2642     case OFORMAT_JSON:
2643         printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2644                " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
2645                (e->start == 0 ? "[" : ",\n"),
2646                e->start, e->length, e->depth,
2647                e->zero ? "true" : "false",
2648                e->data ? "true" : "false");
2649         if (e->has_offset) {
2650             printf(", \"offset\": %"PRId64"", e->offset);
2651         }
2652         putchar('}');
2653 
2654         if (!next) {
2655             printf("]\n");
2656         }
2657         break;
2658     }
2659 }
2660 
2661 static int get_block_status(BlockDriverState *bs, int64_t offset,
2662                             int64_t bytes, MapEntry *e)
2663 {
2664     int ret;
2665     int depth;
2666     BlockDriverState *file;
2667     bool has_offset;
2668     int64_t map;
2669 
2670     /* As an optimization, we could cache the current range of unallocated
2671      * clusters in each file of the chain, and avoid querying the same
2672      * range repeatedly.
2673      */
2674 
2675     depth = 0;
2676     for (;;) {
2677         ret = bdrv_block_status(bs, offset, bytes, &bytes, &map, &file);
2678         if (ret < 0) {
2679             return ret;
2680         }
2681         assert(bytes);
2682         if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2683             break;
2684         }
2685         bs = backing_bs(bs);
2686         if (bs == NULL) {
2687             ret = 0;
2688             break;
2689         }
2690 
2691         depth++;
2692     }
2693 
2694     has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2695 
2696     *e = (MapEntry) {
2697         .start = offset,
2698         .length = bytes,
2699         .data = !!(ret & BDRV_BLOCK_DATA),
2700         .zero = !!(ret & BDRV_BLOCK_ZERO),
2701         .offset = map,
2702         .has_offset = has_offset,
2703         .depth = depth,
2704         .has_filename = file && has_offset,
2705         .filename = file && has_offset ? file->filename : NULL,
2706     };
2707 
2708     return 0;
2709 }
2710 
2711 static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2712 {
2713     if (curr->length == 0) {
2714         return false;
2715     }
2716     if (curr->zero != next->zero ||
2717         curr->data != next->data ||
2718         curr->depth != next->depth ||
2719         curr->has_filename != next->has_filename ||
2720         curr->has_offset != next->has_offset) {
2721         return false;
2722     }
2723     if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2724         return false;
2725     }
2726     if (curr->has_offset && curr->offset + curr->length != next->offset) {
2727         return false;
2728     }
2729     return true;
2730 }
2731 
2732 static int img_map(int argc, char **argv)
2733 {
2734     int c;
2735     OutputFormat output_format = OFORMAT_HUMAN;
2736     BlockBackend *blk;
2737     BlockDriverState *bs;
2738     const char *filename, *fmt, *output;
2739     int64_t length;
2740     MapEntry curr = { .length = 0 }, next;
2741     int ret = 0;
2742     bool image_opts = false;
2743     bool force_share = false;
2744 
2745     fmt = NULL;
2746     output = NULL;
2747     for (;;) {
2748         int option_index = 0;
2749         static const struct option long_options[] = {
2750             {"help", no_argument, 0, 'h'},
2751             {"format", required_argument, 0, 'f'},
2752             {"output", required_argument, 0, OPTION_OUTPUT},
2753             {"object", required_argument, 0, OPTION_OBJECT},
2754             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
2755             {"force-share", no_argument, 0, 'U'},
2756             {0, 0, 0, 0}
2757         };
2758         c = getopt_long(argc, argv, ":f:hU",
2759                         long_options, &option_index);
2760         if (c == -1) {
2761             break;
2762         }
2763         switch (c) {
2764         case ':':
2765             missing_argument(argv[optind - 1]);
2766             break;
2767         case '?':
2768             unrecognized_option(argv[optind - 1]);
2769             break;
2770         case 'h':
2771             help();
2772             break;
2773         case 'f':
2774             fmt = optarg;
2775             break;
2776         case 'U':
2777             force_share = true;
2778             break;
2779         case OPTION_OUTPUT:
2780             output = optarg;
2781             break;
2782         case OPTION_OBJECT: {
2783             QemuOpts *opts;
2784             opts = qemu_opts_parse_noisily(&qemu_object_opts,
2785                                            optarg, true);
2786             if (!opts) {
2787                 return 1;
2788             }
2789         }   break;
2790         case OPTION_IMAGE_OPTS:
2791             image_opts = true;
2792             break;
2793         }
2794     }
2795     if (optind != argc - 1) {
2796         error_exit("Expecting one image file name");
2797     }
2798     filename = argv[optind];
2799 
2800     if (output && !strcmp(output, "json")) {
2801         output_format = OFORMAT_JSON;
2802     } else if (output && !strcmp(output, "human")) {
2803         output_format = OFORMAT_HUMAN;
2804     } else if (output) {
2805         error_report("--output must be used with human or json as argument.");
2806         return 1;
2807     }
2808 
2809     if (qemu_opts_foreach(&qemu_object_opts,
2810                           user_creatable_add_opts_foreach,
2811                           NULL, NULL)) {
2812         return 1;
2813     }
2814 
2815     blk = img_open(image_opts, filename, fmt, 0, false, false, force_share);
2816     if (!blk) {
2817         return 1;
2818     }
2819     bs = blk_bs(blk);
2820 
2821     if (output_format == OFORMAT_HUMAN) {
2822         printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2823     }
2824 
2825     length = blk_getlength(blk);
2826     while (curr.start + curr.length < length) {
2827         int64_t offset = curr.start + curr.length;
2828         int64_t n;
2829 
2830         /* Probe up to 1 GiB at a time.  */
2831         n = QEMU_ALIGN_DOWN(MIN(1 << 30, length - offset), BDRV_SECTOR_SIZE);
2832         ret = get_block_status(bs, offset, n, &next);
2833 
2834         if (ret < 0) {
2835             error_report("Could not read file metadata: %s", strerror(-ret));
2836             goto out;
2837         }
2838 
2839         if (entry_mergeable(&curr, &next)) {
2840             curr.length += next.length;
2841             continue;
2842         }
2843 
2844         if (curr.length > 0) {
2845             dump_map_entry(output_format, &curr, &next);
2846         }
2847         curr = next;
2848     }
2849 
2850     dump_map_entry(output_format, &curr, NULL);
2851 
2852 out:
2853     blk_unref(blk);
2854     return ret < 0;
2855 }
2856 
2857 #define SNAPSHOT_LIST   1
2858 #define SNAPSHOT_CREATE 2
2859 #define SNAPSHOT_APPLY  3
2860 #define SNAPSHOT_DELETE 4
2861 
2862 static int img_snapshot(int argc, char **argv)
2863 {
2864     BlockBackend *blk;
2865     BlockDriverState *bs;
2866     QEMUSnapshotInfo sn;
2867     char *filename, *snapshot_name = NULL;
2868     int c, ret = 0, bdrv_oflags;
2869     int action = 0;
2870     qemu_timeval tv;
2871     bool quiet = false;
2872     Error *err = NULL;
2873     bool image_opts = false;
2874     bool force_share = false;
2875 
2876     bdrv_oflags = BDRV_O_RDWR;
2877     /* Parse commandline parameters */
2878     for(;;) {
2879         static const struct option long_options[] = {
2880             {"help", no_argument, 0, 'h'},
2881             {"object", required_argument, 0, OPTION_OBJECT},
2882             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
2883             {"force-share", no_argument, 0, 'U'},
2884             {0, 0, 0, 0}
2885         };
2886         c = getopt_long(argc, argv, ":la:c:d:hqU",
2887                         long_options, NULL);
2888         if (c == -1) {
2889             break;
2890         }
2891         switch(c) {
2892         case ':':
2893             missing_argument(argv[optind - 1]);
2894             break;
2895         case '?':
2896             unrecognized_option(argv[optind - 1]);
2897             break;
2898         case 'h':
2899             help();
2900             return 0;
2901         case 'l':
2902             if (action) {
2903                 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
2904                 return 0;
2905             }
2906             action = SNAPSHOT_LIST;
2907             bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
2908             break;
2909         case 'a':
2910             if (action) {
2911                 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
2912                 return 0;
2913             }
2914             action = SNAPSHOT_APPLY;
2915             snapshot_name = optarg;
2916             break;
2917         case 'c':
2918             if (action) {
2919                 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
2920                 return 0;
2921             }
2922             action = SNAPSHOT_CREATE;
2923             snapshot_name = optarg;
2924             break;
2925         case 'd':
2926             if (action) {
2927                 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
2928                 return 0;
2929             }
2930             action = SNAPSHOT_DELETE;
2931             snapshot_name = optarg;
2932             break;
2933         case 'q':
2934             quiet = true;
2935             break;
2936         case 'U':
2937             force_share = true;
2938             break;
2939         case OPTION_OBJECT: {
2940             QemuOpts *opts;
2941             opts = qemu_opts_parse_noisily(&qemu_object_opts,
2942                                            optarg, true);
2943             if (!opts) {
2944                 return 1;
2945             }
2946         }   break;
2947         case OPTION_IMAGE_OPTS:
2948             image_opts = true;
2949             break;
2950         }
2951     }
2952 
2953     if (optind != argc - 1) {
2954         error_exit("Expecting one image file name");
2955     }
2956     filename = argv[optind++];
2957 
2958     if (qemu_opts_foreach(&qemu_object_opts,
2959                           user_creatable_add_opts_foreach,
2960                           NULL, NULL)) {
2961         return 1;
2962     }
2963 
2964     /* Open the image */
2965     blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet,
2966                    force_share);
2967     if (!blk) {
2968         return 1;
2969     }
2970     bs = blk_bs(blk);
2971 
2972     /* Perform the requested action */
2973     switch(action) {
2974     case SNAPSHOT_LIST:
2975         dump_snapshots(bs);
2976         break;
2977 
2978     case SNAPSHOT_CREATE:
2979         memset(&sn, 0, sizeof(sn));
2980         pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
2981 
2982         qemu_gettimeofday(&tv);
2983         sn.date_sec = tv.tv_sec;
2984         sn.date_nsec = tv.tv_usec * 1000;
2985 
2986         ret = bdrv_snapshot_create(bs, &sn);
2987         if (ret) {
2988             error_report("Could not create snapshot '%s': %d (%s)",
2989                 snapshot_name, ret, strerror(-ret));
2990         }
2991         break;
2992 
2993     case SNAPSHOT_APPLY:
2994         ret = bdrv_snapshot_goto(bs, snapshot_name, &err);
2995         if (ret) {
2996             error_reportf_err(err, "Could not apply snapshot '%s': ",
2997                               snapshot_name);
2998         }
2999         break;
3000 
3001     case SNAPSHOT_DELETE:
3002         bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
3003         if (err) {
3004             error_reportf_err(err, "Could not delete snapshot '%s': ",
3005                               snapshot_name);
3006             ret = 1;
3007         }
3008         break;
3009     }
3010 
3011     /* Cleanup */
3012     blk_unref(blk);
3013     if (ret) {
3014         return 1;
3015     }
3016     return 0;
3017 }
3018 
3019 static int img_rebase(int argc, char **argv)
3020 {
3021     BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
3022     uint8_t *buf_old = NULL;
3023     uint8_t *buf_new = NULL;
3024     BlockDriverState *bs = NULL;
3025     char *filename;
3026     const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
3027     int c, flags, src_flags, ret;
3028     bool writethrough, src_writethrough;
3029     int unsafe = 0;
3030     bool force_share = false;
3031     int progress = 0;
3032     bool quiet = false;
3033     Error *local_err = NULL;
3034     bool image_opts = false;
3035 
3036     /* Parse commandline parameters */
3037     fmt = NULL;
3038     cache = BDRV_DEFAULT_CACHE;
3039     src_cache = BDRV_DEFAULT_CACHE;
3040     out_baseimg = NULL;
3041     out_basefmt = NULL;
3042     for(;;) {
3043         static const struct option long_options[] = {
3044             {"help", no_argument, 0, 'h'},
3045             {"object", required_argument, 0, OPTION_OBJECT},
3046             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
3047             {"force-share", no_argument, 0, 'U'},
3048             {0, 0, 0, 0}
3049         };
3050         c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU",
3051                         long_options, NULL);
3052         if (c == -1) {
3053             break;
3054         }
3055         switch(c) {
3056         case ':':
3057             missing_argument(argv[optind - 1]);
3058             break;
3059         case '?':
3060             unrecognized_option(argv[optind - 1]);
3061             break;
3062         case 'h':
3063             help();
3064             return 0;
3065         case 'f':
3066             fmt = optarg;
3067             break;
3068         case 'F':
3069             out_basefmt = optarg;
3070             break;
3071         case 'b':
3072             out_baseimg = optarg;
3073             break;
3074         case 'u':
3075             unsafe = 1;
3076             break;
3077         case 'p':
3078             progress = 1;
3079             break;
3080         case 't':
3081             cache = optarg;
3082             break;
3083         case 'T':
3084             src_cache = optarg;
3085             break;
3086         case 'q':
3087             quiet = true;
3088             break;
3089         case OPTION_OBJECT: {
3090             QemuOpts *opts;
3091             opts = qemu_opts_parse_noisily(&qemu_object_opts,
3092                                            optarg, true);
3093             if (!opts) {
3094                 return 1;
3095             }
3096         }   break;
3097         case OPTION_IMAGE_OPTS:
3098             image_opts = true;
3099             break;
3100         case 'U':
3101             force_share = true;
3102             break;
3103         }
3104     }
3105 
3106     if (quiet) {
3107         progress = 0;
3108     }
3109 
3110     if (optind != argc - 1) {
3111         error_exit("Expecting one image file name");
3112     }
3113     if (!unsafe && !out_baseimg) {
3114         error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
3115     }
3116     filename = argv[optind++];
3117 
3118     if (qemu_opts_foreach(&qemu_object_opts,
3119                           user_creatable_add_opts_foreach,
3120                           NULL, NULL)) {
3121         return 1;
3122     }
3123 
3124     qemu_progress_init(progress, 2.0);
3125     qemu_progress_print(0, 100);
3126 
3127     flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
3128     ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
3129     if (ret < 0) {
3130         error_report("Invalid cache option: %s", cache);
3131         goto out;
3132     }
3133 
3134     src_flags = 0;
3135     ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
3136     if (ret < 0) {
3137         error_report("Invalid source cache option: %s", src_cache);
3138         goto out;
3139     }
3140 
3141     /* The source files are opened read-only, don't care about WCE */
3142     assert((src_flags & BDRV_O_RDWR) == 0);
3143     (void) src_writethrough;
3144 
3145     /*
3146      * Open the images.
3147      *
3148      * Ignore the old backing file for unsafe rebase in case we want to correct
3149      * the reference to a renamed or moved backing file.
3150      */
3151     blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3152                    false);
3153     if (!blk) {
3154         ret = -1;
3155         goto out;
3156     }
3157     bs = blk_bs(blk);
3158 
3159     if (out_basefmt != NULL) {
3160         if (bdrv_find_format(out_basefmt) == NULL) {
3161             error_report("Invalid format name: '%s'", out_basefmt);
3162             ret = -1;
3163             goto out;
3164         }
3165     }
3166 
3167     /* For safe rebasing we need to compare old and new backing file */
3168     if (!unsafe) {
3169         char backing_name[PATH_MAX];
3170         QDict *options = NULL;
3171 
3172         if (bs->backing_format[0] != '\0') {
3173             options = qdict_new();
3174             qdict_put_str(options, "driver", bs->backing_format);
3175         }
3176 
3177         if (force_share) {
3178             if (!options) {
3179                 options = qdict_new();
3180             }
3181             qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
3182         }
3183         bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
3184         blk_old_backing = blk_new_open(backing_name, NULL,
3185                                        options, src_flags, &local_err);
3186         if (!blk_old_backing) {
3187             error_reportf_err(local_err,
3188                               "Could not open old backing file '%s': ",
3189                               backing_name);
3190             ret = -1;
3191             goto out;
3192         }
3193 
3194         if (out_baseimg[0]) {
3195             options = qdict_new();
3196             if (out_basefmt) {
3197                 qdict_put_str(options, "driver", out_basefmt);
3198             }
3199             if (force_share) {
3200                 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
3201             }
3202 
3203             blk_new_backing = blk_new_open(out_baseimg, NULL,
3204                                            options, src_flags, &local_err);
3205             if (!blk_new_backing) {
3206                 error_reportf_err(local_err,
3207                                   "Could not open new backing file '%s': ",
3208                                   out_baseimg);
3209                 ret = -1;
3210                 goto out;
3211             }
3212         }
3213     }
3214 
3215     /*
3216      * Check each unallocated cluster in the COW file. If it is unallocated,
3217      * accesses go to the backing file. We must therefore compare this cluster
3218      * in the old and new backing file, and if they differ we need to copy it
3219      * from the old backing file into the COW file.
3220      *
3221      * If qemu-img crashes during this step, no harm is done. The content of
3222      * the image is the same as the original one at any time.
3223      */
3224     if (!unsafe) {
3225         int64_t size;
3226         int64_t old_backing_size;
3227         int64_t new_backing_size = 0;
3228         uint64_t offset;
3229         int64_t n;
3230         float local_progress = 0;
3231 
3232         buf_old = blk_blockalign(blk, IO_BUF_SIZE);
3233         buf_new = blk_blockalign(blk, IO_BUF_SIZE);
3234 
3235         size = blk_getlength(blk);
3236         if (size < 0) {
3237             error_report("Could not get size of '%s': %s",
3238                          filename, strerror(-size));
3239             ret = -1;
3240             goto out;
3241         }
3242         old_backing_size = blk_getlength(blk_old_backing);
3243         if (old_backing_size < 0) {
3244             char backing_name[PATH_MAX];
3245 
3246             bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
3247             error_report("Could not get size of '%s': %s",
3248                          backing_name, strerror(-old_backing_size));
3249             ret = -1;
3250             goto out;
3251         }
3252         if (blk_new_backing) {
3253             new_backing_size = blk_getlength(blk_new_backing);
3254             if (new_backing_size < 0) {
3255                 error_report("Could not get size of '%s': %s",
3256                              out_baseimg, strerror(-new_backing_size));
3257                 ret = -1;
3258                 goto out;
3259             }
3260         }
3261 
3262         if (size != 0) {
3263             local_progress = (float)100 / (size / MIN(size, IO_BUF_SIZE));
3264         }
3265 
3266         for (offset = 0; offset < size; offset += n) {
3267             /* How many bytes can we handle with the next read? */
3268             n = MIN(IO_BUF_SIZE, size - offset);
3269 
3270             /* If the cluster is allocated, we don't need to take action */
3271             ret = bdrv_is_allocated(bs, offset, n, &n);
3272             if (ret < 0) {
3273                 error_report("error while reading image metadata: %s",
3274                              strerror(-ret));
3275                 goto out;
3276             }
3277             if (ret) {
3278                 continue;
3279             }
3280 
3281             /*
3282              * Read old and new backing file and take into consideration that
3283              * backing files may be smaller than the COW image.
3284              */
3285             if (offset >= old_backing_size) {
3286                 memset(buf_old, 0, n);
3287             } else {
3288                 if (offset + n > old_backing_size) {
3289                     n = old_backing_size - offset;
3290                 }
3291 
3292                 ret = blk_pread(blk_old_backing, offset, buf_old, n);
3293                 if (ret < 0) {
3294                     error_report("error while reading from old backing file");
3295                     goto out;
3296                 }
3297             }
3298 
3299             if (offset >= new_backing_size || !blk_new_backing) {
3300                 memset(buf_new, 0, n);
3301             } else {
3302                 if (offset + n > new_backing_size) {
3303                     n = new_backing_size - offset;
3304                 }
3305 
3306                 ret = blk_pread(blk_new_backing, offset, buf_new, n);
3307                 if (ret < 0) {
3308                     error_report("error while reading from new backing file");
3309                     goto out;
3310                 }
3311             }
3312 
3313             /* If they differ, we need to write to the COW file */
3314             uint64_t written = 0;
3315 
3316             while (written < n) {
3317                 int64_t pnum;
3318 
3319                 if (compare_buffers(buf_old + written, buf_new + written,
3320                                     n - written, &pnum))
3321                 {
3322                     ret = blk_pwrite(blk, offset + written,
3323                                      buf_old + written, pnum, 0);
3324                     if (ret < 0) {
3325                         error_report("Error while writing to COW image: %s",
3326                             strerror(-ret));
3327                         goto out;
3328                     }
3329                 }
3330 
3331                 written += pnum;
3332             }
3333             qemu_progress_print(local_progress, 100);
3334         }
3335     }
3336 
3337     /*
3338      * Change the backing file. All clusters that are different from the old
3339      * backing file are overwritten in the COW file now, so the visible content
3340      * doesn't change when we switch the backing file.
3341      */
3342     if (out_baseimg && *out_baseimg) {
3343         ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3344     } else {
3345         ret = bdrv_change_backing_file(bs, NULL, NULL);
3346     }
3347 
3348     if (ret == -ENOSPC) {
3349         error_report("Could not change the backing file to '%s': No "
3350                      "space left in the file header", out_baseimg);
3351     } else if (ret < 0) {
3352         error_report("Could not change the backing file to '%s': %s",
3353             out_baseimg, strerror(-ret));
3354     }
3355 
3356     qemu_progress_print(100, 0);
3357     /*
3358      * TODO At this point it is possible to check if any clusters that are
3359      * allocated in the COW file are the same in the backing file. If so, they
3360      * could be dropped from the COW file. Don't do this before switching the
3361      * backing file, in case of a crash this would lead to corruption.
3362      */
3363 out:
3364     qemu_progress_end();
3365     /* Cleanup */
3366     if (!unsafe) {
3367         blk_unref(blk_old_backing);
3368         blk_unref(blk_new_backing);
3369     }
3370     qemu_vfree(buf_old);
3371     qemu_vfree(buf_new);
3372 
3373     blk_unref(blk);
3374     if (ret) {
3375         return 1;
3376     }
3377     return 0;
3378 }
3379 
3380 static int img_resize(int argc, char **argv)
3381 {
3382     Error *err = NULL;
3383     int c, ret, relative;
3384     const char *filename, *fmt, *size;
3385     int64_t n, total_size, current_size;
3386     bool quiet = false;
3387     BlockBackend *blk = NULL;
3388     PreallocMode prealloc = PREALLOC_MODE_OFF;
3389     QemuOpts *param;
3390 
3391     static QemuOptsList resize_options = {
3392         .name = "resize_options",
3393         .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3394         .desc = {
3395             {
3396                 .name = BLOCK_OPT_SIZE,
3397                 .type = QEMU_OPT_SIZE,
3398                 .help = "Virtual disk size"
3399             }, {
3400                 /* end of list */
3401             }
3402         },
3403     };
3404     bool image_opts = false;
3405     bool shrink = false;
3406 
3407     /* Remove size from argv manually so that negative numbers are not treated
3408      * as options by getopt. */
3409     if (argc < 3) {
3410         error_exit("Not enough arguments");
3411         return 1;
3412     }
3413 
3414     size = argv[--argc];
3415 
3416     /* Parse getopt arguments */
3417     fmt = NULL;
3418     for(;;) {
3419         static const struct option long_options[] = {
3420             {"help", no_argument, 0, 'h'},
3421             {"object", required_argument, 0, OPTION_OBJECT},
3422             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
3423             {"preallocation", required_argument, 0, OPTION_PREALLOCATION},
3424             {"shrink", no_argument, 0, OPTION_SHRINK},
3425             {0, 0, 0, 0}
3426         };
3427         c = getopt_long(argc, argv, ":f:hq",
3428                         long_options, NULL);
3429         if (c == -1) {
3430             break;
3431         }
3432         switch(c) {
3433         case ':':
3434             missing_argument(argv[optind - 1]);
3435             break;
3436         case '?':
3437             unrecognized_option(argv[optind - 1]);
3438             break;
3439         case 'h':
3440             help();
3441             break;
3442         case 'f':
3443             fmt = optarg;
3444             break;
3445         case 'q':
3446             quiet = true;
3447             break;
3448         case OPTION_OBJECT: {
3449             QemuOpts *opts;
3450             opts = qemu_opts_parse_noisily(&qemu_object_opts,
3451                                            optarg, true);
3452             if (!opts) {
3453                 return 1;
3454             }
3455         }   break;
3456         case OPTION_IMAGE_OPTS:
3457             image_opts = true;
3458             break;
3459         case OPTION_PREALLOCATION:
3460             prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg,
3461                                        PREALLOC_MODE__MAX, NULL);
3462             if (prealloc == PREALLOC_MODE__MAX) {
3463                 error_report("Invalid preallocation mode '%s'", optarg);
3464                 return 1;
3465             }
3466             break;
3467         case OPTION_SHRINK:
3468             shrink = true;
3469             break;
3470         }
3471     }
3472     if (optind != argc - 1) {
3473         error_exit("Expecting one image file name");
3474     }
3475     filename = argv[optind++];
3476 
3477     if (qemu_opts_foreach(&qemu_object_opts,
3478                           user_creatable_add_opts_foreach,
3479                           NULL, NULL)) {
3480         return 1;
3481     }
3482 
3483     /* Choose grow, shrink, or absolute resize mode */
3484     switch (size[0]) {
3485     case '+':
3486         relative = 1;
3487         size++;
3488         break;
3489     case '-':
3490         relative = -1;
3491         size++;
3492         break;
3493     default:
3494         relative = 0;
3495         break;
3496     }
3497 
3498     /* Parse size */
3499     param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
3500     qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
3501     if (err) {
3502         error_report_err(err);
3503         ret = -1;
3504         qemu_opts_del(param);
3505         goto out;
3506     }
3507     n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3508     qemu_opts_del(param);
3509 
3510     blk = img_open(image_opts, filename, fmt,
3511                    BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet,
3512                    false);
3513     if (!blk) {
3514         ret = -1;
3515         goto out;
3516     }
3517 
3518     current_size = blk_getlength(blk);
3519     if (current_size < 0) {
3520         error_report("Failed to inquire current image length: %s",
3521                      strerror(-current_size));
3522         ret = -1;
3523         goto out;
3524     }
3525 
3526     if (relative) {
3527         total_size = current_size + n * relative;
3528     } else {
3529         total_size = n;
3530     }
3531     if (total_size <= 0) {
3532         error_report("New image size must be positive");
3533         ret = -1;
3534         goto out;
3535     }
3536 
3537     if (total_size <= current_size && prealloc != PREALLOC_MODE_OFF) {
3538         error_report("Preallocation can only be used for growing images");
3539         ret = -1;
3540         goto out;
3541     }
3542 
3543     if (total_size < current_size && !shrink) {
3544         warn_report("Shrinking an image will delete all data beyond the "
3545                     "shrunken image's end. Before performing such an "
3546                     "operation, make sure there is no important data there.");
3547 
3548         if (g_strcmp0(bdrv_get_format_name(blk_bs(blk)), "raw") != 0) {
3549             error_report(
3550               "Use the --shrink option to perform a shrink operation.");
3551             ret = -1;
3552             goto out;
3553         } else {
3554             warn_report("Using the --shrink option will suppress this message. "
3555                         "Note that future versions of qemu-img may refuse to "
3556                         "shrink images without this option.");
3557         }
3558     }
3559 
3560     ret = blk_truncate(blk, total_size, prealloc, &err);
3561     if (!ret) {
3562         qprintf(quiet, "Image resized.\n");
3563     } else {
3564         error_report_err(err);
3565     }
3566 out:
3567     blk_unref(blk);
3568     if (ret) {
3569         return 1;
3570     }
3571     return 0;
3572 }
3573 
3574 static void amend_status_cb(BlockDriverState *bs,
3575                             int64_t offset, int64_t total_work_size,
3576                             void *opaque)
3577 {
3578     qemu_progress_print(100.f * offset / total_work_size, 0);
3579 }
3580 
3581 static int img_amend(int argc, char **argv)
3582 {
3583     Error *err = NULL;
3584     int c, ret = 0;
3585     char *options = NULL;
3586     QemuOptsList *create_opts = NULL;
3587     QemuOpts *opts = NULL;
3588     const char *fmt = NULL, *filename, *cache;
3589     int flags;
3590     bool writethrough;
3591     bool quiet = false, progress = false;
3592     BlockBackend *blk = NULL;
3593     BlockDriverState *bs = NULL;
3594     bool image_opts = false;
3595 
3596     cache = BDRV_DEFAULT_CACHE;
3597     for (;;) {
3598         static const struct option long_options[] = {
3599             {"help", no_argument, 0, 'h'},
3600             {"object", required_argument, 0, OPTION_OBJECT},
3601             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
3602             {0, 0, 0, 0}
3603         };
3604         c = getopt_long(argc, argv, ":ho:f:t:pq",
3605                         long_options, NULL);
3606         if (c == -1) {
3607             break;
3608         }
3609 
3610         switch (c) {
3611         case ':':
3612             missing_argument(argv[optind - 1]);
3613             break;
3614         case '?':
3615             unrecognized_option(argv[optind - 1]);
3616             break;
3617         case 'h':
3618             help();
3619             break;
3620         case 'o':
3621             if (!is_valid_option_list(optarg)) {
3622                 error_report("Invalid option list: %s", optarg);
3623                 ret = -1;
3624                 goto out_no_progress;
3625             }
3626             if (!options) {
3627                 options = g_strdup(optarg);
3628             } else {
3629                 char *old_options = options;
3630                 options = g_strdup_printf("%s,%s", options, optarg);
3631                 g_free(old_options);
3632             }
3633             break;
3634         case 'f':
3635             fmt = optarg;
3636             break;
3637         case 't':
3638             cache = optarg;
3639             break;
3640         case 'p':
3641             progress = true;
3642             break;
3643         case 'q':
3644             quiet = true;
3645             break;
3646         case OPTION_OBJECT:
3647             opts = qemu_opts_parse_noisily(&qemu_object_opts,
3648                                            optarg, true);
3649             if (!opts) {
3650                 ret = -1;
3651                 goto out_no_progress;
3652             }
3653             break;
3654         case OPTION_IMAGE_OPTS:
3655             image_opts = true;
3656             break;
3657         }
3658     }
3659 
3660     if (!options) {
3661         error_exit("Must specify options (-o)");
3662     }
3663 
3664     if (qemu_opts_foreach(&qemu_object_opts,
3665                           user_creatable_add_opts_foreach,
3666                           NULL, NULL)) {
3667         ret = -1;
3668         goto out_no_progress;
3669     }
3670 
3671     if (quiet) {
3672         progress = false;
3673     }
3674     qemu_progress_init(progress, 1.0);
3675 
3676     filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3677     if (fmt && has_help_option(options)) {
3678         /* If a format is explicitly specified (and possibly no filename is
3679          * given), print option help here */
3680         ret = print_block_option_help(filename, fmt);
3681         goto out;
3682     }
3683 
3684     if (optind != argc - 1) {
3685         error_report("Expecting one image file name");
3686         ret = -1;
3687         goto out;
3688     }
3689 
3690     flags = BDRV_O_RDWR;
3691     ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
3692     if (ret < 0) {
3693         error_report("Invalid cache option: %s", cache);
3694         goto out;
3695     }
3696 
3697     blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3698                    false);
3699     if (!blk) {
3700         ret = -1;
3701         goto out;
3702     }
3703     bs = blk_bs(blk);
3704 
3705     fmt = bs->drv->format_name;
3706 
3707     if (has_help_option(options)) {
3708         /* If the format was auto-detected, print option help here */
3709         ret = print_block_option_help(filename, fmt);
3710         goto out;
3711     }
3712 
3713     if (!bs->drv->create_opts) {
3714         error_report("Format driver '%s' does not support any options to amend",
3715                      fmt);
3716         ret = -1;
3717         goto out;
3718     }
3719 
3720     create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
3721     opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
3722     qemu_opts_do_parse(opts, options, NULL, &err);
3723     if (err) {
3724         error_report_err(err);
3725         ret = -1;
3726         goto out;
3727     }
3728 
3729     /* In case the driver does not call amend_status_cb() */
3730     qemu_progress_print(0.f, 0);
3731     ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL);
3732     qemu_progress_print(100.f, 0);
3733     if (ret < 0) {
3734         error_report("Error while amending options: %s", strerror(-ret));
3735         goto out;
3736     }
3737 
3738 out:
3739     qemu_progress_end();
3740 
3741 out_no_progress:
3742     blk_unref(blk);
3743     qemu_opts_del(opts);
3744     qemu_opts_free(create_opts);
3745     g_free(options);
3746 
3747     if (ret) {
3748         return 1;
3749     }
3750     return 0;
3751 }
3752 
3753 typedef struct BenchData {
3754     BlockBackend *blk;
3755     uint64_t image_size;
3756     bool write;
3757     int bufsize;
3758     int step;
3759     int nrreq;
3760     int n;
3761     int flush_interval;
3762     bool drain_on_flush;
3763     uint8_t *buf;
3764     QEMUIOVector *qiov;
3765 
3766     int in_flight;
3767     bool in_flush;
3768     uint64_t offset;
3769 } BenchData;
3770 
3771 static void bench_undrained_flush_cb(void *opaque, int ret)
3772 {
3773     if (ret < 0) {
3774         error_report("Failed flush request: %s", strerror(-ret));
3775         exit(EXIT_FAILURE);
3776     }
3777 }
3778 
3779 static void bench_cb(void *opaque, int ret)
3780 {
3781     BenchData *b = opaque;
3782     BlockAIOCB *acb;
3783 
3784     if (ret < 0) {
3785         error_report("Failed request: %s", strerror(-ret));
3786         exit(EXIT_FAILURE);
3787     }
3788 
3789     if (b->in_flush) {
3790         /* Just finished a flush with drained queue: Start next requests */
3791         assert(b->in_flight == 0);
3792         b->in_flush = false;
3793     } else if (b->in_flight > 0) {
3794         int remaining = b->n - b->in_flight;
3795 
3796         b->n--;
3797         b->in_flight--;
3798 
3799         /* Time for flush? Drain queue if requested, then flush */
3800         if (b->flush_interval && remaining % b->flush_interval == 0) {
3801             if (!b->in_flight || !b->drain_on_flush) {
3802                 BlockCompletionFunc *cb;
3803 
3804                 if (b->drain_on_flush) {
3805                     b->in_flush = true;
3806                     cb = bench_cb;
3807                 } else {
3808                     cb = bench_undrained_flush_cb;
3809                 }
3810 
3811                 acb = blk_aio_flush(b->blk, cb, b);
3812                 if (!acb) {
3813                     error_report("Failed to issue flush request");
3814                     exit(EXIT_FAILURE);
3815                 }
3816             }
3817             if (b->drain_on_flush) {
3818                 return;
3819             }
3820         }
3821     }
3822 
3823     while (b->n > b->in_flight && b->in_flight < b->nrreq) {
3824         int64_t offset = b->offset;
3825         /* blk_aio_* might look for completed I/Os and kick bench_cb
3826          * again, so make sure this operation is counted by in_flight
3827          * and b->offset is ready for the next submission.
3828          */
3829         b->in_flight++;
3830         b->offset += b->step;
3831         b->offset %= b->image_size;
3832         if (b->write) {
3833             acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b);
3834         } else {
3835             acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b);
3836         }
3837         if (!acb) {
3838             error_report("Failed to issue request");
3839             exit(EXIT_FAILURE);
3840         }
3841     }
3842 }
3843 
3844 static int img_bench(int argc, char **argv)
3845 {
3846     int c, ret = 0;
3847     const char *fmt = NULL, *filename;
3848     bool quiet = false;
3849     bool image_opts = false;
3850     bool is_write = false;
3851     int count = 75000;
3852     int depth = 64;
3853     int64_t offset = 0;
3854     size_t bufsize = 4096;
3855     int pattern = 0;
3856     size_t step = 0;
3857     int flush_interval = 0;
3858     bool drain_on_flush = true;
3859     int64_t image_size;
3860     BlockBackend *blk = NULL;
3861     BenchData data = {};
3862     int flags = 0;
3863     bool writethrough = false;
3864     struct timeval t1, t2;
3865     int i;
3866     bool force_share = false;
3867     size_t buf_size;
3868 
3869     for (;;) {
3870         static const struct option long_options[] = {
3871             {"help", no_argument, 0, 'h'},
3872             {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL},
3873             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
3874             {"pattern", required_argument, 0, OPTION_PATTERN},
3875             {"no-drain", no_argument, 0, OPTION_NO_DRAIN},
3876             {"force-share", no_argument, 0, 'U'},
3877             {0, 0, 0, 0}
3878         };
3879         c = getopt_long(argc, argv, ":hc:d:f:no:qs:S:t:wU", long_options, NULL);
3880         if (c == -1) {
3881             break;
3882         }
3883 
3884         switch (c) {
3885         case ':':
3886             missing_argument(argv[optind - 1]);
3887             break;
3888         case '?':
3889             unrecognized_option(argv[optind - 1]);
3890             break;
3891         case 'h':
3892             help();
3893             break;
3894         case 'c':
3895         {
3896             unsigned long res;
3897 
3898             if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
3899                 error_report("Invalid request count specified");
3900                 return 1;
3901             }
3902             count = res;
3903             break;
3904         }
3905         case 'd':
3906         {
3907             unsigned long res;
3908 
3909             if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
3910                 error_report("Invalid queue depth specified");
3911                 return 1;
3912             }
3913             depth = res;
3914             break;
3915         }
3916         case 'f':
3917             fmt = optarg;
3918             break;
3919         case 'n':
3920             flags |= BDRV_O_NATIVE_AIO;
3921             break;
3922         case 'o':
3923         {
3924             offset = cvtnum(optarg);
3925             if (offset < 0) {
3926                 error_report("Invalid offset specified");
3927                 return 1;
3928             }
3929             break;
3930         }
3931             break;
3932         case 'q':
3933             quiet = true;
3934             break;
3935         case 's':
3936         {
3937             int64_t sval;
3938 
3939             sval = cvtnum(optarg);
3940             if (sval < 0 || sval > INT_MAX) {
3941                 error_report("Invalid buffer size specified");
3942                 return 1;
3943             }
3944 
3945             bufsize = sval;
3946             break;
3947         }
3948         case 'S':
3949         {
3950             int64_t sval;
3951 
3952             sval = cvtnum(optarg);
3953             if (sval < 0 || sval > INT_MAX) {
3954                 error_report("Invalid step size specified");
3955                 return 1;
3956             }
3957 
3958             step = sval;
3959             break;
3960         }
3961         case 't':
3962             ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
3963             if (ret < 0) {
3964                 error_report("Invalid cache mode");
3965                 ret = -1;
3966                 goto out;
3967             }
3968             break;
3969         case 'w':
3970             flags |= BDRV_O_RDWR;
3971             is_write = true;
3972             break;
3973         case 'U':
3974             force_share = true;
3975             break;
3976         case OPTION_PATTERN:
3977         {
3978             unsigned long res;
3979 
3980             if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
3981                 error_report("Invalid pattern byte specified");
3982                 return 1;
3983             }
3984             pattern = res;
3985             break;
3986         }
3987         case OPTION_FLUSH_INTERVAL:
3988         {
3989             unsigned long res;
3990 
3991             if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
3992                 error_report("Invalid flush interval specified");
3993                 return 1;
3994             }
3995             flush_interval = res;
3996             break;
3997         }
3998         case OPTION_NO_DRAIN:
3999             drain_on_flush = false;
4000             break;
4001         case OPTION_IMAGE_OPTS:
4002             image_opts = true;
4003             break;
4004         }
4005     }
4006 
4007     if (optind != argc - 1) {
4008         error_exit("Expecting one image file name");
4009     }
4010     filename = argv[argc - 1];
4011 
4012     if (!is_write && flush_interval) {
4013         error_report("--flush-interval is only available in write tests");
4014         ret = -1;
4015         goto out;
4016     }
4017     if (flush_interval && flush_interval < depth) {
4018         error_report("Flush interval can't be smaller than depth");
4019         ret = -1;
4020         goto out;
4021     }
4022 
4023     blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
4024                    force_share);
4025     if (!blk) {
4026         ret = -1;
4027         goto out;
4028     }
4029 
4030     image_size = blk_getlength(blk);
4031     if (image_size < 0) {
4032         ret = image_size;
4033         goto out;
4034     }
4035 
4036     data = (BenchData) {
4037         .blk            = blk,
4038         .image_size     = image_size,
4039         .bufsize        = bufsize,
4040         .step           = step ?: bufsize,
4041         .nrreq          = depth,
4042         .n              = count,
4043         .offset         = offset,
4044         .write          = is_write,
4045         .flush_interval = flush_interval,
4046         .drain_on_flush = drain_on_flush,
4047     };
4048     printf("Sending %d %s requests, %d bytes each, %d in parallel "
4049            "(starting at offset %" PRId64 ", step size %d)\n",
4050            data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
4051            data.offset, data.step);
4052     if (flush_interval) {
4053         printf("Sending flush every %d requests\n", flush_interval);
4054     }
4055 
4056     buf_size = data.nrreq * data.bufsize;
4057     data.buf = blk_blockalign(blk, buf_size);
4058     memset(data.buf, pattern, data.nrreq * data.bufsize);
4059 
4060     blk_register_buf(blk, data.buf, buf_size);
4061 
4062     data.qiov = g_new(QEMUIOVector, data.nrreq);
4063     for (i = 0; i < data.nrreq; i++) {
4064         qemu_iovec_init(&data.qiov[i], 1);
4065         qemu_iovec_add(&data.qiov[i],
4066                        data.buf + i * data.bufsize, data.bufsize);
4067     }
4068 
4069     gettimeofday(&t1, NULL);
4070     bench_cb(&data, 0);
4071 
4072     while (data.n > 0) {
4073         main_loop_wait(false);
4074     }
4075     gettimeofday(&t2, NULL);
4076 
4077     printf("Run completed in %3.3f seconds.\n",
4078            (t2.tv_sec - t1.tv_sec)
4079            + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
4080 
4081 out:
4082     if (data.buf) {
4083         blk_unregister_buf(blk, data.buf);
4084     }
4085     qemu_vfree(data.buf);
4086     blk_unref(blk);
4087 
4088     if (ret) {
4089         return 1;
4090     }
4091     return 0;
4092 }
4093 
4094 #define C_BS      01
4095 #define C_COUNT   02
4096 #define C_IF      04
4097 #define C_OF      010
4098 #define C_SKIP    020
4099 
4100 struct DdInfo {
4101     unsigned int flags;
4102     int64_t count;
4103 };
4104 
4105 struct DdIo {
4106     int bsz;    /* Block size */
4107     char *filename;
4108     uint8_t *buf;
4109     int64_t offset;
4110 };
4111 
4112 struct DdOpts {
4113     const char *name;
4114     int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *);
4115     unsigned int flag;
4116 };
4117 
4118 static int img_dd_bs(const char *arg,
4119                      struct DdIo *in, struct DdIo *out,
4120                      struct DdInfo *dd)
4121 {
4122     int64_t res;
4123 
4124     res = cvtnum(arg);
4125 
4126     if (res <= 0 || res > INT_MAX) {
4127         error_report("invalid number: '%s'", arg);
4128         return 1;
4129     }
4130     in->bsz = out->bsz = res;
4131 
4132     return 0;
4133 }
4134 
4135 static int img_dd_count(const char *arg,
4136                         struct DdIo *in, struct DdIo *out,
4137                         struct DdInfo *dd)
4138 {
4139     dd->count = cvtnum(arg);
4140 
4141     if (dd->count < 0) {
4142         error_report("invalid number: '%s'", arg);
4143         return 1;
4144     }
4145 
4146     return 0;
4147 }
4148 
4149 static int img_dd_if(const char *arg,
4150                      struct DdIo *in, struct DdIo *out,
4151                      struct DdInfo *dd)
4152 {
4153     in->filename = g_strdup(arg);
4154 
4155     return 0;
4156 }
4157 
4158 static int img_dd_of(const char *arg,
4159                      struct DdIo *in, struct DdIo *out,
4160                      struct DdInfo *dd)
4161 {
4162     out->filename = g_strdup(arg);
4163 
4164     return 0;
4165 }
4166 
4167 static int img_dd_skip(const char *arg,
4168                        struct DdIo *in, struct DdIo *out,
4169                        struct DdInfo *dd)
4170 {
4171     in->offset = cvtnum(arg);
4172 
4173     if (in->offset < 0) {
4174         error_report("invalid number: '%s'", arg);
4175         return 1;
4176     }
4177 
4178     return 0;
4179 }
4180 
4181 static int img_dd(int argc, char **argv)
4182 {
4183     int ret = 0;
4184     char *arg = NULL;
4185     char *tmp;
4186     BlockDriver *drv = NULL, *proto_drv = NULL;
4187     BlockBackend *blk1 = NULL, *blk2 = NULL;
4188     QemuOpts *opts = NULL;
4189     QemuOptsList *create_opts = NULL;
4190     Error *local_err = NULL;
4191     bool image_opts = false;
4192     int c, i;
4193     const char *out_fmt = "raw";
4194     const char *fmt = NULL;
4195     int64_t size = 0;
4196     int64_t block_count = 0, out_pos, in_pos;
4197     bool force_share = false;
4198     struct DdInfo dd = {
4199         .flags = 0,
4200         .count = 0,
4201     };
4202     struct DdIo in = {
4203         .bsz = 512, /* Block size is by default 512 bytes */
4204         .filename = NULL,
4205         .buf = NULL,
4206         .offset = 0
4207     };
4208     struct DdIo out = {
4209         .bsz = 512,
4210         .filename = NULL,
4211         .buf = NULL,
4212         .offset = 0
4213     };
4214 
4215     const struct DdOpts options[] = {
4216         { "bs", img_dd_bs, C_BS },
4217         { "count", img_dd_count, C_COUNT },
4218         { "if", img_dd_if, C_IF },
4219         { "of", img_dd_of, C_OF },
4220         { "skip", img_dd_skip, C_SKIP },
4221         { NULL, NULL, 0 }
4222     };
4223     const struct option long_options[] = {
4224         { "help", no_argument, 0, 'h'},
4225         { "object", required_argument, 0, OPTION_OBJECT},
4226         { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
4227         { "force-share", no_argument, 0, 'U'},
4228         { 0, 0, 0, 0 }
4229     };
4230 
4231     while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) {
4232         if (c == EOF) {
4233             break;
4234         }
4235         switch (c) {
4236         case 'O':
4237             out_fmt = optarg;
4238             break;
4239         case 'f':
4240             fmt = optarg;
4241             break;
4242         case ':':
4243             missing_argument(argv[optind - 1]);
4244             break;
4245         case '?':
4246             unrecognized_option(argv[optind - 1]);
4247             break;
4248         case 'h':
4249             help();
4250             break;
4251         case 'U':
4252             force_share = true;
4253             break;
4254         case OPTION_OBJECT:
4255             if (!qemu_opts_parse_noisily(&qemu_object_opts, optarg, true)) {
4256                 ret = -1;
4257                 goto out;
4258             }
4259             break;
4260         case OPTION_IMAGE_OPTS:
4261             image_opts = true;
4262             break;
4263         }
4264     }
4265 
4266     for (i = optind; i < argc; i++) {
4267         int j;
4268         arg = g_strdup(argv[i]);
4269 
4270         tmp = strchr(arg, '=');
4271         if (tmp == NULL) {
4272             error_report("unrecognized operand %s", arg);
4273             ret = -1;
4274             goto out;
4275         }
4276 
4277         *tmp++ = '\0';
4278 
4279         for (j = 0; options[j].name != NULL; j++) {
4280             if (!strcmp(arg, options[j].name)) {
4281                 break;
4282             }
4283         }
4284         if (options[j].name == NULL) {
4285             error_report("unrecognized operand %s", arg);
4286             ret = -1;
4287             goto out;
4288         }
4289 
4290         if (options[j].f(tmp, &in, &out, &dd) != 0) {
4291             ret = -1;
4292             goto out;
4293         }
4294         dd.flags |= options[j].flag;
4295         g_free(arg);
4296         arg = NULL;
4297     }
4298 
4299     if (!(dd.flags & C_IF && dd.flags & C_OF)) {
4300         error_report("Must specify both input and output files");
4301         ret = -1;
4302         goto out;
4303     }
4304 
4305     if (qemu_opts_foreach(&qemu_object_opts,
4306                           user_creatable_add_opts_foreach,
4307                           NULL, NULL)) {
4308         ret = -1;
4309         goto out;
4310     }
4311 
4312     blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
4313                     force_share);
4314 
4315     if (!blk1) {
4316         ret = -1;
4317         goto out;
4318     }
4319 
4320     drv = bdrv_find_format(out_fmt);
4321     if (!drv) {
4322         error_report("Unknown file format");
4323         ret = -1;
4324         goto out;
4325     }
4326     proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
4327 
4328     if (!proto_drv) {
4329         error_report_err(local_err);
4330         ret = -1;
4331         goto out;
4332     }
4333     if (!drv->create_opts) {
4334         error_report("Format driver '%s' does not support image creation",
4335                      drv->format_name);
4336         ret = -1;
4337         goto out;
4338     }
4339     if (!proto_drv->create_opts) {
4340         error_report("Protocol driver '%s' does not support image creation",
4341                      proto_drv->format_name);
4342         ret = -1;
4343         goto out;
4344     }
4345     create_opts = qemu_opts_append(create_opts, drv->create_opts);
4346     create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
4347 
4348     opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4349 
4350     size = blk_getlength(blk1);
4351     if (size < 0) {
4352         error_report("Failed to get size for '%s'", in.filename);
4353         ret = -1;
4354         goto out;
4355     }
4356 
4357     if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
4358         dd.count * in.bsz < size) {
4359         size = dd.count * in.bsz;
4360     }
4361 
4362     /* Overflow means the specified offset is beyond input image's size */
4363     if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4364                               size < in.bsz * in.offset)) {
4365         qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
4366     } else {
4367         qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
4368                             size - in.bsz * in.offset, &error_abort);
4369     }
4370 
4371     ret = bdrv_create(drv, out.filename, opts, &local_err);
4372     if (ret < 0) {
4373         error_reportf_err(local_err,
4374                           "%s: error while creating output image: ",
4375                           out.filename);
4376         ret = -1;
4377         goto out;
4378     }
4379 
4380     /* TODO, we can't honour --image-opts for the target,
4381      * since it needs to be given in a format compatible
4382      * with the bdrv_create() call above which does not
4383      * support image-opts style.
4384      */
4385     blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
4386                          false, false, false);
4387 
4388     if (!blk2) {
4389         ret = -1;
4390         goto out;
4391     }
4392 
4393     if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4394                               size < in.offset * in.bsz)) {
4395         /* We give a warning if the skip option is bigger than the input
4396          * size and create an empty output disk image (i.e. like dd(1)).
4397          */
4398         error_report("%s: cannot skip to specified offset", in.filename);
4399         in_pos = size;
4400     } else {
4401         in_pos = in.offset * in.bsz;
4402     }
4403 
4404     in.buf = g_new(uint8_t, in.bsz);
4405 
4406     for (out_pos = 0; in_pos < size; block_count++) {
4407         int in_ret, out_ret;
4408 
4409         if (in_pos + in.bsz > size) {
4410             in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos);
4411         } else {
4412             in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
4413         }
4414         if (in_ret < 0) {
4415             error_report("error while reading from input image file: %s",
4416                          strerror(-in_ret));
4417             ret = -1;
4418             goto out;
4419         }
4420         in_pos += in_ret;
4421 
4422         out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
4423 
4424         if (out_ret < 0) {
4425             error_report("error while writing to output image file: %s",
4426                          strerror(-out_ret));
4427             ret = -1;
4428             goto out;
4429         }
4430         out_pos += out_ret;
4431     }
4432 
4433 out:
4434     g_free(arg);
4435     qemu_opts_del(opts);
4436     qemu_opts_free(create_opts);
4437     blk_unref(blk1);
4438     blk_unref(blk2);
4439     g_free(in.filename);
4440     g_free(out.filename);
4441     g_free(in.buf);
4442     g_free(out.buf);
4443 
4444     if (ret) {
4445         return 1;
4446     }
4447     return 0;
4448 }
4449 
4450 static void dump_json_block_measure_info(BlockMeasureInfo *info)
4451 {
4452     QString *str;
4453     QObject *obj;
4454     Visitor *v = qobject_output_visitor_new(&obj);
4455 
4456     visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort);
4457     visit_complete(v, &obj);
4458     str = qobject_to_json_pretty(obj);
4459     assert(str != NULL);
4460     printf("%s\n", qstring_get_str(str));
4461     qobject_decref(obj);
4462     visit_free(v);
4463     QDECREF(str);
4464 }
4465 
4466 static int img_measure(int argc, char **argv)
4467 {
4468     static const struct option long_options[] = {
4469         {"help", no_argument, 0, 'h'},
4470         {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
4471         {"object", required_argument, 0, OPTION_OBJECT},
4472         {"output", required_argument, 0, OPTION_OUTPUT},
4473         {"size", required_argument, 0, OPTION_SIZE},
4474         {"force-share", no_argument, 0, 'U'},
4475         {0, 0, 0, 0}
4476     };
4477     OutputFormat output_format = OFORMAT_HUMAN;
4478     BlockBackend *in_blk = NULL;
4479     BlockDriver *drv;
4480     const char *filename = NULL;
4481     const char *fmt = NULL;
4482     const char *out_fmt = "raw";
4483     char *options = NULL;
4484     char *snapshot_name = NULL;
4485     bool force_share = false;
4486     QemuOpts *opts = NULL;
4487     QemuOpts *object_opts = NULL;
4488     QemuOpts *sn_opts = NULL;
4489     QemuOptsList *create_opts = NULL;
4490     bool image_opts = false;
4491     uint64_t img_size = UINT64_MAX;
4492     BlockMeasureInfo *info = NULL;
4493     Error *local_err = NULL;
4494     int ret = 1;
4495     int c;
4496 
4497     while ((c = getopt_long(argc, argv, "hf:O:o:l:U",
4498                             long_options, NULL)) != -1) {
4499         switch (c) {
4500         case '?':
4501         case 'h':
4502             help();
4503             break;
4504         case 'f':
4505             fmt = optarg;
4506             break;
4507         case 'O':
4508             out_fmt = optarg;
4509             break;
4510         case 'o':
4511             if (!is_valid_option_list(optarg)) {
4512                 error_report("Invalid option list: %s", optarg);
4513                 goto out;
4514             }
4515             if (!options) {
4516                 options = g_strdup(optarg);
4517             } else {
4518                 char *old_options = options;
4519                 options = g_strdup_printf("%s,%s", options, optarg);
4520                 g_free(old_options);
4521             }
4522             break;
4523         case 'l':
4524             if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
4525                 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
4526                                                   optarg, false);
4527                 if (!sn_opts) {
4528                     error_report("Failed in parsing snapshot param '%s'",
4529                                  optarg);
4530                     goto out;
4531                 }
4532             } else {
4533                 snapshot_name = optarg;
4534             }
4535             break;
4536         case 'U':
4537             force_share = true;
4538             break;
4539         case OPTION_OBJECT:
4540             object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
4541                                                   optarg, true);
4542             if (!object_opts) {
4543                 goto out;
4544             }
4545             break;
4546         case OPTION_IMAGE_OPTS:
4547             image_opts = true;
4548             break;
4549         case OPTION_OUTPUT:
4550             if (!strcmp(optarg, "json")) {
4551                 output_format = OFORMAT_JSON;
4552             } else if (!strcmp(optarg, "human")) {
4553                 output_format = OFORMAT_HUMAN;
4554             } else {
4555                 error_report("--output must be used with human or json "
4556                              "as argument.");
4557                 goto out;
4558             }
4559             break;
4560         case OPTION_SIZE:
4561         {
4562             int64_t sval;
4563 
4564             sval = cvtnum(optarg);
4565             if (sval < 0) {
4566                 if (sval == -ERANGE) {
4567                     error_report("Image size must be less than 8 EiB!");
4568                 } else {
4569                     error_report("Invalid image size specified! You may use "
4570                                  "k, M, G, T, P or E suffixes for ");
4571                     error_report("kilobytes, megabytes, gigabytes, terabytes, "
4572                                  "petabytes and exabytes.");
4573                 }
4574                 goto out;
4575             }
4576             img_size = (uint64_t)sval;
4577         }
4578         break;
4579         }
4580     }
4581 
4582     if (qemu_opts_foreach(&qemu_object_opts,
4583                           user_creatable_add_opts_foreach,
4584                           NULL, NULL)) {
4585         goto out;
4586     }
4587 
4588     if (argc - optind > 1) {
4589         error_report("At most one filename argument is allowed.");
4590         goto out;
4591     } else if (argc - optind == 1) {
4592         filename = argv[optind];
4593     }
4594 
4595     if (!filename &&
4596         (object_opts || image_opts || fmt || snapshot_name || sn_opts)) {
4597         error_report("--object, --image-opts, -f, and -l "
4598                      "require a filename argument.");
4599         goto out;
4600     }
4601     if (filename && img_size != UINT64_MAX) {
4602         error_report("--size N cannot be used together with a filename.");
4603         goto out;
4604     }
4605     if (!filename && img_size == UINT64_MAX) {
4606         error_report("Either --size N or one filename must be specified.");
4607         goto out;
4608     }
4609 
4610     if (filename) {
4611         in_blk = img_open(image_opts, filename, fmt, 0,
4612                           false, false, force_share);
4613         if (!in_blk) {
4614             goto out;
4615         }
4616 
4617         if (sn_opts) {
4618             bdrv_snapshot_load_tmp(blk_bs(in_blk),
4619                     qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
4620                     qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
4621                     &local_err);
4622         } else if (snapshot_name != NULL) {
4623             bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk),
4624                     snapshot_name, &local_err);
4625         }
4626         if (local_err) {
4627             error_reportf_err(local_err, "Failed to load snapshot: ");
4628             goto out;
4629         }
4630     }
4631 
4632     drv = bdrv_find_format(out_fmt);
4633     if (!drv) {
4634         error_report("Unknown file format '%s'", out_fmt);
4635         goto out;
4636     }
4637     if (!drv->create_opts) {
4638         error_report("Format driver '%s' does not support image creation",
4639                      drv->format_name);
4640         goto out;
4641     }
4642 
4643     create_opts = qemu_opts_append(create_opts, drv->create_opts);
4644     create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts);
4645     opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4646     if (options) {
4647         qemu_opts_do_parse(opts, options, NULL, &local_err);
4648         if (local_err) {
4649             error_report_err(local_err);
4650             error_report("Invalid options for file format '%s'", out_fmt);
4651             goto out;
4652         }
4653     }
4654     if (img_size != UINT64_MAX) {
4655         qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort);
4656     }
4657 
4658     info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err);
4659     if (local_err) {
4660         error_report_err(local_err);
4661         goto out;
4662     }
4663 
4664     if (output_format == OFORMAT_HUMAN) {
4665         printf("required size: %" PRIu64 "\n", info->required);
4666         printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated);
4667     } else {
4668         dump_json_block_measure_info(info);
4669     }
4670 
4671     ret = 0;
4672 
4673 out:
4674     qapi_free_BlockMeasureInfo(info);
4675     qemu_opts_del(object_opts);
4676     qemu_opts_del(opts);
4677     qemu_opts_del(sn_opts);
4678     qemu_opts_free(create_opts);
4679     g_free(options);
4680     blk_unref(in_blk);
4681     return ret;
4682 }
4683 
4684 static const img_cmd_t img_cmds[] = {
4685 #define DEF(option, callback, arg_string)        \
4686     { option, callback },
4687 #include "qemu-img-cmds.h"
4688 #undef DEF
4689 #undef GEN_DOCS
4690     { NULL, NULL, },
4691 };
4692 
4693 int main(int argc, char **argv)
4694 {
4695     const img_cmd_t *cmd;
4696     const char *cmdname;
4697     Error *local_error = NULL;
4698     char *trace_file = NULL;
4699     int c;
4700     static const struct option long_options[] = {
4701         {"help", no_argument, 0, 'h'},
4702         {"version", no_argument, 0, 'V'},
4703         {"trace", required_argument, NULL, 'T'},
4704         {0, 0, 0, 0}
4705     };
4706 
4707 #ifdef CONFIG_POSIX
4708     signal(SIGPIPE, SIG_IGN);
4709 #endif
4710 
4711     module_call_init(MODULE_INIT_TRACE);
4712     error_set_progname(argv[0]);
4713     qemu_init_exec_dir(argv[0]);
4714 
4715     if (qemu_init_main_loop(&local_error)) {
4716         error_report_err(local_error);
4717         exit(EXIT_FAILURE);
4718     }
4719 
4720     qcrypto_init(&error_fatal);
4721 
4722     module_call_init(MODULE_INIT_QOM);
4723     bdrv_init();
4724     if (argc < 2) {
4725         error_exit("Not enough arguments");
4726     }
4727 
4728     qemu_add_opts(&qemu_object_opts);
4729     qemu_add_opts(&qemu_source_opts);
4730     qemu_add_opts(&qemu_trace_opts);
4731 
4732     while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) {
4733         switch (c) {
4734         case ':':
4735             missing_argument(argv[optind - 1]);
4736             return 0;
4737         case '?':
4738             unrecognized_option(argv[optind - 1]);
4739             return 0;
4740         case 'h':
4741             help();
4742             return 0;
4743         case 'V':
4744             printf(QEMU_IMG_VERSION);
4745             return 0;
4746         case 'T':
4747             g_free(trace_file);
4748             trace_file = trace_opt_parse(optarg);
4749             break;
4750         }
4751     }
4752 
4753     cmdname = argv[optind];
4754 
4755     /* reset getopt_long scanning */
4756     argc -= optind;
4757     if (argc < 1) {
4758         return 0;
4759     }
4760     argv += optind;
4761     optind = 0;
4762 
4763     if (!trace_init_backends()) {
4764         exit(1);
4765     }
4766     trace_init_file(trace_file);
4767     qemu_set_log(LOG_TRACE);
4768 
4769     /* find the command */
4770     for (cmd = img_cmds; cmd->name != NULL; cmd++) {
4771         if (!strcmp(cmdname, cmd->name)) {
4772             return cmd->handler(argc, argv);
4773         }
4774     }
4775 
4776     /* not found */
4777     error_exit("Command not found: %s", cmdname);
4778 }
4779