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
);
271 printf("Supported options:\n");
273 printf("Supported %s options:\n", fmt
);
275 qemu_opts_print_help(create_opts
, false);
276 qemu_opts_free(create_opts
);
280 "The protocol level may support further options.\n"
281 "Specify the target filename to include those options.\n");
288 static BlockBackend
*img_open_opts(const char *optstr
,
289 QemuOpts
*opts
, int flags
, bool writethrough
,
290 bool quiet
, bool force_share
)
293 Error
*local_err
= NULL
;
295 options
= qemu_opts_to_qdict(opts
, NULL
);
297 if (qdict_haskey(options
, BDRV_OPT_FORCE_SHARE
)
298 && strcmp(qdict_get_str(options
, BDRV_OPT_FORCE_SHARE
), "on")) {
299 error_report("--force-share/-U conflicts with image options");
300 qobject_unref(options
);
303 qdict_put_str(options
, BDRV_OPT_FORCE_SHARE
, "on");
305 blk
= blk_new_open(NULL
, NULL
, options
, flags
, &local_err
);
307 error_reportf_err(local_err
, "Could not open '%s': ", optstr
);
310 blk_set_enable_write_cache(blk
, !writethrough
);
315 static BlockBackend
*img_open_file(const char *filename
,
317 const char *fmt
, int flags
,
318 bool writethrough
, bool quiet
,
322 Error
*local_err
= NULL
;
325 options
= qdict_new();
328 qdict_put_str(options
, "driver", fmt
);
332 qdict_put_bool(options
, BDRV_OPT_FORCE_SHARE
, true);
334 blk
= blk_new_open(filename
, NULL
, options
, flags
, &local_err
);
336 error_reportf_err(local_err
, "Could not open '%s': ", filename
);
339 blk_set_enable_write_cache(blk
, !writethrough
);
345 static int img_add_key_secrets(void *opaque
,
346 const char *name
, const char *value
,
349 QDict
*options
= opaque
;
351 if (g_str_has_suffix(name
, "key-secret")) {
352 qdict_put_str(options
, name
, value
);
359 static BlockBackend
*img_open(bool image_opts
,
360 const char *filename
,
361 const char *fmt
, int flags
, bool writethrough
,
362 bool quiet
, bool force_share
)
368 error_report("--image-opts and --format are mutually exclusive");
371 opts
= qemu_opts_parse_noisily(qemu_find_opts("source"),
376 blk
= img_open_opts(filename
, opts
, flags
, writethrough
, quiet
,
379 blk
= img_open_file(filename
, NULL
, fmt
, flags
, writethrough
, quiet
,
386 static int add_old_style_options(const char *fmt
, QemuOpts
*opts
,
387 const char *base_filename
,
388 const char *base_fmt
)
393 qemu_opt_set(opts
, BLOCK_OPT_BACKING_FILE
, base_filename
, &err
);
395 error_report("Backing file not supported for file format '%s'",
402 qemu_opt_set(opts
, BLOCK_OPT_BACKING_FMT
, base_fmt
, &err
);
404 error_report("Backing file format not supported for file "
413 static int64_t cvtnum(const char *s
)
418 err
= qemu_strtosz(s
, NULL
, &value
);
422 if (value
> INT64_MAX
) {
428 static int img_create(int argc
, char **argv
)
431 uint64_t img_size
= -1;
432 const char *fmt
= "raw";
433 const char *base_fmt
= NULL
;
434 const char *filename
;
435 const char *base_filename
= NULL
;
436 char *options
= NULL
;
437 Error
*local_err
= NULL
;
442 static const struct option long_options
[] = {
443 {"help", no_argument
, 0, 'h'},
444 {"object", required_argument
, 0, OPTION_OBJECT
},
447 c
= getopt_long(argc
, argv
, ":F:b:f:ho:qu",
454 missing_argument(argv
[optind
- 1]);
457 unrecognized_option(argv
[optind
- 1]);
466 base_filename
= optarg
;
472 if (!is_valid_option_list(optarg
)) {
473 error_report("Invalid option list: %s", optarg
);
477 options
= g_strdup(optarg
);
479 char *old_options
= options
;
480 options
= g_strdup_printf("%s,%s", options
, optarg
);
488 flags
|= BDRV_O_NO_BACKING
;
490 case OPTION_OBJECT
: {
492 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
501 /* Get the filename */
502 filename
= (optind
< argc
) ? argv
[optind
] : NULL
;
503 if (options
&& has_help_option(options
)) {
505 return print_block_option_help(filename
, fmt
);
508 if (optind
>= argc
) {
509 error_exit("Expecting image file name");
513 if (qemu_opts_foreach(&qemu_object_opts
,
514 user_creatable_add_opts_foreach
,
515 NULL
, &error_fatal
)) {
519 /* Get image size, if specified */
523 sval
= cvtnum(argv
[optind
++]);
525 if (sval
== -ERANGE
) {
526 error_report("Image size must be less than 8 EiB!");
528 error_report("Invalid image size specified! You may use k, M, "
529 "G, T, P or E suffixes for ");
530 error_report("kilobytes, megabytes, gigabytes, terabytes, "
531 "petabytes and exabytes.");
535 img_size
= (uint64_t)sval
;
537 if (optind
!= argc
) {
538 error_exit("Unexpected argument: %s", argv
[optind
]);
541 bdrv_img_create(filename
, fmt
, base_filename
, base_fmt
,
542 options
, img_size
, flags
, quiet
, &local_err
);
544 error_reportf_err(local_err
, "%s: ", filename
);
556 static void dump_json_image_check(ImageCheck
*check
, bool quiet
)
560 Visitor
*v
= qobject_output_visitor_new(&obj
);
562 visit_type_ImageCheck(v
, NULL
, &check
, &error_abort
);
563 visit_complete(v
, &obj
);
564 str
= qobject_to_json_pretty(obj
);
566 qprintf(quiet
, "%s\n", qstring_get_str(str
));
572 static void dump_human_image_check(ImageCheck
*check
, bool quiet
)
574 if (!(check
->corruptions
|| check
->leaks
|| check
->check_errors
)) {
575 qprintf(quiet
, "No errors were found on the image.\n");
577 if (check
->corruptions
) {
578 qprintf(quiet
, "\n%" PRId64
" errors were found on the image.\n"
579 "Data may be corrupted, or further writes to the image "
586 "\n%" PRId64
" leaked clusters were found on the image.\n"
587 "This means waste of disk space, but no harm to data.\n",
591 if (check
->check_errors
) {
594 " internal errors have occurred during the check.\n",
595 check
->check_errors
);
599 if (check
->total_clusters
!= 0 && check
->allocated_clusters
!= 0) {
600 qprintf(quiet
, "%" PRId64
"/%" PRId64
" = %0.2f%% allocated, "
601 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
602 check
->allocated_clusters
, check
->total_clusters
,
603 check
->allocated_clusters
* 100.0 / check
->total_clusters
,
604 check
->fragmented_clusters
* 100.0 / check
->allocated_clusters
,
605 check
->compressed_clusters
* 100.0 /
606 check
->allocated_clusters
);
609 if (check
->image_end_offset
) {
611 "Image end offset: %" PRId64
"\n", check
->image_end_offset
);
615 static int collect_image_check(BlockDriverState
*bs
,
617 const char *filename
,
622 BdrvCheckResult result
;
624 ret
= bdrv_check(bs
, &result
, fix
);
629 check
->filename
= g_strdup(filename
);
630 check
->format
= g_strdup(bdrv_get_format_name(bs
));
631 check
->check_errors
= result
.check_errors
;
632 check
->corruptions
= result
.corruptions
;
633 check
->has_corruptions
= result
.corruptions
!= 0;
634 check
->leaks
= result
.leaks
;
635 check
->has_leaks
= result
.leaks
!= 0;
636 check
->corruptions_fixed
= result
.corruptions_fixed
;
637 check
->has_corruptions_fixed
= result
.corruptions
!= 0;
638 check
->leaks_fixed
= result
.leaks_fixed
;
639 check
->has_leaks_fixed
= result
.leaks
!= 0;
640 check
->image_end_offset
= result
.image_end_offset
;
641 check
->has_image_end_offset
= result
.image_end_offset
!= 0;
642 check
->total_clusters
= result
.bfi
.total_clusters
;
643 check
->has_total_clusters
= result
.bfi
.total_clusters
!= 0;
644 check
->allocated_clusters
= result
.bfi
.allocated_clusters
;
645 check
->has_allocated_clusters
= result
.bfi
.allocated_clusters
!= 0;
646 check
->fragmented_clusters
= result
.bfi
.fragmented_clusters
;
647 check
->has_fragmented_clusters
= result
.bfi
.fragmented_clusters
!= 0;
648 check
->compressed_clusters
= result
.bfi
.compressed_clusters
;
649 check
->has_compressed_clusters
= result
.bfi
.compressed_clusters
!= 0;
655 * Checks an image for consistency. Exit codes:
657 * 0 - Check completed, image is good
658 * 1 - Check not completed because of internal errors
659 * 2 - Check completed, image is corrupted
660 * 3 - Check completed, image has leaked clusters, but is good otherwise
661 * 63 - Checks are not supported by the image format
663 static int img_check(int argc
, char **argv
)
666 OutputFormat output_format
= OFORMAT_HUMAN
;
667 const char *filename
, *fmt
, *output
, *cache
;
669 BlockDriverState
*bs
;
671 int flags
= BDRV_O_CHECK
;
675 bool image_opts
= false;
676 bool force_share
= false;
680 cache
= BDRV_DEFAULT_CACHE
;
683 int option_index
= 0;
684 static const struct option long_options
[] = {
685 {"help", no_argument
, 0, 'h'},
686 {"format", required_argument
, 0, 'f'},
687 {"repair", required_argument
, 0, 'r'},
688 {"output", required_argument
, 0, OPTION_OUTPUT
},
689 {"object", required_argument
, 0, OPTION_OBJECT
},
690 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
691 {"force-share", no_argument
, 0, 'U'},
694 c
= getopt_long(argc
, argv
, ":hf:r:T:qU",
695 long_options
, &option_index
);
701 missing_argument(argv
[optind
- 1]);
704 unrecognized_option(argv
[optind
- 1]);
713 flags
|= BDRV_O_RDWR
;
715 if (!strcmp(optarg
, "leaks")) {
716 fix
= BDRV_FIX_LEAKS
;
717 } else if (!strcmp(optarg
, "all")) {
718 fix
= BDRV_FIX_LEAKS
| BDRV_FIX_ERRORS
;
720 error_exit("Unknown option value for -r "
721 "(expecting 'leaks' or 'all'): %s", optarg
);
736 case OPTION_OBJECT
: {
738 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
744 case OPTION_IMAGE_OPTS
:
749 if (optind
!= argc
- 1) {
750 error_exit("Expecting one image file name");
752 filename
= argv
[optind
++];
754 if (output
&& !strcmp(output
, "json")) {
755 output_format
= OFORMAT_JSON
;
756 } else if (output
&& !strcmp(output
, "human")) {
757 output_format
= OFORMAT_HUMAN
;
759 error_report("--output must be used with human or json as argument.");
763 if (qemu_opts_foreach(&qemu_object_opts
,
764 user_creatable_add_opts_foreach
,
765 NULL
, &error_fatal
)) {
769 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
771 error_report("Invalid source cache option: %s", cache
);
775 blk
= img_open(image_opts
, filename
, fmt
, flags
, writethrough
, quiet
,
782 check
= g_new0(ImageCheck
, 1);
783 ret
= collect_image_check(bs
, check
, filename
, fmt
, fix
);
785 if (ret
== -ENOTSUP
) {
786 error_report("This image format does not support checks");
791 if (check
->corruptions_fixed
|| check
->leaks_fixed
) {
792 int corruptions_fixed
, leaks_fixed
;
794 leaks_fixed
= check
->leaks_fixed
;
795 corruptions_fixed
= check
->corruptions_fixed
;
797 if (output_format
== OFORMAT_HUMAN
) {
799 "The following inconsistencies were found and repaired:\n\n"
800 " %" PRId64
" leaked clusters\n"
801 " %" PRId64
" corruptions\n\n"
802 "Double checking the fixed image now...\n",
804 check
->corruptions_fixed
);
807 ret
= collect_image_check(bs
, check
, filename
, fmt
, 0);
809 check
->leaks_fixed
= leaks_fixed
;
810 check
->corruptions_fixed
= corruptions_fixed
;
814 switch (output_format
) {
816 dump_human_image_check(check
, quiet
);
819 dump_json_image_check(check
, quiet
);
824 if (ret
|| check
->check_errors
) {
826 error_report("Check failed: %s", strerror(-ret
));
828 error_report("Check failed");
834 if (check
->corruptions
) {
836 } else if (check
->leaks
) {
843 qapi_free_ImageCheck(check
);
848 typedef struct CommonBlockJobCBInfo
{
849 BlockDriverState
*bs
;
851 } CommonBlockJobCBInfo
;
853 static void common_block_job_cb(void *opaque
, int ret
)
855 CommonBlockJobCBInfo
*cbi
= opaque
;
858 error_setg_errno(cbi
->errp
, -ret
, "Block job failed");
862 static void run_block_job(BlockJob
*job
, Error
**errp
)
864 AioContext
*aio_context
= blk_get_aio_context(job
->blk
);
867 aio_context_acquire(aio_context
);
870 float progress
= 0.0f
;
871 aio_poll(aio_context
, true);
872 if (job
->job
.progress_total
) {
873 progress
= (float)job
->job
.progress_current
/
874 job
->job
.progress_total
* 100.f
;
876 qemu_progress_print(progress
, 0);
877 } while (!job_is_ready(&job
->job
) && !job_is_completed(&job
->job
));
879 if (!job_is_completed(&job
->job
)) {
880 ret
= job_complete_sync(&job
->job
, errp
);
884 job_unref(&job
->job
);
885 aio_context_release(aio_context
);
887 /* publish completion progress only when success */
889 qemu_progress_print(100.f
, 0);
893 static int img_commit(int argc
, char **argv
)
896 const char *filename
, *fmt
, *cache
, *base
;
898 BlockDriverState
*bs
, *base_bs
;
900 bool progress
= false, quiet
= false, drop
= false;
902 Error
*local_err
= NULL
;
903 CommonBlockJobCBInfo cbi
;
904 bool image_opts
= false;
905 AioContext
*aio_context
;
908 cache
= BDRV_DEFAULT_CACHE
;
911 static const struct option long_options
[] = {
912 {"help", no_argument
, 0, 'h'},
913 {"object", required_argument
, 0, OPTION_OBJECT
},
914 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
917 c
= getopt_long(argc
, argv
, ":f:ht:b:dpq",
924 missing_argument(argv
[optind
- 1]);
927 unrecognized_option(argv
[optind
- 1]);
952 case OPTION_OBJECT
: {
954 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
960 case OPTION_IMAGE_OPTS
:
966 /* Progress is not shown in Quiet mode */
971 if (optind
!= argc
- 1) {
972 error_exit("Expecting one image file name");
974 filename
= argv
[optind
++];
976 if (qemu_opts_foreach(&qemu_object_opts
,
977 user_creatable_add_opts_foreach
,
978 NULL
, &error_fatal
)) {
982 flags
= BDRV_O_RDWR
| BDRV_O_UNMAP
;
983 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
985 error_report("Invalid cache option: %s", cache
);
989 blk
= img_open(image_opts
, filename
, fmt
, flags
, writethrough
, quiet
,
996 qemu_progress_init(progress
, 1.f
);
997 qemu_progress_print(0.f
, 100);
1000 base_bs
= bdrv_find_backing_image(bs
, base
);
1002 error_setg(&local_err
,
1003 "Did not find '%s' in the backing chain of '%s'",
1008 /* This is different from QMP, which by default uses the deepest file in
1009 * the backing chain (i.e., the very base); however, the traditional
1010 * behavior of qemu-img commit is using the immediate backing file. */
1011 base_bs
= backing_bs(bs
);
1013 error_setg(&local_err
, "Image does not have a backing file");
1018 cbi
= (CommonBlockJobCBInfo
){
1023 aio_context
= bdrv_get_aio_context(bs
);
1024 aio_context_acquire(aio_context
);
1025 commit_active_start("commit", bs
, base_bs
, JOB_DEFAULT
, 0,
1026 BLOCKDEV_ON_ERROR_REPORT
, NULL
, common_block_job_cb
,
1027 &cbi
, false, &local_err
);
1028 aio_context_release(aio_context
);
1033 /* When the block job completes, the BlockBackend reference will point to
1034 * the old backing file. In order to avoid that the top image is already
1035 * deleted, so we can still empty it afterwards, increment the reference
1036 * counter here preemptively. */
1041 job
= block_job_get("commit");
1043 run_block_job(job
, &local_err
);
1048 if (!drop
&& bs
->drv
->bdrv_make_empty
) {
1049 ret
= bs
->drv
->bdrv_make_empty(bs
);
1051 error_setg_errno(&local_err
, -ret
, "Could not empty %s",
1063 qemu_progress_end();
1068 error_report_err(local_err
);
1072 qprintf(quiet
, "Image committed.\n");
1077 * Returns -1 if 'buf' contains only zeroes, otherwise the byte index
1078 * of the first sector boundary within buf where the sector contains a
1079 * non-zero byte. This function is robust to a buffer that is not
1082 static int64_t find_nonzero(const uint8_t *buf
, int64_t n
)
1085 int64_t end
= QEMU_ALIGN_DOWN(n
, BDRV_SECTOR_SIZE
);
1087 for (i
= 0; i
< end
; i
+= BDRV_SECTOR_SIZE
) {
1088 if (!buffer_is_zero(buf
+ i
, BDRV_SECTOR_SIZE
)) {
1092 if (i
< n
&& !buffer_is_zero(buf
+ i
, n
- end
)) {
1099 * Returns true iff the first sector pointed to by 'buf' contains at least
1102 * 'pnum' is set to the number of sectors (including and immediately following
1103 * the first one) that are known to be in the same allocated/unallocated state.
1104 * The function will try to align the end offset to alignment boundaries so
1105 * that the request will at least end aligned and consequtive requests will
1106 * also start at an aligned offset.
1108 static int is_allocated_sectors(const uint8_t *buf
, int n
, int *pnum
,
1109 int64_t sector_num
, int alignment
)
1118 is_zero
= buffer_is_zero(buf
, 512);
1119 for(i
= 1; i
< n
; i
++) {
1121 if (is_zero
!= buffer_is_zero(buf
, 512)) {
1126 tail
= (sector_num
+ i
) & (alignment
- 1);
1128 if (is_zero
&& i
<= tail
) {
1129 /* treat unallocated areas which only consist
1130 * of a small tail as allocated. */
1134 /* align up end offset of allocated areas. */
1135 i
+= alignment
- tail
;
1138 /* align down end offset of zero areas. */
1147 * Like is_allocated_sectors, but if the buffer starts with a used sector,
1148 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1149 * breaking up write requests for only small sparse areas.
1151 static int is_allocated_sectors_min(const uint8_t *buf
, int n
, int *pnum
,
1152 int min
, int64_t sector_num
, int alignment
)
1155 int num_checked
, num_used
;
1161 ret
= is_allocated_sectors(buf
, n
, pnum
, sector_num
, alignment
);
1167 buf
+= BDRV_SECTOR_SIZE
* *pnum
;
1169 sector_num
+= *pnum
;
1170 num_checked
= num_used
;
1173 ret
= is_allocated_sectors(buf
, n
, pnum
, sector_num
, alignment
);
1175 buf
+= BDRV_SECTOR_SIZE
* *pnum
;
1177 sector_num
+= *pnum
;
1178 num_checked
+= *pnum
;
1180 num_used
= num_checked
;
1181 } else if (*pnum
>= min
) {
1191 * Compares two buffers sector by sector. Returns 0 if the first
1192 * sector of each buffer matches, non-zero otherwise.
1194 * pnum is set to the sector-aligned size of the buffer prefix that
1195 * has the same matching status as the first sector.
1197 static int compare_buffers(const uint8_t *buf1
, const uint8_t *buf2
,
1198 int64_t bytes
, int64_t *pnum
)
1201 int64_t i
= MIN(bytes
, BDRV_SECTOR_SIZE
);
1205 res
= !!memcmp(buf1
, buf2
, i
);
1207 int64_t len
= MIN(bytes
- i
, BDRV_SECTOR_SIZE
);
1209 if (!!memcmp(buf1
+ i
, buf2
+ i
, len
) != res
) {
1219 #define IO_BUF_SIZE (2 * 1024 * 1024)
1222 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1224 * Intended for use by 'qemu-img compare': Returns 0 in case sectors are
1225 * filled with 0, 1 if sectors contain non-zero data (this is a comparison
1226 * failure), and 4 on error (the exit status for read errors), after emitting
1229 * @param blk: BlockBackend for the image
1230 * @param offset: Starting offset to check
1231 * @param bytes: Number of bytes to check
1232 * @param filename: Name of disk file we are checking (logging purpose)
1233 * @param buffer: Allocated buffer for storing read data
1234 * @param quiet: Flag for quiet mode
1236 static int check_empty_sectors(BlockBackend
*blk
, int64_t offset
,
1237 int64_t bytes
, const char *filename
,
1238 uint8_t *buffer
, bool quiet
)
1243 ret
= blk_pread(blk
, offset
, buffer
, bytes
);
1245 error_report("Error while reading offset %" PRId64
" of %s: %s",
1246 offset
, filename
, strerror(-ret
));
1249 idx
= find_nonzero(buffer
, bytes
);
1251 qprintf(quiet
, "Content mismatch at offset %" PRId64
"!\n",
1260 * Compares two images. Exit codes:
1262 * 0 - Images are identical
1264 * >1 - Error occurred
1266 static int img_compare(int argc
, char **argv
)
1268 const char *fmt1
= NULL
, *fmt2
= NULL
, *cache
, *filename1
, *filename2
;
1269 BlockBackend
*blk1
, *blk2
;
1270 BlockDriverState
*bs1
, *bs2
;
1271 int64_t total_size1
, total_size2
;
1272 uint8_t *buf1
= NULL
, *buf2
= NULL
;
1273 int64_t pnum1
, pnum2
;
1274 int allocated1
, allocated2
;
1275 int ret
= 0; /* return value - 0 Ident, 1 Different, >1 Error */
1276 bool progress
= false, quiet
= false, strict
= false;
1283 uint64_t progress_base
;
1284 bool image_opts
= false;
1285 bool force_share
= false;
1287 cache
= BDRV_DEFAULT_CACHE
;
1289 static const struct option long_options
[] = {
1290 {"help", no_argument
, 0, 'h'},
1291 {"object", required_argument
, 0, OPTION_OBJECT
},
1292 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
1293 {"force-share", no_argument
, 0, 'U'},
1296 c
= getopt_long(argc
, argv
, ":hf:F:T:pqsU",
1297 long_options
, NULL
);
1303 missing_argument(argv
[optind
- 1]);
1306 unrecognized_option(argv
[optind
- 1]);
1332 case OPTION_OBJECT
: {
1334 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
1341 case OPTION_IMAGE_OPTS
:
1347 /* Progress is not shown in Quiet mode */
1353 if (optind
!= argc
- 2) {
1354 error_exit("Expecting two image file names");
1356 filename1
= argv
[optind
++];
1357 filename2
= argv
[optind
++];
1359 if (qemu_opts_foreach(&qemu_object_opts
,
1360 user_creatable_add_opts_foreach
,
1361 NULL
, &error_fatal
)) {
1366 /* Initialize before goto out */
1367 qemu_progress_init(progress
, 2.0);
1370 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
1372 error_report("Invalid source cache option: %s", cache
);
1377 blk1
= img_open(image_opts
, filename1
, fmt1
, flags
, writethrough
, quiet
,
1384 blk2
= img_open(image_opts
, filename2
, fmt2
, flags
, writethrough
, quiet
,
1393 buf1
= blk_blockalign(blk1
, IO_BUF_SIZE
);
1394 buf2
= blk_blockalign(blk2
, IO_BUF_SIZE
);
1395 total_size1
= blk_getlength(blk1
);
1396 if (total_size1
< 0) {
1397 error_report("Can't get size of %s: %s",
1398 filename1
, strerror(-total_size1
));
1402 total_size2
= blk_getlength(blk2
);
1403 if (total_size2
< 0) {
1404 error_report("Can't get size of %s: %s",
1405 filename2
, strerror(-total_size2
));
1409 total_size
= MIN(total_size1
, total_size2
);
1410 progress_base
= MAX(total_size1
, total_size2
);
1412 qemu_progress_print(0, 100);
1414 if (strict
&& total_size1
!= total_size2
) {
1416 qprintf(quiet
, "Strict mode: Image size mismatch!\n");
1420 while (offset
< total_size
) {
1421 int status1
, status2
;
1423 status1
= bdrv_block_status_above(bs1
, NULL
, offset
,
1424 total_size1
- offset
, &pnum1
, NULL
,
1428 error_report("Sector allocation test failed for %s", filename1
);
1431 allocated1
= status1
& BDRV_BLOCK_ALLOCATED
;
1433 status2
= bdrv_block_status_above(bs2
, NULL
, offset
,
1434 total_size2
- offset
, &pnum2
, NULL
,
1438 error_report("Sector allocation test failed for %s", filename2
);
1441 allocated2
= status2
& BDRV_BLOCK_ALLOCATED
;
1443 assert(pnum1
&& pnum2
);
1444 chunk
= MIN(pnum1
, pnum2
);
1447 if (status1
!= status2
) {
1449 qprintf(quiet
, "Strict mode: Offset %" PRId64
1450 " block status mismatch!\n", offset
);
1454 if ((status1
& BDRV_BLOCK_ZERO
) && (status2
& BDRV_BLOCK_ZERO
)) {
1456 } else if (allocated1
== allocated2
) {
1460 chunk
= MIN(chunk
, IO_BUF_SIZE
);
1461 ret
= blk_pread(blk1
, offset
, buf1
, chunk
);
1463 error_report("Error while reading offset %" PRId64
1465 offset
, filename1
, strerror(-ret
));
1469 ret
= blk_pread(blk2
, offset
, buf2
, chunk
);
1471 error_report("Error while reading offset %" PRId64
1473 offset
, filename2
, strerror(-ret
));
1477 ret
= compare_buffers(buf1
, buf2
, chunk
, &pnum
);
1478 if (ret
|| pnum
!= chunk
) {
1479 qprintf(quiet
, "Content mismatch at offset %" PRId64
"!\n",
1480 offset
+ (ret
? 0 : pnum
));
1486 chunk
= MIN(chunk
, IO_BUF_SIZE
);
1488 ret
= check_empty_sectors(blk1
, offset
, chunk
,
1489 filename1
, buf1
, quiet
);
1491 ret
= check_empty_sectors(blk2
, offset
, chunk
,
1492 filename2
, buf1
, quiet
);
1499 qemu_progress_print(((float) chunk
/ progress_base
) * 100, 100);
1502 if (total_size1
!= total_size2
) {
1503 BlockBackend
*blk_over
;
1504 const char *filename_over
;
1506 qprintf(quiet
, "Warning: Image size mismatch!\n");
1507 if (total_size1
> total_size2
) {
1509 filename_over
= filename1
;
1512 filename_over
= filename2
;
1515 while (offset
< progress_base
) {
1516 ret
= bdrv_block_status_above(blk_bs(blk_over
), NULL
, offset
,
1517 progress_base
- offset
, &chunk
,
1521 error_report("Sector allocation test failed for %s",
1526 if (ret
& BDRV_BLOCK_ALLOCATED
&& !(ret
& BDRV_BLOCK_ZERO
)) {
1527 chunk
= MIN(chunk
, IO_BUF_SIZE
);
1528 ret
= check_empty_sectors(blk_over
, offset
, chunk
,
1529 filename_over
, buf1
, quiet
);
1535 qemu_progress_print(((float) chunk
/ progress_base
) * 100, 100);
1539 qprintf(quiet
, "Images are identical.\n");
1549 qemu_progress_end();
1554 enum ImgConvertBlockStatus
{
1560 #define MAX_COROUTINES 16
1562 typedef struct ImgConvertState
{
1564 int64_t *src_sectors
;
1566 int64_t total_sectors
;
1567 int64_t allocated_sectors
;
1568 int64_t allocated_done
;
1571 enum ImgConvertBlockStatus status
;
1572 int64_t sector_next_status
;
1573 BlockBackend
*target
;
1576 bool unallocated_blocks_are_zero
;
1577 bool target_has_backing
;
1578 int64_t target_backing_sectors
; /* negative if unknown */
1583 size_t cluster_sectors
;
1585 long num_coroutines
;
1586 int running_coroutines
;
1587 Coroutine
*co
[MAX_COROUTINES
];
1588 int64_t wait_sector_num
[MAX_COROUTINES
];
1593 static void convert_select_part(ImgConvertState
*s
, int64_t sector_num
,
1594 int *src_cur
, int64_t *src_cur_offset
)
1597 *src_cur_offset
= 0;
1598 while (sector_num
- *src_cur_offset
>= s
->src_sectors
[*src_cur
]) {
1599 *src_cur_offset
+= s
->src_sectors
[*src_cur
];
1601 assert(*src_cur
< s
->src_num
);
1605 static int convert_iteration_sectors(ImgConvertState
*s
, int64_t sector_num
)
1607 int64_t src_cur_offset
;
1608 int ret
, n
, src_cur
;
1609 bool post_backing_zero
= false;
1611 convert_select_part(s
, sector_num
, &src_cur
, &src_cur_offset
);
1613 assert(s
->total_sectors
> sector_num
);
1614 n
= MIN(s
->total_sectors
- sector_num
, BDRV_REQUEST_MAX_SECTORS
);
1616 if (s
->target_backing_sectors
>= 0) {
1617 if (sector_num
>= s
->target_backing_sectors
) {
1618 post_backing_zero
= s
->unallocated_blocks_are_zero
;
1619 } else if (sector_num
+ n
> s
->target_backing_sectors
) {
1620 /* Split requests around target_backing_sectors (because
1621 * starting from there, zeros are handled differently) */
1622 n
= s
->target_backing_sectors
- sector_num
;
1626 if (s
->sector_next_status
<= sector_num
) {
1627 int64_t count
= n
* BDRV_SECTOR_SIZE
;
1629 if (s
->target_has_backing
) {
1631 ret
= bdrv_block_status(blk_bs(s
->src
[src_cur
]),
1632 (sector_num
- src_cur_offset
) *
1634 count
, &count
, NULL
, NULL
);
1636 ret
= bdrv_block_status_above(blk_bs(s
->src
[src_cur
]), NULL
,
1637 (sector_num
- src_cur_offset
) *
1639 count
, &count
, NULL
, NULL
);
1642 error_report("error while reading block status of sector %" PRId64
1643 ": %s", sector_num
, strerror(-ret
));
1646 n
= DIV_ROUND_UP(count
, BDRV_SECTOR_SIZE
);
1648 if (ret
& BDRV_BLOCK_ZERO
) {
1649 s
->status
= post_backing_zero
? BLK_BACKING_FILE
: BLK_ZERO
;
1650 } else if (ret
& BDRV_BLOCK_DATA
) {
1651 s
->status
= BLK_DATA
;
1653 s
->status
= s
->target_has_backing
? BLK_BACKING_FILE
: BLK_DATA
;
1656 s
->sector_next_status
= sector_num
+ n
;
1659 n
= MIN(n
, s
->sector_next_status
- sector_num
);
1660 if (s
->status
== BLK_DATA
) {
1661 n
= MIN(n
, s
->buf_sectors
);
1664 /* We need to write complete clusters for compressed images, so if an
1665 * unallocated area is shorter than that, we must consider the whole
1666 * cluster allocated. */
1667 if (s
->compressed
) {
1668 if (n
< s
->cluster_sectors
) {
1669 n
= MIN(s
->cluster_sectors
, s
->total_sectors
- sector_num
);
1670 s
->status
= BLK_DATA
;
1672 n
= QEMU_ALIGN_DOWN(n
, s
->cluster_sectors
);
1679 static int coroutine_fn
convert_co_read(ImgConvertState
*s
, int64_t sector_num
,
1680 int nb_sectors
, uint8_t *buf
)
1684 assert(nb_sectors
<= s
->buf_sectors
);
1685 while (nb_sectors
> 0) {
1688 int64_t bs_sectors
, src_cur_offset
;
1690 /* In the case of compression with multiple source files, we can get a
1691 * nb_sectors that spreads into the next part. So we must be able to
1692 * read across multiple BDSes for one convert_read() call. */
1693 convert_select_part(s
, sector_num
, &src_cur
, &src_cur_offset
);
1694 blk
= s
->src
[src_cur
];
1695 bs_sectors
= s
->src_sectors
[src_cur
];
1697 n
= MIN(nb_sectors
, bs_sectors
- (sector_num
- src_cur_offset
));
1700 blk
, (sector_num
- src_cur_offset
) << BDRV_SECTOR_BITS
,
1701 n
<< BDRV_SECTOR_BITS
, buf
, 0);
1708 buf
+= n
* BDRV_SECTOR_SIZE
;
1715 static int coroutine_fn
convert_co_write(ImgConvertState
*s
, int64_t sector_num
,
1716 int nb_sectors
, uint8_t *buf
,
1717 enum ImgConvertBlockStatus status
)
1721 while (nb_sectors
> 0) {
1723 BdrvRequestFlags flags
= s
->compressed
? BDRV_REQ_WRITE_COMPRESSED
: 0;
1726 case BLK_BACKING_FILE
:
1727 /* If we have a backing file, leave clusters unallocated that are
1728 * unallocated in the source image, so that the backing file is
1729 * visible at the respective offset. */
1730 assert(s
->target_has_backing
);
1734 /* If we're told to keep the target fully allocated (-S 0) or there
1735 * is real non-zero data, we must write it. Otherwise we can treat
1736 * it as zero sectors.
1737 * Compressed clusters need to be written as a whole, so in that
1738 * case we can only save the write if the buffer is completely
1740 if (!s
->min_sparse
||
1742 is_allocated_sectors_min(buf
, n
, &n
, s
->min_sparse
,
1743 sector_num
, s
->alignment
)) ||
1745 !buffer_is_zero(buf
, n
* BDRV_SECTOR_SIZE
)))
1747 ret
= blk_co_pwrite(s
->target
, sector_num
<< BDRV_SECTOR_BITS
,
1748 n
<< BDRV_SECTOR_BITS
, buf
, flags
);
1757 if (s
->has_zero_init
) {
1758 assert(!s
->target_has_backing
);
1761 ret
= blk_co_pwrite_zeroes(s
->target
,
1762 sector_num
<< BDRV_SECTOR_BITS
,
1763 n
<< BDRV_SECTOR_BITS
,
1764 BDRV_REQ_MAY_UNMAP
);
1773 buf
+= n
* BDRV_SECTOR_SIZE
;
1779 static int coroutine_fn
convert_co_copy_range(ImgConvertState
*s
, int64_t sector_num
,
1784 while (nb_sectors
> 0) {
1787 int64_t bs_sectors
, src_cur_offset
;
1790 convert_select_part(s
, sector_num
, &src_cur
, &src_cur_offset
);
1791 offset
= (sector_num
- src_cur_offset
) << BDRV_SECTOR_BITS
;
1792 blk
= s
->src
[src_cur
];
1793 bs_sectors
= s
->src_sectors
[src_cur
];
1795 n
= MIN(nb_sectors
, bs_sectors
- (sector_num
- src_cur_offset
));
1797 ret
= blk_co_copy_range(blk
, offset
, s
->target
,
1798 sector_num
<< BDRV_SECTOR_BITS
,
1799 n
<< BDRV_SECTOR_BITS
, 0, 0);
1810 static void coroutine_fn
convert_co_do_copy(void *opaque
)
1812 ImgConvertState
*s
= opaque
;
1813 uint8_t *buf
= NULL
;
1817 for (i
= 0; i
< s
->num_coroutines
; i
++) {
1818 if (s
->co
[i
] == qemu_coroutine_self()) {
1825 s
->running_coroutines
++;
1826 buf
= blk_blockalign(s
->target
, s
->buf_sectors
* BDRV_SECTOR_SIZE
);
1831 enum ImgConvertBlockStatus status
;
1834 qemu_co_mutex_lock(&s
->lock
);
1835 if (s
->ret
!= -EINPROGRESS
|| s
->sector_num
>= s
->total_sectors
) {
1836 qemu_co_mutex_unlock(&s
->lock
);
1839 n
= convert_iteration_sectors(s
, s
->sector_num
);
1841 qemu_co_mutex_unlock(&s
->lock
);
1845 /* save current sector and allocation status to local variables */
1846 sector_num
= s
->sector_num
;
1848 if (!s
->min_sparse
&& s
->status
== BLK_ZERO
) {
1849 n
= MIN(n
, s
->buf_sectors
);
1851 /* increment global sector counter so that other coroutines can
1852 * already continue reading beyond this request */
1854 qemu_co_mutex_unlock(&s
->lock
);
1856 if (status
== BLK_DATA
|| (!s
->min_sparse
&& status
== BLK_ZERO
)) {
1857 s
->allocated_done
+= n
;
1858 qemu_progress_print(100.0 * s
->allocated_done
/
1859 s
->allocated_sectors
, 0);
1863 copy_range
= s
->copy_range
&& s
->status
== BLK_DATA
;
1864 if (status
== BLK_DATA
&& !copy_range
) {
1865 ret
= convert_co_read(s
, sector_num
, n
, buf
);
1867 error_report("error while reading sector %" PRId64
1868 ": %s", sector_num
, strerror(-ret
));
1871 } else if (!s
->min_sparse
&& status
== BLK_ZERO
) {
1873 memset(buf
, 0x00, n
* BDRV_SECTOR_SIZE
);
1876 if (s
->wr_in_order
) {
1877 /* keep writes in order */
1878 while (s
->wr_offs
!= sector_num
&& s
->ret
== -EINPROGRESS
) {
1879 s
->wait_sector_num
[index
] = sector_num
;
1880 qemu_coroutine_yield();
1882 s
->wait_sector_num
[index
] = -1;
1885 if (s
->ret
== -EINPROGRESS
) {
1887 ret
= convert_co_copy_range(s
, sector_num
, n
);
1889 s
->copy_range
= false;
1893 ret
= convert_co_write(s
, sector_num
, n
, buf
, status
);
1896 error_report("error while writing sector %" PRId64
1897 ": %s", sector_num
, strerror(-ret
));
1902 if (s
->wr_in_order
) {
1903 /* reenter the coroutine that might have waited
1904 * for this write to complete */
1905 s
->wr_offs
= sector_num
+ n
;
1906 for (i
= 0; i
< s
->num_coroutines
; i
++) {
1907 if (s
->co
[i
] && s
->wait_sector_num
[i
] == s
->wr_offs
) {
1909 * A -> B -> A cannot occur because A has
1910 * s->wait_sector_num[i] == -1 during A -> B. Therefore
1911 * B will never enter A during this time window.
1913 qemu_coroutine_enter(s
->co
[i
]);
1921 s
->co
[index
] = NULL
;
1922 s
->running_coroutines
--;
1923 if (!s
->running_coroutines
&& s
->ret
== -EINPROGRESS
) {
1924 /* the convert job finished successfully */
1929 static int convert_do_copy(ImgConvertState
*s
)
1932 int64_t sector_num
= 0;
1934 /* Check whether we have zero initialisation or can get it efficiently */
1935 s
->has_zero_init
= s
->min_sparse
&& !s
->target_has_backing
1936 ? bdrv_has_zero_init(blk_bs(s
->target
))
1939 if (!s
->has_zero_init
&& !s
->target_has_backing
&&
1940 bdrv_can_write_zeroes_with_unmap(blk_bs(s
->target
)))
1942 ret
= blk_make_zero(s
->target
, BDRV_REQ_MAY_UNMAP
| BDRV_REQ_NO_FALLBACK
);
1944 s
->has_zero_init
= true;
1948 /* Allocate buffer for copied data. For compressed images, only one cluster
1949 * can be copied at a time. */
1950 if (s
->compressed
) {
1951 if (s
->cluster_sectors
<= 0 || s
->cluster_sectors
> s
->buf_sectors
) {
1952 error_report("invalid cluster size");
1955 s
->buf_sectors
= s
->cluster_sectors
;
1958 while (sector_num
< s
->total_sectors
) {
1959 n
= convert_iteration_sectors(s
, sector_num
);
1963 if (s
->status
== BLK_DATA
|| (!s
->min_sparse
&& s
->status
== BLK_ZERO
))
1965 s
->allocated_sectors
+= n
;
1971 s
->sector_next_status
= 0;
1972 s
->ret
= -EINPROGRESS
;
1974 qemu_co_mutex_init(&s
->lock
);
1975 for (i
= 0; i
< s
->num_coroutines
; i
++) {
1976 s
->co
[i
] = qemu_coroutine_create(convert_co_do_copy
, s
);
1977 s
->wait_sector_num
[i
] = -1;
1978 qemu_coroutine_enter(s
->co
[i
]);
1981 while (s
->running_coroutines
) {
1982 main_loop_wait(false);
1985 if (s
->compressed
&& !s
->ret
) {
1986 /* signal EOF to align */
1987 ret
= blk_pwrite_compressed(s
->target
, 0, NULL
, 0);
1996 #define MAX_BUF_SECTORS 32768
1998 static int img_convert(int argc
, char **argv
)
2000 int c
, bs_i
, flags
, src_flags
= 0;
2001 const char *fmt
= NULL
, *out_fmt
= NULL
, *cache
= "unsafe",
2002 *src_cache
= BDRV_DEFAULT_CACHE
, *out_baseimg
= NULL
,
2003 *out_filename
, *out_baseimg_param
, *snapshot_name
= NULL
;
2004 BlockDriver
*drv
= NULL
, *proto_drv
= NULL
;
2005 BlockDriverInfo bdi
;
2006 BlockDriverState
*out_bs
;
2007 QemuOpts
*opts
= NULL
, *sn_opts
= NULL
;
2008 QemuOptsList
*create_opts
= NULL
;
2009 QDict
*open_opts
= NULL
;
2010 char *options
= NULL
;
2011 Error
*local_err
= NULL
;
2012 bool writethrough
, src_writethrough
, quiet
= false, image_opts
= false,
2013 skip_create
= false, progress
= false, tgt_image_opts
= false;
2014 int64_t ret
= -EINVAL
;
2015 bool force_share
= false;
2016 bool explict_min_sparse
= false;
2018 ImgConvertState s
= (ImgConvertState
) {
2019 /* Need at least 4k of zeros for sparse detection */
2021 .copy_range
= false,
2022 .buf_sectors
= IO_BUF_SIZE
/ BDRV_SECTOR_SIZE
,
2023 .wr_in_order
= true,
2024 .num_coroutines
= 8,
2028 static const struct option long_options
[] = {
2029 {"help", no_argument
, 0, 'h'},
2030 {"object", required_argument
, 0, OPTION_OBJECT
},
2031 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
2032 {"force-share", no_argument
, 0, 'U'},
2033 {"target-image-opts", no_argument
, 0, OPTION_TARGET_IMAGE_OPTS
},
2036 c
= getopt_long(argc
, argv
, ":hf:O:B:Cco:l:S:pt:T:qnm:WU",
2037 long_options
, NULL
);
2043 missing_argument(argv
[optind
- 1]);
2046 unrecognized_option(argv
[optind
- 1]);
2058 out_baseimg
= optarg
;
2061 s
.copy_range
= true;
2064 s
.compressed
= true;
2067 if (!is_valid_option_list(optarg
)) {
2068 error_report("Invalid option list: %s", optarg
);
2072 options
= g_strdup(optarg
);
2074 char *old_options
= options
;
2075 options
= g_strdup_printf("%s,%s", options
, optarg
);
2076 g_free(old_options
);
2080 if (strstart(optarg
, SNAPSHOT_OPT_BASE
, NULL
)) {
2081 sn_opts
= qemu_opts_parse_noisily(&internal_snapshot_opts
,
2084 error_report("Failed in parsing snapshot param '%s'",
2089 snapshot_name
= optarg
;
2096 sval
= cvtnum(optarg
);
2097 if (sval
< 0 || sval
& (BDRV_SECTOR_SIZE
- 1) ||
2098 sval
/ BDRV_SECTOR_SIZE
> MAX_BUF_SECTORS
) {
2099 error_report("Invalid buffer size for sparse output specified. "
2100 "Valid sizes are multiples of %llu up to %llu. Select "
2101 "0 to disable sparse detection (fully allocates output).",
2102 BDRV_SECTOR_SIZE
, MAX_BUF_SECTORS
* BDRV_SECTOR_SIZE
);
2106 s
.min_sparse
= sval
/ BDRV_SECTOR_SIZE
;
2107 explict_min_sparse
= true;
2126 if (qemu_strtol(optarg
, NULL
, 0, &s
.num_coroutines
) ||
2127 s
.num_coroutines
< 1 || s
.num_coroutines
> MAX_COROUTINES
) {
2128 error_report("Invalid number of coroutines. Allowed number of"
2129 " coroutines is between 1 and %d", MAX_COROUTINES
);
2134 s
.wr_in_order
= false;
2139 case OPTION_OBJECT
: {
2140 QemuOpts
*object_opts
;
2141 object_opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
2148 case OPTION_IMAGE_OPTS
:
2151 case OPTION_TARGET_IMAGE_OPTS
:
2152 tgt_image_opts
= true;
2157 if (!out_fmt
&& !tgt_image_opts
) {
2161 if (qemu_opts_foreach(&qemu_object_opts
,
2162 user_creatable_add_opts_foreach
,
2163 NULL
, &error_fatal
)) {
2167 if (s
.compressed
&& s
.copy_range
) {
2168 error_report("Cannot enable copy offloading when -c is used");
2172 if (explict_min_sparse
&& s
.copy_range
) {
2173 error_report("Cannot enable copy offloading when -S is used");
2177 if (tgt_image_opts
&& !skip_create
) {
2178 error_report("--target-image-opts requires use of -n flag");
2182 s
.src_num
= argc
- optind
- 1;
2183 out_filename
= s
.src_num
>= 1 ? argv
[argc
- 1] : NULL
;
2185 if (options
&& has_help_option(options
)) {
2187 ret
= print_block_option_help(out_filename
, out_fmt
);
2190 error_report("Option help requires a format be specified");
2195 if (s
.src_num
< 1) {
2196 error_report("Must specify image file name");
2201 /* ret is still -EINVAL until here */
2202 ret
= bdrv_parse_cache_mode(src_cache
, &src_flags
, &src_writethrough
);
2204 error_report("Invalid source cache option: %s", src_cache
);
2208 /* Initialize before goto out */
2212 qemu_progress_init(progress
, 1.0);
2213 qemu_progress_print(0, 100);
2215 s
.src
= g_new0(BlockBackend
*, s
.src_num
);
2216 s
.src_sectors
= g_new(int64_t, s
.src_num
);
2218 for (bs_i
= 0; bs_i
< s
.src_num
; bs_i
++) {
2219 s
.src
[bs_i
] = img_open(image_opts
, argv
[optind
+ bs_i
],
2220 fmt
, src_flags
, src_writethrough
, quiet
,
2226 s
.src_sectors
[bs_i
] = blk_nb_sectors(s
.src
[bs_i
]);
2227 if (s
.src_sectors
[bs_i
] < 0) {
2228 error_report("Could not get size of %s: %s",
2229 argv
[optind
+ bs_i
], strerror(-s
.src_sectors
[bs_i
]));
2233 s
.total_sectors
+= s
.src_sectors
[bs_i
];
2237 bdrv_snapshot_load_tmp(blk_bs(s
.src
[0]),
2238 qemu_opt_get(sn_opts
, SNAPSHOT_OPT_ID
),
2239 qemu_opt_get(sn_opts
, SNAPSHOT_OPT_NAME
),
2241 } else if (snapshot_name
!= NULL
) {
2242 if (s
.src_num
> 1) {
2243 error_report("No support for concatenating multiple snapshot");
2248 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s
.src
[0]), snapshot_name
,
2252 error_reportf_err(local_err
, "Failed to load snapshot: ");
2258 /* Find driver and parse its options */
2259 drv
= bdrv_find_format(out_fmt
);
2261 error_report("Unknown file format '%s'", out_fmt
);
2266 proto_drv
= bdrv_find_protocol(out_filename
, true, &local_err
);
2268 error_report_err(local_err
);
2273 if (!drv
->create_opts
) {
2274 error_report("Format driver '%s' does not support image creation",
2280 if (!proto_drv
->create_opts
) {
2281 error_report("Protocol driver '%s' does not support image creation",
2282 proto_drv
->format_name
);
2287 create_opts
= qemu_opts_append(create_opts
, drv
->create_opts
);
2288 create_opts
= qemu_opts_append(create_opts
, proto_drv
->create_opts
);
2290 opts
= qemu_opts_create(create_opts
, NULL
, 0, &error_abort
);
2292 qemu_opts_do_parse(opts
, options
, NULL
, &local_err
);
2294 error_report_err(local_err
);
2300 qemu_opt_set_number(opts
, BLOCK_OPT_SIZE
, s
.total_sectors
* 512,
2302 ret
= add_old_style_options(out_fmt
, opts
, out_baseimg
, NULL
);
2308 /* Get backing file name if -o backing_file was used */
2309 out_baseimg_param
= qemu_opt_get(opts
, BLOCK_OPT_BACKING_FILE
);
2310 if (out_baseimg_param
) {
2311 out_baseimg
= out_baseimg_param
;
2313 s
.target_has_backing
= (bool) out_baseimg
;
2315 if (s
.src_num
> 1 && out_baseimg
) {
2316 error_report("Having a backing file for the target makes no sense when "
2317 "concatenating multiple input images");
2322 /* Check if compression is supported */
2325 qemu_opt_get_bool(opts
, BLOCK_OPT_ENCRYPT
, false);
2326 const char *encryptfmt
=
2327 qemu_opt_get(opts
, BLOCK_OPT_ENCRYPT_FORMAT
);
2328 const char *preallocation
=
2329 qemu_opt_get(opts
, BLOCK_OPT_PREALLOC
);
2331 if (drv
&& !drv
->bdrv_co_pwritev_compressed
) {
2332 error_report("Compression not supported for this file format");
2337 if (encryption
|| encryptfmt
) {
2338 error_report("Compression and encryption not supported at "
2345 && strcmp(preallocation
, "off"))
2347 error_report("Compression and preallocation not supported at "
2355 * The later open call will need any decryption secrets, and
2356 * bdrv_create() will purge "opts", so extract them now before
2360 open_opts
= qdict_new();
2361 qemu_opt_foreach(opts
, img_add_key_secrets
, open_opts
, &error_abort
);
2365 /* Create the new image */
2366 ret
= bdrv_create(drv
, out_filename
, opts
, &local_err
);
2368 error_reportf_err(local_err
, "%s: error while converting %s: ",
2369 out_filename
, out_fmt
);
2374 flags
= s
.min_sparse
? (BDRV_O_RDWR
| BDRV_O_UNMAP
) : BDRV_O_RDWR
;
2375 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
2377 error_report("Invalid cache option: %s", cache
);
2382 s
.target
= img_open(tgt_image_opts
, out_filename
, out_fmt
,
2383 flags
, writethrough
, quiet
, false);
2385 /* TODO ultimately we should allow --target-image-opts
2386 * to be used even when -n is not given.
2387 * That has to wait for bdrv_create to be improved
2388 * to allow filenames in option syntax
2390 s
.target
= img_open_file(out_filename
, open_opts
, out_fmt
,
2391 flags
, writethrough
, quiet
, false);
2392 open_opts
= NULL
; /* blk_new_open will have freed it */
2398 out_bs
= blk_bs(s
.target
);
2400 if (s
.compressed
&& !out_bs
->drv
->bdrv_co_pwritev_compressed
) {
2401 error_report("Compression not supported for this file format");
2406 /* increase bufsectors from the default 4096 (2M) if opt_transfer
2407 * or discard_alignment of the out_bs is greater. Limit to
2408 * MAX_BUF_SECTORS as maximum which is currently 32768 (16MB). */
2409 s
.buf_sectors
= MIN(MAX_BUF_SECTORS
,
2411 MAX(out_bs
->bl
.opt_transfer
>> BDRV_SECTOR_BITS
,
2412 out_bs
->bl
.pdiscard_alignment
>>
2413 BDRV_SECTOR_BITS
)));
2415 /* try to align the write requests to the destination to avoid unnecessary
2417 s
.alignment
= MAX(pow2floor(s
.min_sparse
),
2418 DIV_ROUND_UP(out_bs
->bl
.request_alignment
,
2420 assert(is_power_of_2(s
.alignment
));
2423 int64_t output_sectors
= blk_nb_sectors(s
.target
);
2424 if (output_sectors
< 0) {
2425 error_report("unable to get output image length: %s",
2426 strerror(-output_sectors
));
2429 } else if (output_sectors
< s
.total_sectors
) {
2430 error_report("output file is smaller than input file");
2436 if (s
.target_has_backing
) {
2437 /* Errors are treated as "backing length unknown" (which means
2438 * s.target_backing_sectors has to be negative, which it will
2439 * be automatically). The backing file length is used only
2440 * for optimizations, so such a case is not fatal. */
2441 s
.target_backing_sectors
= bdrv_nb_sectors(out_bs
->backing
->bs
);
2443 s
.target_backing_sectors
= -1;
2446 ret
= bdrv_get_info(out_bs
, &bdi
);
2449 error_report("could not get block driver info");
2453 s
.compressed
= s
.compressed
|| bdi
.needs_compressed_writes
;
2454 s
.cluster_sectors
= bdi
.cluster_size
/ BDRV_SECTOR_SIZE
;
2455 s
.unallocated_blocks_are_zero
= bdi
.unallocated_blocks_are_zero
;
2458 ret
= convert_do_copy(&s
);
2461 qemu_progress_print(100, 0);
2463 qemu_progress_end();
2464 qemu_opts_del(opts
);
2465 qemu_opts_free(create_opts
);
2466 qemu_opts_del(sn_opts
);
2467 qobject_unref(open_opts
);
2468 blk_unref(s
.target
);
2470 for (bs_i
= 0; bs_i
< s
.src_num
; bs_i
++) {
2471 blk_unref(s
.src
[bs_i
]);
2475 g_free(s
.src_sectors
);
2483 static void dump_snapshots(BlockDriverState
*bs
)
2485 QEMUSnapshotInfo
*sn_tab
, *sn
;
2488 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
2491 printf("Snapshot list:\n");
2492 bdrv_snapshot_dump(NULL
);
2494 for(i
= 0; i
< nb_sns
; i
++) {
2496 bdrv_snapshot_dump(sn
);
2502 static void dump_json_image_info_list(ImageInfoList
*list
)
2506 Visitor
*v
= qobject_output_visitor_new(&obj
);
2508 visit_type_ImageInfoList(v
, NULL
, &list
, &error_abort
);
2509 visit_complete(v
, &obj
);
2510 str
= qobject_to_json_pretty(obj
);
2511 assert(str
!= NULL
);
2512 printf("%s\n", qstring_get_str(str
));
2518 static void dump_json_image_info(ImageInfo
*info
)
2522 Visitor
*v
= qobject_output_visitor_new(&obj
);
2524 visit_type_ImageInfo(v
, NULL
, &info
, &error_abort
);
2525 visit_complete(v
, &obj
);
2526 str
= qobject_to_json_pretty(obj
);
2527 assert(str
!= NULL
);
2528 printf("%s\n", qstring_get_str(str
));
2534 static void dump_human_image_info_list(ImageInfoList
*list
)
2536 ImageInfoList
*elem
;
2539 for (elem
= list
; elem
; elem
= elem
->next
) {
2545 bdrv_image_info_dump(elem
->value
);
2549 static gboolean
str_equal_func(gconstpointer a
, gconstpointer b
)
2551 return strcmp(a
, b
) == 0;
2555 * Open an image file chain and return an ImageInfoList
2557 * @filename: topmost image filename
2558 * @fmt: topmost image format (may be NULL to autodetect)
2559 * @chain: true - enumerate entire backing file chain
2560 * false - only topmost image file
2562 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2563 * image file. If there was an error a message will have been printed to
2566 static ImageInfoList
*collect_image_info_list(bool image_opts
,
2567 const char *filename
,
2569 bool chain
, bool force_share
)
2571 ImageInfoList
*head
= NULL
;
2572 ImageInfoList
**last
= &head
;
2573 GHashTable
*filenames
;
2576 filenames
= g_hash_table_new_full(g_str_hash
, str_equal_func
, NULL
, NULL
);
2580 BlockDriverState
*bs
;
2582 ImageInfoList
*elem
;
2584 if (g_hash_table_lookup_extended(filenames
, filename
, NULL
, NULL
)) {
2585 error_report("Backing file '%s' creates an infinite loop.",
2589 g_hash_table_insert(filenames
, (gpointer
)filename
, NULL
);
2591 blk
= img_open(image_opts
, filename
, fmt
,
2592 BDRV_O_NO_BACKING
| BDRV_O_NO_IO
, false, false,
2599 bdrv_query_image_info(bs
, &info
, &err
);
2601 error_report_err(err
);
2606 elem
= g_new0(ImageInfoList
, 1);
2613 filename
= fmt
= NULL
;
2615 if (info
->has_full_backing_filename
) {
2616 filename
= info
->full_backing_filename
;
2617 } else if (info
->has_backing_filename
) {
2618 error_report("Could not determine absolute backing filename,"
2619 " but backing filename '%s' present",
2620 info
->backing_filename
);
2623 if (info
->has_backing_filename_format
) {
2624 fmt
= info
->backing_filename_format
;
2628 g_hash_table_destroy(filenames
);
2632 qapi_free_ImageInfoList(head
);
2633 g_hash_table_destroy(filenames
);
2637 static int img_info(int argc
, char **argv
)
2640 OutputFormat output_format
= OFORMAT_HUMAN
;
2642 const char *filename
, *fmt
, *output
;
2643 ImageInfoList
*list
;
2644 bool image_opts
= false;
2645 bool force_share
= false;
2650 int option_index
= 0;
2651 static const struct option long_options
[] = {
2652 {"help", no_argument
, 0, 'h'},
2653 {"format", required_argument
, 0, 'f'},
2654 {"output", required_argument
, 0, OPTION_OUTPUT
},
2655 {"backing-chain", no_argument
, 0, OPTION_BACKING_CHAIN
},
2656 {"object", required_argument
, 0, OPTION_OBJECT
},
2657 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
2658 {"force-share", no_argument
, 0, 'U'},
2661 c
= getopt_long(argc
, argv
, ":f:hU",
2662 long_options
, &option_index
);
2668 missing_argument(argv
[optind
- 1]);
2671 unrecognized_option(argv
[optind
- 1]);
2685 case OPTION_BACKING_CHAIN
:
2688 case OPTION_OBJECT
: {
2690 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
2696 case OPTION_IMAGE_OPTS
:
2701 if (optind
!= argc
- 1) {
2702 error_exit("Expecting one image file name");
2704 filename
= argv
[optind
++];
2706 if (output
&& !strcmp(output
, "json")) {
2707 output_format
= OFORMAT_JSON
;
2708 } else if (output
&& !strcmp(output
, "human")) {
2709 output_format
= OFORMAT_HUMAN
;
2710 } else if (output
) {
2711 error_report("--output must be used with human or json as argument.");
2715 if (qemu_opts_foreach(&qemu_object_opts
,
2716 user_creatable_add_opts_foreach
,
2717 NULL
, &error_fatal
)) {
2721 list
= collect_image_info_list(image_opts
, filename
, fmt
, chain
,
2727 switch (output_format
) {
2729 dump_human_image_info_list(list
);
2733 dump_json_image_info_list(list
);
2735 dump_json_image_info(list
->value
);
2740 qapi_free_ImageInfoList(list
);
2744 static int dump_map_entry(OutputFormat output_format
, MapEntry
*e
,
2747 switch (output_format
) {
2749 if (e
->data
&& !e
->has_offset
) {
2750 error_report("File contains external, encrypted or compressed clusters.");
2753 if (e
->data
&& !e
->zero
) {
2754 printf("%#-16"PRIx64
"%#-16"PRIx64
"%#-16"PRIx64
"%s\n",
2755 e
->start
, e
->length
,
2756 e
->has_offset
? e
->offset
: 0,
2757 e
->has_filename
? e
->filename
: "");
2759 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2760 * Modify the flags here to allow more coalescing.
2762 if (next
&& (!next
->data
|| next
->zero
)) {
2768 printf("%s{ \"start\": %"PRId64
", \"length\": %"PRId64
","
2769 " \"depth\": %"PRId64
", \"zero\": %s, \"data\": %s",
2770 (e
->start
== 0 ? "[" : ",\n"),
2771 e
->start
, e
->length
, e
->depth
,
2772 e
->zero
? "true" : "false",
2773 e
->data
? "true" : "false");
2774 if (e
->has_offset
) {
2775 printf(", \"offset\": %"PRId64
"", e
->offset
);
2787 static int get_block_status(BlockDriverState
*bs
, int64_t offset
,
2788 int64_t bytes
, MapEntry
*e
)
2792 BlockDriverState
*file
;
2795 char *filename
= NULL
;
2797 /* As an optimization, we could cache the current range of unallocated
2798 * clusters in each file of the chain, and avoid querying the same
2804 ret
= bdrv_block_status(bs
, offset
, bytes
, &bytes
, &map
, &file
);
2809 if (ret
& (BDRV_BLOCK_ZERO
|BDRV_BLOCK_DATA
)) {
2812 bs
= backing_bs(bs
);
2821 has_offset
= !!(ret
& BDRV_BLOCK_OFFSET_VALID
);
2823 if (file
&& has_offset
) {
2824 bdrv_refresh_filename(file
);
2825 filename
= file
->filename
;
2831 .data
= !!(ret
& BDRV_BLOCK_DATA
),
2832 .zero
= !!(ret
& BDRV_BLOCK_ZERO
),
2834 .has_offset
= has_offset
,
2836 .has_filename
= filename
,
2837 .filename
= filename
,
2843 static inline bool entry_mergeable(const MapEntry
*curr
, const MapEntry
*next
)
2845 if (curr
->length
== 0) {
2848 if (curr
->zero
!= next
->zero
||
2849 curr
->data
!= next
->data
||
2850 curr
->depth
!= next
->depth
||
2851 curr
->has_filename
!= next
->has_filename
||
2852 curr
->has_offset
!= next
->has_offset
) {
2855 if (curr
->has_filename
&& strcmp(curr
->filename
, next
->filename
)) {
2858 if (curr
->has_offset
&& curr
->offset
+ curr
->length
!= next
->offset
) {
2864 static int img_map(int argc
, char **argv
)
2867 OutputFormat output_format
= OFORMAT_HUMAN
;
2869 BlockDriverState
*bs
;
2870 const char *filename
, *fmt
, *output
;
2872 MapEntry curr
= { .length
= 0 }, next
;
2874 bool image_opts
= false;
2875 bool force_share
= false;
2880 int option_index
= 0;
2881 static const struct option long_options
[] = {
2882 {"help", no_argument
, 0, 'h'},
2883 {"format", required_argument
, 0, 'f'},
2884 {"output", required_argument
, 0, OPTION_OUTPUT
},
2885 {"object", required_argument
, 0, OPTION_OBJECT
},
2886 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
2887 {"force-share", no_argument
, 0, 'U'},
2890 c
= getopt_long(argc
, argv
, ":f:hU",
2891 long_options
, &option_index
);
2897 missing_argument(argv
[optind
- 1]);
2900 unrecognized_option(argv
[optind
- 1]);
2914 case OPTION_OBJECT
: {
2916 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
2922 case OPTION_IMAGE_OPTS
:
2927 if (optind
!= argc
- 1) {
2928 error_exit("Expecting one image file name");
2930 filename
= argv
[optind
];
2932 if (output
&& !strcmp(output
, "json")) {
2933 output_format
= OFORMAT_JSON
;
2934 } else if (output
&& !strcmp(output
, "human")) {
2935 output_format
= OFORMAT_HUMAN
;
2936 } else if (output
) {
2937 error_report("--output must be used with human or json as argument.");
2941 if (qemu_opts_foreach(&qemu_object_opts
,
2942 user_creatable_add_opts_foreach
,
2943 NULL
, &error_fatal
)) {
2947 blk
= img_open(image_opts
, filename
, fmt
, 0, false, false, force_share
);
2953 if (output_format
== OFORMAT_HUMAN
) {
2954 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2957 length
= blk_getlength(blk
);
2958 while (curr
.start
+ curr
.length
< length
) {
2959 int64_t offset
= curr
.start
+ curr
.length
;
2962 /* Probe up to 1 GiB at a time. */
2963 n
= MIN(1 << 30, length
- offset
);
2964 ret
= get_block_status(bs
, offset
, n
, &next
);
2967 error_report("Could not read file metadata: %s", strerror(-ret
));
2971 if (entry_mergeable(&curr
, &next
)) {
2972 curr
.length
+= next
.length
;
2976 if (curr
.length
> 0) {
2977 ret
= dump_map_entry(output_format
, &curr
, &next
);
2985 ret
= dump_map_entry(output_format
, &curr
, NULL
);
2992 #define SNAPSHOT_LIST 1
2993 #define SNAPSHOT_CREATE 2
2994 #define SNAPSHOT_APPLY 3
2995 #define SNAPSHOT_DELETE 4
2997 static int img_snapshot(int argc
, char **argv
)
3000 BlockDriverState
*bs
;
3001 QEMUSnapshotInfo sn
;
3002 char *filename
, *snapshot_name
= NULL
;
3003 int c
, ret
= 0, bdrv_oflags
;
3008 bool image_opts
= false;
3009 bool force_share
= false;
3011 bdrv_oflags
= BDRV_O_RDWR
;
3012 /* Parse commandline parameters */
3014 static const struct option long_options
[] = {
3015 {"help", no_argument
, 0, 'h'},
3016 {"object", required_argument
, 0, OPTION_OBJECT
},
3017 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
3018 {"force-share", no_argument
, 0, 'U'},
3021 c
= getopt_long(argc
, argv
, ":la:c:d:hqU",
3022 long_options
, NULL
);
3028 missing_argument(argv
[optind
- 1]);
3031 unrecognized_option(argv
[optind
- 1]);
3038 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
3041 action
= SNAPSHOT_LIST
;
3042 bdrv_oflags
&= ~BDRV_O_RDWR
; /* no need for RW */
3046 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
3049 action
= SNAPSHOT_APPLY
;
3050 snapshot_name
= optarg
;
3054 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
3057 action
= SNAPSHOT_CREATE
;
3058 snapshot_name
= optarg
;
3062 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
3065 action
= SNAPSHOT_DELETE
;
3066 snapshot_name
= optarg
;
3074 case OPTION_OBJECT
: {
3076 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
3082 case OPTION_IMAGE_OPTS
:
3088 if (optind
!= argc
- 1) {
3089 error_exit("Expecting one image file name");
3091 filename
= argv
[optind
++];
3093 if (qemu_opts_foreach(&qemu_object_opts
,
3094 user_creatable_add_opts_foreach
,
3095 NULL
, &error_fatal
)) {
3099 /* Open the image */
3100 blk
= img_open(image_opts
, filename
, NULL
, bdrv_oflags
, false, quiet
,
3107 /* Perform the requested action */
3113 case SNAPSHOT_CREATE
:
3114 memset(&sn
, 0, sizeof(sn
));
3115 pstrcpy(sn
.name
, sizeof(sn
.name
), snapshot_name
);
3117 qemu_gettimeofday(&tv
);
3118 sn
.date_sec
= tv
.tv_sec
;
3119 sn
.date_nsec
= tv
.tv_usec
* 1000;
3121 ret
= bdrv_snapshot_create(bs
, &sn
);
3123 error_report("Could not create snapshot '%s': %d (%s)",
3124 snapshot_name
, ret
, strerror(-ret
));
3128 case SNAPSHOT_APPLY
:
3129 ret
= bdrv_snapshot_goto(bs
, snapshot_name
, &err
);
3131 error_reportf_err(err
, "Could not apply snapshot '%s': ",
3136 case SNAPSHOT_DELETE
:
3137 ret
= bdrv_snapshot_find(bs
, &sn
, snapshot_name
);
3139 error_report("Could not delete snapshot '%s': snapshot not "
3140 "found", snapshot_name
);
3143 ret
= bdrv_snapshot_delete(bs
, sn
.id_str
, sn
.name
, &err
);
3145 error_reportf_err(err
, "Could not delete snapshot '%s': ",
3161 static int img_rebase(int argc
, char **argv
)
3163 BlockBackend
*blk
= NULL
, *blk_old_backing
= NULL
, *blk_new_backing
= NULL
;
3164 uint8_t *buf_old
= NULL
;
3165 uint8_t *buf_new
= NULL
;
3166 BlockDriverState
*bs
= NULL
;
3168 const char *fmt
, *cache
, *src_cache
, *out_basefmt
, *out_baseimg
;
3169 int c
, flags
, src_flags
, ret
;
3170 bool writethrough
, src_writethrough
;
3172 bool force_share
= false;
3175 Error
*local_err
= NULL
;
3176 bool image_opts
= false;
3178 /* Parse commandline parameters */
3180 cache
= BDRV_DEFAULT_CACHE
;
3181 src_cache
= BDRV_DEFAULT_CACHE
;
3185 static const struct option long_options
[] = {
3186 {"help", no_argument
, 0, 'h'},
3187 {"object", required_argument
, 0, OPTION_OBJECT
},
3188 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
3189 {"force-share", no_argument
, 0, 'U'},
3192 c
= getopt_long(argc
, argv
, ":hf:F:b:upt:T:qU",
3193 long_options
, NULL
);
3199 missing_argument(argv
[optind
- 1]);
3202 unrecognized_option(argv
[optind
- 1]);
3211 out_basefmt
= optarg
;
3214 out_baseimg
= optarg
;
3231 case OPTION_OBJECT
: {
3233 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
3239 case OPTION_IMAGE_OPTS
:
3252 if (optind
!= argc
- 1) {
3253 error_exit("Expecting one image file name");
3255 if (!unsafe
&& !out_baseimg
) {
3256 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
3258 filename
= argv
[optind
++];
3260 if (qemu_opts_foreach(&qemu_object_opts
,
3261 user_creatable_add_opts_foreach
,
3262 NULL
, &error_fatal
)) {
3266 qemu_progress_init(progress
, 2.0);
3267 qemu_progress_print(0, 100);
3269 flags
= BDRV_O_RDWR
| (unsafe
? BDRV_O_NO_BACKING
: 0);
3270 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
3272 error_report("Invalid cache option: %s", cache
);
3277 ret
= bdrv_parse_cache_mode(src_cache
, &src_flags
, &src_writethrough
);
3279 error_report("Invalid source cache option: %s", src_cache
);
3283 /* The source files are opened read-only, don't care about WCE */
3284 assert((src_flags
& BDRV_O_RDWR
) == 0);
3285 (void) src_writethrough
;
3290 * Ignore the old backing file for unsafe rebase in case we want to correct
3291 * the reference to a renamed or moved backing file.
3293 blk
= img_open(image_opts
, filename
, fmt
, flags
, writethrough
, quiet
,
3301 if (out_basefmt
!= NULL
) {
3302 if (bdrv_find_format(out_basefmt
) == NULL
) {
3303 error_report("Invalid format name: '%s'", out_basefmt
);
3309 /* For safe rebasing we need to compare old and new backing file */
3311 char backing_name
[PATH_MAX
];
3312 QDict
*options
= NULL
;
3314 if (bs
->backing_format
[0] != '\0') {
3315 options
= qdict_new();
3316 qdict_put_str(options
, "driver", bs
->backing_format
);
3321 options
= qdict_new();
3323 qdict_put_bool(options
, BDRV_OPT_FORCE_SHARE
, true);
3325 bdrv_get_backing_filename(bs
, backing_name
, sizeof(backing_name
));
3326 blk_old_backing
= blk_new_open(backing_name
, NULL
,
3327 options
, src_flags
, &local_err
);
3328 if (!blk_old_backing
) {
3329 error_reportf_err(local_err
,
3330 "Could not open old backing file '%s': ",
3336 if (out_baseimg
[0]) {
3337 const char *overlay_filename
;
3338 char *out_real_path
;
3340 options
= qdict_new();
3342 qdict_put_str(options
, "driver", out_basefmt
);
3345 qdict_put_bool(options
, BDRV_OPT_FORCE_SHARE
, true);
3348 bdrv_refresh_filename(bs
);
3349 overlay_filename
= bs
->exact_filename
[0] ? bs
->exact_filename
3352 bdrv_get_full_backing_filename_from_filename(overlay_filename
,
3356 error_reportf_err(local_err
,
3357 "Could not resolve backing filename: ");
3362 blk_new_backing
= blk_new_open(out_real_path
, NULL
,
3363 options
, src_flags
, &local_err
);
3364 g_free(out_real_path
);
3365 if (!blk_new_backing
) {
3366 error_reportf_err(local_err
,
3367 "Could not open new backing file '%s': ",
3376 * Check each unallocated cluster in the COW file. If it is unallocated,
3377 * accesses go to the backing file. We must therefore compare this cluster
3378 * in the old and new backing file, and if they differ we need to copy it
3379 * from the old backing file into the COW file.
3381 * If qemu-img crashes during this step, no harm is done. The content of
3382 * the image is the same as the original one at any time.
3386 int64_t old_backing_size
;
3387 int64_t new_backing_size
= 0;
3390 float local_progress
= 0;
3392 buf_old
= blk_blockalign(blk
, IO_BUF_SIZE
);
3393 buf_new
= blk_blockalign(blk
, IO_BUF_SIZE
);
3395 size
= blk_getlength(blk
);
3397 error_report("Could not get size of '%s': %s",
3398 filename
, strerror(-size
));
3402 old_backing_size
= blk_getlength(blk_old_backing
);
3403 if (old_backing_size
< 0) {
3404 char backing_name
[PATH_MAX
];
3406 bdrv_get_backing_filename(bs
, backing_name
, sizeof(backing_name
));
3407 error_report("Could not get size of '%s': %s",
3408 backing_name
, strerror(-old_backing_size
));
3412 if (blk_new_backing
) {
3413 new_backing_size
= blk_getlength(blk_new_backing
);
3414 if (new_backing_size
< 0) {
3415 error_report("Could not get size of '%s': %s",
3416 out_baseimg
, strerror(-new_backing_size
));
3423 local_progress
= (float)100 / (size
/ MIN(size
, IO_BUF_SIZE
));
3426 for (offset
= 0; offset
< size
; offset
+= n
) {
3427 /* How many bytes can we handle with the next read? */
3428 n
= MIN(IO_BUF_SIZE
, size
- offset
);
3430 /* If the cluster is allocated, we don't need to take action */
3431 ret
= bdrv_is_allocated(bs
, offset
, n
, &n
);
3433 error_report("error while reading image metadata: %s",
3442 * Read old and new backing file and take into consideration that
3443 * backing files may be smaller than the COW image.
3445 if (offset
>= old_backing_size
) {
3446 memset(buf_old
, 0, n
);
3448 if (offset
+ n
> old_backing_size
) {
3449 n
= old_backing_size
- offset
;
3452 ret
= blk_pread(blk_old_backing
, offset
, buf_old
, n
);
3454 error_report("error while reading from old backing file");
3459 if (offset
>= new_backing_size
|| !blk_new_backing
) {
3460 memset(buf_new
, 0, n
);
3462 if (offset
+ n
> new_backing_size
) {
3463 n
= new_backing_size
- offset
;
3466 ret
= blk_pread(blk_new_backing
, offset
, buf_new
, n
);
3468 error_report("error while reading from new backing file");
3473 /* If they differ, we need to write to the COW file */
3474 uint64_t written
= 0;
3476 while (written
< n
) {
3479 if (compare_buffers(buf_old
+ written
, buf_new
+ written
,
3480 n
- written
, &pnum
))
3482 ret
= blk_pwrite(blk
, offset
+ written
,
3483 buf_old
+ written
, pnum
, 0);
3485 error_report("Error while writing to COW image: %s",
3493 qemu_progress_print(local_progress
, 100);
3498 * Change the backing file. All clusters that are different from the old
3499 * backing file are overwritten in the COW file now, so the visible content
3500 * doesn't change when we switch the backing file.
3502 if (out_baseimg
&& *out_baseimg
) {
3503 ret
= bdrv_change_backing_file(bs
, out_baseimg
, out_basefmt
);
3505 ret
= bdrv_change_backing_file(bs
, NULL
, NULL
);
3508 if (ret
== -ENOSPC
) {
3509 error_report("Could not change the backing file to '%s': No "
3510 "space left in the file header", out_baseimg
);
3511 } else if (ret
< 0) {
3512 error_report("Could not change the backing file to '%s': %s",
3513 out_baseimg
, strerror(-ret
));
3516 qemu_progress_print(100, 0);
3518 * TODO At this point it is possible to check if any clusters that are
3519 * allocated in the COW file are the same in the backing file. If so, they
3520 * could be dropped from the COW file. Don't do this before switching the
3521 * backing file, in case of a crash this would lead to corruption.
3524 qemu_progress_end();
3527 blk_unref(blk_old_backing
);
3528 blk_unref(blk_new_backing
);
3530 qemu_vfree(buf_old
);
3531 qemu_vfree(buf_new
);
3540 static int img_resize(int argc
, char **argv
)
3543 int c
, ret
, relative
;
3544 const char *filename
, *fmt
, *size
;
3545 int64_t n
, total_size
, current_size
, new_size
;
3547 BlockBackend
*blk
= NULL
;
3548 PreallocMode prealloc
= PREALLOC_MODE_OFF
;
3551 static QemuOptsList resize_options
= {
3552 .name
= "resize_options",
3553 .head
= QTAILQ_HEAD_INITIALIZER(resize_options
.head
),
3556 .name
= BLOCK_OPT_SIZE
,
3557 .type
= QEMU_OPT_SIZE
,
3558 .help
= "Virtual disk size"
3564 bool image_opts
= false;
3565 bool shrink
= false;
3567 /* Remove size from argv manually so that negative numbers are not treated
3568 * as options by getopt. */
3570 error_exit("Not enough arguments");
3574 size
= argv
[--argc
];
3576 /* Parse getopt arguments */
3579 static const struct option long_options
[] = {
3580 {"help", no_argument
, 0, 'h'},
3581 {"object", required_argument
, 0, OPTION_OBJECT
},
3582 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
3583 {"preallocation", required_argument
, 0, OPTION_PREALLOCATION
},
3584 {"shrink", no_argument
, 0, OPTION_SHRINK
},
3587 c
= getopt_long(argc
, argv
, ":f:hq",
3588 long_options
, NULL
);
3594 missing_argument(argv
[optind
- 1]);
3597 unrecognized_option(argv
[optind
- 1]);
3608 case OPTION_OBJECT
: {
3610 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
3616 case OPTION_IMAGE_OPTS
:
3619 case OPTION_PREALLOCATION
:
3620 prealloc
= qapi_enum_parse(&PreallocMode_lookup
, optarg
,
3621 PREALLOC_MODE__MAX
, NULL
);
3622 if (prealloc
== PREALLOC_MODE__MAX
) {
3623 error_report("Invalid preallocation mode '%s'", optarg
);
3632 if (optind
!= argc
- 1) {
3633 error_exit("Expecting image file name and size");
3635 filename
= argv
[optind
++];
3637 if (qemu_opts_foreach(&qemu_object_opts
,
3638 user_creatable_add_opts_foreach
,
3639 NULL
, &error_fatal
)) {
3643 /* Choose grow, shrink, or absolute resize mode */
3659 param
= qemu_opts_create(&resize_options
, NULL
, 0, &error_abort
);
3660 qemu_opt_set(param
, BLOCK_OPT_SIZE
, size
, &err
);
3662 error_report_err(err
);
3664 qemu_opts_del(param
);
3667 n
= qemu_opt_get_size(param
, BLOCK_OPT_SIZE
, 0);
3668 qemu_opts_del(param
);
3670 blk
= img_open(image_opts
, filename
, fmt
,
3671 BDRV_O_RDWR
| BDRV_O_RESIZE
, false, quiet
,
3678 current_size
= blk_getlength(blk
);
3679 if (current_size
< 0) {
3680 error_report("Failed to inquire current image length: %s",
3681 strerror(-current_size
));
3687 total_size
= current_size
+ n
* relative
;
3691 if (total_size
<= 0) {
3692 error_report("New image size must be positive");
3697 if (total_size
<= current_size
&& prealloc
!= PREALLOC_MODE_OFF
) {
3698 error_report("Preallocation can only be used for growing images");
3703 if (total_size
< current_size
&& !shrink
) {
3704 warn_report("Shrinking an image will delete all data beyond the "
3705 "shrunken image's end. Before performing such an "
3706 "operation, make sure there is no important data there.");
3708 if (g_strcmp0(bdrv_get_format_name(blk_bs(blk
)), "raw") != 0) {
3710 "Use the --shrink option to perform a shrink operation.");
3714 warn_report("Using the --shrink option will suppress this message. "
3715 "Note that future versions of qemu-img may refuse to "
3716 "shrink images without this option.");
3720 ret
= blk_truncate(blk
, total_size
, prealloc
, &err
);
3722 error_report_err(err
);
3726 new_size
= blk_getlength(blk
);
3728 error_report("Failed to verify truncated image length: %s",
3729 strerror(-new_size
));
3734 /* Some block drivers implement a truncation method, but only so
3735 * the user can cause qemu to refresh the image's size from disk.
3736 * The idea is that the user resizes the image outside of qemu and
3737 * then invokes block_resize to inform qemu about it.
3738 * (This includes iscsi and file-posix for device files.)
3739 * Of course, that is not the behavior someone invoking
3740 * qemu-img resize would find useful, so we catch that behavior
3741 * here and tell the user. */
3742 if (new_size
!= total_size
&& new_size
== current_size
) {
3743 error_report("Image was not resized; resizing may not be supported "
3749 if (new_size
!= total_size
) {
3750 warn_report("Image should have been resized to %" PRIi64
3751 " bytes, but was resized to %" PRIi64
" bytes",
3752 total_size
, new_size
);
3755 qprintf(quiet
, "Image resized.\n");
3765 static void amend_status_cb(BlockDriverState
*bs
,
3766 int64_t offset
, int64_t total_work_size
,
3769 qemu_progress_print(100.f
* offset
/ total_work_size
, 0);
3772 static int print_amend_option_help(const char *format
)
3776 /* Find driver and parse its options */
3777 drv
= bdrv_find_format(format
);
3779 error_report("Unknown file format '%s'", format
);
3783 if (!drv
->bdrv_amend_options
) {
3784 error_report("Format driver '%s' does not support option amendment",
3789 /* Every driver supporting amendment must have create_opts */
3790 assert(drv
->create_opts
);
3792 printf("Creation options for '%s':\n", format
);
3793 qemu_opts_print_help(drv
->create_opts
, false);
3794 printf("\nNote that not all of these options may be amendable.\n");
3798 static int img_amend(int argc
, char **argv
)
3802 char *options
= NULL
;
3803 QemuOptsList
*create_opts
= NULL
;
3804 QemuOpts
*opts
= NULL
;
3805 const char *fmt
= NULL
, *filename
, *cache
;
3808 bool quiet
= false, progress
= false;
3809 BlockBackend
*blk
= NULL
;
3810 BlockDriverState
*bs
= NULL
;
3811 bool image_opts
= false;
3813 cache
= BDRV_DEFAULT_CACHE
;
3815 static const struct option long_options
[] = {
3816 {"help", no_argument
, 0, 'h'},
3817 {"object", required_argument
, 0, OPTION_OBJECT
},
3818 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
3821 c
= getopt_long(argc
, argv
, ":ho:f:t:pq",
3822 long_options
, NULL
);
3829 missing_argument(argv
[optind
- 1]);
3832 unrecognized_option(argv
[optind
- 1]);
3838 if (!is_valid_option_list(optarg
)) {
3839 error_report("Invalid option list: %s", optarg
);
3841 goto out_no_progress
;
3844 options
= g_strdup(optarg
);
3846 char *old_options
= options
;
3847 options
= g_strdup_printf("%s,%s", options
, optarg
);
3848 g_free(old_options
);
3864 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
3868 goto out_no_progress
;
3871 case OPTION_IMAGE_OPTS
:
3878 error_exit("Must specify options (-o)");
3881 if (qemu_opts_foreach(&qemu_object_opts
,
3882 user_creatable_add_opts_foreach
,
3883 NULL
, &error_fatal
)) {
3885 goto out_no_progress
;
3891 qemu_progress_init(progress
, 1.0);
3893 filename
= (optind
== argc
- 1) ? argv
[argc
- 1] : NULL
;
3894 if (fmt
&& has_help_option(options
)) {
3895 /* If a format is explicitly specified (and possibly no filename is
3896 * given), print option help here */
3897 ret
= print_amend_option_help(fmt
);
3901 if (optind
!= argc
- 1) {
3902 error_report("Expecting one image file name");
3907 flags
= BDRV_O_RDWR
;
3908 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
3910 error_report("Invalid cache option: %s", cache
);
3914 blk
= img_open(image_opts
, filename
, fmt
, flags
, writethrough
, quiet
,
3922 fmt
= bs
->drv
->format_name
;
3924 if (has_help_option(options
)) {
3925 /* If the format was auto-detected, print option help here */
3926 ret
= print_amend_option_help(fmt
);
3930 if (!bs
->drv
->bdrv_amend_options
) {
3931 error_report("Format driver '%s' does not support option amendment",
3937 /* Every driver supporting amendment must have create_opts */
3938 assert(bs
->drv
->create_opts
);
3940 create_opts
= qemu_opts_append(create_opts
, bs
->drv
->create_opts
);
3941 opts
= qemu_opts_create(create_opts
, NULL
, 0, &error_abort
);
3942 qemu_opts_do_parse(opts
, options
, NULL
, &err
);
3944 error_report_err(err
);
3949 /* In case the driver does not call amend_status_cb() */
3950 qemu_progress_print(0.f
, 0);
3951 ret
= bdrv_amend_options(bs
, opts
, &amend_status_cb
, NULL
, &err
);
3952 qemu_progress_print(100.f
, 0);
3954 error_report_err(err
);
3959 qemu_progress_end();
3963 qemu_opts_del(opts
);
3964 qemu_opts_free(create_opts
);
3973 typedef struct BenchData
{
3975 uint64_t image_size
;
3982 bool drain_on_flush
;
3991 static void bench_undrained_flush_cb(void *opaque
, int ret
)
3994 error_report("Failed flush request: %s", strerror(-ret
));
3999 static void bench_cb(void *opaque
, int ret
)
4001 BenchData
*b
= opaque
;
4005 error_report("Failed request: %s", strerror(-ret
));
4010 /* Just finished a flush with drained queue: Start next requests */
4011 assert(b
->in_flight
== 0);
4012 b
->in_flush
= false;
4013 } else if (b
->in_flight
> 0) {
4014 int remaining
= b
->n
- b
->in_flight
;
4019 /* Time for flush? Drain queue if requested, then flush */
4020 if (b
->flush_interval
&& remaining
% b
->flush_interval
== 0) {
4021 if (!b
->in_flight
|| !b
->drain_on_flush
) {
4022 BlockCompletionFunc
*cb
;
4024 if (b
->drain_on_flush
) {
4028 cb
= bench_undrained_flush_cb
;
4031 acb
= blk_aio_flush(b
->blk
, cb
, b
);
4033 error_report("Failed to issue flush request");
4037 if (b
->drain_on_flush
) {
4043 while (b
->n
> b
->in_flight
&& b
->in_flight
< b
->nrreq
) {
4044 int64_t offset
= b
->offset
;
4045 /* blk_aio_* might look for completed I/Os and kick bench_cb
4046 * again, so make sure this operation is counted by in_flight
4047 * and b->offset is ready for the next submission.
4050 b
->offset
+= b
->step
;
4051 b
->offset
%= b
->image_size
;
4053 acb
= blk_aio_pwritev(b
->blk
, offset
, b
->qiov
, 0, bench_cb
, b
);
4055 acb
= blk_aio_preadv(b
->blk
, offset
, b
->qiov
, 0, bench_cb
, b
);
4058 error_report("Failed to issue request");
4064 static int img_bench(int argc
, char **argv
)
4067 const char *fmt
= NULL
, *filename
;
4069 bool image_opts
= false;
4070 bool is_write
= false;
4074 size_t bufsize
= 4096;
4077 int flush_interval
= 0;
4078 bool drain_on_flush
= true;
4080 BlockBackend
*blk
= NULL
;
4081 BenchData data
= {};
4083 bool writethrough
= false;
4084 struct timeval t1
, t2
;
4086 bool force_share
= false;
4090 static const struct option long_options
[] = {
4091 {"help", no_argument
, 0, 'h'},
4092 {"flush-interval", required_argument
, 0, OPTION_FLUSH_INTERVAL
},
4093 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
4094 {"pattern", required_argument
, 0, OPTION_PATTERN
},
4095 {"no-drain", no_argument
, 0, OPTION_NO_DRAIN
},
4096 {"force-share", no_argument
, 0, 'U'},
4099 c
= getopt_long(argc
, argv
, ":hc:d:f:no:qs:S:t:wU", long_options
, NULL
);
4106 missing_argument(argv
[optind
- 1]);
4109 unrecognized_option(argv
[optind
- 1]);
4118 if (qemu_strtoul(optarg
, NULL
, 0, &res
) < 0 || res
> INT_MAX
) {
4119 error_report("Invalid request count specified");
4129 if (qemu_strtoul(optarg
, NULL
, 0, &res
) < 0 || res
> INT_MAX
) {
4130 error_report("Invalid queue depth specified");
4140 flags
|= BDRV_O_NATIVE_AIO
;
4144 offset
= cvtnum(optarg
);
4146 error_report("Invalid offset specified");
4159 sval
= cvtnum(optarg
);
4160 if (sval
< 0 || sval
> INT_MAX
) {
4161 error_report("Invalid buffer size specified");
4172 sval
= cvtnum(optarg
);
4173 if (sval
< 0 || sval
> INT_MAX
) {
4174 error_report("Invalid step size specified");
4182 ret
= bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
);
4184 error_report("Invalid cache mode");
4190 flags
|= BDRV_O_RDWR
;
4196 case OPTION_PATTERN
:
4200 if (qemu_strtoul(optarg
, NULL
, 0, &res
) < 0 || res
> 0xff) {
4201 error_report("Invalid pattern byte specified");
4207 case OPTION_FLUSH_INTERVAL
:
4211 if (qemu_strtoul(optarg
, NULL
, 0, &res
) < 0 || res
> INT_MAX
) {
4212 error_report("Invalid flush interval specified");
4215 flush_interval
= res
;
4218 case OPTION_NO_DRAIN
:
4219 drain_on_flush
= false;
4221 case OPTION_IMAGE_OPTS
:
4227 if (optind
!= argc
- 1) {
4228 error_exit("Expecting one image file name");
4230 filename
= argv
[argc
- 1];
4232 if (!is_write
&& flush_interval
) {
4233 error_report("--flush-interval is only available in write tests");
4237 if (flush_interval
&& flush_interval
< depth
) {
4238 error_report("Flush interval can't be smaller than depth");
4243 blk
= img_open(image_opts
, filename
, fmt
, flags
, writethrough
, quiet
,
4250 image_size
= blk_getlength(blk
);
4251 if (image_size
< 0) {
4256 data
= (BenchData
) {
4258 .image_size
= image_size
,
4260 .step
= step
?: bufsize
,
4265 .flush_interval
= flush_interval
,
4266 .drain_on_flush
= drain_on_flush
,
4268 printf("Sending %d %s requests, %d bytes each, %d in parallel "
4269 "(starting at offset %" PRId64
", step size %d)\n",
4270 data
.n
, data
.write
? "write" : "read", data
.bufsize
, data
.nrreq
,
4271 data
.offset
, data
.step
);
4272 if (flush_interval
) {
4273 printf("Sending flush every %d requests\n", flush_interval
);
4276 buf_size
= data
.nrreq
* data
.bufsize
;
4277 data
.buf
= blk_blockalign(blk
, buf_size
);
4278 memset(data
.buf
, pattern
, data
.nrreq
* data
.bufsize
);
4280 blk_register_buf(blk
, data
.buf
, buf_size
);
4282 data
.qiov
= g_new(QEMUIOVector
, data
.nrreq
);
4283 for (i
= 0; i
< data
.nrreq
; i
++) {
4284 qemu_iovec_init(&data
.qiov
[i
], 1);
4285 qemu_iovec_add(&data
.qiov
[i
],
4286 data
.buf
+ i
* data
.bufsize
, data
.bufsize
);
4289 gettimeofday(&t1
, NULL
);
4292 while (data
.n
> 0) {
4293 main_loop_wait(false);
4295 gettimeofday(&t2
, NULL
);
4297 printf("Run completed in %3.3f seconds.\n",
4298 (t2
.tv_sec
- t1
.tv_sec
)
4299 + ((double)(t2
.tv_usec
- t1
.tv_usec
) / 1000000));
4303 blk_unregister_buf(blk
, data
.buf
);
4305 qemu_vfree(data
.buf
);
4326 int bsz
; /* Block size */
4334 int (*f
)(const char *, struct DdIo
*, struct DdIo
*, struct DdInfo
*);
4338 static int img_dd_bs(const char *arg
,
4339 struct DdIo
*in
, struct DdIo
*out
,
4346 if (res
<= 0 || res
> INT_MAX
) {
4347 error_report("invalid number: '%s'", arg
);
4350 in
->bsz
= out
->bsz
= res
;
4355 static int img_dd_count(const char *arg
,
4356 struct DdIo
*in
, struct DdIo
*out
,
4359 dd
->count
= cvtnum(arg
);
4361 if (dd
->count
< 0) {
4362 error_report("invalid number: '%s'", arg
);
4369 static int img_dd_if(const char *arg
,
4370 struct DdIo
*in
, struct DdIo
*out
,
4373 in
->filename
= g_strdup(arg
);
4378 static int img_dd_of(const char *arg
,
4379 struct DdIo
*in
, struct DdIo
*out
,
4382 out
->filename
= g_strdup(arg
);
4387 static int img_dd_skip(const char *arg
,
4388 struct DdIo
*in
, struct DdIo
*out
,
4391 in
->offset
= cvtnum(arg
);
4393 if (in
->offset
< 0) {
4394 error_report("invalid number: '%s'", arg
);
4401 static int img_dd(int argc
, char **argv
)
4406 BlockDriver
*drv
= NULL
, *proto_drv
= NULL
;
4407 BlockBackend
*blk1
= NULL
, *blk2
= NULL
;
4408 QemuOpts
*opts
= NULL
;
4409 QemuOptsList
*create_opts
= NULL
;
4410 Error
*local_err
= NULL
;
4411 bool image_opts
= false;
4413 const char *out_fmt
= "raw";
4414 const char *fmt
= NULL
;
4416 int64_t block_count
= 0, out_pos
, in_pos
;
4417 bool force_share
= false;
4418 struct DdInfo dd
= {
4423 .bsz
= 512, /* Block size is by default 512 bytes */
4435 const struct DdOpts options
[] = {
4436 { "bs", img_dd_bs
, C_BS
},
4437 { "count", img_dd_count
, C_COUNT
},
4438 { "if", img_dd_if
, C_IF
},
4439 { "of", img_dd_of
, C_OF
},
4440 { "skip", img_dd_skip
, C_SKIP
},
4443 const struct option long_options
[] = {
4444 { "help", no_argument
, 0, 'h'},
4445 { "object", required_argument
, 0, OPTION_OBJECT
},
4446 { "image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
4447 { "force-share", no_argument
, 0, 'U'},
4451 while ((c
= getopt_long(argc
, argv
, ":hf:O:U", long_options
, NULL
))) {
4463 missing_argument(argv
[optind
- 1]);
4466 unrecognized_option(argv
[optind
- 1]);
4475 if (!qemu_opts_parse_noisily(&qemu_object_opts
, optarg
, true)) {
4480 case OPTION_IMAGE_OPTS
:
4486 for (i
= optind
; i
< argc
; i
++) {
4488 arg
= g_strdup(argv
[i
]);
4490 tmp
= strchr(arg
, '=');
4492 error_report("unrecognized operand %s", arg
);
4499 for (j
= 0; options
[j
].name
!= NULL
; j
++) {
4500 if (!strcmp(arg
, options
[j
].name
)) {
4504 if (options
[j
].name
== NULL
) {
4505 error_report("unrecognized operand %s", arg
);
4510 if (options
[j
].f(tmp
, &in
, &out
, &dd
) != 0) {
4514 dd
.flags
|= options
[j
].flag
;
4519 if (!(dd
.flags
& C_IF
&& dd
.flags
& C_OF
)) {
4520 error_report("Must specify both input and output files");
4525 if (qemu_opts_foreach(&qemu_object_opts
,
4526 user_creatable_add_opts_foreach
,
4527 NULL
, &error_fatal
)) {
4532 blk1
= img_open(image_opts
, in
.filename
, fmt
, 0, false, false,
4540 drv
= bdrv_find_format(out_fmt
);
4542 error_report("Unknown file format");
4546 proto_drv
= bdrv_find_protocol(out
.filename
, true, &local_err
);
4549 error_report_err(local_err
);
4553 if (!drv
->create_opts
) {
4554 error_report("Format driver '%s' does not support image creation",
4559 if (!proto_drv
->create_opts
) {
4560 error_report("Protocol driver '%s' does not support image creation",
4561 proto_drv
->format_name
);
4565 create_opts
= qemu_opts_append(create_opts
, drv
->create_opts
);
4566 create_opts
= qemu_opts_append(create_opts
, proto_drv
->create_opts
);
4568 opts
= qemu_opts_create(create_opts
, NULL
, 0, &error_abort
);
4570 size
= blk_getlength(blk1
);
4572 error_report("Failed to get size for '%s'", in
.filename
);
4577 if (dd
.flags
& C_COUNT
&& dd
.count
<= INT64_MAX
/ in
.bsz
&&
4578 dd
.count
* in
.bsz
< size
) {
4579 size
= dd
.count
* in
.bsz
;
4582 /* Overflow means the specified offset is beyond input image's size */
4583 if (dd
.flags
& C_SKIP
&& (in
.offset
> INT64_MAX
/ in
.bsz
||
4584 size
< in
.bsz
* in
.offset
)) {
4585 qemu_opt_set_number(opts
, BLOCK_OPT_SIZE
, 0, &error_abort
);
4587 qemu_opt_set_number(opts
, BLOCK_OPT_SIZE
,
4588 size
- in
.bsz
* in
.offset
, &error_abort
);
4591 ret
= bdrv_create(drv
, out
.filename
, opts
, &local_err
);
4593 error_reportf_err(local_err
,
4594 "%s: error while creating output image: ",
4600 /* TODO, we can't honour --image-opts for the target,
4601 * since it needs to be given in a format compatible
4602 * with the bdrv_create() call above which does not
4603 * support image-opts style.
4605 blk2
= img_open_file(out
.filename
, NULL
, out_fmt
, BDRV_O_RDWR
,
4606 false, false, false);
4613 if (dd
.flags
& C_SKIP
&& (in
.offset
> INT64_MAX
/ in
.bsz
||
4614 size
< in
.offset
* in
.bsz
)) {
4615 /* We give a warning if the skip option is bigger than the input
4616 * size and create an empty output disk image (i.e. like dd(1)).
4618 error_report("%s: cannot skip to specified offset", in
.filename
);
4621 in_pos
= in
.offset
* in
.bsz
;
4624 in
.buf
= g_new(uint8_t, in
.bsz
);
4626 for (out_pos
= 0; in_pos
< size
; block_count
++) {
4627 int in_ret
, out_ret
;
4629 if (in_pos
+ in
.bsz
> size
) {
4630 in_ret
= blk_pread(blk1
, in_pos
, in
.buf
, size
- in_pos
);
4632 in_ret
= blk_pread(blk1
, in_pos
, in
.buf
, in
.bsz
);
4635 error_report("error while reading from input image file: %s",
4642 out_ret
= blk_pwrite(blk2
, out_pos
, in
.buf
, in_ret
, 0);
4645 error_report("error while writing to output image file: %s",
4646 strerror(-out_ret
));
4655 qemu_opts_del(opts
);
4656 qemu_opts_free(create_opts
);
4659 g_free(in
.filename
);
4660 g_free(out
.filename
);
4670 static void dump_json_block_measure_info(BlockMeasureInfo
*info
)
4674 Visitor
*v
= qobject_output_visitor_new(&obj
);
4676 visit_type_BlockMeasureInfo(v
, NULL
, &info
, &error_abort
);
4677 visit_complete(v
, &obj
);
4678 str
= qobject_to_json_pretty(obj
);
4679 assert(str
!= NULL
);
4680 printf("%s\n", qstring_get_str(str
));
4686 static int img_measure(int argc
, char **argv
)
4688 static const struct option long_options
[] = {
4689 {"help", no_argument
, 0, 'h'},
4690 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
4691 {"object", required_argument
, 0, OPTION_OBJECT
},
4692 {"output", required_argument
, 0, OPTION_OUTPUT
},
4693 {"size", required_argument
, 0, OPTION_SIZE
},
4694 {"force-share", no_argument
, 0, 'U'},
4697 OutputFormat output_format
= OFORMAT_HUMAN
;
4698 BlockBackend
*in_blk
= NULL
;
4700 const char *filename
= NULL
;
4701 const char *fmt
= NULL
;
4702 const char *out_fmt
= "raw";
4703 char *options
= NULL
;
4704 char *snapshot_name
= NULL
;
4705 bool force_share
= false;
4706 QemuOpts
*opts
= NULL
;
4707 QemuOpts
*object_opts
= NULL
;
4708 QemuOpts
*sn_opts
= NULL
;
4709 QemuOptsList
*create_opts
= NULL
;
4710 bool image_opts
= false;
4711 uint64_t img_size
= UINT64_MAX
;
4712 BlockMeasureInfo
*info
= NULL
;
4713 Error
*local_err
= NULL
;
4717 while ((c
= getopt_long(argc
, argv
, "hf:O:o:l:U",
4718 long_options
, NULL
)) != -1) {
4731 if (!is_valid_option_list(optarg
)) {
4732 error_report("Invalid option list: %s", optarg
);
4736 options
= g_strdup(optarg
);
4738 char *old_options
= options
;
4739 options
= g_strdup_printf("%s,%s", options
, optarg
);
4740 g_free(old_options
);
4744 if (strstart(optarg
, SNAPSHOT_OPT_BASE
, NULL
)) {
4745 sn_opts
= qemu_opts_parse_noisily(&internal_snapshot_opts
,
4748 error_report("Failed in parsing snapshot param '%s'",
4753 snapshot_name
= optarg
;
4760 object_opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
4766 case OPTION_IMAGE_OPTS
:
4770 if (!strcmp(optarg
, "json")) {
4771 output_format
= OFORMAT_JSON
;
4772 } else if (!strcmp(optarg
, "human")) {
4773 output_format
= OFORMAT_HUMAN
;
4775 error_report("--output must be used with human or json "
4784 sval
= cvtnum(optarg
);
4786 if (sval
== -ERANGE
) {
4787 error_report("Image size must be less than 8 EiB!");
4789 error_report("Invalid image size specified! You may use "
4790 "k, M, G, T, P or E suffixes for ");
4791 error_report("kilobytes, megabytes, gigabytes, terabytes, "
4792 "petabytes and exabytes.");
4796 img_size
= (uint64_t)sval
;
4802 if (qemu_opts_foreach(&qemu_object_opts
,
4803 user_creatable_add_opts_foreach
,
4804 NULL
, &error_fatal
)) {
4808 if (argc
- optind
> 1) {
4809 error_report("At most one filename argument is allowed.");
4811 } else if (argc
- optind
== 1) {
4812 filename
= argv
[optind
];
4816 (object_opts
|| image_opts
|| fmt
|| snapshot_name
|| sn_opts
)) {
4817 error_report("--object, --image-opts, -f, and -l "
4818 "require a filename argument.");
4821 if (filename
&& img_size
!= UINT64_MAX
) {
4822 error_report("--size N cannot be used together with a filename.");
4825 if (!filename
&& img_size
== UINT64_MAX
) {
4826 error_report("Either --size N or one filename must be specified.");
4831 in_blk
= img_open(image_opts
, filename
, fmt
, 0,
4832 false, false, force_share
);
4838 bdrv_snapshot_load_tmp(blk_bs(in_blk
),
4839 qemu_opt_get(sn_opts
, SNAPSHOT_OPT_ID
),
4840 qemu_opt_get(sn_opts
, SNAPSHOT_OPT_NAME
),
4842 } else if (snapshot_name
!= NULL
) {
4843 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk
),
4844 snapshot_name
, &local_err
);
4847 error_reportf_err(local_err
, "Failed to load snapshot: ");
4852 drv
= bdrv_find_format(out_fmt
);
4854 error_report("Unknown file format '%s'", out_fmt
);
4857 if (!drv
->create_opts
) {
4858 error_report("Format driver '%s' does not support image creation",
4863 create_opts
= qemu_opts_append(create_opts
, drv
->create_opts
);
4864 create_opts
= qemu_opts_append(create_opts
, bdrv_file
.create_opts
);
4865 opts
= qemu_opts_create(create_opts
, NULL
, 0, &error_abort
);
4867 qemu_opts_do_parse(opts
, options
, NULL
, &local_err
);
4869 error_report_err(local_err
);
4870 error_report("Invalid options for file format '%s'", out_fmt
);
4874 if (img_size
!= UINT64_MAX
) {
4875 qemu_opt_set_number(opts
, BLOCK_OPT_SIZE
, img_size
, &error_abort
);
4878 info
= bdrv_measure(drv
, opts
, in_blk
? blk_bs(in_blk
) : NULL
, &local_err
);
4880 error_report_err(local_err
);
4884 if (output_format
== OFORMAT_HUMAN
) {
4885 printf("required size: %" PRIu64
"\n", info
->required
);
4886 printf("fully allocated size: %" PRIu64
"\n", info
->fully_allocated
);
4888 dump_json_block_measure_info(info
);
4894 qapi_free_BlockMeasureInfo(info
);
4895 qemu_opts_del(object_opts
);
4896 qemu_opts_del(opts
);
4897 qemu_opts_del(sn_opts
);
4898 qemu_opts_free(create_opts
);
4904 static const img_cmd_t img_cmds
[] = {
4905 #define DEF(option, callback, arg_string) \
4906 { option, callback },
4907 #include "qemu-img-cmds.h"
4912 int main(int argc
, char **argv
)
4914 const img_cmd_t
*cmd
;
4915 const char *cmdname
;
4916 Error
*local_error
= NULL
;
4917 char *trace_file
= NULL
;
4919 static const struct option long_options
[] = {
4920 {"help", no_argument
, 0, 'h'},
4921 {"version", no_argument
, 0, 'V'},
4922 {"trace", required_argument
, NULL
, 'T'},
4927 signal(SIGPIPE
, SIG_IGN
);
4930 error_init(argv
[0]);
4931 module_call_init(MODULE_INIT_TRACE
);
4932 qemu_init_exec_dir(argv
[0]);
4934 if (qemu_init_main_loop(&local_error
)) {
4935 error_report_err(local_error
);
4939 qcrypto_init(&error_fatal
);
4941 module_call_init(MODULE_INIT_QOM
);
4944 error_exit("Not enough arguments");
4947 qemu_add_opts(&qemu_object_opts
);
4948 qemu_add_opts(&qemu_source_opts
);
4949 qemu_add_opts(&qemu_trace_opts
);
4951 while ((c
= getopt_long(argc
, argv
, "+:hVT:", long_options
, NULL
)) != -1) {
4954 missing_argument(argv
[optind
- 1]);
4957 unrecognized_option(argv
[optind
- 1]);
4963 printf(QEMU_IMG_VERSION
);
4967 trace_file
= trace_opt_parse(optarg
);
4972 cmdname
= argv
[optind
];
4974 /* reset getopt_long scanning */
4980 qemu_reset_optind();
4982 if (!trace_init_backends()) {
4985 trace_init_file(trace_file
);
4986 qemu_set_log(LOG_TRACE
);
4988 /* find the command */
4989 for (cmd
= img_cmds
; cmd
->name
!= NULL
; cmd
++) {
4990 if (!strcmp(cmdname
, cmd
->name
)) {
4991 return cmd
->handler(argc
, argv
);
4996 error_exit("Command not found: %s", cmdname
);