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
24 #include "qemu/osdep.h"
25 #include "qemu-version.h"
26 #include "qapi/error.h"
27 #include "qapi-visit.h"
28 #include "qapi/qobject-output-visitor.h"
29 #include "qapi/qmp/qerror.h"
30 #include "qapi/qmp/qjson.h"
31 #include "qapi/qmp/qbool.h"
32 #include "qemu/cutils.h"
33 #include "qemu/config-file.h"
34 #include "qemu/option.h"
35 #include "qemu/error-report.h"
37 #include "qom/object_interfaces.h"
38 #include "sysemu/sysemu.h"
39 #include "sysemu/block-backend.h"
40 #include "block/block_int.h"
41 #include "block/blockjob.h"
42 #include "block/qapi.h"
43 #include "crypto/init.h"
44 #include "trace/control.h"
47 #define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION QEMU_PKGVERSION \
48 "\n" QEMU_COPYRIGHT "\n"
50 typedef struct img_cmd_t
{
52 int (*handler
)(int argc
, char **argv
);
57 OPTION_BACKING_CHAIN
= 257,
59 OPTION_IMAGE_OPTS
= 259,
61 OPTION_FLUSH_INTERVAL
= 261,
62 OPTION_NO_DRAIN
= 262,
63 OPTION_TARGET_IMAGE_OPTS
= 263,
66 typedef enum OutputFormat
{
71 /* Default to cache=writeback as data integrity is not important for qemu-img */
72 #define BDRV_DEFAULT_CACHE "writeback"
74 static void format_print(void *opaque
, const char *name
)
79 static void QEMU_NORETURN
GCC_FMT_ATTR(1, 2) error_exit(const char *fmt
, ...)
83 error_printf("qemu-img: ");
86 error_vprintf(fmt
, ap
);
89 error_printf("\nTry 'qemu-img --help' for more information\n");
93 static void QEMU_NORETURN
missing_argument(const char *option
)
95 error_exit("missing argument for option '%s'", option
);
98 static void QEMU_NORETURN
unrecognized_option(const char *option
)
100 error_exit("unrecognized option '%s'", option
);
103 /* Please keep in synch with qemu-img.texi */
104 static void QEMU_NORETURN
help(void)
106 const char *help_msg
=
108 "usage: qemu-img [standard options] command [command options]\n"
109 "QEMU disk image utility\n"
111 " '-h', '--help' display this help and exit\n"
112 " '-V', '--version' output version information and exit\n"
113 " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
114 " specify tracing options\n"
117 #define DEF(option, callback, arg_string) \
119 #include "qemu-img-cmds.h"
123 "Command parameters:\n"
124 " 'filename' is a disk image filename\n"
125 " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
126 " manual page for a description of the object properties. The most common\n"
127 " object type is a 'secret', which is used to supply passwords and/or\n"
128 " encryption keys.\n"
129 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
130 " 'cache' is the cache mode used to write the output disk image, the valid\n"
131 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
132 " 'directsync' and 'unsafe' (default for convert)\n"
133 " 'src_cache' is the cache mode used to read input disk images, the valid\n"
134 " options are the same as for the 'cache' option\n"
135 " 'size' is the disk image size in bytes. Optional suffixes\n"
136 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
137 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
138 " supported. 'b' is ignored.\n"
139 " 'output_filename' is the destination disk image filename\n"
140 " 'output_fmt' is the destination format\n"
141 " 'options' is a comma separated list of format specific options in a\n"
142 " name=value format. Use -o ? for an overview of the options supported by the\n"
144 " 'snapshot_param' is param used for internal snapshot, format\n"
145 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
147 " 'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n"
149 " '-c' indicates that target image must be compressed (qcow format only)\n"
150 " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n"
151 " match exactly. The image doesn't need a working backing file before\n"
152 " rebasing in this case (useful for renaming the backing file)\n"
153 " '-h' with or without a command shows this help and lists the supported formats\n"
154 " '-p' show progress of command (only certain commands)\n"
155 " '-q' use Quiet mode - do not print any output (except errors)\n"
156 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
157 " contain only zeros for qemu-img to create a sparse image during\n"
158 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
159 " unallocated or zero sectors, and the destination image will always be\n"
161 " '--output' takes the format in which the output must be done (human or json)\n"
162 " '-n' skips the target volume creation (useful if the volume is created\n"
163 " prior to running qemu-img)\n"
165 "Parameters to check subcommand:\n"
166 " '-r' tries to repair any inconsistencies that are found during the check.\n"
167 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
168 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
169 " hiding corruption that has already occurred.\n"
171 "Parameters to convert subcommand:\n"
172 " '-m' specifies how many coroutines work in parallel during the convert\n"
173 " process (defaults to 8)\n"
174 " '-W' allow to write to the target out of order rather than sequential\n"
176 "Parameters to snapshot subcommand:\n"
177 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
178 " '-a' applies a snapshot (revert disk to saved state)\n"
179 " '-c' creates a snapshot\n"
180 " '-d' deletes a snapshot\n"
181 " '-l' lists all snapshots in the given image\n"
183 "Parameters to compare subcommand:\n"
184 " '-f' first image format\n"
185 " '-F' second image format\n"
186 " '-s' run in Strict mode - fail on different image size or sector allocation\n"
188 "Parameters to dd subcommand:\n"
189 " 'bs=BYTES' read and write up to BYTES bytes at a time "
191 " 'count=N' copy only N input blocks\n"
192 " 'if=FILE' read from FILE\n"
193 " 'of=FILE' write to FILE\n"
194 " 'skip=N' skip N bs-sized blocks at the start of input\n";
196 printf("%s\nSupported formats:", help_msg
);
197 bdrv_iterate_format(format_print
, NULL
);
202 static QemuOptsList qemu_object_opts
= {
204 .implied_opt_name
= "qom-type",
205 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
211 static QemuOptsList qemu_source_opts
= {
213 .implied_opt_name
= "file",
214 .head
= QTAILQ_HEAD_INITIALIZER(qemu_source_opts
.head
),
220 static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet
, const char *fmt
, ...)
226 ret
= vprintf(fmt
, args
);
233 static int print_block_option_help(const char *filename
, const char *fmt
)
235 BlockDriver
*drv
, *proto_drv
;
236 QemuOptsList
*create_opts
= NULL
;
237 Error
*local_err
= NULL
;
239 /* Find driver and parse its options */
240 drv
= bdrv_find_format(fmt
);
242 error_report("Unknown file format '%s'", fmt
);
246 create_opts
= qemu_opts_append(create_opts
, drv
->create_opts
);
248 proto_drv
= bdrv_find_protocol(filename
, true, &local_err
);
250 error_report_err(local_err
);
251 qemu_opts_free(create_opts
);
254 create_opts
= qemu_opts_append(create_opts
, proto_drv
->create_opts
);
257 qemu_opts_print_help(create_opts
);
258 qemu_opts_free(create_opts
);
263 static BlockBackend
*img_open_opts(const char *optstr
,
264 QemuOpts
*opts
, int flags
, bool writethrough
,
265 bool quiet
, bool force_share
)
268 Error
*local_err
= NULL
;
270 options
= qemu_opts_to_qdict(opts
, NULL
);
272 if (qdict_haskey(options
, BDRV_OPT_FORCE_SHARE
)
273 && !qdict_get_bool(options
, BDRV_OPT_FORCE_SHARE
)) {
274 error_report("--force-share/-U conflicts with image options");
278 qdict_put_bool(options
, BDRV_OPT_FORCE_SHARE
, true);
280 blk
= blk_new_open(NULL
, NULL
, options
, flags
, &local_err
);
282 error_reportf_err(local_err
, "Could not open '%s': ", optstr
);
285 blk_set_enable_write_cache(blk
, !writethrough
);
290 static BlockBackend
*img_open_file(const char *filename
,
292 const char *fmt
, int flags
,
293 bool writethrough
, bool quiet
,
297 Error
*local_err
= NULL
;
300 options
= qdict_new();
303 qdict_put_str(options
, "driver", fmt
);
307 qdict_put_bool(options
, BDRV_OPT_FORCE_SHARE
, true);
309 blk
= blk_new_open(filename
, NULL
, options
, flags
, &local_err
);
311 error_reportf_err(local_err
, "Could not open '%s': ", filename
);
314 blk_set_enable_write_cache(blk
, !writethrough
);
320 static int img_add_key_secrets(void *opaque
,
321 const char *name
, const char *value
,
324 QDict
*options
= opaque
;
326 if (g_str_has_suffix(name
, "key-secret")) {
327 qdict_put(options
, name
, qstring_from_str(value
));
333 static BlockBackend
*img_open_new_file(const char *filename
,
334 QemuOpts
*create_opts
,
335 const char *fmt
, int flags
,
336 bool writethrough
, bool quiet
,
339 QDict
*options
= NULL
;
341 options
= qdict_new();
342 qemu_opt_foreach(create_opts
, img_add_key_secrets
, options
, &error_abort
);
344 return img_open_file(filename
, options
, fmt
, flags
, writethrough
, quiet
,
349 static BlockBackend
*img_open(bool image_opts
,
350 const char *filename
,
351 const char *fmt
, int flags
, bool writethrough
,
352 bool quiet
, bool force_share
)
358 error_report("--image-opts and --format are mutually exclusive");
361 opts
= qemu_opts_parse_noisily(qemu_find_opts("source"),
366 blk
= img_open_opts(filename
, opts
, flags
, writethrough
, quiet
,
369 blk
= img_open_file(filename
, NULL
, fmt
, flags
, writethrough
, quiet
,
376 static int add_old_style_options(const char *fmt
, QemuOpts
*opts
,
377 const char *base_filename
,
378 const char *base_fmt
)
383 qemu_opt_set(opts
, BLOCK_OPT_BACKING_FILE
, base_filename
, &err
);
385 error_report("Backing file not supported for file format '%s'",
392 qemu_opt_set(opts
, BLOCK_OPT_BACKING_FMT
, base_fmt
, &err
);
394 error_report("Backing file format not supported for file "
403 static int64_t cvtnum(const char *s
)
408 err
= qemu_strtosz(s
, NULL
, &value
);
412 if (value
> INT64_MAX
) {
418 static int img_create(int argc
, char **argv
)
421 uint64_t img_size
= -1;
422 const char *fmt
= "raw";
423 const char *base_fmt
= NULL
;
424 const char *filename
;
425 const char *base_filename
= NULL
;
426 char *options
= NULL
;
427 Error
*local_err
= NULL
;
431 static const struct option long_options
[] = {
432 {"help", no_argument
, 0, 'h'},
433 {"object", required_argument
, 0, OPTION_OBJECT
},
436 c
= getopt_long(argc
, argv
, ":F:b:f:ho:q",
443 missing_argument(argv
[optind
- 1]);
446 unrecognized_option(argv
[optind
- 1]);
455 base_filename
= optarg
;
461 if (!is_valid_option_list(optarg
)) {
462 error_report("Invalid option list: %s", optarg
);
466 options
= g_strdup(optarg
);
468 char *old_options
= options
;
469 options
= g_strdup_printf("%s,%s", options
, optarg
);
476 case OPTION_OBJECT
: {
478 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
487 /* Get the filename */
488 filename
= (optind
< argc
) ? argv
[optind
] : NULL
;
489 if (options
&& has_help_option(options
)) {
491 return print_block_option_help(filename
, fmt
);
494 if (optind
>= argc
) {
495 error_exit("Expecting image file name");
499 if (qemu_opts_foreach(&qemu_object_opts
,
500 user_creatable_add_opts_foreach
,
505 /* Get image size, if specified */
509 sval
= cvtnum(argv
[optind
++]);
511 if (sval
== -ERANGE
) {
512 error_report("Image size must be less than 8 EiB!");
514 error_report("Invalid image size specified! You may use k, M, "
515 "G, T, P or E suffixes for ");
516 error_report("kilobytes, megabytes, gigabytes, terabytes, "
517 "petabytes and exabytes.");
521 img_size
= (uint64_t)sval
;
523 if (optind
!= argc
) {
524 error_exit("Unexpected argument: %s", argv
[optind
]);
527 bdrv_img_create(filename
, fmt
, base_filename
, base_fmt
,
528 options
, img_size
, 0, quiet
, &local_err
);
530 error_reportf_err(local_err
, "%s: ", filename
);
542 static void dump_json_image_check(ImageCheck
*check
, bool quiet
)
546 Visitor
*v
= qobject_output_visitor_new(&obj
);
548 visit_type_ImageCheck(v
, NULL
, &check
, &error_abort
);
549 visit_complete(v
, &obj
);
550 str
= qobject_to_json_pretty(obj
);
552 qprintf(quiet
, "%s\n", qstring_get_str(str
));
558 static void dump_human_image_check(ImageCheck
*check
, bool quiet
)
560 if (!(check
->corruptions
|| check
->leaks
|| check
->check_errors
)) {
561 qprintf(quiet
, "No errors were found on the image.\n");
563 if (check
->corruptions
) {
564 qprintf(quiet
, "\n%" PRId64
" errors were found on the image.\n"
565 "Data may be corrupted, or further writes to the image "
572 "\n%" PRId64
" leaked clusters were found on the image.\n"
573 "This means waste of disk space, but no harm to data.\n",
577 if (check
->check_errors
) {
580 " internal errors have occurred during the check.\n",
581 check
->check_errors
);
585 if (check
->total_clusters
!= 0 && check
->allocated_clusters
!= 0) {
586 qprintf(quiet
, "%" PRId64
"/%" PRId64
" = %0.2f%% allocated, "
587 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
588 check
->allocated_clusters
, check
->total_clusters
,
589 check
->allocated_clusters
* 100.0 / check
->total_clusters
,
590 check
->fragmented_clusters
* 100.0 / check
->allocated_clusters
,
591 check
->compressed_clusters
* 100.0 /
592 check
->allocated_clusters
);
595 if (check
->image_end_offset
) {
597 "Image end offset: %" PRId64
"\n", check
->image_end_offset
);
601 static int collect_image_check(BlockDriverState
*bs
,
603 const char *filename
,
608 BdrvCheckResult result
;
610 ret
= bdrv_check(bs
, &result
, fix
);
615 check
->filename
= g_strdup(filename
);
616 check
->format
= g_strdup(bdrv_get_format_name(bs
));
617 check
->check_errors
= result
.check_errors
;
618 check
->corruptions
= result
.corruptions
;
619 check
->has_corruptions
= result
.corruptions
!= 0;
620 check
->leaks
= result
.leaks
;
621 check
->has_leaks
= result
.leaks
!= 0;
622 check
->corruptions_fixed
= result
.corruptions_fixed
;
623 check
->has_corruptions_fixed
= result
.corruptions
!= 0;
624 check
->leaks_fixed
= result
.leaks_fixed
;
625 check
->has_leaks_fixed
= result
.leaks
!= 0;
626 check
->image_end_offset
= result
.image_end_offset
;
627 check
->has_image_end_offset
= result
.image_end_offset
!= 0;
628 check
->total_clusters
= result
.bfi
.total_clusters
;
629 check
->has_total_clusters
= result
.bfi
.total_clusters
!= 0;
630 check
->allocated_clusters
= result
.bfi
.allocated_clusters
;
631 check
->has_allocated_clusters
= result
.bfi
.allocated_clusters
!= 0;
632 check
->fragmented_clusters
= result
.bfi
.fragmented_clusters
;
633 check
->has_fragmented_clusters
= result
.bfi
.fragmented_clusters
!= 0;
634 check
->compressed_clusters
= result
.bfi
.compressed_clusters
;
635 check
->has_compressed_clusters
= result
.bfi
.compressed_clusters
!= 0;
641 * Checks an image for consistency. Exit codes:
643 * 0 - Check completed, image is good
644 * 1 - Check not completed because of internal errors
645 * 2 - Check completed, image is corrupted
646 * 3 - Check completed, image has leaked clusters, but is good otherwise
647 * 63 - Checks are not supported by the image format
649 static int img_check(int argc
, char **argv
)
652 OutputFormat output_format
= OFORMAT_HUMAN
;
653 const char *filename
, *fmt
, *output
, *cache
;
655 BlockDriverState
*bs
;
657 int flags
= BDRV_O_CHECK
;
661 bool image_opts
= false;
662 bool force_share
= false;
666 cache
= BDRV_DEFAULT_CACHE
;
669 int option_index
= 0;
670 static const struct option long_options
[] = {
671 {"help", no_argument
, 0, 'h'},
672 {"format", required_argument
, 0, 'f'},
673 {"repair", required_argument
, 0, 'r'},
674 {"output", required_argument
, 0, OPTION_OUTPUT
},
675 {"object", required_argument
, 0, OPTION_OBJECT
},
676 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
677 {"force-share", no_argument
, 0, 'U'},
680 c
= getopt_long(argc
, argv
, ":hf:r:T:qU",
681 long_options
, &option_index
);
687 missing_argument(argv
[optind
- 1]);
690 unrecognized_option(argv
[optind
- 1]);
699 flags
|= BDRV_O_RDWR
;
701 if (!strcmp(optarg
, "leaks")) {
702 fix
= BDRV_FIX_LEAKS
;
703 } else if (!strcmp(optarg
, "all")) {
704 fix
= BDRV_FIX_LEAKS
| BDRV_FIX_ERRORS
;
706 error_exit("Unknown option value for -r "
707 "(expecting 'leaks' or 'all'): %s", optarg
);
722 case OPTION_OBJECT
: {
724 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
730 case OPTION_IMAGE_OPTS
:
735 if (optind
!= argc
- 1) {
736 error_exit("Expecting one image file name");
738 filename
= argv
[optind
++];
740 if (output
&& !strcmp(output
, "json")) {
741 output_format
= OFORMAT_JSON
;
742 } else if (output
&& !strcmp(output
, "human")) {
743 output_format
= OFORMAT_HUMAN
;
745 error_report("--output must be used with human or json as argument.");
749 if (qemu_opts_foreach(&qemu_object_opts
,
750 user_creatable_add_opts_foreach
,
755 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
757 error_report("Invalid source cache option: %s", cache
);
761 blk
= img_open(image_opts
, filename
, fmt
, flags
, writethrough
, quiet
,
768 check
= g_new0(ImageCheck
, 1);
769 ret
= collect_image_check(bs
, check
, filename
, fmt
, fix
);
771 if (ret
== -ENOTSUP
) {
772 error_report("This image format does not support checks");
777 if (check
->corruptions_fixed
|| check
->leaks_fixed
) {
778 int corruptions_fixed
, leaks_fixed
;
780 leaks_fixed
= check
->leaks_fixed
;
781 corruptions_fixed
= check
->corruptions_fixed
;
783 if (output_format
== OFORMAT_HUMAN
) {
785 "The following inconsistencies were found and repaired:\n\n"
786 " %" PRId64
" leaked clusters\n"
787 " %" PRId64
" corruptions\n\n"
788 "Double checking the fixed image now...\n",
790 check
->corruptions_fixed
);
793 ret
= collect_image_check(bs
, check
, filename
, fmt
, 0);
795 check
->leaks_fixed
= leaks_fixed
;
796 check
->corruptions_fixed
= corruptions_fixed
;
800 switch (output_format
) {
802 dump_human_image_check(check
, quiet
);
805 dump_json_image_check(check
, quiet
);
810 if (ret
|| check
->check_errors
) {
812 error_report("Check failed: %s", strerror(-ret
));
814 error_report("Check failed");
820 if (check
->corruptions
) {
822 } else if (check
->leaks
) {
829 qapi_free_ImageCheck(check
);
834 typedef struct CommonBlockJobCBInfo
{
835 BlockDriverState
*bs
;
837 } CommonBlockJobCBInfo
;
839 static void common_block_job_cb(void *opaque
, int ret
)
841 CommonBlockJobCBInfo
*cbi
= opaque
;
844 error_setg_errno(cbi
->errp
, -ret
, "Block job failed");
848 static void run_block_job(BlockJob
*job
, Error
**errp
)
850 AioContext
*aio_context
= blk_get_aio_context(job
->blk
);
853 aio_context_acquire(aio_context
);
856 aio_poll(aio_context
, true);
857 qemu_progress_print(job
->len
?
858 ((float)job
->offset
/ job
->len
* 100.f
) : 0.0f
, 0);
859 } while (!job
->ready
&& !job
->completed
);
861 if (!job
->completed
) {
862 ret
= block_job_complete_sync(job
, errp
);
866 block_job_unref(job
);
867 aio_context_release(aio_context
);
869 /* publish completion progress only when success */
871 qemu_progress_print(100.f
, 0);
875 static int img_commit(int argc
, char **argv
)
878 const char *filename
, *fmt
, *cache
, *base
;
880 BlockDriverState
*bs
, *base_bs
;
882 bool progress
= false, quiet
= false, drop
= false;
884 Error
*local_err
= NULL
;
885 CommonBlockJobCBInfo cbi
;
886 bool image_opts
= false;
887 AioContext
*aio_context
;
890 cache
= BDRV_DEFAULT_CACHE
;
893 static const struct option long_options
[] = {
894 {"help", no_argument
, 0, 'h'},
895 {"object", required_argument
, 0, OPTION_OBJECT
},
896 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
899 c
= getopt_long(argc
, argv
, ":f:ht:b:dpq",
906 missing_argument(argv
[optind
- 1]);
909 unrecognized_option(argv
[optind
- 1]);
934 case OPTION_OBJECT
: {
936 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
942 case OPTION_IMAGE_OPTS
:
948 /* Progress is not shown in Quiet mode */
953 if (optind
!= argc
- 1) {
954 error_exit("Expecting one image file name");
956 filename
= argv
[optind
++];
958 if (qemu_opts_foreach(&qemu_object_opts
,
959 user_creatable_add_opts_foreach
,
964 flags
= BDRV_O_RDWR
| BDRV_O_UNMAP
;
965 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
967 error_report("Invalid cache option: %s", cache
);
971 blk
= img_open(image_opts
, filename
, fmt
, flags
, writethrough
, quiet
,
978 qemu_progress_init(progress
, 1.f
);
979 qemu_progress_print(0.f
, 100);
982 base_bs
= bdrv_find_backing_image(bs
, base
);
984 error_setg(&local_err
,
985 "Did not find '%s' in the backing chain of '%s'",
990 /* This is different from QMP, which by default uses the deepest file in
991 * the backing chain (i.e., the very base); however, the traditional
992 * behavior of qemu-img commit is using the immediate backing file. */
993 base_bs
= backing_bs(bs
);
995 error_setg(&local_err
, "Image does not have a backing file");
1000 cbi
= (CommonBlockJobCBInfo
){
1005 aio_context
= bdrv_get_aio_context(bs
);
1006 aio_context_acquire(aio_context
);
1007 commit_active_start("commit", bs
, base_bs
, BLOCK_JOB_DEFAULT
, 0,
1008 BLOCKDEV_ON_ERROR_REPORT
, NULL
, common_block_job_cb
,
1009 &cbi
, false, &local_err
);
1010 aio_context_release(aio_context
);
1015 /* When the block job completes, the BlockBackend reference will point to
1016 * the old backing file. In order to avoid that the top image is already
1017 * deleted, so we can still empty it afterwards, increment the reference
1018 * counter here preemptively. */
1023 job
= block_job_get("commit");
1024 run_block_job(job
, &local_err
);
1029 if (!drop
&& bs
->drv
->bdrv_make_empty
) {
1030 ret
= bs
->drv
->bdrv_make_empty(bs
);
1032 error_setg_errno(&local_err
, -ret
, "Could not empty %s",
1044 qemu_progress_end();
1049 error_report_err(local_err
);
1053 qprintf(quiet
, "Image committed.\n");
1058 * Returns true iff the first sector pointed to by 'buf' contains at least
1061 * 'pnum' is set to the number of sectors (including and immediately following
1062 * the first one) that are known to be in the same allocated/unallocated state.
1064 static int is_allocated_sectors(const uint8_t *buf
, int n
, int *pnum
)
1073 is_zero
= buffer_is_zero(buf
, 512);
1074 for(i
= 1; i
< n
; i
++) {
1076 if (is_zero
!= buffer_is_zero(buf
, 512)) {
1085 * Like is_allocated_sectors, but if the buffer starts with a used sector,
1086 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1087 * breaking up write requests for only small sparse areas.
1089 static int is_allocated_sectors_min(const uint8_t *buf
, int n
, int *pnum
,
1093 int num_checked
, num_used
;
1099 ret
= is_allocated_sectors(buf
, n
, pnum
);
1105 buf
+= BDRV_SECTOR_SIZE
* *pnum
;
1107 num_checked
= num_used
;
1110 ret
= is_allocated_sectors(buf
, n
, pnum
);
1112 buf
+= BDRV_SECTOR_SIZE
* *pnum
;
1114 num_checked
+= *pnum
;
1116 num_used
= num_checked
;
1117 } else if (*pnum
>= min
) {
1127 * Compares two buffers sector by sector. Returns 0 if the first sector of both
1128 * buffers matches, non-zero otherwise.
1130 * pnum is set to the number of sectors (including and immediately following
1131 * the first one) that are known to have the same comparison result
1133 static int compare_sectors(const uint8_t *buf1
, const uint8_t *buf2
, int n
,
1144 res
= !!memcmp(buf1
, buf2
, 512);
1145 for(i
= 1; i
< n
; i
++) {
1149 if (!!memcmp(buf1
, buf2
, 512) != res
) {
1158 #define IO_BUF_SIZE (2 * 1024 * 1024)
1160 static int64_t sectors_to_bytes(int64_t sectors
)
1162 return sectors
<< BDRV_SECTOR_BITS
;
1165 static int64_t sectors_to_process(int64_t total
, int64_t from
)
1167 return MIN(total
- from
, IO_BUF_SIZE
>> BDRV_SECTOR_BITS
);
1171 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1173 * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero
1174 * data and negative value on error.
1176 * @param blk: BlockBackend for the image
1177 * @param sect_num: Number of first sector to check
1178 * @param sect_count: Number of sectors to check
1179 * @param filename: Name of disk file we are checking (logging purpose)
1180 * @param buffer: Allocated buffer for storing read data
1181 * @param quiet: Flag for quiet mode
1183 static int check_empty_sectors(BlockBackend
*blk
, int64_t sect_num
,
1184 int sect_count
, const char *filename
,
1185 uint8_t *buffer
, bool quiet
)
1188 ret
= blk_pread(blk
, sect_num
<< BDRV_SECTOR_BITS
, buffer
,
1189 sect_count
<< BDRV_SECTOR_BITS
);
1191 error_report("Error while reading offset %" PRId64
" of %s: %s",
1192 sectors_to_bytes(sect_num
), filename
, strerror(-ret
));
1195 ret
= is_allocated_sectors(buffer
, sect_count
, &pnum
);
1196 if (ret
|| pnum
!= sect_count
) {
1197 qprintf(quiet
, "Content mismatch at offset %" PRId64
"!\n",
1198 sectors_to_bytes(ret
? sect_num
: sect_num
+ pnum
));
1206 * Compares two images. Exit codes:
1208 * 0 - Images are identical
1210 * >1 - Error occurred
1212 static int img_compare(int argc
, char **argv
)
1214 const char *fmt1
= NULL
, *fmt2
= NULL
, *cache
, *filename1
, *filename2
;
1215 BlockBackend
*blk1
, *blk2
;
1216 BlockDriverState
*bs1
, *bs2
;
1217 int64_t total_sectors1
, total_sectors2
;
1218 uint8_t *buf1
= NULL
, *buf2
= NULL
;
1220 int allocated1
, allocated2
;
1221 int ret
= 0; /* return value - 0 Ident, 1 Different, >1 Error */
1222 bool progress
= false, quiet
= false, strict
= false;
1225 int64_t total_sectors
;
1226 int64_t sector_num
= 0;
1229 uint64_t progress_base
;
1230 bool image_opts
= false;
1231 bool force_share
= false;
1233 cache
= BDRV_DEFAULT_CACHE
;
1235 static const struct option long_options
[] = {
1236 {"help", no_argument
, 0, 'h'},
1237 {"object", required_argument
, 0, OPTION_OBJECT
},
1238 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
1239 {"force-share", no_argument
, 0, 'U'},
1242 c
= getopt_long(argc
, argv
, ":hf:F:T:pqsU",
1243 long_options
, NULL
);
1249 missing_argument(argv
[optind
- 1]);
1252 unrecognized_option(argv
[optind
- 1]);
1278 case OPTION_OBJECT
: {
1280 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
1287 case OPTION_IMAGE_OPTS
:
1293 /* Progress is not shown in Quiet mode */
1299 if (optind
!= argc
- 2) {
1300 error_exit("Expecting two image file names");
1302 filename1
= argv
[optind
++];
1303 filename2
= argv
[optind
++];
1305 if (qemu_opts_foreach(&qemu_object_opts
,
1306 user_creatable_add_opts_foreach
,
1312 /* Initialize before goto out */
1313 qemu_progress_init(progress
, 2.0);
1316 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
1318 error_report("Invalid source cache option: %s", cache
);
1323 blk1
= img_open(image_opts
, filename1
, fmt1
, flags
, writethrough
, quiet
,
1330 blk2
= img_open(image_opts
, filename2
, fmt2
, flags
, writethrough
, quiet
,
1339 buf1
= blk_blockalign(blk1
, IO_BUF_SIZE
);
1340 buf2
= blk_blockalign(blk2
, IO_BUF_SIZE
);
1341 total_sectors1
= blk_nb_sectors(blk1
);
1342 if (total_sectors1
< 0) {
1343 error_report("Can't get size of %s: %s",
1344 filename1
, strerror(-total_sectors1
));
1348 total_sectors2
= blk_nb_sectors(blk2
);
1349 if (total_sectors2
< 0) {
1350 error_report("Can't get size of %s: %s",
1351 filename2
, strerror(-total_sectors2
));
1355 total_sectors
= MIN(total_sectors1
, total_sectors2
);
1356 progress_base
= MAX(total_sectors1
, total_sectors2
);
1358 qemu_progress_print(0, 100);
1360 if (strict
&& total_sectors1
!= total_sectors2
) {
1362 qprintf(quiet
, "Strict mode: Image size mismatch!\n");
1367 int64_t status1
, status2
;
1368 BlockDriverState
*file
;
1370 nb_sectors
= sectors_to_process(total_sectors
, sector_num
);
1371 if (nb_sectors
<= 0) {
1374 status1
= bdrv_get_block_status_above(bs1
, NULL
, sector_num
,
1375 total_sectors1
- sector_num
,
1379 error_report("Sector allocation test failed for %s", filename1
);
1382 allocated1
= status1
& BDRV_BLOCK_ALLOCATED
;
1384 status2
= bdrv_get_block_status_above(bs2
, NULL
, sector_num
,
1385 total_sectors2
- sector_num
,
1389 error_report("Sector allocation test failed for %s", filename2
);
1392 allocated2
= status2
& BDRV_BLOCK_ALLOCATED
;
1394 nb_sectors
= MIN(nb_sectors
, pnum1
);
1397 nb_sectors
= MIN(nb_sectors
, pnum2
);
1401 if ((status1
& ~BDRV_BLOCK_OFFSET_MASK
) !=
1402 (status2
& ~BDRV_BLOCK_OFFSET_MASK
)) {
1404 qprintf(quiet
, "Strict mode: Offset %" PRId64
1405 " block status mismatch!\n",
1406 sectors_to_bytes(sector_num
));
1410 if ((status1
& BDRV_BLOCK_ZERO
) && (status2
& BDRV_BLOCK_ZERO
)) {
1411 nb_sectors
= MIN(pnum1
, pnum2
);
1412 } else if (allocated1
== allocated2
) {
1414 ret
= blk_pread(blk1
, sector_num
<< BDRV_SECTOR_BITS
, buf1
,
1415 nb_sectors
<< BDRV_SECTOR_BITS
);
1417 error_report("Error while reading offset %" PRId64
" of %s:"
1418 " %s", sectors_to_bytes(sector_num
), filename1
,
1423 ret
= blk_pread(blk2
, sector_num
<< BDRV_SECTOR_BITS
, buf2
,
1424 nb_sectors
<< BDRV_SECTOR_BITS
);
1426 error_report("Error while reading offset %" PRId64
1427 " of %s: %s", sectors_to_bytes(sector_num
),
1428 filename2
, strerror(-ret
));
1432 ret
= compare_sectors(buf1
, buf2
, nb_sectors
, &pnum
);
1433 if (ret
|| pnum
!= nb_sectors
) {
1434 qprintf(quiet
, "Content mismatch at offset %" PRId64
"!\n",
1436 ret
? sector_num
: sector_num
+ pnum
));
1444 ret
= check_empty_sectors(blk1
, sector_num
, nb_sectors
,
1445 filename1
, buf1
, quiet
);
1447 ret
= check_empty_sectors(blk2
, sector_num
, nb_sectors
,
1448 filename2
, buf1
, quiet
);
1452 error_report("Error while reading offset %" PRId64
": %s",
1453 sectors_to_bytes(sector_num
), strerror(-ret
));
1459 sector_num
+= nb_sectors
;
1460 qemu_progress_print(((float) nb_sectors
/ progress_base
)*100, 100);
1463 if (total_sectors1
!= total_sectors2
) {
1464 BlockBackend
*blk_over
;
1465 int64_t total_sectors_over
;
1466 const char *filename_over
;
1468 qprintf(quiet
, "Warning: Image size mismatch!\n");
1469 if (total_sectors1
> total_sectors2
) {
1470 total_sectors_over
= total_sectors1
;
1472 filename_over
= filename1
;
1474 total_sectors_over
= total_sectors2
;
1476 filename_over
= filename2
;
1482 nb_sectors
= sectors_to_process(total_sectors_over
, sector_num
);
1483 if (nb_sectors
<= 0) {
1486 ret
= bdrv_is_allocated_above(blk_bs(blk_over
), NULL
,
1487 sector_num
* BDRV_SECTOR_SIZE
,
1488 nb_sectors
* BDRV_SECTOR_SIZE
,
1492 error_report("Sector allocation test failed for %s",
1497 /* TODO relax this once bdrv_is_allocated_above does not enforce
1498 * sector alignment */
1499 assert(QEMU_IS_ALIGNED(count
, BDRV_SECTOR_SIZE
));
1500 nb_sectors
= count
>> BDRV_SECTOR_BITS
;
1502 ret
= check_empty_sectors(blk_over
, sector_num
, nb_sectors
,
1503 filename_over
, buf1
, quiet
);
1506 error_report("Error while reading offset %" PRId64
1507 " of %s: %s", sectors_to_bytes(sector_num
),
1508 filename_over
, strerror(-ret
));
1514 sector_num
+= nb_sectors
;
1515 qemu_progress_print(((float) nb_sectors
/ progress_base
)*100, 100);
1519 qprintf(quiet
, "Images are identical.\n");
1529 qemu_progress_end();
1534 enum ImgConvertBlockStatus
{
1540 #define MAX_COROUTINES 16
1542 typedef struct ImgConvertState
{
1544 int64_t *src_sectors
;
1546 int64_t total_sectors
;
1547 int64_t allocated_sectors
;
1548 int64_t allocated_done
;
1551 enum ImgConvertBlockStatus status
;
1552 int64_t sector_next_status
;
1553 BlockBackend
*target
;
1556 bool target_has_backing
;
1559 size_t cluster_sectors
;
1561 long num_coroutines
;
1562 int running_coroutines
;
1563 Coroutine
*co
[MAX_COROUTINES
];
1564 int64_t wait_sector_num
[MAX_COROUTINES
];
1569 static void convert_select_part(ImgConvertState
*s
, int64_t sector_num
,
1570 int *src_cur
, int64_t *src_cur_offset
)
1573 *src_cur_offset
= 0;
1574 while (sector_num
- *src_cur_offset
>= s
->src_sectors
[*src_cur
]) {
1575 *src_cur_offset
+= s
->src_sectors
[*src_cur
];
1577 assert(*src_cur
< s
->src_num
);
1581 static int convert_iteration_sectors(ImgConvertState
*s
, int64_t sector_num
)
1583 int64_t ret
, src_cur_offset
;
1586 convert_select_part(s
, sector_num
, &src_cur
, &src_cur_offset
);
1588 assert(s
->total_sectors
> sector_num
);
1589 n
= MIN(s
->total_sectors
- sector_num
, BDRV_REQUEST_MAX_SECTORS
);
1591 if (s
->sector_next_status
<= sector_num
) {
1592 BlockDriverState
*file
;
1593 if (s
->target_has_backing
) {
1594 ret
= bdrv_get_block_status(blk_bs(s
->src
[src_cur
]),
1595 sector_num
- src_cur_offset
,
1598 ret
= bdrv_get_block_status_above(blk_bs(s
->src
[src_cur
]), NULL
,
1599 sector_num
- src_cur_offset
,
1606 if (ret
& BDRV_BLOCK_ZERO
) {
1607 s
->status
= BLK_ZERO
;
1608 } else if (ret
& BDRV_BLOCK_DATA
) {
1609 s
->status
= BLK_DATA
;
1611 s
->status
= s
->target_has_backing
? BLK_BACKING_FILE
: BLK_DATA
;
1614 s
->sector_next_status
= sector_num
+ n
;
1617 n
= MIN(n
, s
->sector_next_status
- sector_num
);
1618 if (s
->status
== BLK_DATA
) {
1619 n
= MIN(n
, s
->buf_sectors
);
1622 /* We need to write complete clusters for compressed images, so if an
1623 * unallocated area is shorter than that, we must consider the whole
1624 * cluster allocated. */
1625 if (s
->compressed
) {
1626 if (n
< s
->cluster_sectors
) {
1627 n
= MIN(s
->cluster_sectors
, s
->total_sectors
- sector_num
);
1628 s
->status
= BLK_DATA
;
1630 n
= QEMU_ALIGN_DOWN(n
, s
->cluster_sectors
);
1637 static int coroutine_fn
convert_co_read(ImgConvertState
*s
, int64_t sector_num
,
1638 int nb_sectors
, uint8_t *buf
)
1644 assert(nb_sectors
<= s
->buf_sectors
);
1645 while (nb_sectors
> 0) {
1648 int64_t bs_sectors
, src_cur_offset
;
1650 /* In the case of compression with multiple source files, we can get a
1651 * nb_sectors that spreads into the next part. So we must be able to
1652 * read across multiple BDSes for one convert_read() call. */
1653 convert_select_part(s
, sector_num
, &src_cur
, &src_cur_offset
);
1654 blk
= s
->src
[src_cur
];
1655 bs_sectors
= s
->src_sectors
[src_cur
];
1657 n
= MIN(nb_sectors
, bs_sectors
- (sector_num
- src_cur_offset
));
1659 iov
.iov_len
= n
<< BDRV_SECTOR_BITS
;
1660 qemu_iovec_init_external(&qiov
, &iov
, 1);
1662 ret
= blk_co_preadv(
1663 blk
, (sector_num
- src_cur_offset
) << BDRV_SECTOR_BITS
,
1664 n
<< BDRV_SECTOR_BITS
, &qiov
, 0);
1671 buf
+= n
* BDRV_SECTOR_SIZE
;
1678 static int coroutine_fn
convert_co_write(ImgConvertState
*s
, int64_t sector_num
,
1679 int nb_sectors
, uint8_t *buf
,
1680 enum ImgConvertBlockStatus status
)
1686 while (nb_sectors
> 0) {
1688 BdrvRequestFlags flags
= s
->compressed
? BDRV_REQ_WRITE_COMPRESSED
: 0;
1691 case BLK_BACKING_FILE
:
1692 /* If we have a backing file, leave clusters unallocated that are
1693 * unallocated in the source image, so that the backing file is
1694 * visible at the respective offset. */
1695 assert(s
->target_has_backing
);
1699 /* If we're told to keep the target fully allocated (-S 0) or there
1700 * is real non-zero data, we must write it. Otherwise we can treat
1701 * it as zero sectors.
1702 * Compressed clusters need to be written as a whole, so in that
1703 * case we can only save the write if the buffer is completely
1705 if (!s
->min_sparse
||
1707 is_allocated_sectors_min(buf
, n
, &n
, s
->min_sparse
)) ||
1709 !buffer_is_zero(buf
, n
* BDRV_SECTOR_SIZE
)))
1712 iov
.iov_len
= n
<< BDRV_SECTOR_BITS
;
1713 qemu_iovec_init_external(&qiov
, &iov
, 1);
1715 ret
= blk_co_pwritev(s
->target
, sector_num
<< BDRV_SECTOR_BITS
,
1716 n
<< BDRV_SECTOR_BITS
, &qiov
, flags
);
1725 if (s
->has_zero_init
) {
1726 assert(!s
->target_has_backing
);
1729 ret
= blk_co_pwrite_zeroes(s
->target
,
1730 sector_num
<< BDRV_SECTOR_BITS
,
1731 n
<< BDRV_SECTOR_BITS
, 0);
1740 buf
+= n
* BDRV_SECTOR_SIZE
;
1746 static void coroutine_fn
convert_co_do_copy(void *opaque
)
1748 ImgConvertState
*s
= opaque
;
1749 uint8_t *buf
= NULL
;
1753 for (i
= 0; i
< s
->num_coroutines
; i
++) {
1754 if (s
->co
[i
] == qemu_coroutine_self()) {
1761 s
->running_coroutines
++;
1762 buf
= blk_blockalign(s
->target
, s
->buf_sectors
* BDRV_SECTOR_SIZE
);
1767 enum ImgConvertBlockStatus status
;
1769 qemu_co_mutex_lock(&s
->lock
);
1770 if (s
->ret
!= -EINPROGRESS
|| s
->sector_num
>= s
->total_sectors
) {
1771 qemu_co_mutex_unlock(&s
->lock
);
1774 n
= convert_iteration_sectors(s
, s
->sector_num
);
1776 qemu_co_mutex_unlock(&s
->lock
);
1780 /* save current sector and allocation status to local variables */
1781 sector_num
= s
->sector_num
;
1783 if (!s
->min_sparse
&& s
->status
== BLK_ZERO
) {
1784 n
= MIN(n
, s
->buf_sectors
);
1786 /* increment global sector counter so that other coroutines can
1787 * already continue reading beyond this request */
1789 qemu_co_mutex_unlock(&s
->lock
);
1791 if (status
== BLK_DATA
|| (!s
->min_sparse
&& status
== BLK_ZERO
)) {
1792 s
->allocated_done
+= n
;
1793 qemu_progress_print(100.0 * s
->allocated_done
/
1794 s
->allocated_sectors
, 0);
1797 if (status
== BLK_DATA
) {
1798 ret
= convert_co_read(s
, sector_num
, n
, buf
);
1800 error_report("error while reading sector %" PRId64
1801 ": %s", sector_num
, strerror(-ret
));
1804 } else if (!s
->min_sparse
&& status
== BLK_ZERO
) {
1806 memset(buf
, 0x00, n
* BDRV_SECTOR_SIZE
);
1809 if (s
->wr_in_order
) {
1810 /* keep writes in order */
1811 while (s
->wr_offs
!= sector_num
&& s
->ret
== -EINPROGRESS
) {
1812 s
->wait_sector_num
[index
] = sector_num
;
1813 qemu_coroutine_yield();
1815 s
->wait_sector_num
[index
] = -1;
1818 if (s
->ret
== -EINPROGRESS
) {
1819 ret
= convert_co_write(s
, sector_num
, n
, buf
, status
);
1821 error_report("error while writing sector %" PRId64
1822 ": %s", sector_num
, strerror(-ret
));
1827 if (s
->wr_in_order
) {
1828 /* reenter the coroutine that might have waited
1829 * for this write to complete */
1830 s
->wr_offs
= sector_num
+ n
;
1831 for (i
= 0; i
< s
->num_coroutines
; i
++) {
1832 if (s
->co
[i
] && s
->wait_sector_num
[i
] == s
->wr_offs
) {
1834 * A -> B -> A cannot occur because A has
1835 * s->wait_sector_num[i] == -1 during A -> B. Therefore
1836 * B will never enter A during this time window.
1838 qemu_coroutine_enter(s
->co
[i
]);
1846 s
->co
[index
] = NULL
;
1847 s
->running_coroutines
--;
1848 if (!s
->running_coroutines
&& s
->ret
== -EINPROGRESS
) {
1849 /* the convert job finished successfully */
1854 static int convert_do_copy(ImgConvertState
*s
)
1857 int64_t sector_num
= 0;
1859 /* Check whether we have zero initialisation or can get it efficiently */
1860 s
->has_zero_init
= s
->min_sparse
&& !s
->target_has_backing
1861 ? bdrv_has_zero_init(blk_bs(s
->target
))
1864 if (!s
->has_zero_init
&& !s
->target_has_backing
&&
1865 bdrv_can_write_zeroes_with_unmap(blk_bs(s
->target
)))
1867 ret
= blk_make_zero(s
->target
, BDRV_REQ_MAY_UNMAP
);
1869 s
->has_zero_init
= true;
1873 /* Allocate buffer for copied data. For compressed images, only one cluster
1874 * can be copied at a time. */
1875 if (s
->compressed
) {
1876 if (s
->cluster_sectors
<= 0 || s
->cluster_sectors
> s
->buf_sectors
) {
1877 error_report("invalid cluster size");
1880 s
->buf_sectors
= s
->cluster_sectors
;
1883 while (sector_num
< s
->total_sectors
) {
1884 n
= convert_iteration_sectors(s
, sector_num
);
1888 if (s
->status
== BLK_DATA
|| (!s
->min_sparse
&& s
->status
== BLK_ZERO
))
1890 s
->allocated_sectors
+= n
;
1896 s
->sector_next_status
= 0;
1897 s
->ret
= -EINPROGRESS
;
1899 qemu_co_mutex_init(&s
->lock
);
1900 for (i
= 0; i
< s
->num_coroutines
; i
++) {
1901 s
->co
[i
] = qemu_coroutine_create(convert_co_do_copy
, s
);
1902 s
->wait_sector_num
[i
] = -1;
1903 qemu_coroutine_enter(s
->co
[i
]);
1906 while (s
->running_coroutines
) {
1907 main_loop_wait(false);
1910 if (s
->compressed
&& !s
->ret
) {
1911 /* signal EOF to align */
1912 ret
= blk_pwrite_compressed(s
->target
, 0, NULL
, 0);
1921 static int img_convert(int argc
, char **argv
)
1923 int c
, bs_i
, flags
, src_flags
= 0;
1924 const char *fmt
= NULL
, *out_fmt
= NULL
, *cache
= "unsafe",
1925 *src_cache
= BDRV_DEFAULT_CACHE
, *out_baseimg
= NULL
,
1926 *out_filename
, *out_baseimg_param
, *snapshot_name
= NULL
;
1927 BlockDriver
*drv
= NULL
, *proto_drv
= NULL
;
1928 BlockDriverInfo bdi
;
1929 BlockDriverState
*out_bs
;
1930 QemuOpts
*opts
= NULL
, *sn_opts
= NULL
;
1931 QemuOptsList
*create_opts
= NULL
;
1932 char *options
= NULL
;
1933 Error
*local_err
= NULL
;
1934 bool writethrough
, src_writethrough
, quiet
= false, image_opts
= false,
1935 skip_create
= false, progress
= false, tgt_image_opts
= false;
1936 int64_t ret
= -EINVAL
;
1937 bool force_share
= false;
1939 ImgConvertState s
= (ImgConvertState
) {
1940 /* Need at least 4k of zeros for sparse detection */
1942 .buf_sectors
= IO_BUF_SIZE
/ BDRV_SECTOR_SIZE
,
1943 .wr_in_order
= true,
1944 .num_coroutines
= 8,
1948 static const struct option long_options
[] = {
1949 {"help", no_argument
, 0, 'h'},
1950 {"object", required_argument
, 0, OPTION_OBJECT
},
1951 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
1952 {"force-share", no_argument
, 0, 'U'},
1953 {"target-image-opts", no_argument
, 0, OPTION_TARGET_IMAGE_OPTS
},
1956 c
= getopt_long(argc
, argv
, ":hf:O:B:co:s:l:S:pt:T:qnm:WU",
1957 long_options
, NULL
);
1963 missing_argument(argv
[optind
- 1]);
1966 unrecognized_option(argv
[optind
- 1]);
1978 out_baseimg
= optarg
;
1981 s
.compressed
= true;
1984 if (!is_valid_option_list(optarg
)) {
1985 error_report("Invalid option list: %s", optarg
);
1989 options
= g_strdup(optarg
);
1991 char *old_options
= options
;
1992 options
= g_strdup_printf("%s,%s", options
, optarg
);
1993 g_free(old_options
);
1997 snapshot_name
= optarg
;
2000 if (strstart(optarg
, SNAPSHOT_OPT_BASE
, NULL
)) {
2001 sn_opts
= qemu_opts_parse_noisily(&internal_snapshot_opts
,
2004 error_report("Failed in parsing snapshot param '%s'",
2009 snapshot_name
= optarg
;
2016 sval
= cvtnum(optarg
);
2018 error_report("Invalid minimum zero buffer size for sparse output specified");
2022 s
.min_sparse
= sval
/ BDRV_SECTOR_SIZE
;
2041 if (qemu_strtol(optarg
, NULL
, 0, &s
.num_coroutines
) ||
2042 s
.num_coroutines
< 1 || s
.num_coroutines
> MAX_COROUTINES
) {
2043 error_report("Invalid number of coroutines. Allowed number of"
2044 " coroutines is between 1 and %d", MAX_COROUTINES
);
2049 s
.wr_in_order
= false;
2054 case OPTION_OBJECT
: {
2055 QemuOpts
*object_opts
;
2056 object_opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
2063 case OPTION_IMAGE_OPTS
:
2066 case OPTION_TARGET_IMAGE_OPTS
:
2067 tgt_image_opts
= true;
2072 if (!out_fmt
&& !tgt_image_opts
) {
2076 if (qemu_opts_foreach(&qemu_object_opts
,
2077 user_creatable_add_opts_foreach
,
2082 if (!s
.wr_in_order
&& s
.compressed
) {
2083 error_report("Out of order write and compress are mutually exclusive");
2087 if (tgt_image_opts
&& !skip_create
) {
2088 error_report("--target-image-opts requires use of -n flag");
2092 s
.src_num
= argc
- optind
- 1;
2093 out_filename
= s
.src_num
>= 1 ? argv
[argc
- 1] : NULL
;
2095 if (options
&& has_help_option(options
)) {
2097 ret
= print_block_option_help(out_filename
, out_fmt
);
2100 error_report("Option help requires a format be specified");
2105 if (s
.src_num
< 1) {
2106 error_report("Must specify image file name");
2111 /* ret is still -EINVAL until here */
2112 ret
= bdrv_parse_cache_mode(src_cache
, &src_flags
, &src_writethrough
);
2114 error_report("Invalid source cache option: %s", src_cache
);
2118 /* Initialize before goto out */
2122 qemu_progress_init(progress
, 1.0);
2123 qemu_progress_print(0, 100);
2125 s
.src
= g_new0(BlockBackend
*, s
.src_num
);
2126 s
.src_sectors
= g_new(int64_t, s
.src_num
);
2128 for (bs_i
= 0; bs_i
< s
.src_num
; bs_i
++) {
2129 s
.src
[bs_i
] = img_open(image_opts
, argv
[optind
+ bs_i
],
2130 fmt
, src_flags
, src_writethrough
, quiet
,
2136 s
.src_sectors
[bs_i
] = blk_nb_sectors(s
.src
[bs_i
]);
2137 if (s
.src_sectors
[bs_i
] < 0) {
2138 error_report("Could not get size of %s: %s",
2139 argv
[optind
+ bs_i
], strerror(-s
.src_sectors
[bs_i
]));
2143 s
.total_sectors
+= s
.src_sectors
[bs_i
];
2147 bdrv_snapshot_load_tmp(blk_bs(s
.src
[0]),
2148 qemu_opt_get(sn_opts
, SNAPSHOT_OPT_ID
),
2149 qemu_opt_get(sn_opts
, SNAPSHOT_OPT_NAME
),
2151 } else if (snapshot_name
!= NULL
) {
2152 if (s
.src_num
> 1) {
2153 error_report("No support for concatenating multiple snapshot");
2158 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s
.src
[0]), snapshot_name
,
2162 error_reportf_err(local_err
, "Failed to load snapshot: ");
2168 /* Find driver and parse its options */
2169 drv
= bdrv_find_format(out_fmt
);
2171 error_report("Unknown file format '%s'", out_fmt
);
2176 proto_drv
= bdrv_find_protocol(out_filename
, true, &local_err
);
2178 error_report_err(local_err
);
2183 if (!drv
->create_opts
) {
2184 error_report("Format driver '%s' does not support image creation",
2190 if (!proto_drv
->create_opts
) {
2191 error_report("Protocol driver '%s' does not support image creation",
2192 proto_drv
->format_name
);
2197 create_opts
= qemu_opts_append(create_opts
, drv
->create_opts
);
2198 create_opts
= qemu_opts_append(create_opts
, proto_drv
->create_opts
);
2200 opts
= qemu_opts_create(create_opts
, NULL
, 0, &error_abort
);
2202 qemu_opts_do_parse(opts
, options
, NULL
, &local_err
);
2204 error_report_err(local_err
);
2210 qemu_opt_set_number(opts
, BLOCK_OPT_SIZE
, s
.total_sectors
* 512,
2212 ret
= add_old_style_options(out_fmt
, opts
, out_baseimg
, NULL
);
2218 /* Get backing file name if -o backing_file was used */
2219 out_baseimg_param
= qemu_opt_get(opts
, BLOCK_OPT_BACKING_FILE
);
2220 if (out_baseimg_param
) {
2221 out_baseimg
= out_baseimg_param
;
2223 s
.target_has_backing
= (bool) out_baseimg
;
2225 if (s
.src_num
> 1 && out_baseimg
) {
2226 error_report("Having a backing file for the target makes no sense when "
2227 "concatenating multiple input images");
2232 /* Check if compression is supported */
2235 qemu_opt_get_bool(opts
, BLOCK_OPT_ENCRYPT
, false);
2236 const char *encryptfmt
=
2237 qemu_opt_get(opts
, BLOCK_OPT_ENCRYPT_FORMAT
);
2238 const char *preallocation
=
2239 qemu_opt_get(opts
, BLOCK_OPT_PREALLOC
);
2241 if (drv
&& !drv
->bdrv_co_pwritev_compressed
) {
2242 error_report("Compression not supported for this file format");
2247 if (encryption
|| encryptfmt
) {
2248 error_report("Compression and encryption not supported at "
2255 && strcmp(preallocation
, "off"))
2257 error_report("Compression and preallocation not supported at "
2265 /* Create the new image */
2266 ret
= bdrv_create(drv
, out_filename
, opts
, &local_err
);
2268 error_reportf_err(local_err
, "%s: error while converting %s: ",
2269 out_filename
, out_fmt
);
2274 flags
= s
.min_sparse
? (BDRV_O_RDWR
| BDRV_O_UNMAP
) : BDRV_O_RDWR
;
2275 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
2277 error_report("Invalid cache option: %s", cache
);
2282 s
.target
= img_open(tgt_image_opts
, out_filename
, out_fmt
,
2283 flags
, writethrough
, quiet
, false);
2285 /* TODO ultimately we should allow --target-image-opts
2286 * to be used even when -n is not given.
2287 * That has to wait for bdrv_create to be improved
2288 * to allow filenames in option syntax
2290 s
.target
= img_open_new_file(out_filename
, opts
, out_fmt
,
2291 flags
, writethrough
, quiet
, false);
2297 out_bs
= blk_bs(s
.target
);
2299 if (s
.compressed
&& !out_bs
->drv
->bdrv_co_pwritev_compressed
) {
2300 error_report("Compression not supported for this file format");
2305 /* increase bufsectors from the default 4096 (2M) if opt_transfer
2306 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
2308 s
.buf_sectors
= MIN(32768,
2310 MAX(out_bs
->bl
.opt_transfer
>> BDRV_SECTOR_BITS
,
2311 out_bs
->bl
.pdiscard_alignment
>>
2312 BDRV_SECTOR_BITS
)));
2315 int64_t output_sectors
= blk_nb_sectors(s
.target
);
2316 if (output_sectors
< 0) {
2317 error_report("unable to get output image length: %s",
2318 strerror(-output_sectors
));
2321 } else if (output_sectors
< s
.total_sectors
) {
2322 error_report("output file is smaller than input file");
2328 ret
= bdrv_get_info(out_bs
, &bdi
);
2331 error_report("could not get block driver info");
2335 s
.compressed
= s
.compressed
|| bdi
.needs_compressed_writes
;
2336 s
.cluster_sectors
= bdi
.cluster_size
/ BDRV_SECTOR_SIZE
;
2339 ret
= convert_do_copy(&s
);
2342 qemu_progress_print(100, 0);
2344 qemu_progress_end();
2345 qemu_opts_del(opts
);
2346 qemu_opts_free(create_opts
);
2347 qemu_opts_del(sn_opts
);
2348 blk_unref(s
.target
);
2350 for (bs_i
= 0; bs_i
< s
.src_num
; bs_i
++) {
2351 blk_unref(s
.src
[bs_i
]);
2355 g_free(s
.src_sectors
);
2363 static void dump_snapshots(BlockDriverState
*bs
)
2365 QEMUSnapshotInfo
*sn_tab
, *sn
;
2368 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
2371 printf("Snapshot list:\n");
2372 bdrv_snapshot_dump(fprintf
, stdout
, NULL
);
2374 for(i
= 0; i
< nb_sns
; i
++) {
2376 bdrv_snapshot_dump(fprintf
, stdout
, sn
);
2382 static void dump_json_image_info_list(ImageInfoList
*list
)
2386 Visitor
*v
= qobject_output_visitor_new(&obj
);
2388 visit_type_ImageInfoList(v
, NULL
, &list
, &error_abort
);
2389 visit_complete(v
, &obj
);
2390 str
= qobject_to_json_pretty(obj
);
2391 assert(str
!= NULL
);
2392 printf("%s\n", qstring_get_str(str
));
2393 qobject_decref(obj
);
2398 static void dump_json_image_info(ImageInfo
*info
)
2402 Visitor
*v
= qobject_output_visitor_new(&obj
);
2404 visit_type_ImageInfo(v
, NULL
, &info
, &error_abort
);
2405 visit_complete(v
, &obj
);
2406 str
= qobject_to_json_pretty(obj
);
2407 assert(str
!= NULL
);
2408 printf("%s\n", qstring_get_str(str
));
2409 qobject_decref(obj
);
2414 static void dump_human_image_info_list(ImageInfoList
*list
)
2416 ImageInfoList
*elem
;
2419 for (elem
= list
; elem
; elem
= elem
->next
) {
2425 bdrv_image_info_dump(fprintf
, stdout
, elem
->value
);
2429 static gboolean
str_equal_func(gconstpointer a
, gconstpointer b
)
2431 return strcmp(a
, b
) == 0;
2435 * Open an image file chain and return an ImageInfoList
2437 * @filename: topmost image filename
2438 * @fmt: topmost image format (may be NULL to autodetect)
2439 * @chain: true - enumerate entire backing file chain
2440 * false - only topmost image file
2442 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2443 * image file. If there was an error a message will have been printed to
2446 static ImageInfoList
*collect_image_info_list(bool image_opts
,
2447 const char *filename
,
2449 bool chain
, bool force_share
)
2451 ImageInfoList
*head
= NULL
;
2452 ImageInfoList
**last
= &head
;
2453 GHashTable
*filenames
;
2456 filenames
= g_hash_table_new_full(g_str_hash
, str_equal_func
, NULL
, NULL
);
2460 BlockDriverState
*bs
;
2462 ImageInfoList
*elem
;
2464 if (g_hash_table_lookup_extended(filenames
, filename
, NULL
, NULL
)) {
2465 error_report("Backing file '%s' creates an infinite loop.",
2469 g_hash_table_insert(filenames
, (gpointer
)filename
, NULL
);
2471 blk
= img_open(image_opts
, filename
, fmt
,
2472 BDRV_O_NO_BACKING
| BDRV_O_NO_IO
, false, false,
2479 bdrv_query_image_info(bs
, &info
, &err
);
2481 error_report_err(err
);
2486 elem
= g_new0(ImageInfoList
, 1);
2493 filename
= fmt
= NULL
;
2495 if (info
->has_full_backing_filename
) {
2496 filename
= info
->full_backing_filename
;
2497 } else if (info
->has_backing_filename
) {
2498 error_report("Could not determine absolute backing filename,"
2499 " but backing filename '%s' present",
2500 info
->backing_filename
);
2503 if (info
->has_backing_filename_format
) {
2504 fmt
= info
->backing_filename_format
;
2508 g_hash_table_destroy(filenames
);
2512 qapi_free_ImageInfoList(head
);
2513 g_hash_table_destroy(filenames
);
2517 static int img_info(int argc
, char **argv
)
2520 OutputFormat output_format
= OFORMAT_HUMAN
;
2522 const char *filename
, *fmt
, *output
;
2523 ImageInfoList
*list
;
2524 bool image_opts
= false;
2525 bool force_share
= false;
2530 int option_index
= 0;
2531 static const struct option long_options
[] = {
2532 {"help", no_argument
, 0, 'h'},
2533 {"format", required_argument
, 0, 'f'},
2534 {"output", required_argument
, 0, OPTION_OUTPUT
},
2535 {"backing-chain", no_argument
, 0, OPTION_BACKING_CHAIN
},
2536 {"object", required_argument
, 0, OPTION_OBJECT
},
2537 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
2538 {"force-share", no_argument
, 0, 'U'},
2541 c
= getopt_long(argc
, argv
, ":f:hU",
2542 long_options
, &option_index
);
2548 missing_argument(argv
[optind
- 1]);
2551 unrecognized_option(argv
[optind
- 1]);
2565 case OPTION_BACKING_CHAIN
:
2568 case OPTION_OBJECT
: {
2570 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
2576 case OPTION_IMAGE_OPTS
:
2581 if (optind
!= argc
- 1) {
2582 error_exit("Expecting one image file name");
2584 filename
= argv
[optind
++];
2586 if (output
&& !strcmp(output
, "json")) {
2587 output_format
= OFORMAT_JSON
;
2588 } else if (output
&& !strcmp(output
, "human")) {
2589 output_format
= OFORMAT_HUMAN
;
2590 } else if (output
) {
2591 error_report("--output must be used with human or json as argument.");
2595 if (qemu_opts_foreach(&qemu_object_opts
,
2596 user_creatable_add_opts_foreach
,
2601 list
= collect_image_info_list(image_opts
, filename
, fmt
, chain
,
2607 switch (output_format
) {
2609 dump_human_image_info_list(list
);
2613 dump_json_image_info_list(list
);
2615 dump_json_image_info(list
->value
);
2620 qapi_free_ImageInfoList(list
);
2624 static void dump_map_entry(OutputFormat output_format
, MapEntry
*e
,
2627 switch (output_format
) {
2629 if (e
->data
&& !e
->has_offset
) {
2630 error_report("File contains external, encrypted or compressed clusters.");
2633 if (e
->data
&& !e
->zero
) {
2634 printf("%#-16"PRIx64
"%#-16"PRIx64
"%#-16"PRIx64
"%s\n",
2635 e
->start
, e
->length
,
2636 e
->has_offset
? e
->offset
: 0,
2637 e
->has_filename
? e
->filename
: "");
2639 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2640 * Modify the flags here to allow more coalescing.
2642 if (next
&& (!next
->data
|| next
->zero
)) {
2648 printf("%s{ \"start\": %"PRId64
", \"length\": %"PRId64
","
2649 " \"depth\": %"PRId64
", \"zero\": %s, \"data\": %s",
2650 (e
->start
== 0 ? "[" : ",\n"),
2651 e
->start
, e
->length
, e
->depth
,
2652 e
->zero
? "true" : "false",
2653 e
->data
? "true" : "false");
2654 if (e
->has_offset
) {
2655 printf(", \"offset\": %"PRId64
"", e
->offset
);
2666 static int get_block_status(BlockDriverState
*bs
, int64_t sector_num
,
2667 int nb_sectors
, MapEntry
*e
)
2671 BlockDriverState
*file
;
2674 /* As an optimization, we could cache the current range of unallocated
2675 * clusters in each file of the chain, and avoid querying the same
2681 ret
= bdrv_get_block_status(bs
, sector_num
, nb_sectors
, &nb_sectors
,
2687 if (ret
& (BDRV_BLOCK_ZERO
|BDRV_BLOCK_DATA
)) {
2690 bs
= backing_bs(bs
);
2699 has_offset
= !!(ret
& BDRV_BLOCK_OFFSET_VALID
);
2702 .start
= sector_num
* BDRV_SECTOR_SIZE
,
2703 .length
= nb_sectors
* BDRV_SECTOR_SIZE
,
2704 .data
= !!(ret
& BDRV_BLOCK_DATA
),
2705 .zero
= !!(ret
& BDRV_BLOCK_ZERO
),
2706 .offset
= ret
& BDRV_BLOCK_OFFSET_MASK
,
2707 .has_offset
= has_offset
,
2709 .has_filename
= file
&& has_offset
,
2710 .filename
= file
&& has_offset
? file
->filename
: NULL
,
2716 static inline bool entry_mergeable(const MapEntry
*curr
, const MapEntry
*next
)
2718 if (curr
->length
== 0) {
2721 if (curr
->zero
!= next
->zero
||
2722 curr
->data
!= next
->data
||
2723 curr
->depth
!= next
->depth
||
2724 curr
->has_filename
!= next
->has_filename
||
2725 curr
->has_offset
!= next
->has_offset
) {
2728 if (curr
->has_filename
&& strcmp(curr
->filename
, next
->filename
)) {
2731 if (curr
->has_offset
&& curr
->offset
+ curr
->length
!= next
->offset
) {
2737 static int img_map(int argc
, char **argv
)
2740 OutputFormat output_format
= OFORMAT_HUMAN
;
2742 BlockDriverState
*bs
;
2743 const char *filename
, *fmt
, *output
;
2745 MapEntry curr
= { .length
= 0 }, next
;
2747 bool image_opts
= false;
2748 bool force_share
= false;
2753 int option_index
= 0;
2754 static const struct option long_options
[] = {
2755 {"help", no_argument
, 0, 'h'},
2756 {"format", required_argument
, 0, 'f'},
2757 {"output", required_argument
, 0, OPTION_OUTPUT
},
2758 {"object", required_argument
, 0, OPTION_OBJECT
},
2759 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
2760 {"force-share", no_argument
, 0, 'U'},
2763 c
= getopt_long(argc
, argv
, ":f:hU",
2764 long_options
, &option_index
);
2770 missing_argument(argv
[optind
- 1]);
2773 unrecognized_option(argv
[optind
- 1]);
2787 case OPTION_OBJECT
: {
2789 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
2795 case OPTION_IMAGE_OPTS
:
2800 if (optind
!= argc
- 1) {
2801 error_exit("Expecting one image file name");
2803 filename
= argv
[optind
];
2805 if (output
&& !strcmp(output
, "json")) {
2806 output_format
= OFORMAT_JSON
;
2807 } else if (output
&& !strcmp(output
, "human")) {
2808 output_format
= OFORMAT_HUMAN
;
2809 } else if (output
) {
2810 error_report("--output must be used with human or json as argument.");
2814 if (qemu_opts_foreach(&qemu_object_opts
,
2815 user_creatable_add_opts_foreach
,
2820 blk
= img_open(image_opts
, filename
, fmt
, 0, false, false, force_share
);
2826 if (output_format
== OFORMAT_HUMAN
) {
2827 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2830 length
= blk_getlength(blk
);
2831 while (curr
.start
+ curr
.length
< length
) {
2832 int64_t nsectors_left
;
2836 sector_num
= (curr
.start
+ curr
.length
) >> BDRV_SECTOR_BITS
;
2838 /* Probe up to 1 GiB at a time. */
2839 nsectors_left
= DIV_ROUND_UP(length
, BDRV_SECTOR_SIZE
) - sector_num
;
2840 n
= MIN(1 << (30 - BDRV_SECTOR_BITS
), nsectors_left
);
2841 ret
= get_block_status(bs
, sector_num
, n
, &next
);
2844 error_report("Could not read file metadata: %s", strerror(-ret
));
2848 if (entry_mergeable(&curr
, &next
)) {
2849 curr
.length
+= next
.length
;
2853 if (curr
.length
> 0) {
2854 dump_map_entry(output_format
, &curr
, &next
);
2859 dump_map_entry(output_format
, &curr
, NULL
);
2866 #define SNAPSHOT_LIST 1
2867 #define SNAPSHOT_CREATE 2
2868 #define SNAPSHOT_APPLY 3
2869 #define SNAPSHOT_DELETE 4
2871 static int img_snapshot(int argc
, char **argv
)
2874 BlockDriverState
*bs
;
2875 QEMUSnapshotInfo sn
;
2876 char *filename
, *snapshot_name
= NULL
;
2877 int c
, ret
= 0, bdrv_oflags
;
2882 bool image_opts
= false;
2883 bool force_share
= false;
2885 bdrv_oflags
= BDRV_O_RDWR
;
2886 /* Parse commandline parameters */
2888 static const struct option long_options
[] = {
2889 {"help", no_argument
, 0, 'h'},
2890 {"object", required_argument
, 0, OPTION_OBJECT
},
2891 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
2892 {"force-share", no_argument
, 0, 'U'},
2895 c
= getopt_long(argc
, argv
, ":la:c:d:hqU",
2896 long_options
, NULL
);
2902 missing_argument(argv
[optind
- 1]);
2905 unrecognized_option(argv
[optind
- 1]);
2912 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
2915 action
= SNAPSHOT_LIST
;
2916 bdrv_oflags
&= ~BDRV_O_RDWR
; /* no need for RW */
2920 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
2923 action
= SNAPSHOT_APPLY
;
2924 snapshot_name
= optarg
;
2928 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
2931 action
= SNAPSHOT_CREATE
;
2932 snapshot_name
= optarg
;
2936 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
2939 action
= SNAPSHOT_DELETE
;
2940 snapshot_name
= optarg
;
2948 case OPTION_OBJECT
: {
2950 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
2956 case OPTION_IMAGE_OPTS
:
2962 if (optind
!= argc
- 1) {
2963 error_exit("Expecting one image file name");
2965 filename
= argv
[optind
++];
2967 if (qemu_opts_foreach(&qemu_object_opts
,
2968 user_creatable_add_opts_foreach
,
2973 /* Open the image */
2974 blk
= img_open(image_opts
, filename
, NULL
, bdrv_oflags
, false, quiet
,
2981 /* Perform the requested action */
2987 case SNAPSHOT_CREATE
:
2988 memset(&sn
, 0, sizeof(sn
));
2989 pstrcpy(sn
.name
, sizeof(sn
.name
), snapshot_name
);
2991 qemu_gettimeofday(&tv
);
2992 sn
.date_sec
= tv
.tv_sec
;
2993 sn
.date_nsec
= tv
.tv_usec
* 1000;
2995 ret
= bdrv_snapshot_create(bs
, &sn
);
2997 error_report("Could not create snapshot '%s': %d (%s)",
2998 snapshot_name
, ret
, strerror(-ret
));
3002 case SNAPSHOT_APPLY
:
3003 ret
= bdrv_snapshot_goto(bs
, snapshot_name
);
3005 error_report("Could not apply snapshot '%s': %d (%s)",
3006 snapshot_name
, ret
, strerror(-ret
));
3010 case SNAPSHOT_DELETE
:
3011 bdrv_snapshot_delete_by_id_or_name(bs
, snapshot_name
, &err
);
3013 error_reportf_err(err
, "Could not delete snapshot '%s': ",
3028 static int img_rebase(int argc
, char **argv
)
3030 BlockBackend
*blk
= NULL
, *blk_old_backing
= NULL
, *blk_new_backing
= NULL
;
3031 uint8_t *buf_old
= NULL
;
3032 uint8_t *buf_new
= NULL
;
3033 BlockDriverState
*bs
= NULL
;
3035 const char *fmt
, *cache
, *src_cache
, *out_basefmt
, *out_baseimg
;
3036 int c
, flags
, src_flags
, ret
;
3037 bool writethrough
, src_writethrough
;
3039 bool force_share
= false;
3042 Error
*local_err
= NULL
;
3043 bool image_opts
= false;
3045 /* Parse commandline parameters */
3047 cache
= BDRV_DEFAULT_CACHE
;
3048 src_cache
= BDRV_DEFAULT_CACHE
;
3052 static const struct option long_options
[] = {
3053 {"help", no_argument
, 0, 'h'},
3054 {"object", required_argument
, 0, OPTION_OBJECT
},
3055 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
3056 {"force-share", no_argument
, 0, 'U'},
3059 c
= getopt_long(argc
, argv
, ":hf:F:b:upt:T:qU",
3060 long_options
, NULL
);
3066 missing_argument(argv
[optind
- 1]);
3069 unrecognized_option(argv
[optind
- 1]);
3078 out_basefmt
= optarg
;
3081 out_baseimg
= optarg
;
3098 case OPTION_OBJECT
: {
3100 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
3106 case OPTION_IMAGE_OPTS
:
3119 if (optind
!= argc
- 1) {
3120 error_exit("Expecting one image file name");
3122 if (!unsafe
&& !out_baseimg
) {
3123 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
3125 filename
= argv
[optind
++];
3127 if (qemu_opts_foreach(&qemu_object_opts
,
3128 user_creatable_add_opts_foreach
,
3133 qemu_progress_init(progress
, 2.0);
3134 qemu_progress_print(0, 100);
3136 flags
= BDRV_O_RDWR
| (unsafe
? BDRV_O_NO_BACKING
: 0);
3137 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
3139 error_report("Invalid cache option: %s", cache
);
3144 ret
= bdrv_parse_cache_mode(src_cache
, &src_flags
, &src_writethrough
);
3146 error_report("Invalid source cache option: %s", src_cache
);
3150 /* The source files are opened read-only, don't care about WCE */
3151 assert((src_flags
& BDRV_O_RDWR
) == 0);
3152 (void) src_writethrough
;
3157 * Ignore the old backing file for unsafe rebase in case we want to correct
3158 * the reference to a renamed or moved backing file.
3160 blk
= img_open(image_opts
, filename
, fmt
, flags
, writethrough
, quiet
,
3168 if (out_basefmt
!= NULL
) {
3169 if (bdrv_find_format(out_basefmt
) == NULL
) {
3170 error_report("Invalid format name: '%s'", out_basefmt
);
3176 /* For safe rebasing we need to compare old and new backing file */
3178 char backing_name
[PATH_MAX
];
3179 QDict
*options
= NULL
;
3181 if (bs
->backing_format
[0] != '\0') {
3182 options
= qdict_new();
3183 qdict_put_str(options
, "driver", bs
->backing_format
);
3188 options
= qdict_new();
3190 qdict_put_bool(options
, BDRV_OPT_FORCE_SHARE
, true);
3192 bdrv_get_backing_filename(bs
, backing_name
, sizeof(backing_name
));
3193 blk_old_backing
= blk_new_open(backing_name
, NULL
,
3194 options
, src_flags
, &local_err
);
3195 if (!blk_old_backing
) {
3196 error_reportf_err(local_err
,
3197 "Could not open old backing file '%s': ",
3203 if (out_baseimg
[0]) {
3204 options
= qdict_new();
3206 qdict_put_str(options
, "driver", out_basefmt
);
3209 qdict_put_bool(options
, BDRV_OPT_FORCE_SHARE
, true);
3212 blk_new_backing
= blk_new_open(out_baseimg
, NULL
,
3213 options
, src_flags
, &local_err
);
3214 if (!blk_new_backing
) {
3215 error_reportf_err(local_err
,
3216 "Could not open new backing file '%s': ",
3225 * Check each unallocated cluster in the COW file. If it is unallocated,
3226 * accesses go to the backing file. We must therefore compare this cluster
3227 * in the old and new backing file, and if they differ we need to copy it
3228 * from the old backing file into the COW file.
3230 * If qemu-img crashes during this step, no harm is done. The content of
3231 * the image is the same as the original one at any time.
3234 int64_t num_sectors
;
3235 int64_t old_backing_num_sectors
;
3236 int64_t new_backing_num_sectors
= 0;
3240 float local_progress
= 0;
3242 buf_old
= blk_blockalign(blk
, IO_BUF_SIZE
);
3243 buf_new
= blk_blockalign(blk
, IO_BUF_SIZE
);
3245 num_sectors
= blk_nb_sectors(blk
);
3246 if (num_sectors
< 0) {
3247 error_report("Could not get size of '%s': %s",
3248 filename
, strerror(-num_sectors
));
3252 old_backing_num_sectors
= blk_nb_sectors(blk_old_backing
);
3253 if (old_backing_num_sectors
< 0) {
3254 char backing_name
[PATH_MAX
];
3256 bdrv_get_backing_filename(bs
, backing_name
, sizeof(backing_name
));
3257 error_report("Could not get size of '%s': %s",
3258 backing_name
, strerror(-old_backing_num_sectors
));
3262 if (blk_new_backing
) {
3263 new_backing_num_sectors
= blk_nb_sectors(blk_new_backing
);
3264 if (new_backing_num_sectors
< 0) {
3265 error_report("Could not get size of '%s': %s",
3266 out_baseimg
, strerror(-new_backing_num_sectors
));
3272 if (num_sectors
!= 0) {
3273 local_progress
= (float)100 /
3274 (num_sectors
/ MIN(num_sectors
, IO_BUF_SIZE
/ 512));
3277 for (sector
= 0; sector
< num_sectors
; sector
+= n
) {
3279 /* How many sectors can we handle with the next read? */
3280 if (sector
+ (IO_BUF_SIZE
/ 512) <= num_sectors
) {
3281 n
= (IO_BUF_SIZE
/ 512);
3283 n
= num_sectors
- sector
;
3286 /* If the cluster is allocated, we don't need to take action */
3287 ret
= bdrv_is_allocated(bs
, sector
<< BDRV_SECTOR_BITS
,
3288 n
<< BDRV_SECTOR_BITS
, &count
);
3290 error_report("error while reading image metadata: %s",
3294 /* TODO relax this once bdrv_is_allocated does not enforce
3295 * sector alignment */
3296 assert(QEMU_IS_ALIGNED(count
, BDRV_SECTOR_SIZE
));
3297 n
= count
>> BDRV_SECTOR_BITS
;
3303 * Read old and new backing file and take into consideration that
3304 * backing files may be smaller than the COW image.
3306 if (sector
>= old_backing_num_sectors
) {
3307 memset(buf_old
, 0, n
* BDRV_SECTOR_SIZE
);
3309 if (sector
+ n
> old_backing_num_sectors
) {
3310 n
= old_backing_num_sectors
- sector
;
3313 ret
= blk_pread(blk_old_backing
, sector
<< BDRV_SECTOR_BITS
,
3314 buf_old
, n
<< BDRV_SECTOR_BITS
);
3316 error_report("error while reading from old backing file");
3321 if (sector
>= new_backing_num_sectors
|| !blk_new_backing
) {
3322 memset(buf_new
, 0, n
* BDRV_SECTOR_SIZE
);
3324 if (sector
+ n
> new_backing_num_sectors
) {
3325 n
= new_backing_num_sectors
- sector
;
3328 ret
= blk_pread(blk_new_backing
, sector
<< BDRV_SECTOR_BITS
,
3329 buf_new
, n
<< BDRV_SECTOR_BITS
);
3331 error_report("error while reading from new backing file");
3336 /* If they differ, we need to write to the COW file */
3337 uint64_t written
= 0;
3339 while (written
< n
) {
3342 if (compare_sectors(buf_old
+ written
* 512,
3343 buf_new
+ written
* 512, n
- written
, &pnum
))
3345 ret
= blk_pwrite(blk
,
3346 (sector
+ written
) << BDRV_SECTOR_BITS
,
3347 buf_old
+ written
* 512,
3348 pnum
<< BDRV_SECTOR_BITS
, 0);
3350 error_report("Error while writing to COW image: %s",
3358 qemu_progress_print(local_progress
, 100);
3363 * Change the backing file. All clusters that are different from the old
3364 * backing file are overwritten in the COW file now, so the visible content
3365 * doesn't change when we switch the backing file.
3367 if (out_baseimg
&& *out_baseimg
) {
3368 ret
= bdrv_change_backing_file(bs
, out_baseimg
, out_basefmt
);
3370 ret
= bdrv_change_backing_file(bs
, NULL
, NULL
);
3373 if (ret
== -ENOSPC
) {
3374 error_report("Could not change the backing file to '%s': No "
3375 "space left in the file header", out_baseimg
);
3376 } else if (ret
< 0) {
3377 error_report("Could not change the backing file to '%s': %s",
3378 out_baseimg
, strerror(-ret
));
3381 qemu_progress_print(100, 0);
3383 * TODO At this point it is possible to check if any clusters that are
3384 * allocated in the COW file are the same in the backing file. If so, they
3385 * could be dropped from the COW file. Don't do this before switching the
3386 * backing file, in case of a crash this would lead to corruption.
3389 qemu_progress_end();
3392 blk_unref(blk_old_backing
);
3393 blk_unref(blk_new_backing
);
3395 qemu_vfree(buf_old
);
3396 qemu_vfree(buf_new
);
3405 static int img_resize(int argc
, char **argv
)
3408 int c
, ret
, relative
;
3409 const char *filename
, *fmt
, *size
;
3410 int64_t n
, total_size
;
3412 BlockBackend
*blk
= NULL
;
3415 static QemuOptsList resize_options
= {
3416 .name
= "resize_options",
3417 .head
= QTAILQ_HEAD_INITIALIZER(resize_options
.head
),
3420 .name
= BLOCK_OPT_SIZE
,
3421 .type
= QEMU_OPT_SIZE
,
3422 .help
= "Virtual disk size"
3428 bool image_opts
= false;
3430 /* Remove size from argv manually so that negative numbers are not treated
3431 * as options by getopt. */
3433 error_exit("Not enough arguments");
3437 size
= argv
[--argc
];
3439 /* Parse getopt arguments */
3442 static const struct option long_options
[] = {
3443 {"help", no_argument
, 0, 'h'},
3444 {"object", required_argument
, 0, OPTION_OBJECT
},
3445 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
3448 c
= getopt_long(argc
, argv
, ":f:hq",
3449 long_options
, NULL
);
3455 missing_argument(argv
[optind
- 1]);
3458 unrecognized_option(argv
[optind
- 1]);
3469 case OPTION_OBJECT
: {
3471 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
3477 case OPTION_IMAGE_OPTS
:
3482 if (optind
!= argc
- 1) {
3483 error_exit("Expecting one image file name");
3485 filename
= argv
[optind
++];
3487 if (qemu_opts_foreach(&qemu_object_opts
,
3488 user_creatable_add_opts_foreach
,
3493 /* Choose grow, shrink, or absolute resize mode */
3509 param
= qemu_opts_create(&resize_options
, NULL
, 0, &error_abort
);
3510 qemu_opt_set(param
, BLOCK_OPT_SIZE
, size
, &err
);
3512 error_report_err(err
);
3514 qemu_opts_del(param
);
3517 n
= qemu_opt_get_size(param
, BLOCK_OPT_SIZE
, 0);
3518 qemu_opts_del(param
);
3520 blk
= img_open(image_opts
, filename
, fmt
,
3521 BDRV_O_RDWR
| BDRV_O_RESIZE
, false, quiet
,
3529 total_size
= blk_getlength(blk
) + n
* relative
;
3533 if (total_size
<= 0) {
3534 error_report("New image size must be positive");
3539 ret
= blk_truncate(blk
, total_size
, &err
);
3541 qprintf(quiet
, "Image resized.\n");
3543 error_report_err(err
);
3553 static void amend_status_cb(BlockDriverState
*bs
,
3554 int64_t offset
, int64_t total_work_size
,
3557 qemu_progress_print(100.f
* offset
/ total_work_size
, 0);
3560 static int img_amend(int argc
, char **argv
)
3564 char *options
= NULL
;
3565 QemuOptsList
*create_opts
= NULL
;
3566 QemuOpts
*opts
= NULL
;
3567 const char *fmt
= NULL
, *filename
, *cache
;
3570 bool quiet
= false, progress
= false;
3571 BlockBackend
*blk
= NULL
;
3572 BlockDriverState
*bs
= NULL
;
3573 bool image_opts
= false;
3575 cache
= BDRV_DEFAULT_CACHE
;
3577 static const struct option long_options
[] = {
3578 {"help", no_argument
, 0, 'h'},
3579 {"object", required_argument
, 0, OPTION_OBJECT
},
3580 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
3583 c
= getopt_long(argc
, argv
, ":ho:f:t:pq",
3584 long_options
, NULL
);
3591 missing_argument(argv
[optind
- 1]);
3594 unrecognized_option(argv
[optind
- 1]);
3600 if (!is_valid_option_list(optarg
)) {
3601 error_report("Invalid option list: %s", optarg
);
3603 goto out_no_progress
;
3606 options
= g_strdup(optarg
);
3608 char *old_options
= options
;
3609 options
= g_strdup_printf("%s,%s", options
, optarg
);
3610 g_free(old_options
);
3626 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
3630 goto out_no_progress
;
3633 case OPTION_IMAGE_OPTS
:
3640 error_exit("Must specify options (-o)");
3643 if (qemu_opts_foreach(&qemu_object_opts
,
3644 user_creatable_add_opts_foreach
,
3647 goto out_no_progress
;
3653 qemu_progress_init(progress
, 1.0);
3655 filename
= (optind
== argc
- 1) ? argv
[argc
- 1] : NULL
;
3656 if (fmt
&& has_help_option(options
)) {
3657 /* If a format is explicitly specified (and possibly no filename is
3658 * given), print option help here */
3659 ret
= print_block_option_help(filename
, fmt
);
3663 if (optind
!= argc
- 1) {
3664 error_report("Expecting one image file name");
3669 flags
= BDRV_O_RDWR
;
3670 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
3672 error_report("Invalid cache option: %s", cache
);
3676 blk
= img_open(image_opts
, filename
, fmt
, flags
, writethrough
, quiet
,
3684 fmt
= bs
->drv
->format_name
;
3686 if (has_help_option(options
)) {
3687 /* If the format was auto-detected, print option help here */
3688 ret
= print_block_option_help(filename
, fmt
);
3692 if (!bs
->drv
->create_opts
) {
3693 error_report("Format driver '%s' does not support any options to amend",
3699 create_opts
= qemu_opts_append(create_opts
, bs
->drv
->create_opts
);
3700 opts
= qemu_opts_create(create_opts
, NULL
, 0, &error_abort
);
3701 qemu_opts_do_parse(opts
, options
, NULL
, &err
);
3703 error_report_err(err
);
3708 /* In case the driver does not call amend_status_cb() */
3709 qemu_progress_print(0.f
, 0);
3710 ret
= bdrv_amend_options(bs
, opts
, &amend_status_cb
, NULL
);
3711 qemu_progress_print(100.f
, 0);
3713 error_report("Error while amending options: %s", strerror(-ret
));
3718 qemu_progress_end();
3722 qemu_opts_del(opts
);
3723 qemu_opts_free(create_opts
);
3732 typedef struct BenchData
{
3734 uint64_t image_size
;
3741 bool drain_on_flush
;
3750 static void bench_undrained_flush_cb(void *opaque
, int ret
)
3753 error_report("Failed flush request: %s", strerror(-ret
));
3758 static void bench_cb(void *opaque
, int ret
)
3760 BenchData
*b
= opaque
;
3764 error_report("Failed request: %s", strerror(-ret
));
3769 /* Just finished a flush with drained queue: Start next requests */
3770 assert(b
->in_flight
== 0);
3771 b
->in_flush
= false;
3772 } else if (b
->in_flight
> 0) {
3773 int remaining
= b
->n
- b
->in_flight
;
3778 /* Time for flush? Drain queue if requested, then flush */
3779 if (b
->flush_interval
&& remaining
% b
->flush_interval
== 0) {
3780 if (!b
->in_flight
|| !b
->drain_on_flush
) {
3781 BlockCompletionFunc
*cb
;
3783 if (b
->drain_on_flush
) {
3787 cb
= bench_undrained_flush_cb
;
3790 acb
= blk_aio_flush(b
->blk
, cb
, b
);
3792 error_report("Failed to issue flush request");
3796 if (b
->drain_on_flush
) {
3802 while (b
->n
> b
->in_flight
&& b
->in_flight
< b
->nrreq
) {
3803 int64_t offset
= b
->offset
;
3804 /* blk_aio_* might look for completed I/Os and kick bench_cb
3805 * again, so make sure this operation is counted by in_flight
3806 * and b->offset is ready for the next submission.
3809 b
->offset
+= b
->step
;
3810 b
->offset
%= b
->image_size
;
3812 acb
= blk_aio_pwritev(b
->blk
, offset
, b
->qiov
, 0, bench_cb
, b
);
3814 acb
= blk_aio_preadv(b
->blk
, offset
, b
->qiov
, 0, bench_cb
, b
);
3817 error_report("Failed to issue request");
3823 static int img_bench(int argc
, char **argv
)
3826 const char *fmt
= NULL
, *filename
;
3828 bool image_opts
= false;
3829 bool is_write
= false;
3833 size_t bufsize
= 4096;
3836 int flush_interval
= 0;
3837 bool drain_on_flush
= true;
3839 BlockBackend
*blk
= NULL
;
3840 BenchData data
= {};
3842 bool writethrough
= false;
3843 struct timeval t1
, t2
;
3845 bool force_share
= false;
3848 static const struct option long_options
[] = {
3849 {"help", no_argument
, 0, 'h'},
3850 {"flush-interval", required_argument
, 0, OPTION_FLUSH_INTERVAL
},
3851 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
3852 {"pattern", required_argument
, 0, OPTION_PATTERN
},
3853 {"no-drain", no_argument
, 0, OPTION_NO_DRAIN
},
3854 {"force-share", no_argument
, 0, 'U'},
3857 c
= getopt_long(argc
, argv
, ":hc:d:f:no:qs:S:t:wU", long_options
, NULL
);
3864 missing_argument(argv
[optind
- 1]);
3867 unrecognized_option(argv
[optind
- 1]);
3876 if (qemu_strtoul(optarg
, NULL
, 0, &res
) < 0 || res
> INT_MAX
) {
3877 error_report("Invalid request count specified");
3887 if (qemu_strtoul(optarg
, NULL
, 0, &res
) < 0 || res
> INT_MAX
) {
3888 error_report("Invalid queue depth specified");
3898 flags
|= BDRV_O_NATIVE_AIO
;
3902 offset
= cvtnum(optarg
);
3904 error_report("Invalid offset specified");
3917 sval
= cvtnum(optarg
);
3918 if (sval
< 0 || sval
> INT_MAX
) {
3919 error_report("Invalid buffer size specified");
3930 sval
= cvtnum(optarg
);
3931 if (sval
< 0 || sval
> INT_MAX
) {
3932 error_report("Invalid step size specified");
3940 ret
= bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
);
3942 error_report("Invalid cache mode");
3948 flags
|= BDRV_O_RDWR
;
3954 case OPTION_PATTERN
:
3958 if (qemu_strtoul(optarg
, NULL
, 0, &res
) < 0 || res
> 0xff) {
3959 error_report("Invalid pattern byte specified");
3965 case OPTION_FLUSH_INTERVAL
:
3969 if (qemu_strtoul(optarg
, NULL
, 0, &res
) < 0 || res
> INT_MAX
) {
3970 error_report("Invalid flush interval specified");
3973 flush_interval
= res
;
3976 case OPTION_NO_DRAIN
:
3977 drain_on_flush
= false;
3979 case OPTION_IMAGE_OPTS
:
3985 if (optind
!= argc
- 1) {
3986 error_exit("Expecting one image file name");
3988 filename
= argv
[argc
- 1];
3990 if (!is_write
&& flush_interval
) {
3991 error_report("--flush-interval is only available in write tests");
3995 if (flush_interval
&& flush_interval
< depth
) {
3996 error_report("Flush interval can't be smaller than depth");
4001 blk
= img_open(image_opts
, filename
, fmt
, flags
, writethrough
, quiet
,
4008 image_size
= blk_getlength(blk
);
4009 if (image_size
< 0) {
4014 data
= (BenchData
) {
4016 .image_size
= image_size
,
4018 .step
= step
?: bufsize
,
4023 .flush_interval
= flush_interval
,
4024 .drain_on_flush
= drain_on_flush
,
4026 printf("Sending %d %s requests, %d bytes each, %d in parallel "
4027 "(starting at offset %" PRId64
", step size %d)\n",
4028 data
.n
, data
.write
? "write" : "read", data
.bufsize
, data
.nrreq
,
4029 data
.offset
, data
.step
);
4030 if (flush_interval
) {
4031 printf("Sending flush every %d requests\n", flush_interval
);
4034 data
.buf
= blk_blockalign(blk
, data
.nrreq
* data
.bufsize
);
4035 memset(data
.buf
, pattern
, data
.nrreq
* data
.bufsize
);
4037 data
.qiov
= g_new(QEMUIOVector
, data
.nrreq
);
4038 for (i
= 0; i
< data
.nrreq
; i
++) {
4039 qemu_iovec_init(&data
.qiov
[i
], 1);
4040 qemu_iovec_add(&data
.qiov
[i
],
4041 data
.buf
+ i
* data
.bufsize
, data
.bufsize
);
4044 gettimeofday(&t1
, NULL
);
4047 while (data
.n
> 0) {
4048 main_loop_wait(false);
4050 gettimeofday(&t2
, NULL
);
4052 printf("Run completed in %3.3f seconds.\n",
4053 (t2
.tv_sec
- t1
.tv_sec
)
4054 + ((double)(t2
.tv_usec
- t1
.tv_usec
) / 1000000));
4057 qemu_vfree(data
.buf
);
4078 int bsz
; /* Block size */
4086 int (*f
)(const char *, struct DdIo
*, struct DdIo
*, struct DdInfo
*);
4090 static int img_dd_bs(const char *arg
,
4091 struct DdIo
*in
, struct DdIo
*out
,
4098 if (res
<= 0 || res
> INT_MAX
) {
4099 error_report("invalid number: '%s'", arg
);
4102 in
->bsz
= out
->bsz
= res
;
4107 static int img_dd_count(const char *arg
,
4108 struct DdIo
*in
, struct DdIo
*out
,
4111 dd
->count
= cvtnum(arg
);
4113 if (dd
->count
< 0) {
4114 error_report("invalid number: '%s'", arg
);
4121 static int img_dd_if(const char *arg
,
4122 struct DdIo
*in
, struct DdIo
*out
,
4125 in
->filename
= g_strdup(arg
);
4130 static int img_dd_of(const char *arg
,
4131 struct DdIo
*in
, struct DdIo
*out
,
4134 out
->filename
= g_strdup(arg
);
4139 static int img_dd_skip(const char *arg
,
4140 struct DdIo
*in
, struct DdIo
*out
,
4143 in
->offset
= cvtnum(arg
);
4145 if (in
->offset
< 0) {
4146 error_report("invalid number: '%s'", arg
);
4153 static int img_dd(int argc
, char **argv
)
4158 BlockDriver
*drv
= NULL
, *proto_drv
= NULL
;
4159 BlockBackend
*blk1
= NULL
, *blk2
= NULL
;
4160 QemuOpts
*opts
= NULL
;
4161 QemuOptsList
*create_opts
= NULL
;
4162 Error
*local_err
= NULL
;
4163 bool image_opts
= false;
4165 const char *out_fmt
= "raw";
4166 const char *fmt
= NULL
;
4168 int64_t block_count
= 0, out_pos
, in_pos
;
4169 bool force_share
= false;
4170 struct DdInfo dd
= {
4175 .bsz
= 512, /* Block size is by default 512 bytes */
4187 const struct DdOpts options
[] = {
4188 { "bs", img_dd_bs
, C_BS
},
4189 { "count", img_dd_count
, C_COUNT
},
4190 { "if", img_dd_if
, C_IF
},
4191 { "of", img_dd_of
, C_OF
},
4192 { "skip", img_dd_skip
, C_SKIP
},
4195 const struct option long_options
[] = {
4196 { "help", no_argument
, 0, 'h'},
4197 { "object", required_argument
, 0, OPTION_OBJECT
},
4198 { "image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
4199 { "force-share", no_argument
, 0, 'U'},
4203 while ((c
= getopt_long(argc
, argv
, ":hf:O:U", long_options
, NULL
))) {
4215 missing_argument(argv
[optind
- 1]);
4218 unrecognized_option(argv
[optind
- 1]);
4227 if (!qemu_opts_parse_noisily(&qemu_object_opts
, optarg
, true)) {
4232 case OPTION_IMAGE_OPTS
:
4238 for (i
= optind
; i
< argc
; i
++) {
4240 arg
= g_strdup(argv
[i
]);
4242 tmp
= strchr(arg
, '=');
4244 error_report("unrecognized operand %s", arg
);
4251 for (j
= 0; options
[j
].name
!= NULL
; j
++) {
4252 if (!strcmp(arg
, options
[j
].name
)) {
4256 if (options
[j
].name
== NULL
) {
4257 error_report("unrecognized operand %s", arg
);
4262 if (options
[j
].f(tmp
, &in
, &out
, &dd
) != 0) {
4266 dd
.flags
|= options
[j
].flag
;
4271 if (!(dd
.flags
& C_IF
&& dd
.flags
& C_OF
)) {
4272 error_report("Must specify both input and output files");
4277 if (qemu_opts_foreach(&qemu_object_opts
,
4278 user_creatable_add_opts_foreach
,
4284 blk1
= img_open(image_opts
, in
.filename
, fmt
, 0, false, false,
4292 drv
= bdrv_find_format(out_fmt
);
4294 error_report("Unknown file format");
4298 proto_drv
= bdrv_find_protocol(out
.filename
, true, &local_err
);
4301 error_report_err(local_err
);
4305 if (!drv
->create_opts
) {
4306 error_report("Format driver '%s' does not support image creation",
4311 if (!proto_drv
->create_opts
) {
4312 error_report("Protocol driver '%s' does not support image creation",
4313 proto_drv
->format_name
);
4317 create_opts
= qemu_opts_append(create_opts
, drv
->create_opts
);
4318 create_opts
= qemu_opts_append(create_opts
, proto_drv
->create_opts
);
4320 opts
= qemu_opts_create(create_opts
, NULL
, 0, &error_abort
);
4322 size
= blk_getlength(blk1
);
4324 error_report("Failed to get size for '%s'", in
.filename
);
4329 if (dd
.flags
& C_COUNT
&& dd
.count
<= INT64_MAX
/ in
.bsz
&&
4330 dd
.count
* in
.bsz
< size
) {
4331 size
= dd
.count
* in
.bsz
;
4334 /* Overflow means the specified offset is beyond input image's size */
4335 if (dd
.flags
& C_SKIP
&& (in
.offset
> INT64_MAX
/ in
.bsz
||
4336 size
< in
.bsz
* in
.offset
)) {
4337 qemu_opt_set_number(opts
, BLOCK_OPT_SIZE
, 0, &error_abort
);
4339 qemu_opt_set_number(opts
, BLOCK_OPT_SIZE
,
4340 size
- in
.bsz
* in
.offset
, &error_abort
);
4343 ret
= bdrv_create(drv
, out
.filename
, opts
, &local_err
);
4345 error_reportf_err(local_err
,
4346 "%s: error while creating output image: ",
4352 /* TODO, we can't honour --image-opts for the target,
4353 * since it needs to be given in a format compatible
4354 * with the bdrv_create() call above which does not
4355 * support image-opts style.
4357 blk2
= img_open_file(out
.filename
, NULL
, out_fmt
, BDRV_O_RDWR
,
4358 false, false, false);
4365 if (dd
.flags
& C_SKIP
&& (in
.offset
> INT64_MAX
/ in
.bsz
||
4366 size
< in
.offset
* in
.bsz
)) {
4367 /* We give a warning if the skip option is bigger than the input
4368 * size and create an empty output disk image (i.e. like dd(1)).
4370 error_report("%s: cannot skip to specified offset", in
.filename
);
4373 in_pos
= in
.offset
* in
.bsz
;
4376 in
.buf
= g_new(uint8_t, in
.bsz
);
4378 for (out_pos
= 0; in_pos
< size
; block_count
++) {
4379 int in_ret
, out_ret
;
4381 if (in_pos
+ in
.bsz
> size
) {
4382 in_ret
= blk_pread(blk1
, in_pos
, in
.buf
, size
- in_pos
);
4384 in_ret
= blk_pread(blk1
, in_pos
, in
.buf
, in
.bsz
);
4387 error_report("error while reading from input image file: %s",
4394 out_ret
= blk_pwrite(blk2
, out_pos
, in
.buf
, in_ret
, 0);
4397 error_report("error while writing to output image file: %s",
4398 strerror(-out_ret
));
4407 qemu_opts_del(opts
);
4408 qemu_opts_free(create_opts
);
4411 g_free(in
.filename
);
4412 g_free(out
.filename
);
4423 static const img_cmd_t img_cmds
[] = {
4424 #define DEF(option, callback, arg_string) \
4425 { option, callback },
4426 #include "qemu-img-cmds.h"
4432 int main(int argc
, char **argv
)
4434 const img_cmd_t
*cmd
;
4435 const char *cmdname
;
4436 Error
*local_error
= NULL
;
4437 char *trace_file
= NULL
;
4439 static const struct option long_options
[] = {
4440 {"help", no_argument
, 0, 'h'},
4441 {"version", no_argument
, 0, 'V'},
4442 {"trace", required_argument
, NULL
, 'T'},
4447 signal(SIGPIPE
, SIG_IGN
);
4450 module_call_init(MODULE_INIT_TRACE
);
4451 error_set_progname(argv
[0]);
4452 qemu_init_exec_dir(argv
[0]);
4454 if (qemu_init_main_loop(&local_error
)) {
4455 error_report_err(local_error
);
4459 qcrypto_init(&error_fatal
);
4461 module_call_init(MODULE_INIT_QOM
);
4464 error_exit("Not enough arguments");
4467 qemu_add_opts(&qemu_object_opts
);
4468 qemu_add_opts(&qemu_source_opts
);
4469 qemu_add_opts(&qemu_trace_opts
);
4471 while ((c
= getopt_long(argc
, argv
, "+:hVT:", long_options
, NULL
)) != -1) {
4474 missing_argument(argv
[optind
- 1]);
4477 unrecognized_option(argv
[optind
- 1]);
4483 printf(QEMU_IMG_VERSION
);
4487 trace_file
= trace_opt_parse(optarg
);
4492 cmdname
= argv
[optind
];
4494 /* reset getopt_long scanning */
4502 if (!trace_init_backends()) {
4505 trace_init_file(trace_file
);
4506 qemu_set_log(LOG_TRACE
);
4508 /* find the command */
4509 for (cmd
= img_cmds
; cmd
->name
!= NULL
; cmd
++) {
4510 if (!strcmp(cmdname
, cmd
->name
)) {
4511 return cmd
->handler(argc
, argv
);
4516 error_exit("Command not found: %s", cmdname
);