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-common.h"
29 #include "qemu-version.h"
30 #include "qapi/error.h"
31 #include "qapi/qapi-visit-block-core.h"
32 #include "qapi/qobject-output-visitor.h"
33 #include "qapi/qmp/qjson.h"
34 #include "qapi/qmp/qdict.h"
35 #include "qapi/qmp/qstring.h"
36 #include "qemu/cutils.h"
37 #include "qemu/config-file.h"
38 #include "qemu/option.h"
39 #include "qemu/error-report.h"
41 #include "qemu/main-loop.h"
42 #include "qemu/module.h"
43 #include "qemu/units.h"
44 #include "qom/object_interfaces.h"
45 #include "sysemu/block-backend.h"
46 #include "block/block_int.h"
47 #include "block/blockjob.h"
48 #include "block/qapi.h"
49 #include "crypto/init.h"
50 #include "trace/control.h"
52 #define QEMU_IMG_VERSION "qemu-img version " QEMU_FULL_VERSION \
53 "\n" QEMU_COPYRIGHT "\n"
55 typedef struct img_cmd_t
{
57 int (*handler
)(int argc
, char **argv
);
62 OPTION_BACKING_CHAIN
= 257,
64 OPTION_IMAGE_OPTS
= 259,
66 OPTION_FLUSH_INTERVAL
= 261,
67 OPTION_NO_DRAIN
= 262,
68 OPTION_TARGET_IMAGE_OPTS
= 263,
70 OPTION_PREALLOCATION
= 265,
75 typedef enum OutputFormat
{
80 /* Default to cache=writeback as data integrity is not important for qemu-img */
81 #define BDRV_DEFAULT_CACHE "writeback"
83 static void format_print(void *opaque
, const char *name
)
88 static void QEMU_NORETURN
GCC_FMT_ATTR(1, 2) error_exit(const char *fmt
, ...)
93 error_vreport(fmt
, ap
);
96 error_printf("Try 'qemu-img --help' for more information\n");
100 static void QEMU_NORETURN
missing_argument(const char *option
)
102 error_exit("missing argument for option '%s'", option
);
105 static void QEMU_NORETURN
unrecognized_option(const char *option
)
107 error_exit("unrecognized option '%s'", option
);
110 /* Please keep in synch with qemu-img.texi */
111 static void QEMU_NORETURN
help(void)
113 const char *help_msg
=
115 "usage: qemu-img [standard options] command [command options]\n"
116 "QEMU disk image utility\n"
118 " '-h', '--help' display this help and exit\n"
119 " '-V', '--version' output version information and exit\n"
120 " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
121 " specify tracing options\n"
124 #define DEF(option, callback, arg_string) \
126 #include "qemu-img-cmds.h"
129 "Command parameters:\n"
130 " 'filename' is a disk image filename\n"
131 " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
132 " manual page for a description of the object properties. The most common\n"
133 " object type is a 'secret', which is used to supply passwords and/or\n"
134 " encryption keys.\n"
135 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
136 " 'cache' is the cache mode used to write the output disk image, the valid\n"
137 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
138 " 'directsync' and 'unsafe' (default for convert)\n"
139 " 'src_cache' is the cache mode used to read input disk images, the valid\n"
140 " options are the same as for the 'cache' option\n"
141 " 'size' is the disk image size in bytes. Optional suffixes\n"
142 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
143 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
144 " supported. 'b' is ignored.\n"
145 " 'output_filename' is the destination disk image filename\n"
146 " 'output_fmt' is the destination format\n"
147 " 'options' is a comma separated list of format specific options in a\n"
148 " name=value format. Use -o ? for an overview of the options supported by the\n"
150 " 'snapshot_param' is param used for internal snapshot, format\n"
151 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
153 " '-c' indicates that target image must be compressed (qcow format only)\n"
154 " '-u' allows unsafe backing chains. For rebasing, it is assumed that old and\n"
155 " new backing file match exactly. The image doesn't need a working\n"
156 " backing file before rebasing in this case (useful for renaming the\n"
157 " backing file). For image creation, allow creating without attempting\n"
158 " to open the backing file.\n"
159 " '-h' with or without a command shows this help and lists the supported formats\n"
160 " '-p' show progress of command (only certain commands)\n"
161 " '-q' use Quiet mode - do not print any output (except errors)\n"
162 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
163 " contain only zeros for qemu-img to create a sparse image during\n"
164 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
165 " unallocated or zero sectors, and the destination image will always be\n"
167 " '--output' takes the format in which the output must be done (human or json)\n"
168 " '-n' skips the target volume creation (useful if the volume is created\n"
169 " prior to running qemu-img)\n"
171 "Parameters to check subcommand:\n"
172 " '-r' tries to repair any inconsistencies that are found during the check.\n"
173 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
174 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
175 " hiding corruption that has already occurred.\n"
177 "Parameters to convert subcommand:\n"
178 " '-m' specifies how many coroutines work in parallel during the convert\n"
179 " process (defaults to 8)\n"
180 " '-W' allow to write to the target out of order rather than sequential\n"
182 "Parameters to snapshot subcommand:\n"
183 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
184 " '-a' applies a snapshot (revert disk to saved state)\n"
185 " '-c' creates a snapshot\n"
186 " '-d' deletes a snapshot\n"
187 " '-l' lists all snapshots in the given image\n"
189 "Parameters to compare subcommand:\n"
190 " '-f' first image format\n"
191 " '-F' second image format\n"
192 " '-s' run in Strict mode - fail on different image size or sector allocation\n"
194 "Parameters to dd subcommand:\n"
195 " 'bs=BYTES' read and write up to BYTES bytes at a time "
197 " 'count=N' copy only N input blocks\n"
198 " 'if=FILE' read from FILE\n"
199 " 'of=FILE' write to FILE\n"
200 " 'skip=N' skip N bs-sized blocks at the start of input\n";
202 printf("%s\nSupported formats:", help_msg
);
203 bdrv_iterate_format(format_print
, NULL
, false);
204 printf("\n\n" QEMU_HELP_BOTTOM
"\n");
208 static QemuOptsList qemu_object_opts
= {
210 .implied_opt_name
= "qom-type",
211 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
217 static QemuOptsList qemu_source_opts
= {
219 .implied_opt_name
= "file",
220 .head
= QTAILQ_HEAD_INITIALIZER(qemu_source_opts
.head
),
226 static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet
, const char *fmt
, ...)
232 ret
= vprintf(fmt
, args
);
239 static int print_block_option_help(const char *filename
, const char *fmt
)
241 BlockDriver
*drv
, *proto_drv
;
242 QemuOptsList
*create_opts
= NULL
;
243 Error
*local_err
= NULL
;
245 /* Find driver and parse its options */
246 drv
= bdrv_find_format(fmt
);
248 error_report("Unknown file format '%s'", fmt
);
252 if (!drv
->create_opts
) {
253 error_report("Format driver '%s' does not support image creation", fmt
);
257 create_opts
= qemu_opts_append(create_opts
, drv
->create_opts
);
259 proto_drv
= bdrv_find_protocol(filename
, true, &local_err
);
261 error_report_err(local_err
);
262 qemu_opts_free(create_opts
);
265 if (!proto_drv
->create_opts
) {
266 error_report("Protocol driver '%s' does not support image creation",
267 proto_drv
->format_name
);
268 qemu_opts_free(create_opts
);
271 create_opts
= qemu_opts_append(create_opts
, proto_drv
->create_opts
);
275 printf("Supported options:\n");
277 printf("Supported %s options:\n", fmt
);
279 qemu_opts_print_help(create_opts
, false);
280 qemu_opts_free(create_opts
);
284 "The protocol level may support further options.\n"
285 "Specify the target filename to include those options.\n");
292 static BlockBackend
*img_open_opts(const char *optstr
,
293 QemuOpts
*opts
, int flags
, bool writethrough
,
294 bool quiet
, bool force_share
)
297 Error
*local_err
= NULL
;
299 options
= qemu_opts_to_qdict(opts
, NULL
);
301 if (qdict_haskey(options
, BDRV_OPT_FORCE_SHARE
)
302 && strcmp(qdict_get_str(options
, BDRV_OPT_FORCE_SHARE
), "on")) {
303 error_report("--force-share/-U conflicts with image options");
304 qobject_unref(options
);
307 qdict_put_str(options
, BDRV_OPT_FORCE_SHARE
, "on");
309 blk
= blk_new_open(NULL
, NULL
, options
, flags
, &local_err
);
311 error_reportf_err(local_err
, "Could not open '%s': ", optstr
);
314 blk_set_enable_write_cache(blk
, !writethrough
);
319 static BlockBackend
*img_open_file(const char *filename
,
321 const char *fmt
, int flags
,
322 bool writethrough
, bool quiet
,
326 Error
*local_err
= NULL
;
329 options
= qdict_new();
332 qdict_put_str(options
, "driver", fmt
);
336 qdict_put_bool(options
, BDRV_OPT_FORCE_SHARE
, true);
338 blk
= blk_new_open(filename
, NULL
, options
, flags
, &local_err
);
340 error_reportf_err(local_err
, "Could not open '%s': ", filename
);
343 blk_set_enable_write_cache(blk
, !writethrough
);
349 static int img_add_key_secrets(void *opaque
,
350 const char *name
, const char *value
,
353 QDict
*options
= opaque
;
355 if (g_str_has_suffix(name
, "key-secret")) {
356 qdict_put_str(options
, name
, value
);
363 static BlockBackend
*img_open(bool image_opts
,
364 const char *filename
,
365 const char *fmt
, int flags
, bool writethrough
,
366 bool quiet
, bool force_share
)
372 error_report("--image-opts and --format are mutually exclusive");
375 opts
= qemu_opts_parse_noisily(qemu_find_opts("source"),
380 blk
= img_open_opts(filename
, opts
, flags
, writethrough
, quiet
,
383 blk
= img_open_file(filename
, NULL
, fmt
, flags
, writethrough
, quiet
,
390 static int add_old_style_options(const char *fmt
, QemuOpts
*opts
,
391 const char *base_filename
,
392 const char *base_fmt
)
397 qemu_opt_set(opts
, BLOCK_OPT_BACKING_FILE
, base_filename
, &err
);
399 error_report("Backing file not supported for file format '%s'",
406 qemu_opt_set(opts
, BLOCK_OPT_BACKING_FMT
, base_fmt
, &err
);
408 error_report("Backing file format not supported for file "
417 static int64_t cvtnum(const char *s
)
422 err
= qemu_strtosz(s
, NULL
, &value
);
426 if (value
> INT64_MAX
) {
432 static int img_create(int argc
, char **argv
)
435 uint64_t img_size
= -1;
436 const char *fmt
= "raw";
437 const char *base_fmt
= NULL
;
438 const char *filename
;
439 const char *base_filename
= NULL
;
440 char *options
= NULL
;
441 Error
*local_err
= NULL
;
446 static const struct option long_options
[] = {
447 {"help", no_argument
, 0, 'h'},
448 {"object", required_argument
, 0, OPTION_OBJECT
},
451 c
= getopt_long(argc
, argv
, ":F:b:f:ho:qu",
458 missing_argument(argv
[optind
- 1]);
461 unrecognized_option(argv
[optind
- 1]);
470 base_filename
= optarg
;
476 if (!is_valid_option_list(optarg
)) {
477 error_report("Invalid option list: %s", optarg
);
481 options
= g_strdup(optarg
);
483 char *old_options
= options
;
484 options
= g_strdup_printf("%s,%s", options
, optarg
);
492 flags
|= BDRV_O_NO_BACKING
;
494 case OPTION_OBJECT
: {
496 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
505 /* Get the filename */
506 filename
= (optind
< argc
) ? argv
[optind
] : NULL
;
507 if (options
&& has_help_option(options
)) {
509 return print_block_option_help(filename
, fmt
);
512 if (optind
>= argc
) {
513 error_exit("Expecting image file name");
517 if (qemu_opts_foreach(&qemu_object_opts
,
518 user_creatable_add_opts_foreach
,
519 NULL
, &error_fatal
)) {
523 /* Get image size, if specified */
527 sval
= cvtnum(argv
[optind
++]);
529 if (sval
== -ERANGE
) {
530 error_report("Image size must be less than 8 EiB!");
532 error_report("Invalid image size specified! You may use k, M, "
533 "G, T, P or E suffixes for ");
534 error_report("kilobytes, megabytes, gigabytes, terabytes, "
535 "petabytes and exabytes.");
539 img_size
= (uint64_t)sval
;
541 if (optind
!= argc
) {
542 error_exit("Unexpected argument: %s", argv
[optind
]);
545 bdrv_img_create(filename
, fmt
, base_filename
, base_fmt
,
546 options
, img_size
, flags
, quiet
, &local_err
);
548 error_reportf_err(local_err
, "%s: ", filename
);
560 static void dump_json_image_check(ImageCheck
*check
, bool quiet
)
564 Visitor
*v
= qobject_output_visitor_new(&obj
);
566 visit_type_ImageCheck(v
, NULL
, &check
, &error_abort
);
567 visit_complete(v
, &obj
);
568 str
= qobject_to_json_pretty(obj
);
570 qprintf(quiet
, "%s\n", qstring_get_str(str
));
576 static void dump_human_image_check(ImageCheck
*check
, bool quiet
)
578 if (!(check
->corruptions
|| check
->leaks
|| check
->check_errors
)) {
579 qprintf(quiet
, "No errors were found on the image.\n");
581 if (check
->corruptions
) {
582 qprintf(quiet
, "\n%" PRId64
" errors were found on the image.\n"
583 "Data may be corrupted, or further writes to the image "
590 "\n%" PRId64
" leaked clusters were found on the image.\n"
591 "This means waste of disk space, but no harm to data.\n",
595 if (check
->check_errors
) {
598 " internal errors have occurred during the check.\n",
599 check
->check_errors
);
603 if (check
->total_clusters
!= 0 && check
->allocated_clusters
!= 0) {
604 qprintf(quiet
, "%" PRId64
"/%" PRId64
" = %0.2f%% allocated, "
605 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
606 check
->allocated_clusters
, check
->total_clusters
,
607 check
->allocated_clusters
* 100.0 / check
->total_clusters
,
608 check
->fragmented_clusters
* 100.0 / check
->allocated_clusters
,
609 check
->compressed_clusters
* 100.0 /
610 check
->allocated_clusters
);
613 if (check
->image_end_offset
) {
615 "Image end offset: %" PRId64
"\n", check
->image_end_offset
);
619 static int collect_image_check(BlockDriverState
*bs
,
621 const char *filename
,
626 BdrvCheckResult result
;
628 ret
= bdrv_check(bs
, &result
, fix
);
633 check
->filename
= g_strdup(filename
);
634 check
->format
= g_strdup(bdrv_get_format_name(bs
));
635 check
->check_errors
= result
.check_errors
;
636 check
->corruptions
= result
.corruptions
;
637 check
->has_corruptions
= result
.corruptions
!= 0;
638 check
->leaks
= result
.leaks
;
639 check
->has_leaks
= result
.leaks
!= 0;
640 check
->corruptions_fixed
= result
.corruptions_fixed
;
641 check
->has_corruptions_fixed
= result
.corruptions
!= 0;
642 check
->leaks_fixed
= result
.leaks_fixed
;
643 check
->has_leaks_fixed
= result
.leaks
!= 0;
644 check
->image_end_offset
= result
.image_end_offset
;
645 check
->has_image_end_offset
= result
.image_end_offset
!= 0;
646 check
->total_clusters
= result
.bfi
.total_clusters
;
647 check
->has_total_clusters
= result
.bfi
.total_clusters
!= 0;
648 check
->allocated_clusters
= result
.bfi
.allocated_clusters
;
649 check
->has_allocated_clusters
= result
.bfi
.allocated_clusters
!= 0;
650 check
->fragmented_clusters
= result
.bfi
.fragmented_clusters
;
651 check
->has_fragmented_clusters
= result
.bfi
.fragmented_clusters
!= 0;
652 check
->compressed_clusters
= result
.bfi
.compressed_clusters
;
653 check
->has_compressed_clusters
= result
.bfi
.compressed_clusters
!= 0;
659 * Checks an image for consistency. Exit codes:
661 * 0 - Check completed, image is good
662 * 1 - Check not completed because of internal errors
663 * 2 - Check completed, image is corrupted
664 * 3 - Check completed, image has leaked clusters, but is good otherwise
665 * 63 - Checks are not supported by the image format
667 static int img_check(int argc
, char **argv
)
670 OutputFormat output_format
= OFORMAT_HUMAN
;
671 const char *filename
, *fmt
, *output
, *cache
;
673 BlockDriverState
*bs
;
675 int flags
= BDRV_O_CHECK
;
679 bool image_opts
= false;
680 bool force_share
= false;
684 cache
= BDRV_DEFAULT_CACHE
;
687 int option_index
= 0;
688 static const struct option long_options
[] = {
689 {"help", no_argument
, 0, 'h'},
690 {"format", required_argument
, 0, 'f'},
691 {"repair", required_argument
, 0, 'r'},
692 {"output", required_argument
, 0, OPTION_OUTPUT
},
693 {"object", required_argument
, 0, OPTION_OBJECT
},
694 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
695 {"force-share", no_argument
, 0, 'U'},
698 c
= getopt_long(argc
, argv
, ":hf:r:T:qU",
699 long_options
, &option_index
);
705 missing_argument(argv
[optind
- 1]);
708 unrecognized_option(argv
[optind
- 1]);
717 flags
|= BDRV_O_RDWR
;
719 if (!strcmp(optarg
, "leaks")) {
720 fix
= BDRV_FIX_LEAKS
;
721 } else if (!strcmp(optarg
, "all")) {
722 fix
= BDRV_FIX_LEAKS
| BDRV_FIX_ERRORS
;
724 error_exit("Unknown option value for -r "
725 "(expecting 'leaks' or 'all'): %s", optarg
);
740 case OPTION_OBJECT
: {
742 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
748 case OPTION_IMAGE_OPTS
:
753 if (optind
!= argc
- 1) {
754 error_exit("Expecting one image file name");
756 filename
= argv
[optind
++];
758 if (output
&& !strcmp(output
, "json")) {
759 output_format
= OFORMAT_JSON
;
760 } else if (output
&& !strcmp(output
, "human")) {
761 output_format
= OFORMAT_HUMAN
;
763 error_report("--output must be used with human or json as argument.");
767 if (qemu_opts_foreach(&qemu_object_opts
,
768 user_creatable_add_opts_foreach
,
769 NULL
, &error_fatal
)) {
773 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
775 error_report("Invalid source cache option: %s", cache
);
779 blk
= img_open(image_opts
, filename
, fmt
, flags
, writethrough
, quiet
,
786 check
= g_new0(ImageCheck
, 1);
787 ret
= collect_image_check(bs
, check
, filename
, fmt
, fix
);
789 if (ret
== -ENOTSUP
) {
790 error_report("This image format does not support checks");
795 if (check
->corruptions_fixed
|| check
->leaks_fixed
) {
796 int corruptions_fixed
, leaks_fixed
;
798 leaks_fixed
= check
->leaks_fixed
;
799 corruptions_fixed
= check
->corruptions_fixed
;
801 if (output_format
== OFORMAT_HUMAN
) {
803 "The following inconsistencies were found and repaired:\n\n"
804 " %" PRId64
" leaked clusters\n"
805 " %" PRId64
" corruptions\n\n"
806 "Double checking the fixed image now...\n",
808 check
->corruptions_fixed
);
811 ret
= collect_image_check(bs
, check
, filename
, fmt
, 0);
813 check
->leaks_fixed
= leaks_fixed
;
814 check
->corruptions_fixed
= corruptions_fixed
;
818 switch (output_format
) {
820 dump_human_image_check(check
, quiet
);
823 dump_json_image_check(check
, quiet
);
828 if (ret
|| check
->check_errors
) {
830 error_report("Check failed: %s", strerror(-ret
));
832 error_report("Check failed");
838 if (check
->corruptions
) {
840 } else if (check
->leaks
) {
847 qapi_free_ImageCheck(check
);
852 typedef struct CommonBlockJobCBInfo
{
853 BlockDriverState
*bs
;
855 } CommonBlockJobCBInfo
;
857 static void common_block_job_cb(void *opaque
, int ret
)
859 CommonBlockJobCBInfo
*cbi
= opaque
;
862 error_setg_errno(cbi
->errp
, -ret
, "Block job failed");
866 static void run_block_job(BlockJob
*job
, Error
**errp
)
868 AioContext
*aio_context
= blk_get_aio_context(job
->blk
);
871 aio_context_acquire(aio_context
);
874 float progress
= 0.0f
;
875 aio_poll(aio_context
, true);
876 if (job
->job
.progress_total
) {
877 progress
= (float)job
->job
.progress_current
/
878 job
->job
.progress_total
* 100.f
;
880 qemu_progress_print(progress
, 0);
881 } while (!job_is_ready(&job
->job
) && !job_is_completed(&job
->job
));
883 if (!job_is_completed(&job
->job
)) {
884 ret
= job_complete_sync(&job
->job
, errp
);
888 job_unref(&job
->job
);
889 aio_context_release(aio_context
);
891 /* publish completion progress only when success */
893 qemu_progress_print(100.f
, 0);
897 static int img_commit(int argc
, char **argv
)
900 const char *filename
, *fmt
, *cache
, *base
;
902 BlockDriverState
*bs
, *base_bs
;
904 bool progress
= false, quiet
= false, drop
= false;
906 Error
*local_err
= NULL
;
907 CommonBlockJobCBInfo cbi
;
908 bool image_opts
= false;
909 AioContext
*aio_context
;
912 cache
= BDRV_DEFAULT_CACHE
;
915 static const struct option long_options
[] = {
916 {"help", no_argument
, 0, 'h'},
917 {"object", required_argument
, 0, OPTION_OBJECT
},
918 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
921 c
= getopt_long(argc
, argv
, ":f:ht:b:dpq",
928 missing_argument(argv
[optind
- 1]);
931 unrecognized_option(argv
[optind
- 1]);
956 case OPTION_OBJECT
: {
958 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
964 case OPTION_IMAGE_OPTS
:
970 /* Progress is not shown in Quiet mode */
975 if (optind
!= argc
- 1) {
976 error_exit("Expecting one image file name");
978 filename
= argv
[optind
++];
980 if (qemu_opts_foreach(&qemu_object_opts
,
981 user_creatable_add_opts_foreach
,
982 NULL
, &error_fatal
)) {
986 flags
= BDRV_O_RDWR
| BDRV_O_UNMAP
;
987 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
989 error_report("Invalid cache option: %s", cache
);
993 blk
= img_open(image_opts
, filename
, fmt
, flags
, writethrough
, quiet
,
1000 qemu_progress_init(progress
, 1.f
);
1001 qemu_progress_print(0.f
, 100);
1004 base_bs
= bdrv_find_backing_image(bs
, base
);
1006 error_setg(&local_err
,
1007 "Did not find '%s' in the backing chain of '%s'",
1012 /* This is different from QMP, which by default uses the deepest file in
1013 * the backing chain (i.e., the very base); however, the traditional
1014 * behavior of qemu-img commit is using the immediate backing file. */
1015 base_bs
= backing_bs(bs
);
1017 error_setg(&local_err
, "Image does not have a backing file");
1022 cbi
= (CommonBlockJobCBInfo
){
1027 aio_context
= bdrv_get_aio_context(bs
);
1028 aio_context_acquire(aio_context
);
1029 commit_active_start("commit", bs
, base_bs
, JOB_DEFAULT
, 0,
1030 BLOCKDEV_ON_ERROR_REPORT
, NULL
, common_block_job_cb
,
1031 &cbi
, false, &local_err
);
1032 aio_context_release(aio_context
);
1037 /* When the block job completes, the BlockBackend reference will point to
1038 * the old backing file. In order to avoid that the top image is already
1039 * deleted, so we can still empty it afterwards, increment the reference
1040 * counter here preemptively. */
1045 job
= block_job_get("commit");
1047 run_block_job(job
, &local_err
);
1052 if (!drop
&& bs
->drv
->bdrv_make_empty
) {
1053 ret
= bs
->drv
->bdrv_make_empty(bs
);
1055 error_setg_errno(&local_err
, -ret
, "Could not empty %s",
1067 qemu_progress_end();
1072 error_report_err(local_err
);
1076 qprintf(quiet
, "Image committed.\n");
1081 * Returns -1 if 'buf' contains only zeroes, otherwise the byte index
1082 * of the first sector boundary within buf where the sector contains a
1083 * non-zero byte. This function is robust to a buffer that is not
1086 static int64_t find_nonzero(const uint8_t *buf
, int64_t n
)
1089 int64_t end
= QEMU_ALIGN_DOWN(n
, BDRV_SECTOR_SIZE
);
1091 for (i
= 0; i
< end
; i
+= BDRV_SECTOR_SIZE
) {
1092 if (!buffer_is_zero(buf
+ i
, BDRV_SECTOR_SIZE
)) {
1096 if (i
< n
&& !buffer_is_zero(buf
+ i
, n
- end
)) {
1103 * Returns true iff the first sector pointed to by 'buf' contains at least
1106 * 'pnum' is set to the number of sectors (including and immediately following
1107 * the first one) that are known to be in the same allocated/unallocated state.
1108 * The function will try to align the end offset to alignment boundaries so
1109 * that the request will at least end aligned and consequtive requests will
1110 * also start at an aligned offset.
1112 static int is_allocated_sectors(const uint8_t *buf
, int n
, int *pnum
,
1113 int64_t sector_num
, int alignment
)
1122 is_zero
= buffer_is_zero(buf
, 512);
1123 for(i
= 1; i
< n
; i
++) {
1125 if (is_zero
!= buffer_is_zero(buf
, 512)) {
1130 tail
= (sector_num
+ i
) & (alignment
- 1);
1132 if (is_zero
&& i
<= tail
) {
1133 /* treat unallocated areas which only consist
1134 * of a small tail as allocated. */
1138 /* align up end offset of allocated areas. */
1139 i
+= alignment
- tail
;
1142 /* align down end offset of zero areas. */
1151 * Like is_allocated_sectors, but if the buffer starts with a used sector,
1152 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1153 * breaking up write requests for only small sparse areas.
1155 static int is_allocated_sectors_min(const uint8_t *buf
, int n
, int *pnum
,
1156 int min
, int64_t sector_num
, int alignment
)
1159 int num_checked
, num_used
;
1165 ret
= is_allocated_sectors(buf
, n
, pnum
, sector_num
, alignment
);
1171 buf
+= BDRV_SECTOR_SIZE
* *pnum
;
1173 sector_num
+= *pnum
;
1174 num_checked
= num_used
;
1177 ret
= is_allocated_sectors(buf
, n
, pnum
, sector_num
, alignment
);
1179 buf
+= BDRV_SECTOR_SIZE
* *pnum
;
1181 sector_num
+= *pnum
;
1182 num_checked
+= *pnum
;
1184 num_used
= num_checked
;
1185 } else if (*pnum
>= min
) {
1195 * Compares two buffers sector by sector. Returns 0 if the first
1196 * sector of each buffer matches, non-zero otherwise.
1198 * pnum is set to the sector-aligned size of the buffer prefix that
1199 * has the same matching status as the first sector.
1201 static int compare_buffers(const uint8_t *buf1
, const uint8_t *buf2
,
1202 int64_t bytes
, int64_t *pnum
)
1205 int64_t i
= MIN(bytes
, BDRV_SECTOR_SIZE
);
1209 res
= !!memcmp(buf1
, buf2
, i
);
1211 int64_t len
= MIN(bytes
- i
, BDRV_SECTOR_SIZE
);
1213 if (!!memcmp(buf1
+ i
, buf2
+ i
, len
) != res
) {
1223 #define IO_BUF_SIZE (2 * MiB)
1226 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1228 * Intended for use by 'qemu-img compare': Returns 0 in case sectors are
1229 * filled with 0, 1 if sectors contain non-zero data (this is a comparison
1230 * failure), and 4 on error (the exit status for read errors), after emitting
1233 * @param blk: BlockBackend for the image
1234 * @param offset: Starting offset to check
1235 * @param bytes: Number of bytes to check
1236 * @param filename: Name of disk file we are checking (logging purpose)
1237 * @param buffer: Allocated buffer for storing read data
1238 * @param quiet: Flag for quiet mode
1240 static int check_empty_sectors(BlockBackend
*blk
, int64_t offset
,
1241 int64_t bytes
, const char *filename
,
1242 uint8_t *buffer
, bool quiet
)
1247 ret
= blk_pread(blk
, offset
, buffer
, bytes
);
1249 error_report("Error while reading offset %" PRId64
" of %s: %s",
1250 offset
, filename
, strerror(-ret
));
1253 idx
= find_nonzero(buffer
, bytes
);
1255 qprintf(quiet
, "Content mismatch at offset %" PRId64
"!\n",
1264 * Compares two images. Exit codes:
1266 * 0 - Images are identical
1268 * >1 - Error occurred
1270 static int img_compare(int argc
, char **argv
)
1272 const char *fmt1
= NULL
, *fmt2
= NULL
, *cache
, *filename1
, *filename2
;
1273 BlockBackend
*blk1
, *blk2
;
1274 BlockDriverState
*bs1
, *bs2
;
1275 int64_t total_size1
, total_size2
;
1276 uint8_t *buf1
= NULL
, *buf2
= NULL
;
1277 int64_t pnum1
, pnum2
;
1278 int allocated1
, allocated2
;
1279 int ret
= 0; /* return value - 0 Ident, 1 Different, >1 Error */
1280 bool progress
= false, quiet
= false, strict
= false;
1287 uint64_t progress_base
;
1288 bool image_opts
= false;
1289 bool force_share
= false;
1291 cache
= BDRV_DEFAULT_CACHE
;
1293 static const struct option long_options
[] = {
1294 {"help", no_argument
, 0, 'h'},
1295 {"object", required_argument
, 0, OPTION_OBJECT
},
1296 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
1297 {"force-share", no_argument
, 0, 'U'},
1300 c
= getopt_long(argc
, argv
, ":hf:F:T:pqsU",
1301 long_options
, NULL
);
1307 missing_argument(argv
[optind
- 1]);
1310 unrecognized_option(argv
[optind
- 1]);
1336 case OPTION_OBJECT
: {
1338 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
1345 case OPTION_IMAGE_OPTS
:
1351 /* Progress is not shown in Quiet mode */
1357 if (optind
!= argc
- 2) {
1358 error_exit("Expecting two image file names");
1360 filename1
= argv
[optind
++];
1361 filename2
= argv
[optind
++];
1363 if (qemu_opts_foreach(&qemu_object_opts
,
1364 user_creatable_add_opts_foreach
,
1365 NULL
, &error_fatal
)) {
1370 /* Initialize before goto out */
1371 qemu_progress_init(progress
, 2.0);
1374 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
1376 error_report("Invalid source cache option: %s", cache
);
1381 blk1
= img_open(image_opts
, filename1
, fmt1
, flags
, writethrough
, quiet
,
1388 blk2
= img_open(image_opts
, filename2
, fmt2
, flags
, writethrough
, quiet
,
1397 buf1
= blk_blockalign(blk1
, IO_BUF_SIZE
);
1398 buf2
= blk_blockalign(blk2
, IO_BUF_SIZE
);
1399 total_size1
= blk_getlength(blk1
);
1400 if (total_size1
< 0) {
1401 error_report("Can't get size of %s: %s",
1402 filename1
, strerror(-total_size1
));
1406 total_size2
= blk_getlength(blk2
);
1407 if (total_size2
< 0) {
1408 error_report("Can't get size of %s: %s",
1409 filename2
, strerror(-total_size2
));
1413 total_size
= MIN(total_size1
, total_size2
);
1414 progress_base
= MAX(total_size1
, total_size2
);
1416 qemu_progress_print(0, 100);
1418 if (strict
&& total_size1
!= total_size2
) {
1420 qprintf(quiet
, "Strict mode: Image size mismatch!\n");
1424 while (offset
< total_size
) {
1425 int status1
, status2
;
1427 status1
= bdrv_block_status_above(bs1
, NULL
, offset
,
1428 total_size1
- offset
, &pnum1
, NULL
,
1432 error_report("Sector allocation test failed for %s", filename1
);
1435 allocated1
= status1
& BDRV_BLOCK_ALLOCATED
;
1437 status2
= bdrv_block_status_above(bs2
, NULL
, offset
,
1438 total_size2
- offset
, &pnum2
, NULL
,
1442 error_report("Sector allocation test failed for %s", filename2
);
1445 allocated2
= status2
& BDRV_BLOCK_ALLOCATED
;
1447 assert(pnum1
&& pnum2
);
1448 chunk
= MIN(pnum1
, pnum2
);
1451 if (status1
!= status2
) {
1453 qprintf(quiet
, "Strict mode: Offset %" PRId64
1454 " block status mismatch!\n", offset
);
1458 if ((status1
& BDRV_BLOCK_ZERO
) && (status2
& BDRV_BLOCK_ZERO
)) {
1460 } else if (allocated1
== allocated2
) {
1464 chunk
= MIN(chunk
, IO_BUF_SIZE
);
1465 ret
= blk_pread(blk1
, offset
, buf1
, chunk
);
1467 error_report("Error while reading offset %" PRId64
1469 offset
, filename1
, strerror(-ret
));
1473 ret
= blk_pread(blk2
, offset
, buf2
, chunk
);
1475 error_report("Error while reading offset %" PRId64
1477 offset
, filename2
, strerror(-ret
));
1481 ret
= compare_buffers(buf1
, buf2
, chunk
, &pnum
);
1482 if (ret
|| pnum
!= chunk
) {
1483 qprintf(quiet
, "Content mismatch at offset %" PRId64
"!\n",
1484 offset
+ (ret
? 0 : pnum
));
1490 chunk
= MIN(chunk
, IO_BUF_SIZE
);
1492 ret
= check_empty_sectors(blk1
, offset
, chunk
,
1493 filename1
, buf1
, quiet
);
1495 ret
= check_empty_sectors(blk2
, offset
, chunk
,
1496 filename2
, buf1
, quiet
);
1503 qemu_progress_print(((float) chunk
/ progress_base
) * 100, 100);
1506 if (total_size1
!= total_size2
) {
1507 BlockBackend
*blk_over
;
1508 const char *filename_over
;
1510 qprintf(quiet
, "Warning: Image size mismatch!\n");
1511 if (total_size1
> total_size2
) {
1513 filename_over
= filename1
;
1516 filename_over
= filename2
;
1519 while (offset
< progress_base
) {
1520 ret
= bdrv_block_status_above(blk_bs(blk_over
), NULL
, offset
,
1521 progress_base
- offset
, &chunk
,
1525 error_report("Sector allocation test failed for %s",
1530 if (ret
& BDRV_BLOCK_ALLOCATED
&& !(ret
& BDRV_BLOCK_ZERO
)) {
1531 chunk
= MIN(chunk
, IO_BUF_SIZE
);
1532 ret
= check_empty_sectors(blk_over
, offset
, chunk
,
1533 filename_over
, buf1
, quiet
);
1539 qemu_progress_print(((float) chunk
/ progress_base
) * 100, 100);
1543 qprintf(quiet
, "Images are identical.\n");
1553 qemu_progress_end();
1558 enum ImgConvertBlockStatus
{
1564 #define MAX_COROUTINES 16
1566 typedef struct ImgConvertState
{
1568 int64_t *src_sectors
;
1570 int64_t total_sectors
;
1571 int64_t allocated_sectors
;
1572 int64_t allocated_done
;
1575 enum ImgConvertBlockStatus status
;
1576 int64_t sector_next_status
;
1577 BlockBackend
*target
;
1580 bool unallocated_blocks_are_zero
;
1582 bool target_has_backing
;
1583 int64_t target_backing_sectors
; /* negative if unknown */
1590 size_t cluster_sectors
;
1592 long num_coroutines
;
1593 int running_coroutines
;
1594 Coroutine
*co
[MAX_COROUTINES
];
1595 int64_t wait_sector_num
[MAX_COROUTINES
];
1600 static void convert_select_part(ImgConvertState
*s
, int64_t sector_num
,
1601 int *src_cur
, int64_t *src_cur_offset
)
1604 *src_cur_offset
= 0;
1605 while (sector_num
- *src_cur_offset
>= s
->src_sectors
[*src_cur
]) {
1606 *src_cur_offset
+= s
->src_sectors
[*src_cur
];
1608 assert(*src_cur
< s
->src_num
);
1612 static int convert_iteration_sectors(ImgConvertState
*s
, int64_t sector_num
)
1614 int64_t src_cur_offset
;
1615 int ret
, n
, src_cur
;
1616 bool post_backing_zero
= false;
1618 convert_select_part(s
, sector_num
, &src_cur
, &src_cur_offset
);
1620 assert(s
->total_sectors
> sector_num
);
1621 n
= MIN(s
->total_sectors
- sector_num
, BDRV_REQUEST_MAX_SECTORS
);
1623 if (s
->target_backing_sectors
>= 0) {
1624 if (sector_num
>= s
->target_backing_sectors
) {
1625 post_backing_zero
= s
->unallocated_blocks_are_zero
;
1626 } else if (sector_num
+ n
> s
->target_backing_sectors
) {
1627 /* Split requests around target_backing_sectors (because
1628 * starting from there, zeros are handled differently) */
1629 n
= s
->target_backing_sectors
- sector_num
;
1633 if (s
->sector_next_status
<= sector_num
) {
1634 uint64_t offset
= (sector_num
- src_cur_offset
) * BDRV_SECTOR_SIZE
;
1638 count
= n
* BDRV_SECTOR_SIZE
;
1640 if (s
->target_has_backing
) {
1641 ret
= bdrv_block_status(blk_bs(s
->src
[src_cur
]), offset
,
1642 count
, &count
, NULL
, NULL
);
1644 ret
= bdrv_block_status_above(blk_bs(s
->src
[src_cur
]), NULL
,
1645 offset
, count
, &count
, NULL
,
1653 warn_report("error while reading block status at "
1654 "offset %" PRIu64
": %s", offset
,
1657 /* Just try to read the data, then */
1658 ret
= BDRV_BLOCK_DATA
;
1659 count
= BDRV_SECTOR_SIZE
;
1661 /* Retry on a shorter range */
1662 n
= DIV_ROUND_UP(n
, 4);
1665 error_report("error while reading block status at offset "
1666 "%" PRIu64
": %s", offset
, strerror(-ret
));
1672 n
= DIV_ROUND_UP(count
, BDRV_SECTOR_SIZE
);
1674 if (ret
& BDRV_BLOCK_ZERO
) {
1675 s
->status
= post_backing_zero
? BLK_BACKING_FILE
: BLK_ZERO
;
1676 } else if (ret
& BDRV_BLOCK_DATA
) {
1677 s
->status
= BLK_DATA
;
1679 s
->status
= s
->target_has_backing
? BLK_BACKING_FILE
: BLK_DATA
;
1682 s
->sector_next_status
= sector_num
+ n
;
1685 n
= MIN(n
, s
->sector_next_status
- sector_num
);
1686 if (s
->status
== BLK_DATA
) {
1687 n
= MIN(n
, s
->buf_sectors
);
1690 /* We need to write complete clusters for compressed images, so if an
1691 * unallocated area is shorter than that, we must consider the whole
1692 * cluster allocated. */
1693 if (s
->compressed
) {
1694 if (n
< s
->cluster_sectors
) {
1695 n
= MIN(s
->cluster_sectors
, s
->total_sectors
- sector_num
);
1696 s
->status
= BLK_DATA
;
1698 n
= QEMU_ALIGN_DOWN(n
, s
->cluster_sectors
);
1705 static int coroutine_fn
convert_co_read(ImgConvertState
*s
, int64_t sector_num
,
1706 int nb_sectors
, uint8_t *buf
)
1708 uint64_t single_read_until
= 0;
1711 assert(nb_sectors
<= s
->buf_sectors
);
1712 while (nb_sectors
> 0) {
1715 int64_t bs_sectors
, src_cur_offset
;
1718 /* In the case of compression with multiple source files, we can get a
1719 * nb_sectors that spreads into the next part. So we must be able to
1720 * read across multiple BDSes for one convert_read() call. */
1721 convert_select_part(s
, sector_num
, &src_cur
, &src_cur_offset
);
1722 blk
= s
->src
[src_cur
];
1723 bs_sectors
= s
->src_sectors
[src_cur
];
1725 offset
= (sector_num
- src_cur_offset
) << BDRV_SECTOR_BITS
;
1727 n
= MIN(nb_sectors
, bs_sectors
- (sector_num
- src_cur_offset
));
1728 if (single_read_until
> offset
) {
1732 ret
= blk_co_pread(blk
, offset
, n
<< BDRV_SECTOR_BITS
, buf
, 0);
1736 single_read_until
= offset
+ (n
<< BDRV_SECTOR_BITS
);
1740 warn_report("error while reading offset %" PRIu64
1741 ": %s", offset
, strerror(-ret
));
1743 memset(buf
, 0, BDRV_SECTOR_SIZE
);
1752 buf
+= n
* BDRV_SECTOR_SIZE
;
1759 static int coroutine_fn
convert_co_write(ImgConvertState
*s
, int64_t sector_num
,
1760 int nb_sectors
, uint8_t *buf
,
1761 enum ImgConvertBlockStatus status
)
1765 while (nb_sectors
> 0) {
1767 BdrvRequestFlags flags
= s
->compressed
? BDRV_REQ_WRITE_COMPRESSED
: 0;
1770 case BLK_BACKING_FILE
:
1771 /* If we have a backing file, leave clusters unallocated that are
1772 * unallocated in the source image, so that the backing file is
1773 * visible at the respective offset. */
1774 assert(s
->target_has_backing
);
1778 /* If we're told to keep the target fully allocated (-S 0) or there
1779 * is real non-zero data, we must write it. Otherwise we can treat
1780 * it as zero sectors.
1781 * Compressed clusters need to be written as a whole, so in that
1782 * case we can only save the write if the buffer is completely
1784 if (!s
->min_sparse
||
1786 is_allocated_sectors_min(buf
, n
, &n
, s
->min_sparse
,
1787 sector_num
, s
->alignment
)) ||
1789 !buffer_is_zero(buf
, n
* BDRV_SECTOR_SIZE
)))
1791 ret
= blk_co_pwrite(s
->target
, sector_num
<< BDRV_SECTOR_BITS
,
1792 n
<< BDRV_SECTOR_BITS
, buf
, flags
);
1801 if (s
->has_zero_init
) {
1802 assert(!s
->target_has_backing
);
1805 ret
= blk_co_pwrite_zeroes(s
->target
,
1806 sector_num
<< BDRV_SECTOR_BITS
,
1807 n
<< BDRV_SECTOR_BITS
,
1808 BDRV_REQ_MAY_UNMAP
);
1817 buf
+= n
* BDRV_SECTOR_SIZE
;
1823 static int coroutine_fn
convert_co_copy_range(ImgConvertState
*s
, int64_t sector_num
,
1828 while (nb_sectors
> 0) {
1831 int64_t bs_sectors
, src_cur_offset
;
1834 convert_select_part(s
, sector_num
, &src_cur
, &src_cur_offset
);
1835 offset
= (sector_num
- src_cur_offset
) << BDRV_SECTOR_BITS
;
1836 blk
= s
->src
[src_cur
];
1837 bs_sectors
= s
->src_sectors
[src_cur
];
1839 n
= MIN(nb_sectors
, bs_sectors
- (sector_num
- src_cur_offset
));
1841 ret
= blk_co_copy_range(blk
, offset
, s
->target
,
1842 sector_num
<< BDRV_SECTOR_BITS
,
1843 n
<< BDRV_SECTOR_BITS
, 0, 0);
1854 static void coroutine_fn
convert_co_do_copy(void *opaque
)
1856 ImgConvertState
*s
= opaque
;
1857 uint8_t *buf
= NULL
;
1861 for (i
= 0; i
< s
->num_coroutines
; i
++) {
1862 if (s
->co
[i
] == qemu_coroutine_self()) {
1869 s
->running_coroutines
++;
1870 buf
= blk_blockalign(s
->target
, s
->buf_sectors
* BDRV_SECTOR_SIZE
);
1875 enum ImgConvertBlockStatus status
;
1878 qemu_co_mutex_lock(&s
->lock
);
1879 if (s
->ret
!= -EINPROGRESS
|| s
->sector_num
>= s
->total_sectors
) {
1880 qemu_co_mutex_unlock(&s
->lock
);
1883 n
= convert_iteration_sectors(s
, s
->sector_num
);
1885 qemu_co_mutex_unlock(&s
->lock
);
1889 /* save current sector and allocation status to local variables */
1890 sector_num
= s
->sector_num
;
1892 if (!s
->min_sparse
&& s
->status
== BLK_ZERO
) {
1893 n
= MIN(n
, s
->buf_sectors
);
1895 /* increment global sector counter so that other coroutines can
1896 * already continue reading beyond this request */
1898 qemu_co_mutex_unlock(&s
->lock
);
1900 if (status
== BLK_DATA
|| (!s
->min_sparse
&& status
== BLK_ZERO
)) {
1901 s
->allocated_done
+= n
;
1902 qemu_progress_print(100.0 * s
->allocated_done
/
1903 s
->allocated_sectors
, 0);
1907 copy_range
= s
->copy_range
&& s
->status
== BLK_DATA
;
1908 if (status
== BLK_DATA
&& !copy_range
) {
1909 ret
= convert_co_read(s
, sector_num
, n
, buf
);
1911 error_report("error while reading sector %" PRId64
1912 ": %s", sector_num
, strerror(-ret
));
1915 } else if (!s
->min_sparse
&& status
== BLK_ZERO
) {
1917 memset(buf
, 0x00, n
* BDRV_SECTOR_SIZE
);
1920 if (s
->wr_in_order
) {
1921 /* keep writes in order */
1922 while (s
->wr_offs
!= sector_num
&& s
->ret
== -EINPROGRESS
) {
1923 s
->wait_sector_num
[index
] = sector_num
;
1924 qemu_coroutine_yield();
1926 s
->wait_sector_num
[index
] = -1;
1929 if (s
->ret
== -EINPROGRESS
) {
1931 ret
= convert_co_copy_range(s
, sector_num
, n
);
1933 s
->copy_range
= false;
1937 ret
= convert_co_write(s
, sector_num
, n
, buf
, status
);
1940 error_report("error while writing sector %" PRId64
1941 ": %s", sector_num
, strerror(-ret
));
1946 if (s
->wr_in_order
) {
1947 /* reenter the coroutine that might have waited
1948 * for this write to complete */
1949 s
->wr_offs
= sector_num
+ n
;
1950 for (i
= 0; i
< s
->num_coroutines
; i
++) {
1951 if (s
->co
[i
] && s
->wait_sector_num
[i
] == s
->wr_offs
) {
1953 * A -> B -> A cannot occur because A has
1954 * s->wait_sector_num[i] == -1 during A -> B. Therefore
1955 * B will never enter A during this time window.
1957 qemu_coroutine_enter(s
->co
[i
]);
1965 s
->co
[index
] = NULL
;
1966 s
->running_coroutines
--;
1967 if (!s
->running_coroutines
&& s
->ret
== -EINPROGRESS
) {
1968 /* the convert job finished successfully */
1973 static int convert_do_copy(ImgConvertState
*s
)
1976 int64_t sector_num
= 0;
1978 /* Check whether we have zero initialisation or can get it efficiently */
1979 if (s
->target_is_new
&& s
->min_sparse
&& !s
->target_has_backing
) {
1980 s
->has_zero_init
= bdrv_has_zero_init(blk_bs(s
->target
));
1982 s
->has_zero_init
= false;
1985 if (!s
->has_zero_init
&& !s
->target_has_backing
&&
1986 bdrv_can_write_zeroes_with_unmap(blk_bs(s
->target
)))
1988 ret
= blk_make_zero(s
->target
, BDRV_REQ_MAY_UNMAP
| BDRV_REQ_NO_FALLBACK
);
1990 s
->has_zero_init
= true;
1994 /* Allocate buffer for copied data. For compressed images, only one cluster
1995 * can be copied at a time. */
1996 if (s
->compressed
) {
1997 if (s
->cluster_sectors
<= 0 || s
->cluster_sectors
> s
->buf_sectors
) {
1998 error_report("invalid cluster size");
2001 s
->buf_sectors
= s
->cluster_sectors
;
2004 while (sector_num
< s
->total_sectors
) {
2005 n
= convert_iteration_sectors(s
, sector_num
);
2009 if (s
->status
== BLK_DATA
|| (!s
->min_sparse
&& s
->status
== BLK_ZERO
))
2011 s
->allocated_sectors
+= n
;
2017 s
->sector_next_status
= 0;
2018 s
->ret
= -EINPROGRESS
;
2020 qemu_co_mutex_init(&s
->lock
);
2021 for (i
= 0; i
< s
->num_coroutines
; i
++) {
2022 s
->co
[i
] = qemu_coroutine_create(convert_co_do_copy
, s
);
2023 s
->wait_sector_num
[i
] = -1;
2024 qemu_coroutine_enter(s
->co
[i
]);
2027 while (s
->running_coroutines
) {
2028 main_loop_wait(false);
2031 if (s
->compressed
&& !s
->ret
) {
2032 /* signal EOF to align */
2033 ret
= blk_pwrite_compressed(s
->target
, 0, NULL
, 0);
2042 #define MAX_BUF_SECTORS 32768
2044 static int img_convert(int argc
, char **argv
)
2046 int c
, bs_i
, flags
, src_flags
= 0;
2047 const char *fmt
= NULL
, *out_fmt
= NULL
, *cache
= "unsafe",
2048 *src_cache
= BDRV_DEFAULT_CACHE
, *out_baseimg
= NULL
,
2049 *out_filename
, *out_baseimg_param
, *snapshot_name
= NULL
;
2050 BlockDriver
*drv
= NULL
, *proto_drv
= NULL
;
2051 BlockDriverInfo bdi
;
2052 BlockDriverState
*out_bs
;
2053 QemuOpts
*opts
= NULL
, *sn_opts
= NULL
;
2054 QemuOptsList
*create_opts
= NULL
;
2055 QDict
*open_opts
= NULL
;
2056 char *options
= NULL
;
2057 Error
*local_err
= NULL
;
2058 bool writethrough
, src_writethrough
, image_opts
= false,
2059 skip_create
= false, progress
= false, tgt_image_opts
= false;
2060 int64_t ret
= -EINVAL
;
2061 bool force_share
= false;
2062 bool explict_min_sparse
= false;
2064 ImgConvertState s
= (ImgConvertState
) {
2065 /* Need at least 4k of zeros for sparse detection */
2067 .copy_range
= false,
2068 .buf_sectors
= IO_BUF_SIZE
/ BDRV_SECTOR_SIZE
,
2069 .wr_in_order
= true,
2070 .num_coroutines
= 8,
2074 static const struct option long_options
[] = {
2075 {"help", no_argument
, 0, 'h'},
2076 {"object", required_argument
, 0, OPTION_OBJECT
},
2077 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
2078 {"force-share", no_argument
, 0, 'U'},
2079 {"target-image-opts", no_argument
, 0, OPTION_TARGET_IMAGE_OPTS
},
2080 {"salvage", no_argument
, 0, OPTION_SALVAGE
},
2083 c
= getopt_long(argc
, argv
, ":hf:O:B:Cco:l:S:pt:T:qnm:WU",
2084 long_options
, NULL
);
2090 missing_argument(argv
[optind
- 1]);
2093 unrecognized_option(argv
[optind
- 1]);
2105 out_baseimg
= optarg
;
2108 s
.copy_range
= true;
2111 s
.compressed
= true;
2114 if (!is_valid_option_list(optarg
)) {
2115 error_report("Invalid option list: %s", optarg
);
2119 options
= g_strdup(optarg
);
2121 char *old_options
= options
;
2122 options
= g_strdup_printf("%s,%s", options
, optarg
);
2123 g_free(old_options
);
2127 if (strstart(optarg
, SNAPSHOT_OPT_BASE
, NULL
)) {
2128 sn_opts
= qemu_opts_parse_noisily(&internal_snapshot_opts
,
2131 error_report("Failed in parsing snapshot param '%s'",
2136 snapshot_name
= optarg
;
2143 sval
= cvtnum(optarg
);
2144 if (sval
< 0 || sval
& (BDRV_SECTOR_SIZE
- 1) ||
2145 sval
/ BDRV_SECTOR_SIZE
> MAX_BUF_SECTORS
) {
2146 error_report("Invalid buffer size for sparse output specified. "
2147 "Valid sizes are multiples of %llu up to %llu. Select "
2148 "0 to disable sparse detection (fully allocates output).",
2149 BDRV_SECTOR_SIZE
, MAX_BUF_SECTORS
* BDRV_SECTOR_SIZE
);
2153 s
.min_sparse
= sval
/ BDRV_SECTOR_SIZE
;
2154 explict_min_sparse
= true;
2173 if (qemu_strtol(optarg
, NULL
, 0, &s
.num_coroutines
) ||
2174 s
.num_coroutines
< 1 || s
.num_coroutines
> MAX_COROUTINES
) {
2175 error_report("Invalid number of coroutines. Allowed number of"
2176 " coroutines is between 1 and %d", MAX_COROUTINES
);
2181 s
.wr_in_order
= false;
2186 case OPTION_OBJECT
: {
2187 QemuOpts
*object_opts
;
2188 object_opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
2195 case OPTION_IMAGE_OPTS
:
2198 case OPTION_SALVAGE
:
2201 case OPTION_TARGET_IMAGE_OPTS
:
2202 tgt_image_opts
= true;
2207 if (!out_fmt
&& !tgt_image_opts
) {
2211 if (qemu_opts_foreach(&qemu_object_opts
,
2212 user_creatable_add_opts_foreach
,
2213 NULL
, &error_fatal
)) {
2217 if (s
.compressed
&& s
.copy_range
) {
2218 error_report("Cannot enable copy offloading when -c is used");
2222 if (explict_min_sparse
&& s
.copy_range
) {
2223 error_report("Cannot enable copy offloading when -S is used");
2227 if (s
.copy_range
&& s
.salvage
) {
2228 error_report("Cannot use copy offloading in salvaging mode");
2232 if (tgt_image_opts
&& !skip_create
) {
2233 error_report("--target-image-opts requires use of -n flag");
2237 if (skip_create
&& options
) {
2238 warn_report("-o has no effect when skipping image creation");
2239 warn_report("This will become an error in future QEMU versions.");
2242 s
.src_num
= argc
- optind
- 1;
2243 out_filename
= s
.src_num
>= 1 ? argv
[argc
- 1] : NULL
;
2245 if (options
&& has_help_option(options
)) {
2247 ret
= print_block_option_help(out_filename
, out_fmt
);
2250 error_report("Option help requires a format be specified");
2255 if (s
.src_num
< 1) {
2256 error_report("Must specify image file name");
2261 /* ret is still -EINVAL until here */
2262 ret
= bdrv_parse_cache_mode(src_cache
, &src_flags
, &src_writethrough
);
2264 error_report("Invalid source cache option: %s", src_cache
);
2268 /* Initialize before goto out */
2272 qemu_progress_init(progress
, 1.0);
2273 qemu_progress_print(0, 100);
2275 s
.src
= g_new0(BlockBackend
*, s
.src_num
);
2276 s
.src_sectors
= g_new(int64_t, s
.src_num
);
2278 for (bs_i
= 0; bs_i
< s
.src_num
; bs_i
++) {
2279 s
.src
[bs_i
] = img_open(image_opts
, argv
[optind
+ bs_i
],
2280 fmt
, src_flags
, src_writethrough
, s
.quiet
,
2286 s
.src_sectors
[bs_i
] = blk_nb_sectors(s
.src
[bs_i
]);
2287 if (s
.src_sectors
[bs_i
] < 0) {
2288 error_report("Could not get size of %s: %s",
2289 argv
[optind
+ bs_i
], strerror(-s
.src_sectors
[bs_i
]));
2293 s
.total_sectors
+= s
.src_sectors
[bs_i
];
2297 bdrv_snapshot_load_tmp(blk_bs(s
.src
[0]),
2298 qemu_opt_get(sn_opts
, SNAPSHOT_OPT_ID
),
2299 qemu_opt_get(sn_opts
, SNAPSHOT_OPT_NAME
),
2301 } else if (snapshot_name
!= NULL
) {
2302 if (s
.src_num
> 1) {
2303 error_report("No support for concatenating multiple snapshot");
2308 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s
.src
[0]), snapshot_name
,
2312 error_reportf_err(local_err
, "Failed to load snapshot: ");
2318 /* Find driver and parse its options */
2319 drv
= bdrv_find_format(out_fmt
);
2321 error_report("Unknown file format '%s'", out_fmt
);
2326 proto_drv
= bdrv_find_protocol(out_filename
, true, &local_err
);
2328 error_report_err(local_err
);
2333 if (!drv
->create_opts
) {
2334 error_report("Format driver '%s' does not support image creation",
2340 if (!proto_drv
->create_opts
) {
2341 error_report("Protocol driver '%s' does not support image creation",
2342 proto_drv
->format_name
);
2347 create_opts
= qemu_opts_append(create_opts
, drv
->create_opts
);
2348 create_opts
= qemu_opts_append(create_opts
, proto_drv
->create_opts
);
2350 opts
= qemu_opts_create(create_opts
, NULL
, 0, &error_abort
);
2352 qemu_opts_do_parse(opts
, options
, NULL
, &local_err
);
2354 error_report_err(local_err
);
2360 qemu_opt_set_number(opts
, BLOCK_OPT_SIZE
, s
.total_sectors
* 512,
2362 ret
= add_old_style_options(out_fmt
, opts
, out_baseimg
, NULL
);
2368 /* Get backing file name if -o backing_file was used */
2369 out_baseimg_param
= qemu_opt_get(opts
, BLOCK_OPT_BACKING_FILE
);
2370 if (out_baseimg_param
) {
2371 out_baseimg
= out_baseimg_param
;
2373 s
.target_has_backing
= (bool) out_baseimg
;
2375 if (s
.src_num
> 1 && out_baseimg
) {
2376 error_report("Having a backing file for the target makes no sense when "
2377 "concatenating multiple input images");
2382 /* Check if compression is supported */
2385 qemu_opt_get_bool(opts
, BLOCK_OPT_ENCRYPT
, false);
2386 const char *encryptfmt
=
2387 qemu_opt_get(opts
, BLOCK_OPT_ENCRYPT_FORMAT
);
2388 const char *preallocation
=
2389 qemu_opt_get(opts
, BLOCK_OPT_PREALLOC
);
2391 if (drv
&& !block_driver_can_compress(drv
)) {
2392 error_report("Compression not supported for this file format");
2397 if (encryption
|| encryptfmt
) {
2398 error_report("Compression and encryption not supported at "
2405 && strcmp(preallocation
, "off"))
2407 error_report("Compression and preallocation not supported at "
2415 * The later open call will need any decryption secrets, and
2416 * bdrv_create() will purge "opts", so extract them now before
2420 open_opts
= qdict_new();
2421 qemu_opt_foreach(opts
, img_add_key_secrets
, open_opts
, &error_abort
);
2425 /* Create the new image */
2426 ret
= bdrv_create(drv
, out_filename
, opts
, &local_err
);
2428 error_reportf_err(local_err
, "%s: error while converting %s: ",
2429 out_filename
, out_fmt
);
2434 s
.target_is_new
= !skip_create
;
2436 flags
= s
.min_sparse
? (BDRV_O_RDWR
| BDRV_O_UNMAP
) : BDRV_O_RDWR
;
2437 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
2439 error_report("Invalid cache option: %s", cache
);
2444 s
.target
= img_open(tgt_image_opts
, out_filename
, out_fmt
,
2445 flags
, writethrough
, s
.quiet
, false);
2447 /* TODO ultimately we should allow --target-image-opts
2448 * to be used even when -n is not given.
2449 * That has to wait for bdrv_create to be improved
2450 * to allow filenames in option syntax
2452 s
.target
= img_open_file(out_filename
, open_opts
, out_fmt
,
2453 flags
, writethrough
, s
.quiet
, false);
2454 open_opts
= NULL
; /* blk_new_open will have freed it */
2460 out_bs
= blk_bs(s
.target
);
2462 if (s
.compressed
&& !block_driver_can_compress(out_bs
->drv
)) {
2463 error_report("Compression not supported for this file format");
2468 /* increase bufsectors from the default 4096 (2M) if opt_transfer
2469 * or discard_alignment of the out_bs is greater. Limit to
2470 * MAX_BUF_SECTORS as maximum which is currently 32768 (16MB). */
2471 s
.buf_sectors
= MIN(MAX_BUF_SECTORS
,
2473 MAX(out_bs
->bl
.opt_transfer
>> BDRV_SECTOR_BITS
,
2474 out_bs
->bl
.pdiscard_alignment
>>
2475 BDRV_SECTOR_BITS
)));
2477 /* try to align the write requests to the destination to avoid unnecessary
2479 s
.alignment
= MAX(pow2floor(s
.min_sparse
),
2480 DIV_ROUND_UP(out_bs
->bl
.request_alignment
,
2482 assert(is_power_of_2(s
.alignment
));
2485 int64_t output_sectors
= blk_nb_sectors(s
.target
);
2486 if (output_sectors
< 0) {
2487 error_report("unable to get output image length: %s",
2488 strerror(-output_sectors
));
2491 } else if (output_sectors
< s
.total_sectors
) {
2492 error_report("output file is smaller than input file");
2498 if (s
.target_has_backing
) {
2499 /* Errors are treated as "backing length unknown" (which means
2500 * s.target_backing_sectors has to be negative, which it will
2501 * be automatically). The backing file length is used only
2502 * for optimizations, so such a case is not fatal. */
2503 s
.target_backing_sectors
= bdrv_nb_sectors(out_bs
->backing
->bs
);
2505 s
.target_backing_sectors
= -1;
2508 ret
= bdrv_get_info(out_bs
, &bdi
);
2511 error_report("could not get block driver info");
2515 s
.compressed
= s
.compressed
|| bdi
.needs_compressed_writes
;
2516 s
.cluster_sectors
= bdi
.cluster_size
/ BDRV_SECTOR_SIZE
;
2517 s
.unallocated_blocks_are_zero
= bdi
.unallocated_blocks_are_zero
;
2520 ret
= convert_do_copy(&s
);
2523 qemu_progress_print(100, 0);
2525 qemu_progress_end();
2526 qemu_opts_del(opts
);
2527 qemu_opts_free(create_opts
);
2528 qemu_opts_del(sn_opts
);
2529 qobject_unref(open_opts
);
2530 blk_unref(s
.target
);
2532 for (bs_i
= 0; bs_i
< s
.src_num
; bs_i
++) {
2533 blk_unref(s
.src
[bs_i
]);
2537 g_free(s
.src_sectors
);
2545 static void dump_snapshots(BlockDriverState
*bs
)
2547 QEMUSnapshotInfo
*sn_tab
, *sn
;
2550 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
2553 printf("Snapshot list:\n");
2554 bdrv_snapshot_dump(NULL
);
2556 for(i
= 0; i
< nb_sns
; i
++) {
2558 bdrv_snapshot_dump(sn
);
2564 static void dump_json_image_info_list(ImageInfoList
*list
)
2568 Visitor
*v
= qobject_output_visitor_new(&obj
);
2570 visit_type_ImageInfoList(v
, NULL
, &list
, &error_abort
);
2571 visit_complete(v
, &obj
);
2572 str
= qobject_to_json_pretty(obj
);
2573 assert(str
!= NULL
);
2574 printf("%s\n", qstring_get_str(str
));
2580 static void dump_json_image_info(ImageInfo
*info
)
2584 Visitor
*v
= qobject_output_visitor_new(&obj
);
2586 visit_type_ImageInfo(v
, NULL
, &info
, &error_abort
);
2587 visit_complete(v
, &obj
);
2588 str
= qobject_to_json_pretty(obj
);
2589 assert(str
!= NULL
);
2590 printf("%s\n", qstring_get_str(str
));
2596 static void dump_human_image_info_list(ImageInfoList
*list
)
2598 ImageInfoList
*elem
;
2601 for (elem
= list
; elem
; elem
= elem
->next
) {
2607 bdrv_image_info_dump(elem
->value
);
2611 static gboolean
str_equal_func(gconstpointer a
, gconstpointer b
)
2613 return strcmp(a
, b
) == 0;
2617 * Open an image file chain and return an ImageInfoList
2619 * @filename: topmost image filename
2620 * @fmt: topmost image format (may be NULL to autodetect)
2621 * @chain: true - enumerate entire backing file chain
2622 * false - only topmost image file
2624 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2625 * image file. If there was an error a message will have been printed to
2628 static ImageInfoList
*collect_image_info_list(bool image_opts
,
2629 const char *filename
,
2631 bool chain
, bool force_share
)
2633 ImageInfoList
*head
= NULL
;
2634 ImageInfoList
**last
= &head
;
2635 GHashTable
*filenames
;
2638 filenames
= g_hash_table_new_full(g_str_hash
, str_equal_func
, NULL
, NULL
);
2642 BlockDriverState
*bs
;
2644 ImageInfoList
*elem
;
2646 if (g_hash_table_lookup_extended(filenames
, filename
, NULL
, NULL
)) {
2647 error_report("Backing file '%s' creates an infinite loop.",
2651 g_hash_table_insert(filenames
, (gpointer
)filename
, NULL
);
2653 blk
= img_open(image_opts
, filename
, fmt
,
2654 BDRV_O_NO_BACKING
| BDRV_O_NO_IO
, false, false,
2661 bdrv_query_image_info(bs
, &info
, &err
);
2663 error_report_err(err
);
2668 elem
= g_new0(ImageInfoList
, 1);
2675 filename
= fmt
= NULL
;
2677 if (info
->has_full_backing_filename
) {
2678 filename
= info
->full_backing_filename
;
2679 } else if (info
->has_backing_filename
) {
2680 error_report("Could not determine absolute backing filename,"
2681 " but backing filename '%s' present",
2682 info
->backing_filename
);
2685 if (info
->has_backing_filename_format
) {
2686 fmt
= info
->backing_filename_format
;
2690 g_hash_table_destroy(filenames
);
2694 qapi_free_ImageInfoList(head
);
2695 g_hash_table_destroy(filenames
);
2699 static int img_info(int argc
, char **argv
)
2702 OutputFormat output_format
= OFORMAT_HUMAN
;
2704 const char *filename
, *fmt
, *output
;
2705 ImageInfoList
*list
;
2706 bool image_opts
= false;
2707 bool force_share
= false;
2712 int option_index
= 0;
2713 static const struct option long_options
[] = {
2714 {"help", no_argument
, 0, 'h'},
2715 {"format", required_argument
, 0, 'f'},
2716 {"output", required_argument
, 0, OPTION_OUTPUT
},
2717 {"backing-chain", no_argument
, 0, OPTION_BACKING_CHAIN
},
2718 {"object", required_argument
, 0, OPTION_OBJECT
},
2719 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
2720 {"force-share", no_argument
, 0, 'U'},
2723 c
= getopt_long(argc
, argv
, ":f:hU",
2724 long_options
, &option_index
);
2730 missing_argument(argv
[optind
- 1]);
2733 unrecognized_option(argv
[optind
- 1]);
2747 case OPTION_BACKING_CHAIN
:
2750 case OPTION_OBJECT
: {
2752 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
2758 case OPTION_IMAGE_OPTS
:
2763 if (optind
!= argc
- 1) {
2764 error_exit("Expecting one image file name");
2766 filename
= argv
[optind
++];
2768 if (output
&& !strcmp(output
, "json")) {
2769 output_format
= OFORMAT_JSON
;
2770 } else if (output
&& !strcmp(output
, "human")) {
2771 output_format
= OFORMAT_HUMAN
;
2772 } else if (output
) {
2773 error_report("--output must be used with human or json as argument.");
2777 if (qemu_opts_foreach(&qemu_object_opts
,
2778 user_creatable_add_opts_foreach
,
2779 NULL
, &error_fatal
)) {
2783 list
= collect_image_info_list(image_opts
, filename
, fmt
, chain
,
2789 switch (output_format
) {
2791 dump_human_image_info_list(list
);
2795 dump_json_image_info_list(list
);
2797 dump_json_image_info(list
->value
);
2802 qapi_free_ImageInfoList(list
);
2806 static int dump_map_entry(OutputFormat output_format
, MapEntry
*e
,
2809 switch (output_format
) {
2811 if (e
->data
&& !e
->has_offset
) {
2812 error_report("File contains external, encrypted or compressed clusters.");
2815 if (e
->data
&& !e
->zero
) {
2816 printf("%#-16"PRIx64
"%#-16"PRIx64
"%#-16"PRIx64
"%s\n",
2817 e
->start
, e
->length
,
2818 e
->has_offset
? e
->offset
: 0,
2819 e
->has_filename
? e
->filename
: "");
2821 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2822 * Modify the flags here to allow more coalescing.
2824 if (next
&& (!next
->data
|| next
->zero
)) {
2830 printf("%s{ \"start\": %"PRId64
", \"length\": %"PRId64
","
2831 " \"depth\": %"PRId64
", \"zero\": %s, \"data\": %s",
2832 (e
->start
== 0 ? "[" : ",\n"),
2833 e
->start
, e
->length
, e
->depth
,
2834 e
->zero
? "true" : "false",
2835 e
->data
? "true" : "false");
2836 if (e
->has_offset
) {
2837 printf(", \"offset\": %"PRId64
"", e
->offset
);
2849 static int get_block_status(BlockDriverState
*bs
, int64_t offset
,
2850 int64_t bytes
, MapEntry
*e
)
2854 BlockDriverState
*file
;
2857 char *filename
= NULL
;
2859 /* As an optimization, we could cache the current range of unallocated
2860 * clusters in each file of the chain, and avoid querying the same
2866 ret
= bdrv_block_status(bs
, offset
, bytes
, &bytes
, &map
, &file
);
2871 if (ret
& (BDRV_BLOCK_ZERO
|BDRV_BLOCK_DATA
)) {
2874 bs
= backing_bs(bs
);
2883 has_offset
= !!(ret
& BDRV_BLOCK_OFFSET_VALID
);
2885 if (file
&& has_offset
) {
2886 bdrv_refresh_filename(file
);
2887 filename
= file
->filename
;
2893 .data
= !!(ret
& BDRV_BLOCK_DATA
),
2894 .zero
= !!(ret
& BDRV_BLOCK_ZERO
),
2896 .has_offset
= has_offset
,
2898 .has_filename
= filename
,
2899 .filename
= filename
,
2905 static inline bool entry_mergeable(const MapEntry
*curr
, const MapEntry
*next
)
2907 if (curr
->length
== 0) {
2910 if (curr
->zero
!= next
->zero
||
2911 curr
->data
!= next
->data
||
2912 curr
->depth
!= next
->depth
||
2913 curr
->has_filename
!= next
->has_filename
||
2914 curr
->has_offset
!= next
->has_offset
) {
2917 if (curr
->has_filename
&& strcmp(curr
->filename
, next
->filename
)) {
2920 if (curr
->has_offset
&& curr
->offset
+ curr
->length
!= next
->offset
) {
2926 static int img_map(int argc
, char **argv
)
2929 OutputFormat output_format
= OFORMAT_HUMAN
;
2931 BlockDriverState
*bs
;
2932 const char *filename
, *fmt
, *output
;
2934 MapEntry curr
= { .length
= 0 }, next
;
2936 bool image_opts
= false;
2937 bool force_share
= false;
2942 int option_index
= 0;
2943 static const struct option long_options
[] = {
2944 {"help", no_argument
, 0, 'h'},
2945 {"format", required_argument
, 0, 'f'},
2946 {"output", required_argument
, 0, OPTION_OUTPUT
},
2947 {"object", required_argument
, 0, OPTION_OBJECT
},
2948 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
2949 {"force-share", no_argument
, 0, 'U'},
2952 c
= getopt_long(argc
, argv
, ":f:hU",
2953 long_options
, &option_index
);
2959 missing_argument(argv
[optind
- 1]);
2962 unrecognized_option(argv
[optind
- 1]);
2976 case OPTION_OBJECT
: {
2978 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
2984 case OPTION_IMAGE_OPTS
:
2989 if (optind
!= argc
- 1) {
2990 error_exit("Expecting one image file name");
2992 filename
= argv
[optind
];
2994 if (output
&& !strcmp(output
, "json")) {
2995 output_format
= OFORMAT_JSON
;
2996 } else if (output
&& !strcmp(output
, "human")) {
2997 output_format
= OFORMAT_HUMAN
;
2998 } else if (output
) {
2999 error_report("--output must be used with human or json as argument.");
3003 if (qemu_opts_foreach(&qemu_object_opts
,
3004 user_creatable_add_opts_foreach
,
3005 NULL
, &error_fatal
)) {
3009 blk
= img_open(image_opts
, filename
, fmt
, 0, false, false, force_share
);
3015 if (output_format
== OFORMAT_HUMAN
) {
3016 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
3019 length
= blk_getlength(blk
);
3020 while (curr
.start
+ curr
.length
< length
) {
3021 int64_t offset
= curr
.start
+ curr
.length
;
3024 /* Probe up to 1 GiB at a time. */
3025 n
= MIN(1 * GiB
, length
- offset
);
3026 ret
= get_block_status(bs
, offset
, n
, &next
);
3029 error_report("Could not read file metadata: %s", strerror(-ret
));
3033 if (entry_mergeable(&curr
, &next
)) {
3034 curr
.length
+= next
.length
;
3038 if (curr
.length
> 0) {
3039 ret
= dump_map_entry(output_format
, &curr
, &next
);
3047 ret
= dump_map_entry(output_format
, &curr
, NULL
);
3054 #define SNAPSHOT_LIST 1
3055 #define SNAPSHOT_CREATE 2
3056 #define SNAPSHOT_APPLY 3
3057 #define SNAPSHOT_DELETE 4
3059 static int img_snapshot(int argc
, char **argv
)
3062 BlockDriverState
*bs
;
3063 QEMUSnapshotInfo sn
;
3064 char *filename
, *snapshot_name
= NULL
;
3065 int c
, ret
= 0, bdrv_oflags
;
3070 bool image_opts
= false;
3071 bool force_share
= false;
3073 bdrv_oflags
= BDRV_O_RDWR
;
3074 /* Parse commandline parameters */
3076 static const struct option long_options
[] = {
3077 {"help", no_argument
, 0, 'h'},
3078 {"object", required_argument
, 0, OPTION_OBJECT
},
3079 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
3080 {"force-share", no_argument
, 0, 'U'},
3083 c
= getopt_long(argc
, argv
, ":la:c:d:hqU",
3084 long_options
, NULL
);
3090 missing_argument(argv
[optind
- 1]);
3093 unrecognized_option(argv
[optind
- 1]);
3100 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
3103 action
= SNAPSHOT_LIST
;
3104 bdrv_oflags
&= ~BDRV_O_RDWR
; /* no need for RW */
3108 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
3111 action
= SNAPSHOT_APPLY
;
3112 snapshot_name
= optarg
;
3116 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
3119 action
= SNAPSHOT_CREATE
;
3120 snapshot_name
= optarg
;
3124 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
3127 action
= SNAPSHOT_DELETE
;
3128 snapshot_name
= optarg
;
3136 case OPTION_OBJECT
: {
3138 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
3144 case OPTION_IMAGE_OPTS
:
3150 if (optind
!= argc
- 1) {
3151 error_exit("Expecting one image file name");
3153 filename
= argv
[optind
++];
3155 if (qemu_opts_foreach(&qemu_object_opts
,
3156 user_creatable_add_opts_foreach
,
3157 NULL
, &error_fatal
)) {
3161 /* Open the image */
3162 blk
= img_open(image_opts
, filename
, NULL
, bdrv_oflags
, false, quiet
,
3169 /* Perform the requested action */
3175 case SNAPSHOT_CREATE
:
3176 memset(&sn
, 0, sizeof(sn
));
3177 pstrcpy(sn
.name
, sizeof(sn
.name
), snapshot_name
);
3179 qemu_gettimeofday(&tv
);
3180 sn
.date_sec
= tv
.tv_sec
;
3181 sn
.date_nsec
= tv
.tv_usec
* 1000;
3183 ret
= bdrv_snapshot_create(bs
, &sn
);
3185 error_report("Could not create snapshot '%s': %d (%s)",
3186 snapshot_name
, ret
, strerror(-ret
));
3190 case SNAPSHOT_APPLY
:
3191 ret
= bdrv_snapshot_goto(bs
, snapshot_name
, &err
);
3193 error_reportf_err(err
, "Could not apply snapshot '%s': ",
3198 case SNAPSHOT_DELETE
:
3199 ret
= bdrv_snapshot_find(bs
, &sn
, snapshot_name
);
3201 error_report("Could not delete snapshot '%s': snapshot not "
3202 "found", snapshot_name
);
3205 ret
= bdrv_snapshot_delete(bs
, sn
.id_str
, sn
.name
, &err
);
3207 error_reportf_err(err
, "Could not delete snapshot '%s': ",
3223 static int img_rebase(int argc
, char **argv
)
3225 BlockBackend
*blk
= NULL
, *blk_old_backing
= NULL
, *blk_new_backing
= NULL
;
3226 uint8_t *buf_old
= NULL
;
3227 uint8_t *buf_new
= NULL
;
3228 BlockDriverState
*bs
= NULL
, *prefix_chain_bs
= NULL
;
3230 const char *fmt
, *cache
, *src_cache
, *out_basefmt
, *out_baseimg
;
3231 int c
, flags
, src_flags
, ret
;
3232 bool writethrough
, src_writethrough
;
3234 bool force_share
= false;
3237 Error
*local_err
= NULL
;
3238 bool image_opts
= false;
3240 /* Parse commandline parameters */
3242 cache
= BDRV_DEFAULT_CACHE
;
3243 src_cache
= BDRV_DEFAULT_CACHE
;
3247 static const struct option long_options
[] = {
3248 {"help", no_argument
, 0, 'h'},
3249 {"object", required_argument
, 0, OPTION_OBJECT
},
3250 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
3251 {"force-share", no_argument
, 0, 'U'},
3254 c
= getopt_long(argc
, argv
, ":hf:F:b:upt:T:qU",
3255 long_options
, NULL
);
3261 missing_argument(argv
[optind
- 1]);
3264 unrecognized_option(argv
[optind
- 1]);
3273 out_basefmt
= optarg
;
3276 out_baseimg
= optarg
;
3293 case OPTION_OBJECT
: {
3295 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
3301 case OPTION_IMAGE_OPTS
:
3314 if (optind
!= argc
- 1) {
3315 error_exit("Expecting one image file name");
3317 if (!unsafe
&& !out_baseimg
) {
3318 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
3320 filename
= argv
[optind
++];
3322 if (qemu_opts_foreach(&qemu_object_opts
,
3323 user_creatable_add_opts_foreach
,
3324 NULL
, &error_fatal
)) {
3328 qemu_progress_init(progress
, 2.0);
3329 qemu_progress_print(0, 100);
3331 flags
= BDRV_O_RDWR
| (unsafe
? BDRV_O_NO_BACKING
: 0);
3332 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
3334 error_report("Invalid cache option: %s", cache
);
3339 ret
= bdrv_parse_cache_mode(src_cache
, &src_flags
, &src_writethrough
);
3341 error_report("Invalid source cache option: %s", src_cache
);
3345 /* The source files are opened read-only, don't care about WCE */
3346 assert((src_flags
& BDRV_O_RDWR
) == 0);
3347 (void) src_writethrough
;
3352 * Ignore the old backing file for unsafe rebase in case we want to correct
3353 * the reference to a renamed or moved backing file.
3355 blk
= img_open(image_opts
, filename
, fmt
, flags
, writethrough
, quiet
,
3363 if (out_basefmt
!= NULL
) {
3364 if (bdrv_find_format(out_basefmt
) == NULL
) {
3365 error_report("Invalid format name: '%s'", out_basefmt
);
3371 /* For safe rebasing we need to compare old and new backing file */
3373 QDict
*options
= NULL
;
3374 BlockDriverState
*base_bs
= backing_bs(bs
);
3377 blk_old_backing
= blk_new(qemu_get_aio_context(),
3378 BLK_PERM_CONSISTENT_READ
,
3380 ret
= blk_insert_bs(blk_old_backing
, base_bs
,
3383 error_reportf_err(local_err
,
3384 "Could not reuse old backing file '%s': ",
3389 blk_old_backing
= NULL
;
3392 if (out_baseimg
[0]) {
3393 const char *overlay_filename
;
3394 char *out_real_path
;
3396 options
= qdict_new();
3398 qdict_put_str(options
, "driver", out_basefmt
);
3401 qdict_put_bool(options
, BDRV_OPT_FORCE_SHARE
, true);
3404 bdrv_refresh_filename(bs
);
3405 overlay_filename
= bs
->exact_filename
[0] ? bs
->exact_filename
3408 bdrv_get_full_backing_filename_from_filename(overlay_filename
,
3412 qobject_unref(options
);
3413 error_reportf_err(local_err
,
3414 "Could not resolve backing filename: ");
3420 * Find out whether we rebase an image on top of a previous image
3423 prefix_chain_bs
= bdrv_find_backing_image(bs
, out_real_path
);
3424 if (prefix_chain_bs
) {
3425 qobject_unref(options
);
3426 g_free(out_real_path
);
3428 blk_new_backing
= blk_new(qemu_get_aio_context(),
3429 BLK_PERM_CONSISTENT_READ
,
3431 ret
= blk_insert_bs(blk_new_backing
, prefix_chain_bs
,
3434 error_reportf_err(local_err
,
3435 "Could not reuse backing file '%s': ",
3440 blk_new_backing
= blk_new_open(out_real_path
, NULL
,
3441 options
, src_flags
, &local_err
);
3442 g_free(out_real_path
);
3443 if (!blk_new_backing
) {
3444 error_reportf_err(local_err
,
3445 "Could not open new backing file '%s': ",
3455 * Check each unallocated cluster in the COW file. If it is unallocated,
3456 * accesses go to the backing file. We must therefore compare this cluster
3457 * in the old and new backing file, and if they differ we need to copy it
3458 * from the old backing file into the COW file.
3460 * If qemu-img crashes during this step, no harm is done. The content of
3461 * the image is the same as the original one at any time.
3465 int64_t old_backing_size
= 0;
3466 int64_t new_backing_size
= 0;
3469 float local_progress
= 0;
3471 buf_old
= blk_blockalign(blk
, IO_BUF_SIZE
);
3472 buf_new
= blk_blockalign(blk
, IO_BUF_SIZE
);
3474 size
= blk_getlength(blk
);
3476 error_report("Could not get size of '%s': %s",
3477 filename
, strerror(-size
));
3481 if (blk_old_backing
) {
3482 old_backing_size
= blk_getlength(blk_old_backing
);
3483 if (old_backing_size
< 0) {
3484 char backing_name
[PATH_MAX
];
3486 bdrv_get_backing_filename(bs
, backing_name
,
3487 sizeof(backing_name
));
3488 error_report("Could not get size of '%s': %s",
3489 backing_name
, strerror(-old_backing_size
));
3494 if (blk_new_backing
) {
3495 new_backing_size
= blk_getlength(blk_new_backing
);
3496 if (new_backing_size
< 0) {
3497 error_report("Could not get size of '%s': %s",
3498 out_baseimg
, strerror(-new_backing_size
));
3505 local_progress
= (float)100 / (size
/ MIN(size
, IO_BUF_SIZE
));
3508 for (offset
= 0; offset
< size
; offset
+= n
) {
3509 bool buf_old_is_zero
= false;
3511 /* How many bytes can we handle with the next read? */
3512 n
= MIN(IO_BUF_SIZE
, size
- offset
);
3514 /* If the cluster is allocated, we don't need to take action */
3515 ret
= bdrv_is_allocated(bs
, offset
, n
, &n
);
3517 error_report("error while reading image metadata: %s",
3525 if (prefix_chain_bs
) {
3527 * If cluster wasn't changed since prefix_chain, we don't need
3530 ret
= bdrv_is_allocated_above(backing_bs(bs
), prefix_chain_bs
,
3531 false, offset
, n
, &n
);
3533 error_report("error while reading image metadata: %s",
3543 * Read old and new backing file and take into consideration that
3544 * backing files may be smaller than the COW image.
3546 if (offset
>= old_backing_size
) {
3547 memset(buf_old
, 0, n
);
3548 buf_old_is_zero
= true;
3550 if (offset
+ n
> old_backing_size
) {
3551 n
= old_backing_size
- offset
;
3554 ret
= blk_pread(blk_old_backing
, offset
, buf_old
, n
);
3556 error_report("error while reading from old backing file");
3561 if (offset
>= new_backing_size
|| !blk_new_backing
) {
3562 memset(buf_new
, 0, n
);
3564 if (offset
+ n
> new_backing_size
) {
3565 n
= new_backing_size
- offset
;
3568 ret
= blk_pread(blk_new_backing
, offset
, buf_new
, n
);
3570 error_report("error while reading from new backing file");
3575 /* If they differ, we need to write to the COW file */
3576 uint64_t written
= 0;
3578 while (written
< n
) {
3581 if (compare_buffers(buf_old
+ written
, buf_new
+ written
,
3582 n
- written
, &pnum
))
3584 if (buf_old_is_zero
) {
3585 ret
= blk_pwrite_zeroes(blk
, offset
+ written
, pnum
, 0);
3587 ret
= blk_pwrite(blk
, offset
+ written
,
3588 buf_old
+ written
, pnum
, 0);
3591 error_report("Error while writing to COW image: %s",
3599 qemu_progress_print(local_progress
, 100);
3604 * Change the backing file. All clusters that are different from the old
3605 * backing file are overwritten in the COW file now, so the visible content
3606 * doesn't change when we switch the backing file.
3608 if (out_baseimg
&& *out_baseimg
) {
3609 ret
= bdrv_change_backing_file(bs
, out_baseimg
, out_basefmt
);
3611 ret
= bdrv_change_backing_file(bs
, NULL
, NULL
);
3614 if (ret
== -ENOSPC
) {
3615 error_report("Could not change the backing file to '%s': No "
3616 "space left in the file header", out_baseimg
);
3617 } else if (ret
< 0) {
3618 error_report("Could not change the backing file to '%s': %s",
3619 out_baseimg
, strerror(-ret
));
3622 qemu_progress_print(100, 0);
3624 * TODO At this point it is possible to check if any clusters that are
3625 * allocated in the COW file are the same in the backing file. If so, they
3626 * could be dropped from the COW file. Don't do this before switching the
3627 * backing file, in case of a crash this would lead to corruption.
3630 qemu_progress_end();
3633 blk_unref(blk_old_backing
);
3634 blk_unref(blk_new_backing
);
3636 qemu_vfree(buf_old
);
3637 qemu_vfree(buf_new
);
3646 static int img_resize(int argc
, char **argv
)
3649 int c
, ret
, relative
;
3650 const char *filename
, *fmt
, *size
;
3651 int64_t n
, total_size
, current_size
, new_size
;
3653 BlockBackend
*blk
= NULL
;
3654 PreallocMode prealloc
= PREALLOC_MODE_OFF
;
3657 static QemuOptsList resize_options
= {
3658 .name
= "resize_options",
3659 .head
= QTAILQ_HEAD_INITIALIZER(resize_options
.head
),
3662 .name
= BLOCK_OPT_SIZE
,
3663 .type
= QEMU_OPT_SIZE
,
3664 .help
= "Virtual disk size"
3670 bool image_opts
= false;
3671 bool shrink
= false;
3673 /* Remove size from argv manually so that negative numbers are not treated
3674 * as options by getopt. */
3676 error_exit("Not enough arguments");
3680 size
= argv
[--argc
];
3682 /* Parse getopt arguments */
3685 static const struct option long_options
[] = {
3686 {"help", no_argument
, 0, 'h'},
3687 {"object", required_argument
, 0, OPTION_OBJECT
},
3688 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
3689 {"preallocation", required_argument
, 0, OPTION_PREALLOCATION
},
3690 {"shrink", no_argument
, 0, OPTION_SHRINK
},
3693 c
= getopt_long(argc
, argv
, ":f:hq",
3694 long_options
, NULL
);
3700 missing_argument(argv
[optind
- 1]);
3703 unrecognized_option(argv
[optind
- 1]);
3714 case OPTION_OBJECT
: {
3716 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
3722 case OPTION_IMAGE_OPTS
:
3725 case OPTION_PREALLOCATION
:
3726 prealloc
= qapi_enum_parse(&PreallocMode_lookup
, optarg
,
3727 PREALLOC_MODE__MAX
, NULL
);
3728 if (prealloc
== PREALLOC_MODE__MAX
) {
3729 error_report("Invalid preallocation mode '%s'", optarg
);
3738 if (optind
!= argc
- 1) {
3739 error_exit("Expecting image file name and size");
3741 filename
= argv
[optind
++];
3743 if (qemu_opts_foreach(&qemu_object_opts
,
3744 user_creatable_add_opts_foreach
,
3745 NULL
, &error_fatal
)) {
3749 /* Choose grow, shrink, or absolute resize mode */
3765 param
= qemu_opts_create(&resize_options
, NULL
, 0, &error_abort
);
3766 qemu_opt_set(param
, BLOCK_OPT_SIZE
, size
, &err
);
3768 error_report_err(err
);
3770 qemu_opts_del(param
);
3773 n
= qemu_opt_get_size(param
, BLOCK_OPT_SIZE
, 0);
3774 qemu_opts_del(param
);
3776 blk
= img_open(image_opts
, filename
, fmt
,
3777 BDRV_O_RDWR
| BDRV_O_RESIZE
, false, quiet
,
3784 current_size
= blk_getlength(blk
);
3785 if (current_size
< 0) {
3786 error_report("Failed to inquire current image length: %s",
3787 strerror(-current_size
));
3793 total_size
= current_size
+ n
* relative
;
3797 if (total_size
<= 0) {
3798 error_report("New image size must be positive");
3803 if (total_size
<= current_size
&& prealloc
!= PREALLOC_MODE_OFF
) {
3804 error_report("Preallocation can only be used for growing images");
3809 if (total_size
< current_size
&& !shrink
) {
3810 warn_report("Shrinking an image will delete all data beyond the "
3811 "shrunken image's end. Before performing such an "
3812 "operation, make sure there is no important data there.");
3814 if (g_strcmp0(bdrv_get_format_name(blk_bs(blk
)), "raw") != 0) {
3816 "Use the --shrink option to perform a shrink operation.");
3820 warn_report("Using the --shrink option will suppress this message. "
3821 "Note that future versions of qemu-img may refuse to "
3822 "shrink images without this option.");
3826 ret
= blk_truncate(blk
, total_size
, prealloc
, &err
);
3828 error_report_err(err
);
3832 new_size
= blk_getlength(blk
);
3834 error_report("Failed to verify truncated image length: %s",
3835 strerror(-new_size
));
3840 /* Some block drivers implement a truncation method, but only so
3841 * the user can cause qemu to refresh the image's size from disk.
3842 * The idea is that the user resizes the image outside of qemu and
3843 * then invokes block_resize to inform qemu about it.
3844 * (This includes iscsi and file-posix for device files.)
3845 * Of course, that is not the behavior someone invoking
3846 * qemu-img resize would find useful, so we catch that behavior
3847 * here and tell the user. */
3848 if (new_size
!= total_size
&& new_size
== current_size
) {
3849 error_report("Image was not resized; resizing may not be supported "
3855 if (new_size
!= total_size
) {
3856 warn_report("Image should have been resized to %" PRIi64
3857 " bytes, but was resized to %" PRIi64
" bytes",
3858 total_size
, new_size
);
3861 qprintf(quiet
, "Image resized.\n");
3871 static void amend_status_cb(BlockDriverState
*bs
,
3872 int64_t offset
, int64_t total_work_size
,
3875 qemu_progress_print(100.f
* offset
/ total_work_size
, 0);
3878 static int print_amend_option_help(const char *format
)
3882 /* Find driver and parse its options */
3883 drv
= bdrv_find_format(format
);
3885 error_report("Unknown file format '%s'", format
);
3889 if (!drv
->bdrv_amend_options
) {
3890 error_report("Format driver '%s' does not support option amendment",
3895 /* Every driver supporting amendment must have create_opts */
3896 assert(drv
->create_opts
);
3898 printf("Creation options for '%s':\n", format
);
3899 qemu_opts_print_help(drv
->create_opts
, false);
3900 printf("\nNote that not all of these options may be amendable.\n");
3904 static int img_amend(int argc
, char **argv
)
3908 char *options
= NULL
;
3909 QemuOptsList
*create_opts
= NULL
;
3910 QemuOpts
*opts
= NULL
;
3911 const char *fmt
= NULL
, *filename
, *cache
;
3914 bool quiet
= false, progress
= false;
3915 BlockBackend
*blk
= NULL
;
3916 BlockDriverState
*bs
= NULL
;
3917 bool image_opts
= false;
3919 cache
= BDRV_DEFAULT_CACHE
;
3921 static const struct option long_options
[] = {
3922 {"help", no_argument
, 0, 'h'},
3923 {"object", required_argument
, 0, OPTION_OBJECT
},
3924 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
3927 c
= getopt_long(argc
, argv
, ":ho:f:t:pq",
3928 long_options
, NULL
);
3935 missing_argument(argv
[optind
- 1]);
3938 unrecognized_option(argv
[optind
- 1]);
3944 if (!is_valid_option_list(optarg
)) {
3945 error_report("Invalid option list: %s", optarg
);
3947 goto out_no_progress
;
3950 options
= g_strdup(optarg
);
3952 char *old_options
= options
;
3953 options
= g_strdup_printf("%s,%s", options
, optarg
);
3954 g_free(old_options
);
3970 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
3974 goto out_no_progress
;
3977 case OPTION_IMAGE_OPTS
:
3984 error_exit("Must specify options (-o)");
3987 if (qemu_opts_foreach(&qemu_object_opts
,
3988 user_creatable_add_opts_foreach
,
3989 NULL
, &error_fatal
)) {
3991 goto out_no_progress
;
3997 qemu_progress_init(progress
, 1.0);
3999 filename
= (optind
== argc
- 1) ? argv
[argc
- 1] : NULL
;
4000 if (fmt
&& has_help_option(options
)) {
4001 /* If a format is explicitly specified (and possibly no filename is
4002 * given), print option help here */
4003 ret
= print_amend_option_help(fmt
);
4007 if (optind
!= argc
- 1) {
4008 error_report("Expecting one image file name");
4013 flags
= BDRV_O_RDWR
;
4014 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
4016 error_report("Invalid cache option: %s", cache
);
4020 blk
= img_open(image_opts
, filename
, fmt
, flags
, writethrough
, quiet
,
4028 fmt
= bs
->drv
->format_name
;
4030 if (has_help_option(options
)) {
4031 /* If the format was auto-detected, print option help here */
4032 ret
= print_amend_option_help(fmt
);
4036 if (!bs
->drv
->bdrv_amend_options
) {
4037 error_report("Format driver '%s' does not support option amendment",
4043 /* Every driver supporting amendment must have create_opts */
4044 assert(bs
->drv
->create_opts
);
4046 create_opts
= qemu_opts_append(create_opts
, bs
->drv
->create_opts
);
4047 opts
= qemu_opts_create(create_opts
, NULL
, 0, &error_abort
);
4048 qemu_opts_do_parse(opts
, options
, NULL
, &err
);
4050 error_report_err(err
);
4055 /* In case the driver does not call amend_status_cb() */
4056 qemu_progress_print(0.f
, 0);
4057 ret
= bdrv_amend_options(bs
, opts
, &amend_status_cb
, NULL
, &err
);
4058 qemu_progress_print(100.f
, 0);
4060 error_report_err(err
);
4065 qemu_progress_end();
4069 qemu_opts_del(opts
);
4070 qemu_opts_free(create_opts
);
4079 typedef struct BenchData
{
4081 uint64_t image_size
;
4088 bool drain_on_flush
;
4097 static void bench_undrained_flush_cb(void *opaque
, int ret
)
4100 error_report("Failed flush request: %s", strerror(-ret
));
4105 static void bench_cb(void *opaque
, int ret
)
4107 BenchData
*b
= opaque
;
4111 error_report("Failed request: %s", strerror(-ret
));
4116 /* Just finished a flush with drained queue: Start next requests */
4117 assert(b
->in_flight
== 0);
4118 b
->in_flush
= false;
4119 } else if (b
->in_flight
> 0) {
4120 int remaining
= b
->n
- b
->in_flight
;
4125 /* Time for flush? Drain queue if requested, then flush */
4126 if (b
->flush_interval
&& remaining
% b
->flush_interval
== 0) {
4127 if (!b
->in_flight
|| !b
->drain_on_flush
) {
4128 BlockCompletionFunc
*cb
;
4130 if (b
->drain_on_flush
) {
4134 cb
= bench_undrained_flush_cb
;
4137 acb
= blk_aio_flush(b
->blk
, cb
, b
);
4139 error_report("Failed to issue flush request");
4143 if (b
->drain_on_flush
) {
4149 while (b
->n
> b
->in_flight
&& b
->in_flight
< b
->nrreq
) {
4150 int64_t offset
= b
->offset
;
4151 /* blk_aio_* might look for completed I/Os and kick bench_cb
4152 * again, so make sure this operation is counted by in_flight
4153 * and b->offset is ready for the next submission.
4156 b
->offset
+= b
->step
;
4157 b
->offset
%= b
->image_size
;
4159 acb
= blk_aio_pwritev(b
->blk
, offset
, b
->qiov
, 0, bench_cb
, b
);
4161 acb
= blk_aio_preadv(b
->blk
, offset
, b
->qiov
, 0, bench_cb
, b
);
4164 error_report("Failed to issue request");
4170 static int img_bench(int argc
, char **argv
)
4173 const char *fmt
= NULL
, *filename
;
4175 bool image_opts
= false;
4176 bool is_write
= false;
4180 size_t bufsize
= 4096;
4183 int flush_interval
= 0;
4184 bool drain_on_flush
= true;
4186 BlockBackend
*blk
= NULL
;
4187 BenchData data
= {};
4189 bool writethrough
= false;
4190 struct timeval t1
, t2
;
4192 bool force_share
= false;
4196 static const struct option long_options
[] = {
4197 {"help", no_argument
, 0, 'h'},
4198 {"flush-interval", required_argument
, 0, OPTION_FLUSH_INTERVAL
},
4199 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
4200 {"pattern", required_argument
, 0, OPTION_PATTERN
},
4201 {"no-drain", no_argument
, 0, OPTION_NO_DRAIN
},
4202 {"force-share", no_argument
, 0, 'U'},
4205 c
= getopt_long(argc
, argv
, ":hc:d:f:no:qs:S:t:wU", long_options
, NULL
);
4212 missing_argument(argv
[optind
- 1]);
4215 unrecognized_option(argv
[optind
- 1]);
4224 if (qemu_strtoul(optarg
, NULL
, 0, &res
) < 0 || res
> INT_MAX
) {
4225 error_report("Invalid request count specified");
4235 if (qemu_strtoul(optarg
, NULL
, 0, &res
) < 0 || res
> INT_MAX
) {
4236 error_report("Invalid queue depth specified");
4246 flags
|= BDRV_O_NATIVE_AIO
;
4250 offset
= cvtnum(optarg
);
4252 error_report("Invalid offset specified");
4265 sval
= cvtnum(optarg
);
4266 if (sval
< 0 || sval
> INT_MAX
) {
4267 error_report("Invalid buffer size specified");
4278 sval
= cvtnum(optarg
);
4279 if (sval
< 0 || sval
> INT_MAX
) {
4280 error_report("Invalid step size specified");
4288 ret
= bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
);
4290 error_report("Invalid cache mode");
4296 flags
|= BDRV_O_RDWR
;
4302 case OPTION_PATTERN
:
4306 if (qemu_strtoul(optarg
, NULL
, 0, &res
) < 0 || res
> 0xff) {
4307 error_report("Invalid pattern byte specified");
4313 case OPTION_FLUSH_INTERVAL
:
4317 if (qemu_strtoul(optarg
, NULL
, 0, &res
) < 0 || res
> INT_MAX
) {
4318 error_report("Invalid flush interval specified");
4321 flush_interval
= res
;
4324 case OPTION_NO_DRAIN
:
4325 drain_on_flush
= false;
4327 case OPTION_IMAGE_OPTS
:
4333 if (optind
!= argc
- 1) {
4334 error_exit("Expecting one image file name");
4336 filename
= argv
[argc
- 1];
4338 if (!is_write
&& flush_interval
) {
4339 error_report("--flush-interval is only available in write tests");
4343 if (flush_interval
&& flush_interval
< depth
) {
4344 error_report("Flush interval can't be smaller than depth");
4349 blk
= img_open(image_opts
, filename
, fmt
, flags
, writethrough
, quiet
,
4356 image_size
= blk_getlength(blk
);
4357 if (image_size
< 0) {
4362 data
= (BenchData
) {
4364 .image_size
= image_size
,
4366 .step
= step
?: bufsize
,
4371 .flush_interval
= flush_interval
,
4372 .drain_on_flush
= drain_on_flush
,
4374 printf("Sending %d %s requests, %d bytes each, %d in parallel "
4375 "(starting at offset %" PRId64
", step size %d)\n",
4376 data
.n
, data
.write
? "write" : "read", data
.bufsize
, data
.nrreq
,
4377 data
.offset
, data
.step
);
4378 if (flush_interval
) {
4379 printf("Sending flush every %d requests\n", flush_interval
);
4382 buf_size
= data
.nrreq
* data
.bufsize
;
4383 data
.buf
= blk_blockalign(blk
, buf_size
);
4384 memset(data
.buf
, pattern
, data
.nrreq
* data
.bufsize
);
4386 blk_register_buf(blk
, data
.buf
, buf_size
);
4388 data
.qiov
= g_new(QEMUIOVector
, data
.nrreq
);
4389 for (i
= 0; i
< data
.nrreq
; i
++) {
4390 qemu_iovec_init(&data
.qiov
[i
], 1);
4391 qemu_iovec_add(&data
.qiov
[i
],
4392 data
.buf
+ i
* data
.bufsize
, data
.bufsize
);
4395 gettimeofday(&t1
, NULL
);
4398 while (data
.n
> 0) {
4399 main_loop_wait(false);
4401 gettimeofday(&t2
, NULL
);
4403 printf("Run completed in %3.3f seconds.\n",
4404 (t2
.tv_sec
- t1
.tv_sec
)
4405 + ((double)(t2
.tv_usec
- t1
.tv_usec
) / 1000000));
4409 blk_unregister_buf(blk
, data
.buf
);
4411 qemu_vfree(data
.buf
);
4432 int bsz
; /* Block size */
4440 int (*f
)(const char *, struct DdIo
*, struct DdIo
*, struct DdInfo
*);
4444 static int img_dd_bs(const char *arg
,
4445 struct DdIo
*in
, struct DdIo
*out
,
4452 if (res
<= 0 || res
> INT_MAX
) {
4453 error_report("invalid number: '%s'", arg
);
4456 in
->bsz
= out
->bsz
= res
;
4461 static int img_dd_count(const char *arg
,
4462 struct DdIo
*in
, struct DdIo
*out
,
4465 dd
->count
= cvtnum(arg
);
4467 if (dd
->count
< 0) {
4468 error_report("invalid number: '%s'", arg
);
4475 static int img_dd_if(const char *arg
,
4476 struct DdIo
*in
, struct DdIo
*out
,
4479 in
->filename
= g_strdup(arg
);
4484 static int img_dd_of(const char *arg
,
4485 struct DdIo
*in
, struct DdIo
*out
,
4488 out
->filename
= g_strdup(arg
);
4493 static int img_dd_skip(const char *arg
,
4494 struct DdIo
*in
, struct DdIo
*out
,
4497 in
->offset
= cvtnum(arg
);
4499 if (in
->offset
< 0) {
4500 error_report("invalid number: '%s'", arg
);
4507 static int img_dd(int argc
, char **argv
)
4512 BlockDriver
*drv
= NULL
, *proto_drv
= NULL
;
4513 BlockBackend
*blk1
= NULL
, *blk2
= NULL
;
4514 QemuOpts
*opts
= NULL
;
4515 QemuOptsList
*create_opts
= NULL
;
4516 Error
*local_err
= NULL
;
4517 bool image_opts
= false;
4519 const char *out_fmt
= "raw";
4520 const char *fmt
= NULL
;
4522 int64_t block_count
= 0, out_pos
, in_pos
;
4523 bool force_share
= false;
4524 struct DdInfo dd
= {
4529 .bsz
= 512, /* Block size is by default 512 bytes */
4541 const struct DdOpts options
[] = {
4542 { "bs", img_dd_bs
, C_BS
},
4543 { "count", img_dd_count
, C_COUNT
},
4544 { "if", img_dd_if
, C_IF
},
4545 { "of", img_dd_of
, C_OF
},
4546 { "skip", img_dd_skip
, C_SKIP
},
4549 const struct option long_options
[] = {
4550 { "help", no_argument
, 0, 'h'},
4551 { "object", required_argument
, 0, OPTION_OBJECT
},
4552 { "image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
4553 { "force-share", no_argument
, 0, 'U'},
4557 while ((c
= getopt_long(argc
, argv
, ":hf:O:U", long_options
, NULL
))) {
4569 missing_argument(argv
[optind
- 1]);
4572 unrecognized_option(argv
[optind
- 1]);
4581 if (!qemu_opts_parse_noisily(&qemu_object_opts
, optarg
, true)) {
4586 case OPTION_IMAGE_OPTS
:
4592 for (i
= optind
; i
< argc
; i
++) {
4594 arg
= g_strdup(argv
[i
]);
4596 tmp
= strchr(arg
, '=');
4598 error_report("unrecognized operand %s", arg
);
4605 for (j
= 0; options
[j
].name
!= NULL
; j
++) {
4606 if (!strcmp(arg
, options
[j
].name
)) {
4610 if (options
[j
].name
== NULL
) {
4611 error_report("unrecognized operand %s", arg
);
4616 if (options
[j
].f(tmp
, &in
, &out
, &dd
) != 0) {
4620 dd
.flags
|= options
[j
].flag
;
4625 if (!(dd
.flags
& C_IF
&& dd
.flags
& C_OF
)) {
4626 error_report("Must specify both input and output files");
4631 if (qemu_opts_foreach(&qemu_object_opts
,
4632 user_creatable_add_opts_foreach
,
4633 NULL
, &error_fatal
)) {
4638 blk1
= img_open(image_opts
, in
.filename
, fmt
, 0, false, false,
4646 drv
= bdrv_find_format(out_fmt
);
4648 error_report("Unknown file format");
4652 proto_drv
= bdrv_find_protocol(out
.filename
, true, &local_err
);
4655 error_report_err(local_err
);
4659 if (!drv
->create_opts
) {
4660 error_report("Format driver '%s' does not support image creation",
4665 if (!proto_drv
->create_opts
) {
4666 error_report("Protocol driver '%s' does not support image creation",
4667 proto_drv
->format_name
);
4671 create_opts
= qemu_opts_append(create_opts
, drv
->create_opts
);
4672 create_opts
= qemu_opts_append(create_opts
, proto_drv
->create_opts
);
4674 opts
= qemu_opts_create(create_opts
, NULL
, 0, &error_abort
);
4676 size
= blk_getlength(blk1
);
4678 error_report("Failed to get size for '%s'", in
.filename
);
4683 if (dd
.flags
& C_COUNT
&& dd
.count
<= INT64_MAX
/ in
.bsz
&&
4684 dd
.count
* in
.bsz
< size
) {
4685 size
= dd
.count
* in
.bsz
;
4688 /* Overflow means the specified offset is beyond input image's size */
4689 if (dd
.flags
& C_SKIP
&& (in
.offset
> INT64_MAX
/ in
.bsz
||
4690 size
< in
.bsz
* in
.offset
)) {
4691 qemu_opt_set_number(opts
, BLOCK_OPT_SIZE
, 0, &error_abort
);
4693 qemu_opt_set_number(opts
, BLOCK_OPT_SIZE
,
4694 size
- in
.bsz
* in
.offset
, &error_abort
);
4697 ret
= bdrv_create(drv
, out
.filename
, opts
, &local_err
);
4699 error_reportf_err(local_err
,
4700 "%s: error while creating output image: ",
4706 /* TODO, we can't honour --image-opts for the target,
4707 * since it needs to be given in a format compatible
4708 * with the bdrv_create() call above which does not
4709 * support image-opts style.
4711 blk2
= img_open_file(out
.filename
, NULL
, out_fmt
, BDRV_O_RDWR
,
4712 false, false, false);
4719 if (dd
.flags
& C_SKIP
&& (in
.offset
> INT64_MAX
/ in
.bsz
||
4720 size
< in
.offset
* in
.bsz
)) {
4721 /* We give a warning if the skip option is bigger than the input
4722 * size and create an empty output disk image (i.e. like dd(1)).
4724 error_report("%s: cannot skip to specified offset", in
.filename
);
4727 in_pos
= in
.offset
* in
.bsz
;
4730 in
.buf
= g_new(uint8_t, in
.bsz
);
4732 for (out_pos
= 0; in_pos
< size
; block_count
++) {
4733 int in_ret
, out_ret
;
4735 if (in_pos
+ in
.bsz
> size
) {
4736 in_ret
= blk_pread(blk1
, in_pos
, in
.buf
, size
- in_pos
);
4738 in_ret
= blk_pread(blk1
, in_pos
, in
.buf
, in
.bsz
);
4741 error_report("error while reading from input image file: %s",
4748 out_ret
= blk_pwrite(blk2
, out_pos
, in
.buf
, in_ret
, 0);
4751 error_report("error while writing to output image file: %s",
4752 strerror(-out_ret
));
4761 qemu_opts_del(opts
);
4762 qemu_opts_free(create_opts
);
4765 g_free(in
.filename
);
4766 g_free(out
.filename
);
4776 static void dump_json_block_measure_info(BlockMeasureInfo
*info
)
4780 Visitor
*v
= qobject_output_visitor_new(&obj
);
4782 visit_type_BlockMeasureInfo(v
, NULL
, &info
, &error_abort
);
4783 visit_complete(v
, &obj
);
4784 str
= qobject_to_json_pretty(obj
);
4785 assert(str
!= NULL
);
4786 printf("%s\n", qstring_get_str(str
));
4792 static int img_measure(int argc
, char **argv
)
4794 static const struct option long_options
[] = {
4795 {"help", no_argument
, 0, 'h'},
4796 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
4797 {"object", required_argument
, 0, OPTION_OBJECT
},
4798 {"output", required_argument
, 0, OPTION_OUTPUT
},
4799 {"size", required_argument
, 0, OPTION_SIZE
},
4800 {"force-share", no_argument
, 0, 'U'},
4803 OutputFormat output_format
= OFORMAT_HUMAN
;
4804 BlockBackend
*in_blk
= NULL
;
4806 const char *filename
= NULL
;
4807 const char *fmt
= NULL
;
4808 const char *out_fmt
= "raw";
4809 char *options
= NULL
;
4810 char *snapshot_name
= NULL
;
4811 bool force_share
= false;
4812 QemuOpts
*opts
= NULL
;
4813 QemuOpts
*object_opts
= NULL
;
4814 QemuOpts
*sn_opts
= NULL
;
4815 QemuOptsList
*create_opts
= NULL
;
4816 bool image_opts
= false;
4817 uint64_t img_size
= UINT64_MAX
;
4818 BlockMeasureInfo
*info
= NULL
;
4819 Error
*local_err
= NULL
;
4823 while ((c
= getopt_long(argc
, argv
, "hf:O:o:l:U",
4824 long_options
, NULL
)) != -1) {
4837 if (!is_valid_option_list(optarg
)) {
4838 error_report("Invalid option list: %s", optarg
);
4842 options
= g_strdup(optarg
);
4844 char *old_options
= options
;
4845 options
= g_strdup_printf("%s,%s", options
, optarg
);
4846 g_free(old_options
);
4850 if (strstart(optarg
, SNAPSHOT_OPT_BASE
, NULL
)) {
4851 sn_opts
= qemu_opts_parse_noisily(&internal_snapshot_opts
,
4854 error_report("Failed in parsing snapshot param '%s'",
4859 snapshot_name
= optarg
;
4866 object_opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
4872 case OPTION_IMAGE_OPTS
:
4876 if (!strcmp(optarg
, "json")) {
4877 output_format
= OFORMAT_JSON
;
4878 } else if (!strcmp(optarg
, "human")) {
4879 output_format
= OFORMAT_HUMAN
;
4881 error_report("--output must be used with human or json "
4890 sval
= cvtnum(optarg
);
4892 if (sval
== -ERANGE
) {
4893 error_report("Image size must be less than 8 EiB!");
4895 error_report("Invalid image size specified! You may use "
4896 "k, M, G, T, P or E suffixes for ");
4897 error_report("kilobytes, megabytes, gigabytes, terabytes, "
4898 "petabytes and exabytes.");
4902 img_size
= (uint64_t)sval
;
4908 if (qemu_opts_foreach(&qemu_object_opts
,
4909 user_creatable_add_opts_foreach
,
4910 NULL
, &error_fatal
)) {
4914 if (argc
- optind
> 1) {
4915 error_report("At most one filename argument is allowed.");
4917 } else if (argc
- optind
== 1) {
4918 filename
= argv
[optind
];
4922 (object_opts
|| image_opts
|| fmt
|| snapshot_name
|| sn_opts
)) {
4923 error_report("--object, --image-opts, -f, and -l "
4924 "require a filename argument.");
4927 if (filename
&& img_size
!= UINT64_MAX
) {
4928 error_report("--size N cannot be used together with a filename.");
4931 if (!filename
&& img_size
== UINT64_MAX
) {
4932 error_report("Either --size N or one filename must be specified.");
4937 in_blk
= img_open(image_opts
, filename
, fmt
, 0,
4938 false, false, force_share
);
4944 bdrv_snapshot_load_tmp(blk_bs(in_blk
),
4945 qemu_opt_get(sn_opts
, SNAPSHOT_OPT_ID
),
4946 qemu_opt_get(sn_opts
, SNAPSHOT_OPT_NAME
),
4948 } else if (snapshot_name
!= NULL
) {
4949 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk
),
4950 snapshot_name
, &local_err
);
4953 error_reportf_err(local_err
, "Failed to load snapshot: ");
4958 drv
= bdrv_find_format(out_fmt
);
4960 error_report("Unknown file format '%s'", out_fmt
);
4963 if (!drv
->create_opts
) {
4964 error_report("Format driver '%s' does not support image creation",
4969 create_opts
= qemu_opts_append(create_opts
, drv
->create_opts
);
4970 create_opts
= qemu_opts_append(create_opts
, bdrv_file
.create_opts
);
4971 opts
= qemu_opts_create(create_opts
, NULL
, 0, &error_abort
);
4973 qemu_opts_do_parse(opts
, options
, NULL
, &local_err
);
4975 error_report_err(local_err
);
4976 error_report("Invalid options for file format '%s'", out_fmt
);
4980 if (img_size
!= UINT64_MAX
) {
4981 qemu_opt_set_number(opts
, BLOCK_OPT_SIZE
, img_size
, &error_abort
);
4984 info
= bdrv_measure(drv
, opts
, in_blk
? blk_bs(in_blk
) : NULL
, &local_err
);
4986 error_report_err(local_err
);
4990 if (output_format
== OFORMAT_HUMAN
) {
4991 printf("required size: %" PRIu64
"\n", info
->required
);
4992 printf("fully allocated size: %" PRIu64
"\n", info
->fully_allocated
);
4994 dump_json_block_measure_info(info
);
5000 qapi_free_BlockMeasureInfo(info
);
5001 qemu_opts_del(object_opts
);
5002 qemu_opts_del(opts
);
5003 qemu_opts_del(sn_opts
);
5004 qemu_opts_free(create_opts
);
5010 static const img_cmd_t img_cmds
[] = {
5011 #define DEF(option, callback, arg_string) \
5012 { option, callback },
5013 #include "qemu-img-cmds.h"
5018 int main(int argc
, char **argv
)
5020 const img_cmd_t
*cmd
;
5021 const char *cmdname
;
5022 Error
*local_error
= NULL
;
5023 char *trace_file
= NULL
;
5025 static const struct option long_options
[] = {
5026 {"help", no_argument
, 0, 'h'},
5027 {"version", no_argument
, 0, 'V'},
5028 {"trace", required_argument
, NULL
, 'T'},
5033 signal(SIGPIPE
, SIG_IGN
);
5036 error_init(argv
[0]);
5037 module_call_init(MODULE_INIT_TRACE
);
5038 qemu_init_exec_dir(argv
[0]);
5040 if (qemu_init_main_loop(&local_error
)) {
5041 error_report_err(local_error
);
5045 qcrypto_init(&error_fatal
);
5047 module_call_init(MODULE_INIT_QOM
);
5050 error_exit("Not enough arguments");
5053 qemu_add_opts(&qemu_object_opts
);
5054 qemu_add_opts(&qemu_source_opts
);
5055 qemu_add_opts(&qemu_trace_opts
);
5057 while ((c
= getopt_long(argc
, argv
, "+:hVT:", long_options
, NULL
)) != -1) {
5060 missing_argument(argv
[optind
- 1]);
5063 unrecognized_option(argv
[optind
- 1]);
5069 printf(QEMU_IMG_VERSION
);
5073 trace_file
= trace_opt_parse(optarg
);
5078 cmdname
= argv
[optind
];
5080 /* reset getopt_long scanning */
5086 qemu_reset_optind();
5088 if (!trace_init_backends()) {
5091 trace_init_file(trace_file
);
5092 qemu_set_log(LOG_TRACE
);
5094 /* find the command */
5095 for (cmd
= img_cmds
; cmd
->name
!= NULL
; cmd
++) {
5096 if (!strcmp(cmdname
, cmd
->name
)) {
5097 return cmd
->handler(argc
, argv
);
5102 error_exit("Command not found: %s", cmdname
);