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
25 #include "qemu/osdep.h"
28 #include "qemu-version.h"
29 #include "qapi/error.h"
30 #include "qapi/qapi-visit-block-core.h"
31 #include "qapi/qobject-output-visitor.h"
32 #include "qapi/qmp/qjson.h"
33 #include "qapi/qmp/qdict.h"
34 #include "qapi/qmp/qstring.h"
35 #include "qemu/cutils.h"
36 #include "qemu/config-file.h"
37 #include "qemu/option.h"
38 #include "qemu/error-report.h"
40 #include "qom/object_interfaces.h"
41 #include "sysemu/sysemu.h"
42 #include "sysemu/block-backend.h"
43 #include "block/block_int.h"
44 #include "block/blockjob.h"
45 #include "block/qapi.h"
46 #include "crypto/init.h"
47 #include "trace/control.h"
49 #define QEMU_IMG_VERSION "qemu-img version " QEMU_FULL_VERSION \
50 "\n" QEMU_COPYRIGHT "\n"
52 typedef struct img_cmd_t
{
54 int (*handler
)(int argc
, char **argv
);
59 OPTION_BACKING_CHAIN
= 257,
61 OPTION_IMAGE_OPTS
= 259,
63 OPTION_FLUSH_INTERVAL
= 261,
64 OPTION_NO_DRAIN
= 262,
65 OPTION_TARGET_IMAGE_OPTS
= 263,
67 OPTION_PREALLOCATION
= 265,
71 typedef enum OutputFormat
{
76 /* Default to cache=writeback as data integrity is not important for qemu-img */
77 #define BDRV_DEFAULT_CACHE "writeback"
79 static void format_print(void *opaque
, const char *name
)
84 static void QEMU_NORETURN
GCC_FMT_ATTR(1, 2) error_exit(const char *fmt
, ...)
89 error_vreport(fmt
, ap
);
92 error_printf("Try 'qemu-img --help' for more information\n");
96 static void QEMU_NORETURN
missing_argument(const char *option
)
98 error_exit("missing argument for option '%s'", option
);
101 static void QEMU_NORETURN
unrecognized_option(const char *option
)
103 error_exit("unrecognized option '%s'", option
);
106 /* Please keep in synch with qemu-img.texi */
107 static void QEMU_NORETURN
help(void)
109 const char *help_msg
=
111 "usage: qemu-img [standard options] command [command options]\n"
112 "QEMU disk image utility\n"
114 " '-h', '--help' display this help and exit\n"
115 " '-V', '--version' output version information and exit\n"
116 " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
117 " specify tracing options\n"
120 #define DEF(option, callback, arg_string) \
122 #include "qemu-img-cmds.h"
125 "Command parameters:\n"
126 " 'filename' is a disk image filename\n"
127 " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
128 " manual page for a description of the object properties. The most common\n"
129 " object type is a 'secret', which is used to supply passwords and/or\n"
130 " encryption keys.\n"
131 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
132 " 'cache' is the cache mode used to write the output disk image, the valid\n"
133 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
134 " 'directsync' and 'unsafe' (default for convert)\n"
135 " 'src_cache' is the cache mode used to read input disk images, the valid\n"
136 " options are the same as for the 'cache' option\n"
137 " 'size' is the disk image size in bytes. Optional suffixes\n"
138 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
139 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
140 " supported. 'b' is ignored.\n"
141 " 'output_filename' is the destination disk image filename\n"
142 " 'output_fmt' is the destination format\n"
143 " 'options' is a comma separated list of format specific options in a\n"
144 " name=value format. Use -o ? for an overview of the options supported by the\n"
146 " 'snapshot_param' is param used for internal snapshot, format\n"
147 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
149 " '-c' indicates that target image must be compressed (qcow format only)\n"
150 " '-u' allows unsafe backing chains. For rebasing, it is assumed that old and\n"
151 " new backing file match exactly. The image doesn't need a working\n"
152 " backing file before rebasing in this case (useful for renaming the\n"
153 " backing file). For image creation, allow creating without attempting\n"
154 " to open the backing file.\n"
155 " '-h' with or without a command shows this help and lists the supported formats\n"
156 " '-p' show progress of command (only certain commands)\n"
157 " '-q' use Quiet mode - do not print any output (except errors)\n"
158 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
159 " contain only zeros for qemu-img to create a sparse image during\n"
160 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
161 " unallocated or zero sectors, and the destination image will always be\n"
163 " '--output' takes the format in which the output must be done (human or json)\n"
164 " '-n' skips the target volume creation (useful if the volume is created\n"
165 " prior to running qemu-img)\n"
167 "Parameters to check subcommand:\n"
168 " '-r' tries to repair any inconsistencies that are found during the check.\n"
169 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
170 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
171 " hiding corruption that has already occurred.\n"
173 "Parameters to convert subcommand:\n"
174 " '-m' specifies how many coroutines work in parallel during the convert\n"
175 " process (defaults to 8)\n"
176 " '-W' allow to write to the target out of order rather than sequential\n"
178 "Parameters to snapshot subcommand:\n"
179 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
180 " '-a' applies a snapshot (revert disk to saved state)\n"
181 " '-c' creates a snapshot\n"
182 " '-d' deletes a snapshot\n"
183 " '-l' lists all snapshots in the given image\n"
185 "Parameters to compare subcommand:\n"
186 " '-f' first image format\n"
187 " '-F' second image format\n"
188 " '-s' run in Strict mode - fail on different image size or sector allocation\n"
190 "Parameters to dd subcommand:\n"
191 " 'bs=BYTES' read and write up to BYTES bytes at a time "
193 " 'count=N' copy only N input blocks\n"
194 " 'if=FILE' read from FILE\n"
195 " 'of=FILE' write to FILE\n"
196 " 'skip=N' skip N bs-sized blocks at the start of input\n";
198 printf("%s\nSupported formats:", help_msg
);
199 bdrv_iterate_format(format_print
, NULL
, false);
200 printf("\n\n" QEMU_HELP_BOTTOM
"\n");
204 static QemuOptsList qemu_object_opts
= {
206 .implied_opt_name
= "qom-type",
207 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
213 static QemuOptsList qemu_source_opts
= {
215 .implied_opt_name
= "file",
216 .head
= QTAILQ_HEAD_INITIALIZER(qemu_source_opts
.head
),
222 static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet
, const char *fmt
, ...)
228 ret
= vprintf(fmt
, args
);
235 static int print_block_option_help(const char *filename
, const char *fmt
)
237 BlockDriver
*drv
, *proto_drv
;
238 QemuOptsList
*create_opts
= NULL
;
239 Error
*local_err
= NULL
;
241 /* Find driver and parse its options */
242 drv
= bdrv_find_format(fmt
);
244 error_report("Unknown file format '%s'", fmt
);
248 if (!drv
->create_opts
) {
249 error_report("Format driver '%s' does not support image creation", fmt
);
253 create_opts
= qemu_opts_append(create_opts
, drv
->create_opts
);
255 proto_drv
= bdrv_find_protocol(filename
, true, &local_err
);
257 error_report_err(local_err
);
258 qemu_opts_free(create_opts
);
261 if (!proto_drv
->create_opts
) {
262 error_report("Protocol driver '%s' does not support image creation",
263 proto_drv
->format_name
);
264 qemu_opts_free(create_opts
);
267 create_opts
= qemu_opts_append(create_opts
, proto_drv
->create_opts
);
270 printf("Supported options:\n");
271 qemu_opts_print_help(create_opts
, false);
272 qemu_opts_free(create_opts
);
277 static BlockBackend
*img_open_opts(const char *optstr
,
278 QemuOpts
*opts
, int flags
, bool writethrough
,
279 bool quiet
, bool force_share
)
282 Error
*local_err
= NULL
;
284 options
= qemu_opts_to_qdict(opts
, NULL
);
286 if (qdict_haskey(options
, BDRV_OPT_FORCE_SHARE
)
287 && strcmp(qdict_get_str(options
, BDRV_OPT_FORCE_SHARE
), "on")) {
288 error_report("--force-share/-U conflicts with image options");
289 qobject_unref(options
);
292 qdict_put_str(options
, BDRV_OPT_FORCE_SHARE
, "on");
294 blk
= blk_new_open(NULL
, NULL
, options
, flags
, &local_err
);
296 error_reportf_err(local_err
, "Could not open '%s': ", optstr
);
299 blk_set_enable_write_cache(blk
, !writethrough
);
304 static BlockBackend
*img_open_file(const char *filename
,
306 const char *fmt
, int flags
,
307 bool writethrough
, bool quiet
,
311 Error
*local_err
= NULL
;
314 options
= qdict_new();
317 qdict_put_str(options
, "driver", fmt
);
321 qdict_put_bool(options
, BDRV_OPT_FORCE_SHARE
, true);
323 blk
= blk_new_open(filename
, NULL
, options
, flags
, &local_err
);
325 error_reportf_err(local_err
, "Could not open '%s': ", filename
);
328 blk_set_enable_write_cache(blk
, !writethrough
);
334 static int img_add_key_secrets(void *opaque
,
335 const char *name
, const char *value
,
338 QDict
*options
= opaque
;
340 if (g_str_has_suffix(name
, "key-secret")) {
341 qdict_put_str(options
, name
, value
);
348 static BlockBackend
*img_open(bool image_opts
,
349 const char *filename
,
350 const char *fmt
, int flags
, bool writethrough
,
351 bool quiet
, bool force_share
)
357 error_report("--image-opts and --format are mutually exclusive");
360 opts
= qemu_opts_parse_noisily(qemu_find_opts("source"),
365 blk
= img_open_opts(filename
, opts
, flags
, writethrough
, quiet
,
368 blk
= img_open_file(filename
, NULL
, fmt
, flags
, writethrough
, quiet
,
375 static int add_old_style_options(const char *fmt
, QemuOpts
*opts
,
376 const char *base_filename
,
377 const char *base_fmt
)
382 qemu_opt_set(opts
, BLOCK_OPT_BACKING_FILE
, base_filename
, &err
);
384 error_report("Backing file not supported for file format '%s'",
391 qemu_opt_set(opts
, BLOCK_OPT_BACKING_FMT
, base_fmt
, &err
);
393 error_report("Backing file format not supported for file "
402 static int64_t cvtnum(const char *s
)
407 err
= qemu_strtosz(s
, NULL
, &value
);
411 if (value
> INT64_MAX
) {
417 static int img_create(int argc
, char **argv
)
420 uint64_t img_size
= -1;
421 const char *fmt
= "raw";
422 const char *base_fmt
= NULL
;
423 const char *filename
;
424 const char *base_filename
= NULL
;
425 char *options
= NULL
;
426 Error
*local_err
= NULL
;
431 static const struct option long_options
[] = {
432 {"help", no_argument
, 0, 'h'},
433 {"object", required_argument
, 0, OPTION_OBJECT
},
436 c
= getopt_long(argc
, argv
, ":F:b:f:ho:qu",
443 missing_argument(argv
[optind
- 1]);
446 unrecognized_option(argv
[optind
- 1]);
455 base_filename
= optarg
;
461 if (!is_valid_option_list(optarg
)) {
462 error_report("Invalid option list: %s", optarg
);
466 options
= g_strdup(optarg
);
468 char *old_options
= options
;
469 options
= g_strdup_printf("%s,%s", options
, optarg
);
477 flags
|= BDRV_O_NO_BACKING
;
479 case OPTION_OBJECT
: {
481 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
490 /* Get the filename */
491 filename
= (optind
< argc
) ? argv
[optind
] : NULL
;
492 if (options
&& has_help_option(options
)) {
494 return print_block_option_help(filename
, fmt
);
497 if (optind
>= argc
) {
498 error_exit("Expecting image file name");
502 if (qemu_opts_foreach(&qemu_object_opts
,
503 user_creatable_add_opts_foreach
,
504 NULL
, &error_fatal
)) {
508 /* Get image size, if specified */
512 sval
= cvtnum(argv
[optind
++]);
514 if (sval
== -ERANGE
) {
515 error_report("Image size must be less than 8 EiB!");
517 error_report("Invalid image size specified! You may use k, M, "
518 "G, T, P or E suffixes for ");
519 error_report("kilobytes, megabytes, gigabytes, terabytes, "
520 "petabytes and exabytes.");
524 img_size
= (uint64_t)sval
;
526 if (optind
!= argc
) {
527 error_exit("Unexpected argument: %s", argv
[optind
]);
530 bdrv_img_create(filename
, fmt
, base_filename
, base_fmt
,
531 options
, img_size
, flags
, quiet
, &local_err
);
533 error_reportf_err(local_err
, "%s: ", filename
);
545 static void dump_json_image_check(ImageCheck
*check
, bool quiet
)
549 Visitor
*v
= qobject_output_visitor_new(&obj
);
551 visit_type_ImageCheck(v
, NULL
, &check
, &error_abort
);
552 visit_complete(v
, &obj
);
553 str
= qobject_to_json_pretty(obj
);
555 qprintf(quiet
, "%s\n", qstring_get_str(str
));
561 static void dump_human_image_check(ImageCheck
*check
, bool quiet
)
563 if (!(check
->corruptions
|| check
->leaks
|| check
->check_errors
)) {
564 qprintf(quiet
, "No errors were found on the image.\n");
566 if (check
->corruptions
) {
567 qprintf(quiet
, "\n%" PRId64
" errors were found on the image.\n"
568 "Data may be corrupted, or further writes to the image "
575 "\n%" PRId64
" leaked clusters were found on the image.\n"
576 "This means waste of disk space, but no harm to data.\n",
580 if (check
->check_errors
) {
583 " internal errors have occurred during the check.\n",
584 check
->check_errors
);
588 if (check
->total_clusters
!= 0 && check
->allocated_clusters
!= 0) {
589 qprintf(quiet
, "%" PRId64
"/%" PRId64
" = %0.2f%% allocated, "
590 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
591 check
->allocated_clusters
, check
->total_clusters
,
592 check
->allocated_clusters
* 100.0 / check
->total_clusters
,
593 check
->fragmented_clusters
* 100.0 / check
->allocated_clusters
,
594 check
->compressed_clusters
* 100.0 /
595 check
->allocated_clusters
);
598 if (check
->image_end_offset
) {
600 "Image end offset: %" PRId64
"\n", check
->image_end_offset
);
604 static int collect_image_check(BlockDriverState
*bs
,
606 const char *filename
,
611 BdrvCheckResult result
;
613 ret
= bdrv_check(bs
, &result
, fix
);
618 check
->filename
= g_strdup(filename
);
619 check
->format
= g_strdup(bdrv_get_format_name(bs
));
620 check
->check_errors
= result
.check_errors
;
621 check
->corruptions
= result
.corruptions
;
622 check
->has_corruptions
= result
.corruptions
!= 0;
623 check
->leaks
= result
.leaks
;
624 check
->has_leaks
= result
.leaks
!= 0;
625 check
->corruptions_fixed
= result
.corruptions_fixed
;
626 check
->has_corruptions_fixed
= result
.corruptions
!= 0;
627 check
->leaks_fixed
= result
.leaks_fixed
;
628 check
->has_leaks_fixed
= result
.leaks
!= 0;
629 check
->image_end_offset
= result
.image_end_offset
;
630 check
->has_image_end_offset
= result
.image_end_offset
!= 0;
631 check
->total_clusters
= result
.bfi
.total_clusters
;
632 check
->has_total_clusters
= result
.bfi
.total_clusters
!= 0;
633 check
->allocated_clusters
= result
.bfi
.allocated_clusters
;
634 check
->has_allocated_clusters
= result
.bfi
.allocated_clusters
!= 0;
635 check
->fragmented_clusters
= result
.bfi
.fragmented_clusters
;
636 check
->has_fragmented_clusters
= result
.bfi
.fragmented_clusters
!= 0;
637 check
->compressed_clusters
= result
.bfi
.compressed_clusters
;
638 check
->has_compressed_clusters
= result
.bfi
.compressed_clusters
!= 0;
644 * Checks an image for consistency. Exit codes:
646 * 0 - Check completed, image is good
647 * 1 - Check not completed because of internal errors
648 * 2 - Check completed, image is corrupted
649 * 3 - Check completed, image has leaked clusters, but is good otherwise
650 * 63 - Checks are not supported by the image format
652 static int img_check(int argc
, char **argv
)
655 OutputFormat output_format
= OFORMAT_HUMAN
;
656 const char *filename
, *fmt
, *output
, *cache
;
658 BlockDriverState
*bs
;
660 int flags
= BDRV_O_CHECK
;
664 bool image_opts
= false;
665 bool force_share
= false;
669 cache
= BDRV_DEFAULT_CACHE
;
672 int option_index
= 0;
673 static const struct option long_options
[] = {
674 {"help", no_argument
, 0, 'h'},
675 {"format", required_argument
, 0, 'f'},
676 {"repair", required_argument
, 0, 'r'},
677 {"output", required_argument
, 0, OPTION_OUTPUT
},
678 {"object", required_argument
, 0, OPTION_OBJECT
},
679 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
680 {"force-share", no_argument
, 0, 'U'},
683 c
= getopt_long(argc
, argv
, ":hf:r:T:qU",
684 long_options
, &option_index
);
690 missing_argument(argv
[optind
- 1]);
693 unrecognized_option(argv
[optind
- 1]);
702 flags
|= BDRV_O_RDWR
;
704 if (!strcmp(optarg
, "leaks")) {
705 fix
= BDRV_FIX_LEAKS
;
706 } else if (!strcmp(optarg
, "all")) {
707 fix
= BDRV_FIX_LEAKS
| BDRV_FIX_ERRORS
;
709 error_exit("Unknown option value for -r "
710 "(expecting 'leaks' or 'all'): %s", optarg
);
725 case OPTION_OBJECT
: {
727 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
733 case OPTION_IMAGE_OPTS
:
738 if (optind
!= argc
- 1) {
739 error_exit("Expecting one image file name");
741 filename
= argv
[optind
++];
743 if (output
&& !strcmp(output
, "json")) {
744 output_format
= OFORMAT_JSON
;
745 } else if (output
&& !strcmp(output
, "human")) {
746 output_format
= OFORMAT_HUMAN
;
748 error_report("--output must be used with human or json as argument.");
752 if (qemu_opts_foreach(&qemu_object_opts
,
753 user_creatable_add_opts_foreach
,
754 NULL
, &error_fatal
)) {
758 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
760 error_report("Invalid source cache option: %s", cache
);
764 blk
= img_open(image_opts
, filename
, fmt
, flags
, writethrough
, quiet
,
771 check
= g_new0(ImageCheck
, 1);
772 ret
= collect_image_check(bs
, check
, filename
, fmt
, fix
);
774 if (ret
== -ENOTSUP
) {
775 error_report("This image format does not support checks");
780 if (check
->corruptions_fixed
|| check
->leaks_fixed
) {
781 int corruptions_fixed
, leaks_fixed
;
783 leaks_fixed
= check
->leaks_fixed
;
784 corruptions_fixed
= check
->corruptions_fixed
;
786 if (output_format
== OFORMAT_HUMAN
) {
788 "The following inconsistencies were found and repaired:\n\n"
789 " %" PRId64
" leaked clusters\n"
790 " %" PRId64
" corruptions\n\n"
791 "Double checking the fixed image now...\n",
793 check
->corruptions_fixed
);
796 ret
= collect_image_check(bs
, check
, filename
, fmt
, 0);
798 check
->leaks_fixed
= leaks_fixed
;
799 check
->corruptions_fixed
= corruptions_fixed
;
803 switch (output_format
) {
805 dump_human_image_check(check
, quiet
);
808 dump_json_image_check(check
, quiet
);
813 if (ret
|| check
->check_errors
) {
815 error_report("Check failed: %s", strerror(-ret
));
817 error_report("Check failed");
823 if (check
->corruptions
) {
825 } else if (check
->leaks
) {
832 qapi_free_ImageCheck(check
);
837 typedef struct CommonBlockJobCBInfo
{
838 BlockDriverState
*bs
;
840 } CommonBlockJobCBInfo
;
842 static void common_block_job_cb(void *opaque
, int ret
)
844 CommonBlockJobCBInfo
*cbi
= opaque
;
847 error_setg_errno(cbi
->errp
, -ret
, "Block job failed");
851 static void run_block_job(BlockJob
*job
, Error
**errp
)
853 AioContext
*aio_context
= blk_get_aio_context(job
->blk
);
856 aio_context_acquire(aio_context
);
859 float progress
= 0.0f
;
860 aio_poll(aio_context
, true);
861 if (job
->job
.progress_total
) {
862 progress
= (float)job
->job
.progress_current
/
863 job
->job
.progress_total
* 100.f
;
865 qemu_progress_print(progress
, 0);
866 } while (!job_is_ready(&job
->job
) && !job_is_completed(&job
->job
));
868 if (!job_is_completed(&job
->job
)) {
869 ret
= job_complete_sync(&job
->job
, errp
);
873 job_unref(&job
->job
);
874 aio_context_release(aio_context
);
876 /* publish completion progress only when success */
878 qemu_progress_print(100.f
, 0);
882 static int img_commit(int argc
, char **argv
)
885 const char *filename
, *fmt
, *cache
, *base
;
887 BlockDriverState
*bs
, *base_bs
;
889 bool progress
= false, quiet
= false, drop
= false;
891 Error
*local_err
= NULL
;
892 CommonBlockJobCBInfo cbi
;
893 bool image_opts
= false;
894 AioContext
*aio_context
;
897 cache
= BDRV_DEFAULT_CACHE
;
900 static const struct option long_options
[] = {
901 {"help", no_argument
, 0, 'h'},
902 {"object", required_argument
, 0, OPTION_OBJECT
},
903 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
906 c
= getopt_long(argc
, argv
, ":f:ht:b:dpq",
913 missing_argument(argv
[optind
- 1]);
916 unrecognized_option(argv
[optind
- 1]);
941 case OPTION_OBJECT
: {
943 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
949 case OPTION_IMAGE_OPTS
:
955 /* Progress is not shown in Quiet mode */
960 if (optind
!= argc
- 1) {
961 error_exit("Expecting one image file name");
963 filename
= argv
[optind
++];
965 if (qemu_opts_foreach(&qemu_object_opts
,
966 user_creatable_add_opts_foreach
,
967 NULL
, &error_fatal
)) {
971 flags
= BDRV_O_RDWR
| BDRV_O_UNMAP
;
972 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
974 error_report("Invalid cache option: %s", cache
);
978 blk
= img_open(image_opts
, filename
, fmt
, flags
, writethrough
, quiet
,
985 qemu_progress_init(progress
, 1.f
);
986 qemu_progress_print(0.f
, 100);
989 base_bs
= bdrv_find_backing_image(bs
, base
);
991 error_setg(&local_err
,
992 "Did not find '%s' in the backing chain of '%s'",
997 /* This is different from QMP, which by default uses the deepest file in
998 * the backing chain (i.e., the very base); however, the traditional
999 * behavior of qemu-img commit is using the immediate backing file. */
1000 base_bs
= backing_bs(bs
);
1002 error_setg(&local_err
, "Image does not have a backing file");
1007 cbi
= (CommonBlockJobCBInfo
){
1012 aio_context
= bdrv_get_aio_context(bs
);
1013 aio_context_acquire(aio_context
);
1014 commit_active_start("commit", bs
, base_bs
, JOB_DEFAULT
, 0,
1015 BLOCKDEV_ON_ERROR_REPORT
, NULL
, common_block_job_cb
,
1016 &cbi
, false, &local_err
);
1017 aio_context_release(aio_context
);
1022 /* When the block job completes, the BlockBackend reference will point to
1023 * the old backing file. In order to avoid that the top image is already
1024 * deleted, so we can still empty it afterwards, increment the reference
1025 * counter here preemptively. */
1030 job
= block_job_get("commit");
1032 run_block_job(job
, &local_err
);
1037 if (!drop
&& bs
->drv
->bdrv_make_empty
) {
1038 ret
= bs
->drv
->bdrv_make_empty(bs
);
1040 error_setg_errno(&local_err
, -ret
, "Could not empty %s",
1052 qemu_progress_end();
1057 error_report_err(local_err
);
1061 qprintf(quiet
, "Image committed.\n");
1066 * Returns -1 if 'buf' contains only zeroes, otherwise the byte index
1067 * of the first sector boundary within buf where the sector contains a
1068 * non-zero byte. This function is robust to a buffer that is not
1071 static int64_t find_nonzero(const uint8_t *buf
, int64_t n
)
1074 int64_t end
= QEMU_ALIGN_DOWN(n
, BDRV_SECTOR_SIZE
);
1076 for (i
= 0; i
< end
; i
+= BDRV_SECTOR_SIZE
) {
1077 if (!buffer_is_zero(buf
+ i
, BDRV_SECTOR_SIZE
)) {
1081 if (i
< n
&& !buffer_is_zero(buf
+ i
, n
- end
)) {
1088 * Returns true iff the first sector pointed to by 'buf' contains at least
1091 * 'pnum' is set to the number of sectors (including and immediately following
1092 * the first one) that are known to be in the same allocated/unallocated state.
1093 * The function will try to align the end offset to alignment boundaries so
1094 * that the request will at least end aligned and consequtive requests will
1095 * also start at an aligned offset.
1097 static int is_allocated_sectors(const uint8_t *buf
, int n
, int *pnum
,
1098 int64_t sector_num
, int alignment
)
1107 is_zero
= buffer_is_zero(buf
, 512);
1108 for(i
= 1; i
< n
; i
++) {
1110 if (is_zero
!= buffer_is_zero(buf
, 512)) {
1115 tail
= (sector_num
+ i
) & (alignment
- 1);
1117 if (is_zero
&& i
<= tail
) {
1118 /* treat unallocated areas which only consist
1119 * of a small tail as allocated. */
1123 /* align up end offset of allocated areas. */
1124 i
+= alignment
- tail
;
1127 /* align down end offset of zero areas. */
1136 * Like is_allocated_sectors, but if the buffer starts with a used sector,
1137 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1138 * breaking up write requests for only small sparse areas.
1140 static int is_allocated_sectors_min(const uint8_t *buf
, int n
, int *pnum
,
1141 int min
, int64_t sector_num
, int alignment
)
1144 int num_checked
, num_used
;
1150 ret
= is_allocated_sectors(buf
, n
, pnum
, sector_num
, alignment
);
1156 buf
+= BDRV_SECTOR_SIZE
* *pnum
;
1158 sector_num
+= *pnum
;
1159 num_checked
= num_used
;
1162 ret
= is_allocated_sectors(buf
, n
, pnum
, sector_num
, alignment
);
1164 buf
+= BDRV_SECTOR_SIZE
* *pnum
;
1166 sector_num
+= *pnum
;
1167 num_checked
+= *pnum
;
1169 num_used
= num_checked
;
1170 } else if (*pnum
>= min
) {
1180 * Compares two buffers sector by sector. Returns 0 if the first
1181 * sector of each buffer matches, non-zero otherwise.
1183 * pnum is set to the sector-aligned size of the buffer prefix that
1184 * has the same matching status as the first sector.
1186 static int compare_buffers(const uint8_t *buf1
, const uint8_t *buf2
,
1187 int64_t bytes
, int64_t *pnum
)
1190 int64_t i
= MIN(bytes
, BDRV_SECTOR_SIZE
);
1194 res
= !!memcmp(buf1
, buf2
, i
);
1196 int64_t len
= MIN(bytes
- i
, BDRV_SECTOR_SIZE
);
1198 if (!!memcmp(buf1
+ i
, buf2
+ i
, len
) != res
) {
1208 #define IO_BUF_SIZE (2 * 1024 * 1024)
1211 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1213 * Intended for use by 'qemu-img compare': Returns 0 in case sectors are
1214 * filled with 0, 1 if sectors contain non-zero data (this is a comparison
1215 * failure), and 4 on error (the exit status for read errors), after emitting
1218 * @param blk: BlockBackend for the image
1219 * @param offset: Starting offset to check
1220 * @param bytes: Number of bytes to check
1221 * @param filename: Name of disk file we are checking (logging purpose)
1222 * @param buffer: Allocated buffer for storing read data
1223 * @param quiet: Flag for quiet mode
1225 static int check_empty_sectors(BlockBackend
*blk
, int64_t offset
,
1226 int64_t bytes
, const char *filename
,
1227 uint8_t *buffer
, bool quiet
)
1232 ret
= blk_pread(blk
, offset
, buffer
, bytes
);
1234 error_report("Error while reading offset %" PRId64
" of %s: %s",
1235 offset
, filename
, strerror(-ret
));
1238 idx
= find_nonzero(buffer
, bytes
);
1240 qprintf(quiet
, "Content mismatch at offset %" PRId64
"!\n",
1249 * Compares two images. Exit codes:
1251 * 0 - Images are identical
1253 * >1 - Error occurred
1255 static int img_compare(int argc
, char **argv
)
1257 const char *fmt1
= NULL
, *fmt2
= NULL
, *cache
, *filename1
, *filename2
;
1258 BlockBackend
*blk1
, *blk2
;
1259 BlockDriverState
*bs1
, *bs2
;
1260 int64_t total_size1
, total_size2
;
1261 uint8_t *buf1
= NULL
, *buf2
= NULL
;
1262 int64_t pnum1
, pnum2
;
1263 int allocated1
, allocated2
;
1264 int ret
= 0; /* return value - 0 Ident, 1 Different, >1 Error */
1265 bool progress
= false, quiet
= false, strict
= false;
1272 uint64_t progress_base
;
1273 bool image_opts
= false;
1274 bool force_share
= false;
1276 cache
= BDRV_DEFAULT_CACHE
;
1278 static const struct option long_options
[] = {
1279 {"help", no_argument
, 0, 'h'},
1280 {"object", required_argument
, 0, OPTION_OBJECT
},
1281 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
1282 {"force-share", no_argument
, 0, 'U'},
1285 c
= getopt_long(argc
, argv
, ":hf:F:T:pqsU",
1286 long_options
, NULL
);
1292 missing_argument(argv
[optind
- 1]);
1295 unrecognized_option(argv
[optind
- 1]);
1321 case OPTION_OBJECT
: {
1323 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
1330 case OPTION_IMAGE_OPTS
:
1336 /* Progress is not shown in Quiet mode */
1342 if (optind
!= argc
- 2) {
1343 error_exit("Expecting two image file names");
1345 filename1
= argv
[optind
++];
1346 filename2
= argv
[optind
++];
1348 if (qemu_opts_foreach(&qemu_object_opts
,
1349 user_creatable_add_opts_foreach
,
1350 NULL
, &error_fatal
)) {
1355 /* Initialize before goto out */
1356 qemu_progress_init(progress
, 2.0);
1359 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
1361 error_report("Invalid source cache option: %s", cache
);
1366 blk1
= img_open(image_opts
, filename1
, fmt1
, flags
, writethrough
, quiet
,
1373 blk2
= img_open(image_opts
, filename2
, fmt2
, flags
, writethrough
, quiet
,
1382 buf1
= blk_blockalign(blk1
, IO_BUF_SIZE
);
1383 buf2
= blk_blockalign(blk2
, IO_BUF_SIZE
);
1384 total_size1
= blk_getlength(blk1
);
1385 if (total_size1
< 0) {
1386 error_report("Can't get size of %s: %s",
1387 filename1
, strerror(-total_size1
));
1391 total_size2
= blk_getlength(blk2
);
1392 if (total_size2
< 0) {
1393 error_report("Can't get size of %s: %s",
1394 filename2
, strerror(-total_size2
));
1398 total_size
= MIN(total_size1
, total_size2
);
1399 progress_base
= MAX(total_size1
, total_size2
);
1401 qemu_progress_print(0, 100);
1403 if (strict
&& total_size1
!= total_size2
) {
1405 qprintf(quiet
, "Strict mode: Image size mismatch!\n");
1409 while (offset
< total_size
) {
1410 int status1
, status2
;
1412 status1
= bdrv_block_status_above(bs1
, NULL
, offset
,
1413 total_size1
- offset
, &pnum1
, NULL
,
1417 error_report("Sector allocation test failed for %s", filename1
);
1420 allocated1
= status1
& BDRV_BLOCK_ALLOCATED
;
1422 status2
= bdrv_block_status_above(bs2
, NULL
, offset
,
1423 total_size2
- offset
, &pnum2
, NULL
,
1427 error_report("Sector allocation test failed for %s", filename2
);
1430 allocated2
= status2
& BDRV_BLOCK_ALLOCATED
;
1432 assert(pnum1
&& pnum2
);
1433 chunk
= MIN(pnum1
, pnum2
);
1436 if (status1
!= status2
) {
1438 qprintf(quiet
, "Strict mode: Offset %" PRId64
1439 " block status mismatch!\n", offset
);
1443 if ((status1
& BDRV_BLOCK_ZERO
) && (status2
& BDRV_BLOCK_ZERO
)) {
1445 } else if (allocated1
== allocated2
) {
1449 chunk
= MIN(chunk
, IO_BUF_SIZE
);
1450 ret
= blk_pread(blk1
, offset
, buf1
, chunk
);
1452 error_report("Error while reading offset %" PRId64
1454 offset
, filename1
, strerror(-ret
));
1458 ret
= blk_pread(blk2
, offset
, buf2
, chunk
);
1460 error_report("Error while reading offset %" PRId64
1462 offset
, filename2
, strerror(-ret
));
1466 ret
= compare_buffers(buf1
, buf2
, chunk
, &pnum
);
1467 if (ret
|| pnum
!= chunk
) {
1468 qprintf(quiet
, "Content mismatch at offset %" PRId64
"!\n",
1469 offset
+ (ret
? 0 : pnum
));
1475 chunk
= MIN(chunk
, IO_BUF_SIZE
);
1477 ret
= check_empty_sectors(blk1
, offset
, chunk
,
1478 filename1
, buf1
, quiet
);
1480 ret
= check_empty_sectors(blk2
, offset
, chunk
,
1481 filename2
, buf1
, quiet
);
1488 qemu_progress_print(((float) chunk
/ progress_base
) * 100, 100);
1491 if (total_size1
!= total_size2
) {
1492 BlockBackend
*blk_over
;
1493 const char *filename_over
;
1495 qprintf(quiet
, "Warning: Image size mismatch!\n");
1496 if (total_size1
> total_size2
) {
1498 filename_over
= filename1
;
1501 filename_over
= filename2
;
1504 while (offset
< progress_base
) {
1505 ret
= bdrv_block_status_above(blk_bs(blk_over
), NULL
, offset
,
1506 progress_base
- offset
, &chunk
,
1510 error_report("Sector allocation test failed for %s",
1515 if (ret
& BDRV_BLOCK_ALLOCATED
&& !(ret
& BDRV_BLOCK_ZERO
)) {
1516 chunk
= MIN(chunk
, IO_BUF_SIZE
);
1517 ret
= check_empty_sectors(blk_over
, offset
, chunk
,
1518 filename_over
, buf1
, quiet
);
1524 qemu_progress_print(((float) chunk
/ progress_base
) * 100, 100);
1528 qprintf(quiet
, "Images are identical.\n");
1538 qemu_progress_end();
1543 enum ImgConvertBlockStatus
{
1549 #define MAX_COROUTINES 16
1551 typedef struct ImgConvertState
{
1553 int64_t *src_sectors
;
1555 int64_t total_sectors
;
1556 int64_t allocated_sectors
;
1557 int64_t allocated_done
;
1560 enum ImgConvertBlockStatus status
;
1561 int64_t sector_next_status
;
1562 BlockBackend
*target
;
1565 bool unallocated_blocks_are_zero
;
1566 bool target_has_backing
;
1567 int64_t target_backing_sectors
; /* negative if unknown */
1572 size_t cluster_sectors
;
1574 long num_coroutines
;
1575 int running_coroutines
;
1576 Coroutine
*co
[MAX_COROUTINES
];
1577 int64_t wait_sector_num
[MAX_COROUTINES
];
1582 static void convert_select_part(ImgConvertState
*s
, int64_t sector_num
,
1583 int *src_cur
, int64_t *src_cur_offset
)
1586 *src_cur_offset
= 0;
1587 while (sector_num
- *src_cur_offset
>= s
->src_sectors
[*src_cur
]) {
1588 *src_cur_offset
+= s
->src_sectors
[*src_cur
];
1590 assert(*src_cur
< s
->src_num
);
1594 static int convert_iteration_sectors(ImgConvertState
*s
, int64_t sector_num
)
1596 int64_t src_cur_offset
;
1597 int ret
, n
, src_cur
;
1598 bool post_backing_zero
= false;
1600 convert_select_part(s
, sector_num
, &src_cur
, &src_cur_offset
);
1602 assert(s
->total_sectors
> sector_num
);
1603 n
= MIN(s
->total_sectors
- sector_num
, BDRV_REQUEST_MAX_SECTORS
);
1605 if (s
->target_backing_sectors
>= 0) {
1606 if (sector_num
>= s
->target_backing_sectors
) {
1607 post_backing_zero
= s
->unallocated_blocks_are_zero
;
1608 } else if (sector_num
+ n
> s
->target_backing_sectors
) {
1609 /* Split requests around target_backing_sectors (because
1610 * starting from there, zeros are handled differently) */
1611 n
= s
->target_backing_sectors
- sector_num
;
1615 if (s
->sector_next_status
<= sector_num
) {
1616 int64_t count
= n
* BDRV_SECTOR_SIZE
;
1618 if (s
->target_has_backing
) {
1620 ret
= bdrv_block_status(blk_bs(s
->src
[src_cur
]),
1621 (sector_num
- src_cur_offset
) *
1623 count
, &count
, NULL
, NULL
);
1625 ret
= bdrv_block_status_above(blk_bs(s
->src
[src_cur
]), NULL
,
1626 (sector_num
- src_cur_offset
) *
1628 count
, &count
, NULL
, NULL
);
1631 error_report("error while reading block status of sector %" PRId64
1632 ": %s", sector_num
, strerror(-ret
));
1635 n
= DIV_ROUND_UP(count
, BDRV_SECTOR_SIZE
);
1637 if (ret
& BDRV_BLOCK_ZERO
) {
1638 s
->status
= post_backing_zero
? BLK_BACKING_FILE
: BLK_ZERO
;
1639 } else if (ret
& BDRV_BLOCK_DATA
) {
1640 s
->status
= BLK_DATA
;
1642 s
->status
= s
->target_has_backing
? BLK_BACKING_FILE
: BLK_DATA
;
1645 s
->sector_next_status
= sector_num
+ n
;
1648 n
= MIN(n
, s
->sector_next_status
- sector_num
);
1649 if (s
->status
== BLK_DATA
) {
1650 n
= MIN(n
, s
->buf_sectors
);
1653 /* We need to write complete clusters for compressed images, so if an
1654 * unallocated area is shorter than that, we must consider the whole
1655 * cluster allocated. */
1656 if (s
->compressed
) {
1657 if (n
< s
->cluster_sectors
) {
1658 n
= MIN(s
->cluster_sectors
, s
->total_sectors
- sector_num
);
1659 s
->status
= BLK_DATA
;
1661 n
= QEMU_ALIGN_DOWN(n
, s
->cluster_sectors
);
1668 static int coroutine_fn
convert_co_read(ImgConvertState
*s
, int64_t sector_num
,
1669 int nb_sectors
, uint8_t *buf
)
1674 assert(nb_sectors
<= s
->buf_sectors
);
1675 while (nb_sectors
> 0) {
1678 int64_t bs_sectors
, src_cur_offset
;
1680 /* In the case of compression with multiple source files, we can get a
1681 * nb_sectors that spreads into the next part. So we must be able to
1682 * read across multiple BDSes for one convert_read() call. */
1683 convert_select_part(s
, sector_num
, &src_cur
, &src_cur_offset
);
1684 blk
= s
->src
[src_cur
];
1685 bs_sectors
= s
->src_sectors
[src_cur
];
1687 n
= MIN(nb_sectors
, bs_sectors
- (sector_num
- src_cur_offset
));
1688 qemu_iovec_init_buf(&qiov
, buf
, n
<< BDRV_SECTOR_BITS
);
1690 ret
= blk_co_preadv(
1691 blk
, (sector_num
- src_cur_offset
) << BDRV_SECTOR_BITS
,
1692 n
<< BDRV_SECTOR_BITS
, &qiov
, 0);
1699 buf
+= n
* BDRV_SECTOR_SIZE
;
1706 static int coroutine_fn
convert_co_write(ImgConvertState
*s
, int64_t sector_num
,
1707 int nb_sectors
, uint8_t *buf
,
1708 enum ImgConvertBlockStatus status
)
1713 while (nb_sectors
> 0) {
1715 BdrvRequestFlags flags
= s
->compressed
? BDRV_REQ_WRITE_COMPRESSED
: 0;
1718 case BLK_BACKING_FILE
:
1719 /* If we have a backing file, leave clusters unallocated that are
1720 * unallocated in the source image, so that the backing file is
1721 * visible at the respective offset. */
1722 assert(s
->target_has_backing
);
1726 /* If we're told to keep the target fully allocated (-S 0) or there
1727 * is real non-zero data, we must write it. Otherwise we can treat
1728 * it as zero sectors.
1729 * Compressed clusters need to be written as a whole, so in that
1730 * case we can only save the write if the buffer is completely
1732 if (!s
->min_sparse
||
1734 is_allocated_sectors_min(buf
, n
, &n
, s
->min_sparse
,
1735 sector_num
, s
->alignment
)) ||
1737 !buffer_is_zero(buf
, n
* BDRV_SECTOR_SIZE
)))
1739 qemu_iovec_init_buf(&qiov
, buf
, n
<< BDRV_SECTOR_BITS
);
1741 ret
= blk_co_pwritev(s
->target
, sector_num
<< BDRV_SECTOR_BITS
,
1742 n
<< BDRV_SECTOR_BITS
, &qiov
, flags
);
1751 if (s
->has_zero_init
) {
1752 assert(!s
->target_has_backing
);
1755 ret
= blk_co_pwrite_zeroes(s
->target
,
1756 sector_num
<< BDRV_SECTOR_BITS
,
1757 n
<< BDRV_SECTOR_BITS
,
1758 BDRV_REQ_MAY_UNMAP
);
1767 buf
+= n
* BDRV_SECTOR_SIZE
;
1773 static int coroutine_fn
convert_co_copy_range(ImgConvertState
*s
, int64_t sector_num
,
1778 while (nb_sectors
> 0) {
1781 int64_t bs_sectors
, src_cur_offset
;
1784 convert_select_part(s
, sector_num
, &src_cur
, &src_cur_offset
);
1785 offset
= (sector_num
- src_cur_offset
) << BDRV_SECTOR_BITS
;
1786 blk
= s
->src
[src_cur
];
1787 bs_sectors
= s
->src_sectors
[src_cur
];
1789 n
= MIN(nb_sectors
, bs_sectors
- (sector_num
- src_cur_offset
));
1791 ret
= blk_co_copy_range(blk
, offset
, s
->target
,
1792 sector_num
<< BDRV_SECTOR_BITS
,
1793 n
<< BDRV_SECTOR_BITS
, 0, 0);
1804 static void coroutine_fn
convert_co_do_copy(void *opaque
)
1806 ImgConvertState
*s
= opaque
;
1807 uint8_t *buf
= NULL
;
1811 for (i
= 0; i
< s
->num_coroutines
; i
++) {
1812 if (s
->co
[i
] == qemu_coroutine_self()) {
1819 s
->running_coroutines
++;
1820 buf
= blk_blockalign(s
->target
, s
->buf_sectors
* BDRV_SECTOR_SIZE
);
1825 enum ImgConvertBlockStatus status
;
1828 qemu_co_mutex_lock(&s
->lock
);
1829 if (s
->ret
!= -EINPROGRESS
|| s
->sector_num
>= s
->total_sectors
) {
1830 qemu_co_mutex_unlock(&s
->lock
);
1833 n
= convert_iteration_sectors(s
, s
->sector_num
);
1835 qemu_co_mutex_unlock(&s
->lock
);
1839 /* save current sector and allocation status to local variables */
1840 sector_num
= s
->sector_num
;
1842 if (!s
->min_sparse
&& s
->status
== BLK_ZERO
) {
1843 n
= MIN(n
, s
->buf_sectors
);
1845 /* increment global sector counter so that other coroutines can
1846 * already continue reading beyond this request */
1848 qemu_co_mutex_unlock(&s
->lock
);
1850 if (status
== BLK_DATA
|| (!s
->min_sparse
&& status
== BLK_ZERO
)) {
1851 s
->allocated_done
+= n
;
1852 qemu_progress_print(100.0 * s
->allocated_done
/
1853 s
->allocated_sectors
, 0);
1857 copy_range
= s
->copy_range
&& s
->status
== BLK_DATA
;
1858 if (status
== BLK_DATA
&& !copy_range
) {
1859 ret
= convert_co_read(s
, sector_num
, n
, buf
);
1861 error_report("error while reading sector %" PRId64
1862 ": %s", sector_num
, strerror(-ret
));
1865 } else if (!s
->min_sparse
&& status
== BLK_ZERO
) {
1867 memset(buf
, 0x00, n
* BDRV_SECTOR_SIZE
);
1870 if (s
->wr_in_order
) {
1871 /* keep writes in order */
1872 while (s
->wr_offs
!= sector_num
&& s
->ret
== -EINPROGRESS
) {
1873 s
->wait_sector_num
[index
] = sector_num
;
1874 qemu_coroutine_yield();
1876 s
->wait_sector_num
[index
] = -1;
1879 if (s
->ret
== -EINPROGRESS
) {
1881 ret
= convert_co_copy_range(s
, sector_num
, n
);
1883 s
->copy_range
= false;
1887 ret
= convert_co_write(s
, sector_num
, n
, buf
, status
);
1890 error_report("error while writing sector %" PRId64
1891 ": %s", sector_num
, strerror(-ret
));
1896 if (s
->wr_in_order
) {
1897 /* reenter the coroutine that might have waited
1898 * for this write to complete */
1899 s
->wr_offs
= sector_num
+ n
;
1900 for (i
= 0; i
< s
->num_coroutines
; i
++) {
1901 if (s
->co
[i
] && s
->wait_sector_num
[i
] == s
->wr_offs
) {
1903 * A -> B -> A cannot occur because A has
1904 * s->wait_sector_num[i] == -1 during A -> B. Therefore
1905 * B will never enter A during this time window.
1907 qemu_coroutine_enter(s
->co
[i
]);
1915 s
->co
[index
] = NULL
;
1916 s
->running_coroutines
--;
1917 if (!s
->running_coroutines
&& s
->ret
== -EINPROGRESS
) {
1918 /* the convert job finished successfully */
1923 static int convert_do_copy(ImgConvertState
*s
)
1926 int64_t sector_num
= 0;
1928 /* Check whether we have zero initialisation or can get it efficiently */
1929 s
->has_zero_init
= s
->min_sparse
&& !s
->target_has_backing
1930 ? bdrv_has_zero_init(blk_bs(s
->target
))
1933 if (!s
->has_zero_init
&& !s
->target_has_backing
&&
1934 bdrv_can_write_zeroes_with_unmap(blk_bs(s
->target
)))
1936 ret
= blk_make_zero(s
->target
, BDRV_REQ_MAY_UNMAP
| BDRV_REQ_NO_FALLBACK
);
1938 s
->has_zero_init
= true;
1942 /* Allocate buffer for copied data. For compressed images, only one cluster
1943 * can be copied at a time. */
1944 if (s
->compressed
) {
1945 if (s
->cluster_sectors
<= 0 || s
->cluster_sectors
> s
->buf_sectors
) {
1946 error_report("invalid cluster size");
1949 s
->buf_sectors
= s
->cluster_sectors
;
1952 while (sector_num
< s
->total_sectors
) {
1953 n
= convert_iteration_sectors(s
, sector_num
);
1957 if (s
->status
== BLK_DATA
|| (!s
->min_sparse
&& s
->status
== BLK_ZERO
))
1959 s
->allocated_sectors
+= n
;
1965 s
->sector_next_status
= 0;
1966 s
->ret
= -EINPROGRESS
;
1968 qemu_co_mutex_init(&s
->lock
);
1969 for (i
= 0; i
< s
->num_coroutines
; i
++) {
1970 s
->co
[i
] = qemu_coroutine_create(convert_co_do_copy
, s
);
1971 s
->wait_sector_num
[i
] = -1;
1972 qemu_coroutine_enter(s
->co
[i
]);
1975 while (s
->running_coroutines
) {
1976 main_loop_wait(false);
1979 if (s
->compressed
&& !s
->ret
) {
1980 /* signal EOF to align */
1981 ret
= blk_pwrite_compressed(s
->target
, 0, NULL
, 0);
1990 #define MAX_BUF_SECTORS 32768
1992 static int img_convert(int argc
, char **argv
)
1994 int c
, bs_i
, flags
, src_flags
= 0;
1995 const char *fmt
= NULL
, *out_fmt
= NULL
, *cache
= "unsafe",
1996 *src_cache
= BDRV_DEFAULT_CACHE
, *out_baseimg
= NULL
,
1997 *out_filename
, *out_baseimg_param
, *snapshot_name
= NULL
;
1998 BlockDriver
*drv
= NULL
, *proto_drv
= NULL
;
1999 BlockDriverInfo bdi
;
2000 BlockDriverState
*out_bs
;
2001 QemuOpts
*opts
= NULL
, *sn_opts
= NULL
;
2002 QemuOptsList
*create_opts
= NULL
;
2003 QDict
*open_opts
= NULL
;
2004 char *options
= NULL
;
2005 Error
*local_err
= NULL
;
2006 bool writethrough
, src_writethrough
, quiet
= false, image_opts
= false,
2007 skip_create
= false, progress
= false, tgt_image_opts
= false;
2008 int64_t ret
= -EINVAL
;
2009 bool force_share
= false;
2010 bool explict_min_sparse
= false;
2012 ImgConvertState s
= (ImgConvertState
) {
2013 /* Need at least 4k of zeros for sparse detection */
2015 .copy_range
= false,
2016 .buf_sectors
= IO_BUF_SIZE
/ BDRV_SECTOR_SIZE
,
2017 .wr_in_order
= true,
2018 .num_coroutines
= 8,
2022 static const struct option long_options
[] = {
2023 {"help", no_argument
, 0, 'h'},
2024 {"object", required_argument
, 0, OPTION_OBJECT
},
2025 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
2026 {"force-share", no_argument
, 0, 'U'},
2027 {"target-image-opts", no_argument
, 0, OPTION_TARGET_IMAGE_OPTS
},
2030 c
= getopt_long(argc
, argv
, ":hf:O:B:Cco:l:S:pt:T:qnm:WU",
2031 long_options
, NULL
);
2037 missing_argument(argv
[optind
- 1]);
2040 unrecognized_option(argv
[optind
- 1]);
2052 out_baseimg
= optarg
;
2055 s
.copy_range
= true;
2058 s
.compressed
= true;
2061 if (!is_valid_option_list(optarg
)) {
2062 error_report("Invalid option list: %s", optarg
);
2066 options
= g_strdup(optarg
);
2068 char *old_options
= options
;
2069 options
= g_strdup_printf("%s,%s", options
, optarg
);
2070 g_free(old_options
);
2074 if (strstart(optarg
, SNAPSHOT_OPT_BASE
, NULL
)) {
2075 sn_opts
= qemu_opts_parse_noisily(&internal_snapshot_opts
,
2078 error_report("Failed in parsing snapshot param '%s'",
2083 snapshot_name
= optarg
;
2090 sval
= cvtnum(optarg
);
2091 if (sval
< 0 || sval
& (BDRV_SECTOR_SIZE
- 1) ||
2092 sval
/ BDRV_SECTOR_SIZE
> MAX_BUF_SECTORS
) {
2093 error_report("Invalid buffer size for sparse output specified. "
2094 "Valid sizes are multiples of %llu up to %llu. Select "
2095 "0 to disable sparse detection (fully allocates output).",
2096 BDRV_SECTOR_SIZE
, MAX_BUF_SECTORS
* BDRV_SECTOR_SIZE
);
2100 s
.min_sparse
= sval
/ BDRV_SECTOR_SIZE
;
2101 explict_min_sparse
= true;
2120 if (qemu_strtol(optarg
, NULL
, 0, &s
.num_coroutines
) ||
2121 s
.num_coroutines
< 1 || s
.num_coroutines
> MAX_COROUTINES
) {
2122 error_report("Invalid number of coroutines. Allowed number of"
2123 " coroutines is between 1 and %d", MAX_COROUTINES
);
2128 s
.wr_in_order
= false;
2133 case OPTION_OBJECT
: {
2134 QemuOpts
*object_opts
;
2135 object_opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
2142 case OPTION_IMAGE_OPTS
:
2145 case OPTION_TARGET_IMAGE_OPTS
:
2146 tgt_image_opts
= true;
2151 if (!out_fmt
&& !tgt_image_opts
) {
2155 if (qemu_opts_foreach(&qemu_object_opts
,
2156 user_creatable_add_opts_foreach
,
2157 NULL
, &error_fatal
)) {
2161 if (s
.compressed
&& s
.copy_range
) {
2162 error_report("Cannot enable copy offloading when -c is used");
2166 if (explict_min_sparse
&& s
.copy_range
) {
2167 error_report("Cannot enable copy offloading when -S is used");
2171 if (tgt_image_opts
&& !skip_create
) {
2172 error_report("--target-image-opts requires use of -n flag");
2176 s
.src_num
= argc
- optind
- 1;
2177 out_filename
= s
.src_num
>= 1 ? argv
[argc
- 1] : NULL
;
2179 if (options
&& has_help_option(options
)) {
2181 ret
= print_block_option_help(out_filename
, out_fmt
);
2184 error_report("Option help requires a format be specified");
2189 if (s
.src_num
< 1) {
2190 error_report("Must specify image file name");
2195 /* ret is still -EINVAL until here */
2196 ret
= bdrv_parse_cache_mode(src_cache
, &src_flags
, &src_writethrough
);
2198 error_report("Invalid source cache option: %s", src_cache
);
2202 /* Initialize before goto out */
2206 qemu_progress_init(progress
, 1.0);
2207 qemu_progress_print(0, 100);
2209 s
.src
= g_new0(BlockBackend
*, s
.src_num
);
2210 s
.src_sectors
= g_new(int64_t, s
.src_num
);
2212 for (bs_i
= 0; bs_i
< s
.src_num
; bs_i
++) {
2213 s
.src
[bs_i
] = img_open(image_opts
, argv
[optind
+ bs_i
],
2214 fmt
, src_flags
, src_writethrough
, quiet
,
2220 s
.src_sectors
[bs_i
] = blk_nb_sectors(s
.src
[bs_i
]);
2221 if (s
.src_sectors
[bs_i
] < 0) {
2222 error_report("Could not get size of %s: %s",
2223 argv
[optind
+ bs_i
], strerror(-s
.src_sectors
[bs_i
]));
2227 s
.total_sectors
+= s
.src_sectors
[bs_i
];
2231 bdrv_snapshot_load_tmp(blk_bs(s
.src
[0]),
2232 qemu_opt_get(sn_opts
, SNAPSHOT_OPT_ID
),
2233 qemu_opt_get(sn_opts
, SNAPSHOT_OPT_NAME
),
2235 } else if (snapshot_name
!= NULL
) {
2236 if (s
.src_num
> 1) {
2237 error_report("No support for concatenating multiple snapshot");
2242 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s
.src
[0]), snapshot_name
,
2246 error_reportf_err(local_err
, "Failed to load snapshot: ");
2252 /* Find driver and parse its options */
2253 drv
= bdrv_find_format(out_fmt
);
2255 error_report("Unknown file format '%s'", out_fmt
);
2260 proto_drv
= bdrv_find_protocol(out_filename
, true, &local_err
);
2262 error_report_err(local_err
);
2267 if (!drv
->create_opts
) {
2268 error_report("Format driver '%s' does not support image creation",
2274 if (!proto_drv
->create_opts
) {
2275 error_report("Protocol driver '%s' does not support image creation",
2276 proto_drv
->format_name
);
2281 create_opts
= qemu_opts_append(create_opts
, drv
->create_opts
);
2282 create_opts
= qemu_opts_append(create_opts
, proto_drv
->create_opts
);
2284 opts
= qemu_opts_create(create_opts
, NULL
, 0, &error_abort
);
2286 qemu_opts_do_parse(opts
, options
, NULL
, &local_err
);
2288 error_report_err(local_err
);
2294 qemu_opt_set_number(opts
, BLOCK_OPT_SIZE
, s
.total_sectors
* 512,
2296 ret
= add_old_style_options(out_fmt
, opts
, out_baseimg
, NULL
);
2302 /* Get backing file name if -o backing_file was used */
2303 out_baseimg_param
= qemu_opt_get(opts
, BLOCK_OPT_BACKING_FILE
);
2304 if (out_baseimg_param
) {
2305 out_baseimg
= out_baseimg_param
;
2307 s
.target_has_backing
= (bool) out_baseimg
;
2309 if (s
.src_num
> 1 && out_baseimg
) {
2310 error_report("Having a backing file for the target makes no sense when "
2311 "concatenating multiple input images");
2316 /* Check if compression is supported */
2319 qemu_opt_get_bool(opts
, BLOCK_OPT_ENCRYPT
, false);
2320 const char *encryptfmt
=
2321 qemu_opt_get(opts
, BLOCK_OPT_ENCRYPT_FORMAT
);
2322 const char *preallocation
=
2323 qemu_opt_get(opts
, BLOCK_OPT_PREALLOC
);
2325 if (drv
&& !drv
->bdrv_co_pwritev_compressed
) {
2326 error_report("Compression not supported for this file format");
2331 if (encryption
|| encryptfmt
) {
2332 error_report("Compression and encryption not supported at "
2339 && strcmp(preallocation
, "off"))
2341 error_report("Compression and preallocation not supported at "
2349 * The later open call will need any decryption secrets, and
2350 * bdrv_create() will purge "opts", so extract them now before
2354 open_opts
= qdict_new();
2355 qemu_opt_foreach(opts
, img_add_key_secrets
, open_opts
, &error_abort
);
2359 /* Create the new image */
2360 ret
= bdrv_create(drv
, out_filename
, opts
, &local_err
);
2362 error_reportf_err(local_err
, "%s: error while converting %s: ",
2363 out_filename
, out_fmt
);
2368 flags
= s
.min_sparse
? (BDRV_O_RDWR
| BDRV_O_UNMAP
) : BDRV_O_RDWR
;
2369 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
2371 error_report("Invalid cache option: %s", cache
);
2376 s
.target
= img_open(tgt_image_opts
, out_filename
, out_fmt
,
2377 flags
, writethrough
, quiet
, false);
2379 /* TODO ultimately we should allow --target-image-opts
2380 * to be used even when -n is not given.
2381 * That has to wait for bdrv_create to be improved
2382 * to allow filenames in option syntax
2384 s
.target
= img_open_file(out_filename
, open_opts
, out_fmt
,
2385 flags
, writethrough
, quiet
, false);
2386 open_opts
= NULL
; /* blk_new_open will have freed it */
2392 out_bs
= blk_bs(s
.target
);
2394 if (s
.compressed
&& !out_bs
->drv
->bdrv_co_pwritev_compressed
) {
2395 error_report("Compression not supported for this file format");
2400 /* increase bufsectors from the default 4096 (2M) if opt_transfer
2401 * or discard_alignment of the out_bs is greater. Limit to
2402 * MAX_BUF_SECTORS as maximum which is currently 32768 (16MB). */
2403 s
.buf_sectors
= MIN(MAX_BUF_SECTORS
,
2405 MAX(out_bs
->bl
.opt_transfer
>> BDRV_SECTOR_BITS
,
2406 out_bs
->bl
.pdiscard_alignment
>>
2407 BDRV_SECTOR_BITS
)));
2409 /* try to align the write requests to the destination to avoid unnecessary
2411 s
.alignment
= MAX(pow2floor(s
.min_sparse
),
2412 DIV_ROUND_UP(out_bs
->bl
.request_alignment
,
2414 assert(is_power_of_2(s
.alignment
));
2417 int64_t output_sectors
= blk_nb_sectors(s
.target
);
2418 if (output_sectors
< 0) {
2419 error_report("unable to get output image length: %s",
2420 strerror(-output_sectors
));
2423 } else if (output_sectors
< s
.total_sectors
) {
2424 error_report("output file is smaller than input file");
2430 if (s
.target_has_backing
) {
2431 /* Errors are treated as "backing length unknown" (which means
2432 * s.target_backing_sectors has to be negative, which it will
2433 * be automatically). The backing file length is used only
2434 * for optimizations, so such a case is not fatal. */
2435 s
.target_backing_sectors
= bdrv_nb_sectors(out_bs
->backing
->bs
);
2437 s
.target_backing_sectors
= -1;
2440 ret
= bdrv_get_info(out_bs
, &bdi
);
2443 error_report("could not get block driver info");
2447 s
.compressed
= s
.compressed
|| bdi
.needs_compressed_writes
;
2448 s
.cluster_sectors
= bdi
.cluster_size
/ BDRV_SECTOR_SIZE
;
2449 s
.unallocated_blocks_are_zero
= bdi
.unallocated_blocks_are_zero
;
2452 ret
= convert_do_copy(&s
);
2455 qemu_progress_print(100, 0);
2457 qemu_progress_end();
2458 qemu_opts_del(opts
);
2459 qemu_opts_free(create_opts
);
2460 qemu_opts_del(sn_opts
);
2461 qobject_unref(open_opts
);
2462 blk_unref(s
.target
);
2464 for (bs_i
= 0; bs_i
< s
.src_num
; bs_i
++) {
2465 blk_unref(s
.src
[bs_i
]);
2469 g_free(s
.src_sectors
);
2477 static void dump_snapshots(BlockDriverState
*bs
)
2479 QEMUSnapshotInfo
*sn_tab
, *sn
;
2482 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
2485 printf("Snapshot list:\n");
2486 bdrv_snapshot_dump(NULL
);
2488 for(i
= 0; i
< nb_sns
; i
++) {
2490 bdrv_snapshot_dump(sn
);
2496 static void dump_json_image_info_list(ImageInfoList
*list
)
2500 Visitor
*v
= qobject_output_visitor_new(&obj
);
2502 visit_type_ImageInfoList(v
, NULL
, &list
, &error_abort
);
2503 visit_complete(v
, &obj
);
2504 str
= qobject_to_json_pretty(obj
);
2505 assert(str
!= NULL
);
2506 printf("%s\n", qstring_get_str(str
));
2512 static void dump_json_image_info(ImageInfo
*info
)
2516 Visitor
*v
= qobject_output_visitor_new(&obj
);
2518 visit_type_ImageInfo(v
, NULL
, &info
, &error_abort
);
2519 visit_complete(v
, &obj
);
2520 str
= qobject_to_json_pretty(obj
);
2521 assert(str
!= NULL
);
2522 printf("%s\n", qstring_get_str(str
));
2528 static void dump_human_image_info_list(ImageInfoList
*list
)
2530 ImageInfoList
*elem
;
2533 for (elem
= list
; elem
; elem
= elem
->next
) {
2539 bdrv_image_info_dump(elem
->value
);
2543 static gboolean
str_equal_func(gconstpointer a
, gconstpointer b
)
2545 return strcmp(a
, b
) == 0;
2549 * Open an image file chain and return an ImageInfoList
2551 * @filename: topmost image filename
2552 * @fmt: topmost image format (may be NULL to autodetect)
2553 * @chain: true - enumerate entire backing file chain
2554 * false - only topmost image file
2556 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2557 * image file. If there was an error a message will have been printed to
2560 static ImageInfoList
*collect_image_info_list(bool image_opts
,
2561 const char *filename
,
2563 bool chain
, bool force_share
)
2565 ImageInfoList
*head
= NULL
;
2566 ImageInfoList
**last
= &head
;
2567 GHashTable
*filenames
;
2570 filenames
= g_hash_table_new_full(g_str_hash
, str_equal_func
, NULL
, NULL
);
2574 BlockDriverState
*bs
;
2576 ImageInfoList
*elem
;
2578 if (g_hash_table_lookup_extended(filenames
, filename
, NULL
, NULL
)) {
2579 error_report("Backing file '%s' creates an infinite loop.",
2583 g_hash_table_insert(filenames
, (gpointer
)filename
, NULL
);
2585 blk
= img_open(image_opts
, filename
, fmt
,
2586 BDRV_O_NO_BACKING
| BDRV_O_NO_IO
, false, false,
2593 bdrv_query_image_info(bs
, &info
, &err
);
2595 error_report_err(err
);
2600 elem
= g_new0(ImageInfoList
, 1);
2607 filename
= fmt
= NULL
;
2609 if (info
->has_full_backing_filename
) {
2610 filename
= info
->full_backing_filename
;
2611 } else if (info
->has_backing_filename
) {
2612 error_report("Could not determine absolute backing filename,"
2613 " but backing filename '%s' present",
2614 info
->backing_filename
);
2617 if (info
->has_backing_filename_format
) {
2618 fmt
= info
->backing_filename_format
;
2622 g_hash_table_destroy(filenames
);
2626 qapi_free_ImageInfoList(head
);
2627 g_hash_table_destroy(filenames
);
2631 static int img_info(int argc
, char **argv
)
2634 OutputFormat output_format
= OFORMAT_HUMAN
;
2636 const char *filename
, *fmt
, *output
;
2637 ImageInfoList
*list
;
2638 bool image_opts
= false;
2639 bool force_share
= false;
2644 int option_index
= 0;
2645 static const struct option long_options
[] = {
2646 {"help", no_argument
, 0, 'h'},
2647 {"format", required_argument
, 0, 'f'},
2648 {"output", required_argument
, 0, OPTION_OUTPUT
},
2649 {"backing-chain", no_argument
, 0, OPTION_BACKING_CHAIN
},
2650 {"object", required_argument
, 0, OPTION_OBJECT
},
2651 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
2652 {"force-share", no_argument
, 0, 'U'},
2655 c
= getopt_long(argc
, argv
, ":f:hU",
2656 long_options
, &option_index
);
2662 missing_argument(argv
[optind
- 1]);
2665 unrecognized_option(argv
[optind
- 1]);
2679 case OPTION_BACKING_CHAIN
:
2682 case OPTION_OBJECT
: {
2684 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
2690 case OPTION_IMAGE_OPTS
:
2695 if (optind
!= argc
- 1) {
2696 error_exit("Expecting one image file name");
2698 filename
= argv
[optind
++];
2700 if (output
&& !strcmp(output
, "json")) {
2701 output_format
= OFORMAT_JSON
;
2702 } else if (output
&& !strcmp(output
, "human")) {
2703 output_format
= OFORMAT_HUMAN
;
2704 } else if (output
) {
2705 error_report("--output must be used with human or json as argument.");
2709 if (qemu_opts_foreach(&qemu_object_opts
,
2710 user_creatable_add_opts_foreach
,
2711 NULL
, &error_fatal
)) {
2715 list
= collect_image_info_list(image_opts
, filename
, fmt
, chain
,
2721 switch (output_format
) {
2723 dump_human_image_info_list(list
);
2727 dump_json_image_info_list(list
);
2729 dump_json_image_info(list
->value
);
2734 qapi_free_ImageInfoList(list
);
2738 static int dump_map_entry(OutputFormat output_format
, MapEntry
*e
,
2741 switch (output_format
) {
2743 if (e
->data
&& !e
->has_offset
) {
2744 error_report("File contains external, encrypted or compressed clusters.");
2747 if (e
->data
&& !e
->zero
) {
2748 printf("%#-16"PRIx64
"%#-16"PRIx64
"%#-16"PRIx64
"%s\n",
2749 e
->start
, e
->length
,
2750 e
->has_offset
? e
->offset
: 0,
2751 e
->has_filename
? e
->filename
: "");
2753 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2754 * Modify the flags here to allow more coalescing.
2756 if (next
&& (!next
->data
|| next
->zero
)) {
2762 printf("%s{ \"start\": %"PRId64
", \"length\": %"PRId64
","
2763 " \"depth\": %"PRId64
", \"zero\": %s, \"data\": %s",
2764 (e
->start
== 0 ? "[" : ",\n"),
2765 e
->start
, e
->length
, e
->depth
,
2766 e
->zero
? "true" : "false",
2767 e
->data
? "true" : "false");
2768 if (e
->has_offset
) {
2769 printf(", \"offset\": %"PRId64
"", e
->offset
);
2781 static int get_block_status(BlockDriverState
*bs
, int64_t offset
,
2782 int64_t bytes
, MapEntry
*e
)
2786 BlockDriverState
*file
;
2789 char *filename
= NULL
;
2791 /* As an optimization, we could cache the current range of unallocated
2792 * clusters in each file of the chain, and avoid querying the same
2798 ret
= bdrv_block_status(bs
, offset
, bytes
, &bytes
, &map
, &file
);
2803 if (ret
& (BDRV_BLOCK_ZERO
|BDRV_BLOCK_DATA
)) {
2806 bs
= backing_bs(bs
);
2815 has_offset
= !!(ret
& BDRV_BLOCK_OFFSET_VALID
);
2817 if (file
&& has_offset
) {
2818 bdrv_refresh_filename(file
);
2819 filename
= file
->filename
;
2825 .data
= !!(ret
& BDRV_BLOCK_DATA
),
2826 .zero
= !!(ret
& BDRV_BLOCK_ZERO
),
2828 .has_offset
= has_offset
,
2830 .has_filename
= filename
,
2831 .filename
= filename
,
2837 static inline bool entry_mergeable(const MapEntry
*curr
, const MapEntry
*next
)
2839 if (curr
->length
== 0) {
2842 if (curr
->zero
!= next
->zero
||
2843 curr
->data
!= next
->data
||
2844 curr
->depth
!= next
->depth
||
2845 curr
->has_filename
!= next
->has_filename
||
2846 curr
->has_offset
!= next
->has_offset
) {
2849 if (curr
->has_filename
&& strcmp(curr
->filename
, next
->filename
)) {
2852 if (curr
->has_offset
&& curr
->offset
+ curr
->length
!= next
->offset
) {
2858 static int img_map(int argc
, char **argv
)
2861 OutputFormat output_format
= OFORMAT_HUMAN
;
2863 BlockDriverState
*bs
;
2864 const char *filename
, *fmt
, *output
;
2866 MapEntry curr
= { .length
= 0 }, next
;
2868 bool image_opts
= false;
2869 bool force_share
= false;
2874 int option_index
= 0;
2875 static const struct option long_options
[] = {
2876 {"help", no_argument
, 0, 'h'},
2877 {"format", required_argument
, 0, 'f'},
2878 {"output", required_argument
, 0, OPTION_OUTPUT
},
2879 {"object", required_argument
, 0, OPTION_OBJECT
},
2880 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
2881 {"force-share", no_argument
, 0, 'U'},
2884 c
= getopt_long(argc
, argv
, ":f:hU",
2885 long_options
, &option_index
);
2891 missing_argument(argv
[optind
- 1]);
2894 unrecognized_option(argv
[optind
- 1]);
2908 case OPTION_OBJECT
: {
2910 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
2916 case OPTION_IMAGE_OPTS
:
2921 if (optind
!= argc
- 1) {
2922 error_exit("Expecting one image file name");
2924 filename
= argv
[optind
];
2926 if (output
&& !strcmp(output
, "json")) {
2927 output_format
= OFORMAT_JSON
;
2928 } else if (output
&& !strcmp(output
, "human")) {
2929 output_format
= OFORMAT_HUMAN
;
2930 } else if (output
) {
2931 error_report("--output must be used with human or json as argument.");
2935 if (qemu_opts_foreach(&qemu_object_opts
,
2936 user_creatable_add_opts_foreach
,
2937 NULL
, &error_fatal
)) {
2941 blk
= img_open(image_opts
, filename
, fmt
, 0, false, false, force_share
);
2947 if (output_format
== OFORMAT_HUMAN
) {
2948 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2951 length
= blk_getlength(blk
);
2952 while (curr
.start
+ curr
.length
< length
) {
2953 int64_t offset
= curr
.start
+ curr
.length
;
2956 /* Probe up to 1 GiB at a time. */
2957 n
= MIN(1 << 30, length
- offset
);
2958 ret
= get_block_status(bs
, offset
, n
, &next
);
2961 error_report("Could not read file metadata: %s", strerror(-ret
));
2965 if (entry_mergeable(&curr
, &next
)) {
2966 curr
.length
+= next
.length
;
2970 if (curr
.length
> 0) {
2971 ret
= dump_map_entry(output_format
, &curr
, &next
);
2979 ret
= dump_map_entry(output_format
, &curr
, NULL
);
2986 #define SNAPSHOT_LIST 1
2987 #define SNAPSHOT_CREATE 2
2988 #define SNAPSHOT_APPLY 3
2989 #define SNAPSHOT_DELETE 4
2991 static int img_snapshot(int argc
, char **argv
)
2994 BlockDriverState
*bs
;
2995 QEMUSnapshotInfo sn
;
2996 char *filename
, *snapshot_name
= NULL
;
2997 int c
, ret
= 0, bdrv_oflags
;
3002 bool image_opts
= false;
3003 bool force_share
= false;
3005 bdrv_oflags
= BDRV_O_RDWR
;
3006 /* Parse commandline parameters */
3008 static const struct option long_options
[] = {
3009 {"help", no_argument
, 0, 'h'},
3010 {"object", required_argument
, 0, OPTION_OBJECT
},
3011 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
3012 {"force-share", no_argument
, 0, 'U'},
3015 c
= getopt_long(argc
, argv
, ":la:c:d:hqU",
3016 long_options
, NULL
);
3022 missing_argument(argv
[optind
- 1]);
3025 unrecognized_option(argv
[optind
- 1]);
3032 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
3035 action
= SNAPSHOT_LIST
;
3036 bdrv_oflags
&= ~BDRV_O_RDWR
; /* no need for RW */
3040 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
3043 action
= SNAPSHOT_APPLY
;
3044 snapshot_name
= optarg
;
3048 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
3051 action
= SNAPSHOT_CREATE
;
3052 snapshot_name
= optarg
;
3056 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
3059 action
= SNAPSHOT_DELETE
;
3060 snapshot_name
= optarg
;
3068 case OPTION_OBJECT
: {
3070 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
3076 case OPTION_IMAGE_OPTS
:
3082 if (optind
!= argc
- 1) {
3083 error_exit("Expecting one image file name");
3085 filename
= argv
[optind
++];
3087 if (qemu_opts_foreach(&qemu_object_opts
,
3088 user_creatable_add_opts_foreach
,
3089 NULL
, &error_fatal
)) {
3093 /* Open the image */
3094 blk
= img_open(image_opts
, filename
, NULL
, bdrv_oflags
, false, quiet
,
3101 /* Perform the requested action */
3107 case SNAPSHOT_CREATE
:
3108 memset(&sn
, 0, sizeof(sn
));
3109 pstrcpy(sn
.name
, sizeof(sn
.name
), snapshot_name
);
3111 qemu_gettimeofday(&tv
);
3112 sn
.date_sec
= tv
.tv_sec
;
3113 sn
.date_nsec
= tv
.tv_usec
* 1000;
3115 ret
= bdrv_snapshot_create(bs
, &sn
);
3117 error_report("Could not create snapshot '%s': %d (%s)",
3118 snapshot_name
, ret
, strerror(-ret
));
3122 case SNAPSHOT_APPLY
:
3123 ret
= bdrv_snapshot_goto(bs
, snapshot_name
, &err
);
3125 error_reportf_err(err
, "Could not apply snapshot '%s': ",
3130 case SNAPSHOT_DELETE
:
3131 ret
= bdrv_snapshot_find(bs
, &sn
, snapshot_name
);
3133 error_report("Could not delete snapshot '%s': snapshot not "
3134 "found", snapshot_name
);
3137 ret
= bdrv_snapshot_delete(bs
, sn
.id_str
, sn
.name
, &err
);
3139 error_reportf_err(err
, "Could not delete snapshot '%s': ",
3155 static int img_rebase(int argc
, char **argv
)
3157 BlockBackend
*blk
= NULL
, *blk_old_backing
= NULL
, *blk_new_backing
= NULL
;
3158 uint8_t *buf_old
= NULL
;
3159 uint8_t *buf_new
= NULL
;
3160 BlockDriverState
*bs
= NULL
;
3162 const char *fmt
, *cache
, *src_cache
, *out_basefmt
, *out_baseimg
;
3163 int c
, flags
, src_flags
, ret
;
3164 bool writethrough
, src_writethrough
;
3166 bool force_share
= false;
3169 Error
*local_err
= NULL
;
3170 bool image_opts
= false;
3172 /* Parse commandline parameters */
3174 cache
= BDRV_DEFAULT_CACHE
;
3175 src_cache
= BDRV_DEFAULT_CACHE
;
3179 static const struct option long_options
[] = {
3180 {"help", no_argument
, 0, 'h'},
3181 {"object", required_argument
, 0, OPTION_OBJECT
},
3182 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
3183 {"force-share", no_argument
, 0, 'U'},
3186 c
= getopt_long(argc
, argv
, ":hf:F:b:upt:T:qU",
3187 long_options
, NULL
);
3193 missing_argument(argv
[optind
- 1]);
3196 unrecognized_option(argv
[optind
- 1]);
3205 out_basefmt
= optarg
;
3208 out_baseimg
= optarg
;
3225 case OPTION_OBJECT
: {
3227 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
3233 case OPTION_IMAGE_OPTS
:
3246 if (optind
!= argc
- 1) {
3247 error_exit("Expecting one image file name");
3249 if (!unsafe
&& !out_baseimg
) {
3250 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
3252 filename
= argv
[optind
++];
3254 if (qemu_opts_foreach(&qemu_object_opts
,
3255 user_creatable_add_opts_foreach
,
3256 NULL
, &error_fatal
)) {
3260 qemu_progress_init(progress
, 2.0);
3261 qemu_progress_print(0, 100);
3263 flags
= BDRV_O_RDWR
| (unsafe
? BDRV_O_NO_BACKING
: 0);
3264 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
3266 error_report("Invalid cache option: %s", cache
);
3271 ret
= bdrv_parse_cache_mode(src_cache
, &src_flags
, &src_writethrough
);
3273 error_report("Invalid source cache option: %s", src_cache
);
3277 /* The source files are opened read-only, don't care about WCE */
3278 assert((src_flags
& BDRV_O_RDWR
) == 0);
3279 (void) src_writethrough
;
3284 * Ignore the old backing file for unsafe rebase in case we want to correct
3285 * the reference to a renamed or moved backing file.
3287 blk
= img_open(image_opts
, filename
, fmt
, flags
, writethrough
, quiet
,
3295 if (out_basefmt
!= NULL
) {
3296 if (bdrv_find_format(out_basefmt
) == NULL
) {
3297 error_report("Invalid format name: '%s'", out_basefmt
);
3303 /* For safe rebasing we need to compare old and new backing file */
3305 char backing_name
[PATH_MAX
];
3306 QDict
*options
= NULL
;
3308 if (bs
->backing_format
[0] != '\0') {
3309 options
= qdict_new();
3310 qdict_put_str(options
, "driver", bs
->backing_format
);
3315 options
= qdict_new();
3317 qdict_put_bool(options
, BDRV_OPT_FORCE_SHARE
, true);
3319 bdrv_get_backing_filename(bs
, backing_name
, sizeof(backing_name
));
3320 blk_old_backing
= blk_new_open(backing_name
, NULL
,
3321 options
, src_flags
, &local_err
);
3322 if (!blk_old_backing
) {
3323 error_reportf_err(local_err
,
3324 "Could not open old backing file '%s': ",
3330 if (out_baseimg
[0]) {
3331 const char *overlay_filename
;
3332 char *out_real_path
;
3334 options
= qdict_new();
3336 qdict_put_str(options
, "driver", out_basefmt
);
3339 qdict_put_bool(options
, BDRV_OPT_FORCE_SHARE
, true);
3342 bdrv_refresh_filename(bs
);
3343 overlay_filename
= bs
->exact_filename
[0] ? bs
->exact_filename
3346 bdrv_get_full_backing_filename_from_filename(overlay_filename
,
3350 error_reportf_err(local_err
,
3351 "Could not resolve backing filename: ");
3356 blk_new_backing
= blk_new_open(out_real_path
, NULL
,
3357 options
, src_flags
, &local_err
);
3358 g_free(out_real_path
);
3359 if (!blk_new_backing
) {
3360 error_reportf_err(local_err
,
3361 "Could not open new backing file '%s': ",
3370 * Check each unallocated cluster in the COW file. If it is unallocated,
3371 * accesses go to the backing file. We must therefore compare this cluster
3372 * in the old and new backing file, and if they differ we need to copy it
3373 * from the old backing file into the COW file.
3375 * If qemu-img crashes during this step, no harm is done. The content of
3376 * the image is the same as the original one at any time.
3380 int64_t old_backing_size
;
3381 int64_t new_backing_size
= 0;
3384 float local_progress
= 0;
3386 buf_old
= blk_blockalign(blk
, IO_BUF_SIZE
);
3387 buf_new
= blk_blockalign(blk
, IO_BUF_SIZE
);
3389 size
= blk_getlength(blk
);
3391 error_report("Could not get size of '%s': %s",
3392 filename
, strerror(-size
));
3396 old_backing_size
= blk_getlength(blk_old_backing
);
3397 if (old_backing_size
< 0) {
3398 char backing_name
[PATH_MAX
];
3400 bdrv_get_backing_filename(bs
, backing_name
, sizeof(backing_name
));
3401 error_report("Could not get size of '%s': %s",
3402 backing_name
, strerror(-old_backing_size
));
3406 if (blk_new_backing
) {
3407 new_backing_size
= blk_getlength(blk_new_backing
);
3408 if (new_backing_size
< 0) {
3409 error_report("Could not get size of '%s': %s",
3410 out_baseimg
, strerror(-new_backing_size
));
3417 local_progress
= (float)100 / (size
/ MIN(size
, IO_BUF_SIZE
));
3420 for (offset
= 0; offset
< size
; offset
+= n
) {
3421 /* How many bytes can we handle with the next read? */
3422 n
= MIN(IO_BUF_SIZE
, size
- offset
);
3424 /* If the cluster is allocated, we don't need to take action */
3425 ret
= bdrv_is_allocated(bs
, offset
, n
, &n
);
3427 error_report("error while reading image metadata: %s",
3436 * Read old and new backing file and take into consideration that
3437 * backing files may be smaller than the COW image.
3439 if (offset
>= old_backing_size
) {
3440 memset(buf_old
, 0, n
);
3442 if (offset
+ n
> old_backing_size
) {
3443 n
= old_backing_size
- offset
;
3446 ret
= blk_pread(blk_old_backing
, offset
, buf_old
, n
);
3448 error_report("error while reading from old backing file");
3453 if (offset
>= new_backing_size
|| !blk_new_backing
) {
3454 memset(buf_new
, 0, n
);
3456 if (offset
+ n
> new_backing_size
) {
3457 n
= new_backing_size
- offset
;
3460 ret
= blk_pread(blk_new_backing
, offset
, buf_new
, n
);
3462 error_report("error while reading from new backing file");
3467 /* If they differ, we need to write to the COW file */
3468 uint64_t written
= 0;
3470 while (written
< n
) {
3473 if (compare_buffers(buf_old
+ written
, buf_new
+ written
,
3474 n
- written
, &pnum
))
3476 ret
= blk_pwrite(blk
, offset
+ written
,
3477 buf_old
+ written
, pnum
, 0);
3479 error_report("Error while writing to COW image: %s",
3487 qemu_progress_print(local_progress
, 100);
3492 * Change the backing file. All clusters that are different from the old
3493 * backing file are overwritten in the COW file now, so the visible content
3494 * doesn't change when we switch the backing file.
3496 if (out_baseimg
&& *out_baseimg
) {
3497 ret
= bdrv_change_backing_file(bs
, out_baseimg
, out_basefmt
);
3499 ret
= bdrv_change_backing_file(bs
, NULL
, NULL
);
3502 if (ret
== -ENOSPC
) {
3503 error_report("Could not change the backing file to '%s': No "
3504 "space left in the file header", out_baseimg
);
3505 } else if (ret
< 0) {
3506 error_report("Could not change the backing file to '%s': %s",
3507 out_baseimg
, strerror(-ret
));
3510 qemu_progress_print(100, 0);
3512 * TODO At this point it is possible to check if any clusters that are
3513 * allocated in the COW file are the same in the backing file. If so, they
3514 * could be dropped from the COW file. Don't do this before switching the
3515 * backing file, in case of a crash this would lead to corruption.
3518 qemu_progress_end();
3521 blk_unref(blk_old_backing
);
3522 blk_unref(blk_new_backing
);
3524 qemu_vfree(buf_old
);
3525 qemu_vfree(buf_new
);
3534 static int img_resize(int argc
, char **argv
)
3537 int c
, ret
, relative
;
3538 const char *filename
, *fmt
, *size
;
3539 int64_t n
, total_size
, current_size
, new_size
;
3541 BlockBackend
*blk
= NULL
;
3542 PreallocMode prealloc
= PREALLOC_MODE_OFF
;
3545 static QemuOptsList resize_options
= {
3546 .name
= "resize_options",
3547 .head
= QTAILQ_HEAD_INITIALIZER(resize_options
.head
),
3550 .name
= BLOCK_OPT_SIZE
,
3551 .type
= QEMU_OPT_SIZE
,
3552 .help
= "Virtual disk size"
3558 bool image_opts
= false;
3559 bool shrink
= false;
3561 /* Remove size from argv manually so that negative numbers are not treated
3562 * as options by getopt. */
3564 error_exit("Not enough arguments");
3568 size
= argv
[--argc
];
3570 /* Parse getopt arguments */
3573 static const struct option long_options
[] = {
3574 {"help", no_argument
, 0, 'h'},
3575 {"object", required_argument
, 0, OPTION_OBJECT
},
3576 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
3577 {"preallocation", required_argument
, 0, OPTION_PREALLOCATION
},
3578 {"shrink", no_argument
, 0, OPTION_SHRINK
},
3581 c
= getopt_long(argc
, argv
, ":f:hq",
3582 long_options
, NULL
);
3588 missing_argument(argv
[optind
- 1]);
3591 unrecognized_option(argv
[optind
- 1]);
3602 case OPTION_OBJECT
: {
3604 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
3610 case OPTION_IMAGE_OPTS
:
3613 case OPTION_PREALLOCATION
:
3614 prealloc
= qapi_enum_parse(&PreallocMode_lookup
, optarg
,
3615 PREALLOC_MODE__MAX
, NULL
);
3616 if (prealloc
== PREALLOC_MODE__MAX
) {
3617 error_report("Invalid preallocation mode '%s'", optarg
);
3626 if (optind
!= argc
- 1) {
3627 error_exit("Expecting image file name and size");
3629 filename
= argv
[optind
++];
3631 if (qemu_opts_foreach(&qemu_object_opts
,
3632 user_creatable_add_opts_foreach
,
3633 NULL
, &error_fatal
)) {
3637 /* Choose grow, shrink, or absolute resize mode */
3653 param
= qemu_opts_create(&resize_options
, NULL
, 0, &error_abort
);
3654 qemu_opt_set(param
, BLOCK_OPT_SIZE
, size
, &err
);
3656 error_report_err(err
);
3658 qemu_opts_del(param
);
3661 n
= qemu_opt_get_size(param
, BLOCK_OPT_SIZE
, 0);
3662 qemu_opts_del(param
);
3664 blk
= img_open(image_opts
, filename
, fmt
,
3665 BDRV_O_RDWR
| BDRV_O_RESIZE
, false, quiet
,
3672 current_size
= blk_getlength(blk
);
3673 if (current_size
< 0) {
3674 error_report("Failed to inquire current image length: %s",
3675 strerror(-current_size
));
3681 total_size
= current_size
+ n
* relative
;
3685 if (total_size
<= 0) {
3686 error_report("New image size must be positive");
3691 if (total_size
<= current_size
&& prealloc
!= PREALLOC_MODE_OFF
) {
3692 error_report("Preallocation can only be used for growing images");
3697 if (total_size
< current_size
&& !shrink
) {
3698 warn_report("Shrinking an image will delete all data beyond the "
3699 "shrunken image's end. Before performing such an "
3700 "operation, make sure there is no important data there.");
3702 if (g_strcmp0(bdrv_get_format_name(blk_bs(blk
)), "raw") != 0) {
3704 "Use the --shrink option to perform a shrink operation.");
3708 warn_report("Using the --shrink option will suppress this message. "
3709 "Note that future versions of qemu-img may refuse to "
3710 "shrink images without this option.");
3714 ret
= blk_truncate(blk
, total_size
, prealloc
, &err
);
3716 error_report_err(err
);
3720 new_size
= blk_getlength(blk
);
3722 error_report("Failed to verify truncated image length: %s",
3723 strerror(-new_size
));
3728 /* Some block drivers implement a truncation method, but only so
3729 * the user can cause qemu to refresh the image's size from disk.
3730 * The idea is that the user resizes the image outside of qemu and
3731 * then invokes block_resize to inform qemu about it.
3732 * (This includes iscsi and file-posix for device files.)
3733 * Of course, that is not the behavior someone invoking
3734 * qemu-img resize would find useful, so we catch that behavior
3735 * here and tell the user. */
3736 if (new_size
!= total_size
&& new_size
== current_size
) {
3737 error_report("Image was not resized; resizing may not be supported "
3743 if (new_size
!= total_size
) {
3744 warn_report("Image should have been resized to %" PRIi64
3745 " bytes, but was resized to %" PRIi64
" bytes",
3746 total_size
, new_size
);
3749 qprintf(quiet
, "Image resized.\n");
3759 static void amend_status_cb(BlockDriverState
*bs
,
3760 int64_t offset
, int64_t total_work_size
,
3763 qemu_progress_print(100.f
* offset
/ total_work_size
, 0);
3766 static int print_amend_option_help(const char *format
)
3770 /* Find driver and parse its options */
3771 drv
= bdrv_find_format(format
);
3773 error_report("Unknown file format '%s'", format
);
3777 if (!drv
->bdrv_amend_options
) {
3778 error_report("Format driver '%s' does not support option amendment",
3783 /* Every driver supporting amendment must have create_opts */
3784 assert(drv
->create_opts
);
3786 printf("Creation options for '%s':\n", format
);
3787 qemu_opts_print_help(drv
->create_opts
, false);
3788 printf("\nNote that not all of these options may be amendable.\n");
3792 static int img_amend(int argc
, char **argv
)
3796 char *options
= NULL
;
3797 QemuOptsList
*create_opts
= NULL
;
3798 QemuOpts
*opts
= NULL
;
3799 const char *fmt
= NULL
, *filename
, *cache
;
3802 bool quiet
= false, progress
= false;
3803 BlockBackend
*blk
= NULL
;
3804 BlockDriverState
*bs
= NULL
;
3805 bool image_opts
= false;
3807 cache
= BDRV_DEFAULT_CACHE
;
3809 static const struct option long_options
[] = {
3810 {"help", no_argument
, 0, 'h'},
3811 {"object", required_argument
, 0, OPTION_OBJECT
},
3812 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
3815 c
= getopt_long(argc
, argv
, ":ho:f:t:pq",
3816 long_options
, NULL
);
3823 missing_argument(argv
[optind
- 1]);
3826 unrecognized_option(argv
[optind
- 1]);
3832 if (!is_valid_option_list(optarg
)) {
3833 error_report("Invalid option list: %s", optarg
);
3835 goto out_no_progress
;
3838 options
= g_strdup(optarg
);
3840 char *old_options
= options
;
3841 options
= g_strdup_printf("%s,%s", options
, optarg
);
3842 g_free(old_options
);
3858 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
3862 goto out_no_progress
;
3865 case OPTION_IMAGE_OPTS
:
3872 error_exit("Must specify options (-o)");
3875 if (qemu_opts_foreach(&qemu_object_opts
,
3876 user_creatable_add_opts_foreach
,
3877 NULL
, &error_fatal
)) {
3879 goto out_no_progress
;
3885 qemu_progress_init(progress
, 1.0);
3887 filename
= (optind
== argc
- 1) ? argv
[argc
- 1] : NULL
;
3888 if (fmt
&& has_help_option(options
)) {
3889 /* If a format is explicitly specified (and possibly no filename is
3890 * given), print option help here */
3891 ret
= print_amend_option_help(fmt
);
3895 if (optind
!= argc
- 1) {
3896 error_report("Expecting one image file name");
3901 flags
= BDRV_O_RDWR
;
3902 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
3904 error_report("Invalid cache option: %s", cache
);
3908 blk
= img_open(image_opts
, filename
, fmt
, flags
, writethrough
, quiet
,
3916 fmt
= bs
->drv
->format_name
;
3918 if (has_help_option(options
)) {
3919 /* If the format was auto-detected, print option help here */
3920 ret
= print_amend_option_help(fmt
);
3924 if (!bs
->drv
->bdrv_amend_options
) {
3925 error_report("Format driver '%s' does not support option amendment",
3931 /* Every driver supporting amendment must have create_opts */
3932 assert(bs
->drv
->create_opts
);
3934 create_opts
= qemu_opts_append(create_opts
, bs
->drv
->create_opts
);
3935 opts
= qemu_opts_create(create_opts
, NULL
, 0, &error_abort
);
3936 qemu_opts_do_parse(opts
, options
, NULL
, &err
);
3938 error_report_err(err
);
3943 /* In case the driver does not call amend_status_cb() */
3944 qemu_progress_print(0.f
, 0);
3945 ret
= bdrv_amend_options(bs
, opts
, &amend_status_cb
, NULL
, &err
);
3946 qemu_progress_print(100.f
, 0);
3948 error_report_err(err
);
3953 qemu_progress_end();
3957 qemu_opts_del(opts
);
3958 qemu_opts_free(create_opts
);
3967 typedef struct BenchData
{
3969 uint64_t image_size
;
3976 bool drain_on_flush
;
3985 static void bench_undrained_flush_cb(void *opaque
, int ret
)
3988 error_report("Failed flush request: %s", strerror(-ret
));
3993 static void bench_cb(void *opaque
, int ret
)
3995 BenchData
*b
= opaque
;
3999 error_report("Failed request: %s", strerror(-ret
));
4004 /* Just finished a flush with drained queue: Start next requests */
4005 assert(b
->in_flight
== 0);
4006 b
->in_flush
= false;
4007 } else if (b
->in_flight
> 0) {
4008 int remaining
= b
->n
- b
->in_flight
;
4013 /* Time for flush? Drain queue if requested, then flush */
4014 if (b
->flush_interval
&& remaining
% b
->flush_interval
== 0) {
4015 if (!b
->in_flight
|| !b
->drain_on_flush
) {
4016 BlockCompletionFunc
*cb
;
4018 if (b
->drain_on_flush
) {
4022 cb
= bench_undrained_flush_cb
;
4025 acb
= blk_aio_flush(b
->blk
, cb
, b
);
4027 error_report("Failed to issue flush request");
4031 if (b
->drain_on_flush
) {
4037 while (b
->n
> b
->in_flight
&& b
->in_flight
< b
->nrreq
) {
4038 int64_t offset
= b
->offset
;
4039 /* blk_aio_* might look for completed I/Os and kick bench_cb
4040 * again, so make sure this operation is counted by in_flight
4041 * and b->offset is ready for the next submission.
4044 b
->offset
+= b
->step
;
4045 b
->offset
%= b
->image_size
;
4047 acb
= blk_aio_pwritev(b
->blk
, offset
, b
->qiov
, 0, bench_cb
, b
);
4049 acb
= blk_aio_preadv(b
->blk
, offset
, b
->qiov
, 0, bench_cb
, b
);
4052 error_report("Failed to issue request");
4058 static int img_bench(int argc
, char **argv
)
4061 const char *fmt
= NULL
, *filename
;
4063 bool image_opts
= false;
4064 bool is_write
= false;
4068 size_t bufsize
= 4096;
4071 int flush_interval
= 0;
4072 bool drain_on_flush
= true;
4074 BlockBackend
*blk
= NULL
;
4075 BenchData data
= {};
4077 bool writethrough
= false;
4078 struct timeval t1
, t2
;
4080 bool force_share
= false;
4084 static const struct option long_options
[] = {
4085 {"help", no_argument
, 0, 'h'},
4086 {"flush-interval", required_argument
, 0, OPTION_FLUSH_INTERVAL
},
4087 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
4088 {"pattern", required_argument
, 0, OPTION_PATTERN
},
4089 {"no-drain", no_argument
, 0, OPTION_NO_DRAIN
},
4090 {"force-share", no_argument
, 0, 'U'},
4093 c
= getopt_long(argc
, argv
, ":hc:d:f:no:qs:S:t:wU", long_options
, NULL
);
4100 missing_argument(argv
[optind
- 1]);
4103 unrecognized_option(argv
[optind
- 1]);
4112 if (qemu_strtoul(optarg
, NULL
, 0, &res
) < 0 || res
> INT_MAX
) {
4113 error_report("Invalid request count specified");
4123 if (qemu_strtoul(optarg
, NULL
, 0, &res
) < 0 || res
> INT_MAX
) {
4124 error_report("Invalid queue depth specified");
4134 flags
|= BDRV_O_NATIVE_AIO
;
4138 offset
= cvtnum(optarg
);
4140 error_report("Invalid offset specified");
4153 sval
= cvtnum(optarg
);
4154 if (sval
< 0 || sval
> INT_MAX
) {
4155 error_report("Invalid buffer size specified");
4166 sval
= cvtnum(optarg
);
4167 if (sval
< 0 || sval
> INT_MAX
) {
4168 error_report("Invalid step size specified");
4176 ret
= bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
);
4178 error_report("Invalid cache mode");
4184 flags
|= BDRV_O_RDWR
;
4190 case OPTION_PATTERN
:
4194 if (qemu_strtoul(optarg
, NULL
, 0, &res
) < 0 || res
> 0xff) {
4195 error_report("Invalid pattern byte specified");
4201 case OPTION_FLUSH_INTERVAL
:
4205 if (qemu_strtoul(optarg
, NULL
, 0, &res
) < 0 || res
> INT_MAX
) {
4206 error_report("Invalid flush interval specified");
4209 flush_interval
= res
;
4212 case OPTION_NO_DRAIN
:
4213 drain_on_flush
= false;
4215 case OPTION_IMAGE_OPTS
:
4221 if (optind
!= argc
- 1) {
4222 error_exit("Expecting one image file name");
4224 filename
= argv
[argc
- 1];
4226 if (!is_write
&& flush_interval
) {
4227 error_report("--flush-interval is only available in write tests");
4231 if (flush_interval
&& flush_interval
< depth
) {
4232 error_report("Flush interval can't be smaller than depth");
4237 blk
= img_open(image_opts
, filename
, fmt
, flags
, writethrough
, quiet
,
4244 image_size
= blk_getlength(blk
);
4245 if (image_size
< 0) {
4250 data
= (BenchData
) {
4252 .image_size
= image_size
,
4254 .step
= step
?: bufsize
,
4259 .flush_interval
= flush_interval
,
4260 .drain_on_flush
= drain_on_flush
,
4262 printf("Sending %d %s requests, %d bytes each, %d in parallel "
4263 "(starting at offset %" PRId64
", step size %d)\n",
4264 data
.n
, data
.write
? "write" : "read", data
.bufsize
, data
.nrreq
,
4265 data
.offset
, data
.step
);
4266 if (flush_interval
) {
4267 printf("Sending flush every %d requests\n", flush_interval
);
4270 buf_size
= data
.nrreq
* data
.bufsize
;
4271 data
.buf
= blk_blockalign(blk
, buf_size
);
4272 memset(data
.buf
, pattern
, data
.nrreq
* data
.bufsize
);
4274 blk_register_buf(blk
, data
.buf
, buf_size
);
4276 data
.qiov
= g_new(QEMUIOVector
, data
.nrreq
);
4277 for (i
= 0; i
< data
.nrreq
; i
++) {
4278 qemu_iovec_init(&data
.qiov
[i
], 1);
4279 qemu_iovec_add(&data
.qiov
[i
],
4280 data
.buf
+ i
* data
.bufsize
, data
.bufsize
);
4283 gettimeofday(&t1
, NULL
);
4286 while (data
.n
> 0) {
4287 main_loop_wait(false);
4289 gettimeofday(&t2
, NULL
);
4291 printf("Run completed in %3.3f seconds.\n",
4292 (t2
.tv_sec
- t1
.tv_sec
)
4293 + ((double)(t2
.tv_usec
- t1
.tv_usec
) / 1000000));
4297 blk_unregister_buf(blk
, data
.buf
);
4299 qemu_vfree(data
.buf
);
4320 int bsz
; /* Block size */
4328 int (*f
)(const char *, struct DdIo
*, struct DdIo
*, struct DdInfo
*);
4332 static int img_dd_bs(const char *arg
,
4333 struct DdIo
*in
, struct DdIo
*out
,
4340 if (res
<= 0 || res
> INT_MAX
) {
4341 error_report("invalid number: '%s'", arg
);
4344 in
->bsz
= out
->bsz
= res
;
4349 static int img_dd_count(const char *arg
,
4350 struct DdIo
*in
, struct DdIo
*out
,
4353 dd
->count
= cvtnum(arg
);
4355 if (dd
->count
< 0) {
4356 error_report("invalid number: '%s'", arg
);
4363 static int img_dd_if(const char *arg
,
4364 struct DdIo
*in
, struct DdIo
*out
,
4367 in
->filename
= g_strdup(arg
);
4372 static int img_dd_of(const char *arg
,
4373 struct DdIo
*in
, struct DdIo
*out
,
4376 out
->filename
= g_strdup(arg
);
4381 static int img_dd_skip(const char *arg
,
4382 struct DdIo
*in
, struct DdIo
*out
,
4385 in
->offset
= cvtnum(arg
);
4387 if (in
->offset
< 0) {
4388 error_report("invalid number: '%s'", arg
);
4395 static int img_dd(int argc
, char **argv
)
4400 BlockDriver
*drv
= NULL
, *proto_drv
= NULL
;
4401 BlockBackend
*blk1
= NULL
, *blk2
= NULL
;
4402 QemuOpts
*opts
= NULL
;
4403 QemuOptsList
*create_opts
= NULL
;
4404 Error
*local_err
= NULL
;
4405 bool image_opts
= false;
4407 const char *out_fmt
= "raw";
4408 const char *fmt
= NULL
;
4410 int64_t block_count
= 0, out_pos
, in_pos
;
4411 bool force_share
= false;
4412 struct DdInfo dd
= {
4417 .bsz
= 512, /* Block size is by default 512 bytes */
4429 const struct DdOpts options
[] = {
4430 { "bs", img_dd_bs
, C_BS
},
4431 { "count", img_dd_count
, C_COUNT
},
4432 { "if", img_dd_if
, C_IF
},
4433 { "of", img_dd_of
, C_OF
},
4434 { "skip", img_dd_skip
, C_SKIP
},
4437 const struct option long_options
[] = {
4438 { "help", no_argument
, 0, 'h'},
4439 { "object", required_argument
, 0, OPTION_OBJECT
},
4440 { "image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
4441 { "force-share", no_argument
, 0, 'U'},
4445 while ((c
= getopt_long(argc
, argv
, ":hf:O:U", long_options
, NULL
))) {
4457 missing_argument(argv
[optind
- 1]);
4460 unrecognized_option(argv
[optind
- 1]);
4469 if (!qemu_opts_parse_noisily(&qemu_object_opts
, optarg
, true)) {
4474 case OPTION_IMAGE_OPTS
:
4480 for (i
= optind
; i
< argc
; i
++) {
4482 arg
= g_strdup(argv
[i
]);
4484 tmp
= strchr(arg
, '=');
4486 error_report("unrecognized operand %s", arg
);
4493 for (j
= 0; options
[j
].name
!= NULL
; j
++) {
4494 if (!strcmp(arg
, options
[j
].name
)) {
4498 if (options
[j
].name
== NULL
) {
4499 error_report("unrecognized operand %s", arg
);
4504 if (options
[j
].f(tmp
, &in
, &out
, &dd
) != 0) {
4508 dd
.flags
|= options
[j
].flag
;
4513 if (!(dd
.flags
& C_IF
&& dd
.flags
& C_OF
)) {
4514 error_report("Must specify both input and output files");
4519 if (qemu_opts_foreach(&qemu_object_opts
,
4520 user_creatable_add_opts_foreach
,
4521 NULL
, &error_fatal
)) {
4526 blk1
= img_open(image_opts
, in
.filename
, fmt
, 0, false, false,
4534 drv
= bdrv_find_format(out_fmt
);
4536 error_report("Unknown file format");
4540 proto_drv
= bdrv_find_protocol(out
.filename
, true, &local_err
);
4543 error_report_err(local_err
);
4547 if (!drv
->create_opts
) {
4548 error_report("Format driver '%s' does not support image creation",
4553 if (!proto_drv
->create_opts
) {
4554 error_report("Protocol driver '%s' does not support image creation",
4555 proto_drv
->format_name
);
4559 create_opts
= qemu_opts_append(create_opts
, drv
->create_opts
);
4560 create_opts
= qemu_opts_append(create_opts
, proto_drv
->create_opts
);
4562 opts
= qemu_opts_create(create_opts
, NULL
, 0, &error_abort
);
4564 size
= blk_getlength(blk1
);
4566 error_report("Failed to get size for '%s'", in
.filename
);
4571 if (dd
.flags
& C_COUNT
&& dd
.count
<= INT64_MAX
/ in
.bsz
&&
4572 dd
.count
* in
.bsz
< size
) {
4573 size
= dd
.count
* in
.bsz
;
4576 /* Overflow means the specified offset is beyond input image's size */
4577 if (dd
.flags
& C_SKIP
&& (in
.offset
> INT64_MAX
/ in
.bsz
||
4578 size
< in
.bsz
* in
.offset
)) {
4579 qemu_opt_set_number(opts
, BLOCK_OPT_SIZE
, 0, &error_abort
);
4581 qemu_opt_set_number(opts
, BLOCK_OPT_SIZE
,
4582 size
- in
.bsz
* in
.offset
, &error_abort
);
4585 ret
= bdrv_create(drv
, out
.filename
, opts
, &local_err
);
4587 error_reportf_err(local_err
,
4588 "%s: error while creating output image: ",
4594 /* TODO, we can't honour --image-opts for the target,
4595 * since it needs to be given in a format compatible
4596 * with the bdrv_create() call above which does not
4597 * support image-opts style.
4599 blk2
= img_open_file(out
.filename
, NULL
, out_fmt
, BDRV_O_RDWR
,
4600 false, false, false);
4607 if (dd
.flags
& C_SKIP
&& (in
.offset
> INT64_MAX
/ in
.bsz
||
4608 size
< in
.offset
* in
.bsz
)) {
4609 /* We give a warning if the skip option is bigger than the input
4610 * size and create an empty output disk image (i.e. like dd(1)).
4612 error_report("%s: cannot skip to specified offset", in
.filename
);
4615 in_pos
= in
.offset
* in
.bsz
;
4618 in
.buf
= g_new(uint8_t, in
.bsz
);
4620 for (out_pos
= 0; in_pos
< size
; block_count
++) {
4621 int in_ret
, out_ret
;
4623 if (in_pos
+ in
.bsz
> size
) {
4624 in_ret
= blk_pread(blk1
, in_pos
, in
.buf
, size
- in_pos
);
4626 in_ret
= blk_pread(blk1
, in_pos
, in
.buf
, in
.bsz
);
4629 error_report("error while reading from input image file: %s",
4636 out_ret
= blk_pwrite(blk2
, out_pos
, in
.buf
, in_ret
, 0);
4639 error_report("error while writing to output image file: %s",
4640 strerror(-out_ret
));
4649 qemu_opts_del(opts
);
4650 qemu_opts_free(create_opts
);
4653 g_free(in
.filename
);
4654 g_free(out
.filename
);
4664 static void dump_json_block_measure_info(BlockMeasureInfo
*info
)
4668 Visitor
*v
= qobject_output_visitor_new(&obj
);
4670 visit_type_BlockMeasureInfo(v
, NULL
, &info
, &error_abort
);
4671 visit_complete(v
, &obj
);
4672 str
= qobject_to_json_pretty(obj
);
4673 assert(str
!= NULL
);
4674 printf("%s\n", qstring_get_str(str
));
4680 static int img_measure(int argc
, char **argv
)
4682 static const struct option long_options
[] = {
4683 {"help", no_argument
, 0, 'h'},
4684 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
4685 {"object", required_argument
, 0, OPTION_OBJECT
},
4686 {"output", required_argument
, 0, OPTION_OUTPUT
},
4687 {"size", required_argument
, 0, OPTION_SIZE
},
4688 {"force-share", no_argument
, 0, 'U'},
4691 OutputFormat output_format
= OFORMAT_HUMAN
;
4692 BlockBackend
*in_blk
= NULL
;
4694 const char *filename
= NULL
;
4695 const char *fmt
= NULL
;
4696 const char *out_fmt
= "raw";
4697 char *options
= NULL
;
4698 char *snapshot_name
= NULL
;
4699 bool force_share
= false;
4700 QemuOpts
*opts
= NULL
;
4701 QemuOpts
*object_opts
= NULL
;
4702 QemuOpts
*sn_opts
= NULL
;
4703 QemuOptsList
*create_opts
= NULL
;
4704 bool image_opts
= false;
4705 uint64_t img_size
= UINT64_MAX
;
4706 BlockMeasureInfo
*info
= NULL
;
4707 Error
*local_err
= NULL
;
4711 while ((c
= getopt_long(argc
, argv
, "hf:O:o:l:U",
4712 long_options
, NULL
)) != -1) {
4725 if (!is_valid_option_list(optarg
)) {
4726 error_report("Invalid option list: %s", optarg
);
4730 options
= g_strdup(optarg
);
4732 char *old_options
= options
;
4733 options
= g_strdup_printf("%s,%s", options
, optarg
);
4734 g_free(old_options
);
4738 if (strstart(optarg
, SNAPSHOT_OPT_BASE
, NULL
)) {
4739 sn_opts
= qemu_opts_parse_noisily(&internal_snapshot_opts
,
4742 error_report("Failed in parsing snapshot param '%s'",
4747 snapshot_name
= optarg
;
4754 object_opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
4760 case OPTION_IMAGE_OPTS
:
4764 if (!strcmp(optarg
, "json")) {
4765 output_format
= OFORMAT_JSON
;
4766 } else if (!strcmp(optarg
, "human")) {
4767 output_format
= OFORMAT_HUMAN
;
4769 error_report("--output must be used with human or json "
4778 sval
= cvtnum(optarg
);
4780 if (sval
== -ERANGE
) {
4781 error_report("Image size must be less than 8 EiB!");
4783 error_report("Invalid image size specified! You may use "
4784 "k, M, G, T, P or E suffixes for ");
4785 error_report("kilobytes, megabytes, gigabytes, terabytes, "
4786 "petabytes and exabytes.");
4790 img_size
= (uint64_t)sval
;
4796 if (qemu_opts_foreach(&qemu_object_opts
,
4797 user_creatable_add_opts_foreach
,
4798 NULL
, &error_fatal
)) {
4802 if (argc
- optind
> 1) {
4803 error_report("At most one filename argument is allowed.");
4805 } else if (argc
- optind
== 1) {
4806 filename
= argv
[optind
];
4810 (object_opts
|| image_opts
|| fmt
|| snapshot_name
|| sn_opts
)) {
4811 error_report("--object, --image-opts, -f, and -l "
4812 "require a filename argument.");
4815 if (filename
&& img_size
!= UINT64_MAX
) {
4816 error_report("--size N cannot be used together with a filename.");
4819 if (!filename
&& img_size
== UINT64_MAX
) {
4820 error_report("Either --size N or one filename must be specified.");
4825 in_blk
= img_open(image_opts
, filename
, fmt
, 0,
4826 false, false, force_share
);
4832 bdrv_snapshot_load_tmp(blk_bs(in_blk
),
4833 qemu_opt_get(sn_opts
, SNAPSHOT_OPT_ID
),
4834 qemu_opt_get(sn_opts
, SNAPSHOT_OPT_NAME
),
4836 } else if (snapshot_name
!= NULL
) {
4837 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk
),
4838 snapshot_name
, &local_err
);
4841 error_reportf_err(local_err
, "Failed to load snapshot: ");
4846 drv
= bdrv_find_format(out_fmt
);
4848 error_report("Unknown file format '%s'", out_fmt
);
4851 if (!drv
->create_opts
) {
4852 error_report("Format driver '%s' does not support image creation",
4857 create_opts
= qemu_opts_append(create_opts
, drv
->create_opts
);
4858 create_opts
= qemu_opts_append(create_opts
, bdrv_file
.create_opts
);
4859 opts
= qemu_opts_create(create_opts
, NULL
, 0, &error_abort
);
4861 qemu_opts_do_parse(opts
, options
, NULL
, &local_err
);
4863 error_report_err(local_err
);
4864 error_report("Invalid options for file format '%s'", out_fmt
);
4868 if (img_size
!= UINT64_MAX
) {
4869 qemu_opt_set_number(opts
, BLOCK_OPT_SIZE
, img_size
, &error_abort
);
4872 info
= bdrv_measure(drv
, opts
, in_blk
? blk_bs(in_blk
) : NULL
, &local_err
);
4874 error_report_err(local_err
);
4878 if (output_format
== OFORMAT_HUMAN
) {
4879 printf("required size: %" PRIu64
"\n", info
->required
);
4880 printf("fully allocated size: %" PRIu64
"\n", info
->fully_allocated
);
4882 dump_json_block_measure_info(info
);
4888 qapi_free_BlockMeasureInfo(info
);
4889 qemu_opts_del(object_opts
);
4890 qemu_opts_del(opts
);
4891 qemu_opts_del(sn_opts
);
4892 qemu_opts_free(create_opts
);
4898 static const img_cmd_t img_cmds
[] = {
4899 #define DEF(option, callback, arg_string) \
4900 { option, callback },
4901 #include "qemu-img-cmds.h"
4906 int main(int argc
, char **argv
)
4908 const img_cmd_t
*cmd
;
4909 const char *cmdname
;
4910 Error
*local_error
= NULL
;
4911 char *trace_file
= NULL
;
4913 static const struct option long_options
[] = {
4914 {"help", no_argument
, 0, 'h'},
4915 {"version", no_argument
, 0, 'V'},
4916 {"trace", required_argument
, NULL
, 'T'},
4921 signal(SIGPIPE
, SIG_IGN
);
4924 error_init(argv
[0]);
4925 module_call_init(MODULE_INIT_TRACE
);
4926 qemu_init_exec_dir(argv
[0]);
4928 if (qemu_init_main_loop(&local_error
)) {
4929 error_report_err(local_error
);
4933 qcrypto_init(&error_fatal
);
4935 module_call_init(MODULE_INIT_QOM
);
4938 error_exit("Not enough arguments");
4941 qemu_add_opts(&qemu_object_opts
);
4942 qemu_add_opts(&qemu_source_opts
);
4943 qemu_add_opts(&qemu_trace_opts
);
4945 while ((c
= getopt_long(argc
, argv
, "+:hVT:", long_options
, NULL
)) != -1) {
4948 missing_argument(argv
[optind
- 1]);
4951 unrecognized_option(argv
[optind
- 1]);
4957 printf(QEMU_IMG_VERSION
);
4961 trace_file
= trace_opt_parse(optarg
);
4966 cmdname
= argv
[optind
];
4968 /* reset getopt_long scanning */
4974 qemu_reset_optind();
4976 if (!trace_init_backends()) {
4979 trace_init_file(trace_file
);
4980 qemu_set_log(LOG_TRACE
);
4982 /* find the command */
4983 for (cmd
= img_cmds
; cmd
->name
!= NULL
; cmd
++) {
4984 if (!strcmp(cmdname
, cmd
->name
)) {
4985 return cmd
->handler(argc
, argv
);
4990 error_exit("Command not found: %s", cmdname
);