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