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