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/qemu-progress.h"
30 #include "qemu-version.h"
31 #include "qapi/error.h"
32 #include "qapi/qapi-commands-block-core.h"
33 #include "qapi/qapi-visit-block-core.h"
34 #include "qapi/qobject-output-visitor.h"
35 #include "qapi/qmp/qjson.h"
36 #include "qapi/qmp/qdict.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/sockets.h"
45 #include "qemu/units.h"
46 #include "qemu/memalign.h"
47 #include "qom/object_interfaces.h"
48 #include "sysemu/block-backend.h"
49 #include "block/block_int.h"
50 #include "block/blockjob.h"
51 #include "block/qapi.h"
52 #include "crypto/init.h"
53 #include "trace/control.h"
54 #include "qemu/throttle.h"
55 #include "block/throttle-groups.h"
57 #define QEMU_IMG_VERSION "qemu-img version " QEMU_FULL_VERSION \
58 "\n" QEMU_COPYRIGHT "\n"
60 typedef struct img_cmd_t
{
62 int (*handler
)(int argc
, char **argv
);
67 OPTION_BACKING_CHAIN
= 257,
69 OPTION_IMAGE_OPTS
= 259,
71 OPTION_FLUSH_INTERVAL
= 261,
72 OPTION_NO_DRAIN
= 262,
73 OPTION_TARGET_IMAGE_OPTS
= 263,
75 OPTION_PREALLOCATION
= 265,
78 OPTION_TARGET_IS_ZERO
= 268,
87 OPTION_SKIP_BROKEN
= 277,
90 typedef enum OutputFormat
{
95 /* Default to cache=writeback as data integrity is not important for qemu-img */
96 #define BDRV_DEFAULT_CACHE "writeback"
98 static void format_print(void *opaque
, const char *name
)
103 static void QEMU_NORETURN
G_GNUC_PRINTF(1, 2) error_exit(const char *fmt
, ...)
108 error_vreport(fmt
, ap
);
111 error_printf("Try 'qemu-img --help' for more information\n");
115 static void QEMU_NORETURN
missing_argument(const char *option
)
117 error_exit("missing argument for option '%s'", option
);
120 static void QEMU_NORETURN
unrecognized_option(const char *option
)
122 error_exit("unrecognized option '%s'", option
);
125 /* Please keep in synch with docs/tools/qemu-img.rst */
126 static void QEMU_NORETURN
help(void)
128 const char *help_msg
=
130 "usage: qemu-img [standard options] command [command options]\n"
131 "QEMU disk image utility\n"
133 " '-h', '--help' display this help and exit\n"
134 " '-V', '--version' output version information and exit\n"
135 " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
136 " specify tracing options\n"
139 #define DEF(option, callback, arg_string) \
141 #include "qemu-img-cmds.h"
144 "Command parameters:\n"
145 " 'filename' is a disk image filename\n"
146 " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
147 " manual page for a description of the object properties. The most common\n"
148 " object type is a 'secret', which is used to supply passwords and/or\n"
149 " encryption keys.\n"
150 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
151 " 'cache' is the cache mode used to write the output disk image, the valid\n"
152 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
153 " 'directsync' and 'unsafe' (default for convert)\n"
154 " 'src_cache' is the cache mode used to read input disk images, the valid\n"
155 " options are the same as for the 'cache' option\n"
156 " 'size' is the disk image size in bytes. Optional suffixes\n"
157 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
158 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
159 " supported. 'b' is ignored.\n"
160 " 'output_filename' is the destination disk image filename\n"
161 " 'output_fmt' is the destination format\n"
162 " 'options' is a comma separated list of format specific options in a\n"
163 " name=value format. Use -o ? for an overview of the options supported by the\n"
165 " 'snapshot_param' is param used for internal snapshot, format\n"
166 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
168 " '-c' indicates that target image must be compressed (qcow format only)\n"
169 " '-u' allows unsafe backing chains. For rebasing, it is assumed that old and\n"
170 " new backing file match exactly. The image doesn't need a working\n"
171 " backing file before rebasing in this case (useful for renaming the\n"
172 " backing file). For image creation, allow creating without attempting\n"
173 " to open the backing file.\n"
174 " '-h' with or without a command shows this help and lists the supported formats\n"
175 " '-p' show progress of command (only certain commands)\n"
176 " '-q' use Quiet mode - do not print any output (except errors)\n"
177 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
178 " contain only zeros for qemu-img to create a sparse image during\n"
179 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
180 " unallocated or zero sectors, and the destination image will always be\n"
182 " '--output' takes the format in which the output must be done (human or json)\n"
183 " '-n' skips the target volume creation (useful if the volume is created\n"
184 " prior to running qemu-img)\n"
186 "Parameters to bitmap subcommand:\n"
187 " 'bitmap' is the name of the bitmap to manipulate, through one or more\n"
188 " actions from '--add', '--remove', '--clear', '--enable', '--disable',\n"
189 " or '--merge source'\n"
190 " '-g granularity' sets the granularity for '--add' actions\n"
191 " '-b source' and '-F src_fmt' tell '--merge' actions to find the source\n"
192 " bitmaps from an alternative file\n"
194 "Parameters to check subcommand:\n"
195 " '-r' tries to repair any inconsistencies that are found during the check.\n"
196 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
197 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
198 " hiding corruption that has already occurred.\n"
200 "Parameters to convert subcommand:\n"
201 " '--bitmaps' copies all top-level persistent bitmaps to destination\n"
202 " '-m' specifies how many coroutines work in parallel during the convert\n"
203 " process (defaults to 8)\n"
204 " '-W' allow to write to the target out of order rather than sequential\n"
206 "Parameters to snapshot subcommand:\n"
207 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
208 " '-a' applies a snapshot (revert disk to saved state)\n"
209 " '-c' creates a snapshot\n"
210 " '-d' deletes a snapshot\n"
211 " '-l' lists all snapshots in the given image\n"
213 "Parameters to compare subcommand:\n"
214 " '-f' first image format\n"
215 " '-F' second image format\n"
216 " '-s' run in Strict mode - fail on different image size or sector allocation\n"
218 "Parameters to dd subcommand:\n"
219 " 'bs=BYTES' read and write up to BYTES bytes at a time "
221 " 'count=N' copy only N input blocks\n"
222 " 'if=FILE' read from FILE\n"
223 " 'of=FILE' write to FILE\n"
224 " 'skip=N' skip N bs-sized blocks at the start of input\n";
226 printf("%s\nSupported formats:", help_msg
);
227 bdrv_iterate_format(format_print
, NULL
, false);
228 printf("\n\n" QEMU_HELP_BOTTOM
"\n");
233 * Is @optarg safe for accumulate_options()?
234 * It is when multiple of them can be joined together separated by ','.
235 * To make that work, @optarg must not start with ',' (or else a
236 * separating ',' preceding it gets escaped), and it must not end with
237 * an odd number of ',' (or else a separating ',' following it gets
238 * escaped), or be empty (or else a separating ',' preceding it can
239 * escape a separating ',' following it).
242 static bool is_valid_option_list(const char *optarg
)
244 size_t len
= strlen(optarg
);
247 if (!optarg
[0] || optarg
[0] == ',') {
251 for (i
= len
; i
> 0 && optarg
[i
- 1] == ','; i
--) {
260 static int accumulate_options(char **options
, char *optarg
)
264 if (!is_valid_option_list(optarg
)) {
265 error_report("Invalid option list: %s", optarg
);
270 *options
= g_strdup(optarg
);
272 new_options
= g_strdup_printf("%s,%s", *options
, optarg
);
274 *options
= new_options
;
279 static QemuOptsList qemu_source_opts
= {
281 .implied_opt_name
= "file",
282 .head
= QTAILQ_HEAD_INITIALIZER(qemu_source_opts
.head
),
288 static int G_GNUC_PRINTF(2, 3) qprintf(bool quiet
, const char *fmt
, ...)
294 ret
= vprintf(fmt
, args
);
301 static int print_block_option_help(const char *filename
, const char *fmt
)
303 BlockDriver
*drv
, *proto_drv
;
304 QemuOptsList
*create_opts
= NULL
;
305 Error
*local_err
= NULL
;
307 /* Find driver and parse its options */
308 drv
= bdrv_find_format(fmt
);
310 error_report("Unknown file format '%s'", fmt
);
314 if (!drv
->create_opts
) {
315 error_report("Format driver '%s' does not support image creation", fmt
);
319 create_opts
= qemu_opts_append(create_opts
, drv
->create_opts
);
321 proto_drv
= bdrv_find_protocol(filename
, true, &local_err
);
323 error_report_err(local_err
);
324 qemu_opts_free(create_opts
);
327 if (!proto_drv
->create_opts
) {
328 error_report("Protocol driver '%s' does not support image creation",
329 proto_drv
->format_name
);
330 qemu_opts_free(create_opts
);
333 create_opts
= qemu_opts_append(create_opts
, proto_drv
->create_opts
);
337 printf("Supported options:\n");
339 printf("Supported %s options:\n", fmt
);
341 qemu_opts_print_help(create_opts
, false);
342 qemu_opts_free(create_opts
);
346 "The protocol level may support further options.\n"
347 "Specify the target filename to include those options.\n");
354 static BlockBackend
*img_open_opts(const char *optstr
,
355 QemuOpts
*opts
, int flags
, bool writethrough
,
356 bool quiet
, bool force_share
)
359 Error
*local_err
= NULL
;
361 options
= qemu_opts_to_qdict(opts
, NULL
);
363 if (qdict_haskey(options
, BDRV_OPT_FORCE_SHARE
)
364 && strcmp(qdict_get_str(options
, BDRV_OPT_FORCE_SHARE
), "on")) {
365 error_report("--force-share/-U conflicts with image options");
366 qobject_unref(options
);
369 qdict_put_str(options
, BDRV_OPT_FORCE_SHARE
, "on");
371 blk
= blk_new_open(NULL
, NULL
, options
, flags
, &local_err
);
373 error_reportf_err(local_err
, "Could not open '%s': ", optstr
);
376 blk_set_enable_write_cache(blk
, !writethrough
);
381 static BlockBackend
*img_open_file(const char *filename
,
383 const char *fmt
, int flags
,
384 bool writethrough
, bool quiet
,
388 Error
*local_err
= NULL
;
391 options
= qdict_new();
394 qdict_put_str(options
, "driver", fmt
);
398 qdict_put_bool(options
, BDRV_OPT_FORCE_SHARE
, true);
400 blk
= blk_new_open(filename
, NULL
, options
, flags
, &local_err
);
402 error_reportf_err(local_err
, "Could not open '%s': ", filename
);
405 blk_set_enable_write_cache(blk
, !writethrough
);
411 static int img_add_key_secrets(void *opaque
,
412 const char *name
, const char *value
,
415 QDict
*options
= opaque
;
417 if (g_str_has_suffix(name
, "key-secret")) {
418 qdict_put_str(options
, name
, value
);
425 static BlockBackend
*img_open(bool image_opts
,
426 const char *filename
,
427 const char *fmt
, int flags
, bool writethrough
,
428 bool quiet
, bool force_share
)
434 error_report("--image-opts and --format are mutually exclusive");
437 opts
= qemu_opts_parse_noisily(qemu_find_opts("source"),
442 blk
= img_open_opts(filename
, opts
, flags
, writethrough
, quiet
,
445 blk
= img_open_file(filename
, NULL
, fmt
, flags
, writethrough
, quiet
,
452 static int add_old_style_options(const char *fmt
, QemuOpts
*opts
,
453 const char *base_filename
,
454 const char *base_fmt
)
457 if (!qemu_opt_set(opts
, BLOCK_OPT_BACKING_FILE
, base_filename
,
459 error_report("Backing file not supported for file format '%s'",
465 if (!qemu_opt_set(opts
, BLOCK_OPT_BACKING_FMT
, base_fmt
, NULL
)) {
466 error_report("Backing file format not supported for file "
474 static int64_t cvtnum_full(const char *name
, const char *value
, int64_t min
,
480 err
= qemu_strtosz(value
, NULL
, &res
);
481 if (err
< 0 && err
!= -ERANGE
) {
482 error_report("Invalid %s specified. You may use "
483 "k, M, G, T, P or E suffixes for", name
);
484 error_report("kilobytes, megabytes, gigabytes, terabytes, "
485 "petabytes and exabytes.");
488 if (err
== -ERANGE
|| res
> max
|| res
< min
) {
489 error_report("Invalid %s specified. Must be between %" PRId64
490 " and %" PRId64
".", name
, min
, max
);
496 static int64_t cvtnum(const char *name
, const char *value
)
498 return cvtnum_full(name
, value
, 0, INT64_MAX
);
501 static int img_create(int argc
, char **argv
)
504 uint64_t img_size
= -1;
505 const char *fmt
= "raw";
506 const char *base_fmt
= NULL
;
507 const char *filename
;
508 const char *base_filename
= NULL
;
509 char *options
= NULL
;
510 Error
*local_err
= NULL
;
515 static const struct option long_options
[] = {
516 {"help", no_argument
, 0, 'h'},
517 {"object", required_argument
, 0, OPTION_OBJECT
},
520 c
= getopt_long(argc
, argv
, ":F:b:f:ho:qu",
527 missing_argument(argv
[optind
- 1]);
530 unrecognized_option(argv
[optind
- 1]);
539 base_filename
= optarg
;
545 if (accumulate_options(&options
, optarg
) < 0) {
553 flags
|= BDRV_O_NO_BACKING
;
556 user_creatable_process_cmdline(optarg
);
561 /* Get the filename */
562 filename
= (optind
< argc
) ? argv
[optind
] : NULL
;
563 if (options
&& has_help_option(options
)) {
565 return print_block_option_help(filename
, fmt
);
568 if (optind
>= argc
) {
569 error_exit("Expecting image file name");
573 /* Get image size, if specified */
577 sval
= cvtnum("image size", argv
[optind
++]);
581 img_size
= (uint64_t)sval
;
583 if (optind
!= argc
) {
584 error_exit("Unexpected argument: %s", argv
[optind
]);
587 bdrv_img_create(filename
, fmt
, base_filename
, base_fmt
,
588 options
, img_size
, flags
, quiet
, &local_err
);
590 error_reportf_err(local_err
, "%s: ", filename
);
602 static void dump_json_image_check(ImageCheck
*check
, bool quiet
)
606 Visitor
*v
= qobject_output_visitor_new(&obj
);
608 visit_type_ImageCheck(v
, NULL
, &check
, &error_abort
);
609 visit_complete(v
, &obj
);
610 str
= qobject_to_json_pretty(obj
, true);
612 qprintf(quiet
, "%s\n", str
->str
);
615 g_string_free(str
, true);
618 static void dump_human_image_check(ImageCheck
*check
, bool quiet
)
620 if (!(check
->corruptions
|| check
->leaks
|| check
->check_errors
)) {
621 qprintf(quiet
, "No errors were found on the image.\n");
623 if (check
->corruptions
) {
624 qprintf(quiet
, "\n%" PRId64
" errors were found on the image.\n"
625 "Data may be corrupted, or further writes to the image "
632 "\n%" PRId64
" leaked clusters were found on the image.\n"
633 "This means waste of disk space, but no harm to data.\n",
637 if (check
->check_errors
) {
640 " internal errors have occurred during the check.\n",
641 check
->check_errors
);
645 if (check
->total_clusters
!= 0 && check
->allocated_clusters
!= 0) {
646 qprintf(quiet
, "%" PRId64
"/%" PRId64
" = %0.2f%% allocated, "
647 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
648 check
->allocated_clusters
, check
->total_clusters
,
649 check
->allocated_clusters
* 100.0 / check
->total_clusters
,
650 check
->fragmented_clusters
* 100.0 / check
->allocated_clusters
,
651 check
->compressed_clusters
* 100.0 /
652 check
->allocated_clusters
);
655 if (check
->image_end_offset
) {
657 "Image end offset: %" PRId64
"\n", check
->image_end_offset
);
661 static int collect_image_check(BlockDriverState
*bs
,
663 const char *filename
,
668 BdrvCheckResult result
;
670 ret
= bdrv_check(bs
, &result
, fix
);
675 check
->filename
= g_strdup(filename
);
676 check
->format
= g_strdup(bdrv_get_format_name(bs
));
677 check
->check_errors
= result
.check_errors
;
678 check
->corruptions
= result
.corruptions
;
679 check
->has_corruptions
= result
.corruptions
!= 0;
680 check
->leaks
= result
.leaks
;
681 check
->has_leaks
= result
.leaks
!= 0;
682 check
->corruptions_fixed
= result
.corruptions_fixed
;
683 check
->has_corruptions_fixed
= result
.corruptions_fixed
!= 0;
684 check
->leaks_fixed
= result
.leaks_fixed
;
685 check
->has_leaks_fixed
= result
.leaks_fixed
!= 0;
686 check
->image_end_offset
= result
.image_end_offset
;
687 check
->has_image_end_offset
= result
.image_end_offset
!= 0;
688 check
->total_clusters
= result
.bfi
.total_clusters
;
689 check
->has_total_clusters
= result
.bfi
.total_clusters
!= 0;
690 check
->allocated_clusters
= result
.bfi
.allocated_clusters
;
691 check
->has_allocated_clusters
= result
.bfi
.allocated_clusters
!= 0;
692 check
->fragmented_clusters
= result
.bfi
.fragmented_clusters
;
693 check
->has_fragmented_clusters
= result
.bfi
.fragmented_clusters
!= 0;
694 check
->compressed_clusters
= result
.bfi
.compressed_clusters
;
695 check
->has_compressed_clusters
= result
.bfi
.compressed_clusters
!= 0;
701 * Checks an image for consistency. Exit codes:
703 * 0 - Check completed, image is good
704 * 1 - Check not completed because of internal errors
705 * 2 - Check completed, image is corrupted
706 * 3 - Check completed, image has leaked clusters, but is good otherwise
707 * 63 - Checks are not supported by the image format
709 static int img_check(int argc
, char **argv
)
712 OutputFormat output_format
= OFORMAT_HUMAN
;
713 const char *filename
, *fmt
, *output
, *cache
;
715 BlockDriverState
*bs
;
717 int flags
= BDRV_O_CHECK
;
721 bool image_opts
= false;
722 bool force_share
= false;
726 cache
= BDRV_DEFAULT_CACHE
;
729 int option_index
= 0;
730 static const struct option long_options
[] = {
731 {"help", no_argument
, 0, 'h'},
732 {"format", required_argument
, 0, 'f'},
733 {"repair", required_argument
, 0, 'r'},
734 {"output", required_argument
, 0, OPTION_OUTPUT
},
735 {"object", required_argument
, 0, OPTION_OBJECT
},
736 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
737 {"force-share", no_argument
, 0, 'U'},
740 c
= getopt_long(argc
, argv
, ":hf:r:T:qU",
741 long_options
, &option_index
);
747 missing_argument(argv
[optind
- 1]);
750 unrecognized_option(argv
[optind
- 1]);
759 flags
|= BDRV_O_RDWR
;
761 if (!strcmp(optarg
, "leaks")) {
762 fix
= BDRV_FIX_LEAKS
;
763 } else if (!strcmp(optarg
, "all")) {
764 fix
= BDRV_FIX_LEAKS
| BDRV_FIX_ERRORS
;
766 error_exit("Unknown option value for -r "
767 "(expecting 'leaks' or 'all'): %s", optarg
);
783 user_creatable_process_cmdline(optarg
);
785 case OPTION_IMAGE_OPTS
:
790 if (optind
!= argc
- 1) {
791 error_exit("Expecting one image file name");
793 filename
= argv
[optind
++];
795 if (output
&& !strcmp(output
, "json")) {
796 output_format
= OFORMAT_JSON
;
797 } else if (output
&& !strcmp(output
, "human")) {
798 output_format
= OFORMAT_HUMAN
;
800 error_report("--output must be used with human or json as argument.");
804 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
806 error_report("Invalid source cache option: %s", cache
);
810 blk
= img_open(image_opts
, filename
, fmt
, flags
, writethrough
, quiet
,
817 check
= g_new0(ImageCheck
, 1);
818 ret
= collect_image_check(bs
, check
, filename
, fmt
, fix
);
820 if (ret
== -ENOTSUP
) {
821 error_report("This image format does not support checks");
826 if (check
->corruptions_fixed
|| check
->leaks_fixed
) {
827 int corruptions_fixed
, leaks_fixed
;
828 bool has_leaks_fixed
, has_corruptions_fixed
;
830 leaks_fixed
= check
->leaks_fixed
;
831 has_leaks_fixed
= check
->has_leaks_fixed
;
832 corruptions_fixed
= check
->corruptions_fixed
;
833 has_corruptions_fixed
= check
->has_corruptions_fixed
;
835 if (output_format
== OFORMAT_HUMAN
) {
837 "The following inconsistencies were found and repaired:\n\n"
838 " %" PRId64
" leaked clusters\n"
839 " %" PRId64
" corruptions\n\n"
840 "Double checking the fixed image now...\n",
842 check
->corruptions_fixed
);
845 qapi_free_ImageCheck(check
);
846 check
= g_new0(ImageCheck
, 1);
847 ret
= collect_image_check(bs
, check
, filename
, fmt
, 0);
849 check
->leaks_fixed
= leaks_fixed
;
850 check
->has_leaks_fixed
= has_leaks_fixed
;
851 check
->corruptions_fixed
= corruptions_fixed
;
852 check
->has_corruptions_fixed
= has_corruptions_fixed
;
856 switch (output_format
) {
858 dump_human_image_check(check
, quiet
);
861 dump_json_image_check(check
, quiet
);
866 if (ret
|| check
->check_errors
) {
868 error_report("Check failed: %s", strerror(-ret
));
870 error_report("Check failed");
876 if (check
->corruptions
) {
878 } else if (check
->leaks
) {
885 qapi_free_ImageCheck(check
);
890 typedef struct CommonBlockJobCBInfo
{
891 BlockDriverState
*bs
;
893 } CommonBlockJobCBInfo
;
895 static void common_block_job_cb(void *opaque
, int ret
)
897 CommonBlockJobCBInfo
*cbi
= opaque
;
900 error_setg_errno(cbi
->errp
, -ret
, "Block job failed");
904 static void run_block_job(BlockJob
*job
, Error
**errp
)
906 uint64_t progress_current
, progress_total
;
907 AioContext
*aio_context
= block_job_get_aio_context(job
);
910 aio_context_acquire(aio_context
);
913 float progress
= 0.0f
;
914 aio_poll(aio_context
, true);
916 progress_get_snapshot(&job
->job
.progress
, &progress_current
,
918 if (progress_total
) {
919 progress
= (float)progress_current
/ progress_total
* 100.f
;
921 qemu_progress_print(progress
, 0);
922 } while (!job_is_ready(&job
->job
) && !job_is_completed(&job
->job
));
924 if (!job_is_completed(&job
->job
)) {
925 ret
= job_complete_sync(&job
->job
, errp
);
929 job_unref(&job
->job
);
930 aio_context_release(aio_context
);
932 /* publish completion progress only when success */
934 qemu_progress_print(100.f
, 0);
938 static int img_commit(int argc
, char **argv
)
941 const char *filename
, *fmt
, *cache
, *base
;
943 BlockDriverState
*bs
, *base_bs
;
945 bool progress
= false, quiet
= false, drop
= false;
947 Error
*local_err
= NULL
;
948 CommonBlockJobCBInfo cbi
;
949 bool image_opts
= false;
950 AioContext
*aio_context
;
951 int64_t rate_limit
= 0;
954 cache
= BDRV_DEFAULT_CACHE
;
957 static const struct option long_options
[] = {
958 {"help", no_argument
, 0, 'h'},
959 {"object", required_argument
, 0, OPTION_OBJECT
},
960 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
963 c
= getopt_long(argc
, argv
, ":f:ht:b:dpqr:",
970 missing_argument(argv
[optind
- 1]);
973 unrecognized_option(argv
[optind
- 1]);
999 rate_limit
= cvtnum("rate limit", optarg
);
1000 if (rate_limit
< 0) {
1005 user_creatable_process_cmdline(optarg
);
1007 case OPTION_IMAGE_OPTS
:
1013 /* Progress is not shown in Quiet mode */
1018 if (optind
!= argc
- 1) {
1019 error_exit("Expecting one image file name");
1021 filename
= argv
[optind
++];
1023 flags
= BDRV_O_RDWR
| BDRV_O_UNMAP
;
1024 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
1026 error_report("Invalid cache option: %s", cache
);
1030 blk
= img_open(image_opts
, filename
, fmt
, flags
, writethrough
, quiet
,
1037 qemu_progress_init(progress
, 1.f
);
1038 qemu_progress_print(0.f
, 100);
1041 base_bs
= bdrv_find_backing_image(bs
, base
);
1043 error_setg(&local_err
,
1044 "Did not find '%s' in the backing chain of '%s'",
1049 /* This is different from QMP, which by default uses the deepest file in
1050 * the backing chain (i.e., the very base); however, the traditional
1051 * behavior of qemu-img commit is using the immediate backing file. */
1052 base_bs
= bdrv_backing_chain_next(bs
);
1054 error_setg(&local_err
, "Image does not have a backing file");
1059 cbi
= (CommonBlockJobCBInfo
){
1064 aio_context
= bdrv_get_aio_context(bs
);
1065 aio_context_acquire(aio_context
);
1066 commit_active_start("commit", bs
, base_bs
, JOB_DEFAULT
, rate_limit
,
1067 BLOCKDEV_ON_ERROR_REPORT
, NULL
, common_block_job_cb
,
1068 &cbi
, false, &local_err
);
1069 aio_context_release(aio_context
);
1074 /* When the block job completes, the BlockBackend reference will point to
1075 * the old backing file. In order to avoid that the top image is already
1076 * deleted, so we can still empty it afterwards, increment the reference
1077 * counter here preemptively. */
1082 job
= block_job_get("commit");
1084 run_block_job(job
, &local_err
);
1090 BlockBackend
*old_backing_blk
;
1092 old_backing_blk
= blk_new_with_bs(bs
, BLK_PERM_WRITE
, BLK_PERM_ALL
,
1094 if (!old_backing_blk
) {
1097 ret
= blk_make_empty(old_backing_blk
, &local_err
);
1098 blk_unref(old_backing_blk
);
1099 if (ret
== -ENOTSUP
) {
1100 error_free(local_err
);
1102 } else if (ret
< 0) {
1113 qemu_progress_end();
1118 error_report_err(local_err
);
1122 qprintf(quiet
, "Image committed.\n");
1127 * Returns -1 if 'buf' contains only zeroes, otherwise the byte index
1128 * of the first sector boundary within buf where the sector contains a
1129 * non-zero byte. This function is robust to a buffer that is not
1132 static int64_t find_nonzero(const uint8_t *buf
, int64_t n
)
1135 int64_t end
= QEMU_ALIGN_DOWN(n
, BDRV_SECTOR_SIZE
);
1137 for (i
= 0; i
< end
; i
+= BDRV_SECTOR_SIZE
) {
1138 if (!buffer_is_zero(buf
+ i
, BDRV_SECTOR_SIZE
)) {
1142 if (i
< n
&& !buffer_is_zero(buf
+ i
, n
- end
)) {
1149 * Returns true iff the first sector pointed to by 'buf' contains at least
1152 * 'pnum' is set to the number of sectors (including and immediately following
1153 * the first one) that are known to be in the same allocated/unallocated state.
1154 * The function will try to align the end offset to alignment boundaries so
1155 * that the request will at least end aligned and consecutive requests will
1156 * also start at an aligned offset.
1158 static int is_allocated_sectors(const uint8_t *buf
, int n
, int *pnum
,
1159 int64_t sector_num
, int alignment
)
1168 is_zero
= buffer_is_zero(buf
, BDRV_SECTOR_SIZE
);
1169 for(i
= 1; i
< n
; i
++) {
1170 buf
+= BDRV_SECTOR_SIZE
;
1171 if (is_zero
!= buffer_is_zero(buf
, BDRV_SECTOR_SIZE
)) {
1178 * The whole buf is the same.
1179 * No reason to split it into chunks, so return now.
1185 tail
= (sector_num
+ i
) & (alignment
- 1);
1187 if (is_zero
&& i
<= tail
) {
1189 * For sure next sector after i is data, and it will rewrite this
1190 * tail anyway due to RMW. So, let's just write data now.
1195 /* If possible, align up end offset of allocated areas. */
1196 i
+= alignment
- tail
;
1200 * For sure next sector after i is data, and it will rewrite this
1201 * tail anyway due to RMW. Better is avoid RMW and write zeroes up
1212 * Like is_allocated_sectors, but if the buffer starts with a used sector,
1213 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1214 * breaking up write requests for only small sparse areas.
1216 static int is_allocated_sectors_min(const uint8_t *buf
, int n
, int *pnum
,
1217 int min
, int64_t sector_num
, int alignment
)
1220 int num_checked
, num_used
;
1226 ret
= is_allocated_sectors(buf
, n
, pnum
, sector_num
, alignment
);
1232 buf
+= BDRV_SECTOR_SIZE
* *pnum
;
1234 sector_num
+= *pnum
;
1235 num_checked
= num_used
;
1238 ret
= is_allocated_sectors(buf
, n
, pnum
, sector_num
, alignment
);
1240 buf
+= BDRV_SECTOR_SIZE
* *pnum
;
1242 sector_num
+= *pnum
;
1243 num_checked
+= *pnum
;
1245 num_used
= num_checked
;
1246 } else if (*pnum
>= min
) {
1256 * Compares two buffers sector by sector. Returns 0 if the first
1257 * sector of each buffer matches, non-zero otherwise.
1259 * pnum is set to the sector-aligned size of the buffer prefix that
1260 * has the same matching status as the first sector.
1262 static int compare_buffers(const uint8_t *buf1
, const uint8_t *buf2
,
1263 int64_t bytes
, int64_t *pnum
)
1266 int64_t i
= MIN(bytes
, BDRV_SECTOR_SIZE
);
1270 res
= !!memcmp(buf1
, buf2
, i
);
1272 int64_t len
= MIN(bytes
- i
, BDRV_SECTOR_SIZE
);
1274 if (!!memcmp(buf1
+ i
, buf2
+ i
, len
) != res
) {
1284 #define IO_BUF_SIZE (2 * MiB)
1287 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1289 * Intended for use by 'qemu-img compare': Returns 0 in case sectors are
1290 * filled with 0, 1 if sectors contain non-zero data (this is a comparison
1291 * failure), and 4 on error (the exit status for read errors), after emitting
1294 * @param blk: BlockBackend for the image
1295 * @param offset: Starting offset to check
1296 * @param bytes: Number of bytes to check
1297 * @param filename: Name of disk file we are checking (logging purpose)
1298 * @param buffer: Allocated buffer for storing read data
1299 * @param quiet: Flag for quiet mode
1301 static int check_empty_sectors(BlockBackend
*blk
, int64_t offset
,
1302 int64_t bytes
, const char *filename
,
1303 uint8_t *buffer
, bool quiet
)
1308 ret
= blk_pread(blk
, offset
, buffer
, bytes
);
1310 error_report("Error while reading offset %" PRId64
" of %s: %s",
1311 offset
, filename
, strerror(-ret
));
1314 idx
= find_nonzero(buffer
, bytes
);
1316 qprintf(quiet
, "Content mismatch at offset %" PRId64
"!\n",
1325 * Compares two images. Exit codes:
1327 * 0 - Images are identical or the requested help was printed
1329 * >1 - Error occurred
1331 static int img_compare(int argc
, char **argv
)
1333 const char *fmt1
= NULL
, *fmt2
= NULL
, *cache
, *filename1
, *filename2
;
1334 BlockBackend
*blk1
, *blk2
;
1335 BlockDriverState
*bs1
, *bs2
;
1336 int64_t total_size1
, total_size2
;
1337 uint8_t *buf1
= NULL
, *buf2
= NULL
;
1338 int64_t pnum1
, pnum2
;
1339 int allocated1
, allocated2
;
1340 int ret
= 0; /* return value - 0 Ident, 1 Different, >1 Error */
1341 bool progress
= false, quiet
= false, strict
= false;
1348 uint64_t progress_base
;
1349 bool image_opts
= false;
1350 bool force_share
= false;
1352 cache
= BDRV_DEFAULT_CACHE
;
1354 static const struct option long_options
[] = {
1355 {"help", no_argument
, 0, 'h'},
1356 {"object", required_argument
, 0, OPTION_OBJECT
},
1357 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
1358 {"force-share", no_argument
, 0, 'U'},
1361 c
= getopt_long(argc
, argv
, ":hf:F:T:pqsU",
1362 long_options
, NULL
);
1368 missing_argument(argv
[optind
- 1]);
1371 unrecognized_option(argv
[optind
- 1]);
1399 Error
*local_err
= NULL
;
1401 if (!user_creatable_add_from_str(optarg
, &local_err
)) {
1403 error_report_err(local_err
);
1406 /* Help was printed */
1412 case OPTION_IMAGE_OPTS
:
1418 /* Progress is not shown in Quiet mode */
1424 if (optind
!= argc
- 2) {
1425 error_exit("Expecting two image file names");
1427 filename1
= argv
[optind
++];
1428 filename2
= argv
[optind
++];
1430 /* Initialize before goto out */
1431 qemu_progress_init(progress
, 2.0);
1434 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
1436 error_report("Invalid source cache option: %s", cache
);
1441 blk1
= img_open(image_opts
, filename1
, fmt1
, flags
, writethrough
, quiet
,
1448 blk2
= img_open(image_opts
, filename2
, fmt2
, flags
, writethrough
, quiet
,
1457 buf1
= blk_blockalign(blk1
, IO_BUF_SIZE
);
1458 buf2
= blk_blockalign(blk2
, IO_BUF_SIZE
);
1459 total_size1
= blk_getlength(blk1
);
1460 if (total_size1
< 0) {
1461 error_report("Can't get size of %s: %s",
1462 filename1
, strerror(-total_size1
));
1466 total_size2
= blk_getlength(blk2
);
1467 if (total_size2
< 0) {
1468 error_report("Can't get size of %s: %s",
1469 filename2
, strerror(-total_size2
));
1473 total_size
= MIN(total_size1
, total_size2
);
1474 progress_base
= MAX(total_size1
, total_size2
);
1476 qemu_progress_print(0, 100);
1478 if (strict
&& total_size1
!= total_size2
) {
1480 qprintf(quiet
, "Strict mode: Image size mismatch!\n");
1484 while (offset
< total_size
) {
1485 int status1
, status2
;
1487 status1
= bdrv_block_status_above(bs1
, NULL
, offset
,
1488 total_size1
- offset
, &pnum1
, NULL
,
1492 error_report("Sector allocation test failed for %s", filename1
);
1495 allocated1
= status1
& BDRV_BLOCK_ALLOCATED
;
1497 status2
= bdrv_block_status_above(bs2
, NULL
, offset
,
1498 total_size2
- offset
, &pnum2
, NULL
,
1502 error_report("Sector allocation test failed for %s", filename2
);
1505 allocated2
= status2
& BDRV_BLOCK_ALLOCATED
;
1507 assert(pnum1
&& pnum2
);
1508 chunk
= MIN(pnum1
, pnum2
);
1511 if (status1
!= status2
) {
1513 qprintf(quiet
, "Strict mode: Offset %" PRId64
1514 " block status mismatch!\n", offset
);
1518 if ((status1
& BDRV_BLOCK_ZERO
) && (status2
& BDRV_BLOCK_ZERO
)) {
1520 } else if (allocated1
== allocated2
) {
1524 chunk
= MIN(chunk
, IO_BUF_SIZE
);
1525 ret
= blk_pread(blk1
, offset
, buf1
, chunk
);
1527 error_report("Error while reading offset %" PRId64
1529 offset
, filename1
, strerror(-ret
));
1533 ret
= blk_pread(blk2
, offset
, buf2
, chunk
);
1535 error_report("Error while reading offset %" PRId64
1537 offset
, filename2
, strerror(-ret
));
1541 ret
= compare_buffers(buf1
, buf2
, chunk
, &pnum
);
1542 if (ret
|| pnum
!= chunk
) {
1543 qprintf(quiet
, "Content mismatch at offset %" PRId64
"!\n",
1544 offset
+ (ret
? 0 : pnum
));
1550 chunk
= MIN(chunk
, IO_BUF_SIZE
);
1552 ret
= check_empty_sectors(blk1
, offset
, chunk
,
1553 filename1
, buf1
, quiet
);
1555 ret
= check_empty_sectors(blk2
, offset
, chunk
,
1556 filename2
, buf1
, quiet
);
1563 qemu_progress_print(((float) chunk
/ progress_base
) * 100, 100);
1566 if (total_size1
!= total_size2
) {
1567 BlockBackend
*blk_over
;
1568 const char *filename_over
;
1570 qprintf(quiet
, "Warning: Image size mismatch!\n");
1571 if (total_size1
> total_size2
) {
1573 filename_over
= filename1
;
1576 filename_over
= filename2
;
1579 while (offset
< progress_base
) {
1580 ret
= bdrv_block_status_above(blk_bs(blk_over
), NULL
, offset
,
1581 progress_base
- offset
, &chunk
,
1585 error_report("Sector allocation test failed for %s",
1590 if (ret
& BDRV_BLOCK_ALLOCATED
&& !(ret
& BDRV_BLOCK_ZERO
)) {
1591 chunk
= MIN(chunk
, IO_BUF_SIZE
);
1592 ret
= check_empty_sectors(blk_over
, offset
, chunk
,
1593 filename_over
, buf1
, quiet
);
1599 qemu_progress_print(((float) chunk
/ progress_base
) * 100, 100);
1603 qprintf(quiet
, "Images are identical.\n");
1613 qemu_progress_end();
1617 /* Convenience wrapper around qmp_block_dirty_bitmap_merge */
1618 static void do_dirty_bitmap_merge(const char *dst_node
, const char *dst_name
,
1619 const char *src_node
, const char *src_name
,
1622 BlockDirtyBitmapMergeSource
*merge_src
;
1623 BlockDirtyBitmapMergeSourceList
*list
= NULL
;
1625 merge_src
= g_new0(BlockDirtyBitmapMergeSource
, 1);
1626 merge_src
->type
= QTYPE_QDICT
;
1627 merge_src
->u
.external
.node
= g_strdup(src_node
);
1628 merge_src
->u
.external
.name
= g_strdup(src_name
);
1629 QAPI_LIST_PREPEND(list
, merge_src
);
1630 qmp_block_dirty_bitmap_merge(dst_node
, dst_name
, list
, errp
);
1631 qapi_free_BlockDirtyBitmapMergeSourceList(list
);
1634 enum ImgConvertBlockStatus
{
1640 #define MAX_COROUTINES 16
1641 #define CONVERT_THROTTLE_GROUP "img_convert"
1643 typedef struct ImgConvertState
{
1645 int64_t *src_sectors
;
1648 int64_t total_sectors
;
1649 int64_t allocated_sectors
;
1650 int64_t allocated_done
;
1653 enum ImgConvertBlockStatus status
;
1654 int64_t sector_next_status
;
1655 BlockBackend
*target
;
1659 bool target_has_backing
;
1660 int64_t target_backing_sectors
; /* negative if unknown */
1667 size_t cluster_sectors
;
1669 long num_coroutines
;
1670 int running_coroutines
;
1671 Coroutine
*co
[MAX_COROUTINES
];
1672 int64_t wait_sector_num
[MAX_COROUTINES
];
1677 static void convert_select_part(ImgConvertState
*s
, int64_t sector_num
,
1678 int *src_cur
, int64_t *src_cur_offset
)
1681 *src_cur_offset
= 0;
1682 while (sector_num
- *src_cur_offset
>= s
->src_sectors
[*src_cur
]) {
1683 *src_cur_offset
+= s
->src_sectors
[*src_cur
];
1685 assert(*src_cur
< s
->src_num
);
1689 static int convert_iteration_sectors(ImgConvertState
*s
, int64_t sector_num
)
1691 int64_t src_cur_offset
;
1692 int ret
, n
, src_cur
;
1693 bool post_backing_zero
= false;
1695 convert_select_part(s
, sector_num
, &src_cur
, &src_cur_offset
);
1697 assert(s
->total_sectors
> sector_num
);
1698 n
= MIN(s
->total_sectors
- sector_num
, BDRV_REQUEST_MAX_SECTORS
);
1700 if (s
->target_backing_sectors
>= 0) {
1701 if (sector_num
>= s
->target_backing_sectors
) {
1702 post_backing_zero
= true;
1703 } else if (sector_num
+ n
> s
->target_backing_sectors
) {
1704 /* Split requests around target_backing_sectors (because
1705 * starting from there, zeros are handled differently) */
1706 n
= s
->target_backing_sectors
- sector_num
;
1710 if (s
->sector_next_status
<= sector_num
) {
1711 uint64_t offset
= (sector_num
- src_cur_offset
) * BDRV_SECTOR_SIZE
;
1714 BlockDriverState
*src_bs
= blk_bs(s
->src
[src_cur
]);
1715 BlockDriverState
*base
;
1717 if (s
->target_has_backing
) {
1718 base
= bdrv_cow_bs(bdrv_skip_filters(src_bs
));
1724 count
= n
* BDRV_SECTOR_SIZE
;
1726 ret
= bdrv_block_status_above(src_bs
, base
, offset
, count
, &count
,
1733 warn_report("error while reading block status at "
1734 "offset %" PRIu64
": %s", offset
,
1737 /* Just try to read the data, then */
1738 ret
= BDRV_BLOCK_DATA
;
1739 count
= BDRV_SECTOR_SIZE
;
1741 /* Retry on a shorter range */
1742 n
= DIV_ROUND_UP(n
, 4);
1745 error_report("error while reading block status at offset "
1746 "%" PRIu64
": %s", offset
, strerror(-ret
));
1752 n
= DIV_ROUND_UP(count
, BDRV_SECTOR_SIZE
);
1755 * Avoid that s->sector_next_status becomes unaligned to the source
1756 * request alignment and/or cluster size to avoid unnecessary read
1759 tail
= (sector_num
- src_cur_offset
+ n
) % s
->src_alignment
[src_cur
];
1764 if (ret
& BDRV_BLOCK_ZERO
) {
1765 s
->status
= post_backing_zero
? BLK_BACKING_FILE
: BLK_ZERO
;
1766 } else if (ret
& BDRV_BLOCK_DATA
) {
1767 s
->status
= BLK_DATA
;
1769 s
->status
= s
->target_has_backing
? BLK_BACKING_FILE
: BLK_DATA
;
1772 s
->sector_next_status
= sector_num
+ n
;
1775 n
= MIN(n
, s
->sector_next_status
- sector_num
);
1776 if (s
->status
== BLK_DATA
) {
1777 n
= MIN(n
, s
->buf_sectors
);
1780 /* We need to write complete clusters for compressed images, so if an
1781 * unallocated area is shorter than that, we must consider the whole
1782 * cluster allocated. */
1783 if (s
->compressed
) {
1784 if (n
< s
->cluster_sectors
) {
1785 n
= MIN(s
->cluster_sectors
, s
->total_sectors
- sector_num
);
1786 s
->status
= BLK_DATA
;
1788 n
= QEMU_ALIGN_DOWN(n
, s
->cluster_sectors
);
1795 static int coroutine_fn
convert_co_read(ImgConvertState
*s
, int64_t sector_num
,
1796 int nb_sectors
, uint8_t *buf
)
1798 uint64_t single_read_until
= 0;
1801 assert(nb_sectors
<= s
->buf_sectors
);
1802 while (nb_sectors
> 0) {
1805 int64_t bs_sectors
, src_cur_offset
;
1808 /* In the case of compression with multiple source files, we can get a
1809 * nb_sectors that spreads into the next part. So we must be able to
1810 * read across multiple BDSes for one convert_read() call. */
1811 convert_select_part(s
, sector_num
, &src_cur
, &src_cur_offset
);
1812 blk
= s
->src
[src_cur
];
1813 bs_sectors
= s
->src_sectors
[src_cur
];
1815 offset
= (sector_num
- src_cur_offset
) << BDRV_SECTOR_BITS
;
1817 n
= MIN(nb_sectors
, bs_sectors
- (sector_num
- src_cur_offset
));
1818 if (single_read_until
> offset
) {
1822 ret
= blk_co_pread(blk
, offset
, n
<< BDRV_SECTOR_BITS
, buf
, 0);
1826 single_read_until
= offset
+ (n
<< BDRV_SECTOR_BITS
);
1830 warn_report("error while reading offset %" PRIu64
1831 ": %s", offset
, strerror(-ret
));
1833 memset(buf
, 0, BDRV_SECTOR_SIZE
);
1842 buf
+= n
* BDRV_SECTOR_SIZE
;
1849 static int coroutine_fn
convert_co_write(ImgConvertState
*s
, int64_t sector_num
,
1850 int nb_sectors
, uint8_t *buf
,
1851 enum ImgConvertBlockStatus status
)
1855 while (nb_sectors
> 0) {
1857 BdrvRequestFlags flags
= s
->compressed
? BDRV_REQ_WRITE_COMPRESSED
: 0;
1860 case BLK_BACKING_FILE
:
1861 /* If we have a backing file, leave clusters unallocated that are
1862 * unallocated in the source image, so that the backing file is
1863 * visible at the respective offset. */
1864 assert(s
->target_has_backing
);
1868 /* If we're told to keep the target fully allocated (-S 0) or there
1869 * is real non-zero data, we must write it. Otherwise we can treat
1870 * it as zero sectors.
1871 * Compressed clusters need to be written as a whole, so in that
1872 * case we can only save the write if the buffer is completely
1874 if (!s
->min_sparse
||
1876 is_allocated_sectors_min(buf
, n
, &n
, s
->min_sparse
,
1877 sector_num
, s
->alignment
)) ||
1879 !buffer_is_zero(buf
, n
* BDRV_SECTOR_SIZE
)))
1881 ret
= blk_co_pwrite(s
->target
, sector_num
<< BDRV_SECTOR_BITS
,
1882 n
<< BDRV_SECTOR_BITS
, buf
, flags
);
1891 if (s
->has_zero_init
) {
1892 assert(!s
->target_has_backing
);
1895 ret
= blk_co_pwrite_zeroes(s
->target
,
1896 sector_num
<< BDRV_SECTOR_BITS
,
1897 n
<< BDRV_SECTOR_BITS
,
1898 BDRV_REQ_MAY_UNMAP
);
1907 buf
+= n
* BDRV_SECTOR_SIZE
;
1913 static int coroutine_fn
convert_co_copy_range(ImgConvertState
*s
, int64_t sector_num
,
1918 while (nb_sectors
> 0) {
1921 int64_t bs_sectors
, src_cur_offset
;
1924 convert_select_part(s
, sector_num
, &src_cur
, &src_cur_offset
);
1925 offset
= (sector_num
- src_cur_offset
) << BDRV_SECTOR_BITS
;
1926 blk
= s
->src
[src_cur
];
1927 bs_sectors
= s
->src_sectors
[src_cur
];
1929 n
= MIN(nb_sectors
, bs_sectors
- (sector_num
- src_cur_offset
));
1931 ret
= blk_co_copy_range(blk
, offset
, s
->target
,
1932 sector_num
<< BDRV_SECTOR_BITS
,
1933 n
<< BDRV_SECTOR_BITS
, 0, 0);
1944 static void coroutine_fn
convert_co_do_copy(void *opaque
)
1946 ImgConvertState
*s
= opaque
;
1947 uint8_t *buf
= NULL
;
1951 for (i
= 0; i
< s
->num_coroutines
; i
++) {
1952 if (s
->co
[i
] == qemu_coroutine_self()) {
1959 s
->running_coroutines
++;
1960 buf
= blk_blockalign(s
->target
, s
->buf_sectors
* BDRV_SECTOR_SIZE
);
1965 enum ImgConvertBlockStatus status
;
1968 qemu_co_mutex_lock(&s
->lock
);
1969 if (s
->ret
!= -EINPROGRESS
|| s
->sector_num
>= s
->total_sectors
) {
1970 qemu_co_mutex_unlock(&s
->lock
);
1973 n
= convert_iteration_sectors(s
, s
->sector_num
);
1975 qemu_co_mutex_unlock(&s
->lock
);
1979 /* save current sector and allocation status to local variables */
1980 sector_num
= s
->sector_num
;
1982 if (!s
->min_sparse
&& s
->status
== BLK_ZERO
) {
1983 n
= MIN(n
, s
->buf_sectors
);
1985 /* increment global sector counter so that other coroutines can
1986 * already continue reading beyond this request */
1988 qemu_co_mutex_unlock(&s
->lock
);
1990 if (status
== BLK_DATA
|| (!s
->min_sparse
&& status
== BLK_ZERO
)) {
1991 s
->allocated_done
+= n
;
1992 qemu_progress_print(100.0 * s
->allocated_done
/
1993 s
->allocated_sectors
, 0);
1997 copy_range
= s
->copy_range
&& s
->status
== BLK_DATA
;
1998 if (status
== BLK_DATA
&& !copy_range
) {
1999 ret
= convert_co_read(s
, sector_num
, n
, buf
);
2001 error_report("error while reading at byte %lld: %s",
2002 sector_num
* BDRV_SECTOR_SIZE
, strerror(-ret
));
2005 } else if (!s
->min_sparse
&& status
== BLK_ZERO
) {
2007 memset(buf
, 0x00, n
* BDRV_SECTOR_SIZE
);
2010 if (s
->wr_in_order
) {
2011 /* keep writes in order */
2012 while (s
->wr_offs
!= sector_num
&& s
->ret
== -EINPROGRESS
) {
2013 s
->wait_sector_num
[index
] = sector_num
;
2014 qemu_coroutine_yield();
2016 s
->wait_sector_num
[index
] = -1;
2019 if (s
->ret
== -EINPROGRESS
) {
2021 ret
= convert_co_copy_range(s
, sector_num
, n
);
2023 s
->copy_range
= false;
2027 ret
= convert_co_write(s
, sector_num
, n
, buf
, status
);
2030 error_report("error while writing at byte %lld: %s",
2031 sector_num
* BDRV_SECTOR_SIZE
, strerror(-ret
));
2036 if (s
->wr_in_order
) {
2037 /* reenter the coroutine that might have waited
2038 * for this write to complete */
2039 s
->wr_offs
= sector_num
+ n
;
2040 for (i
= 0; i
< s
->num_coroutines
; i
++) {
2041 if (s
->co
[i
] && s
->wait_sector_num
[i
] == s
->wr_offs
) {
2043 * A -> B -> A cannot occur because A has
2044 * s->wait_sector_num[i] == -1 during A -> B. Therefore
2045 * B will never enter A during this time window.
2047 qemu_coroutine_enter(s
->co
[i
]);
2055 s
->co
[index
] = NULL
;
2056 s
->running_coroutines
--;
2057 if (!s
->running_coroutines
&& s
->ret
== -EINPROGRESS
) {
2058 /* the convert job finished successfully */
2063 static int convert_do_copy(ImgConvertState
*s
)
2066 int64_t sector_num
= 0;
2068 /* Check whether we have zero initialisation or can get it efficiently */
2069 if (!s
->has_zero_init
&& s
->target_is_new
&& s
->min_sparse
&&
2070 !s
->target_has_backing
) {
2071 s
->has_zero_init
= bdrv_has_zero_init(blk_bs(s
->target
));
2074 /* Allocate buffer for copied data. For compressed images, only one cluster
2075 * can be copied at a time. */
2076 if (s
->compressed
) {
2077 if (s
->cluster_sectors
<= 0 || s
->cluster_sectors
> s
->buf_sectors
) {
2078 error_report("invalid cluster size");
2081 s
->buf_sectors
= s
->cluster_sectors
;
2084 while (sector_num
< s
->total_sectors
) {
2085 n
= convert_iteration_sectors(s
, sector_num
);
2089 if (s
->status
== BLK_DATA
|| (!s
->min_sparse
&& s
->status
== BLK_ZERO
))
2091 s
->allocated_sectors
+= n
;
2097 s
->sector_next_status
= 0;
2098 s
->ret
= -EINPROGRESS
;
2100 qemu_co_mutex_init(&s
->lock
);
2101 for (i
= 0; i
< s
->num_coroutines
; i
++) {
2102 s
->co
[i
] = qemu_coroutine_create(convert_co_do_copy
, s
);
2103 s
->wait_sector_num
[i
] = -1;
2104 qemu_coroutine_enter(s
->co
[i
]);
2107 while (s
->running_coroutines
) {
2108 main_loop_wait(false);
2111 if (s
->compressed
&& !s
->ret
) {
2112 /* signal EOF to align */
2113 ret
= blk_pwrite_compressed(s
->target
, 0, NULL
, 0);
2122 /* Check that bitmaps can be copied, or output an error */
2123 static int convert_check_bitmaps(BlockDriverState
*src
, bool skip_broken
)
2125 BdrvDirtyBitmap
*bm
;
2127 if (!bdrv_supports_persistent_dirty_bitmap(src
)) {
2128 error_report("Source lacks bitmap support");
2131 FOR_EACH_DIRTY_BITMAP(src
, bm
) {
2132 if (!bdrv_dirty_bitmap_get_persistence(bm
)) {
2135 if (!skip_broken
&& bdrv_dirty_bitmap_inconsistent(bm
)) {
2136 error_report("Cannot copy inconsistent bitmap '%s'",
2137 bdrv_dirty_bitmap_name(bm
));
2138 error_printf("Try --skip-broken-bitmaps, or "
2139 "use 'qemu-img bitmap --remove' to delete it\n");
2146 static int convert_copy_bitmaps(BlockDriverState
*src
, BlockDriverState
*dst
,
2149 BdrvDirtyBitmap
*bm
;
2152 FOR_EACH_DIRTY_BITMAP(src
, bm
) {
2155 if (!bdrv_dirty_bitmap_get_persistence(bm
)) {
2158 name
= bdrv_dirty_bitmap_name(bm
);
2159 if (skip_broken
&& bdrv_dirty_bitmap_inconsistent(bm
)) {
2160 warn_report("Skipping inconsistent bitmap '%s'", name
);
2163 qmp_block_dirty_bitmap_add(dst
->node_name
, name
,
2164 true, bdrv_dirty_bitmap_granularity(bm
),
2166 true, !bdrv_dirty_bitmap_enabled(bm
),
2169 error_reportf_err(err
, "Failed to create bitmap %s: ", name
);
2173 do_dirty_bitmap_merge(dst
->node_name
, name
, src
->node_name
, name
,
2176 error_reportf_err(err
, "Failed to populate bitmap %s: ", name
);
2177 qmp_block_dirty_bitmap_remove(dst
->node_name
, name
, NULL
);
2185 #define MAX_BUF_SECTORS 32768
2187 static void set_rate_limit(BlockBackend
*blk
, int64_t rate_limit
)
2191 throttle_config_init(&cfg
);
2192 cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
= rate_limit
;
2194 blk_io_limits_enable(blk
, CONVERT_THROTTLE_GROUP
);
2195 blk_set_io_limits(blk
, &cfg
);
2198 static int img_convert(int argc
, char **argv
)
2200 int c
, bs_i
, flags
, src_flags
= BDRV_O_NO_SHARE
;
2201 const char *fmt
= NULL
, *out_fmt
= NULL
, *cache
= "unsafe",
2202 *src_cache
= BDRV_DEFAULT_CACHE
, *out_baseimg
= NULL
,
2203 *out_filename
, *out_baseimg_param
, *snapshot_name
= NULL
,
2204 *backing_fmt
= NULL
;
2205 BlockDriver
*drv
= NULL
, *proto_drv
= NULL
;
2206 BlockDriverInfo bdi
;
2207 BlockDriverState
*out_bs
;
2208 QemuOpts
*opts
= NULL
, *sn_opts
= NULL
;
2209 QemuOptsList
*create_opts
= NULL
;
2210 QDict
*open_opts
= NULL
;
2211 char *options
= NULL
;
2212 Error
*local_err
= NULL
;
2213 bool writethrough
, src_writethrough
, image_opts
= false,
2214 skip_create
= false, progress
= false, tgt_image_opts
= false;
2215 int64_t ret
= -EINVAL
;
2216 bool force_share
= false;
2217 bool explict_min_sparse
= false;
2218 bool bitmaps
= false;
2219 bool skip_broken
= false;
2220 int64_t rate_limit
= 0;
2222 ImgConvertState s
= (ImgConvertState
) {
2223 /* Need at least 4k of zeros for sparse detection */
2225 .copy_range
= false,
2226 .buf_sectors
= IO_BUF_SIZE
/ BDRV_SECTOR_SIZE
,
2227 .wr_in_order
= true,
2228 .num_coroutines
= 8,
2232 static const struct option long_options
[] = {
2233 {"help", no_argument
, 0, 'h'},
2234 {"object", required_argument
, 0, OPTION_OBJECT
},
2235 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
2236 {"force-share", no_argument
, 0, 'U'},
2237 {"target-image-opts", no_argument
, 0, OPTION_TARGET_IMAGE_OPTS
},
2238 {"salvage", no_argument
, 0, OPTION_SALVAGE
},
2239 {"target-is-zero", no_argument
, 0, OPTION_TARGET_IS_ZERO
},
2240 {"bitmaps", no_argument
, 0, OPTION_BITMAPS
},
2241 {"skip-broken-bitmaps", no_argument
, 0, OPTION_SKIP_BROKEN
},
2244 c
= getopt_long(argc
, argv
, ":hf:O:B:CcF:o:l:S:pt:T:qnm:WUr:",
2245 long_options
, NULL
);
2251 missing_argument(argv
[optind
- 1]);
2254 unrecognized_option(argv
[optind
- 1]);
2266 out_baseimg
= optarg
;
2269 s
.copy_range
= true;
2272 s
.compressed
= true;
2275 backing_fmt
= optarg
;
2278 if (accumulate_options(&options
, optarg
) < 0) {
2283 if (strstart(optarg
, SNAPSHOT_OPT_BASE
, NULL
)) {
2284 sn_opts
= qemu_opts_parse_noisily(&internal_snapshot_opts
,
2287 error_report("Failed in parsing snapshot param '%s'",
2292 snapshot_name
= optarg
;
2299 sval
= cvtnum("buffer size for sparse output", optarg
);
2302 } else if (!QEMU_IS_ALIGNED(sval
, BDRV_SECTOR_SIZE
) ||
2303 sval
/ BDRV_SECTOR_SIZE
> MAX_BUF_SECTORS
) {
2304 error_report("Invalid buffer size for sparse output specified. "
2305 "Valid sizes are multiples of %llu up to %llu. Select "
2306 "0 to disable sparse detection (fully allocates output).",
2307 BDRV_SECTOR_SIZE
, MAX_BUF_SECTORS
* BDRV_SECTOR_SIZE
);
2311 s
.min_sparse
= sval
/ BDRV_SECTOR_SIZE
;
2312 explict_min_sparse
= true;
2331 if (qemu_strtol(optarg
, NULL
, 0, &s
.num_coroutines
) ||
2332 s
.num_coroutines
< 1 || s
.num_coroutines
> MAX_COROUTINES
) {
2333 error_report("Invalid number of coroutines. Allowed number of"
2334 " coroutines is between 1 and %d", MAX_COROUTINES
);
2339 s
.wr_in_order
= false;
2345 rate_limit
= cvtnum("rate limit", optarg
);
2346 if (rate_limit
< 0) {
2351 user_creatable_process_cmdline(optarg
);
2353 case OPTION_IMAGE_OPTS
:
2356 case OPTION_SALVAGE
:
2359 case OPTION_TARGET_IMAGE_OPTS
:
2360 tgt_image_opts
= true;
2362 case OPTION_TARGET_IS_ZERO
:
2364 * The user asserting that the target is blank has the
2365 * same effect as the target driver supporting zero
2368 s
.has_zero_init
= true;
2370 case OPTION_BITMAPS
:
2373 case OPTION_SKIP_BROKEN
:
2379 if (!out_fmt
&& !tgt_image_opts
) {
2383 if (skip_broken
&& !bitmaps
) {
2384 error_report("Use of --skip-broken-bitmaps requires --bitmaps");
2388 if (s
.compressed
&& s
.copy_range
) {
2389 error_report("Cannot enable copy offloading when -c is used");
2393 if (explict_min_sparse
&& s
.copy_range
) {
2394 error_report("Cannot enable copy offloading when -S is used");
2398 if (s
.copy_range
&& s
.salvage
) {
2399 error_report("Cannot use copy offloading in salvaging mode");
2403 if (tgt_image_opts
&& !skip_create
) {
2404 error_report("--target-image-opts requires use of -n flag");
2408 if (skip_create
&& options
) {
2409 error_report("-o has no effect when skipping image creation");
2413 if (s
.has_zero_init
&& !skip_create
) {
2414 error_report("--target-is-zero requires use of -n flag");
2418 s
.src_num
= argc
- optind
- 1;
2419 out_filename
= s
.src_num
>= 1 ? argv
[argc
- 1] : NULL
;
2421 if (options
&& has_help_option(options
)) {
2423 ret
= print_block_option_help(out_filename
, out_fmt
);
2426 error_report("Option help requires a format be specified");
2431 if (s
.src_num
< 1) {
2432 error_report("Must specify image file name");
2436 /* ret is still -EINVAL until here */
2437 ret
= bdrv_parse_cache_mode(src_cache
, &src_flags
, &src_writethrough
);
2439 error_report("Invalid source cache option: %s", src_cache
);
2443 /* Initialize before goto out */
2447 qemu_progress_init(progress
, 1.0);
2448 qemu_progress_print(0, 100);
2450 s
.src
= g_new0(BlockBackend
*, s
.src_num
);
2451 s
.src_sectors
= g_new(int64_t, s
.src_num
);
2452 s
.src_alignment
= g_new(int, s
.src_num
);
2454 for (bs_i
= 0; bs_i
< s
.src_num
; bs_i
++) {
2455 BlockDriverState
*src_bs
;
2456 s
.src
[bs_i
] = img_open(image_opts
, argv
[optind
+ bs_i
],
2457 fmt
, src_flags
, src_writethrough
, s
.quiet
,
2463 s
.src_sectors
[bs_i
] = blk_nb_sectors(s
.src
[bs_i
]);
2464 if (s
.src_sectors
[bs_i
] < 0) {
2465 error_report("Could not get size of %s: %s",
2466 argv
[optind
+ bs_i
], strerror(-s
.src_sectors
[bs_i
]));
2470 src_bs
= blk_bs(s
.src
[bs_i
]);
2471 s
.src_alignment
[bs_i
] = DIV_ROUND_UP(src_bs
->bl
.request_alignment
,
2473 if (!bdrv_get_info(src_bs
, &bdi
)) {
2474 s
.src_alignment
[bs_i
] = MAX(s
.src_alignment
[bs_i
],
2475 bdi
.cluster_size
/ BDRV_SECTOR_SIZE
);
2477 s
.total_sectors
+= s
.src_sectors
[bs_i
];
2481 bdrv_snapshot_load_tmp(blk_bs(s
.src
[0]),
2482 qemu_opt_get(sn_opts
, SNAPSHOT_OPT_ID
),
2483 qemu_opt_get(sn_opts
, SNAPSHOT_OPT_NAME
),
2485 } else if (snapshot_name
!= NULL
) {
2486 if (s
.src_num
> 1) {
2487 error_report("No support for concatenating multiple snapshot");
2492 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s
.src
[0]), snapshot_name
,
2496 error_reportf_err(local_err
, "Failed to load snapshot: ");
2502 /* Find driver and parse its options */
2503 drv
= bdrv_find_format(out_fmt
);
2505 error_report("Unknown file format '%s'", out_fmt
);
2510 proto_drv
= bdrv_find_protocol(out_filename
, true, &local_err
);
2512 error_report_err(local_err
);
2517 if (!drv
->create_opts
) {
2518 error_report("Format driver '%s' does not support image creation",
2524 if (!proto_drv
->create_opts
) {
2525 error_report("Protocol driver '%s' does not support image creation",
2526 proto_drv
->format_name
);
2531 create_opts
= qemu_opts_append(create_opts
, drv
->create_opts
);
2532 create_opts
= qemu_opts_append(create_opts
, proto_drv
->create_opts
);
2534 opts
= qemu_opts_create(create_opts
, NULL
, 0, &error_abort
);
2536 if (!qemu_opts_do_parse(opts
, options
, NULL
, &local_err
)) {
2537 error_report_err(local_err
);
2543 qemu_opt_set_number(opts
, BLOCK_OPT_SIZE
,
2544 s
.total_sectors
* BDRV_SECTOR_SIZE
, &error_abort
);
2545 ret
= add_old_style_options(out_fmt
, opts
, out_baseimg
, backing_fmt
);
2551 /* Get backing file name if -o backing_file was used */
2552 out_baseimg_param
= qemu_opt_get(opts
, BLOCK_OPT_BACKING_FILE
);
2553 if (out_baseimg_param
) {
2554 out_baseimg
= out_baseimg_param
;
2556 s
.target_has_backing
= (bool) out_baseimg
;
2558 if (s
.has_zero_init
&& s
.target_has_backing
) {
2559 error_report("Cannot use --target-is-zero when the destination "
2560 "image has a backing file");
2564 if (s
.src_num
> 1 && out_baseimg
) {
2565 error_report("Having a backing file for the target makes no sense when "
2566 "concatenating multiple input images");
2571 if (out_baseimg_param
) {
2572 if (!qemu_opt_get(opts
, BLOCK_OPT_BACKING_FMT
)) {
2573 error_report("Use of backing file requires explicit "
2580 /* Check if compression is supported */
2583 qemu_opt_get_bool(opts
, BLOCK_OPT_ENCRYPT
, false);
2584 const char *encryptfmt
=
2585 qemu_opt_get(opts
, BLOCK_OPT_ENCRYPT_FORMAT
);
2586 const char *preallocation
=
2587 qemu_opt_get(opts
, BLOCK_OPT_PREALLOC
);
2589 if (drv
&& !block_driver_can_compress(drv
)) {
2590 error_report("Compression not supported for this file format");
2595 if (encryption
|| encryptfmt
) {
2596 error_report("Compression and encryption not supported at "
2603 && strcmp(preallocation
, "off"))
2605 error_report("Compression and preallocation not supported at "
2612 /* Determine if bitmaps need copying */
2614 if (s
.src_num
> 1) {
2615 error_report("Copying bitmaps only possible with single source");
2619 ret
= convert_check_bitmaps(blk_bs(s
.src
[0]), skip_broken
);
2626 * The later open call will need any decryption secrets, and
2627 * bdrv_create() will purge "opts", so extract them now before
2631 open_opts
= qdict_new();
2632 qemu_opt_foreach(opts
, img_add_key_secrets
, open_opts
, &error_abort
);
2634 /* Create the new image */
2635 ret
= bdrv_create(drv
, out_filename
, opts
, &local_err
);
2637 error_reportf_err(local_err
, "%s: error while converting %s: ",
2638 out_filename
, out_fmt
);
2643 s
.target_is_new
= !skip_create
;
2645 flags
= s
.min_sparse
? (BDRV_O_RDWR
| BDRV_O_UNMAP
) : BDRV_O_RDWR
;
2646 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
2648 error_report("Invalid cache option: %s", cache
);
2652 if (flags
& BDRV_O_NOCACHE
) {
2654 * If we open the target with O_DIRECT, it may be necessary to
2655 * extend its size to align to the physical sector size.
2657 flags
|= BDRV_O_RESIZE
;
2661 s
.target
= img_open(tgt_image_opts
, out_filename
, out_fmt
,
2662 flags
, writethrough
, s
.quiet
, false);
2664 /* TODO ultimately we should allow --target-image-opts
2665 * to be used even when -n is not given.
2666 * That has to wait for bdrv_create to be improved
2667 * to allow filenames in option syntax
2669 s
.target
= img_open_file(out_filename
, open_opts
, out_fmt
,
2670 flags
, writethrough
, s
.quiet
, false);
2671 open_opts
= NULL
; /* blk_new_open will have freed it */
2677 out_bs
= blk_bs(s
.target
);
2679 if (bitmaps
&& !bdrv_supports_persistent_dirty_bitmap(out_bs
)) {
2680 error_report("Format driver '%s' does not support bitmaps",
2681 out_bs
->drv
->format_name
);
2686 if (s
.compressed
&& !block_driver_can_compress(out_bs
->drv
)) {
2687 error_report("Compression not supported for this file format");
2692 /* increase bufsectors from the default 4096 (2M) if opt_transfer
2693 * or discard_alignment of the out_bs is greater. Limit to
2694 * MAX_BUF_SECTORS as maximum which is currently 32768 (16MB). */
2695 s
.buf_sectors
= MIN(MAX_BUF_SECTORS
,
2697 MAX(out_bs
->bl
.opt_transfer
>> BDRV_SECTOR_BITS
,
2698 out_bs
->bl
.pdiscard_alignment
>>
2699 BDRV_SECTOR_BITS
)));
2701 /* try to align the write requests to the destination to avoid unnecessary
2703 s
.alignment
= MAX(pow2floor(s
.min_sparse
),
2704 DIV_ROUND_UP(out_bs
->bl
.request_alignment
,
2706 assert(is_power_of_2(s
.alignment
));
2709 int64_t output_sectors
= blk_nb_sectors(s
.target
);
2710 if (output_sectors
< 0) {
2711 error_report("unable to get output image length: %s",
2712 strerror(-output_sectors
));
2715 } else if (output_sectors
< s
.total_sectors
) {
2716 error_report("output file is smaller than input file");
2722 if (s
.target_has_backing
&& s
.target_is_new
) {
2723 /* Errors are treated as "backing length unknown" (which means
2724 * s.target_backing_sectors has to be negative, which it will
2725 * be automatically). The backing file length is used only
2726 * for optimizations, so such a case is not fatal. */
2727 s
.target_backing_sectors
=
2728 bdrv_nb_sectors(bdrv_backing_chain_next(out_bs
));
2730 s
.target_backing_sectors
= -1;
2733 ret
= bdrv_get_info(out_bs
, &bdi
);
2736 error_report("could not get block driver info");
2740 s
.compressed
= s
.compressed
|| bdi
.needs_compressed_writes
;
2741 s
.cluster_sectors
= bdi
.cluster_size
/ BDRV_SECTOR_SIZE
;
2745 set_rate_limit(s
.target
, rate_limit
);
2748 ret
= convert_do_copy(&s
);
2750 /* Now copy the bitmaps */
2751 if (bitmaps
&& ret
== 0) {
2752 ret
= convert_copy_bitmaps(blk_bs(s
.src
[0]), out_bs
, skip_broken
);
2757 qemu_progress_print(100, 0);
2759 qemu_progress_end();
2760 qemu_opts_del(opts
);
2761 qemu_opts_free(create_opts
);
2762 qobject_unref(open_opts
);
2763 blk_unref(s
.target
);
2765 for (bs_i
= 0; bs_i
< s
.src_num
; bs_i
++) {
2766 blk_unref(s
.src
[bs_i
]);
2770 g_free(s
.src_sectors
);
2771 g_free(s
.src_alignment
);
2773 qemu_opts_del(sn_opts
);
2780 static void dump_snapshots(BlockDriverState
*bs
)
2782 QEMUSnapshotInfo
*sn_tab
, *sn
;
2785 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
2788 printf("Snapshot list:\n");
2789 bdrv_snapshot_dump(NULL
);
2791 for(i
= 0; i
< nb_sns
; i
++) {
2793 bdrv_snapshot_dump(sn
);
2799 static void dump_json_image_info_list(ImageInfoList
*list
)
2803 Visitor
*v
= qobject_output_visitor_new(&obj
);
2805 visit_type_ImageInfoList(v
, NULL
, &list
, &error_abort
);
2806 visit_complete(v
, &obj
);
2807 str
= qobject_to_json_pretty(obj
, true);
2808 assert(str
!= NULL
);
2809 printf("%s\n", str
->str
);
2812 g_string_free(str
, true);
2815 static void dump_json_image_info(ImageInfo
*info
)
2819 Visitor
*v
= qobject_output_visitor_new(&obj
);
2821 visit_type_ImageInfo(v
, NULL
, &info
, &error_abort
);
2822 visit_complete(v
, &obj
);
2823 str
= qobject_to_json_pretty(obj
, true);
2824 assert(str
!= NULL
);
2825 printf("%s\n", str
->str
);
2828 g_string_free(str
, true);
2831 static void dump_human_image_info_list(ImageInfoList
*list
)
2833 ImageInfoList
*elem
;
2836 for (elem
= list
; elem
; elem
= elem
->next
) {
2842 bdrv_image_info_dump(elem
->value
);
2846 static gboolean
str_equal_func(gconstpointer a
, gconstpointer b
)
2848 return strcmp(a
, b
) == 0;
2852 * Open an image file chain and return an ImageInfoList
2854 * @filename: topmost image filename
2855 * @fmt: topmost image format (may be NULL to autodetect)
2856 * @chain: true - enumerate entire backing file chain
2857 * false - only topmost image file
2859 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2860 * image file. If there was an error a message will have been printed to
2863 static ImageInfoList
*collect_image_info_list(bool image_opts
,
2864 const char *filename
,
2866 bool chain
, bool force_share
)
2868 ImageInfoList
*head
= NULL
;
2869 ImageInfoList
**tail
= &head
;
2870 GHashTable
*filenames
;
2873 filenames
= g_hash_table_new_full(g_str_hash
, str_equal_func
, NULL
, NULL
);
2877 BlockDriverState
*bs
;
2880 if (g_hash_table_lookup_extended(filenames
, filename
, NULL
, NULL
)) {
2881 error_report("Backing file '%s' creates an infinite loop.",
2885 g_hash_table_insert(filenames
, (gpointer
)filename
, NULL
);
2887 blk
= img_open(image_opts
, filename
, fmt
,
2888 BDRV_O_NO_BACKING
| BDRV_O_NO_IO
, false, false,
2895 bdrv_query_image_info(bs
, &info
, &err
);
2897 error_report_err(err
);
2902 QAPI_LIST_APPEND(tail
, info
);
2906 /* Clear parameters that only apply to the topmost image */
2907 filename
= fmt
= NULL
;
2911 if (info
->has_full_backing_filename
) {
2912 filename
= info
->full_backing_filename
;
2913 } else if (info
->has_backing_filename
) {
2914 error_report("Could not determine absolute backing filename,"
2915 " but backing filename '%s' present",
2916 info
->backing_filename
);
2919 if (info
->has_backing_filename_format
) {
2920 fmt
= info
->backing_filename_format
;
2924 g_hash_table_destroy(filenames
);
2928 qapi_free_ImageInfoList(head
);
2929 g_hash_table_destroy(filenames
);
2933 static int img_info(int argc
, char **argv
)
2936 OutputFormat output_format
= OFORMAT_HUMAN
;
2938 const char *filename
, *fmt
, *output
;
2939 ImageInfoList
*list
;
2940 bool image_opts
= false;
2941 bool force_share
= false;
2946 int option_index
= 0;
2947 static const struct option long_options
[] = {
2948 {"help", no_argument
, 0, 'h'},
2949 {"format", required_argument
, 0, 'f'},
2950 {"output", required_argument
, 0, OPTION_OUTPUT
},
2951 {"backing-chain", no_argument
, 0, OPTION_BACKING_CHAIN
},
2952 {"object", required_argument
, 0, OPTION_OBJECT
},
2953 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
2954 {"force-share", no_argument
, 0, 'U'},
2957 c
= getopt_long(argc
, argv
, ":f:hU",
2958 long_options
, &option_index
);
2964 missing_argument(argv
[optind
- 1]);
2967 unrecognized_option(argv
[optind
- 1]);
2981 case OPTION_BACKING_CHAIN
:
2985 user_creatable_process_cmdline(optarg
);
2987 case OPTION_IMAGE_OPTS
:
2992 if (optind
!= argc
- 1) {
2993 error_exit("Expecting one image file name");
2995 filename
= argv
[optind
++];
2997 if (output
&& !strcmp(output
, "json")) {
2998 output_format
= OFORMAT_JSON
;
2999 } else if (output
&& !strcmp(output
, "human")) {
3000 output_format
= OFORMAT_HUMAN
;
3001 } else if (output
) {
3002 error_report("--output must be used with human or json as argument.");
3006 list
= collect_image_info_list(image_opts
, filename
, fmt
, chain
,
3012 switch (output_format
) {
3014 dump_human_image_info_list(list
);
3018 dump_json_image_info_list(list
);
3020 dump_json_image_info(list
->value
);
3025 qapi_free_ImageInfoList(list
);
3029 static int dump_map_entry(OutputFormat output_format
, MapEntry
*e
,
3032 switch (output_format
) {
3034 if (e
->data
&& !e
->has_offset
) {
3035 error_report("File contains external, encrypted or compressed clusters.");
3038 if (e
->data
&& !e
->zero
) {
3039 printf("%#-16"PRIx64
"%#-16"PRIx64
"%#-16"PRIx64
"%s\n",
3040 e
->start
, e
->length
,
3041 e
->has_offset
? e
->offset
: 0,
3042 e
->has_filename
? e
->filename
: "");
3044 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
3045 * Modify the flags here to allow more coalescing.
3047 if (next
&& (!next
->data
|| next
->zero
)) {
3053 printf("{ \"start\": %"PRId64
", \"length\": %"PRId64
","
3054 " \"depth\": %"PRId64
", \"present\": %s, \"zero\": %s,"
3055 " \"data\": %s", e
->start
, e
->length
, e
->depth
,
3056 e
->present
? "true" : "false",
3057 e
->zero
? "true" : "false",
3058 e
->data
? "true" : "false");
3059 if (e
->has_offset
) {
3060 printf(", \"offset\": %"PRId64
"", e
->offset
);
3072 static int get_block_status(BlockDriverState
*bs
, int64_t offset
,
3073 int64_t bytes
, MapEntry
*e
)
3077 BlockDriverState
*file
;
3080 char *filename
= NULL
;
3082 /* As an optimization, we could cache the current range of unallocated
3083 * clusters in each file of the chain, and avoid querying the same
3089 bs
= bdrv_skip_filters(bs
);
3090 ret
= bdrv_block_status(bs
, offset
, bytes
, &bytes
, &map
, &file
);
3095 if (ret
& (BDRV_BLOCK_ZERO
|BDRV_BLOCK_DATA
)) {
3098 bs
= bdrv_cow_bs(bs
);
3107 has_offset
= !!(ret
& BDRV_BLOCK_OFFSET_VALID
);
3109 if (file
&& has_offset
) {
3110 bdrv_refresh_filename(file
);
3111 filename
= file
->filename
;
3117 .data
= !!(ret
& BDRV_BLOCK_DATA
),
3118 .zero
= !!(ret
& BDRV_BLOCK_ZERO
),
3120 .has_offset
= has_offset
,
3122 .present
= !!(ret
& BDRV_BLOCK_ALLOCATED
),
3123 .has_filename
= filename
,
3124 .filename
= filename
,
3130 static inline bool entry_mergeable(const MapEntry
*curr
, const MapEntry
*next
)
3132 if (curr
->length
== 0) {
3135 if (curr
->zero
!= next
->zero
||
3136 curr
->data
!= next
->data
||
3137 curr
->depth
!= next
->depth
||
3138 curr
->present
!= next
->present
||
3139 curr
->has_filename
!= next
->has_filename
||
3140 curr
->has_offset
!= next
->has_offset
) {
3143 if (curr
->has_filename
&& strcmp(curr
->filename
, next
->filename
)) {
3146 if (curr
->has_offset
&& curr
->offset
+ curr
->length
!= next
->offset
) {
3152 static int img_map(int argc
, char **argv
)
3155 OutputFormat output_format
= OFORMAT_HUMAN
;
3157 BlockDriverState
*bs
;
3158 const char *filename
, *fmt
, *output
;
3160 MapEntry curr
= { .length
= 0 }, next
;
3162 bool image_opts
= false;
3163 bool force_share
= false;
3164 int64_t start_offset
= 0;
3165 int64_t max_length
= -1;
3170 int option_index
= 0;
3171 static const struct option long_options
[] = {
3172 {"help", no_argument
, 0, 'h'},
3173 {"format", required_argument
, 0, 'f'},
3174 {"output", required_argument
, 0, OPTION_OUTPUT
},
3175 {"object", required_argument
, 0, OPTION_OBJECT
},
3176 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
3177 {"force-share", no_argument
, 0, 'U'},
3178 {"start-offset", required_argument
, 0, 's'},
3179 {"max-length", required_argument
, 0, 'l'},
3182 c
= getopt_long(argc
, argv
, ":f:s:l:hU",
3183 long_options
, &option_index
);
3189 missing_argument(argv
[optind
- 1]);
3192 unrecognized_option(argv
[optind
- 1]);
3207 start_offset
= cvtnum("start offset", optarg
);
3208 if (start_offset
< 0) {
3213 max_length
= cvtnum("max length", optarg
);
3214 if (max_length
< 0) {
3219 user_creatable_process_cmdline(optarg
);
3221 case OPTION_IMAGE_OPTS
:
3226 if (optind
!= argc
- 1) {
3227 error_exit("Expecting one image file name");
3229 filename
= argv
[optind
];
3231 if (output
&& !strcmp(output
, "json")) {
3232 output_format
= OFORMAT_JSON
;
3233 } else if (output
&& !strcmp(output
, "human")) {
3234 output_format
= OFORMAT_HUMAN
;
3235 } else if (output
) {
3236 error_report("--output must be used with human or json as argument.");
3240 blk
= img_open(image_opts
, filename
, fmt
, 0, false, false, force_share
);
3246 if (output_format
== OFORMAT_HUMAN
) {
3247 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
3248 } else if (output_format
== OFORMAT_JSON
) {
3252 length
= blk_getlength(blk
);
3254 error_report("Failed to get size for '%s'", filename
);
3257 if (max_length
!= -1) {
3258 length
= MIN(start_offset
+ max_length
, length
);
3261 curr
.start
= start_offset
;
3262 while (curr
.start
+ curr
.length
< length
) {
3263 int64_t offset
= curr
.start
+ curr
.length
;
3264 int64_t n
= length
- offset
;
3266 ret
= get_block_status(bs
, offset
, n
, &next
);
3268 error_report("Could not read file metadata: %s", strerror(-ret
));
3272 if (entry_mergeable(&curr
, &next
)) {
3273 curr
.length
+= next
.length
;
3277 if (curr
.length
> 0) {
3278 ret
= dump_map_entry(output_format
, &curr
, &next
);
3286 ret
= dump_map_entry(output_format
, &curr
, NULL
);
3287 if (output_format
== OFORMAT_JSON
) {
3296 #define SNAPSHOT_LIST 1
3297 #define SNAPSHOT_CREATE 2
3298 #define SNAPSHOT_APPLY 3
3299 #define SNAPSHOT_DELETE 4
3301 static int img_snapshot(int argc
, char **argv
)
3304 BlockDriverState
*bs
;
3305 QEMUSnapshotInfo sn
;
3306 char *filename
, *snapshot_name
= NULL
;
3307 int c
, ret
= 0, bdrv_oflags
;
3311 bool image_opts
= false;
3312 bool force_share
= false;
3315 bdrv_oflags
= BDRV_O_RDWR
;
3316 /* Parse commandline parameters */
3318 static const struct option long_options
[] = {
3319 {"help", no_argument
, 0, 'h'},
3320 {"object", required_argument
, 0, OPTION_OBJECT
},
3321 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
3322 {"force-share", no_argument
, 0, 'U'},
3325 c
= getopt_long(argc
, argv
, ":la:c:d:hqU",
3326 long_options
, NULL
);
3332 missing_argument(argv
[optind
- 1]);
3335 unrecognized_option(argv
[optind
- 1]);
3342 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
3345 action
= SNAPSHOT_LIST
;
3346 bdrv_oflags
&= ~BDRV_O_RDWR
; /* no need for RW */
3350 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
3353 action
= SNAPSHOT_APPLY
;
3354 snapshot_name
= optarg
;
3358 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
3361 action
= SNAPSHOT_CREATE
;
3362 snapshot_name
= optarg
;
3366 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
3369 action
= SNAPSHOT_DELETE
;
3370 snapshot_name
= optarg
;
3379 user_creatable_process_cmdline(optarg
);
3381 case OPTION_IMAGE_OPTS
:
3387 if (optind
!= argc
- 1) {
3388 error_exit("Expecting one image file name");
3390 filename
= argv
[optind
++];
3392 /* Open the image */
3393 blk
= img_open(image_opts
, filename
, NULL
, bdrv_oflags
, false, quiet
,
3400 /* Perform the requested action */
3406 case SNAPSHOT_CREATE
:
3407 memset(&sn
, 0, sizeof(sn
));
3408 pstrcpy(sn
.name
, sizeof(sn
.name
), snapshot_name
);
3410 rt
= g_get_real_time();
3411 sn
.date_sec
= rt
/ G_USEC_PER_SEC
;
3412 sn
.date_nsec
= (rt
% G_USEC_PER_SEC
) * 1000;
3414 ret
= bdrv_snapshot_create(bs
, &sn
);
3416 error_report("Could not create snapshot '%s': %d (%s)",
3417 snapshot_name
, ret
, strerror(-ret
));
3421 case SNAPSHOT_APPLY
:
3422 ret
= bdrv_snapshot_goto(bs
, snapshot_name
, &err
);
3424 error_reportf_err(err
, "Could not apply snapshot '%s': ",
3429 case SNAPSHOT_DELETE
:
3430 ret
= bdrv_snapshot_find(bs
, &sn
, snapshot_name
);
3432 error_report("Could not delete snapshot '%s': snapshot not "
3433 "found", snapshot_name
);
3436 ret
= bdrv_snapshot_delete(bs
, sn
.id_str
, sn
.name
, &err
);
3438 error_reportf_err(err
, "Could not delete snapshot '%s': ",
3454 static int img_rebase(int argc
, char **argv
)
3456 BlockBackend
*blk
= NULL
, *blk_old_backing
= NULL
, *blk_new_backing
= NULL
;
3457 uint8_t *buf_old
= NULL
;
3458 uint8_t *buf_new
= NULL
;
3459 BlockDriverState
*bs
= NULL
, *prefix_chain_bs
= NULL
;
3460 BlockDriverState
*unfiltered_bs
;
3462 const char *fmt
, *cache
, *src_cache
, *out_basefmt
, *out_baseimg
;
3463 int c
, flags
, src_flags
, ret
;
3464 bool writethrough
, src_writethrough
;
3466 bool force_share
= false;
3469 Error
*local_err
= NULL
;
3470 bool image_opts
= false;
3472 /* Parse commandline parameters */
3474 cache
= BDRV_DEFAULT_CACHE
;
3475 src_cache
= BDRV_DEFAULT_CACHE
;
3479 static const struct option long_options
[] = {
3480 {"help", no_argument
, 0, 'h'},
3481 {"object", required_argument
, 0, OPTION_OBJECT
},
3482 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
3483 {"force-share", no_argument
, 0, 'U'},
3486 c
= getopt_long(argc
, argv
, ":hf:F:b:upt:T:qU",
3487 long_options
, NULL
);
3493 missing_argument(argv
[optind
- 1]);
3496 unrecognized_option(argv
[optind
- 1]);
3505 out_basefmt
= optarg
;
3508 out_baseimg
= optarg
;
3526 user_creatable_process_cmdline(optarg
);
3528 case OPTION_IMAGE_OPTS
:
3541 if (optind
!= argc
- 1) {
3542 error_exit("Expecting one image file name");
3544 if (!unsafe
&& !out_baseimg
) {
3545 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
3547 filename
= argv
[optind
++];
3549 qemu_progress_init(progress
, 2.0);
3550 qemu_progress_print(0, 100);
3552 flags
= BDRV_O_RDWR
| (unsafe
? BDRV_O_NO_BACKING
: 0);
3553 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
3555 error_report("Invalid cache option: %s", cache
);
3560 ret
= bdrv_parse_cache_mode(src_cache
, &src_flags
, &src_writethrough
);
3562 error_report("Invalid source cache option: %s", src_cache
);
3566 /* The source files are opened read-only, don't care about WCE */
3567 assert((src_flags
& BDRV_O_RDWR
) == 0);
3568 (void) src_writethrough
;
3573 * Ignore the old backing file for unsafe rebase in case we want to correct
3574 * the reference to a renamed or moved backing file.
3576 blk
= img_open(image_opts
, filename
, fmt
, flags
, writethrough
, quiet
,
3584 unfiltered_bs
= bdrv_skip_filters(bs
);
3586 if (out_basefmt
!= NULL
) {
3587 if (bdrv_find_format(out_basefmt
) == NULL
) {
3588 error_report("Invalid format name: '%s'", out_basefmt
);
3594 /* For safe rebasing we need to compare old and new backing file */
3596 QDict
*options
= NULL
;
3597 BlockDriverState
*base_bs
= bdrv_cow_bs(unfiltered_bs
);
3600 blk_old_backing
= blk_new(qemu_get_aio_context(),
3601 BLK_PERM_CONSISTENT_READ
,
3603 ret
= blk_insert_bs(blk_old_backing
, base_bs
,
3606 error_reportf_err(local_err
,
3607 "Could not reuse old backing file '%s': ",
3612 blk_old_backing
= NULL
;
3615 if (out_baseimg
[0]) {
3616 const char *overlay_filename
;
3617 char *out_real_path
;
3619 options
= qdict_new();
3621 qdict_put_str(options
, "driver", out_basefmt
);
3624 qdict_put_bool(options
, BDRV_OPT_FORCE_SHARE
, true);
3627 bdrv_refresh_filename(bs
);
3628 overlay_filename
= bs
->exact_filename
[0] ? bs
->exact_filename
3631 bdrv_get_full_backing_filename_from_filename(overlay_filename
,
3635 qobject_unref(options
);
3636 error_reportf_err(local_err
,
3637 "Could not resolve backing filename: ");
3643 * Find out whether we rebase an image on top of a previous image
3646 prefix_chain_bs
= bdrv_find_backing_image(bs
, out_real_path
);
3647 if (prefix_chain_bs
) {
3648 qobject_unref(options
);
3649 g_free(out_real_path
);
3651 blk_new_backing
= blk_new(qemu_get_aio_context(),
3652 BLK_PERM_CONSISTENT_READ
,
3654 ret
= blk_insert_bs(blk_new_backing
, prefix_chain_bs
,
3657 error_reportf_err(local_err
,
3658 "Could not reuse backing file '%s': ",
3663 blk_new_backing
= blk_new_open(out_real_path
, NULL
,
3664 options
, src_flags
, &local_err
);
3665 g_free(out_real_path
);
3666 if (!blk_new_backing
) {
3667 error_reportf_err(local_err
,
3668 "Could not open new backing file '%s': ",
3678 * Check each unallocated cluster in the COW file. If it is unallocated,
3679 * accesses go to the backing file. We must therefore compare this cluster
3680 * in the old and new backing file, and if they differ we need to copy it
3681 * from the old backing file into the COW file.
3683 * If qemu-img crashes during this step, no harm is done. The content of
3684 * the image is the same as the original one at any time.
3688 int64_t old_backing_size
= 0;
3689 int64_t new_backing_size
= 0;
3692 float local_progress
= 0;
3694 buf_old
= blk_blockalign(blk
, IO_BUF_SIZE
);
3695 buf_new
= blk_blockalign(blk
, IO_BUF_SIZE
);
3697 size
= blk_getlength(blk
);
3699 error_report("Could not get size of '%s': %s",
3700 filename
, strerror(-size
));
3704 if (blk_old_backing
) {
3705 old_backing_size
= blk_getlength(blk_old_backing
);
3706 if (old_backing_size
< 0) {
3707 char backing_name
[PATH_MAX
];
3709 bdrv_get_backing_filename(bs
, backing_name
,
3710 sizeof(backing_name
));
3711 error_report("Could not get size of '%s': %s",
3712 backing_name
, strerror(-old_backing_size
));
3717 if (blk_new_backing
) {
3718 new_backing_size
= blk_getlength(blk_new_backing
);
3719 if (new_backing_size
< 0) {
3720 error_report("Could not get size of '%s': %s",
3721 out_baseimg
, strerror(-new_backing_size
));
3728 local_progress
= (float)100 / (size
/ MIN(size
, IO_BUF_SIZE
));
3731 for (offset
= 0; offset
< size
; offset
+= n
) {
3732 bool buf_old_is_zero
= false;
3734 /* How many bytes can we handle with the next read? */
3735 n
= MIN(IO_BUF_SIZE
, size
- offset
);
3737 /* If the cluster is allocated, we don't need to take action */
3738 ret
= bdrv_is_allocated(unfiltered_bs
, offset
, n
, &n
);
3740 error_report("error while reading image metadata: %s",
3748 if (prefix_chain_bs
) {
3750 * If cluster wasn't changed since prefix_chain, we don't need
3753 ret
= bdrv_is_allocated_above(bdrv_cow_bs(unfiltered_bs
),
3754 prefix_chain_bs
, false,
3757 error_report("error while reading image metadata: %s",
3767 * Read old and new backing file and take into consideration that
3768 * backing files may be smaller than the COW image.
3770 if (offset
>= old_backing_size
) {
3771 memset(buf_old
, 0, n
);
3772 buf_old_is_zero
= true;
3774 if (offset
+ n
> old_backing_size
) {
3775 n
= old_backing_size
- offset
;
3778 ret
= blk_pread(blk_old_backing
, offset
, buf_old
, n
);
3780 error_report("error while reading from old backing file");
3785 if (offset
>= new_backing_size
|| !blk_new_backing
) {
3786 memset(buf_new
, 0, n
);
3788 if (offset
+ n
> new_backing_size
) {
3789 n
= new_backing_size
- offset
;
3792 ret
= blk_pread(blk_new_backing
, offset
, buf_new
, n
);
3794 error_report("error while reading from new backing file");
3799 /* If they differ, we need to write to the COW file */
3800 uint64_t written
= 0;
3802 while (written
< n
) {
3805 if (compare_buffers(buf_old
+ written
, buf_new
+ written
,
3806 n
- written
, &pnum
))
3808 if (buf_old_is_zero
) {
3809 ret
= blk_pwrite_zeroes(blk
, offset
+ written
, pnum
, 0);
3811 ret
= blk_pwrite(blk
, offset
+ written
,
3812 buf_old
+ written
, pnum
, 0);
3815 error_report("Error while writing to COW image: %s",
3823 qemu_progress_print(local_progress
, 100);
3828 * Change the backing file. All clusters that are different from the old
3829 * backing file are overwritten in the COW file now, so the visible content
3830 * doesn't change when we switch the backing file.
3832 if (out_baseimg
&& *out_baseimg
) {
3833 ret
= bdrv_change_backing_file(unfiltered_bs
, out_baseimg
, out_basefmt
,
3836 ret
= bdrv_change_backing_file(unfiltered_bs
, NULL
, NULL
, false);
3839 if (ret
== -ENOSPC
) {
3840 error_report("Could not change the backing file to '%s': No "
3841 "space left in the file header", out_baseimg
);
3842 } else if (ret
== -EINVAL
&& out_baseimg
&& !out_basefmt
) {
3843 error_report("Could not change the backing file to '%s': backing "
3844 "format must be specified", out_baseimg
);
3845 } else if (ret
< 0) {
3846 error_report("Could not change the backing file to '%s': %s",
3847 out_baseimg
, strerror(-ret
));
3850 qemu_progress_print(100, 0);
3852 * TODO At this point it is possible to check if any clusters that are
3853 * allocated in the COW file are the same in the backing file. If so, they
3854 * could be dropped from the COW file. Don't do this before switching the
3855 * backing file, in case of a crash this would lead to corruption.
3858 qemu_progress_end();
3861 blk_unref(blk_old_backing
);
3862 blk_unref(blk_new_backing
);
3864 qemu_vfree(buf_old
);
3865 qemu_vfree(buf_new
);
3874 static int img_resize(int argc
, char **argv
)
3877 int c
, ret
, relative
;
3878 const char *filename
, *fmt
, *size
;
3879 int64_t n
, total_size
, current_size
;
3881 BlockBackend
*blk
= NULL
;
3882 PreallocMode prealloc
= PREALLOC_MODE_OFF
;
3885 static QemuOptsList resize_options
= {
3886 .name
= "resize_options",
3887 .head
= QTAILQ_HEAD_INITIALIZER(resize_options
.head
),
3890 .name
= BLOCK_OPT_SIZE
,
3891 .type
= QEMU_OPT_SIZE
,
3892 .help
= "Virtual disk size"
3898 bool image_opts
= false;
3899 bool shrink
= false;
3901 /* Remove size from argv manually so that negative numbers are not treated
3902 * as options by getopt. */
3904 error_exit("Not enough arguments");
3908 size
= argv
[--argc
];
3910 /* Parse getopt arguments */
3913 static const struct option long_options
[] = {
3914 {"help", no_argument
, 0, 'h'},
3915 {"object", required_argument
, 0, OPTION_OBJECT
},
3916 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
3917 {"preallocation", required_argument
, 0, OPTION_PREALLOCATION
},
3918 {"shrink", no_argument
, 0, OPTION_SHRINK
},
3921 c
= getopt_long(argc
, argv
, ":f:hq",
3922 long_options
, NULL
);
3928 missing_argument(argv
[optind
- 1]);
3931 unrecognized_option(argv
[optind
- 1]);
3943 user_creatable_process_cmdline(optarg
);
3945 case OPTION_IMAGE_OPTS
:
3948 case OPTION_PREALLOCATION
:
3949 prealloc
= qapi_enum_parse(&PreallocMode_lookup
, optarg
,
3950 PREALLOC_MODE__MAX
, NULL
);
3951 if (prealloc
== PREALLOC_MODE__MAX
) {
3952 error_report("Invalid preallocation mode '%s'", optarg
);
3961 if (optind
!= argc
- 1) {
3962 error_exit("Expecting image file name and size");
3964 filename
= argv
[optind
++];
3966 /* Choose grow, shrink, or absolute resize mode */
3982 param
= qemu_opts_create(&resize_options
, NULL
, 0, &error_abort
);
3983 if (!qemu_opt_set(param
, BLOCK_OPT_SIZE
, size
, &err
)) {
3984 error_report_err(err
);
3986 qemu_opts_del(param
);
3989 n
= qemu_opt_get_size(param
, BLOCK_OPT_SIZE
, 0);
3990 qemu_opts_del(param
);
3992 blk
= img_open(image_opts
, filename
, fmt
,
3993 BDRV_O_RDWR
| BDRV_O_RESIZE
, false, quiet
,
4000 current_size
= blk_getlength(blk
);
4001 if (current_size
< 0) {
4002 error_report("Failed to inquire current image length: %s",
4003 strerror(-current_size
));
4009 total_size
= current_size
+ n
* relative
;
4013 if (total_size
<= 0) {
4014 error_report("New image size must be positive");
4019 if (total_size
<= current_size
&& prealloc
!= PREALLOC_MODE_OFF
) {
4020 error_report("Preallocation can only be used for growing images");
4025 if (total_size
< current_size
&& !shrink
) {
4026 error_report("Use the --shrink option to perform a shrink operation.");
4027 warn_report("Shrinking an image will delete all data beyond the "
4028 "shrunken image's end. Before performing such an "
4029 "operation, make sure there is no important data there.");
4035 * The user expects the image to have the desired size after
4036 * resizing, so pass @exact=true. It is of no use to report
4037 * success when the image has not actually been resized.
4039 ret
= blk_truncate(blk
, total_size
, true, prealloc
, 0, &err
);
4041 qprintf(quiet
, "Image resized.\n");
4043 error_report_err(err
);
4053 static void amend_status_cb(BlockDriverState
*bs
,
4054 int64_t offset
, int64_t total_work_size
,
4057 qemu_progress_print(100.f
* offset
/ total_work_size
, 0);
4060 static int print_amend_option_help(const char *format
)
4064 /* Find driver and parse its options */
4065 drv
= bdrv_find_format(format
);
4067 error_report("Unknown file format '%s'", format
);
4071 if (!drv
->bdrv_amend_options
) {
4072 error_report("Format driver '%s' does not support option amendment",
4077 /* Every driver supporting amendment must have amend_opts */
4078 assert(drv
->amend_opts
);
4080 printf("Amend options for '%s':\n", format
);
4081 qemu_opts_print_help(drv
->amend_opts
, false);
4085 static int img_amend(int argc
, char **argv
)
4089 char *options
= NULL
;
4090 QemuOptsList
*amend_opts
= NULL
;
4091 QemuOpts
*opts
= NULL
;
4092 const char *fmt
= NULL
, *filename
, *cache
;
4095 bool quiet
= false, progress
= false;
4096 BlockBackend
*blk
= NULL
;
4097 BlockDriverState
*bs
= NULL
;
4098 bool image_opts
= false;
4101 cache
= BDRV_DEFAULT_CACHE
;
4103 static const struct option long_options
[] = {
4104 {"help", no_argument
, 0, 'h'},
4105 {"object", required_argument
, 0, OPTION_OBJECT
},
4106 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
4107 {"force", no_argument
, 0, OPTION_FORCE
},
4110 c
= getopt_long(argc
, argv
, ":ho:f:t:pq",
4111 long_options
, NULL
);
4118 missing_argument(argv
[optind
- 1]);
4121 unrecognized_option(argv
[optind
- 1]);
4127 if (accumulate_options(&options
, optarg
) < 0) {
4129 goto out_no_progress
;
4145 user_creatable_process_cmdline(optarg
);
4147 case OPTION_IMAGE_OPTS
:
4157 error_exit("Must specify options (-o)");
4163 qemu_progress_init(progress
, 1.0);
4165 filename
= (optind
== argc
- 1) ? argv
[argc
- 1] : NULL
;
4166 if (fmt
&& has_help_option(options
)) {
4167 /* If a format is explicitly specified (and possibly no filename is
4168 * given), print option help here */
4169 ret
= print_amend_option_help(fmt
);
4173 if (optind
!= argc
- 1) {
4174 error_report("Expecting one image file name");
4179 flags
= BDRV_O_RDWR
;
4180 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
4182 error_report("Invalid cache option: %s", cache
);
4186 blk
= img_open(image_opts
, filename
, fmt
, flags
, writethrough
, quiet
,
4194 fmt
= bs
->drv
->format_name
;
4196 if (has_help_option(options
)) {
4197 /* If the format was auto-detected, print option help here */
4198 ret
= print_amend_option_help(fmt
);
4202 if (!bs
->drv
->bdrv_amend_options
) {
4203 error_report("Format driver '%s' does not support option amendment",
4209 /* Every driver supporting amendment must have amend_opts */
4210 assert(bs
->drv
->amend_opts
);
4212 amend_opts
= qemu_opts_append(amend_opts
, bs
->drv
->amend_opts
);
4213 opts
= qemu_opts_create(amend_opts
, NULL
, 0, &error_abort
);
4214 if (!qemu_opts_do_parse(opts
, options
, NULL
, &err
)) {
4215 /* Try to parse options using the create options */
4216 amend_opts
= qemu_opts_append(amend_opts
, bs
->drv
->create_opts
);
4217 qemu_opts_del(opts
);
4218 opts
= qemu_opts_create(amend_opts
, NULL
, 0, &error_abort
);
4219 if (qemu_opts_do_parse(opts
, options
, NULL
, NULL
)) {
4220 error_append_hint(&err
,
4221 "This option is only supported for image creation\n");
4224 error_report_err(err
);
4229 /* In case the driver does not call amend_status_cb() */
4230 qemu_progress_print(0.f
, 0);
4231 ret
= bdrv_amend_options(bs
, opts
, &amend_status_cb
, NULL
, force
, &err
);
4232 qemu_progress_print(100.f
, 0);
4234 error_report_err(err
);
4239 qemu_progress_end();
4243 qemu_opts_del(opts
);
4244 qemu_opts_free(amend_opts
);
4253 typedef struct BenchData
{
4255 uint64_t image_size
;
4262 bool drain_on_flush
;
4271 static void bench_undrained_flush_cb(void *opaque
, int ret
)
4274 error_report("Failed flush request: %s", strerror(-ret
));
4279 static void bench_cb(void *opaque
, int ret
)
4281 BenchData
*b
= opaque
;
4285 error_report("Failed request: %s", strerror(-ret
));
4290 /* Just finished a flush with drained queue: Start next requests */
4291 assert(b
->in_flight
== 0);
4292 b
->in_flush
= false;
4293 } else if (b
->in_flight
> 0) {
4294 int remaining
= b
->n
- b
->in_flight
;
4299 /* Time for flush? Drain queue if requested, then flush */
4300 if (b
->flush_interval
&& remaining
% b
->flush_interval
== 0) {
4301 if (!b
->in_flight
|| !b
->drain_on_flush
) {
4302 BlockCompletionFunc
*cb
;
4304 if (b
->drain_on_flush
) {
4308 cb
= bench_undrained_flush_cb
;
4311 acb
= blk_aio_flush(b
->blk
, cb
, b
);
4313 error_report("Failed to issue flush request");
4317 if (b
->drain_on_flush
) {
4323 while (b
->n
> b
->in_flight
&& b
->in_flight
< b
->nrreq
) {
4324 int64_t offset
= b
->offset
;
4325 /* blk_aio_* might look for completed I/Os and kick bench_cb
4326 * again, so make sure this operation is counted by in_flight
4327 * and b->offset is ready for the next submission.
4330 b
->offset
+= b
->step
;
4331 b
->offset
%= b
->image_size
;
4333 acb
= blk_aio_pwritev(b
->blk
, offset
, b
->qiov
, 0, bench_cb
, b
);
4335 acb
= blk_aio_preadv(b
->blk
, offset
, b
->qiov
, 0, bench_cb
, b
);
4338 error_report("Failed to issue request");
4344 static int img_bench(int argc
, char **argv
)
4347 const char *fmt
= NULL
, *filename
;
4349 bool image_opts
= false;
4350 bool is_write
= false;
4354 size_t bufsize
= 4096;
4357 int flush_interval
= 0;
4358 bool drain_on_flush
= true;
4360 BlockBackend
*blk
= NULL
;
4361 BenchData data
= {};
4363 bool writethrough
= false;
4364 struct timeval t1
, t2
;
4366 bool force_share
= false;
4370 static const struct option long_options
[] = {
4371 {"help", no_argument
, 0, 'h'},
4372 {"flush-interval", required_argument
, 0, OPTION_FLUSH_INTERVAL
},
4373 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
4374 {"pattern", required_argument
, 0, OPTION_PATTERN
},
4375 {"no-drain", no_argument
, 0, OPTION_NO_DRAIN
},
4376 {"force-share", no_argument
, 0, 'U'},
4379 c
= getopt_long(argc
, argv
, ":hc:d:f:ni:o:qs:S:t:wU", long_options
,
4387 missing_argument(argv
[optind
- 1]);
4390 unrecognized_option(argv
[optind
- 1]);
4399 if (qemu_strtoul(optarg
, NULL
, 0, &res
) < 0 || res
> INT_MAX
) {
4400 error_report("Invalid request count specified");
4410 if (qemu_strtoul(optarg
, NULL
, 0, &res
) < 0 || res
> INT_MAX
) {
4411 error_report("Invalid queue depth specified");
4421 flags
|= BDRV_O_NATIVE_AIO
;
4424 ret
= bdrv_parse_aio(optarg
, &flags
);
4426 error_report("Invalid aio option: %s", optarg
);
4433 offset
= cvtnum("offset", optarg
);
4447 sval
= cvtnum_full("buffer size", optarg
, 0, INT_MAX
);
4459 sval
= cvtnum_full("step_size", optarg
, 0, INT_MAX
);
4468 ret
= bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
);
4470 error_report("Invalid cache mode");
4476 flags
|= BDRV_O_RDWR
;
4482 case OPTION_PATTERN
:
4486 if (qemu_strtoul(optarg
, NULL
, 0, &res
) < 0 || res
> 0xff) {
4487 error_report("Invalid pattern byte specified");
4493 case OPTION_FLUSH_INTERVAL
:
4497 if (qemu_strtoul(optarg
, NULL
, 0, &res
) < 0 || res
> INT_MAX
) {
4498 error_report("Invalid flush interval specified");
4501 flush_interval
= res
;
4504 case OPTION_NO_DRAIN
:
4505 drain_on_flush
= false;
4507 case OPTION_IMAGE_OPTS
:
4513 if (optind
!= argc
- 1) {
4514 error_exit("Expecting one image file name");
4516 filename
= argv
[argc
- 1];
4518 if (!is_write
&& flush_interval
) {
4519 error_report("--flush-interval is only available in write tests");
4523 if (flush_interval
&& flush_interval
< depth
) {
4524 error_report("Flush interval can't be smaller than depth");
4529 blk
= img_open(image_opts
, filename
, fmt
, flags
, writethrough
, quiet
,
4536 image_size
= blk_getlength(blk
);
4537 if (image_size
< 0) {
4542 data
= (BenchData
) {
4544 .image_size
= image_size
,
4546 .step
= step
?: bufsize
,
4551 .flush_interval
= flush_interval
,
4552 .drain_on_flush
= drain_on_flush
,
4554 printf("Sending %d %s requests, %d bytes each, %d in parallel "
4555 "(starting at offset %" PRId64
", step size %d)\n",
4556 data
.n
, data
.write
? "write" : "read", data
.bufsize
, data
.nrreq
,
4557 data
.offset
, data
.step
);
4558 if (flush_interval
) {
4559 printf("Sending flush every %d requests\n", flush_interval
);
4562 buf_size
= data
.nrreq
* data
.bufsize
;
4563 data
.buf
= blk_blockalign(blk
, buf_size
);
4564 memset(data
.buf
, pattern
, data
.nrreq
* data
.bufsize
);
4566 blk_register_buf(blk
, data
.buf
, buf_size
);
4568 data
.qiov
= g_new(QEMUIOVector
, data
.nrreq
);
4569 for (i
= 0; i
< data
.nrreq
; i
++) {
4570 qemu_iovec_init(&data
.qiov
[i
], 1);
4571 qemu_iovec_add(&data
.qiov
[i
],
4572 data
.buf
+ i
* data
.bufsize
, data
.bufsize
);
4575 gettimeofday(&t1
, NULL
);
4578 while (data
.n
> 0) {
4579 main_loop_wait(false);
4581 gettimeofday(&t2
, NULL
);
4583 printf("Run completed in %3.3f seconds.\n",
4584 (t2
.tv_sec
- t1
.tv_sec
)
4585 + ((double)(t2
.tv_usec
- t1
.tv_usec
) / 1000000));
4589 blk_unregister_buf(blk
, data
.buf
);
4591 qemu_vfree(data
.buf
);
4608 typedef struct ImgBitmapAction
{
4609 enum ImgBitmapAct act
;
4610 const char *src
; /* only used for merge */
4611 QSIMPLEQ_ENTRY(ImgBitmapAction
) next
;
4614 static int img_bitmap(int argc
, char **argv
)
4618 QemuOpts
*opts
= NULL
;
4619 const char *fmt
= NULL
, *src_fmt
= NULL
, *src_filename
= NULL
;
4620 const char *filename
, *bitmap
;
4621 BlockBackend
*blk
= NULL
, *src
= NULL
;
4622 BlockDriverState
*bs
= NULL
, *src_bs
= NULL
;
4623 bool image_opts
= false;
4624 int64_t granularity
= 0;
4625 bool add
= false, merge
= false;
4626 QSIMPLEQ_HEAD(, ImgBitmapAction
) actions
;
4627 ImgBitmapAction
*act
, *act_next
;
4630 QSIMPLEQ_INIT(&actions
);
4633 static const struct option long_options
[] = {
4634 {"help", no_argument
, 0, 'h'},
4635 {"object", required_argument
, 0, OPTION_OBJECT
},
4636 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
4637 {"add", no_argument
, 0, OPTION_ADD
},
4638 {"remove", no_argument
, 0, OPTION_REMOVE
},
4639 {"clear", no_argument
, 0, OPTION_CLEAR
},
4640 {"enable", no_argument
, 0, OPTION_ENABLE
},
4641 {"disable", no_argument
, 0, OPTION_DISABLE
},
4642 {"merge", required_argument
, 0, OPTION_MERGE
},
4643 {"granularity", required_argument
, 0, 'g'},
4644 {"source-file", required_argument
, 0, 'b'},
4645 {"source-format", required_argument
, 0, 'F'},
4648 c
= getopt_long(argc
, argv
, ":b:f:F:g:h", long_options
, NULL
);
4655 missing_argument(argv
[optind
- 1]);
4658 unrecognized_option(argv
[optind
- 1]);
4664 src_filename
= optarg
;
4673 granularity
= cvtnum("granularity", optarg
);
4674 if (granularity
< 0) {
4679 act
= g_new0(ImgBitmapAction
, 1);
4680 act
->act
= BITMAP_ADD
;
4681 QSIMPLEQ_INSERT_TAIL(&actions
, act
, next
);
4685 act
= g_new0(ImgBitmapAction
, 1);
4686 act
->act
= BITMAP_REMOVE
;
4687 QSIMPLEQ_INSERT_TAIL(&actions
, act
, next
);
4690 act
= g_new0(ImgBitmapAction
, 1);
4691 act
->act
= BITMAP_CLEAR
;
4692 QSIMPLEQ_INSERT_TAIL(&actions
, act
, next
);
4695 act
= g_new0(ImgBitmapAction
, 1);
4696 act
->act
= BITMAP_ENABLE
;
4697 QSIMPLEQ_INSERT_TAIL(&actions
, act
, next
);
4699 case OPTION_DISABLE
:
4700 act
= g_new0(ImgBitmapAction
, 1);
4701 act
->act
= BITMAP_DISABLE
;
4702 QSIMPLEQ_INSERT_TAIL(&actions
, act
, next
);
4705 act
= g_new0(ImgBitmapAction
, 1);
4706 act
->act
= BITMAP_MERGE
;
4708 QSIMPLEQ_INSERT_TAIL(&actions
, act
, next
);
4712 user_creatable_process_cmdline(optarg
);
4714 case OPTION_IMAGE_OPTS
:
4720 if (QSIMPLEQ_EMPTY(&actions
)) {
4721 error_report("Need at least one of --add, --remove, --clear, "
4722 "--enable, --disable, or --merge");
4726 if (granularity
&& !add
) {
4727 error_report("granularity only supported with --add");
4730 if (src_fmt
&& !src_filename
) {
4731 error_report("-F only supported with -b");
4734 if (src_filename
&& !merge
) {
4735 error_report("Merge bitmap source file only supported with "
4740 if (optind
!= argc
- 2) {
4741 error_report("Expecting filename and bitmap name");
4745 filename
= argv
[optind
];
4746 bitmap
= argv
[optind
+ 1];
4749 * No need to open backing chains; we will be manipulating bitmaps
4750 * directly in this image without reference to image contents.
4752 blk
= img_open(image_opts
, filename
, fmt
, BDRV_O_RDWR
| BDRV_O_NO_BACKING
,
4753 false, false, false);
4759 src
= img_open(false, src_filename
, src_fmt
, BDRV_O_NO_BACKING
,
4760 false, false, false);
4764 src_bs
= blk_bs(src
);
4769 QSIMPLEQ_FOREACH_SAFE(act
, &actions
, next
, act_next
) {
4772 qmp_block_dirty_bitmap_add(bs
->node_name
, bitmap
,
4773 !!granularity
, granularity
, true, true,
4774 false, false, &err
);
4778 qmp_block_dirty_bitmap_remove(bs
->node_name
, bitmap
, &err
);
4782 qmp_block_dirty_bitmap_clear(bs
->node_name
, bitmap
, &err
);
4786 qmp_block_dirty_bitmap_enable(bs
->node_name
, bitmap
, &err
);
4789 case BITMAP_DISABLE
:
4790 qmp_block_dirty_bitmap_disable(bs
->node_name
, bitmap
, &err
);
4794 do_dirty_bitmap_merge(bs
->node_name
, bitmap
, src_bs
->node_name
,
4799 g_assert_not_reached();
4803 error_reportf_err(err
, "Operation %s on bitmap %s failed: ",
4815 qemu_opts_del(opts
);
4831 int bsz
; /* Block size */
4839 int (*f
)(const char *, struct DdIo
*, struct DdIo
*, struct DdInfo
*);
4843 static int img_dd_bs(const char *arg
,
4844 struct DdIo
*in
, struct DdIo
*out
,
4849 res
= cvtnum_full("bs", arg
, 1, INT_MAX
);
4854 in
->bsz
= out
->bsz
= res
;
4859 static int img_dd_count(const char *arg
,
4860 struct DdIo
*in
, struct DdIo
*out
,
4863 dd
->count
= cvtnum("count", arg
);
4865 if (dd
->count
< 0) {
4872 static int img_dd_if(const char *arg
,
4873 struct DdIo
*in
, struct DdIo
*out
,
4876 in
->filename
= g_strdup(arg
);
4881 static int img_dd_of(const char *arg
,
4882 struct DdIo
*in
, struct DdIo
*out
,
4885 out
->filename
= g_strdup(arg
);
4890 static int img_dd_skip(const char *arg
,
4891 struct DdIo
*in
, struct DdIo
*out
,
4894 in
->offset
= cvtnum("skip", arg
);
4896 if (in
->offset
< 0) {
4903 static int img_dd(int argc
, char **argv
)
4908 BlockDriver
*drv
= NULL
, *proto_drv
= NULL
;
4909 BlockBackend
*blk1
= NULL
, *blk2
= NULL
;
4910 QemuOpts
*opts
= NULL
;
4911 QemuOptsList
*create_opts
= NULL
;
4912 Error
*local_err
= NULL
;
4913 bool image_opts
= false;
4915 const char *out_fmt
= "raw";
4916 const char *fmt
= NULL
;
4918 int64_t block_count
= 0, out_pos
, in_pos
;
4919 bool force_share
= false;
4920 struct DdInfo dd
= {
4925 .bsz
= 512, /* Block size is by default 512 bytes */
4937 const struct DdOpts options
[] = {
4938 { "bs", img_dd_bs
, C_BS
},
4939 { "count", img_dd_count
, C_COUNT
},
4940 { "if", img_dd_if
, C_IF
},
4941 { "of", img_dd_of
, C_OF
},
4942 { "skip", img_dd_skip
, C_SKIP
},
4945 const struct option long_options
[] = {
4946 { "help", no_argument
, 0, 'h'},
4947 { "object", required_argument
, 0, OPTION_OBJECT
},
4948 { "image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
4949 { "force-share", no_argument
, 0, 'U'},
4953 while ((c
= getopt_long(argc
, argv
, ":hf:O:U", long_options
, NULL
))) {
4965 missing_argument(argv
[optind
- 1]);
4968 unrecognized_option(argv
[optind
- 1]);
4977 user_creatable_process_cmdline(optarg
);
4979 case OPTION_IMAGE_OPTS
:
4985 for (i
= optind
; i
< argc
; i
++) {
4987 arg
= g_strdup(argv
[i
]);
4989 tmp
= strchr(arg
, '=');
4991 error_report("unrecognized operand %s", arg
);
4998 for (j
= 0; options
[j
].name
!= NULL
; j
++) {
4999 if (!strcmp(arg
, options
[j
].name
)) {
5003 if (options
[j
].name
== NULL
) {
5004 error_report("unrecognized operand %s", arg
);
5009 if (options
[j
].f(tmp
, &in
, &out
, &dd
) != 0) {
5013 dd
.flags
|= options
[j
].flag
;
5018 if (!(dd
.flags
& C_IF
&& dd
.flags
& C_OF
)) {
5019 error_report("Must specify both input and output files");
5024 blk1
= img_open(image_opts
, in
.filename
, fmt
, 0, false, false,
5032 drv
= bdrv_find_format(out_fmt
);
5034 error_report("Unknown file format");
5038 proto_drv
= bdrv_find_protocol(out
.filename
, true, &local_err
);
5041 error_report_err(local_err
);
5045 if (!drv
->create_opts
) {
5046 error_report("Format driver '%s' does not support image creation",
5051 if (!proto_drv
->create_opts
) {
5052 error_report("Protocol driver '%s' does not support image creation",
5053 proto_drv
->format_name
);
5057 create_opts
= qemu_opts_append(create_opts
, drv
->create_opts
);
5058 create_opts
= qemu_opts_append(create_opts
, proto_drv
->create_opts
);
5060 opts
= qemu_opts_create(create_opts
, NULL
, 0, &error_abort
);
5062 size
= blk_getlength(blk1
);
5064 error_report("Failed to get size for '%s'", in
.filename
);
5069 if (dd
.flags
& C_COUNT
&& dd
.count
<= INT64_MAX
/ in
.bsz
&&
5070 dd
.count
* in
.bsz
< size
) {
5071 size
= dd
.count
* in
.bsz
;
5074 /* Overflow means the specified offset is beyond input image's size */
5075 if (dd
.flags
& C_SKIP
&& (in
.offset
> INT64_MAX
/ in
.bsz
||
5076 size
< in
.bsz
* in
.offset
)) {
5077 qemu_opt_set_number(opts
, BLOCK_OPT_SIZE
, 0, &error_abort
);
5079 qemu_opt_set_number(opts
, BLOCK_OPT_SIZE
,
5080 size
- in
.bsz
* in
.offset
, &error_abort
);
5083 ret
= bdrv_create(drv
, out
.filename
, opts
, &local_err
);
5085 error_reportf_err(local_err
,
5086 "%s: error while creating output image: ",
5092 /* TODO, we can't honour --image-opts for the target,
5093 * since it needs to be given in a format compatible
5094 * with the bdrv_create() call above which does not
5095 * support image-opts style.
5097 blk2
= img_open_file(out
.filename
, NULL
, out_fmt
, BDRV_O_RDWR
,
5098 false, false, false);
5105 if (dd
.flags
& C_SKIP
&& (in
.offset
> INT64_MAX
/ in
.bsz
||
5106 size
< in
.offset
* in
.bsz
)) {
5107 /* We give a warning if the skip option is bigger than the input
5108 * size and create an empty output disk image (i.e. like dd(1)).
5110 error_report("%s: cannot skip to specified offset", in
.filename
);
5113 in_pos
= in
.offset
* in
.bsz
;
5116 in
.buf
= g_new(uint8_t, in
.bsz
);
5118 for (out_pos
= 0; in_pos
< size
; block_count
++) {
5119 int in_ret
, out_ret
;
5121 if (in_pos
+ in
.bsz
> size
) {
5122 in_ret
= blk_pread(blk1
, in_pos
, in
.buf
, size
- in_pos
);
5124 in_ret
= blk_pread(blk1
, in_pos
, in
.buf
, in
.bsz
);
5127 error_report("error while reading from input image file: %s",
5134 out_ret
= blk_pwrite(blk2
, out_pos
, in
.buf
, in_ret
, 0);
5137 error_report("error while writing to output image file: %s",
5138 strerror(-out_ret
));
5147 qemu_opts_del(opts
);
5148 qemu_opts_free(create_opts
);
5151 g_free(in
.filename
);
5152 g_free(out
.filename
);
5162 static void dump_json_block_measure_info(BlockMeasureInfo
*info
)
5166 Visitor
*v
= qobject_output_visitor_new(&obj
);
5168 visit_type_BlockMeasureInfo(v
, NULL
, &info
, &error_abort
);
5169 visit_complete(v
, &obj
);
5170 str
= qobject_to_json_pretty(obj
, true);
5171 assert(str
!= NULL
);
5172 printf("%s\n", str
->str
);
5175 g_string_free(str
, true);
5178 static int img_measure(int argc
, char **argv
)
5180 static const struct option long_options
[] = {
5181 {"help", no_argument
, 0, 'h'},
5182 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
5183 {"object", required_argument
, 0, OPTION_OBJECT
},
5184 {"output", required_argument
, 0, OPTION_OUTPUT
},
5185 {"size", required_argument
, 0, OPTION_SIZE
},
5186 {"force-share", no_argument
, 0, 'U'},
5189 OutputFormat output_format
= OFORMAT_HUMAN
;
5190 BlockBackend
*in_blk
= NULL
;
5192 const char *filename
= NULL
;
5193 const char *fmt
= NULL
;
5194 const char *out_fmt
= "raw";
5195 char *options
= NULL
;
5196 char *snapshot_name
= NULL
;
5197 bool force_share
= false;
5198 QemuOpts
*opts
= NULL
;
5199 QemuOpts
*object_opts
= NULL
;
5200 QemuOpts
*sn_opts
= NULL
;
5201 QemuOptsList
*create_opts
= NULL
;
5202 bool image_opts
= false;
5203 uint64_t img_size
= UINT64_MAX
;
5204 BlockMeasureInfo
*info
= NULL
;
5205 Error
*local_err
= NULL
;
5209 while ((c
= getopt_long(argc
, argv
, "hf:O:o:l:U",
5210 long_options
, NULL
)) != -1) {
5223 if (accumulate_options(&options
, optarg
) < 0) {
5228 if (strstart(optarg
, SNAPSHOT_OPT_BASE
, NULL
)) {
5229 sn_opts
= qemu_opts_parse_noisily(&internal_snapshot_opts
,
5232 error_report("Failed in parsing snapshot param '%s'",
5237 snapshot_name
= optarg
;
5244 user_creatable_process_cmdline(optarg
);
5246 case OPTION_IMAGE_OPTS
:
5250 if (!strcmp(optarg
, "json")) {
5251 output_format
= OFORMAT_JSON
;
5252 } else if (!strcmp(optarg
, "human")) {
5253 output_format
= OFORMAT_HUMAN
;
5255 error_report("--output must be used with human or json "
5264 sval
= cvtnum("image size", optarg
);
5268 img_size
= (uint64_t)sval
;
5274 if (argc
- optind
> 1) {
5275 error_report("At most one filename argument is allowed.");
5277 } else if (argc
- optind
== 1) {
5278 filename
= argv
[optind
];
5281 if (!filename
&& (image_opts
|| fmt
|| snapshot_name
|| sn_opts
)) {
5282 error_report("--image-opts, -f, and -l require a filename argument.");
5285 if (filename
&& img_size
!= UINT64_MAX
) {
5286 error_report("--size N cannot be used together with a filename.");
5289 if (!filename
&& img_size
== UINT64_MAX
) {
5290 error_report("Either --size N or one filename must be specified.");
5295 in_blk
= img_open(image_opts
, filename
, fmt
, 0,
5296 false, false, force_share
);
5302 bdrv_snapshot_load_tmp(blk_bs(in_blk
),
5303 qemu_opt_get(sn_opts
, SNAPSHOT_OPT_ID
),
5304 qemu_opt_get(sn_opts
, SNAPSHOT_OPT_NAME
),
5306 } else if (snapshot_name
!= NULL
) {
5307 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk
),
5308 snapshot_name
, &local_err
);
5311 error_reportf_err(local_err
, "Failed to load snapshot: ");
5316 drv
= bdrv_find_format(out_fmt
);
5318 error_report("Unknown file format '%s'", out_fmt
);
5321 if (!drv
->create_opts
) {
5322 error_report("Format driver '%s' does not support image creation",
5327 create_opts
= qemu_opts_append(create_opts
, drv
->create_opts
);
5328 create_opts
= qemu_opts_append(create_opts
, bdrv_file
.create_opts
);
5329 opts
= qemu_opts_create(create_opts
, NULL
, 0, &error_abort
);
5331 if (!qemu_opts_do_parse(opts
, options
, NULL
, &local_err
)) {
5332 error_report_err(local_err
);
5333 error_report("Invalid options for file format '%s'", out_fmt
);
5337 if (img_size
!= UINT64_MAX
) {
5338 qemu_opt_set_number(opts
, BLOCK_OPT_SIZE
, img_size
, &error_abort
);
5341 info
= bdrv_measure(drv
, opts
, in_blk
? blk_bs(in_blk
) : NULL
, &local_err
);
5343 error_report_err(local_err
);
5347 if (output_format
== OFORMAT_HUMAN
) {
5348 printf("required size: %" PRIu64
"\n", info
->required
);
5349 printf("fully allocated size: %" PRIu64
"\n", info
->fully_allocated
);
5350 if (info
->has_bitmaps
) {
5351 printf("bitmaps size: %" PRIu64
"\n", info
->bitmaps
);
5354 dump_json_block_measure_info(info
);
5360 qapi_free_BlockMeasureInfo(info
);
5361 qemu_opts_del(object_opts
);
5362 qemu_opts_del(opts
);
5363 qemu_opts_del(sn_opts
);
5364 qemu_opts_free(create_opts
);
5370 static const img_cmd_t img_cmds
[] = {
5371 #define DEF(option, callback, arg_string) \
5372 { option, callback },
5373 #include "qemu-img-cmds.h"
5378 int main(int argc
, char **argv
)
5380 const img_cmd_t
*cmd
;
5381 const char *cmdname
;
5383 static const struct option long_options
[] = {
5384 {"help", no_argument
, 0, 'h'},
5385 {"version", no_argument
, 0, 'V'},
5386 {"trace", required_argument
, NULL
, 'T'},
5391 signal(SIGPIPE
, SIG_IGN
);
5395 error_init(argv
[0]);
5396 module_call_init(MODULE_INIT_TRACE
);
5397 qemu_init_exec_dir(argv
[0]);
5399 qemu_init_main_loop(&error_fatal
);
5401 qcrypto_init(&error_fatal
);
5403 module_call_init(MODULE_INIT_QOM
);
5406 error_exit("Not enough arguments");
5409 qemu_add_opts(&qemu_source_opts
);
5410 qemu_add_opts(&qemu_trace_opts
);
5412 while ((c
= getopt_long(argc
, argv
, "+:hVT:", long_options
, NULL
)) != -1) {
5415 missing_argument(argv
[optind
- 1]);
5418 unrecognized_option(argv
[optind
- 1]);
5424 printf(QEMU_IMG_VERSION
);
5427 trace_opt_parse(optarg
);
5432 cmdname
= argv
[optind
];
5434 /* reset getopt_long scanning */
5440 qemu_reset_optind();
5442 if (!trace_init_backends()) {
5446 qemu_set_log(LOG_TRACE
);
5448 /* find the command */
5449 for (cmd
= img_cmds
; cmd
->name
!= NULL
; cmd
++) {
5450 if (!strcmp(cmdname
, cmd
->name
)) {
5451 return cmd
->handler(argc
, argv
);
5456 error_exit("Command not found: %s", cmdname
);