qmp: add persistent flag to block-dirty-bitmap-add
[qemu/ar7.git] / qemu-img.c
blob7f1de74c130cf301573b69a1194f8996ebf8d268
1 /*
2 * QEMU disk image utility
4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "qemu/osdep.h"
25 #include "qemu-version.h"
26 #include "qapi/error.h"
27 #include "qapi-visit.h"
28 #include "qapi/qobject-output-visitor.h"
29 #include "qapi/qmp/qerror.h"
30 #include "qapi/qmp/qjson.h"
31 #include "qapi/qmp/qbool.h"
32 #include "qemu/cutils.h"
33 #include "qemu/config-file.h"
34 #include "qemu/option.h"
35 #include "qemu/error-report.h"
36 #include "qemu/log.h"
37 #include "qom/object_interfaces.h"
38 #include "sysemu/sysemu.h"
39 #include "sysemu/block-backend.h"
40 #include "block/block_int.h"
41 #include "block/blockjob.h"
42 #include "block/qapi.h"
43 #include "crypto/init.h"
44 #include "trace/control.h"
45 #include <getopt.h>
47 #define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION QEMU_PKGVERSION \
48 "\n" QEMU_COPYRIGHT "\n"
50 typedef struct img_cmd_t {
51 const char *name;
52 int (*handler)(int argc, char **argv);
53 } img_cmd_t;
55 enum {
56 OPTION_OUTPUT = 256,
57 OPTION_BACKING_CHAIN = 257,
58 OPTION_OBJECT = 258,
59 OPTION_IMAGE_OPTS = 259,
60 OPTION_PATTERN = 260,
61 OPTION_FLUSH_INTERVAL = 261,
62 OPTION_NO_DRAIN = 262,
63 OPTION_TARGET_IMAGE_OPTS = 263,
66 typedef enum OutputFormat {
67 OFORMAT_JSON,
68 OFORMAT_HUMAN,
69 } OutputFormat;
71 /* Default to cache=writeback as data integrity is not important for qemu-img */
72 #define BDRV_DEFAULT_CACHE "writeback"
74 static void format_print(void *opaque, const char *name)
76 printf(" %s", name);
79 static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
81 va_list ap;
83 error_printf("qemu-img: ");
85 va_start(ap, fmt);
86 error_vprintf(fmt, ap);
87 va_end(ap);
89 error_printf("\nTry 'qemu-img --help' for more information\n");
90 exit(EXIT_FAILURE);
93 static void QEMU_NORETURN missing_argument(const char *option)
95 error_exit("missing argument for option '%s'", option);
98 static void QEMU_NORETURN unrecognized_option(const char *option)
100 error_exit("unrecognized option '%s'", option);
103 /* Please keep in synch with qemu-img.texi */
104 static void QEMU_NORETURN help(void)
106 const char *help_msg =
107 QEMU_IMG_VERSION
108 "usage: qemu-img [standard options] command [command options]\n"
109 "QEMU disk image utility\n"
110 "\n"
111 " '-h', '--help' display this help and exit\n"
112 " '-V', '--version' output version information and exit\n"
113 " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
114 " specify tracing options\n"
115 "\n"
116 "Command syntax:\n"
117 #define DEF(option, callback, arg_string) \
118 " " arg_string "\n"
119 #include "qemu-img-cmds.h"
120 #undef DEF
121 #undef GEN_DOCS
122 "\n"
123 "Command parameters:\n"
124 " 'filename' is a disk image filename\n"
125 " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
126 " manual page for a description of the object properties. The most common\n"
127 " object type is a 'secret', which is used to supply passwords and/or\n"
128 " encryption keys.\n"
129 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
130 " 'cache' is the cache mode used to write the output disk image, the valid\n"
131 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
132 " 'directsync' and 'unsafe' (default for convert)\n"
133 " 'src_cache' is the cache mode used to read input disk images, the valid\n"
134 " options are the same as for the 'cache' option\n"
135 " 'size' is the disk image size in bytes. Optional suffixes\n"
136 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
137 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
138 " supported. 'b' is ignored.\n"
139 " 'output_filename' is the destination disk image filename\n"
140 " 'output_fmt' is the destination format\n"
141 " 'options' is a comma separated list of format specific options in a\n"
142 " name=value format. Use -o ? for an overview of the options supported by the\n"
143 " used format\n"
144 " 'snapshot_param' is param used for internal snapshot, format\n"
145 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
146 " '[ID_OR_NAME]'\n"
147 " 'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n"
148 " instead\n"
149 " '-c' indicates that target image must be compressed (qcow format only)\n"
150 " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n"
151 " match exactly. The image doesn't need a working backing file before\n"
152 " rebasing in this case (useful for renaming the backing file)\n"
153 " '-h' with or without a command shows this help and lists the supported formats\n"
154 " '-p' show progress of command (only certain commands)\n"
155 " '-q' use Quiet mode - do not print any output (except errors)\n"
156 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
157 " contain only zeros for qemu-img to create a sparse image during\n"
158 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
159 " unallocated or zero sectors, and the destination image will always be\n"
160 " fully allocated\n"
161 " '--output' takes the format in which the output must be done (human or json)\n"
162 " '-n' skips the target volume creation (useful if the volume is created\n"
163 " prior to running qemu-img)\n"
164 "\n"
165 "Parameters to check subcommand:\n"
166 " '-r' tries to repair any inconsistencies that are found during the check.\n"
167 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
168 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
169 " hiding corruption that has already occurred.\n"
170 "\n"
171 "Parameters to convert subcommand:\n"
172 " '-m' specifies how many coroutines work in parallel during the convert\n"
173 " process (defaults to 8)\n"
174 " '-W' allow to write to the target out of order rather than sequential\n"
175 "\n"
176 "Parameters to snapshot subcommand:\n"
177 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
178 " '-a' applies a snapshot (revert disk to saved state)\n"
179 " '-c' creates a snapshot\n"
180 " '-d' deletes a snapshot\n"
181 " '-l' lists all snapshots in the given image\n"
182 "\n"
183 "Parameters to compare subcommand:\n"
184 " '-f' first image format\n"
185 " '-F' second image format\n"
186 " '-s' run in Strict mode - fail on different image size or sector allocation\n"
187 "\n"
188 "Parameters to dd subcommand:\n"
189 " 'bs=BYTES' read and write up to BYTES bytes at a time "
190 "(default: 512)\n"
191 " 'count=N' copy only N input blocks\n"
192 " 'if=FILE' read from FILE\n"
193 " 'of=FILE' write to FILE\n"
194 " 'skip=N' skip N bs-sized blocks at the start of input\n";
196 printf("%s\nSupported formats:", help_msg);
197 bdrv_iterate_format(format_print, NULL);
198 printf("\n");
199 exit(EXIT_SUCCESS);
202 static QemuOptsList qemu_object_opts = {
203 .name = "object",
204 .implied_opt_name = "qom-type",
205 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
206 .desc = {
211 static QemuOptsList qemu_source_opts = {
212 .name = "source",
213 .implied_opt_name = "file",
214 .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head),
215 .desc = {
220 static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
222 int ret = 0;
223 if (!quiet) {
224 va_list args;
225 va_start(args, fmt);
226 ret = vprintf(fmt, args);
227 va_end(args);
229 return ret;
233 static int print_block_option_help(const char *filename, const char *fmt)
235 BlockDriver *drv, *proto_drv;
236 QemuOptsList *create_opts = NULL;
237 Error *local_err = NULL;
239 /* Find driver and parse its options */
240 drv = bdrv_find_format(fmt);
241 if (!drv) {
242 error_report("Unknown file format '%s'", fmt);
243 return 1;
246 create_opts = qemu_opts_append(create_opts, drv->create_opts);
247 if (filename) {
248 proto_drv = bdrv_find_protocol(filename, true, &local_err);
249 if (!proto_drv) {
250 error_report_err(local_err);
251 qemu_opts_free(create_opts);
252 return 1;
254 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
257 qemu_opts_print_help(create_opts);
258 qemu_opts_free(create_opts);
259 return 0;
263 static BlockBackend *img_open_opts(const char *optstr,
264 QemuOpts *opts, int flags, bool writethrough,
265 bool quiet, bool force_share)
267 QDict *options;
268 Error *local_err = NULL;
269 BlockBackend *blk;
270 options = qemu_opts_to_qdict(opts, NULL);
271 if (force_share) {
272 if (qdict_haskey(options, BDRV_OPT_FORCE_SHARE)
273 && !qdict_get_bool(options, BDRV_OPT_FORCE_SHARE)) {
274 error_report("--force-share/-U conflicts with image options");
275 QDECREF(options);
276 return NULL;
278 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
280 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
281 if (!blk) {
282 error_reportf_err(local_err, "Could not open '%s': ", optstr);
283 return NULL;
285 blk_set_enable_write_cache(blk, !writethrough);
287 return blk;
290 static BlockBackend *img_open_file(const char *filename,
291 QDict *options,
292 const char *fmt, int flags,
293 bool writethrough, bool quiet,
294 bool force_share)
296 BlockBackend *blk;
297 Error *local_err = NULL;
299 if (!options) {
300 options = qdict_new();
302 if (fmt) {
303 qdict_put_str(options, "driver", fmt);
306 if (force_share) {
307 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
309 blk = blk_new_open(filename, NULL, options, flags, &local_err);
310 if (!blk) {
311 error_reportf_err(local_err, "Could not open '%s': ", filename);
312 return NULL;
314 blk_set_enable_write_cache(blk, !writethrough);
316 return blk;
320 static int img_add_key_secrets(void *opaque,
321 const char *name, const char *value,
322 Error **errp)
324 QDict *options = opaque;
326 if (g_str_has_suffix(name, "key-secret")) {
327 qdict_put(options, name, qstring_from_str(value));
330 return 0;
333 static BlockBackend *img_open_new_file(const char *filename,
334 QemuOpts *create_opts,
335 const char *fmt, int flags,
336 bool writethrough, bool quiet,
337 bool force_share)
339 QDict *options = NULL;
341 options = qdict_new();
342 qemu_opt_foreach(create_opts, img_add_key_secrets, options, &error_abort);
344 return img_open_file(filename, options, fmt, flags, writethrough, quiet,
345 force_share);
349 static BlockBackend *img_open(bool image_opts,
350 const char *filename,
351 const char *fmt, int flags, bool writethrough,
352 bool quiet, bool force_share)
354 BlockBackend *blk;
355 if (image_opts) {
356 QemuOpts *opts;
357 if (fmt) {
358 error_report("--image-opts and --format are mutually exclusive");
359 return NULL;
361 opts = qemu_opts_parse_noisily(qemu_find_opts("source"),
362 filename, true);
363 if (!opts) {
364 return NULL;
366 blk = img_open_opts(filename, opts, flags, writethrough, quiet,
367 force_share);
368 } else {
369 blk = img_open_file(filename, NULL, fmt, flags, writethrough, quiet,
370 force_share);
372 return blk;
376 static int add_old_style_options(const char *fmt, QemuOpts *opts,
377 const char *base_filename,
378 const char *base_fmt)
380 Error *err = NULL;
382 if (base_filename) {
383 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err);
384 if (err) {
385 error_report("Backing file not supported for file format '%s'",
386 fmt);
387 error_free(err);
388 return -1;
391 if (base_fmt) {
392 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err);
393 if (err) {
394 error_report("Backing file format not supported for file "
395 "format '%s'", fmt);
396 error_free(err);
397 return -1;
400 return 0;
403 static int64_t cvtnum(const char *s)
405 int err;
406 uint64_t value;
408 err = qemu_strtosz(s, NULL, &value);
409 if (err < 0) {
410 return err;
412 if (value > INT64_MAX) {
413 return -ERANGE;
415 return value;
418 static int img_create(int argc, char **argv)
420 int c;
421 uint64_t img_size = -1;
422 const char *fmt = "raw";
423 const char *base_fmt = NULL;
424 const char *filename;
425 const char *base_filename = NULL;
426 char *options = NULL;
427 Error *local_err = NULL;
428 bool quiet = false;
430 for(;;) {
431 static const struct option long_options[] = {
432 {"help", no_argument, 0, 'h'},
433 {"object", required_argument, 0, OPTION_OBJECT},
434 {0, 0, 0, 0}
436 c = getopt_long(argc, argv, ":F:b:f:ho:q",
437 long_options, NULL);
438 if (c == -1) {
439 break;
441 switch(c) {
442 case ':':
443 missing_argument(argv[optind - 1]);
444 break;
445 case '?':
446 unrecognized_option(argv[optind - 1]);
447 break;
448 case 'h':
449 help();
450 break;
451 case 'F':
452 base_fmt = optarg;
453 break;
454 case 'b':
455 base_filename = optarg;
456 break;
457 case 'f':
458 fmt = optarg;
459 break;
460 case 'o':
461 if (!is_valid_option_list(optarg)) {
462 error_report("Invalid option list: %s", optarg);
463 goto fail;
465 if (!options) {
466 options = g_strdup(optarg);
467 } else {
468 char *old_options = options;
469 options = g_strdup_printf("%s,%s", options, optarg);
470 g_free(old_options);
472 break;
473 case 'q':
474 quiet = true;
475 break;
476 case OPTION_OBJECT: {
477 QemuOpts *opts;
478 opts = qemu_opts_parse_noisily(&qemu_object_opts,
479 optarg, true);
480 if (!opts) {
481 goto fail;
483 } break;
487 /* Get the filename */
488 filename = (optind < argc) ? argv[optind] : NULL;
489 if (options && has_help_option(options)) {
490 g_free(options);
491 return print_block_option_help(filename, fmt);
494 if (optind >= argc) {
495 error_exit("Expecting image file name");
497 optind++;
499 if (qemu_opts_foreach(&qemu_object_opts,
500 user_creatable_add_opts_foreach,
501 NULL, NULL)) {
502 goto fail;
505 /* Get image size, if specified */
506 if (optind < argc) {
507 int64_t sval;
509 sval = cvtnum(argv[optind++]);
510 if (sval < 0) {
511 if (sval == -ERANGE) {
512 error_report("Image size must be less than 8 EiB!");
513 } else {
514 error_report("Invalid image size specified! You may use k, M, "
515 "G, T, P or E suffixes for ");
516 error_report("kilobytes, megabytes, gigabytes, terabytes, "
517 "petabytes and exabytes.");
519 goto fail;
521 img_size = (uint64_t)sval;
523 if (optind != argc) {
524 error_exit("Unexpected argument: %s", argv[optind]);
527 bdrv_img_create(filename, fmt, base_filename, base_fmt,
528 options, img_size, 0, quiet, &local_err);
529 if (local_err) {
530 error_reportf_err(local_err, "%s: ", filename);
531 goto fail;
534 g_free(options);
535 return 0;
537 fail:
538 g_free(options);
539 return 1;
542 static void dump_json_image_check(ImageCheck *check, bool quiet)
544 QString *str;
545 QObject *obj;
546 Visitor *v = qobject_output_visitor_new(&obj);
548 visit_type_ImageCheck(v, NULL, &check, &error_abort);
549 visit_complete(v, &obj);
550 str = qobject_to_json_pretty(obj);
551 assert(str != NULL);
552 qprintf(quiet, "%s\n", qstring_get_str(str));
553 qobject_decref(obj);
554 visit_free(v);
555 QDECREF(str);
558 static void dump_human_image_check(ImageCheck *check, bool quiet)
560 if (!(check->corruptions || check->leaks || check->check_errors)) {
561 qprintf(quiet, "No errors were found on the image.\n");
562 } else {
563 if (check->corruptions) {
564 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
565 "Data may be corrupted, or further writes to the image "
566 "may corrupt it.\n",
567 check->corruptions);
570 if (check->leaks) {
571 qprintf(quiet,
572 "\n%" PRId64 " leaked clusters were found on the image.\n"
573 "This means waste of disk space, but no harm to data.\n",
574 check->leaks);
577 if (check->check_errors) {
578 qprintf(quiet,
579 "\n%" PRId64
580 " internal errors have occurred during the check.\n",
581 check->check_errors);
585 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
586 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
587 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
588 check->allocated_clusters, check->total_clusters,
589 check->allocated_clusters * 100.0 / check->total_clusters,
590 check->fragmented_clusters * 100.0 / check->allocated_clusters,
591 check->compressed_clusters * 100.0 /
592 check->allocated_clusters);
595 if (check->image_end_offset) {
596 qprintf(quiet,
597 "Image end offset: %" PRId64 "\n", check->image_end_offset);
601 static int collect_image_check(BlockDriverState *bs,
602 ImageCheck *check,
603 const char *filename,
604 const char *fmt,
605 int fix)
607 int ret;
608 BdrvCheckResult result;
610 ret = bdrv_check(bs, &result, fix);
611 if (ret < 0) {
612 return ret;
615 check->filename = g_strdup(filename);
616 check->format = g_strdup(bdrv_get_format_name(bs));
617 check->check_errors = result.check_errors;
618 check->corruptions = result.corruptions;
619 check->has_corruptions = result.corruptions != 0;
620 check->leaks = result.leaks;
621 check->has_leaks = result.leaks != 0;
622 check->corruptions_fixed = result.corruptions_fixed;
623 check->has_corruptions_fixed = result.corruptions != 0;
624 check->leaks_fixed = result.leaks_fixed;
625 check->has_leaks_fixed = result.leaks != 0;
626 check->image_end_offset = result.image_end_offset;
627 check->has_image_end_offset = result.image_end_offset != 0;
628 check->total_clusters = result.bfi.total_clusters;
629 check->has_total_clusters = result.bfi.total_clusters != 0;
630 check->allocated_clusters = result.bfi.allocated_clusters;
631 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
632 check->fragmented_clusters = result.bfi.fragmented_clusters;
633 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
634 check->compressed_clusters = result.bfi.compressed_clusters;
635 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
637 return 0;
641 * Checks an image for consistency. Exit codes:
643 * 0 - Check completed, image is good
644 * 1 - Check not completed because of internal errors
645 * 2 - Check completed, image is corrupted
646 * 3 - Check completed, image has leaked clusters, but is good otherwise
647 * 63 - Checks are not supported by the image format
649 static int img_check(int argc, char **argv)
651 int c, ret;
652 OutputFormat output_format = OFORMAT_HUMAN;
653 const char *filename, *fmt, *output, *cache;
654 BlockBackend *blk;
655 BlockDriverState *bs;
656 int fix = 0;
657 int flags = BDRV_O_CHECK;
658 bool writethrough;
659 ImageCheck *check;
660 bool quiet = false;
661 bool image_opts = false;
662 bool force_share = false;
664 fmt = NULL;
665 output = NULL;
666 cache = BDRV_DEFAULT_CACHE;
668 for(;;) {
669 int option_index = 0;
670 static const struct option long_options[] = {
671 {"help", no_argument, 0, 'h'},
672 {"format", required_argument, 0, 'f'},
673 {"repair", required_argument, 0, 'r'},
674 {"output", required_argument, 0, OPTION_OUTPUT},
675 {"object", required_argument, 0, OPTION_OBJECT},
676 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
677 {"force-share", no_argument, 0, 'U'},
678 {0, 0, 0, 0}
680 c = getopt_long(argc, argv, ":hf:r:T:qU",
681 long_options, &option_index);
682 if (c == -1) {
683 break;
685 switch(c) {
686 case ':':
687 missing_argument(argv[optind - 1]);
688 break;
689 case '?':
690 unrecognized_option(argv[optind - 1]);
691 break;
692 case 'h':
693 help();
694 break;
695 case 'f':
696 fmt = optarg;
697 break;
698 case 'r':
699 flags |= BDRV_O_RDWR;
701 if (!strcmp(optarg, "leaks")) {
702 fix = BDRV_FIX_LEAKS;
703 } else if (!strcmp(optarg, "all")) {
704 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
705 } else {
706 error_exit("Unknown option value for -r "
707 "(expecting 'leaks' or 'all'): %s", optarg);
709 break;
710 case OPTION_OUTPUT:
711 output = optarg;
712 break;
713 case 'T':
714 cache = optarg;
715 break;
716 case 'q':
717 quiet = true;
718 break;
719 case 'U':
720 force_share = true;
721 break;
722 case OPTION_OBJECT: {
723 QemuOpts *opts;
724 opts = qemu_opts_parse_noisily(&qemu_object_opts,
725 optarg, true);
726 if (!opts) {
727 return 1;
729 } break;
730 case OPTION_IMAGE_OPTS:
731 image_opts = true;
732 break;
735 if (optind != argc - 1) {
736 error_exit("Expecting one image file name");
738 filename = argv[optind++];
740 if (output && !strcmp(output, "json")) {
741 output_format = OFORMAT_JSON;
742 } else if (output && !strcmp(output, "human")) {
743 output_format = OFORMAT_HUMAN;
744 } else if (output) {
745 error_report("--output must be used with human or json as argument.");
746 return 1;
749 if (qemu_opts_foreach(&qemu_object_opts,
750 user_creatable_add_opts_foreach,
751 NULL, NULL)) {
752 return 1;
755 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
756 if (ret < 0) {
757 error_report("Invalid source cache option: %s", cache);
758 return 1;
761 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
762 force_share);
763 if (!blk) {
764 return 1;
766 bs = blk_bs(blk);
768 check = g_new0(ImageCheck, 1);
769 ret = collect_image_check(bs, check, filename, fmt, fix);
771 if (ret == -ENOTSUP) {
772 error_report("This image format does not support checks");
773 ret = 63;
774 goto fail;
777 if (check->corruptions_fixed || check->leaks_fixed) {
778 int corruptions_fixed, leaks_fixed;
780 leaks_fixed = check->leaks_fixed;
781 corruptions_fixed = check->corruptions_fixed;
783 if (output_format == OFORMAT_HUMAN) {
784 qprintf(quiet,
785 "The following inconsistencies were found and repaired:\n\n"
786 " %" PRId64 " leaked clusters\n"
787 " %" PRId64 " corruptions\n\n"
788 "Double checking the fixed image now...\n",
789 check->leaks_fixed,
790 check->corruptions_fixed);
793 ret = collect_image_check(bs, check, filename, fmt, 0);
795 check->leaks_fixed = leaks_fixed;
796 check->corruptions_fixed = corruptions_fixed;
799 if (!ret) {
800 switch (output_format) {
801 case OFORMAT_HUMAN:
802 dump_human_image_check(check, quiet);
803 break;
804 case OFORMAT_JSON:
805 dump_json_image_check(check, quiet);
806 break;
810 if (ret || check->check_errors) {
811 if (ret) {
812 error_report("Check failed: %s", strerror(-ret));
813 } else {
814 error_report("Check failed");
816 ret = 1;
817 goto fail;
820 if (check->corruptions) {
821 ret = 2;
822 } else if (check->leaks) {
823 ret = 3;
824 } else {
825 ret = 0;
828 fail:
829 qapi_free_ImageCheck(check);
830 blk_unref(blk);
831 return ret;
834 typedef struct CommonBlockJobCBInfo {
835 BlockDriverState *bs;
836 Error **errp;
837 } CommonBlockJobCBInfo;
839 static void common_block_job_cb(void *opaque, int ret)
841 CommonBlockJobCBInfo *cbi = opaque;
843 if (ret < 0) {
844 error_setg_errno(cbi->errp, -ret, "Block job failed");
848 static void run_block_job(BlockJob *job, Error **errp)
850 AioContext *aio_context = blk_get_aio_context(job->blk);
851 int ret = 0;
853 aio_context_acquire(aio_context);
854 block_job_ref(job);
855 do {
856 aio_poll(aio_context, true);
857 qemu_progress_print(job->len ?
858 ((float)job->offset / job->len * 100.f) : 0.0f, 0);
859 } while (!job->ready && !job->completed);
861 if (!job->completed) {
862 ret = block_job_complete_sync(job, errp);
863 } else {
864 ret = job->ret;
866 block_job_unref(job);
867 aio_context_release(aio_context);
869 /* publish completion progress only when success */
870 if (!ret) {
871 qemu_progress_print(100.f, 0);
875 static int img_commit(int argc, char **argv)
877 int c, ret, flags;
878 const char *filename, *fmt, *cache, *base;
879 BlockBackend *blk;
880 BlockDriverState *bs, *base_bs;
881 BlockJob *job;
882 bool progress = false, quiet = false, drop = false;
883 bool writethrough;
884 Error *local_err = NULL;
885 CommonBlockJobCBInfo cbi;
886 bool image_opts = false;
887 AioContext *aio_context;
889 fmt = NULL;
890 cache = BDRV_DEFAULT_CACHE;
891 base = NULL;
892 for(;;) {
893 static const struct option long_options[] = {
894 {"help", no_argument, 0, 'h'},
895 {"object", required_argument, 0, OPTION_OBJECT},
896 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
897 {0, 0, 0, 0}
899 c = getopt_long(argc, argv, ":f:ht:b:dpq",
900 long_options, NULL);
901 if (c == -1) {
902 break;
904 switch(c) {
905 case ':':
906 missing_argument(argv[optind - 1]);
907 break;
908 case '?':
909 unrecognized_option(argv[optind - 1]);
910 break;
911 case 'h':
912 help();
913 break;
914 case 'f':
915 fmt = optarg;
916 break;
917 case 't':
918 cache = optarg;
919 break;
920 case 'b':
921 base = optarg;
922 /* -b implies -d */
923 drop = true;
924 break;
925 case 'd':
926 drop = true;
927 break;
928 case 'p':
929 progress = true;
930 break;
931 case 'q':
932 quiet = true;
933 break;
934 case OPTION_OBJECT: {
935 QemuOpts *opts;
936 opts = qemu_opts_parse_noisily(&qemu_object_opts,
937 optarg, true);
938 if (!opts) {
939 return 1;
941 } break;
942 case OPTION_IMAGE_OPTS:
943 image_opts = true;
944 break;
948 /* Progress is not shown in Quiet mode */
949 if (quiet) {
950 progress = false;
953 if (optind != argc - 1) {
954 error_exit("Expecting one image file name");
956 filename = argv[optind++];
958 if (qemu_opts_foreach(&qemu_object_opts,
959 user_creatable_add_opts_foreach,
960 NULL, NULL)) {
961 return 1;
964 flags = BDRV_O_RDWR | BDRV_O_UNMAP;
965 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
966 if (ret < 0) {
967 error_report("Invalid cache option: %s", cache);
968 return 1;
971 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
972 false);
973 if (!blk) {
974 return 1;
976 bs = blk_bs(blk);
978 qemu_progress_init(progress, 1.f);
979 qemu_progress_print(0.f, 100);
981 if (base) {
982 base_bs = bdrv_find_backing_image(bs, base);
983 if (!base_bs) {
984 error_setg(&local_err,
985 "Did not find '%s' in the backing chain of '%s'",
986 base, filename);
987 goto done;
989 } else {
990 /* This is different from QMP, which by default uses the deepest file in
991 * the backing chain (i.e., the very base); however, the traditional
992 * behavior of qemu-img commit is using the immediate backing file. */
993 base_bs = backing_bs(bs);
994 if (!base_bs) {
995 error_setg(&local_err, "Image does not have a backing file");
996 goto done;
1000 cbi = (CommonBlockJobCBInfo){
1001 .errp = &local_err,
1002 .bs = bs,
1005 aio_context = bdrv_get_aio_context(bs);
1006 aio_context_acquire(aio_context);
1007 commit_active_start("commit", bs, base_bs, BLOCK_JOB_DEFAULT, 0,
1008 BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb,
1009 &cbi, false, &local_err);
1010 aio_context_release(aio_context);
1011 if (local_err) {
1012 goto done;
1015 /* When the block job completes, the BlockBackend reference will point to
1016 * the old backing file. In order to avoid that the top image is already
1017 * deleted, so we can still empty it afterwards, increment the reference
1018 * counter here preemptively. */
1019 if (!drop) {
1020 bdrv_ref(bs);
1023 job = block_job_get("commit");
1024 run_block_job(job, &local_err);
1025 if (local_err) {
1026 goto unref_backing;
1029 if (!drop && bs->drv->bdrv_make_empty) {
1030 ret = bs->drv->bdrv_make_empty(bs);
1031 if (ret) {
1032 error_setg_errno(&local_err, -ret, "Could not empty %s",
1033 filename);
1034 goto unref_backing;
1038 unref_backing:
1039 if (!drop) {
1040 bdrv_unref(bs);
1043 done:
1044 qemu_progress_end();
1046 blk_unref(blk);
1048 if (local_err) {
1049 error_report_err(local_err);
1050 return 1;
1053 qprintf(quiet, "Image committed.\n");
1054 return 0;
1058 * Returns true iff the first sector pointed to by 'buf' contains at least
1059 * a non-NUL byte.
1061 * 'pnum' is set to the number of sectors (including and immediately following
1062 * the first one) that are known to be in the same allocated/unallocated state.
1064 static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
1066 bool is_zero;
1067 int i;
1069 if (n <= 0) {
1070 *pnum = 0;
1071 return 0;
1073 is_zero = buffer_is_zero(buf, 512);
1074 for(i = 1; i < n; i++) {
1075 buf += 512;
1076 if (is_zero != buffer_is_zero(buf, 512)) {
1077 break;
1080 *pnum = i;
1081 return !is_zero;
1085 * Like is_allocated_sectors, but if the buffer starts with a used sector,
1086 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1087 * breaking up write requests for only small sparse areas.
1089 static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
1090 int min)
1092 int ret;
1093 int num_checked, num_used;
1095 if (n < min) {
1096 min = n;
1099 ret = is_allocated_sectors(buf, n, pnum);
1100 if (!ret) {
1101 return ret;
1104 num_used = *pnum;
1105 buf += BDRV_SECTOR_SIZE * *pnum;
1106 n -= *pnum;
1107 num_checked = num_used;
1109 while (n > 0) {
1110 ret = is_allocated_sectors(buf, n, pnum);
1112 buf += BDRV_SECTOR_SIZE * *pnum;
1113 n -= *pnum;
1114 num_checked += *pnum;
1115 if (ret) {
1116 num_used = num_checked;
1117 } else if (*pnum >= min) {
1118 break;
1122 *pnum = num_used;
1123 return 1;
1127 * Compares two buffers sector by sector. Returns 0 if the first sector of both
1128 * buffers matches, non-zero otherwise.
1130 * pnum is set to the number of sectors (including and immediately following
1131 * the first one) that are known to have the same comparison result
1133 static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
1134 int *pnum)
1136 bool res;
1137 int i;
1139 if (n <= 0) {
1140 *pnum = 0;
1141 return 0;
1144 res = !!memcmp(buf1, buf2, 512);
1145 for(i = 1; i < n; i++) {
1146 buf1 += 512;
1147 buf2 += 512;
1149 if (!!memcmp(buf1, buf2, 512) != res) {
1150 break;
1154 *pnum = i;
1155 return res;
1158 #define IO_BUF_SIZE (2 * 1024 * 1024)
1160 static int64_t sectors_to_bytes(int64_t sectors)
1162 return sectors << BDRV_SECTOR_BITS;
1165 static int64_t sectors_to_process(int64_t total, int64_t from)
1167 return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
1171 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1173 * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero
1174 * data and negative value on error.
1176 * @param blk: BlockBackend for the image
1177 * @param sect_num: Number of first sector to check
1178 * @param sect_count: Number of sectors to check
1179 * @param filename: Name of disk file we are checking (logging purpose)
1180 * @param buffer: Allocated buffer for storing read data
1181 * @param quiet: Flag for quiet mode
1183 static int check_empty_sectors(BlockBackend *blk, int64_t sect_num,
1184 int sect_count, const char *filename,
1185 uint8_t *buffer, bool quiet)
1187 int pnum, ret = 0;
1188 ret = blk_pread(blk, sect_num << BDRV_SECTOR_BITS, buffer,
1189 sect_count << BDRV_SECTOR_BITS);
1190 if (ret < 0) {
1191 error_report("Error while reading offset %" PRId64 " of %s: %s",
1192 sectors_to_bytes(sect_num), filename, strerror(-ret));
1193 return ret;
1195 ret = is_allocated_sectors(buffer, sect_count, &pnum);
1196 if (ret || pnum != sect_count) {
1197 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1198 sectors_to_bytes(ret ? sect_num : sect_num + pnum));
1199 return 1;
1202 return 0;
1206 * Compares two images. Exit codes:
1208 * 0 - Images are identical
1209 * 1 - Images differ
1210 * >1 - Error occurred
1212 static int img_compare(int argc, char **argv)
1214 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
1215 BlockBackend *blk1, *blk2;
1216 BlockDriverState *bs1, *bs2;
1217 int64_t total_sectors1, total_sectors2;
1218 uint8_t *buf1 = NULL, *buf2 = NULL;
1219 int pnum1, pnum2;
1220 int allocated1, allocated2;
1221 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1222 bool progress = false, quiet = false, strict = false;
1223 int flags;
1224 bool writethrough;
1225 int64_t total_sectors;
1226 int64_t sector_num = 0;
1227 int64_t nb_sectors;
1228 int c, pnum;
1229 uint64_t progress_base;
1230 bool image_opts = false;
1231 bool force_share = false;
1233 cache = BDRV_DEFAULT_CACHE;
1234 for (;;) {
1235 static const struct option long_options[] = {
1236 {"help", no_argument, 0, 'h'},
1237 {"object", required_argument, 0, OPTION_OBJECT},
1238 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
1239 {"force-share", no_argument, 0, 'U'},
1240 {0, 0, 0, 0}
1242 c = getopt_long(argc, argv, ":hf:F:T:pqsU",
1243 long_options, NULL);
1244 if (c == -1) {
1245 break;
1247 switch (c) {
1248 case ':':
1249 missing_argument(argv[optind - 1]);
1250 break;
1251 case '?':
1252 unrecognized_option(argv[optind - 1]);
1253 break;
1254 case 'h':
1255 help();
1256 break;
1257 case 'f':
1258 fmt1 = optarg;
1259 break;
1260 case 'F':
1261 fmt2 = optarg;
1262 break;
1263 case 'T':
1264 cache = optarg;
1265 break;
1266 case 'p':
1267 progress = true;
1268 break;
1269 case 'q':
1270 quiet = true;
1271 break;
1272 case 's':
1273 strict = true;
1274 break;
1275 case 'U':
1276 force_share = true;
1277 break;
1278 case OPTION_OBJECT: {
1279 QemuOpts *opts;
1280 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1281 optarg, true);
1282 if (!opts) {
1283 ret = 2;
1284 goto out4;
1286 } break;
1287 case OPTION_IMAGE_OPTS:
1288 image_opts = true;
1289 break;
1293 /* Progress is not shown in Quiet mode */
1294 if (quiet) {
1295 progress = false;
1299 if (optind != argc - 2) {
1300 error_exit("Expecting two image file names");
1302 filename1 = argv[optind++];
1303 filename2 = argv[optind++];
1305 if (qemu_opts_foreach(&qemu_object_opts,
1306 user_creatable_add_opts_foreach,
1307 NULL, NULL)) {
1308 ret = 2;
1309 goto out4;
1312 /* Initialize before goto out */
1313 qemu_progress_init(progress, 2.0);
1315 flags = 0;
1316 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
1317 if (ret < 0) {
1318 error_report("Invalid source cache option: %s", cache);
1319 ret = 2;
1320 goto out3;
1323 blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet,
1324 force_share);
1325 if (!blk1) {
1326 ret = 2;
1327 goto out3;
1330 blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet,
1331 force_share);
1332 if (!blk2) {
1333 ret = 2;
1334 goto out2;
1336 bs1 = blk_bs(blk1);
1337 bs2 = blk_bs(blk2);
1339 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1340 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
1341 total_sectors1 = blk_nb_sectors(blk1);
1342 if (total_sectors1 < 0) {
1343 error_report("Can't get size of %s: %s",
1344 filename1, strerror(-total_sectors1));
1345 ret = 4;
1346 goto out;
1348 total_sectors2 = blk_nb_sectors(blk2);
1349 if (total_sectors2 < 0) {
1350 error_report("Can't get size of %s: %s",
1351 filename2, strerror(-total_sectors2));
1352 ret = 4;
1353 goto out;
1355 total_sectors = MIN(total_sectors1, total_sectors2);
1356 progress_base = MAX(total_sectors1, total_sectors2);
1358 qemu_progress_print(0, 100);
1360 if (strict && total_sectors1 != total_sectors2) {
1361 ret = 1;
1362 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1363 goto out;
1366 for (;;) {
1367 int64_t status1, status2;
1368 BlockDriverState *file;
1370 nb_sectors = sectors_to_process(total_sectors, sector_num);
1371 if (nb_sectors <= 0) {
1372 break;
1374 status1 = bdrv_get_block_status_above(bs1, NULL, sector_num,
1375 total_sectors1 - sector_num,
1376 &pnum1, &file);
1377 if (status1 < 0) {
1378 ret = 3;
1379 error_report("Sector allocation test failed for %s", filename1);
1380 goto out;
1382 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
1384 status2 = bdrv_get_block_status_above(bs2, NULL, sector_num,
1385 total_sectors2 - sector_num,
1386 &pnum2, &file);
1387 if (status2 < 0) {
1388 ret = 3;
1389 error_report("Sector allocation test failed for %s", filename2);
1390 goto out;
1392 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
1393 if (pnum1) {
1394 nb_sectors = MIN(nb_sectors, pnum1);
1396 if (pnum2) {
1397 nb_sectors = MIN(nb_sectors, pnum2);
1400 if (strict) {
1401 if ((status1 & ~BDRV_BLOCK_OFFSET_MASK) !=
1402 (status2 & ~BDRV_BLOCK_OFFSET_MASK)) {
1403 ret = 1;
1404 qprintf(quiet, "Strict mode: Offset %" PRId64
1405 " block status mismatch!\n",
1406 sectors_to_bytes(sector_num));
1407 goto out;
1410 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
1411 nb_sectors = MIN(pnum1, pnum2);
1412 } else if (allocated1 == allocated2) {
1413 if (allocated1) {
1414 ret = blk_pread(blk1, sector_num << BDRV_SECTOR_BITS, buf1,
1415 nb_sectors << BDRV_SECTOR_BITS);
1416 if (ret < 0) {
1417 error_report("Error while reading offset %" PRId64 " of %s:"
1418 " %s", sectors_to_bytes(sector_num), filename1,
1419 strerror(-ret));
1420 ret = 4;
1421 goto out;
1423 ret = blk_pread(blk2, sector_num << BDRV_SECTOR_BITS, buf2,
1424 nb_sectors << BDRV_SECTOR_BITS);
1425 if (ret < 0) {
1426 error_report("Error while reading offset %" PRId64
1427 " of %s: %s", sectors_to_bytes(sector_num),
1428 filename2, strerror(-ret));
1429 ret = 4;
1430 goto out;
1432 ret = compare_sectors(buf1, buf2, nb_sectors, &pnum);
1433 if (ret || pnum != nb_sectors) {
1434 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1435 sectors_to_bytes(
1436 ret ? sector_num : sector_num + pnum));
1437 ret = 1;
1438 goto out;
1441 } else {
1443 if (allocated1) {
1444 ret = check_empty_sectors(blk1, sector_num, nb_sectors,
1445 filename1, buf1, quiet);
1446 } else {
1447 ret = check_empty_sectors(blk2, sector_num, nb_sectors,
1448 filename2, buf1, quiet);
1450 if (ret) {
1451 if (ret < 0) {
1452 error_report("Error while reading offset %" PRId64 ": %s",
1453 sectors_to_bytes(sector_num), strerror(-ret));
1454 ret = 4;
1456 goto out;
1459 sector_num += nb_sectors;
1460 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1463 if (total_sectors1 != total_sectors2) {
1464 BlockBackend *blk_over;
1465 int64_t total_sectors_over;
1466 const char *filename_over;
1468 qprintf(quiet, "Warning: Image size mismatch!\n");
1469 if (total_sectors1 > total_sectors2) {
1470 total_sectors_over = total_sectors1;
1471 blk_over = blk1;
1472 filename_over = filename1;
1473 } else {
1474 total_sectors_over = total_sectors2;
1475 blk_over = blk2;
1476 filename_over = filename2;
1479 for (;;) {
1480 int64_t count;
1482 nb_sectors = sectors_to_process(total_sectors_over, sector_num);
1483 if (nb_sectors <= 0) {
1484 break;
1486 ret = bdrv_is_allocated_above(blk_bs(blk_over), NULL,
1487 sector_num * BDRV_SECTOR_SIZE,
1488 nb_sectors * BDRV_SECTOR_SIZE,
1489 &count);
1490 if (ret < 0) {
1491 ret = 3;
1492 error_report("Sector allocation test failed for %s",
1493 filename_over);
1494 goto out;
1497 /* TODO relax this once bdrv_is_allocated_above does not enforce
1498 * sector alignment */
1499 assert(QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE));
1500 nb_sectors = count >> BDRV_SECTOR_BITS;
1501 if (ret) {
1502 ret = check_empty_sectors(blk_over, sector_num, nb_sectors,
1503 filename_over, buf1, quiet);
1504 if (ret) {
1505 if (ret < 0) {
1506 error_report("Error while reading offset %" PRId64
1507 " of %s: %s", sectors_to_bytes(sector_num),
1508 filename_over, strerror(-ret));
1509 ret = 4;
1511 goto out;
1514 sector_num += nb_sectors;
1515 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1519 qprintf(quiet, "Images are identical.\n");
1520 ret = 0;
1522 out:
1523 qemu_vfree(buf1);
1524 qemu_vfree(buf2);
1525 blk_unref(blk2);
1526 out2:
1527 blk_unref(blk1);
1528 out3:
1529 qemu_progress_end();
1530 out4:
1531 return ret;
1534 enum ImgConvertBlockStatus {
1535 BLK_DATA,
1536 BLK_ZERO,
1537 BLK_BACKING_FILE,
1540 #define MAX_COROUTINES 16
1542 typedef struct ImgConvertState {
1543 BlockBackend **src;
1544 int64_t *src_sectors;
1545 int src_num;
1546 int64_t total_sectors;
1547 int64_t allocated_sectors;
1548 int64_t allocated_done;
1549 int64_t sector_num;
1550 int64_t wr_offs;
1551 enum ImgConvertBlockStatus status;
1552 int64_t sector_next_status;
1553 BlockBackend *target;
1554 bool has_zero_init;
1555 bool compressed;
1556 bool target_has_backing;
1557 bool wr_in_order;
1558 int min_sparse;
1559 size_t cluster_sectors;
1560 size_t buf_sectors;
1561 long num_coroutines;
1562 int running_coroutines;
1563 Coroutine *co[MAX_COROUTINES];
1564 int64_t wait_sector_num[MAX_COROUTINES];
1565 CoMutex lock;
1566 int ret;
1567 } ImgConvertState;
1569 static void convert_select_part(ImgConvertState *s, int64_t sector_num,
1570 int *src_cur, int64_t *src_cur_offset)
1572 *src_cur = 0;
1573 *src_cur_offset = 0;
1574 while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) {
1575 *src_cur_offset += s->src_sectors[*src_cur];
1576 (*src_cur)++;
1577 assert(*src_cur < s->src_num);
1581 static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1583 int64_t ret, src_cur_offset;
1584 int n, src_cur;
1586 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1588 assert(s->total_sectors > sector_num);
1589 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1591 if (s->sector_next_status <= sector_num) {
1592 BlockDriverState *file;
1593 if (s->target_has_backing) {
1594 ret = bdrv_get_block_status(blk_bs(s->src[src_cur]),
1595 sector_num - src_cur_offset,
1596 n, &n, &file);
1597 } else {
1598 ret = bdrv_get_block_status_above(blk_bs(s->src[src_cur]), NULL,
1599 sector_num - src_cur_offset,
1600 n, &n, &file);
1602 if (ret < 0) {
1603 return ret;
1606 if (ret & BDRV_BLOCK_ZERO) {
1607 s->status = BLK_ZERO;
1608 } else if (ret & BDRV_BLOCK_DATA) {
1609 s->status = BLK_DATA;
1610 } else {
1611 s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA;
1614 s->sector_next_status = sector_num + n;
1617 n = MIN(n, s->sector_next_status - sector_num);
1618 if (s->status == BLK_DATA) {
1619 n = MIN(n, s->buf_sectors);
1622 /* We need to write complete clusters for compressed images, so if an
1623 * unallocated area is shorter than that, we must consider the whole
1624 * cluster allocated. */
1625 if (s->compressed) {
1626 if (n < s->cluster_sectors) {
1627 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1628 s->status = BLK_DATA;
1629 } else {
1630 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1634 return n;
1637 static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num,
1638 int nb_sectors, uint8_t *buf)
1640 int n, ret;
1641 QEMUIOVector qiov;
1642 struct iovec iov;
1644 assert(nb_sectors <= s->buf_sectors);
1645 while (nb_sectors > 0) {
1646 BlockBackend *blk;
1647 int src_cur;
1648 int64_t bs_sectors, src_cur_offset;
1650 /* In the case of compression with multiple source files, we can get a
1651 * nb_sectors that spreads into the next part. So we must be able to
1652 * read across multiple BDSes for one convert_read() call. */
1653 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1654 blk = s->src[src_cur];
1655 bs_sectors = s->src_sectors[src_cur];
1657 n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1658 iov.iov_base = buf;
1659 iov.iov_len = n << BDRV_SECTOR_BITS;
1660 qemu_iovec_init_external(&qiov, &iov, 1);
1662 ret = blk_co_preadv(
1663 blk, (sector_num - src_cur_offset) << BDRV_SECTOR_BITS,
1664 n << BDRV_SECTOR_BITS, &qiov, 0);
1665 if (ret < 0) {
1666 return ret;
1669 sector_num += n;
1670 nb_sectors -= n;
1671 buf += n * BDRV_SECTOR_SIZE;
1674 return 0;
1678 static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
1679 int nb_sectors, uint8_t *buf,
1680 enum ImgConvertBlockStatus status)
1682 int ret;
1683 QEMUIOVector qiov;
1684 struct iovec iov;
1686 while (nb_sectors > 0) {
1687 int n = nb_sectors;
1688 BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0;
1690 switch (status) {
1691 case BLK_BACKING_FILE:
1692 /* If we have a backing file, leave clusters unallocated that are
1693 * unallocated in the source image, so that the backing file is
1694 * visible at the respective offset. */
1695 assert(s->target_has_backing);
1696 break;
1698 case BLK_DATA:
1699 /* If we're told to keep the target fully allocated (-S 0) or there
1700 * is real non-zero data, we must write it. Otherwise we can treat
1701 * it as zero sectors.
1702 * Compressed clusters need to be written as a whole, so in that
1703 * case we can only save the write if the buffer is completely
1704 * zeroed. */
1705 if (!s->min_sparse ||
1706 (!s->compressed &&
1707 is_allocated_sectors_min(buf, n, &n, s->min_sparse)) ||
1708 (s->compressed &&
1709 !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)))
1711 iov.iov_base = buf;
1712 iov.iov_len = n << BDRV_SECTOR_BITS;
1713 qemu_iovec_init_external(&qiov, &iov, 1);
1715 ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS,
1716 n << BDRV_SECTOR_BITS, &qiov, flags);
1717 if (ret < 0) {
1718 return ret;
1720 break;
1722 /* fall-through */
1724 case BLK_ZERO:
1725 if (s->has_zero_init) {
1726 assert(!s->target_has_backing);
1727 break;
1729 ret = blk_co_pwrite_zeroes(s->target,
1730 sector_num << BDRV_SECTOR_BITS,
1731 n << BDRV_SECTOR_BITS, 0);
1732 if (ret < 0) {
1733 return ret;
1735 break;
1738 sector_num += n;
1739 nb_sectors -= n;
1740 buf += n * BDRV_SECTOR_SIZE;
1743 return 0;
1746 static void coroutine_fn convert_co_do_copy(void *opaque)
1748 ImgConvertState *s = opaque;
1749 uint8_t *buf = NULL;
1750 int ret, i;
1751 int index = -1;
1753 for (i = 0; i < s->num_coroutines; i++) {
1754 if (s->co[i] == qemu_coroutine_self()) {
1755 index = i;
1756 break;
1759 assert(index >= 0);
1761 s->running_coroutines++;
1762 buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1764 while (1) {
1765 int n;
1766 int64_t sector_num;
1767 enum ImgConvertBlockStatus status;
1769 qemu_co_mutex_lock(&s->lock);
1770 if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) {
1771 qemu_co_mutex_unlock(&s->lock);
1772 break;
1774 n = convert_iteration_sectors(s, s->sector_num);
1775 if (n < 0) {
1776 qemu_co_mutex_unlock(&s->lock);
1777 s->ret = n;
1778 break;
1780 /* save current sector and allocation status to local variables */
1781 sector_num = s->sector_num;
1782 status = s->status;
1783 if (!s->min_sparse && s->status == BLK_ZERO) {
1784 n = MIN(n, s->buf_sectors);
1786 /* increment global sector counter so that other coroutines can
1787 * already continue reading beyond this request */
1788 s->sector_num += n;
1789 qemu_co_mutex_unlock(&s->lock);
1791 if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) {
1792 s->allocated_done += n;
1793 qemu_progress_print(100.0 * s->allocated_done /
1794 s->allocated_sectors, 0);
1797 if (status == BLK_DATA) {
1798 ret = convert_co_read(s, sector_num, n, buf);
1799 if (ret < 0) {
1800 error_report("error while reading sector %" PRId64
1801 ": %s", sector_num, strerror(-ret));
1802 s->ret = ret;
1804 } else if (!s->min_sparse && status == BLK_ZERO) {
1805 status = BLK_DATA;
1806 memset(buf, 0x00, n * BDRV_SECTOR_SIZE);
1809 if (s->wr_in_order) {
1810 /* keep writes in order */
1811 while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) {
1812 s->wait_sector_num[index] = sector_num;
1813 qemu_coroutine_yield();
1815 s->wait_sector_num[index] = -1;
1818 if (s->ret == -EINPROGRESS) {
1819 ret = convert_co_write(s, sector_num, n, buf, status);
1820 if (ret < 0) {
1821 error_report("error while writing sector %" PRId64
1822 ": %s", sector_num, strerror(-ret));
1823 s->ret = ret;
1827 if (s->wr_in_order) {
1828 /* reenter the coroutine that might have waited
1829 * for this write to complete */
1830 s->wr_offs = sector_num + n;
1831 for (i = 0; i < s->num_coroutines; i++) {
1832 if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) {
1834 * A -> B -> A cannot occur because A has
1835 * s->wait_sector_num[i] == -1 during A -> B. Therefore
1836 * B will never enter A during this time window.
1838 qemu_coroutine_enter(s->co[i]);
1839 break;
1845 qemu_vfree(buf);
1846 s->co[index] = NULL;
1847 s->running_coroutines--;
1848 if (!s->running_coroutines && s->ret == -EINPROGRESS) {
1849 /* the convert job finished successfully */
1850 s->ret = 0;
1854 static int convert_do_copy(ImgConvertState *s)
1856 int ret, i, n;
1857 int64_t sector_num = 0;
1859 /* Check whether we have zero initialisation or can get it efficiently */
1860 s->has_zero_init = s->min_sparse && !s->target_has_backing
1861 ? bdrv_has_zero_init(blk_bs(s->target))
1862 : false;
1864 if (!s->has_zero_init && !s->target_has_backing &&
1865 bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
1867 ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP);
1868 if (ret == 0) {
1869 s->has_zero_init = true;
1873 /* Allocate buffer for copied data. For compressed images, only one cluster
1874 * can be copied at a time. */
1875 if (s->compressed) {
1876 if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
1877 error_report("invalid cluster size");
1878 return -EINVAL;
1880 s->buf_sectors = s->cluster_sectors;
1883 while (sector_num < s->total_sectors) {
1884 n = convert_iteration_sectors(s, sector_num);
1885 if (n < 0) {
1886 return n;
1888 if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
1890 s->allocated_sectors += n;
1892 sector_num += n;
1895 /* Do the copy */
1896 s->sector_next_status = 0;
1897 s->ret = -EINPROGRESS;
1899 qemu_co_mutex_init(&s->lock);
1900 for (i = 0; i < s->num_coroutines; i++) {
1901 s->co[i] = qemu_coroutine_create(convert_co_do_copy, s);
1902 s->wait_sector_num[i] = -1;
1903 qemu_coroutine_enter(s->co[i]);
1906 while (s->running_coroutines) {
1907 main_loop_wait(false);
1910 if (s->compressed && !s->ret) {
1911 /* signal EOF to align */
1912 ret = blk_pwrite_compressed(s->target, 0, NULL, 0);
1913 if (ret < 0) {
1914 return ret;
1918 return s->ret;
1921 static int img_convert(int argc, char **argv)
1923 int c, bs_i, flags, src_flags = 0;
1924 const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe",
1925 *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL,
1926 *out_filename, *out_baseimg_param, *snapshot_name = NULL;
1927 BlockDriver *drv = NULL, *proto_drv = NULL;
1928 BlockDriverInfo bdi;
1929 BlockDriverState *out_bs;
1930 QemuOpts *opts = NULL, *sn_opts = NULL;
1931 QemuOptsList *create_opts = NULL;
1932 char *options = NULL;
1933 Error *local_err = NULL;
1934 bool writethrough, src_writethrough, quiet = false, image_opts = false,
1935 skip_create = false, progress = false, tgt_image_opts = false;
1936 int64_t ret = -EINVAL;
1937 bool force_share = false;
1939 ImgConvertState s = (ImgConvertState) {
1940 /* Need at least 4k of zeros for sparse detection */
1941 .min_sparse = 8,
1942 .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
1943 .wr_in_order = true,
1944 .num_coroutines = 8,
1947 for(;;) {
1948 static const struct option long_options[] = {
1949 {"help", no_argument, 0, 'h'},
1950 {"object", required_argument, 0, OPTION_OBJECT},
1951 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
1952 {"force-share", no_argument, 0, 'U'},
1953 {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
1954 {0, 0, 0, 0}
1956 c = getopt_long(argc, argv, ":hf:O:B:co:s:l:S:pt:T:qnm:WU",
1957 long_options, NULL);
1958 if (c == -1) {
1959 break;
1961 switch(c) {
1962 case ':':
1963 missing_argument(argv[optind - 1]);
1964 break;
1965 case '?':
1966 unrecognized_option(argv[optind - 1]);
1967 break;
1968 case 'h':
1969 help();
1970 break;
1971 case 'f':
1972 fmt = optarg;
1973 break;
1974 case 'O':
1975 out_fmt = optarg;
1976 break;
1977 case 'B':
1978 out_baseimg = optarg;
1979 break;
1980 case 'c':
1981 s.compressed = true;
1982 break;
1983 case 'o':
1984 if (!is_valid_option_list(optarg)) {
1985 error_report("Invalid option list: %s", optarg);
1986 goto fail_getopt;
1988 if (!options) {
1989 options = g_strdup(optarg);
1990 } else {
1991 char *old_options = options;
1992 options = g_strdup_printf("%s,%s", options, optarg);
1993 g_free(old_options);
1995 break;
1996 case 's':
1997 snapshot_name = optarg;
1998 break;
1999 case 'l':
2000 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
2001 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
2002 optarg, false);
2003 if (!sn_opts) {
2004 error_report("Failed in parsing snapshot param '%s'",
2005 optarg);
2006 goto fail_getopt;
2008 } else {
2009 snapshot_name = optarg;
2011 break;
2012 case 'S':
2014 int64_t sval;
2016 sval = cvtnum(optarg);
2017 if (sval < 0) {
2018 error_report("Invalid minimum zero buffer size for sparse output specified");
2019 goto fail_getopt;
2022 s.min_sparse = sval / BDRV_SECTOR_SIZE;
2023 break;
2025 case 'p':
2026 progress = true;
2027 break;
2028 case 't':
2029 cache = optarg;
2030 break;
2031 case 'T':
2032 src_cache = optarg;
2033 break;
2034 case 'q':
2035 quiet = true;
2036 break;
2037 case 'n':
2038 skip_create = true;
2039 break;
2040 case 'm':
2041 if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) ||
2042 s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) {
2043 error_report("Invalid number of coroutines. Allowed number of"
2044 " coroutines is between 1 and %d", MAX_COROUTINES);
2045 goto fail_getopt;
2047 break;
2048 case 'W':
2049 s.wr_in_order = false;
2050 break;
2051 case 'U':
2052 force_share = true;
2053 break;
2054 case OPTION_OBJECT: {
2055 QemuOpts *object_opts;
2056 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
2057 optarg, true);
2058 if (!object_opts) {
2059 goto fail_getopt;
2061 break;
2063 case OPTION_IMAGE_OPTS:
2064 image_opts = true;
2065 break;
2066 case OPTION_TARGET_IMAGE_OPTS:
2067 tgt_image_opts = true;
2068 break;
2072 if (!out_fmt && !tgt_image_opts) {
2073 out_fmt = "raw";
2076 if (qemu_opts_foreach(&qemu_object_opts,
2077 user_creatable_add_opts_foreach,
2078 NULL, NULL)) {
2079 goto fail_getopt;
2082 if (!s.wr_in_order && s.compressed) {
2083 error_report("Out of order write and compress are mutually exclusive");
2084 goto fail_getopt;
2087 if (tgt_image_opts && !skip_create) {
2088 error_report("--target-image-opts requires use of -n flag");
2089 goto fail_getopt;
2092 s.src_num = argc - optind - 1;
2093 out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;
2095 if (options && has_help_option(options)) {
2096 if (out_fmt) {
2097 ret = print_block_option_help(out_filename, out_fmt);
2098 goto fail_getopt;
2099 } else {
2100 error_report("Option help requires a format be specified");
2101 goto fail_getopt;
2105 if (s.src_num < 1) {
2106 error_report("Must specify image file name");
2107 goto fail_getopt;
2111 /* ret is still -EINVAL until here */
2112 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
2113 if (ret < 0) {
2114 error_report("Invalid source cache option: %s", src_cache);
2115 goto fail_getopt;
2118 /* Initialize before goto out */
2119 if (quiet) {
2120 progress = false;
2122 qemu_progress_init(progress, 1.0);
2123 qemu_progress_print(0, 100);
2125 s.src = g_new0(BlockBackend *, s.src_num);
2126 s.src_sectors = g_new(int64_t, s.src_num);
2128 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2129 s.src[bs_i] = img_open(image_opts, argv[optind + bs_i],
2130 fmt, src_flags, src_writethrough, quiet,
2131 force_share);
2132 if (!s.src[bs_i]) {
2133 ret = -1;
2134 goto out;
2136 s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]);
2137 if (s.src_sectors[bs_i] < 0) {
2138 error_report("Could not get size of %s: %s",
2139 argv[optind + bs_i], strerror(-s.src_sectors[bs_i]));
2140 ret = -1;
2141 goto out;
2143 s.total_sectors += s.src_sectors[bs_i];
2146 if (sn_opts) {
2147 bdrv_snapshot_load_tmp(blk_bs(s.src[0]),
2148 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
2149 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
2150 &local_err);
2151 } else if (snapshot_name != NULL) {
2152 if (s.src_num > 1) {
2153 error_report("No support for concatenating multiple snapshot");
2154 ret = -1;
2155 goto out;
2158 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name,
2159 &local_err);
2161 if (local_err) {
2162 error_reportf_err(local_err, "Failed to load snapshot: ");
2163 ret = -1;
2164 goto out;
2167 if (!skip_create) {
2168 /* Find driver and parse its options */
2169 drv = bdrv_find_format(out_fmt);
2170 if (!drv) {
2171 error_report("Unknown file format '%s'", out_fmt);
2172 ret = -1;
2173 goto out;
2176 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
2177 if (!proto_drv) {
2178 error_report_err(local_err);
2179 ret = -1;
2180 goto out;
2183 if (!drv->create_opts) {
2184 error_report("Format driver '%s' does not support image creation",
2185 drv->format_name);
2186 ret = -1;
2187 goto out;
2190 if (!proto_drv->create_opts) {
2191 error_report("Protocol driver '%s' does not support image creation",
2192 proto_drv->format_name);
2193 ret = -1;
2194 goto out;
2197 create_opts = qemu_opts_append(create_opts, drv->create_opts);
2198 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
2200 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
2201 if (options) {
2202 qemu_opts_do_parse(opts, options, NULL, &local_err);
2203 if (local_err) {
2204 error_report_err(local_err);
2205 ret = -1;
2206 goto out;
2210 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s.total_sectors * 512,
2211 &error_abort);
2212 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
2213 if (ret < 0) {
2214 goto out;
2218 /* Get backing file name if -o backing_file was used */
2219 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
2220 if (out_baseimg_param) {
2221 out_baseimg = out_baseimg_param;
2223 s.target_has_backing = (bool) out_baseimg;
2225 if (s.src_num > 1 && out_baseimg) {
2226 error_report("Having a backing file for the target makes no sense when "
2227 "concatenating multiple input images");
2228 ret = -1;
2229 goto out;
2232 /* Check if compression is supported */
2233 if (s.compressed) {
2234 bool encryption =
2235 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
2236 const char *encryptfmt =
2237 qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT);
2238 const char *preallocation =
2239 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
2241 if (drv && !drv->bdrv_co_pwritev_compressed) {
2242 error_report("Compression not supported for this file format");
2243 ret = -1;
2244 goto out;
2247 if (encryption || encryptfmt) {
2248 error_report("Compression and encryption not supported at "
2249 "the same time");
2250 ret = -1;
2251 goto out;
2254 if (preallocation
2255 && strcmp(preallocation, "off"))
2257 error_report("Compression and preallocation not supported at "
2258 "the same time");
2259 ret = -1;
2260 goto out;
2264 if (!skip_create) {
2265 /* Create the new image */
2266 ret = bdrv_create(drv, out_filename, opts, &local_err);
2267 if (ret < 0) {
2268 error_reportf_err(local_err, "%s: error while converting %s: ",
2269 out_filename, out_fmt);
2270 goto out;
2274 flags = s.min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
2275 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
2276 if (ret < 0) {
2277 error_report("Invalid cache option: %s", cache);
2278 goto out;
2281 if (skip_create) {
2282 s.target = img_open(tgt_image_opts, out_filename, out_fmt,
2283 flags, writethrough, quiet, false);
2284 } else {
2285 /* TODO ultimately we should allow --target-image-opts
2286 * to be used even when -n is not given.
2287 * That has to wait for bdrv_create to be improved
2288 * to allow filenames in option syntax
2290 s.target = img_open_new_file(out_filename, opts, out_fmt,
2291 flags, writethrough, quiet, false);
2293 if (!s.target) {
2294 ret = -1;
2295 goto out;
2297 out_bs = blk_bs(s.target);
2299 if (s.compressed && !out_bs->drv->bdrv_co_pwritev_compressed) {
2300 error_report("Compression not supported for this file format");
2301 ret = -1;
2302 goto out;
2305 /* increase bufsectors from the default 4096 (2M) if opt_transfer
2306 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
2307 * as maximum. */
2308 s.buf_sectors = MIN(32768,
2309 MAX(s.buf_sectors,
2310 MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS,
2311 out_bs->bl.pdiscard_alignment >>
2312 BDRV_SECTOR_BITS)));
2314 if (skip_create) {
2315 int64_t output_sectors = blk_nb_sectors(s.target);
2316 if (output_sectors < 0) {
2317 error_report("unable to get output image length: %s",
2318 strerror(-output_sectors));
2319 ret = -1;
2320 goto out;
2321 } else if (output_sectors < s.total_sectors) {
2322 error_report("output file is smaller than input file");
2323 ret = -1;
2324 goto out;
2328 ret = bdrv_get_info(out_bs, &bdi);
2329 if (ret < 0) {
2330 if (s.compressed) {
2331 error_report("could not get block driver info");
2332 goto out;
2334 } else {
2335 s.compressed = s.compressed || bdi.needs_compressed_writes;
2336 s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
2339 ret = convert_do_copy(&s);
2340 out:
2341 if (!ret) {
2342 qemu_progress_print(100, 0);
2344 qemu_progress_end();
2345 qemu_opts_del(opts);
2346 qemu_opts_free(create_opts);
2347 qemu_opts_del(sn_opts);
2348 blk_unref(s.target);
2349 if (s.src) {
2350 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2351 blk_unref(s.src[bs_i]);
2353 g_free(s.src);
2355 g_free(s.src_sectors);
2356 fail_getopt:
2357 g_free(options);
2359 return !!ret;
2363 static void dump_snapshots(BlockDriverState *bs)
2365 QEMUSnapshotInfo *sn_tab, *sn;
2366 int nb_sns, i;
2368 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2369 if (nb_sns <= 0)
2370 return;
2371 printf("Snapshot list:\n");
2372 bdrv_snapshot_dump(fprintf, stdout, NULL);
2373 printf("\n");
2374 for(i = 0; i < nb_sns; i++) {
2375 sn = &sn_tab[i];
2376 bdrv_snapshot_dump(fprintf, stdout, sn);
2377 printf("\n");
2379 g_free(sn_tab);
2382 static void dump_json_image_info_list(ImageInfoList *list)
2384 QString *str;
2385 QObject *obj;
2386 Visitor *v = qobject_output_visitor_new(&obj);
2388 visit_type_ImageInfoList(v, NULL, &list, &error_abort);
2389 visit_complete(v, &obj);
2390 str = qobject_to_json_pretty(obj);
2391 assert(str != NULL);
2392 printf("%s\n", qstring_get_str(str));
2393 qobject_decref(obj);
2394 visit_free(v);
2395 QDECREF(str);
2398 static void dump_json_image_info(ImageInfo *info)
2400 QString *str;
2401 QObject *obj;
2402 Visitor *v = qobject_output_visitor_new(&obj);
2404 visit_type_ImageInfo(v, NULL, &info, &error_abort);
2405 visit_complete(v, &obj);
2406 str = qobject_to_json_pretty(obj);
2407 assert(str != NULL);
2408 printf("%s\n", qstring_get_str(str));
2409 qobject_decref(obj);
2410 visit_free(v);
2411 QDECREF(str);
2414 static void dump_human_image_info_list(ImageInfoList *list)
2416 ImageInfoList *elem;
2417 bool delim = false;
2419 for (elem = list; elem; elem = elem->next) {
2420 if (delim) {
2421 printf("\n");
2423 delim = true;
2425 bdrv_image_info_dump(fprintf, stdout, elem->value);
2429 static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2431 return strcmp(a, b) == 0;
2435 * Open an image file chain and return an ImageInfoList
2437 * @filename: topmost image filename
2438 * @fmt: topmost image format (may be NULL to autodetect)
2439 * @chain: true - enumerate entire backing file chain
2440 * false - only topmost image file
2442 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2443 * image file. If there was an error a message will have been printed to
2444 * stderr.
2446 static ImageInfoList *collect_image_info_list(bool image_opts,
2447 const char *filename,
2448 const char *fmt,
2449 bool chain, bool force_share)
2451 ImageInfoList *head = NULL;
2452 ImageInfoList **last = &head;
2453 GHashTable *filenames;
2454 Error *err = NULL;
2456 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2458 while (filename) {
2459 BlockBackend *blk;
2460 BlockDriverState *bs;
2461 ImageInfo *info;
2462 ImageInfoList *elem;
2464 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2465 error_report("Backing file '%s' creates an infinite loop.",
2466 filename);
2467 goto err;
2469 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2471 blk = img_open(image_opts, filename, fmt,
2472 BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false,
2473 force_share);
2474 if (!blk) {
2475 goto err;
2477 bs = blk_bs(blk);
2479 bdrv_query_image_info(bs, &info, &err);
2480 if (err) {
2481 error_report_err(err);
2482 blk_unref(blk);
2483 goto err;
2486 elem = g_new0(ImageInfoList, 1);
2487 elem->value = info;
2488 *last = elem;
2489 last = &elem->next;
2491 blk_unref(blk);
2493 filename = fmt = NULL;
2494 if (chain) {
2495 if (info->has_full_backing_filename) {
2496 filename = info->full_backing_filename;
2497 } else if (info->has_backing_filename) {
2498 error_report("Could not determine absolute backing filename,"
2499 " but backing filename '%s' present",
2500 info->backing_filename);
2501 goto err;
2503 if (info->has_backing_filename_format) {
2504 fmt = info->backing_filename_format;
2508 g_hash_table_destroy(filenames);
2509 return head;
2511 err:
2512 qapi_free_ImageInfoList(head);
2513 g_hash_table_destroy(filenames);
2514 return NULL;
2517 static int img_info(int argc, char **argv)
2519 int c;
2520 OutputFormat output_format = OFORMAT_HUMAN;
2521 bool chain = false;
2522 const char *filename, *fmt, *output;
2523 ImageInfoList *list;
2524 bool image_opts = false;
2525 bool force_share = false;
2527 fmt = NULL;
2528 output = NULL;
2529 for(;;) {
2530 int option_index = 0;
2531 static const struct option long_options[] = {
2532 {"help", no_argument, 0, 'h'},
2533 {"format", required_argument, 0, 'f'},
2534 {"output", required_argument, 0, OPTION_OUTPUT},
2535 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
2536 {"object", required_argument, 0, OPTION_OBJECT},
2537 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
2538 {"force-share", no_argument, 0, 'U'},
2539 {0, 0, 0, 0}
2541 c = getopt_long(argc, argv, ":f:hU",
2542 long_options, &option_index);
2543 if (c == -1) {
2544 break;
2546 switch(c) {
2547 case ':':
2548 missing_argument(argv[optind - 1]);
2549 break;
2550 case '?':
2551 unrecognized_option(argv[optind - 1]);
2552 break;
2553 case 'h':
2554 help();
2555 break;
2556 case 'f':
2557 fmt = optarg;
2558 break;
2559 case 'U':
2560 force_share = true;
2561 break;
2562 case OPTION_OUTPUT:
2563 output = optarg;
2564 break;
2565 case OPTION_BACKING_CHAIN:
2566 chain = true;
2567 break;
2568 case OPTION_OBJECT: {
2569 QemuOpts *opts;
2570 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2571 optarg, true);
2572 if (!opts) {
2573 return 1;
2575 } break;
2576 case OPTION_IMAGE_OPTS:
2577 image_opts = true;
2578 break;
2581 if (optind != argc - 1) {
2582 error_exit("Expecting one image file name");
2584 filename = argv[optind++];
2586 if (output && !strcmp(output, "json")) {
2587 output_format = OFORMAT_JSON;
2588 } else if (output && !strcmp(output, "human")) {
2589 output_format = OFORMAT_HUMAN;
2590 } else if (output) {
2591 error_report("--output must be used with human or json as argument.");
2592 return 1;
2595 if (qemu_opts_foreach(&qemu_object_opts,
2596 user_creatable_add_opts_foreach,
2597 NULL, NULL)) {
2598 return 1;
2601 list = collect_image_info_list(image_opts, filename, fmt, chain,
2602 force_share);
2603 if (!list) {
2604 return 1;
2607 switch (output_format) {
2608 case OFORMAT_HUMAN:
2609 dump_human_image_info_list(list);
2610 break;
2611 case OFORMAT_JSON:
2612 if (chain) {
2613 dump_json_image_info_list(list);
2614 } else {
2615 dump_json_image_info(list->value);
2617 break;
2620 qapi_free_ImageInfoList(list);
2621 return 0;
2624 static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2625 MapEntry *next)
2627 switch (output_format) {
2628 case OFORMAT_HUMAN:
2629 if (e->data && !e->has_offset) {
2630 error_report("File contains external, encrypted or compressed clusters.");
2631 exit(1);
2633 if (e->data && !e->zero) {
2634 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
2635 e->start, e->length,
2636 e->has_offset ? e->offset : 0,
2637 e->has_filename ? e->filename : "");
2639 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2640 * Modify the flags here to allow more coalescing.
2642 if (next && (!next->data || next->zero)) {
2643 next->data = false;
2644 next->zero = true;
2646 break;
2647 case OFORMAT_JSON:
2648 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2649 " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
2650 (e->start == 0 ? "[" : ",\n"),
2651 e->start, e->length, e->depth,
2652 e->zero ? "true" : "false",
2653 e->data ? "true" : "false");
2654 if (e->has_offset) {
2655 printf(", \"offset\": %"PRId64"", e->offset);
2657 putchar('}');
2659 if (!next) {
2660 printf("]\n");
2662 break;
2666 static int get_block_status(BlockDriverState *bs, int64_t sector_num,
2667 int nb_sectors, MapEntry *e)
2669 int64_t ret;
2670 int depth;
2671 BlockDriverState *file;
2672 bool has_offset;
2674 /* As an optimization, we could cache the current range of unallocated
2675 * clusters in each file of the chain, and avoid querying the same
2676 * range repeatedly.
2679 depth = 0;
2680 for (;;) {
2681 ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors,
2682 &file);
2683 if (ret < 0) {
2684 return ret;
2686 assert(nb_sectors);
2687 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2688 break;
2690 bs = backing_bs(bs);
2691 if (bs == NULL) {
2692 ret = 0;
2693 break;
2696 depth++;
2699 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2701 *e = (MapEntry) {
2702 .start = sector_num * BDRV_SECTOR_SIZE,
2703 .length = nb_sectors * BDRV_SECTOR_SIZE,
2704 .data = !!(ret & BDRV_BLOCK_DATA),
2705 .zero = !!(ret & BDRV_BLOCK_ZERO),
2706 .offset = ret & BDRV_BLOCK_OFFSET_MASK,
2707 .has_offset = has_offset,
2708 .depth = depth,
2709 .has_filename = file && has_offset,
2710 .filename = file && has_offset ? file->filename : NULL,
2713 return 0;
2716 static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2718 if (curr->length == 0) {
2719 return false;
2721 if (curr->zero != next->zero ||
2722 curr->data != next->data ||
2723 curr->depth != next->depth ||
2724 curr->has_filename != next->has_filename ||
2725 curr->has_offset != next->has_offset) {
2726 return false;
2728 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2729 return false;
2731 if (curr->has_offset && curr->offset + curr->length != next->offset) {
2732 return false;
2734 return true;
2737 static int img_map(int argc, char **argv)
2739 int c;
2740 OutputFormat output_format = OFORMAT_HUMAN;
2741 BlockBackend *blk;
2742 BlockDriverState *bs;
2743 const char *filename, *fmt, *output;
2744 int64_t length;
2745 MapEntry curr = { .length = 0 }, next;
2746 int ret = 0;
2747 bool image_opts = false;
2748 bool force_share = false;
2750 fmt = NULL;
2751 output = NULL;
2752 for (;;) {
2753 int option_index = 0;
2754 static const struct option long_options[] = {
2755 {"help", no_argument, 0, 'h'},
2756 {"format", required_argument, 0, 'f'},
2757 {"output", required_argument, 0, OPTION_OUTPUT},
2758 {"object", required_argument, 0, OPTION_OBJECT},
2759 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
2760 {"force-share", no_argument, 0, 'U'},
2761 {0, 0, 0, 0}
2763 c = getopt_long(argc, argv, ":f:hU",
2764 long_options, &option_index);
2765 if (c == -1) {
2766 break;
2768 switch (c) {
2769 case ':':
2770 missing_argument(argv[optind - 1]);
2771 break;
2772 case '?':
2773 unrecognized_option(argv[optind - 1]);
2774 break;
2775 case 'h':
2776 help();
2777 break;
2778 case 'f':
2779 fmt = optarg;
2780 break;
2781 case 'U':
2782 force_share = true;
2783 break;
2784 case OPTION_OUTPUT:
2785 output = optarg;
2786 break;
2787 case OPTION_OBJECT: {
2788 QemuOpts *opts;
2789 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2790 optarg, true);
2791 if (!opts) {
2792 return 1;
2794 } break;
2795 case OPTION_IMAGE_OPTS:
2796 image_opts = true;
2797 break;
2800 if (optind != argc - 1) {
2801 error_exit("Expecting one image file name");
2803 filename = argv[optind];
2805 if (output && !strcmp(output, "json")) {
2806 output_format = OFORMAT_JSON;
2807 } else if (output && !strcmp(output, "human")) {
2808 output_format = OFORMAT_HUMAN;
2809 } else if (output) {
2810 error_report("--output must be used with human or json as argument.");
2811 return 1;
2814 if (qemu_opts_foreach(&qemu_object_opts,
2815 user_creatable_add_opts_foreach,
2816 NULL, NULL)) {
2817 return 1;
2820 blk = img_open(image_opts, filename, fmt, 0, false, false, force_share);
2821 if (!blk) {
2822 return 1;
2824 bs = blk_bs(blk);
2826 if (output_format == OFORMAT_HUMAN) {
2827 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2830 length = blk_getlength(blk);
2831 while (curr.start + curr.length < length) {
2832 int64_t nsectors_left;
2833 int64_t sector_num;
2834 int n;
2836 sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS;
2838 /* Probe up to 1 GiB at a time. */
2839 nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num;
2840 n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left);
2841 ret = get_block_status(bs, sector_num, n, &next);
2843 if (ret < 0) {
2844 error_report("Could not read file metadata: %s", strerror(-ret));
2845 goto out;
2848 if (entry_mergeable(&curr, &next)) {
2849 curr.length += next.length;
2850 continue;
2853 if (curr.length > 0) {
2854 dump_map_entry(output_format, &curr, &next);
2856 curr = next;
2859 dump_map_entry(output_format, &curr, NULL);
2861 out:
2862 blk_unref(blk);
2863 return ret < 0;
2866 #define SNAPSHOT_LIST 1
2867 #define SNAPSHOT_CREATE 2
2868 #define SNAPSHOT_APPLY 3
2869 #define SNAPSHOT_DELETE 4
2871 static int img_snapshot(int argc, char **argv)
2873 BlockBackend *blk;
2874 BlockDriverState *bs;
2875 QEMUSnapshotInfo sn;
2876 char *filename, *snapshot_name = NULL;
2877 int c, ret = 0, bdrv_oflags;
2878 int action = 0;
2879 qemu_timeval tv;
2880 bool quiet = false;
2881 Error *err = NULL;
2882 bool image_opts = false;
2883 bool force_share = false;
2885 bdrv_oflags = BDRV_O_RDWR;
2886 /* Parse commandline parameters */
2887 for(;;) {
2888 static const struct option long_options[] = {
2889 {"help", no_argument, 0, 'h'},
2890 {"object", required_argument, 0, OPTION_OBJECT},
2891 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
2892 {"force-share", no_argument, 0, 'U'},
2893 {0, 0, 0, 0}
2895 c = getopt_long(argc, argv, ":la:c:d:hqU",
2896 long_options, NULL);
2897 if (c == -1) {
2898 break;
2900 switch(c) {
2901 case ':':
2902 missing_argument(argv[optind - 1]);
2903 break;
2904 case '?':
2905 unrecognized_option(argv[optind - 1]);
2906 break;
2907 case 'h':
2908 help();
2909 return 0;
2910 case 'l':
2911 if (action) {
2912 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
2913 return 0;
2915 action = SNAPSHOT_LIST;
2916 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
2917 break;
2918 case 'a':
2919 if (action) {
2920 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
2921 return 0;
2923 action = SNAPSHOT_APPLY;
2924 snapshot_name = optarg;
2925 break;
2926 case 'c':
2927 if (action) {
2928 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
2929 return 0;
2931 action = SNAPSHOT_CREATE;
2932 snapshot_name = optarg;
2933 break;
2934 case 'd':
2935 if (action) {
2936 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
2937 return 0;
2939 action = SNAPSHOT_DELETE;
2940 snapshot_name = optarg;
2941 break;
2942 case 'q':
2943 quiet = true;
2944 break;
2945 case 'U':
2946 force_share = true;
2947 break;
2948 case OPTION_OBJECT: {
2949 QemuOpts *opts;
2950 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2951 optarg, true);
2952 if (!opts) {
2953 return 1;
2955 } break;
2956 case OPTION_IMAGE_OPTS:
2957 image_opts = true;
2958 break;
2962 if (optind != argc - 1) {
2963 error_exit("Expecting one image file name");
2965 filename = argv[optind++];
2967 if (qemu_opts_foreach(&qemu_object_opts,
2968 user_creatable_add_opts_foreach,
2969 NULL, NULL)) {
2970 return 1;
2973 /* Open the image */
2974 blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet,
2975 force_share);
2976 if (!blk) {
2977 return 1;
2979 bs = blk_bs(blk);
2981 /* Perform the requested action */
2982 switch(action) {
2983 case SNAPSHOT_LIST:
2984 dump_snapshots(bs);
2985 break;
2987 case SNAPSHOT_CREATE:
2988 memset(&sn, 0, sizeof(sn));
2989 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
2991 qemu_gettimeofday(&tv);
2992 sn.date_sec = tv.tv_sec;
2993 sn.date_nsec = tv.tv_usec * 1000;
2995 ret = bdrv_snapshot_create(bs, &sn);
2996 if (ret) {
2997 error_report("Could not create snapshot '%s': %d (%s)",
2998 snapshot_name, ret, strerror(-ret));
3000 break;
3002 case SNAPSHOT_APPLY:
3003 ret = bdrv_snapshot_goto(bs, snapshot_name);
3004 if (ret) {
3005 error_report("Could not apply snapshot '%s': %d (%s)",
3006 snapshot_name, ret, strerror(-ret));
3008 break;
3010 case SNAPSHOT_DELETE:
3011 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
3012 if (err) {
3013 error_reportf_err(err, "Could not delete snapshot '%s': ",
3014 snapshot_name);
3015 ret = 1;
3017 break;
3020 /* Cleanup */
3021 blk_unref(blk);
3022 if (ret) {
3023 return 1;
3025 return 0;
3028 static int img_rebase(int argc, char **argv)
3030 BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
3031 uint8_t *buf_old = NULL;
3032 uint8_t *buf_new = NULL;
3033 BlockDriverState *bs = NULL;
3034 char *filename;
3035 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
3036 int c, flags, src_flags, ret;
3037 bool writethrough, src_writethrough;
3038 int unsafe = 0;
3039 bool force_share = false;
3040 int progress = 0;
3041 bool quiet = false;
3042 Error *local_err = NULL;
3043 bool image_opts = false;
3045 /* Parse commandline parameters */
3046 fmt = NULL;
3047 cache = BDRV_DEFAULT_CACHE;
3048 src_cache = BDRV_DEFAULT_CACHE;
3049 out_baseimg = NULL;
3050 out_basefmt = NULL;
3051 for(;;) {
3052 static const struct option long_options[] = {
3053 {"help", no_argument, 0, 'h'},
3054 {"object", required_argument, 0, OPTION_OBJECT},
3055 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
3056 {"force-share", no_argument, 0, 'U'},
3057 {0, 0, 0, 0}
3059 c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU",
3060 long_options, NULL);
3061 if (c == -1) {
3062 break;
3064 switch(c) {
3065 case ':':
3066 missing_argument(argv[optind - 1]);
3067 break;
3068 case '?':
3069 unrecognized_option(argv[optind - 1]);
3070 break;
3071 case 'h':
3072 help();
3073 return 0;
3074 case 'f':
3075 fmt = optarg;
3076 break;
3077 case 'F':
3078 out_basefmt = optarg;
3079 break;
3080 case 'b':
3081 out_baseimg = optarg;
3082 break;
3083 case 'u':
3084 unsafe = 1;
3085 break;
3086 case 'p':
3087 progress = 1;
3088 break;
3089 case 't':
3090 cache = optarg;
3091 break;
3092 case 'T':
3093 src_cache = optarg;
3094 break;
3095 case 'q':
3096 quiet = true;
3097 break;
3098 case OPTION_OBJECT: {
3099 QemuOpts *opts;
3100 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3101 optarg, true);
3102 if (!opts) {
3103 return 1;
3105 } break;
3106 case OPTION_IMAGE_OPTS:
3107 image_opts = true;
3108 break;
3109 case 'U':
3110 force_share = true;
3111 break;
3115 if (quiet) {
3116 progress = 0;
3119 if (optind != argc - 1) {
3120 error_exit("Expecting one image file name");
3122 if (!unsafe && !out_baseimg) {
3123 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
3125 filename = argv[optind++];
3127 if (qemu_opts_foreach(&qemu_object_opts,
3128 user_creatable_add_opts_foreach,
3129 NULL, NULL)) {
3130 return 1;
3133 qemu_progress_init(progress, 2.0);
3134 qemu_progress_print(0, 100);
3136 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
3137 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
3138 if (ret < 0) {
3139 error_report("Invalid cache option: %s", cache);
3140 goto out;
3143 src_flags = 0;
3144 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
3145 if (ret < 0) {
3146 error_report("Invalid source cache option: %s", src_cache);
3147 goto out;
3150 /* The source files are opened read-only, don't care about WCE */
3151 assert((src_flags & BDRV_O_RDWR) == 0);
3152 (void) src_writethrough;
3155 * Open the images.
3157 * Ignore the old backing file for unsafe rebase in case we want to correct
3158 * the reference to a renamed or moved backing file.
3160 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3161 false);
3162 if (!blk) {
3163 ret = -1;
3164 goto out;
3166 bs = blk_bs(blk);
3168 if (out_basefmt != NULL) {
3169 if (bdrv_find_format(out_basefmt) == NULL) {
3170 error_report("Invalid format name: '%s'", out_basefmt);
3171 ret = -1;
3172 goto out;
3176 /* For safe rebasing we need to compare old and new backing file */
3177 if (!unsafe) {
3178 char backing_name[PATH_MAX];
3179 QDict *options = NULL;
3181 if (bs->backing_format[0] != '\0') {
3182 options = qdict_new();
3183 qdict_put_str(options, "driver", bs->backing_format);
3186 if (force_share) {
3187 if (!options) {
3188 options = qdict_new();
3190 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
3192 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
3193 blk_old_backing = blk_new_open(backing_name, NULL,
3194 options, src_flags, &local_err);
3195 if (!blk_old_backing) {
3196 error_reportf_err(local_err,
3197 "Could not open old backing file '%s': ",
3198 backing_name);
3199 ret = -1;
3200 goto out;
3203 if (out_baseimg[0]) {
3204 options = qdict_new();
3205 if (out_basefmt) {
3206 qdict_put_str(options, "driver", out_basefmt);
3208 if (force_share) {
3209 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
3212 blk_new_backing = blk_new_open(out_baseimg, NULL,
3213 options, src_flags, &local_err);
3214 if (!blk_new_backing) {
3215 error_reportf_err(local_err,
3216 "Could not open new backing file '%s': ",
3217 out_baseimg);
3218 ret = -1;
3219 goto out;
3225 * Check each unallocated cluster in the COW file. If it is unallocated,
3226 * accesses go to the backing file. We must therefore compare this cluster
3227 * in the old and new backing file, and if they differ we need to copy it
3228 * from the old backing file into the COW file.
3230 * If qemu-img crashes during this step, no harm is done. The content of
3231 * the image is the same as the original one at any time.
3233 if (!unsafe) {
3234 int64_t num_sectors;
3235 int64_t old_backing_num_sectors;
3236 int64_t new_backing_num_sectors = 0;
3237 uint64_t sector;
3238 int n;
3239 int64_t count;
3240 float local_progress = 0;
3242 buf_old = blk_blockalign(blk, IO_BUF_SIZE);
3243 buf_new = blk_blockalign(blk, IO_BUF_SIZE);
3245 num_sectors = blk_nb_sectors(blk);
3246 if (num_sectors < 0) {
3247 error_report("Could not get size of '%s': %s",
3248 filename, strerror(-num_sectors));
3249 ret = -1;
3250 goto out;
3252 old_backing_num_sectors = blk_nb_sectors(blk_old_backing);
3253 if (old_backing_num_sectors < 0) {
3254 char backing_name[PATH_MAX];
3256 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
3257 error_report("Could not get size of '%s': %s",
3258 backing_name, strerror(-old_backing_num_sectors));
3259 ret = -1;
3260 goto out;
3262 if (blk_new_backing) {
3263 new_backing_num_sectors = blk_nb_sectors(blk_new_backing);
3264 if (new_backing_num_sectors < 0) {
3265 error_report("Could not get size of '%s': %s",
3266 out_baseimg, strerror(-new_backing_num_sectors));
3267 ret = -1;
3268 goto out;
3272 if (num_sectors != 0) {
3273 local_progress = (float)100 /
3274 (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
3277 for (sector = 0; sector < num_sectors; sector += n) {
3279 /* How many sectors can we handle with the next read? */
3280 if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
3281 n = (IO_BUF_SIZE / 512);
3282 } else {
3283 n = num_sectors - sector;
3286 /* If the cluster is allocated, we don't need to take action */
3287 ret = bdrv_is_allocated(bs, sector << BDRV_SECTOR_BITS,
3288 n << BDRV_SECTOR_BITS, &count);
3289 if (ret < 0) {
3290 error_report("error while reading image metadata: %s",
3291 strerror(-ret));
3292 goto out;
3294 /* TODO relax this once bdrv_is_allocated does not enforce
3295 * sector alignment */
3296 assert(QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE));
3297 n = count >> BDRV_SECTOR_BITS;
3298 if (ret) {
3299 continue;
3303 * Read old and new backing file and take into consideration that
3304 * backing files may be smaller than the COW image.
3306 if (sector >= old_backing_num_sectors) {
3307 memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
3308 } else {
3309 if (sector + n > old_backing_num_sectors) {
3310 n = old_backing_num_sectors - sector;
3313 ret = blk_pread(blk_old_backing, sector << BDRV_SECTOR_BITS,
3314 buf_old, n << BDRV_SECTOR_BITS);
3315 if (ret < 0) {
3316 error_report("error while reading from old backing file");
3317 goto out;
3321 if (sector >= new_backing_num_sectors || !blk_new_backing) {
3322 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
3323 } else {
3324 if (sector + n > new_backing_num_sectors) {
3325 n = new_backing_num_sectors - sector;
3328 ret = blk_pread(blk_new_backing, sector << BDRV_SECTOR_BITS,
3329 buf_new, n << BDRV_SECTOR_BITS);
3330 if (ret < 0) {
3331 error_report("error while reading from new backing file");
3332 goto out;
3336 /* If they differ, we need to write to the COW file */
3337 uint64_t written = 0;
3339 while (written < n) {
3340 int pnum;
3342 if (compare_sectors(buf_old + written * 512,
3343 buf_new + written * 512, n - written, &pnum))
3345 ret = blk_pwrite(blk,
3346 (sector + written) << BDRV_SECTOR_BITS,
3347 buf_old + written * 512,
3348 pnum << BDRV_SECTOR_BITS, 0);
3349 if (ret < 0) {
3350 error_report("Error while writing to COW image: %s",
3351 strerror(-ret));
3352 goto out;
3356 written += pnum;
3358 qemu_progress_print(local_progress, 100);
3363 * Change the backing file. All clusters that are different from the old
3364 * backing file are overwritten in the COW file now, so the visible content
3365 * doesn't change when we switch the backing file.
3367 if (out_baseimg && *out_baseimg) {
3368 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3369 } else {
3370 ret = bdrv_change_backing_file(bs, NULL, NULL);
3373 if (ret == -ENOSPC) {
3374 error_report("Could not change the backing file to '%s': No "
3375 "space left in the file header", out_baseimg);
3376 } else if (ret < 0) {
3377 error_report("Could not change the backing file to '%s': %s",
3378 out_baseimg, strerror(-ret));
3381 qemu_progress_print(100, 0);
3383 * TODO At this point it is possible to check if any clusters that are
3384 * allocated in the COW file are the same in the backing file. If so, they
3385 * could be dropped from the COW file. Don't do this before switching the
3386 * backing file, in case of a crash this would lead to corruption.
3388 out:
3389 qemu_progress_end();
3390 /* Cleanup */
3391 if (!unsafe) {
3392 blk_unref(blk_old_backing);
3393 blk_unref(blk_new_backing);
3395 qemu_vfree(buf_old);
3396 qemu_vfree(buf_new);
3398 blk_unref(blk);
3399 if (ret) {
3400 return 1;
3402 return 0;
3405 static int img_resize(int argc, char **argv)
3407 Error *err = NULL;
3408 int c, ret, relative;
3409 const char *filename, *fmt, *size;
3410 int64_t n, total_size;
3411 bool quiet = false;
3412 BlockBackend *blk = NULL;
3413 QemuOpts *param;
3415 static QemuOptsList resize_options = {
3416 .name = "resize_options",
3417 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3418 .desc = {
3420 .name = BLOCK_OPT_SIZE,
3421 .type = QEMU_OPT_SIZE,
3422 .help = "Virtual disk size"
3423 }, {
3424 /* end of list */
3428 bool image_opts = false;
3430 /* Remove size from argv manually so that negative numbers are not treated
3431 * as options by getopt. */
3432 if (argc < 3) {
3433 error_exit("Not enough arguments");
3434 return 1;
3437 size = argv[--argc];
3439 /* Parse getopt arguments */
3440 fmt = NULL;
3441 for(;;) {
3442 static const struct option long_options[] = {
3443 {"help", no_argument, 0, 'h'},
3444 {"object", required_argument, 0, OPTION_OBJECT},
3445 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
3446 {0, 0, 0, 0}
3448 c = getopt_long(argc, argv, ":f:hq",
3449 long_options, NULL);
3450 if (c == -1) {
3451 break;
3453 switch(c) {
3454 case ':':
3455 missing_argument(argv[optind - 1]);
3456 break;
3457 case '?':
3458 unrecognized_option(argv[optind - 1]);
3459 break;
3460 case 'h':
3461 help();
3462 break;
3463 case 'f':
3464 fmt = optarg;
3465 break;
3466 case 'q':
3467 quiet = true;
3468 break;
3469 case OPTION_OBJECT: {
3470 QemuOpts *opts;
3471 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3472 optarg, true);
3473 if (!opts) {
3474 return 1;
3476 } break;
3477 case OPTION_IMAGE_OPTS:
3478 image_opts = true;
3479 break;
3482 if (optind != argc - 1) {
3483 error_exit("Expecting one image file name");
3485 filename = argv[optind++];
3487 if (qemu_opts_foreach(&qemu_object_opts,
3488 user_creatable_add_opts_foreach,
3489 NULL, NULL)) {
3490 return 1;
3493 /* Choose grow, shrink, or absolute resize mode */
3494 switch (size[0]) {
3495 case '+':
3496 relative = 1;
3497 size++;
3498 break;
3499 case '-':
3500 relative = -1;
3501 size++;
3502 break;
3503 default:
3504 relative = 0;
3505 break;
3508 /* Parse size */
3509 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
3510 qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
3511 if (err) {
3512 error_report_err(err);
3513 ret = -1;
3514 qemu_opts_del(param);
3515 goto out;
3517 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3518 qemu_opts_del(param);
3520 blk = img_open(image_opts, filename, fmt,
3521 BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet,
3522 false);
3523 if (!blk) {
3524 ret = -1;
3525 goto out;
3528 if (relative) {
3529 total_size = blk_getlength(blk) + n * relative;
3530 } else {
3531 total_size = n;
3533 if (total_size <= 0) {
3534 error_report("New image size must be positive");
3535 ret = -1;
3536 goto out;
3539 ret = blk_truncate(blk, total_size, &err);
3540 if (!ret) {
3541 qprintf(quiet, "Image resized.\n");
3542 } else {
3543 error_report_err(err);
3545 out:
3546 blk_unref(blk);
3547 if (ret) {
3548 return 1;
3550 return 0;
3553 static void amend_status_cb(BlockDriverState *bs,
3554 int64_t offset, int64_t total_work_size,
3555 void *opaque)
3557 qemu_progress_print(100.f * offset / total_work_size, 0);
3560 static int img_amend(int argc, char **argv)
3562 Error *err = NULL;
3563 int c, ret = 0;
3564 char *options = NULL;
3565 QemuOptsList *create_opts = NULL;
3566 QemuOpts *opts = NULL;
3567 const char *fmt = NULL, *filename, *cache;
3568 int flags;
3569 bool writethrough;
3570 bool quiet = false, progress = false;
3571 BlockBackend *blk = NULL;
3572 BlockDriverState *bs = NULL;
3573 bool image_opts = false;
3575 cache = BDRV_DEFAULT_CACHE;
3576 for (;;) {
3577 static const struct option long_options[] = {
3578 {"help", no_argument, 0, 'h'},
3579 {"object", required_argument, 0, OPTION_OBJECT},
3580 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
3581 {0, 0, 0, 0}
3583 c = getopt_long(argc, argv, ":ho:f:t:pq",
3584 long_options, NULL);
3585 if (c == -1) {
3586 break;
3589 switch (c) {
3590 case ':':
3591 missing_argument(argv[optind - 1]);
3592 break;
3593 case '?':
3594 unrecognized_option(argv[optind - 1]);
3595 break;
3596 case 'h':
3597 help();
3598 break;
3599 case 'o':
3600 if (!is_valid_option_list(optarg)) {
3601 error_report("Invalid option list: %s", optarg);
3602 ret = -1;
3603 goto out_no_progress;
3605 if (!options) {
3606 options = g_strdup(optarg);
3607 } else {
3608 char *old_options = options;
3609 options = g_strdup_printf("%s,%s", options, optarg);
3610 g_free(old_options);
3612 break;
3613 case 'f':
3614 fmt = optarg;
3615 break;
3616 case 't':
3617 cache = optarg;
3618 break;
3619 case 'p':
3620 progress = true;
3621 break;
3622 case 'q':
3623 quiet = true;
3624 break;
3625 case OPTION_OBJECT:
3626 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3627 optarg, true);
3628 if (!opts) {
3629 ret = -1;
3630 goto out_no_progress;
3632 break;
3633 case OPTION_IMAGE_OPTS:
3634 image_opts = true;
3635 break;
3639 if (!options) {
3640 error_exit("Must specify options (-o)");
3643 if (qemu_opts_foreach(&qemu_object_opts,
3644 user_creatable_add_opts_foreach,
3645 NULL, NULL)) {
3646 ret = -1;
3647 goto out_no_progress;
3650 if (quiet) {
3651 progress = false;
3653 qemu_progress_init(progress, 1.0);
3655 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3656 if (fmt && has_help_option(options)) {
3657 /* If a format is explicitly specified (and possibly no filename is
3658 * given), print option help here */
3659 ret = print_block_option_help(filename, fmt);
3660 goto out;
3663 if (optind != argc - 1) {
3664 error_report("Expecting one image file name");
3665 ret = -1;
3666 goto out;
3669 flags = BDRV_O_RDWR;
3670 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
3671 if (ret < 0) {
3672 error_report("Invalid cache option: %s", cache);
3673 goto out;
3676 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3677 false);
3678 if (!blk) {
3679 ret = -1;
3680 goto out;
3682 bs = blk_bs(blk);
3684 fmt = bs->drv->format_name;
3686 if (has_help_option(options)) {
3687 /* If the format was auto-detected, print option help here */
3688 ret = print_block_option_help(filename, fmt);
3689 goto out;
3692 if (!bs->drv->create_opts) {
3693 error_report("Format driver '%s' does not support any options to amend",
3694 fmt);
3695 ret = -1;
3696 goto out;
3699 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
3700 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
3701 qemu_opts_do_parse(opts, options, NULL, &err);
3702 if (err) {
3703 error_report_err(err);
3704 ret = -1;
3705 goto out;
3708 /* In case the driver does not call amend_status_cb() */
3709 qemu_progress_print(0.f, 0);
3710 ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL);
3711 qemu_progress_print(100.f, 0);
3712 if (ret < 0) {
3713 error_report("Error while amending options: %s", strerror(-ret));
3714 goto out;
3717 out:
3718 qemu_progress_end();
3720 out_no_progress:
3721 blk_unref(blk);
3722 qemu_opts_del(opts);
3723 qemu_opts_free(create_opts);
3724 g_free(options);
3726 if (ret) {
3727 return 1;
3729 return 0;
3732 typedef struct BenchData {
3733 BlockBackend *blk;
3734 uint64_t image_size;
3735 bool write;
3736 int bufsize;
3737 int step;
3738 int nrreq;
3739 int n;
3740 int flush_interval;
3741 bool drain_on_flush;
3742 uint8_t *buf;
3743 QEMUIOVector *qiov;
3745 int in_flight;
3746 bool in_flush;
3747 uint64_t offset;
3748 } BenchData;
3750 static void bench_undrained_flush_cb(void *opaque, int ret)
3752 if (ret < 0) {
3753 error_report("Failed flush request: %s", strerror(-ret));
3754 exit(EXIT_FAILURE);
3758 static void bench_cb(void *opaque, int ret)
3760 BenchData *b = opaque;
3761 BlockAIOCB *acb;
3763 if (ret < 0) {
3764 error_report("Failed request: %s", strerror(-ret));
3765 exit(EXIT_FAILURE);
3768 if (b->in_flush) {
3769 /* Just finished a flush with drained queue: Start next requests */
3770 assert(b->in_flight == 0);
3771 b->in_flush = false;
3772 } else if (b->in_flight > 0) {
3773 int remaining = b->n - b->in_flight;
3775 b->n--;
3776 b->in_flight--;
3778 /* Time for flush? Drain queue if requested, then flush */
3779 if (b->flush_interval && remaining % b->flush_interval == 0) {
3780 if (!b->in_flight || !b->drain_on_flush) {
3781 BlockCompletionFunc *cb;
3783 if (b->drain_on_flush) {
3784 b->in_flush = true;
3785 cb = bench_cb;
3786 } else {
3787 cb = bench_undrained_flush_cb;
3790 acb = blk_aio_flush(b->blk, cb, b);
3791 if (!acb) {
3792 error_report("Failed to issue flush request");
3793 exit(EXIT_FAILURE);
3796 if (b->drain_on_flush) {
3797 return;
3802 while (b->n > b->in_flight && b->in_flight < b->nrreq) {
3803 int64_t offset = b->offset;
3804 /* blk_aio_* might look for completed I/Os and kick bench_cb
3805 * again, so make sure this operation is counted by in_flight
3806 * and b->offset is ready for the next submission.
3808 b->in_flight++;
3809 b->offset += b->step;
3810 b->offset %= b->image_size;
3811 if (b->write) {
3812 acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b);
3813 } else {
3814 acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b);
3816 if (!acb) {
3817 error_report("Failed to issue request");
3818 exit(EXIT_FAILURE);
3823 static int img_bench(int argc, char **argv)
3825 int c, ret = 0;
3826 const char *fmt = NULL, *filename;
3827 bool quiet = false;
3828 bool image_opts = false;
3829 bool is_write = false;
3830 int count = 75000;
3831 int depth = 64;
3832 int64_t offset = 0;
3833 size_t bufsize = 4096;
3834 int pattern = 0;
3835 size_t step = 0;
3836 int flush_interval = 0;
3837 bool drain_on_flush = true;
3838 int64_t image_size;
3839 BlockBackend *blk = NULL;
3840 BenchData data = {};
3841 int flags = 0;
3842 bool writethrough = false;
3843 struct timeval t1, t2;
3844 int i;
3845 bool force_share = false;
3847 for (;;) {
3848 static const struct option long_options[] = {
3849 {"help", no_argument, 0, 'h'},
3850 {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL},
3851 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
3852 {"pattern", required_argument, 0, OPTION_PATTERN},
3853 {"no-drain", no_argument, 0, OPTION_NO_DRAIN},
3854 {"force-share", no_argument, 0, 'U'},
3855 {0, 0, 0, 0}
3857 c = getopt_long(argc, argv, ":hc:d:f:no:qs:S:t:wU", long_options, NULL);
3858 if (c == -1) {
3859 break;
3862 switch (c) {
3863 case ':':
3864 missing_argument(argv[optind - 1]);
3865 break;
3866 case '?':
3867 unrecognized_option(argv[optind - 1]);
3868 break;
3869 case 'h':
3870 help();
3871 break;
3872 case 'c':
3874 unsigned long res;
3876 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
3877 error_report("Invalid request count specified");
3878 return 1;
3880 count = res;
3881 break;
3883 case 'd':
3885 unsigned long res;
3887 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
3888 error_report("Invalid queue depth specified");
3889 return 1;
3891 depth = res;
3892 break;
3894 case 'f':
3895 fmt = optarg;
3896 break;
3897 case 'n':
3898 flags |= BDRV_O_NATIVE_AIO;
3899 break;
3900 case 'o':
3902 offset = cvtnum(optarg);
3903 if (offset < 0) {
3904 error_report("Invalid offset specified");
3905 return 1;
3907 break;
3909 break;
3910 case 'q':
3911 quiet = true;
3912 break;
3913 case 's':
3915 int64_t sval;
3917 sval = cvtnum(optarg);
3918 if (sval < 0 || sval > INT_MAX) {
3919 error_report("Invalid buffer size specified");
3920 return 1;
3923 bufsize = sval;
3924 break;
3926 case 'S':
3928 int64_t sval;
3930 sval = cvtnum(optarg);
3931 if (sval < 0 || sval > INT_MAX) {
3932 error_report("Invalid step size specified");
3933 return 1;
3936 step = sval;
3937 break;
3939 case 't':
3940 ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
3941 if (ret < 0) {
3942 error_report("Invalid cache mode");
3943 ret = -1;
3944 goto out;
3946 break;
3947 case 'w':
3948 flags |= BDRV_O_RDWR;
3949 is_write = true;
3950 break;
3951 case 'U':
3952 force_share = true;
3953 break;
3954 case OPTION_PATTERN:
3956 unsigned long res;
3958 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
3959 error_report("Invalid pattern byte specified");
3960 return 1;
3962 pattern = res;
3963 break;
3965 case OPTION_FLUSH_INTERVAL:
3967 unsigned long res;
3969 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
3970 error_report("Invalid flush interval specified");
3971 return 1;
3973 flush_interval = res;
3974 break;
3976 case OPTION_NO_DRAIN:
3977 drain_on_flush = false;
3978 break;
3979 case OPTION_IMAGE_OPTS:
3980 image_opts = true;
3981 break;
3985 if (optind != argc - 1) {
3986 error_exit("Expecting one image file name");
3988 filename = argv[argc - 1];
3990 if (!is_write && flush_interval) {
3991 error_report("--flush-interval is only available in write tests");
3992 ret = -1;
3993 goto out;
3995 if (flush_interval && flush_interval < depth) {
3996 error_report("Flush interval can't be smaller than depth");
3997 ret = -1;
3998 goto out;
4001 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
4002 force_share);
4003 if (!blk) {
4004 ret = -1;
4005 goto out;
4008 image_size = blk_getlength(blk);
4009 if (image_size < 0) {
4010 ret = image_size;
4011 goto out;
4014 data = (BenchData) {
4015 .blk = blk,
4016 .image_size = image_size,
4017 .bufsize = bufsize,
4018 .step = step ?: bufsize,
4019 .nrreq = depth,
4020 .n = count,
4021 .offset = offset,
4022 .write = is_write,
4023 .flush_interval = flush_interval,
4024 .drain_on_flush = drain_on_flush,
4026 printf("Sending %d %s requests, %d bytes each, %d in parallel "
4027 "(starting at offset %" PRId64 ", step size %d)\n",
4028 data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
4029 data.offset, data.step);
4030 if (flush_interval) {
4031 printf("Sending flush every %d requests\n", flush_interval);
4034 data.buf = blk_blockalign(blk, data.nrreq * data.bufsize);
4035 memset(data.buf, pattern, data.nrreq * data.bufsize);
4037 data.qiov = g_new(QEMUIOVector, data.nrreq);
4038 for (i = 0; i < data.nrreq; i++) {
4039 qemu_iovec_init(&data.qiov[i], 1);
4040 qemu_iovec_add(&data.qiov[i],
4041 data.buf + i * data.bufsize, data.bufsize);
4044 gettimeofday(&t1, NULL);
4045 bench_cb(&data, 0);
4047 while (data.n > 0) {
4048 main_loop_wait(false);
4050 gettimeofday(&t2, NULL);
4052 printf("Run completed in %3.3f seconds.\n",
4053 (t2.tv_sec - t1.tv_sec)
4054 + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
4056 out:
4057 qemu_vfree(data.buf);
4058 blk_unref(blk);
4060 if (ret) {
4061 return 1;
4063 return 0;
4066 #define C_BS 01
4067 #define C_COUNT 02
4068 #define C_IF 04
4069 #define C_OF 010
4070 #define C_SKIP 020
4072 struct DdInfo {
4073 unsigned int flags;
4074 int64_t count;
4077 struct DdIo {
4078 int bsz; /* Block size */
4079 char *filename;
4080 uint8_t *buf;
4081 int64_t offset;
4084 struct DdOpts {
4085 const char *name;
4086 int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *);
4087 unsigned int flag;
4090 static int img_dd_bs(const char *arg,
4091 struct DdIo *in, struct DdIo *out,
4092 struct DdInfo *dd)
4094 int64_t res;
4096 res = cvtnum(arg);
4098 if (res <= 0 || res > INT_MAX) {
4099 error_report("invalid number: '%s'", arg);
4100 return 1;
4102 in->bsz = out->bsz = res;
4104 return 0;
4107 static int img_dd_count(const char *arg,
4108 struct DdIo *in, struct DdIo *out,
4109 struct DdInfo *dd)
4111 dd->count = cvtnum(arg);
4113 if (dd->count < 0) {
4114 error_report("invalid number: '%s'", arg);
4115 return 1;
4118 return 0;
4121 static int img_dd_if(const char *arg,
4122 struct DdIo *in, struct DdIo *out,
4123 struct DdInfo *dd)
4125 in->filename = g_strdup(arg);
4127 return 0;
4130 static int img_dd_of(const char *arg,
4131 struct DdIo *in, struct DdIo *out,
4132 struct DdInfo *dd)
4134 out->filename = g_strdup(arg);
4136 return 0;
4139 static int img_dd_skip(const char *arg,
4140 struct DdIo *in, struct DdIo *out,
4141 struct DdInfo *dd)
4143 in->offset = cvtnum(arg);
4145 if (in->offset < 0) {
4146 error_report("invalid number: '%s'", arg);
4147 return 1;
4150 return 0;
4153 static int img_dd(int argc, char **argv)
4155 int ret = 0;
4156 char *arg = NULL;
4157 char *tmp;
4158 BlockDriver *drv = NULL, *proto_drv = NULL;
4159 BlockBackend *blk1 = NULL, *blk2 = NULL;
4160 QemuOpts *opts = NULL;
4161 QemuOptsList *create_opts = NULL;
4162 Error *local_err = NULL;
4163 bool image_opts = false;
4164 int c, i;
4165 const char *out_fmt = "raw";
4166 const char *fmt = NULL;
4167 int64_t size = 0;
4168 int64_t block_count = 0, out_pos, in_pos;
4169 bool force_share = false;
4170 struct DdInfo dd = {
4171 .flags = 0,
4172 .count = 0,
4174 struct DdIo in = {
4175 .bsz = 512, /* Block size is by default 512 bytes */
4176 .filename = NULL,
4177 .buf = NULL,
4178 .offset = 0
4180 struct DdIo out = {
4181 .bsz = 512,
4182 .filename = NULL,
4183 .buf = NULL,
4184 .offset = 0
4187 const struct DdOpts options[] = {
4188 { "bs", img_dd_bs, C_BS },
4189 { "count", img_dd_count, C_COUNT },
4190 { "if", img_dd_if, C_IF },
4191 { "of", img_dd_of, C_OF },
4192 { "skip", img_dd_skip, C_SKIP },
4193 { NULL, NULL, 0 }
4195 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-share", no_argument, 0, 'U'},
4200 { 0, 0, 0, 0 }
4203 while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) {
4204 if (c == EOF) {
4205 break;
4207 switch (c) {
4208 case 'O':
4209 out_fmt = optarg;
4210 break;
4211 case 'f':
4212 fmt = optarg;
4213 break;
4214 case ':':
4215 missing_argument(argv[optind - 1]);
4216 break;
4217 case '?':
4218 unrecognized_option(argv[optind - 1]);
4219 break;
4220 case 'h':
4221 help();
4222 break;
4223 case 'U':
4224 force_share = true;
4225 break;
4226 case OPTION_OBJECT:
4227 if (!qemu_opts_parse_noisily(&qemu_object_opts, optarg, true)) {
4228 ret = -1;
4229 goto out;
4231 break;
4232 case OPTION_IMAGE_OPTS:
4233 image_opts = true;
4234 break;
4238 for (i = optind; i < argc; i++) {
4239 int j;
4240 arg = g_strdup(argv[i]);
4242 tmp = strchr(arg, '=');
4243 if (tmp == NULL) {
4244 error_report("unrecognized operand %s", arg);
4245 ret = -1;
4246 goto out;
4249 *tmp++ = '\0';
4251 for (j = 0; options[j].name != NULL; j++) {
4252 if (!strcmp(arg, options[j].name)) {
4253 break;
4256 if (options[j].name == NULL) {
4257 error_report("unrecognized operand %s", arg);
4258 ret = -1;
4259 goto out;
4262 if (options[j].f(tmp, &in, &out, &dd) != 0) {
4263 ret = -1;
4264 goto out;
4266 dd.flags |= options[j].flag;
4267 g_free(arg);
4268 arg = NULL;
4271 if (!(dd.flags & C_IF && dd.flags & C_OF)) {
4272 error_report("Must specify both input and output files");
4273 ret = -1;
4274 goto out;
4277 if (qemu_opts_foreach(&qemu_object_opts,
4278 user_creatable_add_opts_foreach,
4279 NULL, NULL)) {
4280 ret = -1;
4281 goto out;
4284 blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
4285 force_share);
4287 if (!blk1) {
4288 ret = -1;
4289 goto out;
4292 drv = bdrv_find_format(out_fmt);
4293 if (!drv) {
4294 error_report("Unknown file format");
4295 ret = -1;
4296 goto out;
4298 proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
4300 if (!proto_drv) {
4301 error_report_err(local_err);
4302 ret = -1;
4303 goto out;
4305 if (!drv->create_opts) {
4306 error_report("Format driver '%s' does not support image creation",
4307 drv->format_name);
4308 ret = -1;
4309 goto out;
4311 if (!proto_drv->create_opts) {
4312 error_report("Protocol driver '%s' does not support image creation",
4313 proto_drv->format_name);
4314 ret = -1;
4315 goto out;
4317 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4318 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
4320 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4322 size = blk_getlength(blk1);
4323 if (size < 0) {
4324 error_report("Failed to get size for '%s'", in.filename);
4325 ret = -1;
4326 goto out;
4329 if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
4330 dd.count * in.bsz < size) {
4331 size = dd.count * in.bsz;
4334 /* Overflow means the specified offset is beyond input image's size */
4335 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4336 size < in.bsz * in.offset)) {
4337 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
4338 } else {
4339 qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
4340 size - in.bsz * in.offset, &error_abort);
4343 ret = bdrv_create(drv, out.filename, opts, &local_err);
4344 if (ret < 0) {
4345 error_reportf_err(local_err,
4346 "%s: error while creating output image: ",
4347 out.filename);
4348 ret = -1;
4349 goto out;
4352 /* TODO, we can't honour --image-opts for the target,
4353 * since it needs to be given in a format compatible
4354 * with the bdrv_create() call above which does not
4355 * support image-opts style.
4357 blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
4358 false, false, false);
4360 if (!blk2) {
4361 ret = -1;
4362 goto out;
4365 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4366 size < in.offset * in.bsz)) {
4367 /* We give a warning if the skip option is bigger than the input
4368 * size and create an empty output disk image (i.e. like dd(1)).
4370 error_report("%s: cannot skip to specified offset", in.filename);
4371 in_pos = size;
4372 } else {
4373 in_pos = in.offset * in.bsz;
4376 in.buf = g_new(uint8_t, in.bsz);
4378 for (out_pos = 0; in_pos < size; block_count++) {
4379 int in_ret, out_ret;
4381 if (in_pos + in.bsz > size) {
4382 in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos);
4383 } else {
4384 in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
4386 if (in_ret < 0) {
4387 error_report("error while reading from input image file: %s",
4388 strerror(-in_ret));
4389 ret = -1;
4390 goto out;
4392 in_pos += in_ret;
4394 out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
4396 if (out_ret < 0) {
4397 error_report("error while writing to output image file: %s",
4398 strerror(-out_ret));
4399 ret = -1;
4400 goto out;
4402 out_pos += out_ret;
4405 out:
4406 g_free(arg);
4407 qemu_opts_del(opts);
4408 qemu_opts_free(create_opts);
4409 blk_unref(blk1);
4410 blk_unref(blk2);
4411 g_free(in.filename);
4412 g_free(out.filename);
4413 g_free(in.buf);
4414 g_free(out.buf);
4416 if (ret) {
4417 return 1;
4419 return 0;
4423 static const img_cmd_t img_cmds[] = {
4424 #define DEF(option, callback, arg_string) \
4425 { option, callback },
4426 #include "qemu-img-cmds.h"
4427 #undef DEF
4428 #undef GEN_DOCS
4429 { NULL, NULL, },
4432 int main(int argc, char **argv)
4434 const img_cmd_t *cmd;
4435 const char *cmdname;
4436 Error *local_error = NULL;
4437 char *trace_file = NULL;
4438 int c;
4439 static const struct option long_options[] = {
4440 {"help", no_argument, 0, 'h'},
4441 {"version", no_argument, 0, 'V'},
4442 {"trace", required_argument, NULL, 'T'},
4443 {0, 0, 0, 0}
4446 #ifdef CONFIG_POSIX
4447 signal(SIGPIPE, SIG_IGN);
4448 #endif
4450 module_call_init(MODULE_INIT_TRACE);
4451 error_set_progname(argv[0]);
4452 qemu_init_exec_dir(argv[0]);
4454 if (qemu_init_main_loop(&local_error)) {
4455 error_report_err(local_error);
4456 exit(EXIT_FAILURE);
4459 qcrypto_init(&error_fatal);
4461 module_call_init(MODULE_INIT_QOM);
4462 bdrv_init();
4463 if (argc < 2) {
4464 error_exit("Not enough arguments");
4467 qemu_add_opts(&qemu_object_opts);
4468 qemu_add_opts(&qemu_source_opts);
4469 qemu_add_opts(&qemu_trace_opts);
4471 while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) {
4472 switch (c) {
4473 case ':':
4474 missing_argument(argv[optind - 1]);
4475 return 0;
4476 case '?':
4477 unrecognized_option(argv[optind - 1]);
4478 return 0;
4479 case 'h':
4480 help();
4481 return 0;
4482 case 'V':
4483 printf(QEMU_IMG_VERSION);
4484 return 0;
4485 case 'T':
4486 g_free(trace_file);
4487 trace_file = trace_opt_parse(optarg);
4488 break;
4492 cmdname = argv[optind];
4494 /* reset getopt_long scanning */
4495 argc -= optind;
4496 if (argc < 1) {
4497 return 0;
4499 argv += optind;
4500 optind = 0;
4502 if (!trace_init_backends()) {
4503 exit(1);
4505 trace_init_file(trace_file);
4506 qemu_set_log(LOG_TRACE);
4508 /* find the command */
4509 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
4510 if (!strcmp(cmdname, cmd->name)) {
4511 return cmd->handler(argc, argv);
4515 /* not found */
4516 error_exit("Command not found: %s", cmdname);