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-commands-block-core.h"
32 #include "qapi/qapi-visit-block-core.h"
33 #include "qapi/qobject-output-visitor.h"
34 #include "qapi/qmp/qjson.h"
35 #include "qapi/qmp/qdict.h"
36 #include "qapi/qmp/qstring.h"
37 #include "qemu/cutils.h"
38 #include "qemu/config-file.h"
39 #include "qemu/option.h"
40 #include "qemu/error-report.h"
42 #include "qemu/main-loop.h"
43 #include "qemu/module.h"
44 #include "qemu/units.h"
45 #include "qom/object_interfaces.h"
46 #include "sysemu/block-backend.h"
47 #include "block/block_int.h"
48 #include "block/blockjob.h"
49 #include "block/qapi.h"
50 #include "crypto/init.h"
51 #include "trace/control.h"
53 #define QEMU_IMG_VERSION "qemu-img version " QEMU_FULL_VERSION \
54 "\n" QEMU_COPYRIGHT "\n"
56 typedef struct img_cmd_t
{
58 int (*handler
)(int argc
, char **argv
);
63 OPTION_BACKING_CHAIN
= 257,
65 OPTION_IMAGE_OPTS
= 259,
67 OPTION_FLUSH_INTERVAL
= 261,
68 OPTION_NO_DRAIN
= 262,
69 OPTION_TARGET_IMAGE_OPTS
= 263,
71 OPTION_PREALLOCATION
= 265,
74 OPTION_TARGET_IS_ZERO
= 268,
85 typedef enum OutputFormat
{
90 /* Default to cache=writeback as data integrity is not important for qemu-img */
91 #define BDRV_DEFAULT_CACHE "writeback"
93 static void format_print(void *opaque
, const char *name
)
98 static void QEMU_NORETURN
GCC_FMT_ATTR(1, 2) error_exit(const char *fmt
, ...)
103 error_vreport(fmt
, ap
);
106 error_printf("Try 'qemu-img --help' for more information\n");
110 static void QEMU_NORETURN
missing_argument(const char *option
)
112 error_exit("missing argument for option '%s'", option
);
115 static void QEMU_NORETURN
unrecognized_option(const char *option
)
117 error_exit("unrecognized option '%s'", option
);
120 /* Please keep in synch with docs/tools/qemu-img.rst */
121 static void QEMU_NORETURN
help(void)
123 const char *help_msg
=
125 "usage: qemu-img [standard options] command [command options]\n"
126 "QEMU disk image utility\n"
128 " '-h', '--help' display this help and exit\n"
129 " '-V', '--version' output version information and exit\n"
130 " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
131 " specify tracing options\n"
134 #define DEF(option, callback, arg_string) \
136 #include "qemu-img-cmds.h"
139 "Command parameters:\n"
140 " 'filename' is a disk image filename\n"
141 " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
142 " manual page for a description of the object properties. The most common\n"
143 " object type is a 'secret', which is used to supply passwords and/or\n"
144 " encryption keys.\n"
145 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
146 " 'cache' is the cache mode used to write the output disk image, the valid\n"
147 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
148 " 'directsync' and 'unsafe' (default for convert)\n"
149 " 'src_cache' is the cache mode used to read input disk images, the valid\n"
150 " options are the same as for the 'cache' option\n"
151 " 'size' is the disk image size in bytes. Optional suffixes\n"
152 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
153 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
154 " supported. 'b' is ignored.\n"
155 " 'output_filename' is the destination disk image filename\n"
156 " 'output_fmt' is the destination format\n"
157 " 'options' is a comma separated list of format specific options in a\n"
158 " name=value format. Use -o ? for an overview of the options supported by the\n"
160 " 'snapshot_param' is param used for internal snapshot, format\n"
161 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
163 " '-c' indicates that target image must be compressed (qcow format only)\n"
164 " '-u' allows unsafe backing chains. For rebasing, it is assumed that old and\n"
165 " new backing file match exactly. The image doesn't need a working\n"
166 " backing file before rebasing in this case (useful for renaming the\n"
167 " backing file). For image creation, allow creating without attempting\n"
168 " to open the backing file.\n"
169 " '-h' with or without a command shows this help and lists the supported formats\n"
170 " '-p' show progress of command (only certain commands)\n"
171 " '-q' use Quiet mode - do not print any output (except errors)\n"
172 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
173 " contain only zeros for qemu-img to create a sparse image during\n"
174 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
175 " unallocated or zero sectors, and the destination image will always be\n"
177 " '--output' takes the format in which the output must be done (human or json)\n"
178 " '-n' skips the target volume creation (useful if the volume is created\n"
179 " prior to running qemu-img)\n"
181 "Parameters to bitmap subcommand:\n"
182 " 'bitmap' is the name of the bitmap to manipulate, through one or more\n"
183 " actions from '--add', '--remove', '--clear', '--enable', '--disable',\n"
184 " or '--merge source'\n"
185 " '-g granularity' sets the granularity for '--add' actions\n"
186 " '-b source' and '-F src_fmt' tell '--merge' actions to find the source\n"
187 " bitmaps from an alternative file\n"
189 "Parameters to check subcommand:\n"
190 " '-r' tries to repair any inconsistencies that are found during the check.\n"
191 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
192 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
193 " hiding corruption that has already occurred.\n"
195 "Parameters to convert subcommand:\n"
196 " '--bitmaps' copies all top-level persistent bitmaps to destination\n"
197 " '-m' specifies how many coroutines work in parallel during the convert\n"
198 " process (defaults to 8)\n"
199 " '-W' allow to write to the target out of order rather than sequential\n"
201 "Parameters to snapshot subcommand:\n"
202 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
203 " '-a' applies a snapshot (revert disk to saved state)\n"
204 " '-c' creates a snapshot\n"
205 " '-d' deletes a snapshot\n"
206 " '-l' lists all snapshots in the given image\n"
208 "Parameters to compare subcommand:\n"
209 " '-f' first image format\n"
210 " '-F' second image format\n"
211 " '-s' run in Strict mode - fail on different image size or sector allocation\n"
213 "Parameters to dd subcommand:\n"
214 " 'bs=BYTES' read and write up to BYTES bytes at a time "
216 " 'count=N' copy only N input blocks\n"
217 " 'if=FILE' read from FILE\n"
218 " 'of=FILE' write to FILE\n"
219 " 'skip=N' skip N bs-sized blocks at the start of input\n";
221 printf("%s\nSupported formats:", help_msg
);
222 bdrv_iterate_format(format_print
, NULL
, false);
223 printf("\n\n" QEMU_HELP_BOTTOM
"\n");
227 static QemuOptsList qemu_object_opts
= {
229 .implied_opt_name
= "qom-type",
230 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
236 static bool qemu_img_object_print_help(const char *type
, QemuOpts
*opts
)
238 if (user_creatable_print_help(type
, opts
)) {
245 * Is @optarg safe for accumulate_options()?
246 * It is when multiple of them can be joined together separated by ','.
247 * To make that work, @optarg must not start with ',' (or else a
248 * separating ',' preceding it gets escaped), and it must not end with
249 * an odd number of ',' (or else a separating ',' following it gets
250 * escaped), or be empty (or else a separating ',' preceding it can
251 * escape a separating ',' following it).
254 static bool is_valid_option_list(const char *optarg
)
256 size_t len
= strlen(optarg
);
259 if (!optarg
[0] || optarg
[0] == ',') {
263 for (i
= len
; i
> 0 && optarg
[i
- 1] == ','; i
--) {
272 static int accumulate_options(char **options
, char *optarg
)
276 if (!is_valid_option_list(optarg
)) {
277 error_report("Invalid option list: %s", optarg
);
282 *options
= g_strdup(optarg
);
284 new_options
= g_strdup_printf("%s,%s", *options
, optarg
);
286 *options
= new_options
;
291 static QemuOptsList qemu_source_opts
= {
293 .implied_opt_name
= "file",
294 .head
= QTAILQ_HEAD_INITIALIZER(qemu_source_opts
.head
),
300 static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet
, const char *fmt
, ...)
306 ret
= vprintf(fmt
, args
);
313 static int print_block_option_help(const char *filename
, const char *fmt
)
315 BlockDriver
*drv
, *proto_drv
;
316 QemuOptsList
*create_opts
= NULL
;
317 Error
*local_err
= NULL
;
319 /* Find driver and parse its options */
320 drv
= bdrv_find_format(fmt
);
322 error_report("Unknown file format '%s'", fmt
);
326 if (!drv
->create_opts
) {
327 error_report("Format driver '%s' does not support image creation", fmt
);
331 create_opts
= qemu_opts_append(create_opts
, drv
->create_opts
);
333 proto_drv
= bdrv_find_protocol(filename
, true, &local_err
);
335 error_report_err(local_err
);
336 qemu_opts_free(create_opts
);
339 if (!proto_drv
->create_opts
) {
340 error_report("Protocol driver '%s' does not support image creation",
341 proto_drv
->format_name
);
342 qemu_opts_free(create_opts
);
345 create_opts
= qemu_opts_append(create_opts
, proto_drv
->create_opts
);
349 printf("Supported options:\n");
351 printf("Supported %s options:\n", fmt
);
353 qemu_opts_print_help(create_opts
, false);
354 qemu_opts_free(create_opts
);
358 "The protocol level may support further options.\n"
359 "Specify the target filename to include those options.\n");
366 static BlockBackend
*img_open_opts(const char *optstr
,
367 QemuOpts
*opts
, int flags
, bool writethrough
,
368 bool quiet
, bool force_share
)
371 Error
*local_err
= NULL
;
373 options
= qemu_opts_to_qdict(opts
, NULL
);
375 if (qdict_haskey(options
, BDRV_OPT_FORCE_SHARE
)
376 && strcmp(qdict_get_str(options
, BDRV_OPT_FORCE_SHARE
), "on")) {
377 error_report("--force-share/-U conflicts with image options");
378 qobject_unref(options
);
381 qdict_put_str(options
, BDRV_OPT_FORCE_SHARE
, "on");
383 blk
= blk_new_open(NULL
, NULL
, options
, flags
, &local_err
);
385 error_reportf_err(local_err
, "Could not open '%s': ", optstr
);
388 blk_set_enable_write_cache(blk
, !writethrough
);
393 static BlockBackend
*img_open_file(const char *filename
,
395 const char *fmt
, int flags
,
396 bool writethrough
, bool quiet
,
400 Error
*local_err
= NULL
;
403 options
= qdict_new();
406 qdict_put_str(options
, "driver", fmt
);
410 qdict_put_bool(options
, BDRV_OPT_FORCE_SHARE
, true);
412 blk
= blk_new_open(filename
, NULL
, options
, flags
, &local_err
);
414 error_reportf_err(local_err
, "Could not open '%s': ", filename
);
417 blk_set_enable_write_cache(blk
, !writethrough
);
423 static int img_add_key_secrets(void *opaque
,
424 const char *name
, const char *value
,
427 QDict
*options
= opaque
;
429 if (g_str_has_suffix(name
, "key-secret")) {
430 qdict_put_str(options
, name
, value
);
437 static BlockBackend
*img_open(bool image_opts
,
438 const char *filename
,
439 const char *fmt
, int flags
, bool writethrough
,
440 bool quiet
, bool force_share
)
446 error_report("--image-opts and --format are mutually exclusive");
449 opts
= qemu_opts_parse_noisily(qemu_find_opts("source"),
454 blk
= img_open_opts(filename
, opts
, flags
, writethrough
, quiet
,
457 blk
= img_open_file(filename
, NULL
, fmt
, flags
, writethrough
, quiet
,
464 static int add_old_style_options(const char *fmt
, QemuOpts
*opts
,
465 const char *base_filename
,
466 const char *base_fmt
)
469 if (!qemu_opt_set(opts
, BLOCK_OPT_BACKING_FILE
, base_filename
,
471 error_report("Backing file not supported for file format '%s'",
477 if (!qemu_opt_set(opts
, BLOCK_OPT_BACKING_FMT
, base_fmt
, NULL
)) {
478 error_report("Backing file format not supported for file "
486 static int64_t cvtnum_full(const char *name
, const char *value
, int64_t min
,
492 err
= qemu_strtosz(value
, NULL
, &res
);
493 if (err
< 0 && err
!= -ERANGE
) {
494 error_report("Invalid %s specified. You may use "
495 "k, M, G, T, P or E suffixes for", name
);
496 error_report("kilobytes, megabytes, gigabytes, terabytes, "
497 "petabytes and exabytes.");
500 if (err
== -ERANGE
|| res
> max
|| res
< min
) {
501 error_report("Invalid %s specified. Must be between %" PRId64
502 " and %" PRId64
".", name
, min
, max
);
508 static int64_t cvtnum(const char *name
, const char *value
)
510 return cvtnum_full(name
, value
, 0, INT64_MAX
);
513 static int img_create(int argc
, char **argv
)
516 uint64_t img_size
= -1;
517 const char *fmt
= "raw";
518 const char *base_fmt
= NULL
;
519 const char *filename
;
520 const char *base_filename
= NULL
;
521 char *options
= NULL
;
522 Error
*local_err
= NULL
;
527 static const struct option long_options
[] = {
528 {"help", no_argument
, 0, 'h'},
529 {"object", required_argument
, 0, OPTION_OBJECT
},
532 c
= getopt_long(argc
, argv
, ":F:b:f:ho:qu",
539 missing_argument(argv
[optind
- 1]);
542 unrecognized_option(argv
[optind
- 1]);
551 base_filename
= optarg
;
557 if (accumulate_options(&options
, optarg
) < 0) {
565 flags
|= BDRV_O_NO_BACKING
;
567 case OPTION_OBJECT
: {
569 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
578 /* Get the filename */
579 filename
= (optind
< argc
) ? argv
[optind
] : NULL
;
580 if (options
&& has_help_option(options
)) {
582 return print_block_option_help(filename
, fmt
);
585 if (optind
>= argc
) {
586 error_exit("Expecting image file name");
590 if (qemu_opts_foreach(&qemu_object_opts
,
591 user_creatable_add_opts_foreach
,
592 qemu_img_object_print_help
, &error_fatal
)) {
596 /* Get image size, if specified */
600 sval
= cvtnum("image size", argv
[optind
++]);
604 img_size
= (uint64_t)sval
;
606 if (optind
!= argc
) {
607 error_exit("Unexpected argument: %s", argv
[optind
]);
610 bdrv_img_create(filename
, fmt
, base_filename
, base_fmt
,
611 options
, img_size
, flags
, quiet
, &local_err
);
613 error_reportf_err(local_err
, "%s: ", filename
);
625 static void dump_json_image_check(ImageCheck
*check
, bool quiet
)
629 Visitor
*v
= qobject_output_visitor_new(&obj
);
631 visit_type_ImageCheck(v
, NULL
, &check
, &error_abort
);
632 visit_complete(v
, &obj
);
633 str
= qobject_to_json_pretty(obj
);
635 qprintf(quiet
, "%s\n", qstring_get_str(str
));
641 static void dump_human_image_check(ImageCheck
*check
, bool quiet
)
643 if (!(check
->corruptions
|| check
->leaks
|| check
->check_errors
)) {
644 qprintf(quiet
, "No errors were found on the image.\n");
646 if (check
->corruptions
) {
647 qprintf(quiet
, "\n%" PRId64
" errors were found on the image.\n"
648 "Data may be corrupted, or further writes to the image "
655 "\n%" PRId64
" leaked clusters were found on the image.\n"
656 "This means waste of disk space, but no harm to data.\n",
660 if (check
->check_errors
) {
663 " internal errors have occurred during the check.\n",
664 check
->check_errors
);
668 if (check
->total_clusters
!= 0 && check
->allocated_clusters
!= 0) {
669 qprintf(quiet
, "%" PRId64
"/%" PRId64
" = %0.2f%% allocated, "
670 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
671 check
->allocated_clusters
, check
->total_clusters
,
672 check
->allocated_clusters
* 100.0 / check
->total_clusters
,
673 check
->fragmented_clusters
* 100.0 / check
->allocated_clusters
,
674 check
->compressed_clusters
* 100.0 /
675 check
->allocated_clusters
);
678 if (check
->image_end_offset
) {
680 "Image end offset: %" PRId64
"\n", check
->image_end_offset
);
684 static int collect_image_check(BlockDriverState
*bs
,
686 const char *filename
,
691 BdrvCheckResult result
;
693 ret
= bdrv_check(bs
, &result
, fix
);
698 check
->filename
= g_strdup(filename
);
699 check
->format
= g_strdup(bdrv_get_format_name(bs
));
700 check
->check_errors
= result
.check_errors
;
701 check
->corruptions
= result
.corruptions
;
702 check
->has_corruptions
= result
.corruptions
!= 0;
703 check
->leaks
= result
.leaks
;
704 check
->has_leaks
= result
.leaks
!= 0;
705 check
->corruptions_fixed
= result
.corruptions_fixed
;
706 check
->has_corruptions_fixed
= result
.corruptions_fixed
!= 0;
707 check
->leaks_fixed
= result
.leaks_fixed
;
708 check
->has_leaks_fixed
= result
.leaks_fixed
!= 0;
709 check
->image_end_offset
= result
.image_end_offset
;
710 check
->has_image_end_offset
= result
.image_end_offset
!= 0;
711 check
->total_clusters
= result
.bfi
.total_clusters
;
712 check
->has_total_clusters
= result
.bfi
.total_clusters
!= 0;
713 check
->allocated_clusters
= result
.bfi
.allocated_clusters
;
714 check
->has_allocated_clusters
= result
.bfi
.allocated_clusters
!= 0;
715 check
->fragmented_clusters
= result
.bfi
.fragmented_clusters
;
716 check
->has_fragmented_clusters
= result
.bfi
.fragmented_clusters
!= 0;
717 check
->compressed_clusters
= result
.bfi
.compressed_clusters
;
718 check
->has_compressed_clusters
= result
.bfi
.compressed_clusters
!= 0;
724 * Checks an image for consistency. Exit codes:
726 * 0 - Check completed, image is good
727 * 1 - Check not completed because of internal errors
728 * 2 - Check completed, image is corrupted
729 * 3 - Check completed, image has leaked clusters, but is good otherwise
730 * 63 - Checks are not supported by the image format
732 static int img_check(int argc
, char **argv
)
735 OutputFormat output_format
= OFORMAT_HUMAN
;
736 const char *filename
, *fmt
, *output
, *cache
;
738 BlockDriverState
*bs
;
740 int flags
= BDRV_O_CHECK
;
744 bool image_opts
= false;
745 bool force_share
= false;
749 cache
= BDRV_DEFAULT_CACHE
;
752 int option_index
= 0;
753 static const struct option long_options
[] = {
754 {"help", no_argument
, 0, 'h'},
755 {"format", required_argument
, 0, 'f'},
756 {"repair", required_argument
, 0, 'r'},
757 {"output", required_argument
, 0, OPTION_OUTPUT
},
758 {"object", required_argument
, 0, OPTION_OBJECT
},
759 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
760 {"force-share", no_argument
, 0, 'U'},
763 c
= getopt_long(argc
, argv
, ":hf:r:T:qU",
764 long_options
, &option_index
);
770 missing_argument(argv
[optind
- 1]);
773 unrecognized_option(argv
[optind
- 1]);
782 flags
|= BDRV_O_RDWR
;
784 if (!strcmp(optarg
, "leaks")) {
785 fix
= BDRV_FIX_LEAKS
;
786 } else if (!strcmp(optarg
, "all")) {
787 fix
= BDRV_FIX_LEAKS
| BDRV_FIX_ERRORS
;
789 error_exit("Unknown option value for -r "
790 "(expecting 'leaks' or 'all'): %s", optarg
);
805 case OPTION_OBJECT
: {
807 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
813 case OPTION_IMAGE_OPTS
:
818 if (optind
!= argc
- 1) {
819 error_exit("Expecting one image file name");
821 filename
= argv
[optind
++];
823 if (output
&& !strcmp(output
, "json")) {
824 output_format
= OFORMAT_JSON
;
825 } else if (output
&& !strcmp(output
, "human")) {
826 output_format
= OFORMAT_HUMAN
;
828 error_report("--output must be used with human or json as argument.");
832 if (qemu_opts_foreach(&qemu_object_opts
,
833 user_creatable_add_opts_foreach
,
834 qemu_img_object_print_help
, &error_fatal
)) {
838 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
840 error_report("Invalid source cache option: %s", cache
);
844 blk
= img_open(image_opts
, filename
, fmt
, flags
, writethrough
, quiet
,
851 check
= g_new0(ImageCheck
, 1);
852 ret
= collect_image_check(bs
, check
, filename
, fmt
, fix
);
854 if (ret
== -ENOTSUP
) {
855 error_report("This image format does not support checks");
860 if (check
->corruptions_fixed
|| check
->leaks_fixed
) {
861 int corruptions_fixed
, leaks_fixed
;
862 bool has_leaks_fixed
, has_corruptions_fixed
;
864 leaks_fixed
= check
->leaks_fixed
;
865 has_leaks_fixed
= check
->has_leaks_fixed
;
866 corruptions_fixed
= check
->corruptions_fixed
;
867 has_corruptions_fixed
= check
->has_corruptions_fixed
;
869 if (output_format
== OFORMAT_HUMAN
) {
871 "The following inconsistencies were found and repaired:\n\n"
872 " %" PRId64
" leaked clusters\n"
873 " %" PRId64
" corruptions\n\n"
874 "Double checking the fixed image now...\n",
876 check
->corruptions_fixed
);
879 qapi_free_ImageCheck(check
);
880 check
= g_new0(ImageCheck
, 1);
881 ret
= collect_image_check(bs
, check
, filename
, fmt
, 0);
883 check
->leaks_fixed
= leaks_fixed
;
884 check
->has_leaks_fixed
= has_leaks_fixed
;
885 check
->corruptions_fixed
= corruptions_fixed
;
886 check
->has_corruptions_fixed
= has_corruptions_fixed
;
890 switch (output_format
) {
892 dump_human_image_check(check
, quiet
);
895 dump_json_image_check(check
, quiet
);
900 if (ret
|| check
->check_errors
) {
902 error_report("Check failed: %s", strerror(-ret
));
904 error_report("Check failed");
910 if (check
->corruptions
) {
912 } else if (check
->leaks
) {
919 qapi_free_ImageCheck(check
);
924 typedef struct CommonBlockJobCBInfo
{
925 BlockDriverState
*bs
;
927 } CommonBlockJobCBInfo
;
929 static void common_block_job_cb(void *opaque
, int ret
)
931 CommonBlockJobCBInfo
*cbi
= opaque
;
934 error_setg_errno(cbi
->errp
, -ret
, "Block job failed");
938 static void run_block_job(BlockJob
*job
, Error
**errp
)
940 AioContext
*aio_context
= blk_get_aio_context(job
->blk
);
943 aio_context_acquire(aio_context
);
946 float progress
= 0.0f
;
947 aio_poll(aio_context
, true);
948 if (job
->job
.progress
.total
) {
949 progress
= (float)job
->job
.progress
.current
/
950 job
->job
.progress
.total
* 100.f
;
952 qemu_progress_print(progress
, 0);
953 } while (!job_is_ready(&job
->job
) && !job_is_completed(&job
->job
));
955 if (!job_is_completed(&job
->job
)) {
956 ret
= job_complete_sync(&job
->job
, errp
);
960 job_unref(&job
->job
);
961 aio_context_release(aio_context
);
963 /* publish completion progress only when success */
965 qemu_progress_print(100.f
, 0);
969 static int img_commit(int argc
, char **argv
)
972 const char *filename
, *fmt
, *cache
, *base
;
974 BlockDriverState
*bs
, *base_bs
;
976 bool progress
= false, quiet
= false, drop
= false;
978 Error
*local_err
= NULL
;
979 CommonBlockJobCBInfo cbi
;
980 bool image_opts
= false;
981 AioContext
*aio_context
;
984 cache
= BDRV_DEFAULT_CACHE
;
987 static const struct option long_options
[] = {
988 {"help", no_argument
, 0, 'h'},
989 {"object", required_argument
, 0, OPTION_OBJECT
},
990 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
993 c
= getopt_long(argc
, argv
, ":f:ht:b:dpq",
1000 missing_argument(argv
[optind
- 1]);
1003 unrecognized_option(argv
[optind
- 1]);
1028 case OPTION_OBJECT
: {
1030 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
1036 case OPTION_IMAGE_OPTS
:
1042 /* Progress is not shown in Quiet mode */
1047 if (optind
!= argc
- 1) {
1048 error_exit("Expecting one image file name");
1050 filename
= argv
[optind
++];
1052 if (qemu_opts_foreach(&qemu_object_opts
,
1053 user_creatable_add_opts_foreach
,
1054 qemu_img_object_print_help
, &error_fatal
)) {
1058 flags
= BDRV_O_RDWR
| BDRV_O_UNMAP
;
1059 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
1061 error_report("Invalid cache option: %s", cache
);
1065 blk
= img_open(image_opts
, filename
, fmt
, flags
, writethrough
, quiet
,
1072 qemu_progress_init(progress
, 1.f
);
1073 qemu_progress_print(0.f
, 100);
1076 base_bs
= bdrv_find_backing_image(bs
, base
);
1078 error_setg(&local_err
,
1079 "Did not find '%s' in the backing chain of '%s'",
1084 /* This is different from QMP, which by default uses the deepest file in
1085 * the backing chain (i.e., the very base); however, the traditional
1086 * behavior of qemu-img commit is using the immediate backing file. */
1087 base_bs
= backing_bs(bs
);
1089 error_setg(&local_err
, "Image does not have a backing file");
1094 cbi
= (CommonBlockJobCBInfo
){
1099 aio_context
= bdrv_get_aio_context(bs
);
1100 aio_context_acquire(aio_context
);
1101 commit_active_start("commit", bs
, base_bs
, JOB_DEFAULT
, 0,
1102 BLOCKDEV_ON_ERROR_REPORT
, NULL
, common_block_job_cb
,
1103 &cbi
, false, &local_err
);
1104 aio_context_release(aio_context
);
1109 /* When the block job completes, the BlockBackend reference will point to
1110 * the old backing file. In order to avoid that the top image is already
1111 * deleted, so we can still empty it afterwards, increment the reference
1112 * counter here preemptively. */
1117 job
= block_job_get("commit");
1119 run_block_job(job
, &local_err
);
1125 BlockBackend
*old_backing_blk
;
1127 old_backing_blk
= blk_new_with_bs(bs
, BLK_PERM_WRITE
, BLK_PERM_ALL
,
1129 if (!old_backing_blk
) {
1132 ret
= blk_make_empty(old_backing_blk
, &local_err
);
1133 blk_unref(old_backing_blk
);
1134 if (ret
== -ENOTSUP
) {
1135 error_free(local_err
);
1137 } else if (ret
< 0) {
1148 qemu_progress_end();
1153 error_report_err(local_err
);
1157 qprintf(quiet
, "Image committed.\n");
1162 * Returns -1 if 'buf' contains only zeroes, otherwise the byte index
1163 * of the first sector boundary within buf where the sector contains a
1164 * non-zero byte. This function is robust to a buffer that is not
1167 static int64_t find_nonzero(const uint8_t *buf
, int64_t n
)
1170 int64_t end
= QEMU_ALIGN_DOWN(n
, BDRV_SECTOR_SIZE
);
1172 for (i
= 0; i
< end
; i
+= BDRV_SECTOR_SIZE
) {
1173 if (!buffer_is_zero(buf
+ i
, BDRV_SECTOR_SIZE
)) {
1177 if (i
< n
&& !buffer_is_zero(buf
+ i
, n
- end
)) {
1184 * Returns true iff the first sector pointed to by 'buf' contains at least
1187 * 'pnum' is set to the number of sectors (including and immediately following
1188 * the first one) that are known to be in the same allocated/unallocated state.
1189 * The function will try to align the end offset to alignment boundaries so
1190 * that the request will at least end aligned and consequtive requests will
1191 * also start at an aligned offset.
1193 static int is_allocated_sectors(const uint8_t *buf
, int n
, int *pnum
,
1194 int64_t sector_num
, int alignment
)
1203 is_zero
= buffer_is_zero(buf
, 512);
1204 for(i
= 1; i
< n
; i
++) {
1206 if (is_zero
!= buffer_is_zero(buf
, 512)) {
1211 tail
= (sector_num
+ i
) & (alignment
- 1);
1213 if (is_zero
&& i
<= tail
) {
1214 /* treat unallocated areas which only consist
1215 * of a small tail as allocated. */
1219 /* align up end offset of allocated areas. */
1220 i
+= alignment
- tail
;
1223 /* align down end offset of zero areas. */
1232 * Like is_allocated_sectors, but if the buffer starts with a used sector,
1233 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1234 * breaking up write requests for only small sparse areas.
1236 static int is_allocated_sectors_min(const uint8_t *buf
, int n
, int *pnum
,
1237 int min
, int64_t sector_num
, int alignment
)
1240 int num_checked
, num_used
;
1246 ret
= is_allocated_sectors(buf
, n
, pnum
, sector_num
, alignment
);
1252 buf
+= BDRV_SECTOR_SIZE
* *pnum
;
1254 sector_num
+= *pnum
;
1255 num_checked
= num_used
;
1258 ret
= is_allocated_sectors(buf
, n
, pnum
, sector_num
, alignment
);
1260 buf
+= BDRV_SECTOR_SIZE
* *pnum
;
1262 sector_num
+= *pnum
;
1263 num_checked
+= *pnum
;
1265 num_used
= num_checked
;
1266 } else if (*pnum
>= min
) {
1276 * Compares two buffers sector by sector. Returns 0 if the first
1277 * sector of each buffer matches, non-zero otherwise.
1279 * pnum is set to the sector-aligned size of the buffer prefix that
1280 * has the same matching status as the first sector.
1282 static int compare_buffers(const uint8_t *buf1
, const uint8_t *buf2
,
1283 int64_t bytes
, int64_t *pnum
)
1286 int64_t i
= MIN(bytes
, BDRV_SECTOR_SIZE
);
1290 res
= !!memcmp(buf1
, buf2
, i
);
1292 int64_t len
= MIN(bytes
- i
, BDRV_SECTOR_SIZE
);
1294 if (!!memcmp(buf1
+ i
, buf2
+ i
, len
) != res
) {
1304 #define IO_BUF_SIZE (2 * MiB)
1307 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1309 * Intended for use by 'qemu-img compare': Returns 0 in case sectors are
1310 * filled with 0, 1 if sectors contain non-zero data (this is a comparison
1311 * failure), and 4 on error (the exit status for read errors), after emitting
1314 * @param blk: BlockBackend for the image
1315 * @param offset: Starting offset to check
1316 * @param bytes: Number of bytes to check
1317 * @param filename: Name of disk file we are checking (logging purpose)
1318 * @param buffer: Allocated buffer for storing read data
1319 * @param quiet: Flag for quiet mode
1321 static int check_empty_sectors(BlockBackend
*blk
, int64_t offset
,
1322 int64_t bytes
, const char *filename
,
1323 uint8_t *buffer
, bool quiet
)
1328 ret
= blk_pread(blk
, offset
, buffer
, bytes
);
1330 error_report("Error while reading offset %" PRId64
" of %s: %s",
1331 offset
, filename
, strerror(-ret
));
1334 idx
= find_nonzero(buffer
, bytes
);
1336 qprintf(quiet
, "Content mismatch at offset %" PRId64
"!\n",
1345 * Compares two images. Exit codes:
1347 * 0 - Images are identical
1349 * >1 - Error occurred
1351 static int img_compare(int argc
, char **argv
)
1353 const char *fmt1
= NULL
, *fmt2
= NULL
, *cache
, *filename1
, *filename2
;
1354 BlockBackend
*blk1
, *blk2
;
1355 BlockDriverState
*bs1
, *bs2
;
1356 int64_t total_size1
, total_size2
;
1357 uint8_t *buf1
= NULL
, *buf2
= NULL
;
1358 int64_t pnum1
, pnum2
;
1359 int allocated1
, allocated2
;
1360 int ret
= 0; /* return value - 0 Ident, 1 Different, >1 Error */
1361 bool progress
= false, quiet
= false, strict
= false;
1368 uint64_t progress_base
;
1369 bool image_opts
= false;
1370 bool force_share
= false;
1372 cache
= BDRV_DEFAULT_CACHE
;
1374 static const struct option long_options
[] = {
1375 {"help", no_argument
, 0, 'h'},
1376 {"object", required_argument
, 0, OPTION_OBJECT
},
1377 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
1378 {"force-share", no_argument
, 0, 'U'},
1381 c
= getopt_long(argc
, argv
, ":hf:F:T:pqsU",
1382 long_options
, NULL
);
1388 missing_argument(argv
[optind
- 1]);
1391 unrecognized_option(argv
[optind
- 1]);
1417 case OPTION_OBJECT
: {
1419 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
1426 case OPTION_IMAGE_OPTS
:
1432 /* Progress is not shown in Quiet mode */
1438 if (optind
!= argc
- 2) {
1439 error_exit("Expecting two image file names");
1441 filename1
= argv
[optind
++];
1442 filename2
= argv
[optind
++];
1444 if (qemu_opts_foreach(&qemu_object_opts
,
1445 user_creatable_add_opts_foreach
,
1446 qemu_img_object_print_help
, &error_fatal
)) {
1451 /* Initialize before goto out */
1452 qemu_progress_init(progress
, 2.0);
1455 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
1457 error_report("Invalid source cache option: %s", cache
);
1462 blk1
= img_open(image_opts
, filename1
, fmt1
, flags
, writethrough
, quiet
,
1469 blk2
= img_open(image_opts
, filename2
, fmt2
, flags
, writethrough
, quiet
,
1478 buf1
= blk_blockalign(blk1
, IO_BUF_SIZE
);
1479 buf2
= blk_blockalign(blk2
, IO_BUF_SIZE
);
1480 total_size1
= blk_getlength(blk1
);
1481 if (total_size1
< 0) {
1482 error_report("Can't get size of %s: %s",
1483 filename1
, strerror(-total_size1
));
1487 total_size2
= blk_getlength(blk2
);
1488 if (total_size2
< 0) {
1489 error_report("Can't get size of %s: %s",
1490 filename2
, strerror(-total_size2
));
1494 total_size
= MIN(total_size1
, total_size2
);
1495 progress_base
= MAX(total_size1
, total_size2
);
1497 qemu_progress_print(0, 100);
1499 if (strict
&& total_size1
!= total_size2
) {
1501 qprintf(quiet
, "Strict mode: Image size mismatch!\n");
1505 while (offset
< total_size
) {
1506 int status1
, status2
;
1508 status1
= bdrv_block_status_above(bs1
, NULL
, offset
,
1509 total_size1
- offset
, &pnum1
, NULL
,
1513 error_report("Sector allocation test failed for %s", filename1
);
1516 allocated1
= status1
& BDRV_BLOCK_ALLOCATED
;
1518 status2
= bdrv_block_status_above(bs2
, NULL
, offset
,
1519 total_size2
- offset
, &pnum2
, NULL
,
1523 error_report("Sector allocation test failed for %s", filename2
);
1526 allocated2
= status2
& BDRV_BLOCK_ALLOCATED
;
1528 assert(pnum1
&& pnum2
);
1529 chunk
= MIN(pnum1
, pnum2
);
1532 if (status1
!= status2
) {
1534 qprintf(quiet
, "Strict mode: Offset %" PRId64
1535 " block status mismatch!\n", offset
);
1539 if ((status1
& BDRV_BLOCK_ZERO
) && (status2
& BDRV_BLOCK_ZERO
)) {
1541 } else if (allocated1
== allocated2
) {
1545 chunk
= MIN(chunk
, IO_BUF_SIZE
);
1546 ret
= blk_pread(blk1
, offset
, buf1
, chunk
);
1548 error_report("Error while reading offset %" PRId64
1550 offset
, filename1
, strerror(-ret
));
1554 ret
= blk_pread(blk2
, offset
, buf2
, chunk
);
1556 error_report("Error while reading offset %" PRId64
1558 offset
, filename2
, strerror(-ret
));
1562 ret
= compare_buffers(buf1
, buf2
, chunk
, &pnum
);
1563 if (ret
|| pnum
!= chunk
) {
1564 qprintf(quiet
, "Content mismatch at offset %" PRId64
"!\n",
1565 offset
+ (ret
? 0 : pnum
));
1571 chunk
= MIN(chunk
, IO_BUF_SIZE
);
1573 ret
= check_empty_sectors(blk1
, offset
, chunk
,
1574 filename1
, buf1
, quiet
);
1576 ret
= check_empty_sectors(blk2
, offset
, chunk
,
1577 filename2
, buf1
, quiet
);
1584 qemu_progress_print(((float) chunk
/ progress_base
) * 100, 100);
1587 if (total_size1
!= total_size2
) {
1588 BlockBackend
*blk_over
;
1589 const char *filename_over
;
1591 qprintf(quiet
, "Warning: Image size mismatch!\n");
1592 if (total_size1
> total_size2
) {
1594 filename_over
= filename1
;
1597 filename_over
= filename2
;
1600 while (offset
< progress_base
) {
1601 ret
= bdrv_block_status_above(blk_bs(blk_over
), NULL
, offset
,
1602 progress_base
- offset
, &chunk
,
1606 error_report("Sector allocation test failed for %s",
1611 if (ret
& BDRV_BLOCK_ALLOCATED
&& !(ret
& BDRV_BLOCK_ZERO
)) {
1612 chunk
= MIN(chunk
, IO_BUF_SIZE
);
1613 ret
= check_empty_sectors(blk_over
, offset
, chunk
,
1614 filename_over
, buf1
, quiet
);
1620 qemu_progress_print(((float) chunk
/ progress_base
) * 100, 100);
1624 qprintf(quiet
, "Images are identical.\n");
1634 qemu_progress_end();
1639 /* Convenience wrapper around qmp_block_dirty_bitmap_merge */
1640 static void do_dirty_bitmap_merge(const char *dst_node
, const char *dst_name
,
1641 const char *src_node
, const char *src_name
,
1644 BlockDirtyBitmapMergeSource
*merge_src
;
1645 BlockDirtyBitmapMergeSourceList
*list
;
1647 merge_src
= g_new0(BlockDirtyBitmapMergeSource
, 1);
1648 merge_src
->type
= QTYPE_QDICT
;
1649 merge_src
->u
.external
.node
= g_strdup(src_node
);
1650 merge_src
->u
.external
.name
= g_strdup(src_name
);
1651 list
= g_new0(BlockDirtyBitmapMergeSourceList
, 1);
1652 list
->value
= merge_src
;
1653 qmp_block_dirty_bitmap_merge(dst_node
, dst_name
, list
, errp
);
1654 qapi_free_BlockDirtyBitmapMergeSourceList(list
);
1657 enum ImgConvertBlockStatus
{
1663 #define MAX_COROUTINES 16
1665 typedef struct ImgConvertState
{
1667 int64_t *src_sectors
;
1669 int64_t total_sectors
;
1670 int64_t allocated_sectors
;
1671 int64_t allocated_done
;
1674 enum ImgConvertBlockStatus status
;
1675 int64_t sector_next_status
;
1676 BlockBackend
*target
;
1680 bool target_has_backing
;
1681 int64_t target_backing_sectors
; /* negative if unknown */
1688 size_t cluster_sectors
;
1690 long num_coroutines
;
1691 int running_coroutines
;
1692 Coroutine
*co
[MAX_COROUTINES
];
1693 int64_t wait_sector_num
[MAX_COROUTINES
];
1698 static void convert_select_part(ImgConvertState
*s
, int64_t sector_num
,
1699 int *src_cur
, int64_t *src_cur_offset
)
1702 *src_cur_offset
= 0;
1703 while (sector_num
- *src_cur_offset
>= s
->src_sectors
[*src_cur
]) {
1704 *src_cur_offset
+= s
->src_sectors
[*src_cur
];
1706 assert(*src_cur
< s
->src_num
);
1710 static int convert_iteration_sectors(ImgConvertState
*s
, int64_t sector_num
)
1712 int64_t src_cur_offset
;
1713 int ret
, n
, src_cur
;
1714 bool post_backing_zero
= false;
1716 convert_select_part(s
, sector_num
, &src_cur
, &src_cur_offset
);
1718 assert(s
->total_sectors
> sector_num
);
1719 n
= MIN(s
->total_sectors
- sector_num
, BDRV_REQUEST_MAX_SECTORS
);
1721 if (s
->target_backing_sectors
>= 0) {
1722 if (sector_num
>= s
->target_backing_sectors
) {
1723 post_backing_zero
= true;
1724 } else if (sector_num
+ n
> s
->target_backing_sectors
) {
1725 /* Split requests around target_backing_sectors (because
1726 * starting from there, zeros are handled differently) */
1727 n
= s
->target_backing_sectors
- sector_num
;
1731 if (s
->sector_next_status
<= sector_num
) {
1732 uint64_t offset
= (sector_num
- src_cur_offset
) * BDRV_SECTOR_SIZE
;
1736 count
= n
* BDRV_SECTOR_SIZE
;
1738 if (s
->target_has_backing
) {
1739 ret
= bdrv_block_status(blk_bs(s
->src
[src_cur
]), offset
,
1740 count
, &count
, NULL
, NULL
);
1742 ret
= bdrv_block_status_above(blk_bs(s
->src
[src_cur
]), NULL
,
1743 offset
, count
, &count
, NULL
,
1751 warn_report("error while reading block status at "
1752 "offset %" PRIu64
": %s", offset
,
1755 /* Just try to read the data, then */
1756 ret
= BDRV_BLOCK_DATA
;
1757 count
= BDRV_SECTOR_SIZE
;
1759 /* Retry on a shorter range */
1760 n
= DIV_ROUND_UP(n
, 4);
1763 error_report("error while reading block status at offset "
1764 "%" PRIu64
": %s", offset
, strerror(-ret
));
1770 n
= DIV_ROUND_UP(count
, BDRV_SECTOR_SIZE
);
1772 if (ret
& BDRV_BLOCK_ZERO
) {
1773 s
->status
= post_backing_zero
? BLK_BACKING_FILE
: BLK_ZERO
;
1774 } else if (ret
& BDRV_BLOCK_DATA
) {
1775 s
->status
= BLK_DATA
;
1777 s
->status
= s
->target_has_backing
? BLK_BACKING_FILE
: BLK_DATA
;
1780 s
->sector_next_status
= sector_num
+ n
;
1783 n
= MIN(n
, s
->sector_next_status
- sector_num
);
1784 if (s
->status
== BLK_DATA
) {
1785 n
= MIN(n
, s
->buf_sectors
);
1788 /* We need to write complete clusters for compressed images, so if an
1789 * unallocated area is shorter than that, we must consider the whole
1790 * cluster allocated. */
1791 if (s
->compressed
) {
1792 if (n
< s
->cluster_sectors
) {
1793 n
= MIN(s
->cluster_sectors
, s
->total_sectors
- sector_num
);
1794 s
->status
= BLK_DATA
;
1796 n
= QEMU_ALIGN_DOWN(n
, s
->cluster_sectors
);
1803 static int coroutine_fn
convert_co_read(ImgConvertState
*s
, int64_t sector_num
,
1804 int nb_sectors
, uint8_t *buf
)
1806 uint64_t single_read_until
= 0;
1809 assert(nb_sectors
<= s
->buf_sectors
);
1810 while (nb_sectors
> 0) {
1813 int64_t bs_sectors
, src_cur_offset
;
1816 /* In the case of compression with multiple source files, we can get a
1817 * nb_sectors that spreads into the next part. So we must be able to
1818 * read across multiple BDSes for one convert_read() call. */
1819 convert_select_part(s
, sector_num
, &src_cur
, &src_cur_offset
);
1820 blk
= s
->src
[src_cur
];
1821 bs_sectors
= s
->src_sectors
[src_cur
];
1823 offset
= (sector_num
- src_cur_offset
) << BDRV_SECTOR_BITS
;
1825 n
= MIN(nb_sectors
, bs_sectors
- (sector_num
- src_cur_offset
));
1826 if (single_read_until
> offset
) {
1830 ret
= blk_co_pread(blk
, offset
, n
<< BDRV_SECTOR_BITS
, buf
, 0);
1834 single_read_until
= offset
+ (n
<< BDRV_SECTOR_BITS
);
1838 warn_report("error while reading offset %" PRIu64
1839 ": %s", offset
, strerror(-ret
));
1841 memset(buf
, 0, BDRV_SECTOR_SIZE
);
1850 buf
+= n
* BDRV_SECTOR_SIZE
;
1857 static int coroutine_fn
convert_co_write(ImgConvertState
*s
, int64_t sector_num
,
1858 int nb_sectors
, uint8_t *buf
,
1859 enum ImgConvertBlockStatus status
)
1863 while (nb_sectors
> 0) {
1865 BdrvRequestFlags flags
= s
->compressed
? BDRV_REQ_WRITE_COMPRESSED
: 0;
1868 case BLK_BACKING_FILE
:
1869 /* If we have a backing file, leave clusters unallocated that are
1870 * unallocated in the source image, so that the backing file is
1871 * visible at the respective offset. */
1872 assert(s
->target_has_backing
);
1876 /* If we're told to keep the target fully allocated (-S 0) or there
1877 * is real non-zero data, we must write it. Otherwise we can treat
1878 * it as zero sectors.
1879 * Compressed clusters need to be written as a whole, so in that
1880 * case we can only save the write if the buffer is completely
1882 if (!s
->min_sparse
||
1884 is_allocated_sectors_min(buf
, n
, &n
, s
->min_sparse
,
1885 sector_num
, s
->alignment
)) ||
1887 !buffer_is_zero(buf
, n
* BDRV_SECTOR_SIZE
)))
1889 ret
= blk_co_pwrite(s
->target
, sector_num
<< BDRV_SECTOR_BITS
,
1890 n
<< BDRV_SECTOR_BITS
, buf
, flags
);
1899 if (s
->has_zero_init
) {
1900 assert(!s
->target_has_backing
);
1903 ret
= blk_co_pwrite_zeroes(s
->target
,
1904 sector_num
<< BDRV_SECTOR_BITS
,
1905 n
<< BDRV_SECTOR_BITS
,
1906 BDRV_REQ_MAY_UNMAP
);
1915 buf
+= n
* BDRV_SECTOR_SIZE
;
1921 static int coroutine_fn
convert_co_copy_range(ImgConvertState
*s
, int64_t sector_num
,
1926 while (nb_sectors
> 0) {
1929 int64_t bs_sectors
, src_cur_offset
;
1932 convert_select_part(s
, sector_num
, &src_cur
, &src_cur_offset
);
1933 offset
= (sector_num
- src_cur_offset
) << BDRV_SECTOR_BITS
;
1934 blk
= s
->src
[src_cur
];
1935 bs_sectors
= s
->src_sectors
[src_cur
];
1937 n
= MIN(nb_sectors
, bs_sectors
- (sector_num
- src_cur_offset
));
1939 ret
= blk_co_copy_range(blk
, offset
, s
->target
,
1940 sector_num
<< BDRV_SECTOR_BITS
,
1941 n
<< BDRV_SECTOR_BITS
, 0, 0);
1952 static void coroutine_fn
convert_co_do_copy(void *opaque
)
1954 ImgConvertState
*s
= opaque
;
1955 uint8_t *buf
= NULL
;
1959 for (i
= 0; i
< s
->num_coroutines
; i
++) {
1960 if (s
->co
[i
] == qemu_coroutine_self()) {
1967 s
->running_coroutines
++;
1968 buf
= blk_blockalign(s
->target
, s
->buf_sectors
* BDRV_SECTOR_SIZE
);
1973 enum ImgConvertBlockStatus status
;
1976 qemu_co_mutex_lock(&s
->lock
);
1977 if (s
->ret
!= -EINPROGRESS
|| s
->sector_num
>= s
->total_sectors
) {
1978 qemu_co_mutex_unlock(&s
->lock
);
1981 n
= convert_iteration_sectors(s
, s
->sector_num
);
1983 qemu_co_mutex_unlock(&s
->lock
);
1987 /* save current sector and allocation status to local variables */
1988 sector_num
= s
->sector_num
;
1990 if (!s
->min_sparse
&& s
->status
== BLK_ZERO
) {
1991 n
= MIN(n
, s
->buf_sectors
);
1993 /* increment global sector counter so that other coroutines can
1994 * already continue reading beyond this request */
1996 qemu_co_mutex_unlock(&s
->lock
);
1998 if (status
== BLK_DATA
|| (!s
->min_sparse
&& status
== BLK_ZERO
)) {
1999 s
->allocated_done
+= n
;
2000 qemu_progress_print(100.0 * s
->allocated_done
/
2001 s
->allocated_sectors
, 0);
2005 copy_range
= s
->copy_range
&& s
->status
== BLK_DATA
;
2006 if (status
== BLK_DATA
&& !copy_range
) {
2007 ret
= convert_co_read(s
, sector_num
, n
, buf
);
2009 error_report("error while reading at byte %lld: %s",
2010 sector_num
* BDRV_SECTOR_SIZE
, strerror(-ret
));
2013 } else if (!s
->min_sparse
&& status
== BLK_ZERO
) {
2015 memset(buf
, 0x00, n
* BDRV_SECTOR_SIZE
);
2018 if (s
->wr_in_order
) {
2019 /* keep writes in order */
2020 while (s
->wr_offs
!= sector_num
&& s
->ret
== -EINPROGRESS
) {
2021 s
->wait_sector_num
[index
] = sector_num
;
2022 qemu_coroutine_yield();
2024 s
->wait_sector_num
[index
] = -1;
2027 if (s
->ret
== -EINPROGRESS
) {
2029 ret
= convert_co_copy_range(s
, sector_num
, n
);
2031 s
->copy_range
= false;
2035 ret
= convert_co_write(s
, sector_num
, n
, buf
, status
);
2038 error_report("error while writing at byte %lld: %s",
2039 sector_num
* BDRV_SECTOR_SIZE
, strerror(-ret
));
2044 if (s
->wr_in_order
) {
2045 /* reenter the coroutine that might have waited
2046 * for this write to complete */
2047 s
->wr_offs
= sector_num
+ n
;
2048 for (i
= 0; i
< s
->num_coroutines
; i
++) {
2049 if (s
->co
[i
] && s
->wait_sector_num
[i
] == s
->wr_offs
) {
2051 * A -> B -> A cannot occur because A has
2052 * s->wait_sector_num[i] == -1 during A -> B. Therefore
2053 * B will never enter A during this time window.
2055 qemu_coroutine_enter(s
->co
[i
]);
2063 s
->co
[index
] = NULL
;
2064 s
->running_coroutines
--;
2065 if (!s
->running_coroutines
&& s
->ret
== -EINPROGRESS
) {
2066 /* the convert job finished successfully */
2071 static int convert_do_copy(ImgConvertState
*s
)
2074 int64_t sector_num
= 0;
2076 /* Check whether we have zero initialisation or can get it efficiently */
2077 if (!s
->has_zero_init
&& s
->target_is_new
&& s
->min_sparse
&&
2078 !s
->target_has_backing
) {
2079 s
->has_zero_init
= bdrv_has_zero_init(blk_bs(s
->target
));
2082 /* Allocate buffer for copied data. For compressed images, only one cluster
2083 * can be copied at a time. */
2084 if (s
->compressed
) {
2085 if (s
->cluster_sectors
<= 0 || s
->cluster_sectors
> s
->buf_sectors
) {
2086 error_report("invalid cluster size");
2089 s
->buf_sectors
= s
->cluster_sectors
;
2092 while (sector_num
< s
->total_sectors
) {
2093 n
= convert_iteration_sectors(s
, sector_num
);
2097 if (s
->status
== BLK_DATA
|| (!s
->min_sparse
&& s
->status
== BLK_ZERO
))
2099 s
->allocated_sectors
+= n
;
2105 s
->sector_next_status
= 0;
2106 s
->ret
= -EINPROGRESS
;
2108 qemu_co_mutex_init(&s
->lock
);
2109 for (i
= 0; i
< s
->num_coroutines
; i
++) {
2110 s
->co
[i
] = qemu_coroutine_create(convert_co_do_copy
, s
);
2111 s
->wait_sector_num
[i
] = -1;
2112 qemu_coroutine_enter(s
->co
[i
]);
2115 while (s
->running_coroutines
) {
2116 main_loop_wait(false);
2119 if (s
->compressed
&& !s
->ret
) {
2120 /* signal EOF to align */
2121 ret
= blk_pwrite_compressed(s
->target
, 0, NULL
, 0);
2130 static int convert_copy_bitmaps(BlockDriverState
*src
, BlockDriverState
*dst
)
2132 BdrvDirtyBitmap
*bm
;
2135 FOR_EACH_DIRTY_BITMAP(src
, bm
) {
2138 if (!bdrv_dirty_bitmap_get_persistence(bm
)) {
2141 name
= bdrv_dirty_bitmap_name(bm
);
2142 qmp_block_dirty_bitmap_add(dst
->node_name
, name
,
2143 true, bdrv_dirty_bitmap_granularity(bm
),
2145 true, !bdrv_dirty_bitmap_enabled(bm
),
2148 error_reportf_err(err
, "Failed to create bitmap %s: ", name
);
2152 do_dirty_bitmap_merge(dst
->node_name
, name
, src
->node_name
, name
,
2155 error_reportf_err(err
, "Failed to populate bitmap %s: ", name
);
2163 #define MAX_BUF_SECTORS 32768
2165 static int img_convert(int argc
, char **argv
)
2167 int c
, bs_i
, flags
, src_flags
= 0;
2168 const char *fmt
= NULL
, *out_fmt
= NULL
, *cache
= "unsafe",
2169 *src_cache
= BDRV_DEFAULT_CACHE
, *out_baseimg
= NULL
,
2170 *out_filename
, *out_baseimg_param
, *snapshot_name
= NULL
;
2171 BlockDriver
*drv
= NULL
, *proto_drv
= NULL
;
2172 BlockDriverInfo bdi
;
2173 BlockDriverState
*out_bs
;
2174 QemuOpts
*opts
= NULL
, *sn_opts
= NULL
;
2175 QemuOptsList
*create_opts
= NULL
;
2176 QDict
*open_opts
= NULL
;
2177 char *options
= NULL
;
2178 Error
*local_err
= NULL
;
2179 bool writethrough
, src_writethrough
, image_opts
= false,
2180 skip_create
= false, progress
= false, tgt_image_opts
= false;
2181 int64_t ret
= -EINVAL
;
2182 bool force_share
= false;
2183 bool explict_min_sparse
= false;
2184 bool bitmaps
= false;
2186 ImgConvertState s
= (ImgConvertState
) {
2187 /* Need at least 4k of zeros for sparse detection */
2189 .copy_range
= false,
2190 .buf_sectors
= IO_BUF_SIZE
/ BDRV_SECTOR_SIZE
,
2191 .wr_in_order
= true,
2192 .num_coroutines
= 8,
2196 static const struct option long_options
[] = {
2197 {"help", no_argument
, 0, 'h'},
2198 {"object", required_argument
, 0, OPTION_OBJECT
},
2199 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
2200 {"force-share", no_argument
, 0, 'U'},
2201 {"target-image-opts", no_argument
, 0, OPTION_TARGET_IMAGE_OPTS
},
2202 {"salvage", no_argument
, 0, OPTION_SALVAGE
},
2203 {"target-is-zero", no_argument
, 0, OPTION_TARGET_IS_ZERO
},
2204 {"bitmaps", no_argument
, 0, OPTION_BITMAPS
},
2207 c
= getopt_long(argc
, argv
, ":hf:O:B:Cco:l:S:pt:T:qnm:WU",
2208 long_options
, NULL
);
2214 missing_argument(argv
[optind
- 1]);
2217 unrecognized_option(argv
[optind
- 1]);
2229 out_baseimg
= optarg
;
2232 s
.copy_range
= true;
2235 s
.compressed
= true;
2238 if (accumulate_options(&options
, optarg
) < 0) {
2243 if (strstart(optarg
, SNAPSHOT_OPT_BASE
, NULL
)) {
2244 sn_opts
= qemu_opts_parse_noisily(&internal_snapshot_opts
,
2247 error_report("Failed in parsing snapshot param '%s'",
2252 snapshot_name
= optarg
;
2259 sval
= cvtnum("buffer size for sparse output", optarg
);
2262 } else if (!QEMU_IS_ALIGNED(sval
, BDRV_SECTOR_SIZE
) ||
2263 sval
/ BDRV_SECTOR_SIZE
> MAX_BUF_SECTORS
) {
2264 error_report("Invalid buffer size for sparse output specified. "
2265 "Valid sizes are multiples of %llu up to %llu. Select "
2266 "0 to disable sparse detection (fully allocates output).",
2267 BDRV_SECTOR_SIZE
, MAX_BUF_SECTORS
* BDRV_SECTOR_SIZE
);
2271 s
.min_sparse
= sval
/ BDRV_SECTOR_SIZE
;
2272 explict_min_sparse
= true;
2291 if (qemu_strtol(optarg
, NULL
, 0, &s
.num_coroutines
) ||
2292 s
.num_coroutines
< 1 || s
.num_coroutines
> MAX_COROUTINES
) {
2293 error_report("Invalid number of coroutines. Allowed number of"
2294 " coroutines is between 1 and %d", MAX_COROUTINES
);
2299 s
.wr_in_order
= false;
2304 case OPTION_OBJECT
: {
2305 QemuOpts
*object_opts
;
2306 object_opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
2313 case OPTION_IMAGE_OPTS
:
2316 case OPTION_SALVAGE
:
2319 case OPTION_TARGET_IMAGE_OPTS
:
2320 tgt_image_opts
= true;
2322 case OPTION_TARGET_IS_ZERO
:
2324 * The user asserting that the target is blank has the
2325 * same effect as the target driver supporting zero
2328 s
.has_zero_init
= true;
2330 case OPTION_BITMAPS
:
2336 if (!out_fmt
&& !tgt_image_opts
) {
2340 if (qemu_opts_foreach(&qemu_object_opts
,
2341 user_creatable_add_opts_foreach
,
2342 qemu_img_object_print_help
, &error_fatal
)) {
2346 if (s
.compressed
&& s
.copy_range
) {
2347 error_report("Cannot enable copy offloading when -c is used");
2351 if (explict_min_sparse
&& s
.copy_range
) {
2352 error_report("Cannot enable copy offloading when -S is used");
2356 if (s
.copy_range
&& s
.salvage
) {
2357 error_report("Cannot use copy offloading in salvaging mode");
2361 if (tgt_image_opts
&& !skip_create
) {
2362 error_report("--target-image-opts requires use of -n flag");
2366 if (skip_create
&& options
) {
2367 warn_report("-o has no effect when skipping image creation");
2368 warn_report("This will become an error in future QEMU versions.");
2371 if (s
.has_zero_init
&& !skip_create
) {
2372 error_report("--target-is-zero requires use of -n flag");
2376 s
.src_num
= argc
- optind
- 1;
2377 out_filename
= s
.src_num
>= 1 ? argv
[argc
- 1] : NULL
;
2379 if (options
&& has_help_option(options
)) {
2381 ret
= print_block_option_help(out_filename
, out_fmt
);
2384 error_report("Option help requires a format be specified");
2389 if (s
.src_num
< 1) {
2390 error_report("Must specify image file name");
2394 /* ret is still -EINVAL until here */
2395 ret
= bdrv_parse_cache_mode(src_cache
, &src_flags
, &src_writethrough
);
2397 error_report("Invalid source cache option: %s", src_cache
);
2401 /* Initialize before goto out */
2405 qemu_progress_init(progress
, 1.0);
2406 qemu_progress_print(0, 100);
2408 s
.src
= g_new0(BlockBackend
*, s
.src_num
);
2409 s
.src_sectors
= g_new(int64_t, s
.src_num
);
2411 for (bs_i
= 0; bs_i
< s
.src_num
; bs_i
++) {
2412 s
.src
[bs_i
] = img_open(image_opts
, argv
[optind
+ bs_i
],
2413 fmt
, src_flags
, src_writethrough
, s
.quiet
,
2419 s
.src_sectors
[bs_i
] = blk_nb_sectors(s
.src
[bs_i
]);
2420 if (s
.src_sectors
[bs_i
] < 0) {
2421 error_report("Could not get size of %s: %s",
2422 argv
[optind
+ bs_i
], strerror(-s
.src_sectors
[bs_i
]));
2426 s
.total_sectors
+= s
.src_sectors
[bs_i
];
2430 bdrv_snapshot_load_tmp(blk_bs(s
.src
[0]),
2431 qemu_opt_get(sn_opts
, SNAPSHOT_OPT_ID
),
2432 qemu_opt_get(sn_opts
, SNAPSHOT_OPT_NAME
),
2434 } else if (snapshot_name
!= NULL
) {
2435 if (s
.src_num
> 1) {
2436 error_report("No support for concatenating multiple snapshot");
2441 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s
.src
[0]), snapshot_name
,
2445 error_reportf_err(local_err
, "Failed to load snapshot: ");
2451 /* Find driver and parse its options */
2452 drv
= bdrv_find_format(out_fmt
);
2454 error_report("Unknown file format '%s'", out_fmt
);
2459 proto_drv
= bdrv_find_protocol(out_filename
, true, &local_err
);
2461 error_report_err(local_err
);
2466 if (!drv
->create_opts
) {
2467 error_report("Format driver '%s' does not support image creation",
2473 if (!proto_drv
->create_opts
) {
2474 error_report("Protocol driver '%s' does not support image creation",
2475 proto_drv
->format_name
);
2480 create_opts
= qemu_opts_append(create_opts
, drv
->create_opts
);
2481 create_opts
= qemu_opts_append(create_opts
, proto_drv
->create_opts
);
2483 opts
= qemu_opts_create(create_opts
, NULL
, 0, &error_abort
);
2485 if (!qemu_opts_do_parse(opts
, options
, NULL
, &local_err
)) {
2486 error_report_err(local_err
);
2492 qemu_opt_set_number(opts
, BLOCK_OPT_SIZE
, s
.total_sectors
* 512,
2494 ret
= add_old_style_options(out_fmt
, opts
, out_baseimg
, NULL
);
2500 /* Get backing file name if -o backing_file was used */
2501 out_baseimg_param
= qemu_opt_get(opts
, BLOCK_OPT_BACKING_FILE
);
2502 if (out_baseimg_param
) {
2503 out_baseimg
= out_baseimg_param
;
2505 s
.target_has_backing
= (bool) out_baseimg
;
2507 if (s
.has_zero_init
&& s
.target_has_backing
) {
2508 error_report("Cannot use --target-is-zero when the destination "
2509 "image has a backing file");
2513 if (s
.src_num
> 1 && out_baseimg
) {
2514 error_report("Having a backing file for the target makes no sense when "
2515 "concatenating multiple input images");
2520 /* Check if compression is supported */
2523 qemu_opt_get_bool(opts
, BLOCK_OPT_ENCRYPT
, false);
2524 const char *encryptfmt
=
2525 qemu_opt_get(opts
, BLOCK_OPT_ENCRYPT_FORMAT
);
2526 const char *preallocation
=
2527 qemu_opt_get(opts
, BLOCK_OPT_PREALLOC
);
2529 if (drv
&& !block_driver_can_compress(drv
)) {
2530 error_report("Compression not supported for this file format");
2535 if (encryption
|| encryptfmt
) {
2536 error_report("Compression and encryption not supported at "
2543 && strcmp(preallocation
, "off"))
2545 error_report("Compression and preallocation not supported at "
2552 /* Determine if bitmaps need copying */
2554 if (s
.src_num
> 1) {
2555 error_report("Copying bitmaps only possible with single source");
2559 if (!bdrv_supports_persistent_dirty_bitmap(blk_bs(s
.src
[0]))) {
2560 error_report("Source lacks bitmap support");
2567 * The later open call will need any decryption secrets, and
2568 * bdrv_create() will purge "opts", so extract them now before
2572 open_opts
= qdict_new();
2573 qemu_opt_foreach(opts
, img_add_key_secrets
, open_opts
, &error_abort
);
2575 /* Create the new image */
2576 ret
= bdrv_create(drv
, out_filename
, opts
, &local_err
);
2578 error_reportf_err(local_err
, "%s: error while converting %s: ",
2579 out_filename
, out_fmt
);
2584 s
.target_is_new
= !skip_create
;
2586 flags
= s
.min_sparse
? (BDRV_O_RDWR
| BDRV_O_UNMAP
) : BDRV_O_RDWR
;
2587 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
2589 error_report("Invalid cache option: %s", cache
);
2594 s
.target
= img_open(tgt_image_opts
, out_filename
, out_fmt
,
2595 flags
, writethrough
, s
.quiet
, false);
2597 /* TODO ultimately we should allow --target-image-opts
2598 * to be used even when -n is not given.
2599 * That has to wait for bdrv_create to be improved
2600 * to allow filenames in option syntax
2602 s
.target
= img_open_file(out_filename
, open_opts
, out_fmt
,
2603 flags
, writethrough
, s
.quiet
, false);
2604 open_opts
= NULL
; /* blk_new_open will have freed it */
2610 out_bs
= blk_bs(s
.target
);
2612 if (bitmaps
&& !bdrv_supports_persistent_dirty_bitmap(out_bs
)) {
2613 error_report("Format driver '%s' does not support bitmaps",
2614 out_bs
->drv
->format_name
);
2619 if (s
.compressed
&& !block_driver_can_compress(out_bs
->drv
)) {
2620 error_report("Compression not supported for this file format");
2625 /* increase bufsectors from the default 4096 (2M) if opt_transfer
2626 * or discard_alignment of the out_bs is greater. Limit to
2627 * MAX_BUF_SECTORS as maximum which is currently 32768 (16MB). */
2628 s
.buf_sectors
= MIN(MAX_BUF_SECTORS
,
2630 MAX(out_bs
->bl
.opt_transfer
>> BDRV_SECTOR_BITS
,
2631 out_bs
->bl
.pdiscard_alignment
>>
2632 BDRV_SECTOR_BITS
)));
2634 /* try to align the write requests to the destination to avoid unnecessary
2636 s
.alignment
= MAX(pow2floor(s
.min_sparse
),
2637 DIV_ROUND_UP(out_bs
->bl
.request_alignment
,
2639 assert(is_power_of_2(s
.alignment
));
2642 int64_t output_sectors
= blk_nb_sectors(s
.target
);
2643 if (output_sectors
< 0) {
2644 error_report("unable to get output image length: %s",
2645 strerror(-output_sectors
));
2648 } else if (output_sectors
< s
.total_sectors
) {
2649 error_report("output file is smaller than input file");
2655 if (s
.target_has_backing
&& s
.target_is_new
) {
2656 /* Errors are treated as "backing length unknown" (which means
2657 * s.target_backing_sectors has to be negative, which it will
2658 * be automatically). The backing file length is used only
2659 * for optimizations, so such a case is not fatal. */
2660 s
.target_backing_sectors
= bdrv_nb_sectors(out_bs
->backing
->bs
);
2662 s
.target_backing_sectors
= -1;
2665 ret
= bdrv_get_info(out_bs
, &bdi
);
2668 error_report("could not get block driver info");
2672 s
.compressed
= s
.compressed
|| bdi
.needs_compressed_writes
;
2673 s
.cluster_sectors
= bdi
.cluster_size
/ BDRV_SECTOR_SIZE
;
2676 ret
= convert_do_copy(&s
);
2678 /* Now copy the bitmaps */
2679 if (bitmaps
&& ret
== 0) {
2680 ret
= convert_copy_bitmaps(blk_bs(s
.src
[0]), out_bs
);
2685 qemu_progress_print(100, 0);
2687 qemu_progress_end();
2688 qemu_opts_del(opts
);
2689 qemu_opts_free(create_opts
);
2690 qemu_opts_del(sn_opts
);
2691 qobject_unref(open_opts
);
2692 blk_unref(s
.target
);
2694 for (bs_i
= 0; bs_i
< s
.src_num
; bs_i
++) {
2695 blk_unref(s
.src
[bs_i
]);
2699 g_free(s
.src_sectors
);
2707 static void dump_snapshots(BlockDriverState
*bs
)
2709 QEMUSnapshotInfo
*sn_tab
, *sn
;
2712 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
2715 printf("Snapshot list:\n");
2716 bdrv_snapshot_dump(NULL
);
2718 for(i
= 0; i
< nb_sns
; i
++) {
2720 bdrv_snapshot_dump(sn
);
2726 static void dump_json_image_info_list(ImageInfoList
*list
)
2730 Visitor
*v
= qobject_output_visitor_new(&obj
);
2732 visit_type_ImageInfoList(v
, NULL
, &list
, &error_abort
);
2733 visit_complete(v
, &obj
);
2734 str
= qobject_to_json_pretty(obj
);
2735 assert(str
!= NULL
);
2736 printf("%s\n", qstring_get_str(str
));
2742 static void dump_json_image_info(ImageInfo
*info
)
2746 Visitor
*v
= qobject_output_visitor_new(&obj
);
2748 visit_type_ImageInfo(v
, NULL
, &info
, &error_abort
);
2749 visit_complete(v
, &obj
);
2750 str
= qobject_to_json_pretty(obj
);
2751 assert(str
!= NULL
);
2752 printf("%s\n", qstring_get_str(str
));
2758 static void dump_human_image_info_list(ImageInfoList
*list
)
2760 ImageInfoList
*elem
;
2763 for (elem
= list
; elem
; elem
= elem
->next
) {
2769 bdrv_image_info_dump(elem
->value
);
2773 static gboolean
str_equal_func(gconstpointer a
, gconstpointer b
)
2775 return strcmp(a
, b
) == 0;
2779 * Open an image file chain and return an ImageInfoList
2781 * @filename: topmost image filename
2782 * @fmt: topmost image format (may be NULL to autodetect)
2783 * @chain: true - enumerate entire backing file chain
2784 * false - only topmost image file
2786 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2787 * image file. If there was an error a message will have been printed to
2790 static ImageInfoList
*collect_image_info_list(bool image_opts
,
2791 const char *filename
,
2793 bool chain
, bool force_share
)
2795 ImageInfoList
*head
= NULL
;
2796 ImageInfoList
**last
= &head
;
2797 GHashTable
*filenames
;
2800 filenames
= g_hash_table_new_full(g_str_hash
, str_equal_func
, NULL
, NULL
);
2804 BlockDriverState
*bs
;
2806 ImageInfoList
*elem
;
2808 if (g_hash_table_lookup_extended(filenames
, filename
, NULL
, NULL
)) {
2809 error_report("Backing file '%s' creates an infinite loop.",
2813 g_hash_table_insert(filenames
, (gpointer
)filename
, NULL
);
2815 blk
= img_open(image_opts
, filename
, fmt
,
2816 BDRV_O_NO_BACKING
| BDRV_O_NO_IO
, false, false,
2823 bdrv_query_image_info(bs
, &info
, &err
);
2825 error_report_err(err
);
2830 elem
= g_new0(ImageInfoList
, 1);
2837 /* Clear parameters that only apply to the topmost image */
2838 filename
= fmt
= NULL
;
2842 if (info
->has_full_backing_filename
) {
2843 filename
= info
->full_backing_filename
;
2844 } else if (info
->has_backing_filename
) {
2845 error_report("Could not determine absolute backing filename,"
2846 " but backing filename '%s' present",
2847 info
->backing_filename
);
2850 if (info
->has_backing_filename_format
) {
2851 fmt
= info
->backing_filename_format
;
2855 g_hash_table_destroy(filenames
);
2859 qapi_free_ImageInfoList(head
);
2860 g_hash_table_destroy(filenames
);
2864 static int img_info(int argc
, char **argv
)
2867 OutputFormat output_format
= OFORMAT_HUMAN
;
2869 const char *filename
, *fmt
, *output
;
2870 ImageInfoList
*list
;
2871 bool image_opts
= false;
2872 bool force_share
= false;
2877 int option_index
= 0;
2878 static const struct option long_options
[] = {
2879 {"help", no_argument
, 0, 'h'},
2880 {"format", required_argument
, 0, 'f'},
2881 {"output", required_argument
, 0, OPTION_OUTPUT
},
2882 {"backing-chain", no_argument
, 0, OPTION_BACKING_CHAIN
},
2883 {"object", required_argument
, 0, OPTION_OBJECT
},
2884 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
2885 {"force-share", no_argument
, 0, 'U'},
2888 c
= getopt_long(argc
, argv
, ":f:hU",
2889 long_options
, &option_index
);
2895 missing_argument(argv
[optind
- 1]);
2898 unrecognized_option(argv
[optind
- 1]);
2912 case OPTION_BACKING_CHAIN
:
2915 case OPTION_OBJECT
: {
2917 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
2923 case OPTION_IMAGE_OPTS
:
2928 if (optind
!= argc
- 1) {
2929 error_exit("Expecting one image file name");
2931 filename
= argv
[optind
++];
2933 if (output
&& !strcmp(output
, "json")) {
2934 output_format
= OFORMAT_JSON
;
2935 } else if (output
&& !strcmp(output
, "human")) {
2936 output_format
= OFORMAT_HUMAN
;
2937 } else if (output
) {
2938 error_report("--output must be used with human or json as argument.");
2942 if (qemu_opts_foreach(&qemu_object_opts
,
2943 user_creatable_add_opts_foreach
,
2944 qemu_img_object_print_help
, &error_fatal
)) {
2948 list
= collect_image_info_list(image_opts
, filename
, fmt
, chain
,
2954 switch (output_format
) {
2956 dump_human_image_info_list(list
);
2960 dump_json_image_info_list(list
);
2962 dump_json_image_info(list
->value
);
2967 qapi_free_ImageInfoList(list
);
2971 static int dump_map_entry(OutputFormat output_format
, MapEntry
*e
,
2974 switch (output_format
) {
2976 if (e
->data
&& !e
->has_offset
) {
2977 error_report("File contains external, encrypted or compressed clusters.");
2980 if (e
->data
&& !e
->zero
) {
2981 printf("%#-16"PRIx64
"%#-16"PRIx64
"%#-16"PRIx64
"%s\n",
2982 e
->start
, e
->length
,
2983 e
->has_offset
? e
->offset
: 0,
2984 e
->has_filename
? e
->filename
: "");
2986 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2987 * Modify the flags here to allow more coalescing.
2989 if (next
&& (!next
->data
|| next
->zero
)) {
2995 printf("{ \"start\": %"PRId64
", \"length\": %"PRId64
","
2996 " \"depth\": %"PRId64
", \"zero\": %s, \"data\": %s",
2997 e
->start
, e
->length
, e
->depth
,
2998 e
->zero
? "true" : "false",
2999 e
->data
? "true" : "false");
3000 if (e
->has_offset
) {
3001 printf(", \"offset\": %"PRId64
"", e
->offset
);
3013 static int get_block_status(BlockDriverState
*bs
, int64_t offset
,
3014 int64_t bytes
, MapEntry
*e
)
3018 BlockDriverState
*file
;
3021 char *filename
= NULL
;
3023 /* As an optimization, we could cache the current range of unallocated
3024 * clusters in each file of the chain, and avoid querying the same
3030 ret
= bdrv_block_status(bs
, offset
, bytes
, &bytes
, &map
, &file
);
3035 if (ret
& (BDRV_BLOCK_ZERO
|BDRV_BLOCK_DATA
)) {
3038 bs
= backing_bs(bs
);
3047 has_offset
= !!(ret
& BDRV_BLOCK_OFFSET_VALID
);
3049 if (file
&& has_offset
) {
3050 bdrv_refresh_filename(file
);
3051 filename
= file
->filename
;
3057 .data
= !!(ret
& BDRV_BLOCK_DATA
),
3058 .zero
= !!(ret
& BDRV_BLOCK_ZERO
),
3060 .has_offset
= has_offset
,
3062 .has_filename
= filename
,
3063 .filename
= filename
,
3069 static inline bool entry_mergeable(const MapEntry
*curr
, const MapEntry
*next
)
3071 if (curr
->length
== 0) {
3074 if (curr
->zero
!= next
->zero
||
3075 curr
->data
!= next
->data
||
3076 curr
->depth
!= next
->depth
||
3077 curr
->has_filename
!= next
->has_filename
||
3078 curr
->has_offset
!= next
->has_offset
) {
3081 if (curr
->has_filename
&& strcmp(curr
->filename
, next
->filename
)) {
3084 if (curr
->has_offset
&& curr
->offset
+ curr
->length
!= next
->offset
) {
3090 static int img_map(int argc
, char **argv
)
3093 OutputFormat output_format
= OFORMAT_HUMAN
;
3095 BlockDriverState
*bs
;
3096 const char *filename
, *fmt
, *output
;
3098 MapEntry curr
= { .length
= 0 }, next
;
3100 bool image_opts
= false;
3101 bool force_share
= false;
3102 int64_t start_offset
= 0;
3103 int64_t max_length
= -1;
3108 int option_index
= 0;
3109 static const struct option long_options
[] = {
3110 {"help", no_argument
, 0, 'h'},
3111 {"format", required_argument
, 0, 'f'},
3112 {"output", required_argument
, 0, OPTION_OUTPUT
},
3113 {"object", required_argument
, 0, OPTION_OBJECT
},
3114 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
3115 {"force-share", no_argument
, 0, 'U'},
3116 {"start-offset", required_argument
, 0, 's'},
3117 {"max-length", required_argument
, 0, 'l'},
3120 c
= getopt_long(argc
, argv
, ":f:s:l:hU",
3121 long_options
, &option_index
);
3127 missing_argument(argv
[optind
- 1]);
3130 unrecognized_option(argv
[optind
- 1]);
3145 start_offset
= cvtnum("start offset", optarg
);
3146 if (start_offset
< 0) {
3151 max_length
= cvtnum("max length", optarg
);
3152 if (max_length
< 0) {
3156 case OPTION_OBJECT
: {
3158 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
3164 case OPTION_IMAGE_OPTS
:
3169 if (optind
!= argc
- 1) {
3170 error_exit("Expecting one image file name");
3172 filename
= argv
[optind
];
3174 if (output
&& !strcmp(output
, "json")) {
3175 output_format
= OFORMAT_JSON
;
3176 } else if (output
&& !strcmp(output
, "human")) {
3177 output_format
= OFORMAT_HUMAN
;
3178 } else if (output
) {
3179 error_report("--output must be used with human or json as argument.");
3183 if (qemu_opts_foreach(&qemu_object_opts
,
3184 user_creatable_add_opts_foreach
,
3185 qemu_img_object_print_help
, &error_fatal
)) {
3189 blk
= img_open(image_opts
, filename
, fmt
, 0, false, false, force_share
);
3195 if (output_format
== OFORMAT_HUMAN
) {
3196 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
3197 } else if (output_format
== OFORMAT_JSON
) {
3201 length
= blk_getlength(blk
);
3203 error_report("Failed to get size for '%s'", filename
);
3206 if (max_length
!= -1) {
3207 length
= MIN(start_offset
+ max_length
, length
);
3210 curr
.start
= start_offset
;
3211 while (curr
.start
+ curr
.length
< length
) {
3212 int64_t offset
= curr
.start
+ curr
.length
;
3215 /* Probe up to 1 GiB at a time. */
3216 n
= MIN(1 * GiB
, length
- offset
);
3217 ret
= get_block_status(bs
, offset
, n
, &next
);
3220 error_report("Could not read file metadata: %s", strerror(-ret
));
3224 if (entry_mergeable(&curr
, &next
)) {
3225 curr
.length
+= next
.length
;
3229 if (curr
.length
> 0) {
3230 ret
= dump_map_entry(output_format
, &curr
, &next
);
3238 ret
= dump_map_entry(output_format
, &curr
, NULL
);
3239 if (output_format
== OFORMAT_JSON
) {
3248 #define SNAPSHOT_LIST 1
3249 #define SNAPSHOT_CREATE 2
3250 #define SNAPSHOT_APPLY 3
3251 #define SNAPSHOT_DELETE 4
3253 static int img_snapshot(int argc
, char **argv
)
3256 BlockDriverState
*bs
;
3257 QEMUSnapshotInfo sn
;
3258 char *filename
, *snapshot_name
= NULL
;
3259 int c
, ret
= 0, bdrv_oflags
;
3264 bool image_opts
= false;
3265 bool force_share
= false;
3267 bdrv_oflags
= BDRV_O_RDWR
;
3268 /* Parse commandline parameters */
3270 static const struct option long_options
[] = {
3271 {"help", no_argument
, 0, 'h'},
3272 {"object", required_argument
, 0, OPTION_OBJECT
},
3273 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
3274 {"force-share", no_argument
, 0, 'U'},
3277 c
= getopt_long(argc
, argv
, ":la:c:d:hqU",
3278 long_options
, NULL
);
3284 missing_argument(argv
[optind
- 1]);
3287 unrecognized_option(argv
[optind
- 1]);
3294 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
3297 action
= SNAPSHOT_LIST
;
3298 bdrv_oflags
&= ~BDRV_O_RDWR
; /* no need for RW */
3302 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
3305 action
= SNAPSHOT_APPLY
;
3306 snapshot_name
= optarg
;
3310 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
3313 action
= SNAPSHOT_CREATE
;
3314 snapshot_name
= optarg
;
3318 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
3321 action
= SNAPSHOT_DELETE
;
3322 snapshot_name
= optarg
;
3330 case OPTION_OBJECT
: {
3332 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
3338 case OPTION_IMAGE_OPTS
:
3344 if (optind
!= argc
- 1) {
3345 error_exit("Expecting one image file name");
3347 filename
= argv
[optind
++];
3349 if (qemu_opts_foreach(&qemu_object_opts
,
3350 user_creatable_add_opts_foreach
,
3351 qemu_img_object_print_help
, &error_fatal
)) {
3355 /* Open the image */
3356 blk
= img_open(image_opts
, filename
, NULL
, bdrv_oflags
, false, quiet
,
3363 /* Perform the requested action */
3369 case SNAPSHOT_CREATE
:
3370 memset(&sn
, 0, sizeof(sn
));
3371 pstrcpy(sn
.name
, sizeof(sn
.name
), snapshot_name
);
3373 qemu_gettimeofday(&tv
);
3374 sn
.date_sec
= tv
.tv_sec
;
3375 sn
.date_nsec
= tv
.tv_usec
* 1000;
3377 ret
= bdrv_snapshot_create(bs
, &sn
);
3379 error_report("Could not create snapshot '%s': %d (%s)",
3380 snapshot_name
, ret
, strerror(-ret
));
3384 case SNAPSHOT_APPLY
:
3385 ret
= bdrv_snapshot_goto(bs
, snapshot_name
, &err
);
3387 error_reportf_err(err
, "Could not apply snapshot '%s': ",
3392 case SNAPSHOT_DELETE
:
3393 ret
= bdrv_snapshot_find(bs
, &sn
, snapshot_name
);
3395 error_report("Could not delete snapshot '%s': snapshot not "
3396 "found", snapshot_name
);
3399 ret
= bdrv_snapshot_delete(bs
, sn
.id_str
, sn
.name
, &err
);
3401 error_reportf_err(err
, "Could not delete snapshot '%s': ",
3417 static int img_rebase(int argc
, char **argv
)
3419 BlockBackend
*blk
= NULL
, *blk_old_backing
= NULL
, *blk_new_backing
= NULL
;
3420 uint8_t *buf_old
= NULL
;
3421 uint8_t *buf_new
= NULL
;
3422 BlockDriverState
*bs
= NULL
, *prefix_chain_bs
= NULL
;
3424 const char *fmt
, *cache
, *src_cache
, *out_basefmt
, *out_baseimg
;
3425 int c
, flags
, src_flags
, ret
;
3426 bool writethrough
, src_writethrough
;
3428 bool force_share
= false;
3431 Error
*local_err
= NULL
;
3432 bool image_opts
= false;
3434 /* Parse commandline parameters */
3436 cache
= BDRV_DEFAULT_CACHE
;
3437 src_cache
= BDRV_DEFAULT_CACHE
;
3441 static const struct option long_options
[] = {
3442 {"help", no_argument
, 0, 'h'},
3443 {"object", required_argument
, 0, OPTION_OBJECT
},
3444 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
3445 {"force-share", no_argument
, 0, 'U'},
3448 c
= getopt_long(argc
, argv
, ":hf:F:b:upt:T:qU",
3449 long_options
, NULL
);
3455 missing_argument(argv
[optind
- 1]);
3458 unrecognized_option(argv
[optind
- 1]);
3467 out_basefmt
= optarg
;
3470 out_baseimg
= optarg
;
3487 case OPTION_OBJECT
: {
3489 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
3495 case OPTION_IMAGE_OPTS
:
3508 if (optind
!= argc
- 1) {
3509 error_exit("Expecting one image file name");
3511 if (!unsafe
&& !out_baseimg
) {
3512 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
3514 filename
= argv
[optind
++];
3516 if (qemu_opts_foreach(&qemu_object_opts
,
3517 user_creatable_add_opts_foreach
,
3518 qemu_img_object_print_help
, &error_fatal
)) {
3522 qemu_progress_init(progress
, 2.0);
3523 qemu_progress_print(0, 100);
3525 flags
= BDRV_O_RDWR
| (unsafe
? BDRV_O_NO_BACKING
: 0);
3526 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
3528 error_report("Invalid cache option: %s", cache
);
3533 ret
= bdrv_parse_cache_mode(src_cache
, &src_flags
, &src_writethrough
);
3535 error_report("Invalid source cache option: %s", src_cache
);
3539 /* The source files are opened read-only, don't care about WCE */
3540 assert((src_flags
& BDRV_O_RDWR
) == 0);
3541 (void) src_writethrough
;
3546 * Ignore the old backing file for unsafe rebase in case we want to correct
3547 * the reference to a renamed or moved backing file.
3549 blk
= img_open(image_opts
, filename
, fmt
, flags
, writethrough
, quiet
,
3557 if (out_basefmt
!= NULL
) {
3558 if (bdrv_find_format(out_basefmt
) == NULL
) {
3559 error_report("Invalid format name: '%s'", out_basefmt
);
3565 /* For safe rebasing we need to compare old and new backing file */
3567 QDict
*options
= NULL
;
3568 BlockDriverState
*base_bs
= backing_bs(bs
);
3571 blk_old_backing
= blk_new(qemu_get_aio_context(),
3572 BLK_PERM_CONSISTENT_READ
,
3574 ret
= blk_insert_bs(blk_old_backing
, base_bs
,
3577 error_reportf_err(local_err
,
3578 "Could not reuse old backing file '%s': ",
3583 blk_old_backing
= NULL
;
3586 if (out_baseimg
[0]) {
3587 const char *overlay_filename
;
3588 char *out_real_path
;
3590 options
= qdict_new();
3592 qdict_put_str(options
, "driver", out_basefmt
);
3595 qdict_put_bool(options
, BDRV_OPT_FORCE_SHARE
, true);
3598 bdrv_refresh_filename(bs
);
3599 overlay_filename
= bs
->exact_filename
[0] ? bs
->exact_filename
3602 bdrv_get_full_backing_filename_from_filename(overlay_filename
,
3606 qobject_unref(options
);
3607 error_reportf_err(local_err
,
3608 "Could not resolve backing filename: ");
3614 * Find out whether we rebase an image on top of a previous image
3617 prefix_chain_bs
= bdrv_find_backing_image(bs
, out_real_path
);
3618 if (prefix_chain_bs
) {
3619 qobject_unref(options
);
3620 g_free(out_real_path
);
3622 blk_new_backing
= blk_new(qemu_get_aio_context(),
3623 BLK_PERM_CONSISTENT_READ
,
3625 ret
= blk_insert_bs(blk_new_backing
, prefix_chain_bs
,
3628 error_reportf_err(local_err
,
3629 "Could not reuse backing file '%s': ",
3634 blk_new_backing
= blk_new_open(out_real_path
, NULL
,
3635 options
, src_flags
, &local_err
);
3636 g_free(out_real_path
);
3637 if (!blk_new_backing
) {
3638 error_reportf_err(local_err
,
3639 "Could not open new backing file '%s': ",
3649 * Check each unallocated cluster in the COW file. If it is unallocated,
3650 * accesses go to the backing file. We must therefore compare this cluster
3651 * in the old and new backing file, and if they differ we need to copy it
3652 * from the old backing file into the COW file.
3654 * If qemu-img crashes during this step, no harm is done. The content of
3655 * the image is the same as the original one at any time.
3659 int64_t old_backing_size
= 0;
3660 int64_t new_backing_size
= 0;
3663 float local_progress
= 0;
3665 buf_old
= blk_blockalign(blk
, IO_BUF_SIZE
);
3666 buf_new
= blk_blockalign(blk
, IO_BUF_SIZE
);
3668 size
= blk_getlength(blk
);
3670 error_report("Could not get size of '%s': %s",
3671 filename
, strerror(-size
));
3675 if (blk_old_backing
) {
3676 old_backing_size
= blk_getlength(blk_old_backing
);
3677 if (old_backing_size
< 0) {
3678 char backing_name
[PATH_MAX
];
3680 bdrv_get_backing_filename(bs
, backing_name
,
3681 sizeof(backing_name
));
3682 error_report("Could not get size of '%s': %s",
3683 backing_name
, strerror(-old_backing_size
));
3688 if (blk_new_backing
) {
3689 new_backing_size
= blk_getlength(blk_new_backing
);
3690 if (new_backing_size
< 0) {
3691 error_report("Could not get size of '%s': %s",
3692 out_baseimg
, strerror(-new_backing_size
));
3699 local_progress
= (float)100 / (size
/ MIN(size
, IO_BUF_SIZE
));
3702 for (offset
= 0; offset
< size
; offset
+= n
) {
3703 bool buf_old_is_zero
= false;
3705 /* How many bytes can we handle with the next read? */
3706 n
= MIN(IO_BUF_SIZE
, size
- offset
);
3708 /* If the cluster is allocated, we don't need to take action */
3709 ret
= bdrv_is_allocated(bs
, offset
, n
, &n
);
3711 error_report("error while reading image metadata: %s",
3719 if (prefix_chain_bs
) {
3721 * If cluster wasn't changed since prefix_chain, we don't need
3724 ret
= bdrv_is_allocated_above(backing_bs(bs
), prefix_chain_bs
,
3725 false, offset
, n
, &n
);
3727 error_report("error while reading image metadata: %s",
3737 * Read old and new backing file and take into consideration that
3738 * backing files may be smaller than the COW image.
3740 if (offset
>= old_backing_size
) {
3741 memset(buf_old
, 0, n
);
3742 buf_old_is_zero
= true;
3744 if (offset
+ n
> old_backing_size
) {
3745 n
= old_backing_size
- offset
;
3748 ret
= blk_pread(blk_old_backing
, offset
, buf_old
, n
);
3750 error_report("error while reading from old backing file");
3755 if (offset
>= new_backing_size
|| !blk_new_backing
) {
3756 memset(buf_new
, 0, n
);
3758 if (offset
+ n
> new_backing_size
) {
3759 n
= new_backing_size
- offset
;
3762 ret
= blk_pread(blk_new_backing
, offset
, buf_new
, n
);
3764 error_report("error while reading from new backing file");
3769 /* If they differ, we need to write to the COW file */
3770 uint64_t written
= 0;
3772 while (written
< n
) {
3775 if (compare_buffers(buf_old
+ written
, buf_new
+ written
,
3776 n
- written
, &pnum
))
3778 if (buf_old_is_zero
) {
3779 ret
= blk_pwrite_zeroes(blk
, offset
+ written
, pnum
, 0);
3781 ret
= blk_pwrite(blk
, offset
+ written
,
3782 buf_old
+ written
, pnum
, 0);
3785 error_report("Error while writing to COW image: %s",
3793 qemu_progress_print(local_progress
, 100);
3798 * Change the backing file. All clusters that are different from the old
3799 * backing file are overwritten in the COW file now, so the visible content
3800 * doesn't change when we switch the backing file.
3802 if (out_baseimg
&& *out_baseimg
) {
3803 ret
= bdrv_change_backing_file(bs
, out_baseimg
, out_basefmt
);
3805 ret
= bdrv_change_backing_file(bs
, NULL
, NULL
);
3808 if (ret
== -ENOSPC
) {
3809 error_report("Could not change the backing file to '%s': No "
3810 "space left in the file header", out_baseimg
);
3811 } else if (ret
< 0) {
3812 error_report("Could not change the backing file to '%s': %s",
3813 out_baseimg
, strerror(-ret
));
3816 qemu_progress_print(100, 0);
3818 * TODO At this point it is possible to check if any clusters that are
3819 * allocated in the COW file are the same in the backing file. If so, they
3820 * could be dropped from the COW file. Don't do this before switching the
3821 * backing file, in case of a crash this would lead to corruption.
3824 qemu_progress_end();
3827 blk_unref(blk_old_backing
);
3828 blk_unref(blk_new_backing
);
3830 qemu_vfree(buf_old
);
3831 qemu_vfree(buf_new
);
3840 static int img_resize(int argc
, char **argv
)
3843 int c
, ret
, relative
;
3844 const char *filename
, *fmt
, *size
;
3845 int64_t n
, total_size
, current_size
;
3847 BlockBackend
*blk
= NULL
;
3848 PreallocMode prealloc
= PREALLOC_MODE_OFF
;
3851 static QemuOptsList resize_options
= {
3852 .name
= "resize_options",
3853 .head
= QTAILQ_HEAD_INITIALIZER(resize_options
.head
),
3856 .name
= BLOCK_OPT_SIZE
,
3857 .type
= QEMU_OPT_SIZE
,
3858 .help
= "Virtual disk size"
3864 bool image_opts
= false;
3865 bool shrink
= false;
3867 /* Remove size from argv manually so that negative numbers are not treated
3868 * as options by getopt. */
3870 error_exit("Not enough arguments");
3874 size
= argv
[--argc
];
3876 /* Parse getopt arguments */
3879 static const struct option long_options
[] = {
3880 {"help", no_argument
, 0, 'h'},
3881 {"object", required_argument
, 0, OPTION_OBJECT
},
3882 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
3883 {"preallocation", required_argument
, 0, OPTION_PREALLOCATION
},
3884 {"shrink", no_argument
, 0, OPTION_SHRINK
},
3887 c
= getopt_long(argc
, argv
, ":f:hq",
3888 long_options
, NULL
);
3894 missing_argument(argv
[optind
- 1]);
3897 unrecognized_option(argv
[optind
- 1]);
3908 case OPTION_OBJECT
: {
3910 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
3916 case OPTION_IMAGE_OPTS
:
3919 case OPTION_PREALLOCATION
:
3920 prealloc
= qapi_enum_parse(&PreallocMode_lookup
, optarg
,
3921 PREALLOC_MODE__MAX
, NULL
);
3922 if (prealloc
== PREALLOC_MODE__MAX
) {
3923 error_report("Invalid preallocation mode '%s'", optarg
);
3932 if (optind
!= argc
- 1) {
3933 error_exit("Expecting image file name and size");
3935 filename
= argv
[optind
++];
3937 if (qemu_opts_foreach(&qemu_object_opts
,
3938 user_creatable_add_opts_foreach
,
3939 qemu_img_object_print_help
, &error_fatal
)) {
3943 /* Choose grow, shrink, or absolute resize mode */
3959 param
= qemu_opts_create(&resize_options
, NULL
, 0, &error_abort
);
3960 if (!qemu_opt_set(param
, BLOCK_OPT_SIZE
, size
, &err
)) {
3961 error_report_err(err
);
3963 qemu_opts_del(param
);
3966 n
= qemu_opt_get_size(param
, BLOCK_OPT_SIZE
, 0);
3967 qemu_opts_del(param
);
3969 blk
= img_open(image_opts
, filename
, fmt
,
3970 BDRV_O_RDWR
| BDRV_O_RESIZE
, false, quiet
,
3977 current_size
= blk_getlength(blk
);
3978 if (current_size
< 0) {
3979 error_report("Failed to inquire current image length: %s",
3980 strerror(-current_size
));
3986 total_size
= current_size
+ n
* relative
;
3990 if (total_size
<= 0) {
3991 error_report("New image size must be positive");
3996 if (total_size
<= current_size
&& prealloc
!= PREALLOC_MODE_OFF
) {
3997 error_report("Preallocation can only be used for growing images");
4002 if (total_size
< current_size
&& !shrink
) {
4003 warn_report("Shrinking an image will delete all data beyond the "
4004 "shrunken image's end. Before performing such an "
4005 "operation, make sure there is no important data there.");
4007 if (g_strcmp0(bdrv_get_format_name(blk_bs(blk
)), "raw") != 0) {
4009 "Use the --shrink option to perform a shrink operation.");
4013 warn_report("Using the --shrink option will suppress this message. "
4014 "Note that future versions of qemu-img may refuse to "
4015 "shrink images without this option.");
4020 * The user expects the image to have the desired size after
4021 * resizing, so pass @exact=true. It is of no use to report
4022 * success when the image has not actually been resized.
4024 ret
= blk_truncate(blk
, total_size
, true, prealloc
, 0, &err
);
4026 qprintf(quiet
, "Image resized.\n");
4028 error_report_err(err
);
4038 static void amend_status_cb(BlockDriverState
*bs
,
4039 int64_t offset
, int64_t total_work_size
,
4042 qemu_progress_print(100.f
* offset
/ total_work_size
, 0);
4045 static int print_amend_option_help(const char *format
)
4049 /* Find driver and parse its options */
4050 drv
= bdrv_find_format(format
);
4052 error_report("Unknown file format '%s'", format
);
4056 if (!drv
->bdrv_amend_options
) {
4057 error_report("Format driver '%s' does not support option amendment",
4062 /* Every driver supporting amendment must have amend_opts */
4063 assert(drv
->amend_opts
);
4065 printf("Amend options for '%s':\n", format
);
4066 qemu_opts_print_help(drv
->amend_opts
, false);
4070 static int img_amend(int argc
, char **argv
)
4074 char *options
= NULL
;
4075 QemuOptsList
*amend_opts
= NULL
;
4076 QemuOpts
*opts
= NULL
;
4077 const char *fmt
= NULL
, *filename
, *cache
;
4080 bool quiet
= false, progress
= false;
4081 BlockBackend
*blk
= NULL
;
4082 BlockDriverState
*bs
= NULL
;
4083 bool image_opts
= false;
4086 cache
= BDRV_DEFAULT_CACHE
;
4088 static const struct option long_options
[] = {
4089 {"help", no_argument
, 0, 'h'},
4090 {"object", required_argument
, 0, OPTION_OBJECT
},
4091 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
4092 {"force", no_argument
, 0, OPTION_FORCE
},
4095 c
= getopt_long(argc
, argv
, ":ho:f:t:pq",
4096 long_options
, NULL
);
4103 missing_argument(argv
[optind
- 1]);
4106 unrecognized_option(argv
[optind
- 1]);
4112 if (accumulate_options(&options
, optarg
) < 0) {
4114 goto out_no_progress
;
4130 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
4134 goto out_no_progress
;
4137 case OPTION_IMAGE_OPTS
:
4147 error_exit("Must specify options (-o)");
4150 if (qemu_opts_foreach(&qemu_object_opts
,
4151 user_creatable_add_opts_foreach
,
4152 qemu_img_object_print_help
, &error_fatal
)) {
4154 goto out_no_progress
;
4160 qemu_progress_init(progress
, 1.0);
4162 filename
= (optind
== argc
- 1) ? argv
[argc
- 1] : NULL
;
4163 if (fmt
&& has_help_option(options
)) {
4164 /* If a format is explicitly specified (and possibly no filename is
4165 * given), print option help here */
4166 ret
= print_amend_option_help(fmt
);
4170 if (optind
!= argc
- 1) {
4171 error_report("Expecting one image file name");
4176 flags
= BDRV_O_RDWR
;
4177 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
4179 error_report("Invalid cache option: %s", cache
);
4183 blk
= img_open(image_opts
, filename
, fmt
, flags
, writethrough
, quiet
,
4191 fmt
= bs
->drv
->format_name
;
4193 if (has_help_option(options
)) {
4194 /* If the format was auto-detected, print option help here */
4195 ret
= print_amend_option_help(fmt
);
4199 if (!bs
->drv
->bdrv_amend_options
) {
4200 error_report("Format driver '%s' does not support option amendment",
4206 /* Every driver supporting amendment must have amend_opts */
4207 assert(bs
->drv
->amend_opts
);
4209 amend_opts
= qemu_opts_append(amend_opts
, bs
->drv
->amend_opts
);
4210 opts
= qemu_opts_create(amend_opts
, NULL
, 0, &error_abort
);
4211 if (!qemu_opts_do_parse(opts
, options
, NULL
, &err
)) {
4212 /* Try to parse options using the create options */
4213 amend_opts
= qemu_opts_append(amend_opts
, bs
->drv
->create_opts
);
4214 qemu_opts_del(opts
);
4215 opts
= qemu_opts_create(amend_opts
, NULL
, 0, &error_abort
);
4216 if (qemu_opts_do_parse(opts
, options
, NULL
, NULL
)) {
4217 error_append_hint(&err
,
4218 "This option is only supported for image creation\n");
4221 error_report_err(err
);
4226 /* In case the driver does not call amend_status_cb() */
4227 qemu_progress_print(0.f
, 0);
4228 ret
= bdrv_amend_options(bs
, opts
, &amend_status_cb
, NULL
, force
, &err
);
4229 qemu_progress_print(100.f
, 0);
4231 error_report_err(err
);
4236 qemu_progress_end();
4240 qemu_opts_del(opts
);
4241 qemu_opts_free(amend_opts
);
4250 typedef struct BenchData
{
4252 uint64_t image_size
;
4259 bool drain_on_flush
;
4268 static void bench_undrained_flush_cb(void *opaque
, int ret
)
4271 error_report("Failed flush request: %s", strerror(-ret
));
4276 static void bench_cb(void *opaque
, int ret
)
4278 BenchData
*b
= opaque
;
4282 error_report("Failed request: %s", strerror(-ret
));
4287 /* Just finished a flush with drained queue: Start next requests */
4288 assert(b
->in_flight
== 0);
4289 b
->in_flush
= false;
4290 } else if (b
->in_flight
> 0) {
4291 int remaining
= b
->n
- b
->in_flight
;
4296 /* Time for flush? Drain queue if requested, then flush */
4297 if (b
->flush_interval
&& remaining
% b
->flush_interval
== 0) {
4298 if (!b
->in_flight
|| !b
->drain_on_flush
) {
4299 BlockCompletionFunc
*cb
;
4301 if (b
->drain_on_flush
) {
4305 cb
= bench_undrained_flush_cb
;
4308 acb
= blk_aio_flush(b
->blk
, cb
, b
);
4310 error_report("Failed to issue flush request");
4314 if (b
->drain_on_flush
) {
4320 while (b
->n
> b
->in_flight
&& b
->in_flight
< b
->nrreq
) {
4321 int64_t offset
= b
->offset
;
4322 /* blk_aio_* might look for completed I/Os and kick bench_cb
4323 * again, so make sure this operation is counted by in_flight
4324 * and b->offset is ready for the next submission.
4327 b
->offset
+= b
->step
;
4328 b
->offset
%= b
->image_size
;
4330 acb
= blk_aio_pwritev(b
->blk
, offset
, b
->qiov
, 0, bench_cb
, b
);
4332 acb
= blk_aio_preadv(b
->blk
, offset
, b
->qiov
, 0, bench_cb
, b
);
4335 error_report("Failed to issue request");
4341 static int img_bench(int argc
, char **argv
)
4344 const char *fmt
= NULL
, *filename
;
4346 bool image_opts
= false;
4347 bool is_write
= false;
4351 size_t bufsize
= 4096;
4354 int flush_interval
= 0;
4355 bool drain_on_flush
= true;
4357 BlockBackend
*blk
= NULL
;
4358 BenchData data
= {};
4360 bool writethrough
= false;
4361 struct timeval t1
, t2
;
4363 bool force_share
= false;
4367 static const struct option long_options
[] = {
4368 {"help", no_argument
, 0, 'h'},
4369 {"flush-interval", required_argument
, 0, OPTION_FLUSH_INTERVAL
},
4370 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
4371 {"pattern", required_argument
, 0, OPTION_PATTERN
},
4372 {"no-drain", no_argument
, 0, OPTION_NO_DRAIN
},
4373 {"force-share", no_argument
, 0, 'U'},
4376 c
= getopt_long(argc
, argv
, ":hc:d:f:ni:o:qs:S:t:wU", long_options
,
4384 missing_argument(argv
[optind
- 1]);
4387 unrecognized_option(argv
[optind
- 1]);
4396 if (qemu_strtoul(optarg
, NULL
, 0, &res
) < 0 || res
> INT_MAX
) {
4397 error_report("Invalid request count specified");
4407 if (qemu_strtoul(optarg
, NULL
, 0, &res
) < 0 || res
> INT_MAX
) {
4408 error_report("Invalid queue depth specified");
4418 flags
|= BDRV_O_NATIVE_AIO
;
4421 ret
= bdrv_parse_aio(optarg
, &flags
);
4423 error_report("Invalid aio option: %s", optarg
);
4430 offset
= cvtnum("offset", optarg
);
4444 sval
= cvtnum_full("buffer size", optarg
, 0, INT_MAX
);
4456 sval
= cvtnum_full("step_size", optarg
, 0, INT_MAX
);
4465 ret
= bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
);
4467 error_report("Invalid cache mode");
4473 flags
|= BDRV_O_RDWR
;
4479 case OPTION_PATTERN
:
4483 if (qemu_strtoul(optarg
, NULL
, 0, &res
) < 0 || res
> 0xff) {
4484 error_report("Invalid pattern byte specified");
4490 case OPTION_FLUSH_INTERVAL
:
4494 if (qemu_strtoul(optarg
, NULL
, 0, &res
) < 0 || res
> INT_MAX
) {
4495 error_report("Invalid flush interval specified");
4498 flush_interval
= res
;
4501 case OPTION_NO_DRAIN
:
4502 drain_on_flush
= false;
4504 case OPTION_IMAGE_OPTS
:
4510 if (optind
!= argc
- 1) {
4511 error_exit("Expecting one image file name");
4513 filename
= argv
[argc
- 1];
4515 if (!is_write
&& flush_interval
) {
4516 error_report("--flush-interval is only available in write tests");
4520 if (flush_interval
&& flush_interval
< depth
) {
4521 error_report("Flush interval can't be smaller than depth");
4526 blk
= img_open(image_opts
, filename
, fmt
, flags
, writethrough
, quiet
,
4533 image_size
= blk_getlength(blk
);
4534 if (image_size
< 0) {
4539 data
= (BenchData
) {
4541 .image_size
= image_size
,
4543 .step
= step
?: bufsize
,
4548 .flush_interval
= flush_interval
,
4549 .drain_on_flush
= drain_on_flush
,
4551 printf("Sending %d %s requests, %d bytes each, %d in parallel "
4552 "(starting at offset %" PRId64
", step size %d)\n",
4553 data
.n
, data
.write
? "write" : "read", data
.bufsize
, data
.nrreq
,
4554 data
.offset
, data
.step
);
4555 if (flush_interval
) {
4556 printf("Sending flush every %d requests\n", flush_interval
);
4559 buf_size
= data
.nrreq
* data
.bufsize
;
4560 data
.buf
= blk_blockalign(blk
, buf_size
);
4561 memset(data
.buf
, pattern
, data
.nrreq
* data
.bufsize
);
4563 blk_register_buf(blk
, data
.buf
, buf_size
);
4565 data
.qiov
= g_new(QEMUIOVector
, data
.nrreq
);
4566 for (i
= 0; i
< data
.nrreq
; i
++) {
4567 qemu_iovec_init(&data
.qiov
[i
], 1);
4568 qemu_iovec_add(&data
.qiov
[i
],
4569 data
.buf
+ i
* data
.bufsize
, data
.bufsize
);
4572 gettimeofday(&t1
, NULL
);
4575 while (data
.n
> 0) {
4576 main_loop_wait(false);
4578 gettimeofday(&t2
, NULL
);
4580 printf("Run completed in %3.3f seconds.\n",
4581 (t2
.tv_sec
- t1
.tv_sec
)
4582 + ((double)(t2
.tv_usec
- t1
.tv_usec
) / 1000000));
4586 blk_unregister_buf(blk
, data
.buf
);
4588 qemu_vfree(data
.buf
);
4605 typedef struct ImgBitmapAction
{
4606 enum ImgBitmapAct act
;
4607 const char *src
; /* only used for merge */
4608 QSIMPLEQ_ENTRY(ImgBitmapAction
) next
;
4611 static int img_bitmap(int argc
, char **argv
)
4615 QemuOpts
*opts
= NULL
;
4616 const char *fmt
= NULL
, *src_fmt
= NULL
, *src_filename
= NULL
;
4617 const char *filename
, *bitmap
;
4618 BlockBackend
*blk
= NULL
, *src
= NULL
;
4619 BlockDriverState
*bs
= NULL
, *src_bs
= NULL
;
4620 bool image_opts
= false;
4621 int64_t granularity
= 0;
4622 bool add
= false, merge
= false;
4623 QSIMPLEQ_HEAD(, ImgBitmapAction
) actions
;
4624 ImgBitmapAction
*act
, *act_next
;
4627 QSIMPLEQ_INIT(&actions
);
4630 static const struct option long_options
[] = {
4631 {"help", no_argument
, 0, 'h'},
4632 {"object", required_argument
, 0, OPTION_OBJECT
},
4633 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
4634 {"add", no_argument
, 0, OPTION_ADD
},
4635 {"remove", no_argument
, 0, OPTION_REMOVE
},
4636 {"clear", no_argument
, 0, OPTION_CLEAR
},
4637 {"enable", no_argument
, 0, OPTION_ENABLE
},
4638 {"disable", no_argument
, 0, OPTION_DISABLE
},
4639 {"merge", required_argument
, 0, OPTION_MERGE
},
4640 {"granularity", required_argument
, 0, 'g'},
4641 {"source-file", required_argument
, 0, 'b'},
4642 {"source-format", required_argument
, 0, 'F'},
4645 c
= getopt_long(argc
, argv
, ":b:f:F:g:h", long_options
, NULL
);
4652 missing_argument(argv
[optind
- 1]);
4655 unrecognized_option(argv
[optind
- 1]);
4661 src_filename
= optarg
;
4670 granularity
= cvtnum("granularity", optarg
);
4671 if (granularity
< 0) {
4676 act
= g_new0(ImgBitmapAction
, 1);
4677 act
->act
= BITMAP_ADD
;
4678 QSIMPLEQ_INSERT_TAIL(&actions
, act
, next
);
4682 act
= g_new0(ImgBitmapAction
, 1);
4683 act
->act
= BITMAP_REMOVE
;
4684 QSIMPLEQ_INSERT_TAIL(&actions
, act
, next
);
4687 act
= g_new0(ImgBitmapAction
, 1);
4688 act
->act
= BITMAP_CLEAR
;
4689 QSIMPLEQ_INSERT_TAIL(&actions
, act
, next
);
4692 act
= g_new0(ImgBitmapAction
, 1);
4693 act
->act
= BITMAP_ENABLE
;
4694 QSIMPLEQ_INSERT_TAIL(&actions
, act
, next
);
4696 case OPTION_DISABLE
:
4697 act
= g_new0(ImgBitmapAction
, 1);
4698 act
->act
= BITMAP_DISABLE
;
4699 QSIMPLEQ_INSERT_TAIL(&actions
, act
, next
);
4702 act
= g_new0(ImgBitmapAction
, 1);
4703 act
->act
= BITMAP_MERGE
;
4705 QSIMPLEQ_INSERT_TAIL(&actions
, act
, next
);
4709 opts
= qemu_opts_parse_noisily(&qemu_object_opts
, optarg
, true);
4714 case OPTION_IMAGE_OPTS
:
4720 if (qemu_opts_foreach(&qemu_object_opts
,
4721 user_creatable_add_opts_foreach
,
4722 qemu_img_object_print_help
, &error_fatal
)) {
4726 if (QSIMPLEQ_EMPTY(&actions
)) {
4727 error_report("Need at least one of --add, --remove, --clear, "
4728 "--enable, --disable, or --merge");
4732 if (granularity
&& !add
) {
4733 error_report("granularity only supported with --add");
4736 if (src_fmt
&& !src_filename
) {
4737 error_report("-F only supported with -b");
4740 if (src_filename
&& !merge
) {
4741 error_report("Merge bitmap source file only supported with "
4746 if (optind
!= argc
- 2) {
4747 error_report("Expecting filename and bitmap name");
4751 filename
= argv
[optind
];
4752 bitmap
= argv
[optind
+ 1];
4754 blk
= img_open(image_opts
, filename
, fmt
, BDRV_O_RDWR
, false, false,
4761 src
= img_open(false, src_filename
, src_fmt
, 0, false, false, false);
4765 src_bs
= blk_bs(src
);
4770 QSIMPLEQ_FOREACH_SAFE(act
, &actions
, next
, act_next
) {
4773 qmp_block_dirty_bitmap_add(bs
->node_name
, bitmap
,
4774 !!granularity
, granularity
, true, true,
4775 false, false, &err
);
4779 qmp_block_dirty_bitmap_remove(bs
->node_name
, bitmap
, &err
);
4783 qmp_block_dirty_bitmap_clear(bs
->node_name
, bitmap
, &err
);
4787 qmp_block_dirty_bitmap_enable(bs
->node_name
, bitmap
, &err
);
4790 case BITMAP_DISABLE
:
4791 qmp_block_dirty_bitmap_disable(bs
->node_name
, bitmap
, &err
);
4795 do_dirty_bitmap_merge(bs
->node_name
, bitmap
, src_bs
->node_name
,
4800 g_assert_not_reached();
4804 error_reportf_err(err
, "Operation %s on bitmap %s failed: ",
4816 qemu_opts_del(opts
);
4832 int bsz
; /* Block size */
4840 int (*f
)(const char *, struct DdIo
*, struct DdIo
*, struct DdInfo
*);
4844 static int img_dd_bs(const char *arg
,
4845 struct DdIo
*in
, struct DdIo
*out
,
4850 res
= cvtnum_full("bs", arg
, 1, INT_MAX
);
4855 in
->bsz
= out
->bsz
= res
;
4860 static int img_dd_count(const char *arg
,
4861 struct DdIo
*in
, struct DdIo
*out
,
4864 dd
->count
= cvtnum("count", arg
);
4866 if (dd
->count
< 0) {
4873 static int img_dd_if(const char *arg
,
4874 struct DdIo
*in
, struct DdIo
*out
,
4877 in
->filename
= g_strdup(arg
);
4882 static int img_dd_of(const char *arg
,
4883 struct DdIo
*in
, struct DdIo
*out
,
4886 out
->filename
= g_strdup(arg
);
4891 static int img_dd_skip(const char *arg
,
4892 struct DdIo
*in
, struct DdIo
*out
,
4895 in
->offset
= cvtnum("skip", arg
);
4897 if (in
->offset
< 0) {
4904 static int img_dd(int argc
, char **argv
)
4909 BlockDriver
*drv
= NULL
, *proto_drv
= NULL
;
4910 BlockBackend
*blk1
= NULL
, *blk2
= NULL
;
4911 QemuOpts
*opts
= NULL
;
4912 QemuOptsList
*create_opts
= NULL
;
4913 Error
*local_err
= NULL
;
4914 bool image_opts
= false;
4916 const char *out_fmt
= "raw";
4917 const char *fmt
= NULL
;
4919 int64_t block_count
= 0, out_pos
, in_pos
;
4920 bool force_share
= false;
4921 struct DdInfo dd
= {
4926 .bsz
= 512, /* Block size is by default 512 bytes */
4938 const struct DdOpts options
[] = {
4939 { "bs", img_dd_bs
, C_BS
},
4940 { "count", img_dd_count
, C_COUNT
},
4941 { "if", img_dd_if
, C_IF
},
4942 { "of", img_dd_of
, C_OF
},
4943 { "skip", img_dd_skip
, C_SKIP
},
4946 const struct option long_options
[] = {
4947 { "help", no_argument
, 0, 'h'},
4948 { "object", required_argument
, 0, OPTION_OBJECT
},
4949 { "image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
4950 { "force-share", no_argument
, 0, 'U'},
4954 while ((c
= getopt_long(argc
, argv
, ":hf:O:U", long_options
, NULL
))) {
4966 missing_argument(argv
[optind
- 1]);
4969 unrecognized_option(argv
[optind
- 1]);
4978 if (!qemu_opts_parse_noisily(&qemu_object_opts
, optarg
, true)) {
4983 case OPTION_IMAGE_OPTS
:
4989 for (i
= optind
; i
< argc
; i
++) {
4991 arg
= g_strdup(argv
[i
]);
4993 tmp
= strchr(arg
, '=');
4995 error_report("unrecognized operand %s", arg
);
5002 for (j
= 0; options
[j
].name
!= NULL
; j
++) {
5003 if (!strcmp(arg
, options
[j
].name
)) {
5007 if (options
[j
].name
== NULL
) {
5008 error_report("unrecognized operand %s", arg
);
5013 if (options
[j
].f(tmp
, &in
, &out
, &dd
) != 0) {
5017 dd
.flags
|= options
[j
].flag
;
5022 if (!(dd
.flags
& C_IF
&& dd
.flags
& C_OF
)) {
5023 error_report("Must specify both input and output files");
5028 if (qemu_opts_foreach(&qemu_object_opts
,
5029 user_creatable_add_opts_foreach
,
5030 qemu_img_object_print_help
, &error_fatal
)) {
5035 blk1
= img_open(image_opts
, in
.filename
, fmt
, 0, false, false,
5043 drv
= bdrv_find_format(out_fmt
);
5045 error_report("Unknown file format");
5049 proto_drv
= bdrv_find_protocol(out
.filename
, true, &local_err
);
5052 error_report_err(local_err
);
5056 if (!drv
->create_opts
) {
5057 error_report("Format driver '%s' does not support image creation",
5062 if (!proto_drv
->create_opts
) {
5063 error_report("Protocol driver '%s' does not support image creation",
5064 proto_drv
->format_name
);
5068 create_opts
= qemu_opts_append(create_opts
, drv
->create_opts
);
5069 create_opts
= qemu_opts_append(create_opts
, proto_drv
->create_opts
);
5071 opts
= qemu_opts_create(create_opts
, NULL
, 0, &error_abort
);
5073 size
= blk_getlength(blk1
);
5075 error_report("Failed to get size for '%s'", in
.filename
);
5080 if (dd
.flags
& C_COUNT
&& dd
.count
<= INT64_MAX
/ in
.bsz
&&
5081 dd
.count
* in
.bsz
< size
) {
5082 size
= dd
.count
* in
.bsz
;
5085 /* Overflow means the specified offset is beyond input image's size */
5086 if (dd
.flags
& C_SKIP
&& (in
.offset
> INT64_MAX
/ in
.bsz
||
5087 size
< in
.bsz
* in
.offset
)) {
5088 qemu_opt_set_number(opts
, BLOCK_OPT_SIZE
, 0, &error_abort
);
5090 qemu_opt_set_number(opts
, BLOCK_OPT_SIZE
,
5091 size
- in
.bsz
* in
.offset
, &error_abort
);
5094 ret
= bdrv_create(drv
, out
.filename
, opts
, &local_err
);
5096 error_reportf_err(local_err
,
5097 "%s: error while creating output image: ",
5103 /* TODO, we can't honour --image-opts for the target,
5104 * since it needs to be given in a format compatible
5105 * with the bdrv_create() call above which does not
5106 * support image-opts style.
5108 blk2
= img_open_file(out
.filename
, NULL
, out_fmt
, BDRV_O_RDWR
,
5109 false, false, false);
5116 if (dd
.flags
& C_SKIP
&& (in
.offset
> INT64_MAX
/ in
.bsz
||
5117 size
< in
.offset
* in
.bsz
)) {
5118 /* We give a warning if the skip option is bigger than the input
5119 * size and create an empty output disk image (i.e. like dd(1)).
5121 error_report("%s: cannot skip to specified offset", in
.filename
);
5124 in_pos
= in
.offset
* in
.bsz
;
5127 in
.buf
= g_new(uint8_t, in
.bsz
);
5129 for (out_pos
= 0; in_pos
< size
; block_count
++) {
5130 int in_ret
, out_ret
;
5132 if (in_pos
+ in
.bsz
> size
) {
5133 in_ret
= blk_pread(blk1
, in_pos
, in
.buf
, size
- in_pos
);
5135 in_ret
= blk_pread(blk1
, in_pos
, in
.buf
, in
.bsz
);
5138 error_report("error while reading from input image file: %s",
5145 out_ret
= blk_pwrite(blk2
, out_pos
, in
.buf
, in_ret
, 0);
5148 error_report("error while writing to output image file: %s",
5149 strerror(-out_ret
));
5158 qemu_opts_del(opts
);
5159 qemu_opts_free(create_opts
);
5162 g_free(in
.filename
);
5163 g_free(out
.filename
);
5173 static void dump_json_block_measure_info(BlockMeasureInfo
*info
)
5177 Visitor
*v
= qobject_output_visitor_new(&obj
);
5179 visit_type_BlockMeasureInfo(v
, NULL
, &info
, &error_abort
);
5180 visit_complete(v
, &obj
);
5181 str
= qobject_to_json_pretty(obj
);
5182 assert(str
!= NULL
);
5183 printf("%s\n", qstring_get_str(str
));
5189 static int img_measure(int argc
, char **argv
)
5191 static const struct option long_options
[] = {
5192 {"help", no_argument
, 0, 'h'},
5193 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
5194 {"object", required_argument
, 0, OPTION_OBJECT
},
5195 {"output", required_argument
, 0, OPTION_OUTPUT
},
5196 {"size", required_argument
, 0, OPTION_SIZE
},
5197 {"force-share", no_argument
, 0, 'U'},
5200 OutputFormat output_format
= OFORMAT_HUMAN
;
5201 BlockBackend
*in_blk
= NULL
;
5203 const char *filename
= NULL
;
5204 const char *fmt
= NULL
;
5205 const char *out_fmt
= "raw";
5206 char *options
= NULL
;
5207 char *snapshot_name
= NULL
;
5208 bool force_share
= false;
5209 QemuOpts
*opts
= NULL
;
5210 QemuOpts
*object_opts
= NULL
;
5211 QemuOpts
*sn_opts
= NULL
;
5212 QemuOptsList
*create_opts
= NULL
;
5213 bool image_opts
= false;
5214 uint64_t img_size
= UINT64_MAX
;
5215 BlockMeasureInfo
*info
= NULL
;
5216 Error
*local_err
= NULL
;
5220 while ((c
= getopt_long(argc
, argv
, "hf:O:o:l:U",
5221 long_options
, NULL
)) != -1) {
5234 if (accumulate_options(&options
, optarg
) < 0) {
5239 if (strstart(optarg
, SNAPSHOT_OPT_BASE
, NULL
)) {
5240 sn_opts
= qemu_opts_parse_noisily(&internal_snapshot_opts
,
5243 error_report("Failed in parsing snapshot param '%s'",
5248 snapshot_name
= optarg
;
5255 object_opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
5261 case OPTION_IMAGE_OPTS
:
5265 if (!strcmp(optarg
, "json")) {
5266 output_format
= OFORMAT_JSON
;
5267 } else if (!strcmp(optarg
, "human")) {
5268 output_format
= OFORMAT_HUMAN
;
5270 error_report("--output must be used with human or json "
5279 sval
= cvtnum("image size", optarg
);
5283 img_size
= (uint64_t)sval
;
5289 if (qemu_opts_foreach(&qemu_object_opts
,
5290 user_creatable_add_opts_foreach
,
5291 qemu_img_object_print_help
, &error_fatal
)) {
5295 if (argc
- optind
> 1) {
5296 error_report("At most one filename argument is allowed.");
5298 } else if (argc
- optind
== 1) {
5299 filename
= argv
[optind
];
5302 if (!filename
&& (image_opts
|| fmt
|| snapshot_name
|| sn_opts
)) {
5303 error_report("--image-opts, -f, and -l require a filename argument.");
5306 if (filename
&& img_size
!= UINT64_MAX
) {
5307 error_report("--size N cannot be used together with a filename.");
5310 if (!filename
&& img_size
== UINT64_MAX
) {
5311 error_report("Either --size N or one filename must be specified.");
5316 in_blk
= img_open(image_opts
, filename
, fmt
, 0,
5317 false, false, force_share
);
5323 bdrv_snapshot_load_tmp(blk_bs(in_blk
),
5324 qemu_opt_get(sn_opts
, SNAPSHOT_OPT_ID
),
5325 qemu_opt_get(sn_opts
, SNAPSHOT_OPT_NAME
),
5327 } else if (snapshot_name
!= NULL
) {
5328 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk
),
5329 snapshot_name
, &local_err
);
5332 error_reportf_err(local_err
, "Failed to load snapshot: ");
5337 drv
= bdrv_find_format(out_fmt
);
5339 error_report("Unknown file format '%s'", out_fmt
);
5342 if (!drv
->create_opts
) {
5343 error_report("Format driver '%s' does not support image creation",
5348 create_opts
= qemu_opts_append(create_opts
, drv
->create_opts
);
5349 create_opts
= qemu_opts_append(create_opts
, bdrv_file
.create_opts
);
5350 opts
= qemu_opts_create(create_opts
, NULL
, 0, &error_abort
);
5352 if (!qemu_opts_do_parse(opts
, options
, NULL
, &local_err
)) {
5353 error_report_err(local_err
);
5354 error_report("Invalid options for file format '%s'", out_fmt
);
5358 if (img_size
!= UINT64_MAX
) {
5359 qemu_opt_set_number(opts
, BLOCK_OPT_SIZE
, img_size
, &error_abort
);
5362 info
= bdrv_measure(drv
, opts
, in_blk
? blk_bs(in_blk
) : NULL
, &local_err
);
5364 error_report_err(local_err
);
5368 if (output_format
== OFORMAT_HUMAN
) {
5369 printf("required size: %" PRIu64
"\n", info
->required
);
5370 printf("fully allocated size: %" PRIu64
"\n", info
->fully_allocated
);
5371 if (info
->has_bitmaps
) {
5372 printf("bitmaps size: %" PRIu64
"\n", info
->bitmaps
);
5375 dump_json_block_measure_info(info
);
5381 qapi_free_BlockMeasureInfo(info
);
5382 qemu_opts_del(object_opts
);
5383 qemu_opts_del(opts
);
5384 qemu_opts_del(sn_opts
);
5385 qemu_opts_free(create_opts
);
5391 static const img_cmd_t img_cmds
[] = {
5392 #define DEF(option, callback, arg_string) \
5393 { option, callback },
5394 #include "qemu-img-cmds.h"
5399 int main(int argc
, char **argv
)
5401 const img_cmd_t
*cmd
;
5402 const char *cmdname
;
5403 Error
*local_error
= NULL
;
5404 char *trace_file
= NULL
;
5406 static const struct option long_options
[] = {
5407 {"help", no_argument
, 0, 'h'},
5408 {"version", no_argument
, 0, 'V'},
5409 {"trace", required_argument
, NULL
, 'T'},
5414 signal(SIGPIPE
, SIG_IGN
);
5417 error_init(argv
[0]);
5418 module_call_init(MODULE_INIT_TRACE
);
5419 qemu_init_exec_dir(argv
[0]);
5421 if (qemu_init_main_loop(&local_error
)) {
5422 error_report_err(local_error
);
5426 qcrypto_init(&error_fatal
);
5428 module_call_init(MODULE_INIT_QOM
);
5431 error_exit("Not enough arguments");
5434 qemu_add_opts(&qemu_object_opts
);
5435 qemu_add_opts(&qemu_source_opts
);
5436 qemu_add_opts(&qemu_trace_opts
);
5438 while ((c
= getopt_long(argc
, argv
, "+:hVT:", long_options
, NULL
)) != -1) {
5441 missing_argument(argv
[optind
- 1]);
5444 unrecognized_option(argv
[optind
- 1]);
5450 printf(QEMU_IMG_VERSION
);
5454 trace_file
= trace_opt_parse(optarg
);
5459 cmdname
= argv
[optind
];
5461 /* reset getopt_long scanning */
5467 qemu_reset_optind();
5469 if (!trace_init_backends()) {
5472 trace_init_file(trace_file
);
5473 qemu_set_log(LOG_TRACE
);
5475 /* find the command */
5476 for (cmd
= img_cmds
; cmd
->name
!= NULL
; cmd
++) {
5477 if (!strcmp(cmdname
, cmd
->name
)) {
5478 return cmd
->handler(argc
, argv
);
5483 error_exit("Command not found: %s", cmdname
);