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