qemu-img: add support for rate limit in qemu-img commit
[qemu.git] / qemu-img.c
1 /*
2 * QEMU disk image utility
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25 #include "qemu/osdep.h"
26 #include <getopt.h>
27
28 #include "qemu-common.h"
29 #include "qemu-version.h"
30 #include "qapi/error.h"
31 #include "qapi/qapi-commands-block-core.h"
32 #include "qapi/qapi-visit-block-core.h"
33 #include "qapi/qobject-output-visitor.h"
34 #include "qapi/qmp/qjson.h"
35 #include "qapi/qmp/qdict.h"
36 #include "qapi/qmp/qstring.h"
37 #include "qemu/cutils.h"
38 #include "qemu/config-file.h"
39 #include "qemu/option.h"
40 #include "qemu/error-report.h"
41 #include "qemu/log.h"
42 #include "qemu/main-loop.h"
43 #include "qemu/module.h"
44 #include "qemu/sockets.h"
45 #include "qemu/units.h"
46 #include "qom/object_interfaces.h"
47 #include "sysemu/block-backend.h"
48 #include "block/block_int.h"
49 #include "block/blockjob.h"
50 #include "block/qapi.h"
51 #include "crypto/init.h"
52 #include "trace/control.h"
53
54 #define QEMU_IMG_VERSION "qemu-img version " QEMU_FULL_VERSION \
55 "\n" QEMU_COPYRIGHT "\n"
56
57 typedef struct img_cmd_t {
58 const char *name;
59 int (*handler)(int argc, char **argv);
60 } img_cmd_t;
61
62 enum {
63 OPTION_OUTPUT = 256,
64 OPTION_BACKING_CHAIN = 257,
65 OPTION_OBJECT = 258,
66 OPTION_IMAGE_OPTS = 259,
67 OPTION_PATTERN = 260,
68 OPTION_FLUSH_INTERVAL = 261,
69 OPTION_NO_DRAIN = 262,
70 OPTION_TARGET_IMAGE_OPTS = 263,
71 OPTION_SIZE = 264,
72 OPTION_PREALLOCATION = 265,
73 OPTION_SHRINK = 266,
74 OPTION_SALVAGE = 267,
75 OPTION_TARGET_IS_ZERO = 268,
76 OPTION_ADD = 269,
77 OPTION_REMOVE = 270,
78 OPTION_CLEAR = 271,
79 OPTION_ENABLE = 272,
80 OPTION_DISABLE = 273,
81 OPTION_MERGE = 274,
82 OPTION_BITMAPS = 275,
83 OPTION_FORCE = 276,
84 };
85
86 typedef enum OutputFormat {
87 OFORMAT_JSON,
88 OFORMAT_HUMAN,
89 } OutputFormat;
90
91 /* Default to cache=writeback as data integrity is not important for qemu-img */
92 #define BDRV_DEFAULT_CACHE "writeback"
93
94 static void format_print(void *opaque, const char *name)
95 {
96 printf(" %s", name);
97 }
98
99 static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
100 {
101 va_list ap;
102
103 va_start(ap, fmt);
104 error_vreport(fmt, ap);
105 va_end(ap);
106
107 error_printf("Try 'qemu-img --help' for more information\n");
108 exit(EXIT_FAILURE);
109 }
110
111 static void QEMU_NORETURN missing_argument(const char *option)
112 {
113 error_exit("missing argument for option '%s'", option);
114 }
115
116 static void QEMU_NORETURN unrecognized_option(const char *option)
117 {
118 error_exit("unrecognized option '%s'", option);
119 }
120
121 /* Please keep in synch with docs/tools/qemu-img.rst */
122 static void QEMU_NORETURN help(void)
123 {
124 const char *help_msg =
125 QEMU_IMG_VERSION
126 "usage: qemu-img [standard options] command [command options]\n"
127 "QEMU disk image utility\n"
128 "\n"
129 " '-h', '--help' display this help and exit\n"
130 " '-V', '--version' output version information and exit\n"
131 " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
132 " specify tracing options\n"
133 "\n"
134 "Command syntax:\n"
135 #define DEF(option, callback, arg_string) \
136 " " arg_string "\n"
137 #include "qemu-img-cmds.h"
138 #undef DEF
139 "\n"
140 "Command parameters:\n"
141 " 'filename' is a disk image filename\n"
142 " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
143 " manual page for a description of the object properties. The most common\n"
144 " object type is a 'secret', which is used to supply passwords and/or\n"
145 " encryption keys.\n"
146 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
147 " 'cache' is the cache mode used to write the output disk image, the valid\n"
148 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
149 " 'directsync' and 'unsafe' (default for convert)\n"
150 " 'src_cache' is the cache mode used to read input disk images, the valid\n"
151 " options are the same as for the 'cache' option\n"
152 " 'size' is the disk image size in bytes. Optional suffixes\n"
153 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
154 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
155 " supported. 'b' is ignored.\n"
156 " 'output_filename' is the destination disk image filename\n"
157 " 'output_fmt' is the destination format\n"
158 " 'options' is a comma separated list of format specific options in a\n"
159 " name=value format. Use -o ? for an overview of the options supported by the\n"
160 " used format\n"
161 " 'snapshot_param' is param used for internal snapshot, format\n"
162 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
163 " '[ID_OR_NAME]'\n"
164 " '-c' indicates that target image must be compressed (qcow format only)\n"
165 " '-u' allows unsafe backing chains. For rebasing, it is assumed that old and\n"
166 " new backing file match exactly. The image doesn't need a working\n"
167 " backing file before rebasing in this case (useful for renaming the\n"
168 " backing file). For image creation, allow creating without attempting\n"
169 " to open the backing file.\n"
170 " '-h' with or without a command shows this help and lists the supported formats\n"
171 " '-p' show progress of command (only certain commands)\n"
172 " '-q' use Quiet mode - do not print any output (except errors)\n"
173 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
174 " contain only zeros for qemu-img to create a sparse image during\n"
175 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
176 " unallocated or zero sectors, and the destination image will always be\n"
177 " fully allocated\n"
178 " '--output' takes the format in which the output must be done (human or json)\n"
179 " '-n' skips the target volume creation (useful if the volume is created\n"
180 " prior to running qemu-img)\n"
181 "\n"
182 "Parameters to bitmap subcommand:\n"
183 " 'bitmap' is the name of the bitmap to manipulate, through one or more\n"
184 " actions from '--add', '--remove', '--clear', '--enable', '--disable',\n"
185 " or '--merge source'\n"
186 " '-g granularity' sets the granularity for '--add' actions\n"
187 " '-b source' and '-F src_fmt' tell '--merge' actions to find the source\n"
188 " bitmaps from an alternative file\n"
189 "\n"
190 "Parameters to check subcommand:\n"
191 " '-r' tries to repair any inconsistencies that are found during the check.\n"
192 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
193 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
194 " hiding corruption that has already occurred.\n"
195 "\n"
196 "Parameters to convert subcommand:\n"
197 " '--bitmaps' copies all top-level persistent bitmaps to destination\n"
198 " '-m' specifies how many coroutines work in parallel during the convert\n"
199 " process (defaults to 8)\n"
200 " '-W' allow to write to the target out of order rather than sequential\n"
201 "\n"
202 "Parameters to snapshot subcommand:\n"
203 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
204 " '-a' applies a snapshot (revert disk to saved state)\n"
205 " '-c' creates a snapshot\n"
206 " '-d' deletes a snapshot\n"
207 " '-l' lists all snapshots in the given image\n"
208 "\n"
209 "Parameters to compare subcommand:\n"
210 " '-f' first image format\n"
211 " '-F' second image format\n"
212 " '-s' run in Strict mode - fail on different image size or sector allocation\n"
213 "\n"
214 "Parameters to dd subcommand:\n"
215 " 'bs=BYTES' read and write up to BYTES bytes at a time "
216 "(default: 512)\n"
217 " 'count=N' copy only N input blocks\n"
218 " 'if=FILE' read from FILE\n"
219 " 'of=FILE' write to FILE\n"
220 " 'skip=N' skip N bs-sized blocks at the start of input\n";
221
222 printf("%s\nSupported formats:", help_msg);
223 bdrv_iterate_format(format_print, NULL, false);
224 printf("\n\n" QEMU_HELP_BOTTOM "\n");
225 exit(EXIT_SUCCESS);
226 }
227
228 static QemuOptsList qemu_object_opts = {
229 .name = "object",
230 .implied_opt_name = "qom-type",
231 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
232 .desc = {
233 { }
234 },
235 };
236
237 static bool qemu_img_object_print_help(const char *type, QemuOpts *opts)
238 {
239 if (user_creatable_print_help(type, opts)) {
240 exit(0);
241 }
242 return true;
243 }
244
245 /*
246 * Is @optarg safe for accumulate_options()?
247 * It is when multiple of them can be joined together separated by ','.
248 * To make that work, @optarg must not start with ',' (or else a
249 * separating ',' preceding it gets escaped), and it must not end with
250 * an odd number of ',' (or else a separating ',' following it gets
251 * escaped), or be empty (or else a separating ',' preceding it can
252 * escape a separating ',' following it).
253 *
254 */
255 static bool is_valid_option_list(const char *optarg)
256 {
257 size_t len = strlen(optarg);
258 size_t i;
259
260 if (!optarg[0] || optarg[0] == ',') {
261 return false;
262 }
263
264 for (i = len; i > 0 && optarg[i - 1] == ','; i--) {
265 }
266 if ((len - i) % 2) {
267 return false;
268 }
269
270 return true;
271 }
272
273 static int accumulate_options(char **options, char *optarg)
274 {
275 char *new_options;
276
277 if (!is_valid_option_list(optarg)) {
278 error_report("Invalid option list: %s", optarg);
279 return -1;
280 }
281
282 if (!*options) {
283 *options = g_strdup(optarg);
284 } else {
285 new_options = g_strdup_printf("%s,%s", *options, optarg);
286 g_free(*options);
287 *options = new_options;
288 }
289 return 0;
290 }
291
292 static QemuOptsList qemu_source_opts = {
293 .name = "source",
294 .implied_opt_name = "file",
295 .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head),
296 .desc = {
297 { }
298 },
299 };
300
301 static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
302 {
303 int ret = 0;
304 if (!quiet) {
305 va_list args;
306 va_start(args, fmt);
307 ret = vprintf(fmt, args);
308 va_end(args);
309 }
310 return ret;
311 }
312
313
314 static int print_block_option_help(const char *filename, const char *fmt)
315 {
316 BlockDriver *drv, *proto_drv;
317 QemuOptsList *create_opts = NULL;
318 Error *local_err = NULL;
319
320 /* Find driver and parse its options */
321 drv = bdrv_find_format(fmt);
322 if (!drv) {
323 error_report("Unknown file format '%s'", fmt);
324 return 1;
325 }
326
327 if (!drv->create_opts) {
328 error_report("Format driver '%s' does not support image creation", fmt);
329 return 1;
330 }
331
332 create_opts = qemu_opts_append(create_opts, drv->create_opts);
333 if (filename) {
334 proto_drv = bdrv_find_protocol(filename, true, &local_err);
335 if (!proto_drv) {
336 error_report_err(local_err);
337 qemu_opts_free(create_opts);
338 return 1;
339 }
340 if (!proto_drv->create_opts) {
341 error_report("Protocol driver '%s' does not support image creation",
342 proto_drv->format_name);
343 qemu_opts_free(create_opts);
344 return 1;
345 }
346 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
347 }
348
349 if (filename) {
350 printf("Supported options:\n");
351 } else {
352 printf("Supported %s options:\n", fmt);
353 }
354 qemu_opts_print_help(create_opts, false);
355 qemu_opts_free(create_opts);
356
357 if (!filename) {
358 printf("\n"
359 "The protocol level may support further options.\n"
360 "Specify the target filename to include those options.\n");
361 }
362
363 return 0;
364 }
365
366
367 static BlockBackend *img_open_opts(const char *optstr,
368 QemuOpts *opts, int flags, bool writethrough,
369 bool quiet, bool force_share)
370 {
371 QDict *options;
372 Error *local_err = NULL;
373 BlockBackend *blk;
374 options = qemu_opts_to_qdict(opts, NULL);
375 if (force_share) {
376 if (qdict_haskey(options, BDRV_OPT_FORCE_SHARE)
377 && strcmp(qdict_get_str(options, BDRV_OPT_FORCE_SHARE), "on")) {
378 error_report("--force-share/-U conflicts with image options");
379 qobject_unref(options);
380 return NULL;
381 }
382 qdict_put_str(options, BDRV_OPT_FORCE_SHARE, "on");
383 }
384 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
385 if (!blk) {
386 error_reportf_err(local_err, "Could not open '%s': ", optstr);
387 return NULL;
388 }
389 blk_set_enable_write_cache(blk, !writethrough);
390
391 return blk;
392 }
393
394 static BlockBackend *img_open_file(const char *filename,
395 QDict *options,
396 const char *fmt, int flags,
397 bool writethrough, bool quiet,
398 bool force_share)
399 {
400 BlockBackend *blk;
401 Error *local_err = NULL;
402
403 if (!options) {
404 options = qdict_new();
405 }
406 if (fmt) {
407 qdict_put_str(options, "driver", fmt);
408 }
409
410 if (force_share) {
411 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
412 }
413 blk = blk_new_open(filename, NULL, options, flags, &local_err);
414 if (!blk) {
415 error_reportf_err(local_err, "Could not open '%s': ", filename);
416 return NULL;
417 }
418 blk_set_enable_write_cache(blk, !writethrough);
419
420 return blk;
421 }
422
423
424 static int img_add_key_secrets(void *opaque,
425 const char *name, const char *value,
426 Error **errp)
427 {
428 QDict *options = opaque;
429
430 if (g_str_has_suffix(name, "key-secret")) {
431 qdict_put_str(options, name, value);
432 }
433
434 return 0;
435 }
436
437
438 static BlockBackend *img_open(bool image_opts,
439 const char *filename,
440 const char *fmt, int flags, bool writethrough,
441 bool quiet, bool force_share)
442 {
443 BlockBackend *blk;
444 if (image_opts) {
445 QemuOpts *opts;
446 if (fmt) {
447 error_report("--image-opts and --format are mutually exclusive");
448 return NULL;
449 }
450 opts = qemu_opts_parse_noisily(qemu_find_opts("source"),
451 filename, true);
452 if (!opts) {
453 return NULL;
454 }
455 blk = img_open_opts(filename, opts, flags, writethrough, quiet,
456 force_share);
457 } else {
458 blk = img_open_file(filename, NULL, fmt, flags, writethrough, quiet,
459 force_share);
460 }
461 return blk;
462 }
463
464
465 static int add_old_style_options(const char *fmt, QemuOpts *opts,
466 const char *base_filename,
467 const char *base_fmt)
468 {
469 if (base_filename) {
470 if (!qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename,
471 NULL)) {
472 error_report("Backing file not supported for file format '%s'",
473 fmt);
474 return -1;
475 }
476 }
477 if (base_fmt) {
478 if (!qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, NULL)) {
479 error_report("Backing file format not supported for file "
480 "format '%s'", fmt);
481 return -1;
482 }
483 }
484 return 0;
485 }
486
487 static int64_t cvtnum_full(const char *name, const char *value, int64_t min,
488 int64_t max)
489 {
490 int err;
491 uint64_t res;
492
493 err = qemu_strtosz(value, NULL, &res);
494 if (err < 0 && err != -ERANGE) {
495 error_report("Invalid %s specified. You may use "
496 "k, M, G, T, P or E suffixes for", name);
497 error_report("kilobytes, megabytes, gigabytes, terabytes, "
498 "petabytes and exabytes.");
499 return err;
500 }
501 if (err == -ERANGE || res > max || res < min) {
502 error_report("Invalid %s specified. Must be between %" PRId64
503 " and %" PRId64 ".", name, min, max);
504 return -ERANGE;
505 }
506 return res;
507 }
508
509 static int64_t cvtnum(const char *name, const char *value)
510 {
511 return cvtnum_full(name, value, 0, INT64_MAX);
512 }
513
514 static int img_create(int argc, char **argv)
515 {
516 int c;
517 uint64_t img_size = -1;
518 const char *fmt = "raw";
519 const char *base_fmt = NULL;
520 const char *filename;
521 const char *base_filename = NULL;
522 char *options = NULL;
523 Error *local_err = NULL;
524 bool quiet = false;
525 int flags = 0;
526
527 for(;;) {
528 static const struct option long_options[] = {
529 {"help", no_argument, 0, 'h'},
530 {"object", required_argument, 0, OPTION_OBJECT},
531 {0, 0, 0, 0}
532 };
533 c = getopt_long(argc, argv, ":F:b:f:ho:qu",
534 long_options, NULL);
535 if (c == -1) {
536 break;
537 }
538 switch(c) {
539 case ':':
540 missing_argument(argv[optind - 1]);
541 break;
542 case '?':
543 unrecognized_option(argv[optind - 1]);
544 break;
545 case 'h':
546 help();
547 break;
548 case 'F':
549 base_fmt = optarg;
550 break;
551 case 'b':
552 base_filename = optarg;
553 break;
554 case 'f':
555 fmt = optarg;
556 break;
557 case 'o':
558 if (accumulate_options(&options, optarg) < 0) {
559 goto fail;
560 }
561 break;
562 case 'q':
563 quiet = true;
564 break;
565 case 'u':
566 flags |= BDRV_O_NO_BACKING;
567 break;
568 case OPTION_OBJECT: {
569 QemuOpts *opts;
570 opts = qemu_opts_parse_noisily(&qemu_object_opts,
571 optarg, true);
572 if (!opts) {
573 goto fail;
574 }
575 } break;
576 }
577 }
578
579 /* Get the filename */
580 filename = (optind < argc) ? argv[optind] : NULL;
581 if (options && has_help_option(options)) {
582 g_free(options);
583 return print_block_option_help(filename, fmt);
584 }
585
586 if (optind >= argc) {
587 error_exit("Expecting image file name");
588 }
589 optind++;
590
591 if (qemu_opts_foreach(&qemu_object_opts,
592 user_creatable_add_opts_foreach,
593 qemu_img_object_print_help, &error_fatal)) {
594 goto fail;
595 }
596
597 /* Get image size, if specified */
598 if (optind < argc) {
599 int64_t sval;
600
601 sval = cvtnum("image size", argv[optind++]);
602 if (sval < 0) {
603 goto fail;
604 }
605 img_size = (uint64_t)sval;
606 }
607 if (optind != argc) {
608 error_exit("Unexpected argument: %s", argv[optind]);
609 }
610
611 bdrv_img_create(filename, fmt, base_filename, base_fmt,
612 options, img_size, flags, quiet, &local_err);
613 if (local_err) {
614 error_reportf_err(local_err, "%s: ", filename);
615 goto fail;
616 }
617
618 g_free(options);
619 return 0;
620
621 fail:
622 g_free(options);
623 return 1;
624 }
625
626 static void dump_json_image_check(ImageCheck *check, bool quiet)
627 {
628 QString *str;
629 QObject *obj;
630 Visitor *v = qobject_output_visitor_new(&obj);
631
632 visit_type_ImageCheck(v, NULL, &check, &error_abort);
633 visit_complete(v, &obj);
634 str = qobject_to_json_pretty(obj);
635 assert(str != NULL);
636 qprintf(quiet, "%s\n", qstring_get_str(str));
637 qobject_unref(obj);
638 visit_free(v);
639 qobject_unref(str);
640 }
641
642 static void dump_human_image_check(ImageCheck *check, bool quiet)
643 {
644 if (!(check->corruptions || check->leaks || check->check_errors)) {
645 qprintf(quiet, "No errors were found on the image.\n");
646 } else {
647 if (check->corruptions) {
648 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
649 "Data may be corrupted, or further writes to the image "
650 "may corrupt it.\n",
651 check->corruptions);
652 }
653
654 if (check->leaks) {
655 qprintf(quiet,
656 "\n%" PRId64 " leaked clusters were found on the image.\n"
657 "This means waste of disk space, but no harm to data.\n",
658 check->leaks);
659 }
660
661 if (check->check_errors) {
662 qprintf(quiet,
663 "\n%" PRId64
664 " internal errors have occurred during the check.\n",
665 check->check_errors);
666 }
667 }
668
669 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
670 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
671 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
672 check->allocated_clusters, check->total_clusters,
673 check->allocated_clusters * 100.0 / check->total_clusters,
674 check->fragmented_clusters * 100.0 / check->allocated_clusters,
675 check->compressed_clusters * 100.0 /
676 check->allocated_clusters);
677 }
678
679 if (check->image_end_offset) {
680 qprintf(quiet,
681 "Image end offset: %" PRId64 "\n", check->image_end_offset);
682 }
683 }
684
685 static int collect_image_check(BlockDriverState *bs,
686 ImageCheck *check,
687 const char *filename,
688 const char *fmt,
689 int fix)
690 {
691 int ret;
692 BdrvCheckResult result;
693
694 ret = bdrv_check(bs, &result, fix);
695 if (ret < 0) {
696 return ret;
697 }
698
699 check->filename = g_strdup(filename);
700 check->format = g_strdup(bdrv_get_format_name(bs));
701 check->check_errors = result.check_errors;
702 check->corruptions = result.corruptions;
703 check->has_corruptions = result.corruptions != 0;
704 check->leaks = result.leaks;
705 check->has_leaks = result.leaks != 0;
706 check->corruptions_fixed = result.corruptions_fixed;
707 check->has_corruptions_fixed = result.corruptions_fixed != 0;
708 check->leaks_fixed = result.leaks_fixed;
709 check->has_leaks_fixed = result.leaks_fixed != 0;
710 check->image_end_offset = result.image_end_offset;
711 check->has_image_end_offset = result.image_end_offset != 0;
712 check->total_clusters = result.bfi.total_clusters;
713 check->has_total_clusters = result.bfi.total_clusters != 0;
714 check->allocated_clusters = result.bfi.allocated_clusters;
715 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
716 check->fragmented_clusters = result.bfi.fragmented_clusters;
717 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
718 check->compressed_clusters = result.bfi.compressed_clusters;
719 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
720
721 return 0;
722 }
723
724 /*
725 * Checks an image for consistency. Exit codes:
726 *
727 * 0 - Check completed, image is good
728 * 1 - Check not completed because of internal errors
729 * 2 - Check completed, image is corrupted
730 * 3 - Check completed, image has leaked clusters, but is good otherwise
731 * 63 - Checks are not supported by the image format
732 */
733 static int img_check(int argc, char **argv)
734 {
735 int c, ret;
736 OutputFormat output_format = OFORMAT_HUMAN;
737 const char *filename, *fmt, *output, *cache;
738 BlockBackend *blk;
739 BlockDriverState *bs;
740 int fix = 0;
741 int flags = BDRV_O_CHECK;
742 bool writethrough;
743 ImageCheck *check;
744 bool quiet = false;
745 bool image_opts = false;
746 bool force_share = false;
747
748 fmt = NULL;
749 output = NULL;
750 cache = BDRV_DEFAULT_CACHE;
751
752 for(;;) {
753 int option_index = 0;
754 static const struct option long_options[] = {
755 {"help", no_argument, 0, 'h'},
756 {"format", required_argument, 0, 'f'},
757 {"repair", required_argument, 0, 'r'},
758 {"output", required_argument, 0, OPTION_OUTPUT},
759 {"object", required_argument, 0, OPTION_OBJECT},
760 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
761 {"force-share", no_argument, 0, 'U'},
762 {0, 0, 0, 0}
763 };
764 c = getopt_long(argc, argv, ":hf:r:T:qU",
765 long_options, &option_index);
766 if (c == -1) {
767 break;
768 }
769 switch(c) {
770 case ':':
771 missing_argument(argv[optind - 1]);
772 break;
773 case '?':
774 unrecognized_option(argv[optind - 1]);
775 break;
776 case 'h':
777 help();
778 break;
779 case 'f':
780 fmt = optarg;
781 break;
782 case 'r':
783 flags |= BDRV_O_RDWR;
784
785 if (!strcmp(optarg, "leaks")) {
786 fix = BDRV_FIX_LEAKS;
787 } else if (!strcmp(optarg, "all")) {
788 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
789 } else {
790 error_exit("Unknown option value for -r "
791 "(expecting 'leaks' or 'all'): %s", optarg);
792 }
793 break;
794 case OPTION_OUTPUT:
795 output = optarg;
796 break;
797 case 'T':
798 cache = optarg;
799 break;
800 case 'q':
801 quiet = true;
802 break;
803 case 'U':
804 force_share = true;
805 break;
806 case OPTION_OBJECT: {
807 QemuOpts *opts;
808 opts = qemu_opts_parse_noisily(&qemu_object_opts,
809 optarg, true);
810 if (!opts) {
811 return 1;
812 }
813 } break;
814 case OPTION_IMAGE_OPTS:
815 image_opts = true;
816 break;
817 }
818 }
819 if (optind != argc - 1) {
820 error_exit("Expecting one image file name");
821 }
822 filename = argv[optind++];
823
824 if (output && !strcmp(output, "json")) {
825 output_format = OFORMAT_JSON;
826 } else if (output && !strcmp(output, "human")) {
827 output_format = OFORMAT_HUMAN;
828 } else if (output) {
829 error_report("--output must be used with human or json as argument.");
830 return 1;
831 }
832
833 if (qemu_opts_foreach(&qemu_object_opts,
834 user_creatable_add_opts_foreach,
835 qemu_img_object_print_help, &error_fatal)) {
836 return 1;
837 }
838
839 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
840 if (ret < 0) {
841 error_report("Invalid source cache option: %s", cache);
842 return 1;
843 }
844
845 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
846 force_share);
847 if (!blk) {
848 return 1;
849 }
850 bs = blk_bs(blk);
851
852 check = g_new0(ImageCheck, 1);
853 ret = collect_image_check(bs, check, filename, fmt, fix);
854
855 if (ret == -ENOTSUP) {
856 error_report("This image format does not support checks");
857 ret = 63;
858 goto fail;
859 }
860
861 if (check->corruptions_fixed || check->leaks_fixed) {
862 int corruptions_fixed, leaks_fixed;
863 bool has_leaks_fixed, has_corruptions_fixed;
864
865 leaks_fixed = check->leaks_fixed;
866 has_leaks_fixed = check->has_leaks_fixed;
867 corruptions_fixed = check->corruptions_fixed;
868 has_corruptions_fixed = check->has_corruptions_fixed;
869
870 if (output_format == OFORMAT_HUMAN) {
871 qprintf(quiet,
872 "The following inconsistencies were found and repaired:\n\n"
873 " %" PRId64 " leaked clusters\n"
874 " %" PRId64 " corruptions\n\n"
875 "Double checking the fixed image now...\n",
876 check->leaks_fixed,
877 check->corruptions_fixed);
878 }
879
880 qapi_free_ImageCheck(check);
881 check = g_new0(ImageCheck, 1);
882 ret = collect_image_check(bs, check, filename, fmt, 0);
883
884 check->leaks_fixed = leaks_fixed;
885 check->has_leaks_fixed = has_leaks_fixed;
886 check->corruptions_fixed = corruptions_fixed;
887 check->has_corruptions_fixed = has_corruptions_fixed;
888 }
889
890 if (!ret) {
891 switch (output_format) {
892 case OFORMAT_HUMAN:
893 dump_human_image_check(check, quiet);
894 break;
895 case OFORMAT_JSON:
896 dump_json_image_check(check, quiet);
897 break;
898 }
899 }
900
901 if (ret || check->check_errors) {
902 if (ret) {
903 error_report("Check failed: %s", strerror(-ret));
904 } else {
905 error_report("Check failed");
906 }
907 ret = 1;
908 goto fail;
909 }
910
911 if (check->corruptions) {
912 ret = 2;
913 } else if (check->leaks) {
914 ret = 3;
915 } else {
916 ret = 0;
917 }
918
919 fail:
920 qapi_free_ImageCheck(check);
921 blk_unref(blk);
922 return ret;
923 }
924
925 typedef struct CommonBlockJobCBInfo {
926 BlockDriverState *bs;
927 Error **errp;
928 } CommonBlockJobCBInfo;
929
930 static void common_block_job_cb(void *opaque, int ret)
931 {
932 CommonBlockJobCBInfo *cbi = opaque;
933
934 if (ret < 0) {
935 error_setg_errno(cbi->errp, -ret, "Block job failed");
936 }
937 }
938
939 static void run_block_job(BlockJob *job, Error **errp)
940 {
941 AioContext *aio_context = blk_get_aio_context(job->blk);
942 int ret = 0;
943
944 aio_context_acquire(aio_context);
945 job_ref(&job->job);
946 do {
947 float progress = 0.0f;
948 aio_poll(aio_context, true);
949 if (job->job.progress.total) {
950 progress = (float)job->job.progress.current /
951 job->job.progress.total * 100.f;
952 }
953 qemu_progress_print(progress, 0);
954 } while (!job_is_ready(&job->job) && !job_is_completed(&job->job));
955
956 if (!job_is_completed(&job->job)) {
957 ret = job_complete_sync(&job->job, errp);
958 } else {
959 ret = job->job.ret;
960 }
961 job_unref(&job->job);
962 aio_context_release(aio_context);
963
964 /* publish completion progress only when success */
965 if (!ret) {
966 qemu_progress_print(100.f, 0);
967 }
968 }
969
970 static int img_commit(int argc, char **argv)
971 {
972 int c, ret, flags;
973 const char *filename, *fmt, *cache, *base;
974 BlockBackend *blk;
975 BlockDriverState *bs, *base_bs;
976 BlockJob *job;
977 bool progress = false, quiet = false, drop = false;
978 bool writethrough;
979 Error *local_err = NULL;
980 CommonBlockJobCBInfo cbi;
981 bool image_opts = false;
982 AioContext *aio_context;
983 int64_t rate_limit = 0;
984
985 fmt = NULL;
986 cache = BDRV_DEFAULT_CACHE;
987 base = NULL;
988 for(;;) {
989 static const struct option long_options[] = {
990 {"help", no_argument, 0, 'h'},
991 {"object", required_argument, 0, OPTION_OBJECT},
992 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
993 {0, 0, 0, 0}
994 };
995 c = getopt_long(argc, argv, ":f:ht:b:dpqr:",
996 long_options, NULL);
997 if (c == -1) {
998 break;
999 }
1000 switch(c) {
1001 case ':':
1002 missing_argument(argv[optind - 1]);
1003 break;
1004 case '?':
1005 unrecognized_option(argv[optind - 1]);
1006 break;
1007 case 'h':
1008 help();
1009 break;
1010 case 'f':
1011 fmt = optarg;
1012 break;
1013 case 't':
1014 cache = optarg;
1015 break;
1016 case 'b':
1017 base = optarg;
1018 /* -b implies -d */
1019 drop = true;
1020 break;
1021 case 'd':
1022 drop = true;
1023 break;
1024 case 'p':
1025 progress = true;
1026 break;
1027 case 'q':
1028 quiet = true;
1029 break;
1030 case 'r':
1031 rate_limit = cvtnum("rate limit", optarg);
1032 if (rate_limit < 0) {
1033 return 1;
1034 }
1035 break;
1036 case OPTION_OBJECT: {
1037 QemuOpts *opts;
1038 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1039 optarg, true);
1040 if (!opts) {
1041 return 1;
1042 }
1043 } break;
1044 case OPTION_IMAGE_OPTS:
1045 image_opts = true;
1046 break;
1047 }
1048 }
1049
1050 /* Progress is not shown in Quiet mode */
1051 if (quiet) {
1052 progress = false;
1053 }
1054
1055 if (optind != argc - 1) {
1056 error_exit("Expecting one image file name");
1057 }
1058 filename = argv[optind++];
1059
1060 if (qemu_opts_foreach(&qemu_object_opts,
1061 user_creatable_add_opts_foreach,
1062 qemu_img_object_print_help, &error_fatal)) {
1063 return 1;
1064 }
1065
1066 flags = BDRV_O_RDWR | BDRV_O_UNMAP;
1067 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
1068 if (ret < 0) {
1069 error_report("Invalid cache option: %s", cache);
1070 return 1;
1071 }
1072
1073 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
1074 false);
1075 if (!blk) {
1076 return 1;
1077 }
1078 bs = blk_bs(blk);
1079
1080 qemu_progress_init(progress, 1.f);
1081 qemu_progress_print(0.f, 100);
1082
1083 if (base) {
1084 base_bs = bdrv_find_backing_image(bs, base);
1085 if (!base_bs) {
1086 error_setg(&local_err,
1087 "Did not find '%s' in the backing chain of '%s'",
1088 base, filename);
1089 goto done;
1090 }
1091 } else {
1092 /* This is different from QMP, which by default uses the deepest file in
1093 * the backing chain (i.e., the very base); however, the traditional
1094 * behavior of qemu-img commit is using the immediate backing file. */
1095 base_bs = bdrv_backing_chain_next(bs);
1096 if (!base_bs) {
1097 error_setg(&local_err, "Image does not have a backing file");
1098 goto done;
1099 }
1100 }
1101
1102 cbi = (CommonBlockJobCBInfo){
1103 .errp = &local_err,
1104 .bs = bs,
1105 };
1106
1107 aio_context = bdrv_get_aio_context(bs);
1108 aio_context_acquire(aio_context);
1109 commit_active_start("commit", bs, base_bs, JOB_DEFAULT, rate_limit,
1110 BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb,
1111 &cbi, false, &local_err);
1112 aio_context_release(aio_context);
1113 if (local_err) {
1114 goto done;
1115 }
1116
1117 /* When the block job completes, the BlockBackend reference will point to
1118 * the old backing file. In order to avoid that the top image is already
1119 * deleted, so we can still empty it afterwards, increment the reference
1120 * counter here preemptively. */
1121 if (!drop) {
1122 bdrv_ref(bs);
1123 }
1124
1125 job = block_job_get("commit");
1126 assert(job);
1127 run_block_job(job, &local_err);
1128 if (local_err) {
1129 goto unref_backing;
1130 }
1131
1132 if (!drop) {
1133 BlockBackend *old_backing_blk;
1134
1135 old_backing_blk = blk_new_with_bs(bs, BLK_PERM_WRITE, BLK_PERM_ALL,
1136 &local_err);
1137 if (!old_backing_blk) {
1138 goto unref_backing;
1139 }
1140 ret = blk_make_empty(old_backing_blk, &local_err);
1141 blk_unref(old_backing_blk);
1142 if (ret == -ENOTSUP) {
1143 error_free(local_err);
1144 local_err = NULL;
1145 } else if (ret < 0) {
1146 goto unref_backing;
1147 }
1148 }
1149
1150 unref_backing:
1151 if (!drop) {
1152 bdrv_unref(bs);
1153 }
1154
1155 done:
1156 qemu_progress_end();
1157
1158 blk_unref(blk);
1159
1160 if (local_err) {
1161 error_report_err(local_err);
1162 return 1;
1163 }
1164
1165 qprintf(quiet, "Image committed.\n");
1166 return 0;
1167 }
1168
1169 /*
1170 * Returns -1 if 'buf' contains only zeroes, otherwise the byte index
1171 * of the first sector boundary within buf where the sector contains a
1172 * non-zero byte. This function is robust to a buffer that is not
1173 * sector-aligned.
1174 */
1175 static int64_t find_nonzero(const uint8_t *buf, int64_t n)
1176 {
1177 int64_t i;
1178 int64_t end = QEMU_ALIGN_DOWN(n, BDRV_SECTOR_SIZE);
1179
1180 for (i = 0; i < end; i += BDRV_SECTOR_SIZE) {
1181 if (!buffer_is_zero(buf + i, BDRV_SECTOR_SIZE)) {
1182 return i;
1183 }
1184 }
1185 if (i < n && !buffer_is_zero(buf + i, n - end)) {
1186 return i;
1187 }
1188 return -1;
1189 }
1190
1191 /*
1192 * Returns true iff the first sector pointed to by 'buf' contains at least
1193 * a non-NUL byte.
1194 *
1195 * 'pnum' is set to the number of sectors (including and immediately following
1196 * the first one) that are known to be in the same allocated/unallocated state.
1197 * The function will try to align the end offset to alignment boundaries so
1198 * that the request will at least end aligned and consecutive requests will
1199 * also start at an aligned offset.
1200 */
1201 static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum,
1202 int64_t sector_num, int alignment)
1203 {
1204 bool is_zero;
1205 int i, tail;
1206
1207 if (n <= 0) {
1208 *pnum = 0;
1209 return 0;
1210 }
1211 is_zero = buffer_is_zero(buf, BDRV_SECTOR_SIZE);
1212 for(i = 1; i < n; i++) {
1213 buf += BDRV_SECTOR_SIZE;
1214 if (is_zero != buffer_is_zero(buf, BDRV_SECTOR_SIZE)) {
1215 break;
1216 }
1217 }
1218
1219 tail = (sector_num + i) & (alignment - 1);
1220 if (tail) {
1221 if (is_zero && i <= tail) {
1222 /* treat unallocated areas which only consist
1223 * of a small tail as allocated. */
1224 is_zero = false;
1225 }
1226 if (!is_zero) {
1227 /* align up end offset of allocated areas. */
1228 i += alignment - tail;
1229 i = MIN(i, n);
1230 } else {
1231 /* align down end offset of zero areas. */
1232 i -= tail;
1233 }
1234 }
1235 *pnum = i;
1236 return !is_zero;
1237 }
1238
1239 /*
1240 * Like is_allocated_sectors, but if the buffer starts with a used sector,
1241 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1242 * breaking up write requests for only small sparse areas.
1243 */
1244 static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
1245 int min, int64_t sector_num, int alignment)
1246 {
1247 int ret;
1248 int num_checked, num_used;
1249
1250 if (n < min) {
1251 min = n;
1252 }
1253
1254 ret = is_allocated_sectors(buf, n, pnum, sector_num, alignment);
1255 if (!ret) {
1256 return ret;
1257 }
1258
1259 num_used = *pnum;
1260 buf += BDRV_SECTOR_SIZE * *pnum;
1261 n -= *pnum;
1262 sector_num += *pnum;
1263 num_checked = num_used;
1264
1265 while (n > 0) {
1266 ret = is_allocated_sectors(buf, n, pnum, sector_num, alignment);
1267
1268 buf += BDRV_SECTOR_SIZE * *pnum;
1269 n -= *pnum;
1270 sector_num += *pnum;
1271 num_checked += *pnum;
1272 if (ret) {
1273 num_used = num_checked;
1274 } else if (*pnum >= min) {
1275 break;
1276 }
1277 }
1278
1279 *pnum = num_used;
1280 return 1;
1281 }
1282
1283 /*
1284 * Compares two buffers sector by sector. Returns 0 if the first
1285 * sector of each buffer matches, non-zero otherwise.
1286 *
1287 * pnum is set to the sector-aligned size of the buffer prefix that
1288 * has the same matching status as the first sector.
1289 */
1290 static int compare_buffers(const uint8_t *buf1, const uint8_t *buf2,
1291 int64_t bytes, int64_t *pnum)
1292 {
1293 bool res;
1294 int64_t i = MIN(bytes, BDRV_SECTOR_SIZE);
1295
1296 assert(bytes > 0);
1297
1298 res = !!memcmp(buf1, buf2, i);
1299 while (i < bytes) {
1300 int64_t len = MIN(bytes - i, BDRV_SECTOR_SIZE);
1301
1302 if (!!memcmp(buf1 + i, buf2 + i, len) != res) {
1303 break;
1304 }
1305 i += len;
1306 }
1307
1308 *pnum = i;
1309 return res;
1310 }
1311
1312 #define IO_BUF_SIZE (2 * MiB)
1313
1314 /*
1315 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1316 *
1317 * Intended for use by 'qemu-img compare': Returns 0 in case sectors are
1318 * filled with 0, 1 if sectors contain non-zero data (this is a comparison
1319 * failure), and 4 on error (the exit status for read errors), after emitting
1320 * an error message.
1321 *
1322 * @param blk: BlockBackend for the image
1323 * @param offset: Starting offset to check
1324 * @param bytes: Number of bytes to check
1325 * @param filename: Name of disk file we are checking (logging purpose)
1326 * @param buffer: Allocated buffer for storing read data
1327 * @param quiet: Flag for quiet mode
1328 */
1329 static int check_empty_sectors(BlockBackend *blk, int64_t offset,
1330 int64_t bytes, const char *filename,
1331 uint8_t *buffer, bool quiet)
1332 {
1333 int ret = 0;
1334 int64_t idx;
1335
1336 ret = blk_pread(blk, offset, buffer, bytes);
1337 if (ret < 0) {
1338 error_report("Error while reading offset %" PRId64 " of %s: %s",
1339 offset, filename, strerror(-ret));
1340 return 4;
1341 }
1342 idx = find_nonzero(buffer, bytes);
1343 if (idx >= 0) {
1344 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1345 offset + idx);
1346 return 1;
1347 }
1348
1349 return 0;
1350 }
1351
1352 /*
1353 * Compares two images. Exit codes:
1354 *
1355 * 0 - Images are identical
1356 * 1 - Images differ
1357 * >1 - Error occurred
1358 */
1359 static int img_compare(int argc, char **argv)
1360 {
1361 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
1362 BlockBackend *blk1, *blk2;
1363 BlockDriverState *bs1, *bs2;
1364 int64_t total_size1, total_size2;
1365 uint8_t *buf1 = NULL, *buf2 = NULL;
1366 int64_t pnum1, pnum2;
1367 int allocated1, allocated2;
1368 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1369 bool progress = false, quiet = false, strict = false;
1370 int flags;
1371 bool writethrough;
1372 int64_t total_size;
1373 int64_t offset = 0;
1374 int64_t chunk;
1375 int c;
1376 uint64_t progress_base;
1377 bool image_opts = false;
1378 bool force_share = false;
1379
1380 cache = BDRV_DEFAULT_CACHE;
1381 for (;;) {
1382 static const struct option long_options[] = {
1383 {"help", no_argument, 0, 'h'},
1384 {"object", required_argument, 0, OPTION_OBJECT},
1385 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
1386 {"force-share", no_argument, 0, 'U'},
1387 {0, 0, 0, 0}
1388 };
1389 c = getopt_long(argc, argv, ":hf:F:T:pqsU",
1390 long_options, NULL);
1391 if (c == -1) {
1392 break;
1393 }
1394 switch (c) {
1395 case ':':
1396 missing_argument(argv[optind - 1]);
1397 break;
1398 case '?':
1399 unrecognized_option(argv[optind - 1]);
1400 break;
1401 case 'h':
1402 help();
1403 break;
1404 case 'f':
1405 fmt1 = optarg;
1406 break;
1407 case 'F':
1408 fmt2 = optarg;
1409 break;
1410 case 'T':
1411 cache = optarg;
1412 break;
1413 case 'p':
1414 progress = true;
1415 break;
1416 case 'q':
1417 quiet = true;
1418 break;
1419 case 's':
1420 strict = true;
1421 break;
1422 case 'U':
1423 force_share = true;
1424 break;
1425 case OPTION_OBJECT: {
1426 QemuOpts *opts;
1427 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1428 optarg, true);
1429 if (!opts) {
1430 ret = 2;
1431 goto out4;
1432 }
1433 } break;
1434 case OPTION_IMAGE_OPTS:
1435 image_opts = true;
1436 break;
1437 }
1438 }
1439
1440 /* Progress is not shown in Quiet mode */
1441 if (quiet) {
1442 progress = false;
1443 }
1444
1445
1446 if (optind != argc - 2) {
1447 error_exit("Expecting two image file names");
1448 }
1449 filename1 = argv[optind++];
1450 filename2 = argv[optind++];
1451
1452 if (qemu_opts_foreach(&qemu_object_opts,
1453 user_creatable_add_opts_foreach,
1454 qemu_img_object_print_help, &error_fatal)) {
1455 ret = 2;
1456 goto out4;
1457 }
1458
1459 /* Initialize before goto out */
1460 qemu_progress_init(progress, 2.0);
1461
1462 flags = 0;
1463 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
1464 if (ret < 0) {
1465 error_report("Invalid source cache option: %s", cache);
1466 ret = 2;
1467 goto out3;
1468 }
1469
1470 blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet,
1471 force_share);
1472 if (!blk1) {
1473 ret = 2;
1474 goto out3;
1475 }
1476
1477 blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet,
1478 force_share);
1479 if (!blk2) {
1480 ret = 2;
1481 goto out2;
1482 }
1483 bs1 = blk_bs(blk1);
1484 bs2 = blk_bs(blk2);
1485
1486 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1487 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
1488 total_size1 = blk_getlength(blk1);
1489 if (total_size1 < 0) {
1490 error_report("Can't get size of %s: %s",
1491 filename1, strerror(-total_size1));
1492 ret = 4;
1493 goto out;
1494 }
1495 total_size2 = blk_getlength(blk2);
1496 if (total_size2 < 0) {
1497 error_report("Can't get size of %s: %s",
1498 filename2, strerror(-total_size2));
1499 ret = 4;
1500 goto out;
1501 }
1502 total_size = MIN(total_size1, total_size2);
1503 progress_base = MAX(total_size1, total_size2);
1504
1505 qemu_progress_print(0, 100);
1506
1507 if (strict && total_size1 != total_size2) {
1508 ret = 1;
1509 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1510 goto out;
1511 }
1512
1513 while (offset < total_size) {
1514 int status1, status2;
1515
1516 status1 = bdrv_block_status_above(bs1, NULL, offset,
1517 total_size1 - offset, &pnum1, NULL,
1518 NULL);
1519 if (status1 < 0) {
1520 ret = 3;
1521 error_report("Sector allocation test failed for %s", filename1);
1522 goto out;
1523 }
1524 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
1525
1526 status2 = bdrv_block_status_above(bs2, NULL, offset,
1527 total_size2 - offset, &pnum2, NULL,
1528 NULL);
1529 if (status2 < 0) {
1530 ret = 3;
1531 error_report("Sector allocation test failed for %s", filename2);
1532 goto out;
1533 }
1534 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
1535
1536 assert(pnum1 && pnum2);
1537 chunk = MIN(pnum1, pnum2);
1538
1539 if (strict) {
1540 if (status1 != status2) {
1541 ret = 1;
1542 qprintf(quiet, "Strict mode: Offset %" PRId64
1543 " block status mismatch!\n", offset);
1544 goto out;
1545 }
1546 }
1547 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
1548 /* nothing to do */
1549 } else if (allocated1 == allocated2) {
1550 if (allocated1) {
1551 int64_t pnum;
1552
1553 chunk = MIN(chunk, IO_BUF_SIZE);
1554 ret = blk_pread(blk1, offset, buf1, chunk);
1555 if (ret < 0) {
1556 error_report("Error while reading offset %" PRId64
1557 " of %s: %s",
1558 offset, filename1, strerror(-ret));
1559 ret = 4;
1560 goto out;
1561 }
1562 ret = blk_pread(blk2, offset, buf2, chunk);
1563 if (ret < 0) {
1564 error_report("Error while reading offset %" PRId64
1565 " of %s: %s",
1566 offset, filename2, strerror(-ret));
1567 ret = 4;
1568 goto out;
1569 }
1570 ret = compare_buffers(buf1, buf2, chunk, &pnum);
1571 if (ret || pnum != chunk) {
1572 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1573 offset + (ret ? 0 : pnum));
1574 ret = 1;
1575 goto out;
1576 }
1577 }
1578 } else {
1579 chunk = MIN(chunk, IO_BUF_SIZE);
1580 if (allocated1) {
1581 ret = check_empty_sectors(blk1, offset, chunk,
1582 filename1, buf1, quiet);
1583 } else {
1584 ret = check_empty_sectors(blk2, offset, chunk,
1585 filename2, buf1, quiet);
1586 }
1587 if (ret) {
1588 goto out;
1589 }
1590 }
1591 offset += chunk;
1592 qemu_progress_print(((float) chunk / progress_base) * 100, 100);
1593 }
1594
1595 if (total_size1 != total_size2) {
1596 BlockBackend *blk_over;
1597 const char *filename_over;
1598
1599 qprintf(quiet, "Warning: Image size mismatch!\n");
1600 if (total_size1 > total_size2) {
1601 blk_over = blk1;
1602 filename_over = filename1;
1603 } else {
1604 blk_over = blk2;
1605 filename_over = filename2;
1606 }
1607
1608 while (offset < progress_base) {
1609 ret = bdrv_block_status_above(blk_bs(blk_over), NULL, offset,
1610 progress_base - offset, &chunk,
1611 NULL, NULL);
1612 if (ret < 0) {
1613 ret = 3;
1614 error_report("Sector allocation test failed for %s",
1615 filename_over);
1616 goto out;
1617
1618 }
1619 if (ret & BDRV_BLOCK_ALLOCATED && !(ret & BDRV_BLOCK_ZERO)) {
1620 chunk = MIN(chunk, IO_BUF_SIZE);
1621 ret = check_empty_sectors(blk_over, offset, chunk,
1622 filename_over, buf1, quiet);
1623 if (ret) {
1624 goto out;
1625 }
1626 }
1627 offset += chunk;
1628 qemu_progress_print(((float) chunk / progress_base) * 100, 100);
1629 }
1630 }
1631
1632 qprintf(quiet, "Images are identical.\n");
1633 ret = 0;
1634
1635 out:
1636 qemu_vfree(buf1);
1637 qemu_vfree(buf2);
1638 blk_unref(blk2);
1639 out2:
1640 blk_unref(blk1);
1641 out3:
1642 qemu_progress_end();
1643 out4:
1644 return ret;
1645 }
1646
1647 /* Convenience wrapper around qmp_block_dirty_bitmap_merge */
1648 static void do_dirty_bitmap_merge(const char *dst_node, const char *dst_name,
1649 const char *src_node, const char *src_name,
1650 Error **errp)
1651 {
1652 BlockDirtyBitmapMergeSource *merge_src;
1653 BlockDirtyBitmapMergeSourceList *list;
1654
1655 merge_src = g_new0(BlockDirtyBitmapMergeSource, 1);
1656 merge_src->type = QTYPE_QDICT;
1657 merge_src->u.external.node = g_strdup(src_node);
1658 merge_src->u.external.name = g_strdup(src_name);
1659 list = g_new0(BlockDirtyBitmapMergeSourceList, 1);
1660 list->value = merge_src;
1661 qmp_block_dirty_bitmap_merge(dst_node, dst_name, list, errp);
1662 qapi_free_BlockDirtyBitmapMergeSourceList(list);
1663 }
1664
1665 enum ImgConvertBlockStatus {
1666 BLK_DATA,
1667 BLK_ZERO,
1668 BLK_BACKING_FILE,
1669 };
1670
1671 #define MAX_COROUTINES 16
1672
1673 typedef struct ImgConvertState {
1674 BlockBackend **src;
1675 int64_t *src_sectors;
1676 int *src_alignment;
1677 int src_num;
1678 int64_t total_sectors;
1679 int64_t allocated_sectors;
1680 int64_t allocated_done;
1681 int64_t sector_num;
1682 int64_t wr_offs;
1683 enum ImgConvertBlockStatus status;
1684 int64_t sector_next_status;
1685 BlockBackend *target;
1686 bool has_zero_init;
1687 bool compressed;
1688 bool target_is_new;
1689 bool target_has_backing;
1690 int64_t target_backing_sectors; /* negative if unknown */
1691 bool wr_in_order;
1692 bool copy_range;
1693 bool salvage;
1694 bool quiet;
1695 int min_sparse;
1696 int alignment;
1697 size_t cluster_sectors;
1698 size_t buf_sectors;
1699 long num_coroutines;
1700 int running_coroutines;
1701 Coroutine *co[MAX_COROUTINES];
1702 int64_t wait_sector_num[MAX_COROUTINES];
1703 CoMutex lock;
1704 int ret;
1705 } ImgConvertState;
1706
1707 static void convert_select_part(ImgConvertState *s, int64_t sector_num,
1708 int *src_cur, int64_t *src_cur_offset)
1709 {
1710 *src_cur = 0;
1711 *src_cur_offset = 0;
1712 while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) {
1713 *src_cur_offset += s->src_sectors[*src_cur];
1714 (*src_cur)++;
1715 assert(*src_cur < s->src_num);
1716 }
1717 }
1718
1719 static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1720 {
1721 int64_t src_cur_offset;
1722 int ret, n, src_cur;
1723 bool post_backing_zero = false;
1724
1725 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1726
1727 assert(s->total_sectors > sector_num);
1728 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1729
1730 if (s->target_backing_sectors >= 0) {
1731 if (sector_num >= s->target_backing_sectors) {
1732 post_backing_zero = true;
1733 } else if (sector_num + n > s->target_backing_sectors) {
1734 /* Split requests around target_backing_sectors (because
1735 * starting from there, zeros are handled differently) */
1736 n = s->target_backing_sectors - sector_num;
1737 }
1738 }
1739
1740 if (s->sector_next_status <= sector_num) {
1741 uint64_t offset = (sector_num - src_cur_offset) * BDRV_SECTOR_SIZE;
1742 int64_t count;
1743 int tail;
1744 BlockDriverState *src_bs = blk_bs(s->src[src_cur]);
1745 BlockDriverState *base;
1746
1747 if (s->target_has_backing) {
1748 base = bdrv_cow_bs(bdrv_skip_filters(src_bs));
1749 } else {
1750 base = NULL;
1751 }
1752
1753 do {
1754 count = n * BDRV_SECTOR_SIZE;
1755
1756 ret = bdrv_block_status_above(src_bs, base, offset, count, &count,
1757 NULL, NULL);
1758
1759 if (ret < 0) {
1760 if (s->salvage) {
1761 if (n == 1) {
1762 if (!s->quiet) {
1763 warn_report("error while reading block status at "
1764 "offset %" PRIu64 ": %s", offset,
1765 strerror(-ret));
1766 }
1767 /* Just try to read the data, then */
1768 ret = BDRV_BLOCK_DATA;
1769 count = BDRV_SECTOR_SIZE;
1770 } else {
1771 /* Retry on a shorter range */
1772 n = DIV_ROUND_UP(n, 4);
1773 }
1774 } else {
1775 error_report("error while reading block status at offset "
1776 "%" PRIu64 ": %s", offset, strerror(-ret));
1777 return ret;
1778 }
1779 }
1780 } while (ret < 0);
1781
1782 n = DIV_ROUND_UP(count, BDRV_SECTOR_SIZE);
1783
1784 /*
1785 * Avoid that s->sector_next_status becomes unaligned to the source
1786 * request alignment and/or cluster size to avoid unnecessary read
1787 * cycles.
1788 */
1789 tail = (sector_num - src_cur_offset + n) % s->src_alignment[src_cur];
1790 if (n > tail) {
1791 n -= tail;
1792 }
1793
1794 if (ret & BDRV_BLOCK_ZERO) {
1795 s->status = post_backing_zero ? BLK_BACKING_FILE : BLK_ZERO;
1796 } else if (ret & BDRV_BLOCK_DATA) {
1797 s->status = BLK_DATA;
1798 } else {
1799 s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA;
1800 }
1801
1802 s->sector_next_status = sector_num + n;
1803 }
1804
1805 n = MIN(n, s->sector_next_status - sector_num);
1806 if (s->status == BLK_DATA) {
1807 n = MIN(n, s->buf_sectors);
1808 }
1809
1810 /* We need to write complete clusters for compressed images, so if an
1811 * unallocated area is shorter than that, we must consider the whole
1812 * cluster allocated. */
1813 if (s->compressed) {
1814 if (n < s->cluster_sectors) {
1815 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1816 s->status = BLK_DATA;
1817 } else {
1818 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1819 }
1820 }
1821
1822 return n;
1823 }
1824
1825 static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num,
1826 int nb_sectors, uint8_t *buf)
1827 {
1828 uint64_t single_read_until = 0;
1829 int n, ret;
1830
1831 assert(nb_sectors <= s->buf_sectors);
1832 while (nb_sectors > 0) {
1833 BlockBackend *blk;
1834 int src_cur;
1835 int64_t bs_sectors, src_cur_offset;
1836 uint64_t offset;
1837
1838 /* In the case of compression with multiple source files, we can get a
1839 * nb_sectors that spreads into the next part. So we must be able to
1840 * read across multiple BDSes for one convert_read() call. */
1841 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1842 blk = s->src[src_cur];
1843 bs_sectors = s->src_sectors[src_cur];
1844
1845 offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS;
1846
1847 n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1848 if (single_read_until > offset) {
1849 n = 1;
1850 }
1851
1852 ret = blk_co_pread(blk, offset, n << BDRV_SECTOR_BITS, buf, 0);
1853 if (ret < 0) {
1854 if (s->salvage) {
1855 if (n > 1) {
1856 single_read_until = offset + (n << BDRV_SECTOR_BITS);
1857 continue;
1858 } else {
1859 if (!s->quiet) {
1860 warn_report("error while reading offset %" PRIu64
1861 ": %s", offset, strerror(-ret));
1862 }
1863 memset(buf, 0, BDRV_SECTOR_SIZE);
1864 }
1865 } else {
1866 return ret;
1867 }
1868 }
1869
1870 sector_num += n;
1871 nb_sectors -= n;
1872 buf += n * BDRV_SECTOR_SIZE;
1873 }
1874
1875 return 0;
1876 }
1877
1878
1879 static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
1880 int nb_sectors, uint8_t *buf,
1881 enum ImgConvertBlockStatus status)
1882 {
1883 int ret;
1884
1885 while (nb_sectors > 0) {
1886 int n = nb_sectors;
1887 BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0;
1888
1889 switch (status) {
1890 case BLK_BACKING_FILE:
1891 /* If we have a backing file, leave clusters unallocated that are
1892 * unallocated in the source image, so that the backing file is
1893 * visible at the respective offset. */
1894 assert(s->target_has_backing);
1895 break;
1896
1897 case BLK_DATA:
1898 /* If we're told to keep the target fully allocated (-S 0) or there
1899 * is real non-zero data, we must write it. Otherwise we can treat
1900 * it as zero sectors.
1901 * Compressed clusters need to be written as a whole, so in that
1902 * case we can only save the write if the buffer is completely
1903 * zeroed. */
1904 if (!s->min_sparse ||
1905 (!s->compressed &&
1906 is_allocated_sectors_min(buf, n, &n, s->min_sparse,
1907 sector_num, s->alignment)) ||
1908 (s->compressed &&
1909 !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)))
1910 {
1911 ret = blk_co_pwrite(s->target, sector_num << BDRV_SECTOR_BITS,
1912 n << BDRV_SECTOR_BITS, buf, flags);
1913 if (ret < 0) {
1914 return ret;
1915 }
1916 break;
1917 }
1918 /* fall-through */
1919
1920 case BLK_ZERO:
1921 if (s->has_zero_init) {
1922 assert(!s->target_has_backing);
1923 break;
1924 }
1925 ret = blk_co_pwrite_zeroes(s->target,
1926 sector_num << BDRV_SECTOR_BITS,
1927 n << BDRV_SECTOR_BITS,
1928 BDRV_REQ_MAY_UNMAP);
1929 if (ret < 0) {
1930 return ret;
1931 }
1932 break;
1933 }
1934
1935 sector_num += n;
1936 nb_sectors -= n;
1937 buf += n * BDRV_SECTOR_SIZE;
1938 }
1939
1940 return 0;
1941 }
1942
1943 static int coroutine_fn convert_co_copy_range(ImgConvertState *s, int64_t sector_num,
1944 int nb_sectors)
1945 {
1946 int n, ret;
1947
1948 while (nb_sectors > 0) {
1949 BlockBackend *blk;
1950 int src_cur;
1951 int64_t bs_sectors, src_cur_offset;
1952 int64_t offset;
1953
1954 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1955 offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS;
1956 blk = s->src[src_cur];
1957 bs_sectors = s->src_sectors[src_cur];
1958
1959 n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1960
1961 ret = blk_co_copy_range(blk, offset, s->target,
1962 sector_num << BDRV_SECTOR_BITS,
1963 n << BDRV_SECTOR_BITS, 0, 0);
1964 if (ret < 0) {
1965 return ret;
1966 }
1967
1968 sector_num += n;
1969 nb_sectors -= n;
1970 }
1971 return 0;
1972 }
1973
1974 static void coroutine_fn convert_co_do_copy(void *opaque)
1975 {
1976 ImgConvertState *s = opaque;
1977 uint8_t *buf = NULL;
1978 int ret, i;
1979 int index = -1;
1980
1981 for (i = 0; i < s->num_coroutines; i++) {
1982 if (s->co[i] == qemu_coroutine_self()) {
1983 index = i;
1984 break;
1985 }
1986 }
1987 assert(index >= 0);
1988
1989 s->running_coroutines++;
1990 buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1991
1992 while (1) {
1993 int n;
1994 int64_t sector_num;
1995 enum ImgConvertBlockStatus status;
1996 bool copy_range;
1997
1998 qemu_co_mutex_lock(&s->lock);
1999 if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) {
2000 qemu_co_mutex_unlock(&s->lock);
2001 break;
2002 }
2003 n = convert_iteration_sectors(s, s->sector_num);
2004 if (n < 0) {
2005 qemu_co_mutex_unlock(&s->lock);
2006 s->ret = n;
2007 break;
2008 }
2009 /* save current sector and allocation status to local variables */
2010 sector_num = s->sector_num;
2011 status = s->status;
2012 if (!s->min_sparse && s->status == BLK_ZERO) {
2013 n = MIN(n, s->buf_sectors);
2014 }
2015 /* increment global sector counter so that other coroutines can
2016 * already continue reading beyond this request */
2017 s->sector_num += n;
2018 qemu_co_mutex_unlock(&s->lock);
2019
2020 if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) {
2021 s->allocated_done += n;
2022 qemu_progress_print(100.0 * s->allocated_done /
2023 s->allocated_sectors, 0);
2024 }
2025
2026 retry:
2027 copy_range = s->copy_range && s->status == BLK_DATA;
2028 if (status == BLK_DATA && !copy_range) {
2029 ret = convert_co_read(s, sector_num, n, buf);
2030 if (ret < 0) {
2031 error_report("error while reading at byte %lld: %s",
2032 sector_num * BDRV_SECTOR_SIZE, strerror(-ret));
2033 s->ret = ret;
2034 }
2035 } else if (!s->min_sparse && status == BLK_ZERO) {
2036 status = BLK_DATA;
2037 memset(buf, 0x00, n * BDRV_SECTOR_SIZE);
2038 }
2039
2040 if (s->wr_in_order) {
2041 /* keep writes in order */
2042 while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) {
2043 s->wait_sector_num[index] = sector_num;
2044 qemu_coroutine_yield();
2045 }
2046 s->wait_sector_num[index] = -1;
2047 }
2048
2049 if (s->ret == -EINPROGRESS) {
2050 if (copy_range) {
2051 ret = convert_co_copy_range(s, sector_num, n);
2052 if (ret) {
2053 s->copy_range = false;
2054 goto retry;
2055 }
2056 } else {
2057 ret = convert_co_write(s, sector_num, n, buf, status);
2058 }
2059 if (ret < 0) {
2060 error_report("error while writing at byte %lld: %s",
2061 sector_num * BDRV_SECTOR_SIZE, strerror(-ret));
2062 s->ret = ret;
2063 }
2064 }
2065
2066 if (s->wr_in_order) {
2067 /* reenter the coroutine that might have waited
2068 * for this write to complete */
2069 s->wr_offs = sector_num + n;
2070 for (i = 0; i < s->num_coroutines; i++) {
2071 if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) {
2072 /*
2073 * A -> B -> A cannot occur because A has
2074 * s->wait_sector_num[i] == -1 during A -> B. Therefore
2075 * B will never enter A during this time window.
2076 */
2077 qemu_coroutine_enter(s->co[i]);
2078 break;
2079 }
2080 }
2081 }
2082 }
2083
2084 qemu_vfree(buf);
2085 s->co[index] = NULL;
2086 s->running_coroutines--;
2087 if (!s->running_coroutines && s->ret == -EINPROGRESS) {
2088 /* the convert job finished successfully */
2089 s->ret = 0;
2090 }
2091 }
2092
2093 static int convert_do_copy(ImgConvertState *s)
2094 {
2095 int ret, i, n;
2096 int64_t sector_num = 0;
2097
2098 /* Check whether we have zero initialisation or can get it efficiently */
2099 if (!s->has_zero_init && s->target_is_new && s->min_sparse &&
2100 !s->target_has_backing) {
2101 s->has_zero_init = bdrv_has_zero_init(blk_bs(s->target));
2102 }
2103
2104 /* Allocate buffer for copied data. For compressed images, only one cluster
2105 * can be copied at a time. */
2106 if (s->compressed) {
2107 if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
2108 error_report("invalid cluster size");
2109 return -EINVAL;
2110 }
2111 s->buf_sectors = s->cluster_sectors;
2112 }
2113
2114 while (sector_num < s->total_sectors) {
2115 n = convert_iteration_sectors(s, sector_num);
2116 if (n < 0) {
2117 return n;
2118 }
2119 if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
2120 {
2121 s->allocated_sectors += n;
2122 }
2123 sector_num += n;
2124 }
2125
2126 /* Do the copy */
2127 s->sector_next_status = 0;
2128 s->ret = -EINPROGRESS;
2129
2130 qemu_co_mutex_init(&s->lock);
2131 for (i = 0; i < s->num_coroutines; i++) {
2132 s->co[i] = qemu_coroutine_create(convert_co_do_copy, s);
2133 s->wait_sector_num[i] = -1;
2134 qemu_coroutine_enter(s->co[i]);
2135 }
2136
2137 while (s->running_coroutines) {
2138 main_loop_wait(false);
2139 }
2140
2141 if (s->compressed && !s->ret) {
2142 /* signal EOF to align */
2143 ret = blk_pwrite_compressed(s->target, 0, NULL, 0);
2144 if (ret < 0) {
2145 return ret;
2146 }
2147 }
2148
2149 return s->ret;
2150 }
2151
2152 static int convert_copy_bitmaps(BlockDriverState *src, BlockDriverState *dst)
2153 {
2154 BdrvDirtyBitmap *bm;
2155 Error *err = NULL;
2156
2157 FOR_EACH_DIRTY_BITMAP(src, bm) {
2158 const char *name;
2159
2160 if (!bdrv_dirty_bitmap_get_persistence(bm)) {
2161 continue;
2162 }
2163 name = bdrv_dirty_bitmap_name(bm);
2164 qmp_block_dirty_bitmap_add(dst->node_name, name,
2165 true, bdrv_dirty_bitmap_granularity(bm),
2166 true, true,
2167 true, !bdrv_dirty_bitmap_enabled(bm),
2168 &err);
2169 if (err) {
2170 error_reportf_err(err, "Failed to create bitmap %s: ", name);
2171 return -1;
2172 }
2173
2174 do_dirty_bitmap_merge(dst->node_name, name, src->node_name, name,
2175 &err);
2176 if (err) {
2177 error_reportf_err(err, "Failed to populate bitmap %s: ", name);
2178 return -1;
2179 }
2180 }
2181
2182 return 0;
2183 }
2184
2185 #define MAX_BUF_SECTORS 32768
2186
2187 static int img_convert(int argc, char **argv)
2188 {
2189 int c, bs_i, flags, src_flags = 0;
2190 const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe",
2191 *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL,
2192 *out_filename, *out_baseimg_param, *snapshot_name = NULL;
2193 BlockDriver *drv = NULL, *proto_drv = NULL;
2194 BlockDriverInfo bdi;
2195 BlockDriverState *out_bs;
2196 QemuOpts *opts = NULL, *sn_opts = NULL;
2197 QemuOptsList *create_opts = NULL;
2198 QDict *open_opts = NULL;
2199 char *options = NULL;
2200 Error *local_err = NULL;
2201 bool writethrough, src_writethrough, image_opts = false,
2202 skip_create = false, progress = false, tgt_image_opts = false;
2203 int64_t ret = -EINVAL;
2204 bool force_share = false;
2205 bool explict_min_sparse = false;
2206 bool bitmaps = false;
2207
2208 ImgConvertState s = (ImgConvertState) {
2209 /* Need at least 4k of zeros for sparse detection */
2210 .min_sparse = 8,
2211 .copy_range = false,
2212 .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
2213 .wr_in_order = true,
2214 .num_coroutines = 8,
2215 };
2216
2217 for(;;) {
2218 static const struct option long_options[] = {
2219 {"help", no_argument, 0, 'h'},
2220 {"object", required_argument, 0, OPTION_OBJECT},
2221 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
2222 {"force-share", no_argument, 0, 'U'},
2223 {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
2224 {"salvage", no_argument, 0, OPTION_SALVAGE},
2225 {"target-is-zero", no_argument, 0, OPTION_TARGET_IS_ZERO},
2226 {"bitmaps", no_argument, 0, OPTION_BITMAPS},
2227 {0, 0, 0, 0}
2228 };
2229 c = getopt_long(argc, argv, ":hf:O:B:Cco:l:S:pt:T:qnm:WU",
2230 long_options, NULL);
2231 if (c == -1) {
2232 break;
2233 }
2234 switch(c) {
2235 case ':':
2236 missing_argument(argv[optind - 1]);
2237 break;
2238 case '?':
2239 unrecognized_option(argv[optind - 1]);
2240 break;
2241 case 'h':
2242 help();
2243 break;
2244 case 'f':
2245 fmt = optarg;
2246 break;
2247 case 'O':
2248 out_fmt = optarg;
2249 break;
2250 case 'B':
2251 out_baseimg = optarg;
2252 break;
2253 case 'C':
2254 s.copy_range = true;
2255 break;
2256 case 'c':
2257 s.compressed = true;
2258 break;
2259 case 'o':
2260 if (accumulate_options(&options, optarg) < 0) {
2261 goto fail_getopt;
2262 }
2263 break;
2264 case 'l':
2265 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
2266 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
2267 optarg, false);
2268 if (!sn_opts) {
2269 error_report("Failed in parsing snapshot param '%s'",
2270 optarg);
2271 goto fail_getopt;
2272 }
2273 } else {
2274 snapshot_name = optarg;
2275 }
2276 break;
2277 case 'S':
2278 {
2279 int64_t sval;
2280
2281 sval = cvtnum("buffer size for sparse output", optarg);
2282 if (sval < 0) {
2283 goto fail_getopt;
2284 } else if (!QEMU_IS_ALIGNED(sval, BDRV_SECTOR_SIZE) ||
2285 sval / BDRV_SECTOR_SIZE > MAX_BUF_SECTORS) {
2286 error_report("Invalid buffer size for sparse output specified. "
2287 "Valid sizes are multiples of %llu up to %llu. Select "
2288 "0 to disable sparse detection (fully allocates output).",
2289 BDRV_SECTOR_SIZE, MAX_BUF_SECTORS * BDRV_SECTOR_SIZE);
2290 goto fail_getopt;
2291 }
2292
2293 s.min_sparse = sval / BDRV_SECTOR_SIZE;
2294 explict_min_sparse = true;
2295 break;
2296 }
2297 case 'p':
2298 progress = true;
2299 break;
2300 case 't':
2301 cache = optarg;
2302 break;
2303 case 'T':
2304 src_cache = optarg;
2305 break;
2306 case 'q':
2307 s.quiet = true;
2308 break;
2309 case 'n':
2310 skip_create = true;
2311 break;
2312 case 'm':
2313 if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) ||
2314 s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) {
2315 error_report("Invalid number of coroutines. Allowed number of"
2316 " coroutines is between 1 and %d", MAX_COROUTINES);
2317 goto fail_getopt;
2318 }
2319 break;
2320 case 'W':
2321 s.wr_in_order = false;
2322 break;
2323 case 'U':
2324 force_share = true;
2325 break;
2326 case OPTION_OBJECT: {
2327 QemuOpts *object_opts;
2328 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
2329 optarg, true);
2330 if (!object_opts) {
2331 goto fail_getopt;
2332 }
2333 break;
2334 }
2335 case OPTION_IMAGE_OPTS:
2336 image_opts = true;
2337 break;
2338 case OPTION_SALVAGE:
2339 s.salvage = true;
2340 break;
2341 case OPTION_TARGET_IMAGE_OPTS:
2342 tgt_image_opts = true;
2343 break;
2344 case OPTION_TARGET_IS_ZERO:
2345 /*
2346 * The user asserting that the target is blank has the
2347 * same effect as the target driver supporting zero
2348 * initialisation.
2349 */
2350 s.has_zero_init = true;
2351 break;
2352 case OPTION_BITMAPS:
2353 bitmaps = true;
2354 break;
2355 }
2356 }
2357
2358 if (!out_fmt && !tgt_image_opts) {
2359 out_fmt = "raw";
2360 }
2361
2362 if (qemu_opts_foreach(&qemu_object_opts,
2363 user_creatable_add_opts_foreach,
2364 qemu_img_object_print_help, &error_fatal)) {
2365 goto fail_getopt;
2366 }
2367
2368 if (s.compressed && s.copy_range) {
2369 error_report("Cannot enable copy offloading when -c is used");
2370 goto fail_getopt;
2371 }
2372
2373 if (explict_min_sparse && s.copy_range) {
2374 error_report("Cannot enable copy offloading when -S is used");
2375 goto fail_getopt;
2376 }
2377
2378 if (s.copy_range && s.salvage) {
2379 error_report("Cannot use copy offloading in salvaging mode");
2380 goto fail_getopt;
2381 }
2382
2383 if (tgt_image_opts && !skip_create) {
2384 error_report("--target-image-opts requires use of -n flag");
2385 goto fail_getopt;
2386 }
2387
2388 if (skip_create && options) {
2389 error_report("-o has no effect when skipping image creation");
2390 goto fail_getopt;
2391 }
2392
2393 if (s.has_zero_init && !skip_create) {
2394 error_report("--target-is-zero requires use of -n flag");
2395 goto fail_getopt;
2396 }
2397
2398 s.src_num = argc - optind - 1;
2399 out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;
2400
2401 if (options && has_help_option(options)) {
2402 if (out_fmt) {
2403 ret = print_block_option_help(out_filename, out_fmt);
2404 goto fail_getopt;
2405 } else {
2406 error_report("Option help requires a format be specified");
2407 goto fail_getopt;
2408 }
2409 }
2410
2411 if (s.src_num < 1) {
2412 error_report("Must specify image file name");
2413 goto fail_getopt;
2414 }
2415
2416 /* ret is still -EINVAL until here */
2417 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
2418 if (ret < 0) {
2419 error_report("Invalid source cache option: %s", src_cache);
2420 goto fail_getopt;
2421 }
2422
2423 /* Initialize before goto out */
2424 if (s.quiet) {
2425 progress = false;
2426 }
2427 qemu_progress_init(progress, 1.0);
2428 qemu_progress_print(0, 100);
2429
2430 s.src = g_new0(BlockBackend *, s.src_num);
2431 s.src_sectors = g_new(int64_t, s.src_num);
2432 s.src_alignment = g_new(int, s.src_num);
2433
2434 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2435 BlockDriverState *src_bs;
2436 s.src[bs_i] = img_open(image_opts, argv[optind + bs_i],
2437 fmt, src_flags, src_writethrough, s.quiet,
2438 force_share);
2439 if (!s.src[bs_i]) {
2440 ret = -1;
2441 goto out;
2442 }
2443 s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]);
2444 if (s.src_sectors[bs_i] < 0) {
2445 error_report("Could not get size of %s: %s",
2446 argv[optind + bs_i], strerror(-s.src_sectors[bs_i]));
2447 ret = -1;
2448 goto out;
2449 }
2450 src_bs = blk_bs(s.src[bs_i]);
2451 s.src_alignment[bs_i] = DIV_ROUND_UP(src_bs->bl.request_alignment,
2452 BDRV_SECTOR_SIZE);
2453 if (!bdrv_get_info(src_bs, &bdi)) {
2454 s.src_alignment[bs_i] = MAX(s.src_alignment[bs_i],
2455 bdi.cluster_size / BDRV_SECTOR_SIZE);
2456 }
2457 s.total_sectors += s.src_sectors[bs_i];
2458 }
2459
2460 if (sn_opts) {
2461 bdrv_snapshot_load_tmp(blk_bs(s.src[0]),
2462 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
2463 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
2464 &local_err);
2465 } else if (snapshot_name != NULL) {
2466 if (s.src_num > 1) {
2467 error_report("No support for concatenating multiple snapshot");
2468 ret = -1;
2469 goto out;
2470 }
2471
2472 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name,
2473 &local_err);
2474 }
2475 if (local_err) {
2476 error_reportf_err(local_err, "Failed to load snapshot: ");
2477 ret = -1;
2478 goto out;
2479 }
2480
2481 if (!skip_create) {
2482 /* Find driver and parse its options */
2483 drv = bdrv_find_format(out_fmt);
2484 if (!drv) {
2485 error_report("Unknown file format '%s'", out_fmt);
2486 ret = -1;
2487 goto out;
2488 }
2489
2490 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
2491 if (!proto_drv) {
2492 error_report_err(local_err);
2493 ret = -1;
2494 goto out;
2495 }
2496
2497 if (!drv->create_opts) {
2498 error_report("Format driver '%s' does not support image creation",
2499 drv->format_name);
2500 ret = -1;
2501 goto out;
2502 }
2503
2504 if (!proto_drv->create_opts) {
2505 error_report("Protocol driver '%s' does not support image creation",
2506 proto_drv->format_name);
2507 ret = -1;
2508 goto out;
2509 }
2510
2511 create_opts = qemu_opts_append(create_opts, drv->create_opts);
2512 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
2513
2514 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
2515 if (options) {
2516 if (!qemu_opts_do_parse(opts, options, NULL, &local_err)) {
2517 error_report_err(local_err);
2518 ret = -1;
2519 goto out;
2520 }
2521 }
2522
2523 qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
2524 s.total_sectors * BDRV_SECTOR_SIZE, &error_abort);
2525 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
2526 if (ret < 0) {
2527 goto out;
2528 }
2529 }
2530
2531 /* Get backing file name if -o backing_file was used */
2532 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
2533 if (out_baseimg_param) {
2534 out_baseimg = out_baseimg_param;
2535 }
2536 s.target_has_backing = (bool) out_baseimg;
2537
2538 if (s.has_zero_init && s.target_has_backing) {
2539 error_report("Cannot use --target-is-zero when the destination "
2540 "image has a backing file");
2541 goto out;
2542 }
2543
2544 if (s.src_num > 1 && out_baseimg) {
2545 error_report("Having a backing file for the target makes no sense when "
2546 "concatenating multiple input images");
2547 ret = -1;
2548 goto out;
2549 }
2550
2551 if (out_baseimg_param) {
2552 if (!qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT)) {
2553 warn_report("Deprecated use of backing file without explicit "
2554 "backing format");
2555 }
2556 }
2557
2558 /* Check if compression is supported */
2559 if (s.compressed) {
2560 bool encryption =
2561 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
2562 const char *encryptfmt =
2563 qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT);
2564 const char *preallocation =
2565 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
2566
2567 if (drv && !block_driver_can_compress(drv)) {
2568 error_report("Compression not supported for this file format");
2569 ret = -1;
2570 goto out;
2571 }
2572
2573 if (encryption || encryptfmt) {
2574 error_report("Compression and encryption not supported at "
2575 "the same time");
2576 ret = -1;
2577 goto out;
2578 }
2579
2580 if (preallocation
2581 && strcmp(preallocation, "off"))
2582 {
2583 error_report("Compression and preallocation not supported at "
2584 "the same time");
2585 ret = -1;
2586 goto out;
2587 }
2588 }
2589
2590 /* Determine if bitmaps need copying */
2591 if (bitmaps) {
2592 if (s.src_num > 1) {
2593 error_report("Copying bitmaps only possible with single source");
2594 ret = -1;
2595 goto out;
2596 }
2597 if (!bdrv_supports_persistent_dirty_bitmap(blk_bs(s.src[0]))) {
2598 error_report("Source lacks bitmap support");
2599 ret = -1;
2600 goto out;
2601 }
2602 }
2603
2604 /*
2605 * The later open call will need any decryption secrets, and
2606 * bdrv_create() will purge "opts", so extract them now before
2607 * they are lost.
2608 */
2609 if (!skip_create) {
2610 open_opts = qdict_new();
2611 qemu_opt_foreach(opts, img_add_key_secrets, open_opts, &error_abort);
2612
2613 /* Create the new image */
2614 ret = bdrv_create(drv, out_filename, opts, &local_err);
2615 if (ret < 0) {
2616 error_reportf_err(local_err, "%s: error while converting %s: ",
2617 out_filename, out_fmt);
2618 goto out;
2619 }
2620 }
2621
2622 s.target_is_new = !skip_create;
2623
2624 flags = s.min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
2625 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
2626 if (ret < 0) {
2627 error_report("Invalid cache option: %s", cache);
2628 goto out;
2629 }
2630
2631 if (skip_create) {
2632 s.target = img_open(tgt_image_opts, out_filename, out_fmt,
2633 flags, writethrough, s.quiet, false);
2634 } else {
2635 /* TODO ultimately we should allow --target-image-opts
2636 * to be used even when -n is not given.
2637 * That has to wait for bdrv_create to be improved
2638 * to allow filenames in option syntax
2639 */
2640 s.target = img_open_file(out_filename, open_opts, out_fmt,
2641 flags, writethrough, s.quiet, false);
2642 open_opts = NULL; /* blk_new_open will have freed it */
2643 }
2644 if (!s.target) {
2645 ret = -1;
2646 goto out;
2647 }
2648 out_bs = blk_bs(s.target);
2649
2650 if (bitmaps && !bdrv_supports_persistent_dirty_bitmap(out_bs)) {
2651 error_report("Format driver '%s' does not support bitmaps",
2652 out_bs->drv->format_name);
2653 ret = -1;
2654 goto out;
2655 }
2656
2657 if (s.compressed && !block_driver_can_compress(out_bs->drv)) {
2658 error_report("Compression not supported for this file format");
2659 ret = -1;
2660 goto out;
2661 }
2662
2663 /* increase bufsectors from the default 4096 (2M) if opt_transfer
2664 * or discard_alignment of the out_bs is greater. Limit to
2665 * MAX_BUF_SECTORS as maximum which is currently 32768 (16MB). */
2666 s.buf_sectors = MIN(MAX_BUF_SECTORS,
2667 MAX(s.buf_sectors,
2668 MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS,
2669 out_bs->bl.pdiscard_alignment >>
2670 BDRV_SECTOR_BITS)));
2671
2672 /* try to align the write requests to the destination to avoid unnecessary
2673 * RMW cycles. */
2674 s.alignment = MAX(pow2floor(s.min_sparse),
2675 DIV_ROUND_UP(out_bs->bl.request_alignment,
2676 BDRV_SECTOR_SIZE));
2677 assert(is_power_of_2(s.alignment));
2678
2679 if (skip_create) {
2680 int64_t output_sectors = blk_nb_sectors(s.target);
2681 if (output_sectors < 0) {
2682 error_report("unable to get output image length: %s",
2683 strerror(-output_sectors));
2684 ret = -1;
2685 goto out;
2686 } else if (output_sectors < s.total_sectors) {
2687 error_report("output file is smaller than input file");
2688 ret = -1;
2689 goto out;
2690 }
2691 }
2692
2693 if (s.target_has_backing && s.target_is_new) {
2694 /* Errors are treated as "backing length unknown" (which means
2695 * s.target_backing_sectors has to be negative, which it will
2696 * be automatically). The backing file length is used only
2697 * for optimizations, so such a case is not fatal. */
2698 s.target_backing_sectors =
2699 bdrv_nb_sectors(bdrv_backing_chain_next(out_bs));
2700 } else {
2701 s.target_backing_sectors = -1;
2702 }
2703
2704 ret = bdrv_get_info(out_bs, &bdi);
2705 if (ret < 0) {
2706 if (s.compressed) {
2707 error_report("could not get block driver info");
2708 goto out;
2709 }
2710 } else {
2711 s.compressed = s.compressed || bdi.needs_compressed_writes;
2712 s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
2713 }
2714
2715 ret = convert_do_copy(&s);
2716
2717 /* Now copy the bitmaps */
2718 if (bitmaps && ret == 0) {
2719 ret = convert_copy_bitmaps(blk_bs(s.src[0]), out_bs);
2720 }
2721
2722 out:
2723 if (!ret) {
2724 qemu_progress_print(100, 0);
2725 }
2726 qemu_progress_end();
2727 qemu_opts_del(opts);
2728 qemu_opts_free(create_opts);
2729 qemu_opts_del(sn_opts);
2730 qobject_unref(open_opts);
2731 blk_unref(s.target);
2732 if (s.src) {
2733 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2734 blk_unref(s.src[bs_i]);
2735 }
2736 g_free(s.src);
2737 }
2738 g_free(s.src_sectors);
2739 g_free(s.src_alignment);
2740 fail_getopt:
2741 g_free(options);
2742
2743 return !!ret;
2744 }
2745
2746
2747 static void dump_snapshots(BlockDriverState *bs)
2748 {
2749 QEMUSnapshotInfo *sn_tab, *sn;
2750 int nb_sns, i;
2751
2752 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2753 if (nb_sns <= 0)
2754 return;
2755 printf("Snapshot list:\n");
2756 bdrv_snapshot_dump(NULL);
2757 printf("\n");
2758 for(i = 0; i < nb_sns; i++) {
2759 sn = &sn_tab[i];
2760 bdrv_snapshot_dump(sn);
2761 printf("\n");
2762 }
2763 g_free(sn_tab);
2764 }
2765
2766 static void dump_json_image_info_list(ImageInfoList *list)
2767 {
2768 QString *str;
2769 QObject *obj;
2770 Visitor *v = qobject_output_visitor_new(&obj);
2771
2772 visit_type_ImageInfoList(v, NULL, &list, &error_abort);
2773 visit_complete(v, &obj);
2774 str = qobject_to_json_pretty(obj);
2775 assert(str != NULL);
2776 printf("%s\n", qstring_get_str(str));
2777 qobject_unref(obj);
2778 visit_free(v);
2779 qobject_unref(str);
2780 }
2781
2782 static void dump_json_image_info(ImageInfo *info)
2783 {
2784 QString *str;
2785 QObject *obj;
2786 Visitor *v = qobject_output_visitor_new(&obj);
2787
2788 visit_type_ImageInfo(v, NULL, &info, &error_abort);
2789 visit_complete(v, &obj);
2790 str = qobject_to_json_pretty(obj);
2791 assert(str != NULL);
2792 printf("%s\n", qstring_get_str(str));
2793 qobject_unref(obj);
2794 visit_free(v);
2795 qobject_unref(str);
2796 }
2797
2798 static void dump_human_image_info_list(ImageInfoList *list)
2799 {
2800 ImageInfoList *elem;
2801 bool delim = false;
2802
2803 for (elem = list; elem; elem = elem->next) {
2804 if (delim) {
2805 printf("\n");
2806 }
2807 delim = true;
2808
2809 bdrv_image_info_dump(elem->value);
2810 }
2811 }
2812
2813 static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2814 {
2815 return strcmp(a, b) == 0;
2816 }
2817
2818 /**
2819 * Open an image file chain and return an ImageInfoList
2820 *
2821 * @filename: topmost image filename
2822 * @fmt: topmost image format (may be NULL to autodetect)
2823 * @chain: true - enumerate entire backing file chain
2824 * false - only topmost image file
2825 *
2826 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2827 * image file. If there was an error a message will have been printed to
2828 * stderr.
2829 */
2830 static ImageInfoList *collect_image_info_list(bool image_opts,
2831 const char *filename,
2832 const char *fmt,
2833 bool chain, bool force_share)
2834 {
2835 ImageInfoList *head = NULL;
2836 ImageInfoList **last = &head;
2837 GHashTable *filenames;
2838 Error *err = NULL;
2839
2840 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2841
2842 while (filename) {
2843 BlockBackend *blk;
2844 BlockDriverState *bs;
2845 ImageInfo *info;
2846 ImageInfoList *elem;
2847
2848 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2849 error_report("Backing file '%s' creates an infinite loop.",
2850 filename);
2851 goto err;
2852 }
2853 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2854
2855 blk = img_open(image_opts, filename, fmt,
2856 BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false,
2857 force_share);
2858 if (!blk) {
2859 goto err;
2860 }
2861 bs = blk_bs(blk);
2862
2863 bdrv_query_image_info(bs, &info, &err);
2864 if (err) {
2865 error_report_err(err);
2866 blk_unref(blk);
2867 goto err;
2868 }
2869
2870 elem = g_new0(ImageInfoList, 1);
2871 elem->value = info;
2872 *last = elem;
2873 last = &elem->next;
2874
2875 blk_unref(blk);
2876
2877 /* Clear parameters that only apply to the topmost image */
2878 filename = fmt = NULL;
2879 image_opts = false;
2880
2881 if (chain) {
2882 if (info->has_full_backing_filename) {
2883 filename = info->full_backing_filename;
2884 } else if (info->has_backing_filename) {
2885 error_report("Could not determine absolute backing filename,"
2886 " but backing filename '%s' present",
2887 info->backing_filename);
2888 goto err;
2889 }
2890 if (info->has_backing_filename_format) {
2891 fmt = info->backing_filename_format;
2892 }
2893 }
2894 }
2895 g_hash_table_destroy(filenames);
2896 return head;
2897
2898 err:
2899 qapi_free_ImageInfoList(head);
2900 g_hash_table_destroy(filenames);
2901 return NULL;
2902 }
2903
2904 static int img_info(int argc, char **argv)
2905 {
2906 int c;
2907 OutputFormat output_format = OFORMAT_HUMAN;
2908 bool chain = false;
2909 const char *filename, *fmt, *output;
2910 ImageInfoList *list;
2911 bool image_opts = false;
2912 bool force_share = false;
2913
2914 fmt = NULL;
2915 output = NULL;
2916 for(;;) {
2917 int option_index = 0;
2918 static const struct option long_options[] = {
2919 {"help", no_argument, 0, 'h'},
2920 {"format", required_argument, 0, 'f'},
2921 {"output", required_argument, 0, OPTION_OUTPUT},
2922 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
2923 {"object", required_argument, 0, OPTION_OBJECT},
2924 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
2925 {"force-share", no_argument, 0, 'U'},
2926 {0, 0, 0, 0}
2927 };
2928 c = getopt_long(argc, argv, ":f:hU",
2929 long_options, &option_index);
2930 if (c == -1) {
2931 break;
2932 }
2933 switch(c) {
2934 case ':':
2935 missing_argument(argv[optind - 1]);
2936 break;
2937 case '?':
2938 unrecognized_option(argv[optind - 1]);
2939 break;
2940 case 'h':
2941 help();
2942 break;
2943 case 'f':
2944 fmt = optarg;
2945 break;
2946 case 'U':
2947 force_share = true;
2948 break;
2949 case OPTION_OUTPUT:
2950 output = optarg;
2951 break;
2952 case OPTION_BACKING_CHAIN:
2953 chain = true;
2954 break;
2955 case OPTION_OBJECT: {
2956 QemuOpts *opts;
2957 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2958 optarg, true);
2959 if (!opts) {
2960 return 1;
2961 }
2962 } break;
2963 case OPTION_IMAGE_OPTS:
2964 image_opts = true;
2965 break;
2966 }
2967 }
2968 if (optind != argc - 1) {
2969 error_exit("Expecting one image file name");
2970 }
2971 filename = argv[optind++];
2972
2973 if (output && !strcmp(output, "json")) {
2974 output_format = OFORMAT_JSON;
2975 } else if (output && !strcmp(output, "human")) {
2976 output_format = OFORMAT_HUMAN;
2977 } else if (output) {
2978 error_report("--output must be used with human or json as argument.");
2979 return 1;
2980 }
2981
2982 if (qemu_opts_foreach(&qemu_object_opts,
2983 user_creatable_add_opts_foreach,
2984 qemu_img_object_print_help, &error_fatal)) {
2985 return 1;
2986 }
2987
2988 list = collect_image_info_list(image_opts, filename, fmt, chain,
2989 force_share);
2990 if (!list) {
2991 return 1;
2992 }
2993
2994 switch (output_format) {
2995 case OFORMAT_HUMAN:
2996 dump_human_image_info_list(list);
2997 break;
2998 case OFORMAT_JSON:
2999 if (chain) {
3000 dump_json_image_info_list(list);
3001 } else {
3002 dump_json_image_info(list->value);
3003 }
3004 break;
3005 }
3006
3007 qapi_free_ImageInfoList(list);
3008 return 0;
3009 }
3010
3011 static int dump_map_entry(OutputFormat output_format, MapEntry *e,
3012 MapEntry *next)
3013 {
3014 switch (output_format) {
3015 case OFORMAT_HUMAN:
3016 if (e->data && !e->has_offset) {
3017 error_report("File contains external, encrypted or compressed clusters.");
3018 return -1;
3019 }
3020 if (e->data && !e->zero) {
3021 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
3022 e->start, e->length,
3023 e->has_offset ? e->offset : 0,
3024 e->has_filename ? e->filename : "");
3025 }
3026 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
3027 * Modify the flags here to allow more coalescing.
3028 */
3029 if (next && (!next->data || next->zero)) {
3030 next->data = false;
3031 next->zero = true;
3032 }
3033 break;
3034 case OFORMAT_JSON:
3035 printf("{ \"start\": %"PRId64", \"length\": %"PRId64","
3036 " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
3037 e->start, e->length, e->depth,
3038 e->zero ? "true" : "false",
3039 e->data ? "true" : "false");
3040 if (e->has_offset) {
3041 printf(", \"offset\": %"PRId64"", e->offset);
3042 }
3043 putchar('}');
3044
3045 if (next) {
3046 puts(",");
3047 }
3048 break;
3049 }
3050 return 0;
3051 }
3052
3053 static int get_block_status(BlockDriverState *bs, int64_t offset,
3054 int64_t bytes, MapEntry *e)
3055 {
3056 int ret;
3057 int depth;
3058 BlockDriverState *file;
3059 bool has_offset;
3060 int64_t map;
3061 char *filename = NULL;
3062
3063 /* As an optimization, we could cache the current range of unallocated
3064 * clusters in each file of the chain, and avoid querying the same
3065 * range repeatedly.
3066 */
3067
3068 depth = 0;
3069 for (;;) {
3070 bs = bdrv_skip_filters(bs);
3071 ret = bdrv_block_status(bs, offset, bytes, &bytes, &map, &file);
3072 if (ret < 0) {
3073 return ret;
3074 }
3075 assert(bytes);
3076 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
3077 break;
3078 }
3079 bs = bdrv_cow_bs(bs);
3080 if (bs == NULL) {
3081 ret = 0;
3082 break;
3083 }
3084
3085 depth++;
3086 }
3087
3088 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
3089
3090 if (file && has_offset) {
3091 bdrv_refresh_filename(file);
3092 filename = file->filename;
3093 }
3094
3095 *e = (MapEntry) {
3096 .start = offset,
3097 .length = bytes,
3098 .data = !!(ret & BDRV_BLOCK_DATA),
3099 .zero = !!(ret & BDRV_BLOCK_ZERO),
3100 .offset = map,
3101 .has_offset = has_offset,
3102 .depth = depth,
3103 .has_filename = filename,
3104 .filename = filename,
3105 };
3106
3107 return 0;
3108 }
3109
3110 static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
3111 {
3112 if (curr->length == 0) {
3113 return false;
3114 }
3115 if (curr->zero != next->zero ||
3116 curr->data != next->data ||
3117 curr->depth != next->depth ||
3118 curr->has_filename != next->has_filename ||
3119 curr->has_offset != next->has_offset) {
3120 return false;
3121 }
3122 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
3123 return false;
3124 }
3125 if (curr->has_offset && curr->offset + curr->length != next->offset) {
3126 return false;
3127 }
3128 return true;
3129 }
3130
3131 static int img_map(int argc, char **argv)
3132 {
3133 int c;
3134 OutputFormat output_format = OFORMAT_HUMAN;
3135 BlockBackend *blk;
3136 BlockDriverState *bs;
3137 const char *filename, *fmt, *output;
3138 int64_t length;
3139 MapEntry curr = { .length = 0 }, next;
3140 int ret = 0;
3141 bool image_opts = false;
3142 bool force_share = false;
3143 int64_t start_offset = 0;
3144 int64_t max_length = -1;
3145
3146 fmt = NULL;
3147 output = NULL;
3148 for (;;) {
3149 int option_index = 0;
3150 static const struct option long_options[] = {
3151 {"help", no_argument, 0, 'h'},
3152 {"format", required_argument, 0, 'f'},
3153 {"output", required_argument, 0, OPTION_OUTPUT},
3154 {"object", required_argument, 0, OPTION_OBJECT},
3155 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
3156 {"force-share", no_argument, 0, 'U'},
3157 {"start-offset", required_argument, 0, 's'},
3158 {"max-length", required_argument, 0, 'l'},
3159 {0, 0, 0, 0}
3160 };
3161 c = getopt_long(argc, argv, ":f:s:l:hU",
3162 long_options, &option_index);
3163 if (c == -1) {
3164 break;
3165 }
3166 switch (c) {
3167 case ':':
3168 missing_argument(argv[optind - 1]);
3169 break;
3170 case '?':
3171 unrecognized_option(argv[optind - 1]);
3172 break;
3173 case 'h':
3174 help();
3175 break;
3176 case 'f':
3177 fmt = optarg;
3178 break;
3179 case 'U':
3180 force_share = true;
3181 break;
3182 case OPTION_OUTPUT:
3183 output = optarg;
3184 break;
3185 case 's':
3186 start_offset = cvtnum("start offset", optarg);
3187 if (start_offset < 0) {
3188 return 1;
3189 }
3190 break;
3191 case 'l':
3192 max_length = cvtnum("max length", optarg);
3193 if (max_length < 0) {
3194 return 1;
3195 }
3196 break;
3197 case OPTION_OBJECT: {
3198 QemuOpts *opts;
3199 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3200 optarg, true);
3201 if (!opts) {
3202 return 1;
3203 }
3204 } break;
3205 case OPTION_IMAGE_OPTS:
3206 image_opts = true;
3207 break;
3208 }
3209 }
3210 if (optind != argc - 1) {
3211 error_exit("Expecting one image file name");
3212 }
3213 filename = argv[optind];
3214
3215 if (output && !strcmp(output, "json")) {
3216 output_format = OFORMAT_JSON;
3217 } else if (output && !strcmp(output, "human")) {
3218 output_format = OFORMAT_HUMAN;
3219 } else if (output) {
3220 error_report("--output must be used with human or json as argument.");
3221 return 1;
3222 }
3223
3224 if (qemu_opts_foreach(&qemu_object_opts,
3225 user_creatable_add_opts_foreach,
3226 qemu_img_object_print_help, &error_fatal)) {
3227 return 1;
3228 }
3229
3230 blk = img_open(image_opts, filename, fmt, 0, false, false, force_share);
3231 if (!blk) {
3232 return 1;
3233 }
3234 bs = blk_bs(blk);
3235
3236 if (output_format == OFORMAT_HUMAN) {
3237 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
3238 } else if (output_format == OFORMAT_JSON) {
3239 putchar('[');
3240 }
3241
3242 length = blk_getlength(blk);
3243 if (length < 0) {
3244 error_report("Failed to get size for '%s'", filename);
3245 return 1;
3246 }
3247 if (max_length != -1) {
3248 length = MIN(start_offset + max_length, length);
3249 }
3250
3251 curr.start = start_offset;
3252 while (curr.start + curr.length < length) {
3253 int64_t offset = curr.start + curr.length;
3254 int64_t n = length - offset;
3255
3256 ret = get_block_status(bs, offset, n, &next);
3257 if (ret < 0) {
3258 error_report("Could not read file metadata: %s", strerror(-ret));
3259 goto out;
3260 }
3261
3262 if (entry_mergeable(&curr, &next)) {
3263 curr.length += next.length;
3264 continue;
3265 }
3266
3267 if (curr.length > 0) {
3268 ret = dump_map_entry(output_format, &curr, &next);
3269 if (ret < 0) {
3270 goto out;
3271 }
3272 }
3273 curr = next;
3274 }
3275
3276 ret = dump_map_entry(output_format, &curr, NULL);
3277 if (output_format == OFORMAT_JSON) {
3278 puts("]");
3279 }
3280
3281 out:
3282 blk_unref(blk);
3283 return ret < 0;
3284 }
3285
3286 #define SNAPSHOT_LIST 1
3287 #define SNAPSHOT_CREATE 2
3288 #define SNAPSHOT_APPLY 3
3289 #define SNAPSHOT_DELETE 4
3290
3291 static int img_snapshot(int argc, char **argv)
3292 {
3293 BlockBackend *blk;
3294 BlockDriverState *bs;
3295 QEMUSnapshotInfo sn;
3296 char *filename, *snapshot_name = NULL;
3297 int c, ret = 0, bdrv_oflags;
3298 int action = 0;
3299 qemu_timeval tv;
3300 bool quiet = false;
3301 Error *err = NULL;
3302 bool image_opts = false;
3303 bool force_share = false;
3304
3305 bdrv_oflags = BDRV_O_RDWR;
3306 /* Parse commandline parameters */
3307 for(;;) {
3308 static const struct option long_options[] = {
3309 {"help", no_argument, 0, 'h'},
3310 {"object", required_argument, 0, OPTION_OBJECT},
3311 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
3312 {"force-share", no_argument, 0, 'U'},
3313 {0, 0, 0, 0}
3314 };
3315 c = getopt_long(argc, argv, ":la:c:d:hqU",
3316 long_options, NULL);
3317 if (c == -1) {
3318 break;
3319 }
3320 switch(c) {
3321 case ':':
3322 missing_argument(argv[optind - 1]);
3323 break;
3324 case '?':
3325 unrecognized_option(argv[optind - 1]);
3326 break;
3327 case 'h':
3328 help();
3329 return 0;
3330 case 'l':
3331 if (action) {
3332 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
3333 return 0;
3334 }
3335 action = SNAPSHOT_LIST;
3336 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
3337 break;
3338 case 'a':
3339 if (action) {
3340 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
3341 return 0;
3342 }
3343 action = SNAPSHOT_APPLY;
3344 snapshot_name = optarg;
3345 break;
3346 case 'c':
3347 if (action) {
3348 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
3349 return 0;
3350 }
3351 action = SNAPSHOT_CREATE;
3352 snapshot_name = optarg;
3353 break;
3354 case 'd':
3355 if (action) {
3356 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
3357 return 0;
3358 }
3359 action = SNAPSHOT_DELETE;
3360 snapshot_name = optarg;
3361 break;
3362 case 'q':
3363 quiet = true;
3364 break;
3365 case 'U':
3366 force_share = true;
3367 break;
3368 case OPTION_OBJECT: {
3369 QemuOpts *opts;
3370 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3371 optarg, true);
3372 if (!opts) {
3373 return 1;
3374 }
3375 } break;
3376 case OPTION_IMAGE_OPTS:
3377 image_opts = true;
3378 break;
3379 }
3380 }
3381
3382 if (optind != argc - 1) {
3383 error_exit("Expecting one image file name");
3384 }
3385 filename = argv[optind++];
3386
3387 if (qemu_opts_foreach(&qemu_object_opts,
3388 user_creatable_add_opts_foreach,
3389 qemu_img_object_print_help, &error_fatal)) {
3390 return 1;
3391 }
3392
3393 /* Open the image */
3394 blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet,
3395 force_share);
3396 if (!blk) {
3397 return 1;
3398 }
3399 bs = blk_bs(blk);
3400
3401 /* Perform the requested action */
3402 switch(action) {
3403 case SNAPSHOT_LIST:
3404 dump_snapshots(bs);
3405 break;
3406
3407 case SNAPSHOT_CREATE:
3408 memset(&sn, 0, sizeof(sn));
3409 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
3410
3411