xref: /openbmc/qemu/qemu-img.c (revision 4a66d3bf)
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 "qapi-visit.h"
25 #include "qapi/qmp-output-visitor.h"
26 #include "qapi/qmp/qjson.h"
27 #include "qemu-common.h"
28 #include "qemu/option.h"
29 #include "qemu/error-report.h"
30 #include "qemu/osdep.h"
31 #include "sysemu/sysemu.h"
32 #include "block/block_int.h"
33 #include "block/qapi.h"
34 #include <getopt.h>
35 
36 typedef struct img_cmd_t {
37     const char *name;
38     int (*handler)(int argc, char **argv);
39 } img_cmd_t;
40 
41 enum {
42     OPTION_OUTPUT = 256,
43     OPTION_BACKING_CHAIN = 257,
44 };
45 
46 typedef enum OutputFormat {
47     OFORMAT_JSON,
48     OFORMAT_HUMAN,
49 } OutputFormat;
50 
51 /* Default to cache=writeback as data integrity is not important for qemu-tcg. */
52 #define BDRV_O_FLAGS BDRV_O_CACHE_WB
53 #define BDRV_DEFAULT_CACHE "writeback"
54 
55 static void format_print(void *opaque, const char *name)
56 {
57     printf(" %s", name);
58 }
59 
60 static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
61 {
62     va_list ap;
63 
64     error_printf("qemu-img: ");
65 
66     va_start(ap, fmt);
67     error_vprintf(fmt, ap);
68     va_end(ap);
69 
70     error_printf("\nTry 'qemu-img --help' for more information\n");
71     exit(EXIT_FAILURE);
72 }
73 
74 /* Please keep in synch with qemu-img.texi */
75 static void QEMU_NORETURN help(void)
76 {
77     const char *help_msg =
78            "qemu-img version " QEMU_VERSION ", Copyright (c) 2004-2008 Fabrice Bellard\n"
79            "usage: qemu-img command [command options]\n"
80            "QEMU disk image utility\n"
81            "\n"
82            "Command syntax:\n"
83 #define DEF(option, callback, arg_string)        \
84            "  " arg_string "\n"
85 #include "qemu-img-cmds.h"
86 #undef DEF
87 #undef GEN_DOCS
88            "\n"
89            "Command parameters:\n"
90            "  'filename' is a disk image filename\n"
91            "  'fmt' is the disk image format. It is guessed automatically in most cases\n"
92            "  'cache' is the cache mode used to write the output disk image, the valid\n"
93            "    options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
94            "    'directsync' and 'unsafe' (default for convert)\n"
95            "  'size' is the disk image size in bytes. Optional suffixes\n"
96            "    'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
97            "    'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P)  are\n"
98            "    supported. 'b' is ignored.\n"
99            "  'output_filename' is the destination disk image filename\n"
100            "  'output_fmt' is the destination format\n"
101            "  'options' is a comma separated list of format specific options in a\n"
102            "    name=value format. Use -o ? for an overview of the options supported by the\n"
103            "    used format\n"
104            "  'snapshot_param' is param used for internal snapshot, format\n"
105            "    is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
106            "    '[ID_OR_NAME]'\n"
107            "  'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n"
108            "    instead\n"
109            "  '-c' indicates that target image must be compressed (qcow format only)\n"
110            "  '-u' enables unsafe rebasing. It is assumed that old and new backing file\n"
111            "       match exactly. The image doesn't need a working backing file before\n"
112            "       rebasing in this case (useful for renaming the backing file)\n"
113            "  '-h' with or without a command shows this help and lists the supported formats\n"
114            "  '-p' show progress of command (only certain commands)\n"
115            "  '-q' use Quiet mode - do not print any output (except errors)\n"
116            "  '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
117            "       contain only zeros for qemu-img to create a sparse image during\n"
118            "       conversion. If the number of bytes is 0, the source will not be scanned for\n"
119            "       unallocated or zero sectors, and the destination image will always be\n"
120            "       fully allocated\n"
121            "  '--output' takes the format in which the output must be done (human or json)\n"
122            "  '-n' skips the target volume creation (useful if the volume is created\n"
123            "       prior to running qemu-img)\n"
124            "\n"
125            "Parameters to check subcommand:\n"
126            "  '-r' tries to repair any inconsistencies that are found during the check.\n"
127            "       '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
128            "       kinds of errors, with a higher risk of choosing the wrong fix or\n"
129            "       hiding corruption that has already occurred.\n"
130            "\n"
131            "Parameters to snapshot subcommand:\n"
132            "  'snapshot' is the name of the snapshot to create, apply or delete\n"
133            "  '-a' applies a snapshot (revert disk to saved state)\n"
134            "  '-c' creates a snapshot\n"
135            "  '-d' deletes a snapshot\n"
136            "  '-l' lists all snapshots in the given image\n"
137            "\n"
138            "Parameters to compare subcommand:\n"
139            "  '-f' first image format\n"
140            "  '-F' second image format\n"
141            "  '-s' run in Strict mode - fail on different image size or sector allocation\n";
142 
143     printf("%s\nSupported formats:", help_msg);
144     bdrv_iterate_format(format_print, NULL);
145     printf("\n");
146     exit(EXIT_SUCCESS);
147 }
148 
149 static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
150 {
151     int ret = 0;
152     if (!quiet) {
153         va_list args;
154         va_start(args, fmt);
155         ret = vprintf(fmt, args);
156         va_end(args);
157     }
158     return ret;
159 }
160 
161 #if defined(WIN32)
162 /* XXX: put correct support for win32 */
163 static int read_password(char *buf, int buf_size)
164 {
165     int c, i;
166     printf("Password: ");
167     fflush(stdout);
168     i = 0;
169     for(;;) {
170         c = getchar();
171         if (c == '\n')
172             break;
173         if (i < (buf_size - 1))
174             buf[i++] = c;
175     }
176     buf[i] = '\0';
177     return 0;
178 }
179 
180 #else
181 
182 #include <termios.h>
183 
184 static struct termios oldtty;
185 
186 static void term_exit(void)
187 {
188     tcsetattr (0, TCSANOW, &oldtty);
189 }
190 
191 static void term_init(void)
192 {
193     struct termios tty;
194 
195     tcgetattr (0, &tty);
196     oldtty = tty;
197 
198     tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
199                           |INLCR|IGNCR|ICRNL|IXON);
200     tty.c_oflag |= OPOST;
201     tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
202     tty.c_cflag &= ~(CSIZE|PARENB);
203     tty.c_cflag |= CS8;
204     tty.c_cc[VMIN] = 1;
205     tty.c_cc[VTIME] = 0;
206 
207     tcsetattr (0, TCSANOW, &tty);
208 
209     atexit(term_exit);
210 }
211 
212 static int read_password(char *buf, int buf_size)
213 {
214     uint8_t ch;
215     int i, ret;
216 
217     printf("password: ");
218     fflush(stdout);
219     term_init();
220     i = 0;
221     for(;;) {
222         ret = read(0, &ch, 1);
223         if (ret == -1) {
224             if (errno == EAGAIN || errno == EINTR) {
225                 continue;
226             } else {
227                 ret = -1;
228                 break;
229             }
230         } else if (ret == 0) {
231             ret = -1;
232             break;
233         } else {
234             if (ch == '\r') {
235                 ret = 0;
236                 break;
237             }
238             if (i < (buf_size - 1))
239                 buf[i++] = ch;
240         }
241     }
242     term_exit();
243     buf[i] = '\0';
244     printf("\n");
245     return ret;
246 }
247 #endif
248 
249 static int print_block_option_help(const char *filename, const char *fmt)
250 {
251     BlockDriver *drv, *proto_drv;
252     QEMUOptionParameter *create_options = NULL;
253 
254     /* Find driver and parse its options */
255     drv = bdrv_find_format(fmt);
256     if (!drv) {
257         error_report("Unknown file format '%s'", fmt);
258         return 1;
259     }
260 
261     create_options = append_option_parameters(create_options,
262                                               drv->create_options);
263 
264     if (filename) {
265         proto_drv = bdrv_find_protocol(filename, true);
266         if (!proto_drv) {
267             error_report("Unknown protocol '%s'", filename);
268             return 1;
269         }
270         create_options = append_option_parameters(create_options,
271                                                   proto_drv->create_options);
272     }
273 
274     print_option_help(create_options);
275     free_option_parameters(create_options);
276     return 0;
277 }
278 
279 static BlockDriverState *bdrv_new_open(const char *id,
280                                        const char *filename,
281                                        const char *fmt,
282                                        int flags,
283                                        bool require_io,
284                                        bool quiet)
285 {
286     BlockDriverState *bs;
287     BlockDriver *drv;
288     char password[256];
289     Error *local_err = NULL;
290     int ret;
291 
292     bs = bdrv_new(id, &error_abort);
293 
294     if (fmt) {
295         drv = bdrv_find_format(fmt);
296         if (!drv) {
297             error_report("Unknown file format '%s'", fmt);
298             goto fail;
299         }
300     } else {
301         drv = NULL;
302     }
303 
304     ret = bdrv_open(&bs, filename, NULL, NULL, flags, drv, &local_err);
305     if (ret < 0) {
306         error_report("Could not open '%s': %s", filename,
307                      error_get_pretty(local_err));
308         error_free(local_err);
309         goto fail;
310     }
311 
312     if (bdrv_is_encrypted(bs) && require_io) {
313         qprintf(quiet, "Disk image '%s' is encrypted.\n", filename);
314         if (read_password(password, sizeof(password)) < 0) {
315             error_report("No password given");
316             goto fail;
317         }
318         if (bdrv_set_key(bs, password) < 0) {
319             error_report("invalid password");
320             goto fail;
321         }
322     }
323     return bs;
324 fail:
325     bdrv_unref(bs);
326     return NULL;
327 }
328 
329 static int add_old_style_options(const char *fmt, QEMUOptionParameter *list,
330                                  const char *base_filename,
331                                  const char *base_fmt)
332 {
333     if (base_filename) {
334         if (set_option_parameter(list, BLOCK_OPT_BACKING_FILE, base_filename)) {
335             error_report("Backing file not supported for file format '%s'",
336                          fmt);
337             return -1;
338         }
339     }
340     if (base_fmt) {
341         if (set_option_parameter(list, BLOCK_OPT_BACKING_FMT, base_fmt)) {
342             error_report("Backing file format not supported for file "
343                          "format '%s'", fmt);
344             return -1;
345         }
346     }
347     return 0;
348 }
349 
350 static int img_create(int argc, char **argv)
351 {
352     int c;
353     uint64_t img_size = -1;
354     const char *fmt = "raw";
355     const char *base_fmt = NULL;
356     const char *filename;
357     const char *base_filename = NULL;
358     char *options = NULL;
359     Error *local_err = NULL;
360     bool quiet = false;
361 
362     for(;;) {
363         c = getopt(argc, argv, "F:b:f:he6o:q");
364         if (c == -1) {
365             break;
366         }
367         switch(c) {
368         case '?':
369         case 'h':
370             help();
371             break;
372         case 'F':
373             base_fmt = optarg;
374             break;
375         case 'b':
376             base_filename = optarg;
377             break;
378         case 'f':
379             fmt = optarg;
380             break;
381         case 'e':
382             error_report("option -e is deprecated, please use \'-o "
383                   "encryption\' instead!");
384             goto fail;
385         case '6':
386             error_report("option -6 is deprecated, please use \'-o "
387                   "compat6\' instead!");
388             goto fail;
389         case 'o':
390             if (!is_valid_option_list(optarg)) {
391                 error_report("Invalid option list: %s", optarg);
392                 goto fail;
393             }
394             if (!options) {
395                 options = g_strdup(optarg);
396             } else {
397                 char *old_options = options;
398                 options = g_strdup_printf("%s,%s", options, optarg);
399                 g_free(old_options);
400             }
401             break;
402         case 'q':
403             quiet = true;
404             break;
405         }
406     }
407 
408     /* Get the filename */
409     filename = (optind < argc) ? argv[optind] : NULL;
410     if (options && has_help_option(options)) {
411         g_free(options);
412         return print_block_option_help(filename, fmt);
413     }
414 
415     if (optind >= argc) {
416         error_exit("Expecting image file name");
417     }
418     optind++;
419 
420     /* Get image size, if specified */
421     if (optind < argc) {
422         int64_t sval;
423         char *end;
424         sval = strtosz_suffix(argv[optind++], &end, STRTOSZ_DEFSUFFIX_B);
425         if (sval < 0 || *end) {
426             if (sval == -ERANGE) {
427                 error_report("Image size must be less than 8 EiB!");
428             } else {
429                 error_report("Invalid image size specified! You may use k, M, "
430                       "G, T, P or E suffixes for ");
431                 error_report("kilobytes, megabytes, gigabytes, terabytes, "
432                              "petabytes and exabytes.");
433             }
434             goto fail;
435         }
436         img_size = (uint64_t)sval;
437     }
438     if (optind != argc) {
439         error_exit("Unexpected argument: %s", argv[optind]);
440     }
441 
442     bdrv_img_create(filename, fmt, base_filename, base_fmt,
443                     options, img_size, BDRV_O_FLAGS, &local_err, quiet);
444     if (local_err) {
445         error_report("%s: %s", filename, error_get_pretty(local_err));
446         error_free(local_err);
447         goto fail;
448     }
449 
450     g_free(options);
451     return 0;
452 
453 fail:
454     g_free(options);
455     return 1;
456 }
457 
458 static void dump_json_image_check(ImageCheck *check, bool quiet)
459 {
460     Error *errp = NULL;
461     QString *str;
462     QmpOutputVisitor *ov = qmp_output_visitor_new();
463     QObject *obj;
464     visit_type_ImageCheck(qmp_output_get_visitor(ov),
465                           &check, NULL, &errp);
466     obj = qmp_output_get_qobject(ov);
467     str = qobject_to_json_pretty(obj);
468     assert(str != NULL);
469     qprintf(quiet, "%s\n", qstring_get_str(str));
470     qobject_decref(obj);
471     qmp_output_visitor_cleanup(ov);
472     QDECREF(str);
473 }
474 
475 static void dump_human_image_check(ImageCheck *check, bool quiet)
476 {
477     if (!(check->corruptions || check->leaks || check->check_errors)) {
478         qprintf(quiet, "No errors were found on the image.\n");
479     } else {
480         if (check->corruptions) {
481             qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
482                     "Data may be corrupted, or further writes to the image "
483                     "may corrupt it.\n",
484                     check->corruptions);
485         }
486 
487         if (check->leaks) {
488             qprintf(quiet,
489                     "\n%" PRId64 " leaked clusters were found on the image.\n"
490                     "This means waste of disk space, but no harm to data.\n",
491                     check->leaks);
492         }
493 
494         if (check->check_errors) {
495             qprintf(quiet,
496                     "\n%" PRId64
497                     " internal errors have occurred during the check.\n",
498                     check->check_errors);
499         }
500     }
501 
502     if (check->total_clusters != 0 && check->allocated_clusters != 0) {
503         qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
504                 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
505                 check->allocated_clusters, check->total_clusters,
506                 check->allocated_clusters * 100.0 / check->total_clusters,
507                 check->fragmented_clusters * 100.0 / check->allocated_clusters,
508                 check->compressed_clusters * 100.0 /
509                 check->allocated_clusters);
510     }
511 
512     if (check->image_end_offset) {
513         qprintf(quiet,
514                 "Image end offset: %" PRId64 "\n", check->image_end_offset);
515     }
516 }
517 
518 static int collect_image_check(BlockDriverState *bs,
519                    ImageCheck *check,
520                    const char *filename,
521                    const char *fmt,
522                    int fix)
523 {
524     int ret;
525     BdrvCheckResult result;
526 
527     ret = bdrv_check(bs, &result, fix);
528     if (ret < 0) {
529         return ret;
530     }
531 
532     check->filename                 = g_strdup(filename);
533     check->format                   = g_strdup(bdrv_get_format_name(bs));
534     check->check_errors             = result.check_errors;
535     check->corruptions              = result.corruptions;
536     check->has_corruptions          = result.corruptions != 0;
537     check->leaks                    = result.leaks;
538     check->has_leaks                = result.leaks != 0;
539     check->corruptions_fixed        = result.corruptions_fixed;
540     check->has_corruptions_fixed    = result.corruptions != 0;
541     check->leaks_fixed              = result.leaks_fixed;
542     check->has_leaks_fixed          = result.leaks != 0;
543     check->image_end_offset         = result.image_end_offset;
544     check->has_image_end_offset     = result.image_end_offset != 0;
545     check->total_clusters           = result.bfi.total_clusters;
546     check->has_total_clusters       = result.bfi.total_clusters != 0;
547     check->allocated_clusters       = result.bfi.allocated_clusters;
548     check->has_allocated_clusters   = result.bfi.allocated_clusters != 0;
549     check->fragmented_clusters      = result.bfi.fragmented_clusters;
550     check->has_fragmented_clusters  = result.bfi.fragmented_clusters != 0;
551     check->compressed_clusters      = result.bfi.compressed_clusters;
552     check->has_compressed_clusters  = result.bfi.compressed_clusters != 0;
553 
554     return 0;
555 }
556 
557 /*
558  * Checks an image for consistency. Exit codes:
559  *
560  * 0 - Check completed, image is good
561  * 1 - Check not completed because of internal errors
562  * 2 - Check completed, image is corrupted
563  * 3 - Check completed, image has leaked clusters, but is good otherwise
564  */
565 static int img_check(int argc, char **argv)
566 {
567     int c, ret;
568     OutputFormat output_format = OFORMAT_HUMAN;
569     const char *filename, *fmt, *output;
570     BlockDriverState *bs;
571     int fix = 0;
572     int flags = BDRV_O_FLAGS | BDRV_O_CHECK;
573     ImageCheck *check;
574     bool quiet = false;
575 
576     fmt = NULL;
577     output = NULL;
578     for(;;) {
579         int option_index = 0;
580         static const struct option long_options[] = {
581             {"help", no_argument, 0, 'h'},
582             {"format", required_argument, 0, 'f'},
583             {"repair", required_argument, 0, 'r'},
584             {"output", required_argument, 0, OPTION_OUTPUT},
585             {0, 0, 0, 0}
586         };
587         c = getopt_long(argc, argv, "f:hr:q",
588                         long_options, &option_index);
589         if (c == -1) {
590             break;
591         }
592         switch(c) {
593         case '?':
594         case 'h':
595             help();
596             break;
597         case 'f':
598             fmt = optarg;
599             break;
600         case 'r':
601             flags |= BDRV_O_RDWR;
602 
603             if (!strcmp(optarg, "leaks")) {
604                 fix = BDRV_FIX_LEAKS;
605             } else if (!strcmp(optarg, "all")) {
606                 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
607             } else {
608                 error_exit("Unknown option value for -r "
609                            "(expecting 'leaks' or 'all'): %s", optarg);
610             }
611             break;
612         case OPTION_OUTPUT:
613             output = optarg;
614             break;
615         case 'q':
616             quiet = true;
617             break;
618         }
619     }
620     if (optind != argc - 1) {
621         error_exit("Expecting one image file name");
622     }
623     filename = argv[optind++];
624 
625     if (output && !strcmp(output, "json")) {
626         output_format = OFORMAT_JSON;
627     } else if (output && !strcmp(output, "human")) {
628         output_format = OFORMAT_HUMAN;
629     } else if (output) {
630         error_report("--output must be used with human or json as argument.");
631         return 1;
632     }
633 
634     bs = bdrv_new_open("image", filename, fmt, flags, true, quiet);
635     if (!bs) {
636         return 1;
637     }
638 
639     check = g_new0(ImageCheck, 1);
640     ret = collect_image_check(bs, check, filename, fmt, fix);
641 
642     if (ret == -ENOTSUP) {
643         if (output_format == OFORMAT_HUMAN) {
644             error_report("This image format does not support checks");
645         }
646         ret = 63;
647         goto fail;
648     }
649 
650     if (check->corruptions_fixed || check->leaks_fixed) {
651         int corruptions_fixed, leaks_fixed;
652 
653         leaks_fixed         = check->leaks_fixed;
654         corruptions_fixed   = check->corruptions_fixed;
655 
656         if (output_format == OFORMAT_HUMAN) {
657             qprintf(quiet,
658                     "The following inconsistencies were found and repaired:\n\n"
659                     "    %" PRId64 " leaked clusters\n"
660                     "    %" PRId64 " corruptions\n\n"
661                     "Double checking the fixed image now...\n",
662                     check->leaks_fixed,
663                     check->corruptions_fixed);
664         }
665 
666         ret = collect_image_check(bs, check, filename, fmt, 0);
667 
668         check->leaks_fixed          = leaks_fixed;
669         check->corruptions_fixed    = corruptions_fixed;
670     }
671 
672     switch (output_format) {
673     case OFORMAT_HUMAN:
674         dump_human_image_check(check, quiet);
675         break;
676     case OFORMAT_JSON:
677         dump_json_image_check(check, quiet);
678         break;
679     }
680 
681     if (ret || check->check_errors) {
682         ret = 1;
683         goto fail;
684     }
685 
686     if (check->corruptions) {
687         ret = 2;
688     } else if (check->leaks) {
689         ret = 3;
690     } else {
691         ret = 0;
692     }
693 
694 fail:
695     qapi_free_ImageCheck(check);
696     bdrv_unref(bs);
697 
698     return ret;
699 }
700 
701 static int img_commit(int argc, char **argv)
702 {
703     int c, ret, flags;
704     const char *filename, *fmt, *cache;
705     BlockDriverState *bs;
706     bool quiet = false;
707 
708     fmt = NULL;
709     cache = BDRV_DEFAULT_CACHE;
710     for(;;) {
711         c = getopt(argc, argv, "f:ht:q");
712         if (c == -1) {
713             break;
714         }
715         switch(c) {
716         case '?':
717         case 'h':
718             help();
719             break;
720         case 'f':
721             fmt = optarg;
722             break;
723         case 't':
724             cache = optarg;
725             break;
726         case 'q':
727             quiet = true;
728             break;
729         }
730     }
731     if (optind != argc - 1) {
732         error_exit("Expecting one image file name");
733     }
734     filename = argv[optind++];
735 
736     flags = BDRV_O_RDWR;
737     ret = bdrv_parse_cache_flags(cache, &flags);
738     if (ret < 0) {
739         error_report("Invalid cache option: %s", cache);
740         return -1;
741     }
742 
743     bs = bdrv_new_open("image", filename, fmt, flags, true, quiet);
744     if (!bs) {
745         return 1;
746     }
747     ret = bdrv_commit(bs);
748     switch(ret) {
749     case 0:
750         qprintf(quiet, "Image committed.\n");
751         break;
752     case -ENOENT:
753         error_report("No disk inserted");
754         break;
755     case -EACCES:
756         error_report("Image is read-only");
757         break;
758     case -ENOTSUP:
759         error_report("Image is already committed");
760         break;
761     default:
762         error_report("Error while committing image");
763         break;
764     }
765 
766     bdrv_unref(bs);
767     if (ret) {
768         return 1;
769     }
770     return 0;
771 }
772 
773 /*
774  * Returns true iff the first sector pointed to by 'buf' contains at least
775  * a non-NUL byte.
776  *
777  * 'pnum' is set to the number of sectors (including and immediately following
778  * the first one) that are known to be in the same allocated/unallocated state.
779  */
780 static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
781 {
782     bool is_zero;
783     int i;
784 
785     if (n <= 0) {
786         *pnum = 0;
787         return 0;
788     }
789     is_zero = buffer_is_zero(buf, 512);
790     for(i = 1; i < n; i++) {
791         buf += 512;
792         if (is_zero != buffer_is_zero(buf, 512)) {
793             break;
794         }
795     }
796     *pnum = i;
797     return !is_zero;
798 }
799 
800 /*
801  * Like is_allocated_sectors, but if the buffer starts with a used sector,
802  * up to 'min' consecutive sectors containing zeros are ignored. This avoids
803  * breaking up write requests for only small sparse areas.
804  */
805 static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
806     int min)
807 {
808     int ret;
809     int num_checked, num_used;
810 
811     if (n < min) {
812         min = n;
813     }
814 
815     ret = is_allocated_sectors(buf, n, pnum);
816     if (!ret) {
817         return ret;
818     }
819 
820     num_used = *pnum;
821     buf += BDRV_SECTOR_SIZE * *pnum;
822     n -= *pnum;
823     num_checked = num_used;
824 
825     while (n > 0) {
826         ret = is_allocated_sectors(buf, n, pnum);
827 
828         buf += BDRV_SECTOR_SIZE * *pnum;
829         n -= *pnum;
830         num_checked += *pnum;
831         if (ret) {
832             num_used = num_checked;
833         } else if (*pnum >= min) {
834             break;
835         }
836     }
837 
838     *pnum = num_used;
839     return 1;
840 }
841 
842 /*
843  * Compares two buffers sector by sector. Returns 0 if the first sector of both
844  * buffers matches, non-zero otherwise.
845  *
846  * pnum is set to the number of sectors (including and immediately following
847  * the first one) that are known to have the same comparison result
848  */
849 static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
850     int *pnum)
851 {
852     int res, i;
853 
854     if (n <= 0) {
855         *pnum = 0;
856         return 0;
857     }
858 
859     res = !!memcmp(buf1, buf2, 512);
860     for(i = 1; i < n; i++) {
861         buf1 += 512;
862         buf2 += 512;
863 
864         if (!!memcmp(buf1, buf2, 512) != res) {
865             break;
866         }
867     }
868 
869     *pnum = i;
870     return res;
871 }
872 
873 #define IO_BUF_SIZE (2 * 1024 * 1024)
874 
875 static int64_t sectors_to_bytes(int64_t sectors)
876 {
877     return sectors << BDRV_SECTOR_BITS;
878 }
879 
880 static int64_t sectors_to_process(int64_t total, int64_t from)
881 {
882     return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
883 }
884 
885 /*
886  * Check if passed sectors are empty (not allocated or contain only 0 bytes)
887  *
888  * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero
889  * data and negative value on error.
890  *
891  * @param bs:  Driver used for accessing file
892  * @param sect_num: Number of first sector to check
893  * @param sect_count: Number of sectors to check
894  * @param filename: Name of disk file we are checking (logging purpose)
895  * @param buffer: Allocated buffer for storing read data
896  * @param quiet: Flag for quiet mode
897  */
898 static int check_empty_sectors(BlockDriverState *bs, int64_t sect_num,
899                                int sect_count, const char *filename,
900                                uint8_t *buffer, bool quiet)
901 {
902     int pnum, ret = 0;
903     ret = bdrv_read(bs, sect_num, buffer, sect_count);
904     if (ret < 0) {
905         error_report("Error while reading offset %" PRId64 " of %s: %s",
906                      sectors_to_bytes(sect_num), filename, strerror(-ret));
907         return ret;
908     }
909     ret = is_allocated_sectors(buffer, sect_count, &pnum);
910     if (ret || pnum != sect_count) {
911         qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
912                 sectors_to_bytes(ret ? sect_num : sect_num + pnum));
913         return 1;
914     }
915 
916     return 0;
917 }
918 
919 /*
920  * Compares two images. Exit codes:
921  *
922  * 0 - Images are identical
923  * 1 - Images differ
924  * >1 - Error occurred
925  */
926 static int img_compare(int argc, char **argv)
927 {
928     const char *fmt1 = NULL, *fmt2 = NULL, *filename1, *filename2;
929     BlockDriverState *bs1, *bs2;
930     int64_t total_sectors1, total_sectors2;
931     uint8_t *buf1 = NULL, *buf2 = NULL;
932     int pnum1, pnum2;
933     int allocated1, allocated2;
934     int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
935     bool progress = false, quiet = false, strict = false;
936     int64_t total_sectors;
937     int64_t sector_num = 0;
938     int64_t nb_sectors;
939     int c, pnum;
940     uint64_t bs_sectors;
941     uint64_t progress_base;
942 
943     for (;;) {
944         c = getopt(argc, argv, "hpf:F:sq");
945         if (c == -1) {
946             break;
947         }
948         switch (c) {
949         case '?':
950         case 'h':
951             help();
952             break;
953         case 'f':
954             fmt1 = optarg;
955             break;
956         case 'F':
957             fmt2 = optarg;
958             break;
959         case 'p':
960             progress = true;
961             break;
962         case 'q':
963             quiet = true;
964             break;
965         case 's':
966             strict = true;
967             break;
968         }
969     }
970 
971     /* Progress is not shown in Quiet mode */
972     if (quiet) {
973         progress = false;
974     }
975 
976 
977     if (optind != argc - 2) {
978         error_exit("Expecting two image file names");
979     }
980     filename1 = argv[optind++];
981     filename2 = argv[optind++];
982 
983     /* Initialize before goto out */
984     qemu_progress_init(progress, 2.0);
985 
986     bs1 = bdrv_new_open("image 1", filename1, fmt1, BDRV_O_FLAGS, true, quiet);
987     if (!bs1) {
988         error_report("Can't open file %s", filename1);
989         ret = 2;
990         goto out3;
991     }
992 
993     bs2 = bdrv_new_open("image 2", filename2, fmt2, BDRV_O_FLAGS, true, quiet);
994     if (!bs2) {
995         error_report("Can't open file %s", filename2);
996         ret = 2;
997         goto out2;
998     }
999 
1000     buf1 = qemu_blockalign(bs1, IO_BUF_SIZE);
1001     buf2 = qemu_blockalign(bs2, IO_BUF_SIZE);
1002     bdrv_get_geometry(bs1, &bs_sectors);
1003     total_sectors1 = bs_sectors;
1004     bdrv_get_geometry(bs2, &bs_sectors);
1005     total_sectors2 = bs_sectors;
1006     total_sectors = MIN(total_sectors1, total_sectors2);
1007     progress_base = MAX(total_sectors1, total_sectors2);
1008 
1009     qemu_progress_print(0, 100);
1010 
1011     if (strict && total_sectors1 != total_sectors2) {
1012         ret = 1;
1013         qprintf(quiet, "Strict mode: Image size mismatch!\n");
1014         goto out;
1015     }
1016 
1017     for (;;) {
1018         nb_sectors = sectors_to_process(total_sectors, sector_num);
1019         if (nb_sectors <= 0) {
1020             break;
1021         }
1022         allocated1 = bdrv_is_allocated_above(bs1, NULL, sector_num, nb_sectors,
1023                                              &pnum1);
1024         if (allocated1 < 0) {
1025             ret = 3;
1026             error_report("Sector allocation test failed for %s", filename1);
1027             goto out;
1028         }
1029 
1030         allocated2 = bdrv_is_allocated_above(bs2, NULL, sector_num, nb_sectors,
1031                                              &pnum2);
1032         if (allocated2 < 0) {
1033             ret = 3;
1034             error_report("Sector allocation test failed for %s", filename2);
1035             goto out;
1036         }
1037         nb_sectors = MIN(pnum1, pnum2);
1038 
1039         if (allocated1 == allocated2) {
1040             if (allocated1) {
1041                 ret = bdrv_read(bs1, sector_num, buf1, nb_sectors);
1042                 if (ret < 0) {
1043                     error_report("Error while reading offset %" PRId64 " of %s:"
1044                                  " %s", sectors_to_bytes(sector_num), filename1,
1045                                  strerror(-ret));
1046                     ret = 4;
1047                     goto out;
1048                 }
1049                 ret = bdrv_read(bs2, sector_num, buf2, nb_sectors);
1050                 if (ret < 0) {
1051                     error_report("Error while reading offset %" PRId64
1052                                  " of %s: %s", sectors_to_bytes(sector_num),
1053                                  filename2, strerror(-ret));
1054                     ret = 4;
1055                     goto out;
1056                 }
1057                 ret = compare_sectors(buf1, buf2, nb_sectors, &pnum);
1058                 if (ret || pnum != nb_sectors) {
1059                     qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1060                             sectors_to_bytes(
1061                                 ret ? sector_num : sector_num + pnum));
1062                     ret = 1;
1063                     goto out;
1064                 }
1065             }
1066         } else {
1067             if (strict) {
1068                 ret = 1;
1069                 qprintf(quiet, "Strict mode: Offset %" PRId64
1070                         " allocation mismatch!\n",
1071                         sectors_to_bytes(sector_num));
1072                 goto out;
1073             }
1074 
1075             if (allocated1) {
1076                 ret = check_empty_sectors(bs1, sector_num, nb_sectors,
1077                                           filename1, buf1, quiet);
1078             } else {
1079                 ret = check_empty_sectors(bs2, sector_num, nb_sectors,
1080                                           filename2, buf1, quiet);
1081             }
1082             if (ret) {
1083                 if (ret < 0) {
1084                     error_report("Error while reading offset %" PRId64 ": %s",
1085                                  sectors_to_bytes(sector_num), strerror(-ret));
1086                     ret = 4;
1087                 }
1088                 goto out;
1089             }
1090         }
1091         sector_num += nb_sectors;
1092         qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1093     }
1094 
1095     if (total_sectors1 != total_sectors2) {
1096         BlockDriverState *bs_over;
1097         int64_t total_sectors_over;
1098         const char *filename_over;
1099 
1100         qprintf(quiet, "Warning: Image size mismatch!\n");
1101         if (total_sectors1 > total_sectors2) {
1102             total_sectors_over = total_sectors1;
1103             bs_over = bs1;
1104             filename_over = filename1;
1105         } else {
1106             total_sectors_over = total_sectors2;
1107             bs_over = bs2;
1108             filename_over = filename2;
1109         }
1110 
1111         for (;;) {
1112             nb_sectors = sectors_to_process(total_sectors_over, sector_num);
1113             if (nb_sectors <= 0) {
1114                 break;
1115             }
1116             ret = bdrv_is_allocated_above(bs_over, NULL, sector_num,
1117                                           nb_sectors, &pnum);
1118             if (ret < 0) {
1119                 ret = 3;
1120                 error_report("Sector allocation test failed for %s",
1121                              filename_over);
1122                 goto out;
1123 
1124             }
1125             nb_sectors = pnum;
1126             if (ret) {
1127                 ret = check_empty_sectors(bs_over, sector_num, nb_sectors,
1128                                           filename_over, buf1, quiet);
1129                 if (ret) {
1130                     if (ret < 0) {
1131                         error_report("Error while reading offset %" PRId64
1132                                      " of %s: %s", sectors_to_bytes(sector_num),
1133                                      filename_over, strerror(-ret));
1134                         ret = 4;
1135                     }
1136                     goto out;
1137                 }
1138             }
1139             sector_num += nb_sectors;
1140             qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1141         }
1142     }
1143 
1144     qprintf(quiet, "Images are identical.\n");
1145     ret = 0;
1146 
1147 out:
1148     bdrv_unref(bs2);
1149     qemu_vfree(buf1);
1150     qemu_vfree(buf2);
1151 out2:
1152     bdrv_unref(bs1);
1153 out3:
1154     qemu_progress_end();
1155     return ret;
1156 }
1157 
1158 static int img_convert(int argc, char **argv)
1159 {
1160     int c, n, n1, bs_n, bs_i, compress, cluster_sectors, skip_create;
1161     int64_t ret = 0;
1162     int progress = 0, flags;
1163     const char *fmt, *out_fmt, *cache, *out_baseimg, *out_filename;
1164     BlockDriver *drv, *proto_drv;
1165     BlockDriverState **bs = NULL, *out_bs = NULL;
1166     int64_t total_sectors, nb_sectors, sector_num, bs_offset;
1167     uint64_t bs_sectors;
1168     uint8_t * buf = NULL;
1169     size_t bufsectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE;
1170     const uint8_t *buf1;
1171     BlockDriverInfo bdi;
1172     QEMUOptionParameter *param = NULL, *create_options = NULL;
1173     QEMUOptionParameter *out_baseimg_param;
1174     char *options = NULL;
1175     const char *snapshot_name = NULL;
1176     int min_sparse = 8; /* Need at least 4k of zeros for sparse detection */
1177     bool quiet = false;
1178     Error *local_err = NULL;
1179     QemuOpts *sn_opts = NULL;
1180 
1181     fmt = NULL;
1182     out_fmt = "raw";
1183     cache = "unsafe";
1184     out_baseimg = NULL;
1185     compress = 0;
1186     skip_create = 0;
1187     for(;;) {
1188         c = getopt(argc, argv, "f:O:B:s:hce6o:pS:t:qnl:");
1189         if (c == -1) {
1190             break;
1191         }
1192         switch(c) {
1193         case '?':
1194         case 'h':
1195             help();
1196             break;
1197         case 'f':
1198             fmt = optarg;
1199             break;
1200         case 'O':
1201             out_fmt = optarg;
1202             break;
1203         case 'B':
1204             out_baseimg = optarg;
1205             break;
1206         case 'c':
1207             compress = 1;
1208             break;
1209         case 'e':
1210             error_report("option -e is deprecated, please use \'-o "
1211                   "encryption\' instead!");
1212             ret = -1;
1213             goto fail_getopt;
1214         case '6':
1215             error_report("option -6 is deprecated, please use \'-o "
1216                   "compat6\' instead!");
1217             ret = -1;
1218             goto fail_getopt;
1219         case 'o':
1220             if (!is_valid_option_list(optarg)) {
1221                 error_report("Invalid option list: %s", optarg);
1222                 ret = -1;
1223                 goto fail_getopt;
1224             }
1225             if (!options) {
1226                 options = g_strdup(optarg);
1227             } else {
1228                 char *old_options = options;
1229                 options = g_strdup_printf("%s,%s", options, optarg);
1230                 g_free(old_options);
1231             }
1232             break;
1233         case 's':
1234             snapshot_name = optarg;
1235             break;
1236         case 'l':
1237             if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
1238                 sn_opts = qemu_opts_parse(&internal_snapshot_opts, optarg, 0);
1239                 if (!sn_opts) {
1240                     error_report("Failed in parsing snapshot param '%s'",
1241                                  optarg);
1242                     ret = -1;
1243                     goto fail_getopt;
1244                 }
1245             } else {
1246                 snapshot_name = optarg;
1247             }
1248             break;
1249         case 'S':
1250         {
1251             int64_t sval;
1252             char *end;
1253             sval = strtosz_suffix(optarg, &end, STRTOSZ_DEFSUFFIX_B);
1254             if (sval < 0 || *end) {
1255                 error_report("Invalid minimum zero buffer size for sparse output specified");
1256                 ret = -1;
1257                 goto fail_getopt;
1258             }
1259 
1260             min_sparse = sval / BDRV_SECTOR_SIZE;
1261             break;
1262         }
1263         case 'p':
1264             progress = 1;
1265             break;
1266         case 't':
1267             cache = optarg;
1268             break;
1269         case 'q':
1270             quiet = true;
1271             break;
1272         case 'n':
1273             skip_create = 1;
1274             break;
1275         }
1276     }
1277 
1278     /* Initialize before goto out */
1279     if (quiet) {
1280         progress = 0;
1281     }
1282     qemu_progress_init(progress, 1.0);
1283 
1284 
1285     bs_n = argc - optind - 1;
1286     out_filename = bs_n >= 1 ? argv[argc - 1] : NULL;
1287 
1288     if (options && has_help_option(options)) {
1289         ret = print_block_option_help(out_filename, out_fmt);
1290         goto out;
1291     }
1292 
1293     if (bs_n < 1) {
1294         error_exit("Must specify image file name");
1295     }
1296 
1297 
1298     if (bs_n > 1 && out_baseimg) {
1299         error_report("-B makes no sense when concatenating multiple input "
1300                      "images");
1301         ret = -1;
1302         goto out;
1303     }
1304 
1305     qemu_progress_print(0, 100);
1306 
1307     bs = g_malloc0(bs_n * sizeof(BlockDriverState *));
1308 
1309     total_sectors = 0;
1310     for (bs_i = 0; bs_i < bs_n; bs_i++) {
1311         char *id = bs_n > 1 ? g_strdup_printf("source %d", bs_i)
1312                             : g_strdup("source");
1313         bs[bs_i] = bdrv_new_open(id, argv[optind + bs_i], fmt, BDRV_O_FLAGS,
1314                                  true, quiet);
1315         g_free(id);
1316         if (!bs[bs_i]) {
1317             error_report("Could not open '%s'", argv[optind + bs_i]);
1318             ret = -1;
1319             goto out;
1320         }
1321         bdrv_get_geometry(bs[bs_i], &bs_sectors);
1322         total_sectors += bs_sectors;
1323     }
1324 
1325     if (sn_opts) {
1326         ret = bdrv_snapshot_load_tmp(bs[0],
1327                                      qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
1328                                      qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
1329                                      &local_err);
1330     } else if (snapshot_name != NULL) {
1331         if (bs_n > 1) {
1332             error_report("No support for concatenating multiple snapshot");
1333             ret = -1;
1334             goto out;
1335         }
1336 
1337         bdrv_snapshot_load_tmp_by_id_or_name(bs[0], snapshot_name, &local_err);
1338     }
1339     if (local_err) {
1340         error_report("Failed to load snapshot: %s",
1341                      error_get_pretty(local_err));
1342         error_free(local_err);
1343         ret = -1;
1344         goto out;
1345     }
1346 
1347     /* Find driver and parse its options */
1348     drv = bdrv_find_format(out_fmt);
1349     if (!drv) {
1350         error_report("Unknown file format '%s'", out_fmt);
1351         ret = -1;
1352         goto out;
1353     }
1354 
1355     proto_drv = bdrv_find_protocol(out_filename, true);
1356     if (!proto_drv) {
1357         error_report("Unknown protocol '%s'", out_filename);
1358         ret = -1;
1359         goto out;
1360     }
1361 
1362     create_options = append_option_parameters(create_options,
1363                                               drv->create_options);
1364     create_options = append_option_parameters(create_options,
1365                                               proto_drv->create_options);
1366 
1367     if (options) {
1368         param = parse_option_parameters(options, create_options, param);
1369         if (param == NULL) {
1370             error_report("Invalid options for file format '%s'.", out_fmt);
1371             ret = -1;
1372             goto out;
1373         }
1374     } else {
1375         param = parse_option_parameters("", create_options, param);
1376     }
1377 
1378     set_option_parameter_int(param, BLOCK_OPT_SIZE, total_sectors * 512);
1379     ret = add_old_style_options(out_fmt, param, out_baseimg, NULL);
1380     if (ret < 0) {
1381         goto out;
1382     }
1383 
1384     /* Get backing file name if -o backing_file was used */
1385     out_baseimg_param = get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
1386     if (out_baseimg_param) {
1387         out_baseimg = out_baseimg_param->value.s;
1388     }
1389 
1390     /* Check if compression is supported */
1391     if (compress) {
1392         QEMUOptionParameter *encryption =
1393             get_option_parameter(param, BLOCK_OPT_ENCRYPT);
1394         QEMUOptionParameter *preallocation =
1395             get_option_parameter(param, BLOCK_OPT_PREALLOC);
1396 
1397         if (!drv->bdrv_write_compressed) {
1398             error_report("Compression not supported for this file format");
1399             ret = -1;
1400             goto out;
1401         }
1402 
1403         if (encryption && encryption->value.n) {
1404             error_report("Compression and encryption not supported at "
1405                          "the same time");
1406             ret = -1;
1407             goto out;
1408         }
1409 
1410         if (preallocation && preallocation->value.s
1411             && strcmp(preallocation->value.s, "off"))
1412         {
1413             error_report("Compression and preallocation not supported at "
1414                          "the same time");
1415             ret = -1;
1416             goto out;
1417         }
1418     }
1419 
1420     if (!skip_create) {
1421         /* Create the new image */
1422         ret = bdrv_create(drv, out_filename, param, &local_err);
1423         if (ret < 0) {
1424             error_report("%s: error while converting %s: %s",
1425                          out_filename, out_fmt, error_get_pretty(local_err));
1426             error_free(local_err);
1427             goto out;
1428         }
1429     }
1430 
1431     flags = min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
1432     ret = bdrv_parse_cache_flags(cache, &flags);
1433     if (ret < 0) {
1434         error_report("Invalid cache option: %s", cache);
1435         return -1;
1436     }
1437 
1438     out_bs = bdrv_new_open("target", out_filename, out_fmt, flags, true, quiet);
1439     if (!out_bs) {
1440         ret = -1;
1441         goto out;
1442     }
1443 
1444     bs_i = 0;
1445     bs_offset = 0;
1446     bdrv_get_geometry(bs[0], &bs_sectors);
1447 
1448     /* increase bufsectors from the default 4096 (2M) if opt_transfer_length
1449      * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
1450      * as maximum. */
1451     bufsectors = MIN(32768,
1452                      MAX(bufsectors, MAX(out_bs->bl.opt_transfer_length,
1453                                          out_bs->bl.discard_alignment))
1454                     );
1455 
1456     buf = qemu_blockalign(out_bs, bufsectors * BDRV_SECTOR_SIZE);
1457 
1458     if (skip_create) {
1459         int64_t output_length = bdrv_getlength(out_bs);
1460         if (output_length < 0) {
1461             error_report("unable to get output image length: %s\n",
1462                          strerror(-output_length));
1463             ret = -1;
1464             goto out;
1465         } else if (output_length < total_sectors << BDRV_SECTOR_BITS) {
1466             error_report("output file is smaller than input file");
1467             ret = -1;
1468             goto out;
1469         }
1470     }
1471 
1472     cluster_sectors = 0;
1473     ret = bdrv_get_info(out_bs, &bdi);
1474     if (ret < 0) {
1475         if (compress) {
1476             error_report("could not get block driver info");
1477             goto out;
1478         }
1479     } else {
1480         cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
1481     }
1482 
1483     if (compress) {
1484         if (cluster_sectors <= 0 || cluster_sectors > bufsectors) {
1485             error_report("invalid cluster size");
1486             ret = -1;
1487             goto out;
1488         }
1489         sector_num = 0;
1490 
1491         nb_sectors = total_sectors;
1492 
1493         for(;;) {
1494             int64_t bs_num;
1495             int remainder;
1496             uint8_t *buf2;
1497 
1498             nb_sectors = total_sectors - sector_num;
1499             if (nb_sectors <= 0)
1500                 break;
1501             if (nb_sectors >= cluster_sectors)
1502                 n = cluster_sectors;
1503             else
1504                 n = nb_sectors;
1505 
1506             bs_num = sector_num - bs_offset;
1507             assert (bs_num >= 0);
1508             remainder = n;
1509             buf2 = buf;
1510             while (remainder > 0) {
1511                 int nlow;
1512                 while (bs_num == bs_sectors) {
1513                     bs_i++;
1514                     assert (bs_i < bs_n);
1515                     bs_offset += bs_sectors;
1516                     bdrv_get_geometry(bs[bs_i], &bs_sectors);
1517                     bs_num = 0;
1518                     /* printf("changing part: sector_num=%" PRId64 ", "
1519                        "bs_i=%d, bs_offset=%" PRId64 ", bs_sectors=%" PRId64
1520                        "\n", sector_num, bs_i, bs_offset, bs_sectors); */
1521                 }
1522                 assert (bs_num < bs_sectors);
1523 
1524                 nlow = (remainder > bs_sectors - bs_num) ? bs_sectors - bs_num : remainder;
1525 
1526                 ret = bdrv_read(bs[bs_i], bs_num, buf2, nlow);
1527                 if (ret < 0) {
1528                     error_report("error while reading sector %" PRId64 ": %s",
1529                                  bs_num, strerror(-ret));
1530                     goto out;
1531                 }
1532 
1533                 buf2 += nlow * 512;
1534                 bs_num += nlow;
1535 
1536                 remainder -= nlow;
1537             }
1538             assert (remainder == 0);
1539 
1540             if (!buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)) {
1541                 ret = bdrv_write_compressed(out_bs, sector_num, buf, n);
1542                 if (ret != 0) {
1543                     error_report("error while compressing sector %" PRId64
1544                                  ": %s", sector_num, strerror(-ret));
1545                     goto out;
1546                 }
1547             }
1548             sector_num += n;
1549             qemu_progress_print(100.0 * sector_num / total_sectors, 0);
1550         }
1551         /* signal EOF to align */
1552         bdrv_write_compressed(out_bs, 0, NULL, 0);
1553     } else {
1554         int64_t sectors_to_read, sectors_read, sector_num_next_status;
1555         bool count_allocated_sectors;
1556         int has_zero_init = min_sparse ? bdrv_has_zero_init(out_bs) : 0;
1557 
1558         if (!has_zero_init && bdrv_can_write_zeroes_with_unmap(out_bs)) {
1559             ret = bdrv_make_zero(out_bs, BDRV_REQ_MAY_UNMAP);
1560             if (ret < 0) {
1561                 goto out;
1562             }
1563             has_zero_init = 1;
1564         }
1565 
1566         sectors_to_read = total_sectors;
1567         count_allocated_sectors = progress && (out_baseimg || has_zero_init);
1568 restart:
1569         sector_num = 0; // total number of sectors converted so far
1570         sectors_read = 0;
1571         sector_num_next_status = 0;
1572 
1573         for(;;) {
1574             nb_sectors = total_sectors - sector_num;
1575             if (nb_sectors <= 0) {
1576                 if (count_allocated_sectors) {
1577                     sectors_to_read = sectors_read;
1578                     count_allocated_sectors = false;
1579                     goto restart;
1580                 }
1581                 ret = 0;
1582                 break;
1583             }
1584 
1585             while (sector_num - bs_offset >= bs_sectors) {
1586                 bs_i ++;
1587                 assert (bs_i < bs_n);
1588                 bs_offset += bs_sectors;
1589                 bdrv_get_geometry(bs[bs_i], &bs_sectors);
1590                 /* printf("changing part: sector_num=%" PRId64 ", bs_i=%d, "
1591                   "bs_offset=%" PRId64 ", bs_sectors=%" PRId64 "\n",
1592                    sector_num, bs_i, bs_offset, bs_sectors); */
1593             }
1594 
1595             if ((out_baseimg || has_zero_init) &&
1596                 sector_num >= sector_num_next_status) {
1597                 n = nb_sectors > INT_MAX ? INT_MAX : nb_sectors;
1598                 ret = bdrv_get_block_status(bs[bs_i], sector_num - bs_offset,
1599                                             n, &n1);
1600                 if (ret < 0) {
1601                     error_report("error while reading block status of sector %"
1602                                  PRId64 ": %s", sector_num - bs_offset,
1603                                  strerror(-ret));
1604                     goto out;
1605                 }
1606                 /* If the output image is zero initialized, we are not working
1607                  * on a shared base and the input is zero we can skip the next
1608                  * n1 sectors */
1609                 if (has_zero_init && !out_baseimg && (ret & BDRV_BLOCK_ZERO)) {
1610                     sector_num += n1;
1611                     continue;
1612                 }
1613                 /* If the output image is being created as a copy on write
1614                  * image, assume that sectors which are unallocated in the
1615                  * input image are present in both the output's and input's
1616                  * base images (no need to copy them). */
1617                 if (out_baseimg) {
1618                     if (!(ret & BDRV_BLOCK_DATA)) {
1619                         sector_num += n1;
1620                         continue;
1621                     }
1622                     /* The next 'n1' sectors are allocated in the input image.
1623                      * Copy only those as they may be followed by unallocated
1624                      * sectors. */
1625                     nb_sectors = n1;
1626                 }
1627                 /* avoid redundant callouts to get_block_status */
1628                 sector_num_next_status = sector_num + n1;
1629             }
1630 
1631             n = MIN(nb_sectors, bufsectors);
1632 
1633             /* round down request length to an aligned sector, but
1634              * do not bother doing this on short requests. They happen
1635              * when we found an all-zero area, and the next sector to
1636              * write will not be sector_num + n. */
1637             if (cluster_sectors > 0 && n >= cluster_sectors) {
1638                 int64_t next_aligned_sector = (sector_num + n);
1639                 next_aligned_sector -= next_aligned_sector % cluster_sectors;
1640                 if (sector_num + n > next_aligned_sector) {
1641                     n = next_aligned_sector - sector_num;
1642                 }
1643             }
1644 
1645             n = MIN(n, bs_sectors - (sector_num - bs_offset));
1646 
1647             sectors_read += n;
1648             if (count_allocated_sectors) {
1649                 sector_num += n;
1650                 continue;
1651             }
1652 
1653             n1 = n;
1654             ret = bdrv_read(bs[bs_i], sector_num - bs_offset, buf, n);
1655             if (ret < 0) {
1656                 error_report("error while reading sector %" PRId64 ": %s",
1657                              sector_num - bs_offset, strerror(-ret));
1658                 goto out;
1659             }
1660             /* NOTE: at the same time we convert, we do not write zero
1661                sectors to have a chance to compress the image. Ideally, we
1662                should add a specific call to have the info to go faster */
1663             buf1 = buf;
1664             while (n > 0) {
1665                 if (!has_zero_init ||
1666                     is_allocated_sectors_min(buf1, n, &n1, min_sparse)) {
1667                     ret = bdrv_write(out_bs, sector_num, buf1, n1);
1668                     if (ret < 0) {
1669                         error_report("error while writing sector %" PRId64
1670                                      ": %s", sector_num, strerror(-ret));
1671                         goto out;
1672                     }
1673                 }
1674                 sector_num += n1;
1675                 n -= n1;
1676                 buf1 += n1 * 512;
1677             }
1678             qemu_progress_print(100.0 * sectors_read / sectors_to_read, 0);
1679         }
1680     }
1681 out:
1682     if (!ret) {
1683         qemu_progress_print(100, 0);
1684     }
1685     qemu_progress_end();
1686     free_option_parameters(create_options);
1687     free_option_parameters(param);
1688     qemu_vfree(buf);
1689     if (sn_opts) {
1690         qemu_opts_del(sn_opts);
1691     }
1692     if (out_bs) {
1693         bdrv_unref(out_bs);
1694     }
1695     if (bs) {
1696         for (bs_i = 0; bs_i < bs_n; bs_i++) {
1697             if (bs[bs_i]) {
1698                 bdrv_unref(bs[bs_i]);
1699             }
1700         }
1701         g_free(bs);
1702     }
1703 fail_getopt:
1704     g_free(options);
1705 
1706     if (ret) {
1707         return 1;
1708     }
1709     return 0;
1710 }
1711 
1712 
1713 static void dump_snapshots(BlockDriverState *bs)
1714 {
1715     QEMUSnapshotInfo *sn_tab, *sn;
1716     int nb_sns, i;
1717 
1718     nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1719     if (nb_sns <= 0)
1720         return;
1721     printf("Snapshot list:\n");
1722     bdrv_snapshot_dump(fprintf, stdout, NULL);
1723     printf("\n");
1724     for(i = 0; i < nb_sns; i++) {
1725         sn = &sn_tab[i];
1726         bdrv_snapshot_dump(fprintf, stdout, sn);
1727         printf("\n");
1728     }
1729     g_free(sn_tab);
1730 }
1731 
1732 static void dump_json_image_info_list(ImageInfoList *list)
1733 {
1734     Error *errp = NULL;
1735     QString *str;
1736     QmpOutputVisitor *ov = qmp_output_visitor_new();
1737     QObject *obj;
1738     visit_type_ImageInfoList(qmp_output_get_visitor(ov),
1739                              &list, NULL, &errp);
1740     obj = qmp_output_get_qobject(ov);
1741     str = qobject_to_json_pretty(obj);
1742     assert(str != NULL);
1743     printf("%s\n", qstring_get_str(str));
1744     qobject_decref(obj);
1745     qmp_output_visitor_cleanup(ov);
1746     QDECREF(str);
1747 }
1748 
1749 static void dump_json_image_info(ImageInfo *info)
1750 {
1751     Error *errp = NULL;
1752     QString *str;
1753     QmpOutputVisitor *ov = qmp_output_visitor_new();
1754     QObject *obj;
1755     visit_type_ImageInfo(qmp_output_get_visitor(ov),
1756                          &info, NULL, &errp);
1757     obj = qmp_output_get_qobject(ov);
1758     str = qobject_to_json_pretty(obj);
1759     assert(str != NULL);
1760     printf("%s\n", qstring_get_str(str));
1761     qobject_decref(obj);
1762     qmp_output_visitor_cleanup(ov);
1763     QDECREF(str);
1764 }
1765 
1766 static void dump_human_image_info_list(ImageInfoList *list)
1767 {
1768     ImageInfoList *elem;
1769     bool delim = false;
1770 
1771     for (elem = list; elem; elem = elem->next) {
1772         if (delim) {
1773             printf("\n");
1774         }
1775         delim = true;
1776 
1777         bdrv_image_info_dump(fprintf, stdout, elem->value);
1778     }
1779 }
1780 
1781 static gboolean str_equal_func(gconstpointer a, gconstpointer b)
1782 {
1783     return strcmp(a, b) == 0;
1784 }
1785 
1786 /**
1787  * Open an image file chain and return an ImageInfoList
1788  *
1789  * @filename: topmost image filename
1790  * @fmt: topmost image format (may be NULL to autodetect)
1791  * @chain: true  - enumerate entire backing file chain
1792  *         false - only topmost image file
1793  *
1794  * Returns a list of ImageInfo objects or NULL if there was an error opening an
1795  * image file.  If there was an error a message will have been printed to
1796  * stderr.
1797  */
1798 static ImageInfoList *collect_image_info_list(const char *filename,
1799                                               const char *fmt,
1800                                               bool chain)
1801 {
1802     ImageInfoList *head = NULL;
1803     ImageInfoList **last = &head;
1804     GHashTable *filenames;
1805     Error *err = NULL;
1806 
1807     filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
1808 
1809     while (filename) {
1810         BlockDriverState *bs;
1811         ImageInfo *info;
1812         ImageInfoList *elem;
1813 
1814         if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
1815             error_report("Backing file '%s' creates an infinite loop.",
1816                          filename);
1817             goto err;
1818         }
1819         g_hash_table_insert(filenames, (gpointer)filename, NULL);
1820 
1821         bs = bdrv_new_open("image", filename, fmt,
1822                            BDRV_O_FLAGS | BDRV_O_NO_BACKING, false, false);
1823         if (!bs) {
1824             goto err;
1825         }
1826 
1827         bdrv_query_image_info(bs, &info, &err);
1828         if (err) {
1829             error_report("%s", error_get_pretty(err));
1830             error_free(err);
1831             bdrv_unref(bs);
1832             goto err;
1833         }
1834 
1835         elem = g_new0(ImageInfoList, 1);
1836         elem->value = info;
1837         *last = elem;
1838         last = &elem->next;
1839 
1840         bdrv_unref(bs);
1841 
1842         filename = fmt = NULL;
1843         if (chain) {
1844             if (info->has_full_backing_filename) {
1845                 filename = info->full_backing_filename;
1846             } else if (info->has_backing_filename) {
1847                 filename = info->backing_filename;
1848             }
1849             if (info->has_backing_filename_format) {
1850                 fmt = info->backing_filename_format;
1851             }
1852         }
1853     }
1854     g_hash_table_destroy(filenames);
1855     return head;
1856 
1857 err:
1858     qapi_free_ImageInfoList(head);
1859     g_hash_table_destroy(filenames);
1860     return NULL;
1861 }
1862 
1863 static int img_info(int argc, char **argv)
1864 {
1865     int c;
1866     OutputFormat output_format = OFORMAT_HUMAN;
1867     bool chain = false;
1868     const char *filename, *fmt, *output;
1869     ImageInfoList *list;
1870 
1871     fmt = NULL;
1872     output = NULL;
1873     for(;;) {
1874         int option_index = 0;
1875         static const struct option long_options[] = {
1876             {"help", no_argument, 0, 'h'},
1877             {"format", required_argument, 0, 'f'},
1878             {"output", required_argument, 0, OPTION_OUTPUT},
1879             {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
1880             {0, 0, 0, 0}
1881         };
1882         c = getopt_long(argc, argv, "f:h",
1883                         long_options, &option_index);
1884         if (c == -1) {
1885             break;
1886         }
1887         switch(c) {
1888         case '?':
1889         case 'h':
1890             help();
1891             break;
1892         case 'f':
1893             fmt = optarg;
1894             break;
1895         case OPTION_OUTPUT:
1896             output = optarg;
1897             break;
1898         case OPTION_BACKING_CHAIN:
1899             chain = true;
1900             break;
1901         }
1902     }
1903     if (optind != argc - 1) {
1904         error_exit("Expecting one image file name");
1905     }
1906     filename = argv[optind++];
1907 
1908     if (output && !strcmp(output, "json")) {
1909         output_format = OFORMAT_JSON;
1910     } else if (output && !strcmp(output, "human")) {
1911         output_format = OFORMAT_HUMAN;
1912     } else if (output) {
1913         error_report("--output must be used with human or json as argument.");
1914         return 1;
1915     }
1916 
1917     list = collect_image_info_list(filename, fmt, chain);
1918     if (!list) {
1919         return 1;
1920     }
1921 
1922     switch (output_format) {
1923     case OFORMAT_HUMAN:
1924         dump_human_image_info_list(list);
1925         break;
1926     case OFORMAT_JSON:
1927         if (chain) {
1928             dump_json_image_info_list(list);
1929         } else {
1930             dump_json_image_info(list->value);
1931         }
1932         break;
1933     }
1934 
1935     qapi_free_ImageInfoList(list);
1936     return 0;
1937 }
1938 
1939 
1940 typedef struct MapEntry {
1941     int flags;
1942     int depth;
1943     int64_t start;
1944     int64_t length;
1945     int64_t offset;
1946     BlockDriverState *bs;
1947 } MapEntry;
1948 
1949 static void dump_map_entry(OutputFormat output_format, MapEntry *e,
1950                            MapEntry *next)
1951 {
1952     switch (output_format) {
1953     case OFORMAT_HUMAN:
1954         if ((e->flags & BDRV_BLOCK_DATA) &&
1955             !(e->flags & BDRV_BLOCK_OFFSET_VALID)) {
1956             error_report("File contains external, encrypted or compressed clusters.");
1957             exit(1);
1958         }
1959         if ((e->flags & (BDRV_BLOCK_DATA|BDRV_BLOCK_ZERO)) == BDRV_BLOCK_DATA) {
1960             printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
1961                    e->start, e->length, e->offset, e->bs->filename);
1962         }
1963         /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
1964          * Modify the flags here to allow more coalescing.
1965          */
1966         if (next &&
1967             (next->flags & (BDRV_BLOCK_DATA|BDRV_BLOCK_ZERO)) != BDRV_BLOCK_DATA) {
1968             next->flags &= ~BDRV_BLOCK_DATA;
1969             next->flags |= BDRV_BLOCK_ZERO;
1970         }
1971         break;
1972     case OFORMAT_JSON:
1973         printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64", \"depth\": %d,"
1974                " \"zero\": %s, \"data\": %s",
1975                (e->start == 0 ? "[" : ",\n"),
1976                e->start, e->length, e->depth,
1977                (e->flags & BDRV_BLOCK_ZERO) ? "true" : "false",
1978                (e->flags & BDRV_BLOCK_DATA) ? "true" : "false");
1979         if (e->flags & BDRV_BLOCK_OFFSET_VALID) {
1980             printf(", \"offset\": %"PRId64"", e->offset);
1981         }
1982         putchar('}');
1983 
1984         if (!next) {
1985             printf("]\n");
1986         }
1987         break;
1988     }
1989 }
1990 
1991 static int get_block_status(BlockDriverState *bs, int64_t sector_num,
1992                             int nb_sectors, MapEntry *e)
1993 {
1994     int64_t ret;
1995     int depth;
1996 
1997     /* As an optimization, we could cache the current range of unallocated
1998      * clusters in each file of the chain, and avoid querying the same
1999      * range repeatedly.
2000      */
2001 
2002     depth = 0;
2003     for (;;) {
2004         ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors);
2005         if (ret < 0) {
2006             return ret;
2007         }
2008         assert(nb_sectors);
2009         if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2010             break;
2011         }
2012         bs = bs->backing_hd;
2013         if (bs == NULL) {
2014             ret = 0;
2015             break;
2016         }
2017 
2018         depth++;
2019     }
2020 
2021     e->start = sector_num * BDRV_SECTOR_SIZE;
2022     e->length = nb_sectors * BDRV_SECTOR_SIZE;
2023     e->flags = ret & ~BDRV_BLOCK_OFFSET_MASK;
2024     e->offset = ret & BDRV_BLOCK_OFFSET_MASK;
2025     e->depth = depth;
2026     e->bs = bs;
2027     return 0;
2028 }
2029 
2030 static int img_map(int argc, char **argv)
2031 {
2032     int c;
2033     OutputFormat output_format = OFORMAT_HUMAN;
2034     BlockDriverState *bs;
2035     const char *filename, *fmt, *output;
2036     int64_t length;
2037     MapEntry curr = { .length = 0 }, next;
2038     int ret = 0;
2039 
2040     fmt = NULL;
2041     output = NULL;
2042     for (;;) {
2043         int option_index = 0;
2044         static const struct option long_options[] = {
2045             {"help", no_argument, 0, 'h'},
2046             {"format", required_argument, 0, 'f'},
2047             {"output", required_argument, 0, OPTION_OUTPUT},
2048             {0, 0, 0, 0}
2049         };
2050         c = getopt_long(argc, argv, "f:h",
2051                         long_options, &option_index);
2052         if (c == -1) {
2053             break;
2054         }
2055         switch (c) {
2056         case '?':
2057         case 'h':
2058             help();
2059             break;
2060         case 'f':
2061             fmt = optarg;
2062             break;
2063         case OPTION_OUTPUT:
2064             output = optarg;
2065             break;
2066         }
2067     }
2068     if (optind != argc - 1) {
2069         error_exit("Expecting one image file name");
2070     }
2071     filename = argv[optind];
2072 
2073     if (output && !strcmp(output, "json")) {
2074         output_format = OFORMAT_JSON;
2075     } else if (output && !strcmp(output, "human")) {
2076         output_format = OFORMAT_HUMAN;
2077     } else if (output) {
2078         error_report("--output must be used with human or json as argument.");
2079         return 1;
2080     }
2081 
2082     bs = bdrv_new_open("image", filename, fmt, BDRV_O_FLAGS, true, false);
2083     if (!bs) {
2084         return 1;
2085     }
2086 
2087     if (output_format == OFORMAT_HUMAN) {
2088         printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2089     }
2090 
2091     length = bdrv_getlength(bs);
2092     while (curr.start + curr.length < length) {
2093         int64_t nsectors_left;
2094         int64_t sector_num;
2095         int n;
2096 
2097         sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS;
2098 
2099         /* Probe up to 1 GiB at a time.  */
2100         nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num;
2101         n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left);
2102         ret = get_block_status(bs, sector_num, n, &next);
2103 
2104         if (ret < 0) {
2105             error_report("Could not read file metadata: %s", strerror(-ret));
2106             goto out;
2107         }
2108 
2109         if (curr.length != 0 && curr.flags == next.flags &&
2110             curr.depth == next.depth &&
2111             ((curr.flags & BDRV_BLOCK_OFFSET_VALID) == 0 ||
2112              curr.offset + curr.length == next.offset)) {
2113             curr.length += next.length;
2114             continue;
2115         }
2116 
2117         if (curr.length > 0) {
2118             dump_map_entry(output_format, &curr, &next);
2119         }
2120         curr = next;
2121     }
2122 
2123     dump_map_entry(output_format, &curr, NULL);
2124 
2125 out:
2126     bdrv_unref(bs);
2127     return ret < 0;
2128 }
2129 
2130 #define SNAPSHOT_LIST   1
2131 #define SNAPSHOT_CREATE 2
2132 #define SNAPSHOT_APPLY  3
2133 #define SNAPSHOT_DELETE 4
2134 
2135 static int img_snapshot(int argc, char **argv)
2136 {
2137     BlockDriverState *bs;
2138     QEMUSnapshotInfo sn;
2139     char *filename, *snapshot_name = NULL;
2140     int c, ret = 0, bdrv_oflags;
2141     int action = 0;
2142     qemu_timeval tv;
2143     bool quiet = false;
2144     Error *err = NULL;
2145 
2146     bdrv_oflags = BDRV_O_FLAGS | BDRV_O_RDWR;
2147     /* Parse commandline parameters */
2148     for(;;) {
2149         c = getopt(argc, argv, "la:c:d:hq");
2150         if (c == -1) {
2151             break;
2152         }
2153         switch(c) {
2154         case '?':
2155         case 'h':
2156             help();
2157             return 0;
2158         case 'l':
2159             if (action) {
2160                 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
2161                 return 0;
2162             }
2163             action = SNAPSHOT_LIST;
2164             bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
2165             break;
2166         case 'a':
2167             if (action) {
2168                 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
2169                 return 0;
2170             }
2171             action = SNAPSHOT_APPLY;
2172             snapshot_name = optarg;
2173             break;
2174         case 'c':
2175             if (action) {
2176                 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
2177                 return 0;
2178             }
2179             action = SNAPSHOT_CREATE;
2180             snapshot_name = optarg;
2181             break;
2182         case 'd':
2183             if (action) {
2184                 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
2185                 return 0;
2186             }
2187             action = SNAPSHOT_DELETE;
2188             snapshot_name = optarg;
2189             break;
2190         case 'q':
2191             quiet = true;
2192             break;
2193         }
2194     }
2195 
2196     if (optind != argc - 1) {
2197         error_exit("Expecting one image file name");
2198     }
2199     filename = argv[optind++];
2200 
2201     /* Open the image */
2202     bs = bdrv_new_open("image", filename, NULL, bdrv_oflags, true, quiet);
2203     if (!bs) {
2204         return 1;
2205     }
2206 
2207     /* Perform the requested action */
2208     switch(action) {
2209     case SNAPSHOT_LIST:
2210         dump_snapshots(bs);
2211         break;
2212 
2213     case SNAPSHOT_CREATE:
2214         memset(&sn, 0, sizeof(sn));
2215         pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
2216 
2217         qemu_gettimeofday(&tv);
2218         sn.date_sec = tv.tv_sec;
2219         sn.date_nsec = tv.tv_usec * 1000;
2220 
2221         ret = bdrv_snapshot_create(bs, &sn);
2222         if (ret) {
2223             error_report("Could not create snapshot '%s': %d (%s)",
2224                 snapshot_name, ret, strerror(-ret));
2225         }
2226         break;
2227 
2228     case SNAPSHOT_APPLY:
2229         ret = bdrv_snapshot_goto(bs, snapshot_name);
2230         if (ret) {
2231             error_report("Could not apply snapshot '%s': %d (%s)",
2232                 snapshot_name, ret, strerror(-ret));
2233         }
2234         break;
2235 
2236     case SNAPSHOT_DELETE:
2237         bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
2238         if (err) {
2239             error_report("Could not delete snapshot '%s': (%s)",
2240                          snapshot_name, error_get_pretty(err));
2241             error_free(err);
2242             ret = 1;
2243         }
2244         break;
2245     }
2246 
2247     /* Cleanup */
2248     bdrv_unref(bs);
2249     if (ret) {
2250         return 1;
2251     }
2252     return 0;
2253 }
2254 
2255 static int img_rebase(int argc, char **argv)
2256 {
2257     BlockDriverState *bs, *bs_old_backing = NULL, *bs_new_backing = NULL;
2258     BlockDriver *old_backing_drv, *new_backing_drv;
2259     char *filename;
2260     const char *fmt, *cache, *out_basefmt, *out_baseimg;
2261     int c, flags, ret;
2262     int unsafe = 0;
2263     int progress = 0;
2264     bool quiet = false;
2265     Error *local_err = NULL;
2266 
2267     /* Parse commandline parameters */
2268     fmt = NULL;
2269     cache = BDRV_DEFAULT_CACHE;
2270     out_baseimg = NULL;
2271     out_basefmt = NULL;
2272     for(;;) {
2273         c = getopt(argc, argv, "uhf:F:b:pt:q");
2274         if (c == -1) {
2275             break;
2276         }
2277         switch(c) {
2278         case '?':
2279         case 'h':
2280             help();
2281             return 0;
2282         case 'f':
2283             fmt = optarg;
2284             break;
2285         case 'F':
2286             out_basefmt = optarg;
2287             break;
2288         case 'b':
2289             out_baseimg = optarg;
2290             break;
2291         case 'u':
2292             unsafe = 1;
2293             break;
2294         case 'p':
2295             progress = 1;
2296             break;
2297         case 't':
2298             cache = optarg;
2299             break;
2300         case 'q':
2301             quiet = true;
2302             break;
2303         }
2304     }
2305 
2306     if (quiet) {
2307         progress = 0;
2308     }
2309 
2310     if (optind != argc - 1) {
2311         error_exit("Expecting one image file name");
2312     }
2313     if (!unsafe && !out_baseimg) {
2314         error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
2315     }
2316     filename = argv[optind++];
2317 
2318     qemu_progress_init(progress, 2.0);
2319     qemu_progress_print(0, 100);
2320 
2321     flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
2322     ret = bdrv_parse_cache_flags(cache, &flags);
2323     if (ret < 0) {
2324         error_report("Invalid cache option: %s", cache);
2325         return -1;
2326     }
2327 
2328     /*
2329      * Open the images.
2330      *
2331      * Ignore the old backing file for unsafe rebase in case we want to correct
2332      * the reference to a renamed or moved backing file.
2333      */
2334     bs = bdrv_new_open("image", filename, fmt, flags, true, quiet);
2335     if (!bs) {
2336         return 1;
2337     }
2338 
2339     /* Find the right drivers for the backing files */
2340     old_backing_drv = NULL;
2341     new_backing_drv = NULL;
2342 
2343     if (!unsafe && bs->backing_format[0] != '\0') {
2344         old_backing_drv = bdrv_find_format(bs->backing_format);
2345         if (old_backing_drv == NULL) {
2346             error_report("Invalid format name: '%s'", bs->backing_format);
2347             ret = -1;
2348             goto out;
2349         }
2350     }
2351 
2352     if (out_basefmt != NULL) {
2353         new_backing_drv = bdrv_find_format(out_basefmt);
2354         if (new_backing_drv == NULL) {
2355             error_report("Invalid format name: '%s'", out_basefmt);
2356             ret = -1;
2357             goto out;
2358         }
2359     }
2360 
2361     /* For safe rebasing we need to compare old and new backing file */
2362     if (unsafe) {
2363         /* Make the compiler happy */
2364         bs_old_backing = NULL;
2365         bs_new_backing = NULL;
2366     } else {
2367         char backing_name[1024];
2368 
2369         bs_old_backing = bdrv_new("old_backing", &error_abort);
2370         bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
2371         ret = bdrv_open(&bs_old_backing, backing_name, NULL, NULL, BDRV_O_FLAGS,
2372                         old_backing_drv, &local_err);
2373         if (ret) {
2374             error_report("Could not open old backing file '%s': %s",
2375                          backing_name, error_get_pretty(local_err));
2376             error_free(local_err);
2377             goto out;
2378         }
2379         if (out_baseimg[0]) {
2380             bs_new_backing = bdrv_new("new_backing", &error_abort);
2381             ret = bdrv_open(&bs_new_backing, out_baseimg, NULL, NULL,
2382                             BDRV_O_FLAGS, new_backing_drv, &local_err);
2383             if (ret) {
2384                 error_report("Could not open new backing file '%s': %s",
2385                              out_baseimg, error_get_pretty(local_err));
2386                 error_free(local_err);
2387                 goto out;
2388             }
2389         }
2390     }
2391 
2392     /*
2393      * Check each unallocated cluster in the COW file. If it is unallocated,
2394      * accesses go to the backing file. We must therefore compare this cluster
2395      * in the old and new backing file, and if they differ we need to copy it
2396      * from the old backing file into the COW file.
2397      *
2398      * If qemu-img crashes during this step, no harm is done. The content of
2399      * the image is the same as the original one at any time.
2400      */
2401     if (!unsafe) {
2402         uint64_t num_sectors;
2403         uint64_t old_backing_num_sectors;
2404         uint64_t new_backing_num_sectors = 0;
2405         uint64_t sector;
2406         int n;
2407         uint8_t * buf_old;
2408         uint8_t * buf_new;
2409         float local_progress = 0;
2410 
2411         buf_old = qemu_blockalign(bs, IO_BUF_SIZE);
2412         buf_new = qemu_blockalign(bs, IO_BUF_SIZE);
2413 
2414         bdrv_get_geometry(bs, &num_sectors);
2415         bdrv_get_geometry(bs_old_backing, &old_backing_num_sectors);
2416         if (bs_new_backing) {
2417             bdrv_get_geometry(bs_new_backing, &new_backing_num_sectors);
2418         }
2419 
2420         if (num_sectors != 0) {
2421             local_progress = (float)100 /
2422                 (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
2423         }
2424 
2425         for (sector = 0; sector < num_sectors; sector += n) {
2426 
2427             /* How many sectors can we handle with the next read? */
2428             if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
2429                 n = (IO_BUF_SIZE / 512);
2430             } else {
2431                 n = num_sectors - sector;
2432             }
2433 
2434             /* If the cluster is allocated, we don't need to take action */
2435             ret = bdrv_is_allocated(bs, sector, n, &n);
2436             if (ret < 0) {
2437                 error_report("error while reading image metadata: %s",
2438                              strerror(-ret));
2439                 goto out;
2440             }
2441             if (ret) {
2442                 continue;
2443             }
2444 
2445             /*
2446              * Read old and new backing file and take into consideration that
2447              * backing files may be smaller than the COW image.
2448              */
2449             if (sector >= old_backing_num_sectors) {
2450                 memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
2451             } else {
2452                 if (sector + n > old_backing_num_sectors) {
2453                     n = old_backing_num_sectors - sector;
2454                 }
2455 
2456                 ret = bdrv_read(bs_old_backing, sector, buf_old, n);
2457                 if (ret < 0) {
2458                     error_report("error while reading from old backing file");
2459                     goto out;
2460                 }
2461             }
2462 
2463             if (sector >= new_backing_num_sectors || !bs_new_backing) {
2464                 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
2465             } else {
2466                 if (sector + n > new_backing_num_sectors) {
2467                     n = new_backing_num_sectors - sector;
2468                 }
2469 
2470                 ret = bdrv_read(bs_new_backing, sector, buf_new, n);
2471                 if (ret < 0) {
2472                     error_report("error while reading from new backing file");
2473                     goto out;
2474                 }
2475             }
2476 
2477             /* If they differ, we need to write to the COW file */
2478             uint64_t written = 0;
2479 
2480             while (written < n) {
2481                 int pnum;
2482 
2483                 if (compare_sectors(buf_old + written * 512,
2484                     buf_new + written * 512, n - written, &pnum))
2485                 {
2486                     ret = bdrv_write(bs, sector + written,
2487                         buf_old + written * 512, pnum);
2488                     if (ret < 0) {
2489                         error_report("Error while writing to COW image: %s",
2490                             strerror(-ret));
2491                         goto out;
2492                     }
2493                 }
2494 
2495                 written += pnum;
2496             }
2497             qemu_progress_print(local_progress, 100);
2498         }
2499 
2500         qemu_vfree(buf_old);
2501         qemu_vfree(buf_new);
2502     }
2503 
2504     /*
2505      * Change the backing file. All clusters that are different from the old
2506      * backing file are overwritten in the COW file now, so the visible content
2507      * doesn't change when we switch the backing file.
2508      */
2509     if (out_baseimg && *out_baseimg) {
2510         ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
2511     } else {
2512         ret = bdrv_change_backing_file(bs, NULL, NULL);
2513     }
2514 
2515     if (ret == -ENOSPC) {
2516         error_report("Could not change the backing file to '%s': No "
2517                      "space left in the file header", out_baseimg);
2518     } else if (ret < 0) {
2519         error_report("Could not change the backing file to '%s': %s",
2520             out_baseimg, strerror(-ret));
2521     }
2522 
2523     qemu_progress_print(100, 0);
2524     /*
2525      * TODO At this point it is possible to check if any clusters that are
2526      * allocated in the COW file are the same in the backing file. If so, they
2527      * could be dropped from the COW file. Don't do this before switching the
2528      * backing file, in case of a crash this would lead to corruption.
2529      */
2530 out:
2531     qemu_progress_end();
2532     /* Cleanup */
2533     if (!unsafe) {
2534         if (bs_old_backing != NULL) {
2535             bdrv_unref(bs_old_backing);
2536         }
2537         if (bs_new_backing != NULL) {
2538             bdrv_unref(bs_new_backing);
2539         }
2540     }
2541 
2542     bdrv_unref(bs);
2543     if (ret) {
2544         return 1;
2545     }
2546     return 0;
2547 }
2548 
2549 static int img_resize(int argc, char **argv)
2550 {
2551     int c, ret, relative;
2552     const char *filename, *fmt, *size;
2553     int64_t n, total_size;
2554     bool quiet = false;
2555     BlockDriverState *bs = NULL;
2556     QemuOpts *param;
2557     static QemuOptsList resize_options = {
2558         .name = "resize_options",
2559         .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
2560         .desc = {
2561             {
2562                 .name = BLOCK_OPT_SIZE,
2563                 .type = QEMU_OPT_SIZE,
2564                 .help = "Virtual disk size"
2565             }, {
2566                 /* end of list */
2567             }
2568         },
2569     };
2570 
2571     /* Remove size from argv manually so that negative numbers are not treated
2572      * as options by getopt. */
2573     if (argc < 3) {
2574         error_exit("Not enough arguments");
2575         return 1;
2576     }
2577 
2578     size = argv[--argc];
2579 
2580     /* Parse getopt arguments */
2581     fmt = NULL;
2582     for(;;) {
2583         c = getopt(argc, argv, "f:hq");
2584         if (c == -1) {
2585             break;
2586         }
2587         switch(c) {
2588         case '?':
2589         case 'h':
2590             help();
2591             break;
2592         case 'f':
2593             fmt = optarg;
2594             break;
2595         case 'q':
2596             quiet = true;
2597             break;
2598         }
2599     }
2600     if (optind != argc - 1) {
2601         error_exit("Expecting one image file name");
2602     }
2603     filename = argv[optind++];
2604 
2605     /* Choose grow, shrink, or absolute resize mode */
2606     switch (size[0]) {
2607     case '+':
2608         relative = 1;
2609         size++;
2610         break;
2611     case '-':
2612         relative = -1;
2613         size++;
2614         break;
2615     default:
2616         relative = 0;
2617         break;
2618     }
2619 
2620     /* Parse size */
2621     param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
2622     if (qemu_opt_set(param, BLOCK_OPT_SIZE, size)) {
2623         /* Error message already printed when size parsing fails */
2624         ret = -1;
2625         qemu_opts_del(param);
2626         goto out;
2627     }
2628     n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
2629     qemu_opts_del(param);
2630 
2631     bs = bdrv_new_open("image", filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR,
2632                        true, quiet);
2633     if (!bs) {
2634         ret = -1;
2635         goto out;
2636     }
2637 
2638     if (relative) {
2639         total_size = bdrv_getlength(bs) + n * relative;
2640     } else {
2641         total_size = n;
2642     }
2643     if (total_size <= 0) {
2644         error_report("New image size must be positive");
2645         ret = -1;
2646         goto out;
2647     }
2648 
2649     ret = bdrv_truncate(bs, total_size);
2650     switch (ret) {
2651     case 0:
2652         qprintf(quiet, "Image resized.\n");
2653         break;
2654     case -ENOTSUP:
2655         error_report("This image does not support resize");
2656         break;
2657     case -EACCES:
2658         error_report("Image is read-only");
2659         break;
2660     default:
2661         error_report("Error resizing image (%d)", -ret);
2662         break;
2663     }
2664 out:
2665     if (bs) {
2666         bdrv_unref(bs);
2667     }
2668     if (ret) {
2669         return 1;
2670     }
2671     return 0;
2672 }
2673 
2674 static int img_amend(int argc, char **argv)
2675 {
2676     int c, ret = 0;
2677     char *options = NULL;
2678     QEMUOptionParameter *create_options = NULL, *options_param = NULL;
2679     const char *fmt = NULL, *filename;
2680     bool quiet = false;
2681     BlockDriverState *bs = NULL;
2682 
2683     for (;;) {
2684         c = getopt(argc, argv, "hqf:o:");
2685         if (c == -1) {
2686             break;
2687         }
2688 
2689         switch (c) {
2690             case 'h':
2691             case '?':
2692                 help();
2693                 break;
2694             case 'o':
2695                 if (!is_valid_option_list(optarg)) {
2696                     error_report("Invalid option list: %s", optarg);
2697                     ret = -1;
2698                     goto out;
2699                 }
2700                 if (!options) {
2701                     options = g_strdup(optarg);
2702                 } else {
2703                     char *old_options = options;
2704                     options = g_strdup_printf("%s,%s", options, optarg);
2705                     g_free(old_options);
2706                 }
2707                 break;
2708             case 'f':
2709                 fmt = optarg;
2710                 break;
2711             case 'q':
2712                 quiet = true;
2713                 break;
2714         }
2715     }
2716 
2717     if (!options) {
2718         error_exit("Must specify options (-o)");
2719     }
2720 
2721     filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
2722     if (fmt && has_help_option(options)) {
2723         /* If a format is explicitly specified (and possibly no filename is
2724          * given), print option help here */
2725         ret = print_block_option_help(filename, fmt);
2726         goto out;
2727     }
2728 
2729     if (optind != argc - 1) {
2730         error_exit("Expecting one image file name");
2731     }
2732 
2733     bs = bdrv_new_open("image", filename, fmt,
2734                        BDRV_O_FLAGS | BDRV_O_RDWR, true, quiet);
2735     if (!bs) {
2736         error_report("Could not open image '%s'", filename);
2737         ret = -1;
2738         goto out;
2739     }
2740 
2741     fmt = bs->drv->format_name;
2742 
2743     if (has_help_option(options)) {
2744         /* If the format was auto-detected, print option help here */
2745         ret = print_block_option_help(filename, fmt);
2746         goto out;
2747     }
2748 
2749     create_options = append_option_parameters(create_options,
2750             bs->drv->create_options);
2751     options_param = parse_option_parameters(options, create_options,
2752             options_param);
2753     if (options_param == NULL) {
2754         error_report("Invalid options for file format '%s'", fmt);
2755         ret = -1;
2756         goto out;
2757     }
2758 
2759     ret = bdrv_amend_options(bs, options_param);
2760     if (ret < 0) {
2761         error_report("Error while amending options: %s", strerror(-ret));
2762         goto out;
2763     }
2764 
2765 out:
2766     if (bs) {
2767         bdrv_unref(bs);
2768     }
2769     free_option_parameters(create_options);
2770     free_option_parameters(options_param);
2771     g_free(options);
2772 
2773     if (ret) {
2774         return 1;
2775     }
2776     return 0;
2777 }
2778 
2779 static const img_cmd_t img_cmds[] = {
2780 #define DEF(option, callback, arg_string)        \
2781     { option, callback },
2782 #include "qemu-img-cmds.h"
2783 #undef DEF
2784 #undef GEN_DOCS
2785     { NULL, NULL, },
2786 };
2787 
2788 int main(int argc, char **argv)
2789 {
2790     const img_cmd_t *cmd;
2791     const char *cmdname;
2792 
2793 #ifdef CONFIG_POSIX
2794     signal(SIGPIPE, SIG_IGN);
2795 #endif
2796 
2797     error_set_progname(argv[0]);
2798     qemu_init_exec_dir(argv[0]);
2799 
2800     qemu_init_main_loop();
2801     bdrv_init();
2802     if (argc < 2) {
2803         error_exit("Not enough arguments");
2804     }
2805     cmdname = argv[1];
2806     argc--; argv++;
2807 
2808     /* find the command */
2809     for(cmd = img_cmds; cmd->name != NULL; cmd++) {
2810         if (!strcmp(cmdname, cmd->name)) {
2811             return cmd->handler(argc, argv);
2812         }
2813     }
2814 
2815     /* not found */
2816     error_exit("Command not found: %s", cmdname);
2817 }
2818