2 * QEMU disk image utility
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
28 #include "qemu-version.h"
29 #include "qapi/error.h"
30 #include "qapi/qapi-visit-block-core.h"
31 #include "qapi/qobject-output-visitor.h"
32 #include "qapi/qmp/qjson.h"
33 #include "qapi/qmp/qdict.h"
34 #include "qapi/qmp/qstring.h"
35 #include "qemu/cutils.h"
36 #include "qemu/config-file.h"
37 #include "qemu/option.h"
38 #include "qemu/error-report.h"
40 #include "qom/object_interfaces.h"
41 #include "sysemu/sysemu.h"
42 #include "sysemu/block-backend.h"
43 #include "block/block_int.h"
44 #include "block/blockjob.h"
45 #include "block/qapi.h"
46 #include "crypto/init.h"
47 #include "trace/control.h"
49 #define QEMU_IMG_VERSION "qemu-img version " QEMU_FULL_VERSION \
50 "\n" QEMU_COPYRIGHT "\n"
52 typedef struct img_cmd_t
{
54 int (*handler
)(int argc
, char **argv
);
59 OPTION_BACKING_CHAIN
= 257,
61 OPTION_IMAGE_OPTS
= 259,
63 OPTION_FLUSH_INTERVAL
= 261,
64 OPTION_NO_DRAIN
= 262,
65 OPTION_TARGET_IMAGE_OPTS
= 263,
67 OPTION_PREALLOCATION
= 265,
71 typedef enum OutputFormat
{
76 /* Default to cache=writeback as data integrity is not important for qemu-img */
77 #define BDRV_DEFAULT_CACHE "writeback"
79 static void format_print(void *opaque
, const char *name
)
84 static void QEMU_NORETURN
GCC_FMT_ATTR(1, 2) error_exit(const char *fmt
, ...)
88 error_printf("qemu-img: ");
91 error_vprintf(fmt
, ap
);
94 error_printf("\nTry 'qemu-img --help' for more information\n");
98 static void QEMU_NORETURN
missing_argument(const char *option
)
100 error_exit("missing argument for option '%s'", option
);
103 static void QEMU_NORETURN
unrecognized_option(const char *option
)
105 error_exit("unrecognized option '%s'", option
);
108 /* Please keep in synch with qemu-img.texi */
109 static void QEMU_NORETURN
help(void)
111 const char *help_msg
=
113 "usage: qemu-img [standard options] command [command options]\n"
114 "QEMU disk image utility\n"
116 " '-h', '--help' display this help and exit\n"
117 " '-V', '--version' output version information and exit\n"
118 " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
119 " specify tracing options\n"
122 #define DEF(option, callback, arg_string) \
124 #include "qemu-img-cmds.h"
127 "Command parameters:\n"
128 " 'filename' is a disk image filename\n"
129 " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
130 " manual page for a description of the object properties. The most common\n"
131 " object type is a 'secret', which is used to supply passwords and/or\n"
132 " encryption keys.\n"
133 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
134 " 'cache' is the cache mode used to write the output disk image, the valid\n"
135 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
136 " 'directsync' and 'unsafe' (default for convert)\n"
137 " 'src_cache' is the cache mode used to read input disk images, the valid\n"
138 " options are the same as for the 'cache' option\n"
139 " 'size' is the disk image size in bytes. Optional suffixes\n"
140 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
141 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
142 " supported. 'b' is ignored.\n"
143 " 'output_filename' is the destination disk image filename\n"
144 " 'output_fmt' is the destination format\n"
145 " 'options' is a comma separated list of format specific options in a\n"
146 " name=value format. Use -o ? for an overview of the options supported by the\n"
148 " 'snapshot_param' is param used for internal snapshot, format\n"
149 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
151 " 'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n"
153 " '-c' indicates that target image must be compressed (qcow format only)\n"
154 " '-u' allows unsafe backing chains. For rebasing, it is assumed that old and\n"
155 " new backing file match exactly. The image doesn't need a working\n"
156 " backing file before rebasing in this case (useful for renaming the\n"
157 " backing file). For image creation, allow creating without attempting\n"
158 " to open the backing file.\n"
159 " '-h' with or without a command shows this help and lists the supported formats\n"
160 " '-p' show progress of command (only certain commands)\n"
161 " '-q' use Quiet mode - do not print any output (except errors)\n"
162 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
163 " contain only zeros for qemu-img to create a sparse image during\n"
164 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
165 " unallocated or zero sectors, and the destination image will always be\n"
167 " '--output' takes the format in which the output must be done (human or json)\n"
168 " '-n' skips the target volume creation (useful if the volume is created\n"
169 " prior to running qemu-img)\n"
171 "Parameters to check subcommand:\n"
172 " '-r' tries to repair any inconsistencies that are found during the check.\n"
173 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
174 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
175 " hiding corruption that has already occurred.\n"
177 "Parameters to convert subcommand:\n"
178 " '-m' specifies how many coroutines work in parallel during the convert\n"
179 " process (defaults to 8)\n"
180 " '-W' allow to write to the target out of order rather than sequential\n"
182 "Parameters to snapshot subcommand:\n"
183 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
184 " '-a' applies a snapshot (revert disk to saved state)\n"
185 " '-c' creates a snapshot\n"
186 " '-d' deletes a snapshot\n"
187 " '-l' lists all snapshots in the given image\n"
189 "Parameters to compare subcommand:\n"
190 " '-f' first image format\n"
191 " '-F' second image format\n"
192 " '-s' run in Strict mode - fail on different image size or sector allocation\n"
194 "Parameters to dd subcommand:\n"
195 " 'bs=BYTES' read and write up to BYTES bytes at a time "
197 " 'count=N' copy only N input blocks\n"
198 " 'if=FILE' read from FILE\n"
199 " 'of=FILE' write to FILE\n"
200 " 'skip=N' skip N bs-sized blocks at the start of input\n";
202 printf("%s\nSupported formats:", help_msg
);
203 bdrv_iterate_format(format_print
, NULL
);
204 printf("\n\n" QEMU_HELP_BOTTOM
"\n");
208 static QemuOptsList qemu_object_opts
= {
210 .implied_opt_name
= "qom-type",
211 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
217 static QemuOptsList qemu_source_opts
= {
219 .implied_opt_name
= "file",
220 .head
= QTAILQ_HEAD_INITIALIZER(qemu_source_opts
.head
),
226 static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet
, const char *fmt
, ...)
232 ret
= vprintf(fmt
, args
);
239 static int print_block_option_help(const char *filename
, const char *fmt
)
241 BlockDriver
*drv
, *proto_drv
;
242 QemuOptsList
*create_opts
= NULL
;
243 Error
*local_err
= NULL
;
245 /* Find driver and parse its options */
246 drv
= bdrv_find_format(fmt
);
248 error_report("Unknown file format '%s'", fmt
);
252 if (!drv
->create_opts
) {
253 error_report("Format driver '%s' does not support image creation", fmt
);
257 create_opts
= qemu_opts_append(create_opts
, drv
->create_opts
);
259 proto_drv
= bdrv_find_protocol(filename
, true, &local_err
);
261 error_report_err(local_err
);
262 qemu_opts_free(create_opts
);
265 if (!proto_drv
->create_opts
) {
266 error_report("Protocal driver '%s' does not support image creation",
267 proto_drv
->format_name
);
270 create_opts
= qemu_opts_append(create_opts
, proto_drv
->create_opts
);
273 printf("Supported options:\n");
274 qemu_opts_print_help(create_opts
);
275 qemu_opts_free(create_opts
);
280 static BlockBackend
*img_open_opts(const char *optstr
,
281 QemuOpts
*opts
, int flags
, bool writethrough
,
282 bool quiet
, bool force_share
)
285 Error
*local_err
= NULL
;
287 options
= qemu_opts_to_qdict(opts
, NULL
);
289 if (qdict_haskey(options
, BDRV_OPT_FORCE_SHARE
)
290 && strcmp(qdict_get_str(options
, BDRV_OPT_FORCE_SHARE
), "on")) {
291 error_report("--force-share/-U conflicts with image options");
292 qobject_unref(options
);
295 qdict_put_str(options
, BDRV_OPT_FORCE_SHARE
, "on");
297 blk
= blk_new_open(NULL
, NULL
, options
, flags
, &local_err
);
299 error_reportf_err(local_err
, "Could not open '%s': ", optstr
);
302 blk_set_enable_write_cache(blk
, !writethrough
);
307 static BlockBackend
*img_open_file(const char *filename
,
309 const char *fmt
, int flags
,
310 bool writethrough
, bool quiet
,
314 Error
*local_err
= NULL
;
317 options
= qdict_new();
320 qdict_put_str(options
, "driver", fmt
);
324 qdict_put_bool(options
, BDRV_OPT_FORCE_SHARE
, true);
326 blk
= blk_new_open(filename
, NULL
, options
, flags
, &local_err
);
328 error_reportf_err(local_err
, "Could not open '%s': ", filename
);
331 blk_set_enable_write_cache(blk
, !writethrough
);
337 static int img_add_key_secrets(void *opaque
,
338 const char *name
, const char *value
,
341 QDict
*options
= opaque
;
343 if (g_str_has_suffix(name
, "key-secret")) {
344 qdict_put_str(options
, name
, value
);
350 static BlockBackend
*img_open_new_file(const char *filename
,
351 QemuOpts
*create_opts
,
352 const char *fmt
, int flags
,
353 bool writethrough
, bool quiet
,
356 QDict
*options
= NULL
;
358 options
= qdict_new();
359 qemu_opt_foreach(create_opts
, img_add_key_secrets
, options
, &error_abort
);
361 return img_open_file(filename
, options
, fmt
, flags
, writethrough
, quiet
,
366 static BlockBackend
*img_open(bool image_opts
,
367 const char *filename
,
368 const char *fmt
, int flags
, bool writethrough
,
369 bool quiet
, bool force_share
)
375 error_report("--image-opts and --format are mutually exclusive");
378 opts
= qemu_opts_parse_noisily(qemu_find_opts("source"),
383 blk
= img_open_opts(filename
, opts
, flags
, writethrough
, quiet
,
386 blk
= img_open_file(filename
, NULL
, fmt
, flags
, writethrough
, quiet
,
393 static int add_old_style_options(const char *fmt
, QemuOpts
*opts
,
394 const char *base_filename
,
395 const char *base_fmt
)
400 qemu_opt_set(opts
, BLOCK_OPT_BACKING_FILE
, base_filename
, &err
);
402 error_report("Backing file not supported for file format '%s'",
409 qemu_opt_set(opts
, BLOCK_OPT_BACKING_FMT
, base_fmt
, &err
);
411 error_report("Backing file format not supported for file "
420 static int64_t cvtnum(const char *s
)
425 err
= qemu_strtosz(s
, NULL
, &value
);
429 if (value
> INT64_MAX
) {
435 static int img_create(int argc
, char **argv
)
438 uint64_t img_size
= -1;
439 const char *fmt
= "raw";
440 const char *base_fmt
= NULL
;
441 const char *filename
;
442 const char *base_filename
= NULL
;
443 char *options
= NULL
;
444 Error
*local_err
= NULL
;
449 static const struct option long_options
[] = {
450 {"help", no_argument
, 0, 'h'},
451 {"object", required_argument
, 0, OPTION_OBJECT
},
454 c
= getopt_long(argc
, argv
, ":F:b:f:ho:qu",
461 missing_argument(argv
[optind
- 1]);
464 unrecognized_option(argv
[optind
- 1]);
473 base_filename
= optarg
;
479 if (!is_valid_option_list(optarg
)) {
480 error_report("Invalid option list: %s", optarg
);
484 options
= g_strdup(optarg
);
486 char *old_options
= options
;
487 options
= g_strdup_printf("%s,%s", options
, optarg
);
495 flags
|= BDRV_O_NO_BACKING
;
497 case OPTION_OBJECT
: {
499 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
508 /* Get the filename */
509 filename
= (optind
< argc
) ? argv
[optind
] : NULL
;
510 if (options
&& has_help_option(options
)) {
512 return print_block_option_help(filename
, fmt
);
515 if (optind
>= argc
) {
516 error_exit("Expecting image file name");
520 if (qemu_opts_foreach(&qemu_object_opts
,
521 user_creatable_add_opts_foreach
,
526 /* Get image size, if specified */
530 sval
= cvtnum(argv
[optind
++]);
532 if (sval
== -ERANGE
) {
533 error_report("Image size must be less than 8 EiB!");
535 error_report("Invalid image size specified! You may use k, M, "
536 "G, T, P or E suffixes for ");
537 error_report("kilobytes, megabytes, gigabytes, terabytes, "
538 "petabytes and exabytes.");
542 img_size
= (uint64_t)sval
;
544 if (optind
!= argc
) {
545 error_exit("Unexpected argument: %s", argv
[optind
]);
548 bdrv_img_create(filename
, fmt
, base_filename
, base_fmt
,
549 options
, img_size
, flags
, quiet
, &local_err
);
551 error_reportf_err(local_err
, "%s: ", filename
);
563 static void dump_json_image_check(ImageCheck
*check
, bool quiet
)
567 Visitor
*v
= qobject_output_visitor_new(&obj
);
569 visit_type_ImageCheck(v
, NULL
, &check
, &error_abort
);
570 visit_complete(v
, &obj
);
571 str
= qobject_to_json_pretty(obj
);
573 qprintf(quiet
, "%s\n", qstring_get_str(str
));
579 static void dump_human_image_check(ImageCheck
*check
, bool quiet
)
581 if (!(check
->corruptions
|| check
->leaks
|| check
->check_errors
)) {
582 qprintf(quiet
, "No errors were found on the image.\n");
584 if (check
->corruptions
) {
585 qprintf(quiet
, "\n%" PRId64
" errors were found on the image.\n"
586 "Data may be corrupted, or further writes to the image "
593 "\n%" PRId64
" leaked clusters were found on the image.\n"
594 "This means waste of disk space, but no harm to data.\n",
598 if (check
->check_errors
) {
601 " internal errors have occurred during the check.\n",
602 check
->check_errors
);
606 if (check
->total_clusters
!= 0 && check
->allocated_clusters
!= 0) {
607 qprintf(quiet
, "%" PRId64
"/%" PRId64
" = %0.2f%% allocated, "
608 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
609 check
->allocated_clusters
, check
->total_clusters
,
610 check
->allocated_clusters
* 100.0 / check
->total_clusters
,
611 check
->fragmented_clusters
* 100.0 / check
->allocated_clusters
,
612 check
->compressed_clusters
* 100.0 /
613 check
->allocated_clusters
);
616 if (check
->image_end_offset
) {
618 "Image end offset: %" PRId64
"\n", check
->image_end_offset
);
622 static int collect_image_check(BlockDriverState
*bs
,
624 const char *filename
,
629 BdrvCheckResult result
;
631 ret
= bdrv_check(bs
, &result
, fix
);
636 check
->filename
= g_strdup(filename
);
637 check
->format
= g_strdup(bdrv_get_format_name(bs
));
638 check
->check_errors
= result
.check_errors
;
639 check
->corruptions
= result
.corruptions
;
640 check
->has_corruptions
= result
.corruptions
!= 0;
641 check
->leaks
= result
.leaks
;
642 check
->has_leaks
= result
.leaks
!= 0;
643 check
->corruptions_fixed
= result
.corruptions_fixed
;
644 check
->has_corruptions_fixed
= result
.corruptions
!= 0;
645 check
->leaks_fixed
= result
.leaks_fixed
;
646 check
->has_leaks_fixed
= result
.leaks
!= 0;
647 check
->image_end_offset
= result
.image_end_offset
;
648 check
->has_image_end_offset
= result
.image_end_offset
!= 0;
649 check
->total_clusters
= result
.bfi
.total_clusters
;
650 check
->has_total_clusters
= result
.bfi
.total_clusters
!= 0;
651 check
->allocated_clusters
= result
.bfi
.allocated_clusters
;
652 check
->has_allocated_clusters
= result
.bfi
.allocated_clusters
!= 0;
653 check
->fragmented_clusters
= result
.bfi
.fragmented_clusters
;
654 check
->has_fragmented_clusters
= result
.bfi
.fragmented_clusters
!= 0;
655 check
->compressed_clusters
= result
.bfi
.compressed_clusters
;
656 check
->has_compressed_clusters
= result
.bfi
.compressed_clusters
!= 0;
662 * Checks an image for consistency. Exit codes:
664 * 0 - Check completed, image is good
665 * 1 - Check not completed because of internal errors
666 * 2 - Check completed, image is corrupted
667 * 3 - Check completed, image has leaked clusters, but is good otherwise
668 * 63 - Checks are not supported by the image format
670 static int img_check(int argc
, char **argv
)
673 OutputFormat output_format
= OFORMAT_HUMAN
;
674 const char *filename
, *fmt
, *output
, *cache
;
676 BlockDriverState
*bs
;
678 int flags
= BDRV_O_CHECK
;
682 bool image_opts
= false;
683 bool force_share
= false;
687 cache
= BDRV_DEFAULT_CACHE
;
690 int option_index
= 0;
691 static const struct option long_options
[] = {
692 {"help", no_argument
, 0, 'h'},
693 {"format", required_argument
, 0, 'f'},
694 {"repair", required_argument
, 0, 'r'},
695 {"output", required_argument
, 0, OPTION_OUTPUT
},
696 {"object", required_argument
, 0, OPTION_OBJECT
},
697 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
698 {"force-share", no_argument
, 0, 'U'},
701 c
= getopt_long(argc
, argv
, ":hf:r:T:qU",
702 long_options
, &option_index
);
708 missing_argument(argv
[optind
- 1]);
711 unrecognized_option(argv
[optind
- 1]);
720 flags
|= BDRV_O_RDWR
;
722 if (!strcmp(optarg
, "leaks")) {
723 fix
= BDRV_FIX_LEAKS
;
724 } else if (!strcmp(optarg
, "all")) {
725 fix
= BDRV_FIX_LEAKS
| BDRV_FIX_ERRORS
;
727 error_exit("Unknown option value for -r "
728 "(expecting 'leaks' or 'all'): %s", optarg
);
743 case OPTION_OBJECT
: {
745 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
751 case OPTION_IMAGE_OPTS
:
756 if (optind
!= argc
- 1) {
757 error_exit("Expecting one image file name");
759 filename
= argv
[optind
++];
761 if (output
&& !strcmp(output
, "json")) {
762 output_format
= OFORMAT_JSON
;
763 } else if (output
&& !strcmp(output
, "human")) {
764 output_format
= OFORMAT_HUMAN
;
766 error_report("--output must be used with human or json as argument.");
770 if (qemu_opts_foreach(&qemu_object_opts
,
771 user_creatable_add_opts_foreach
,
776 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
778 error_report("Invalid source cache option: %s", cache
);
782 blk
= img_open(image_opts
, filename
, fmt
, flags
, writethrough
, quiet
,
789 check
= g_new0(ImageCheck
, 1);
790 ret
= collect_image_check(bs
, check
, filename
, fmt
, fix
);
792 if (ret
== -ENOTSUP
) {
793 error_report("This image format does not support checks");
798 if (check
->corruptions_fixed
|| check
->leaks_fixed
) {
799 int corruptions_fixed
, leaks_fixed
;
801 leaks_fixed
= check
->leaks_fixed
;
802 corruptions_fixed
= check
->corruptions_fixed
;
804 if (output_format
== OFORMAT_HUMAN
) {
806 "The following inconsistencies were found and repaired:\n\n"
807 " %" PRId64
" leaked clusters\n"
808 " %" PRId64
" corruptions\n\n"
809 "Double checking the fixed image now...\n",
811 check
->corruptions_fixed
);
814 ret
= collect_image_check(bs
, check
, filename
, fmt
, 0);
816 check
->leaks_fixed
= leaks_fixed
;
817 check
->corruptions_fixed
= corruptions_fixed
;
821 switch (output_format
) {
823 dump_human_image_check(check
, quiet
);
826 dump_json_image_check(check
, quiet
);
831 if (ret
|| check
->check_errors
) {
833 error_report("Check failed: %s", strerror(-ret
));
835 error_report("Check failed");
841 if (check
->corruptions
) {
843 } else if (check
->leaks
) {
850 qapi_free_ImageCheck(check
);
855 typedef struct CommonBlockJobCBInfo
{
856 BlockDriverState
*bs
;
858 } CommonBlockJobCBInfo
;
860 static void common_block_job_cb(void *opaque
, int ret
)
862 CommonBlockJobCBInfo
*cbi
= opaque
;
865 error_setg_errno(cbi
->errp
, -ret
, "Block job failed");
869 static void run_block_job(BlockJob
*job
, Error
**errp
)
871 AioContext
*aio_context
= blk_get_aio_context(job
->blk
);
874 aio_context_acquire(aio_context
);
877 float progress
= 0.0f
;
878 aio_poll(aio_context
, true);
879 if (job
->job
.progress_total
) {
880 progress
= (float)job
->job
.progress_current
/
881 job
->job
.progress_total
* 100.f
;
883 qemu_progress_print(progress
, 0);
884 } while (!job_is_ready(&job
->job
) && !job_is_completed(&job
->job
));
886 if (!job_is_completed(&job
->job
)) {
887 ret
= job_complete_sync(&job
->job
, errp
);
891 job_unref(&job
->job
);
892 aio_context_release(aio_context
);
894 /* publish completion progress only when success */
896 qemu_progress_print(100.f
, 0);
900 static int img_commit(int argc
, char **argv
)
903 const char *filename
, *fmt
, *cache
, *base
;
905 BlockDriverState
*bs
, *base_bs
;
907 bool progress
= false, quiet
= false, drop
= false;
909 Error
*local_err
= NULL
;
910 CommonBlockJobCBInfo cbi
;
911 bool image_opts
= false;
912 AioContext
*aio_context
;
915 cache
= BDRV_DEFAULT_CACHE
;
918 static const struct option long_options
[] = {
919 {"help", no_argument
, 0, 'h'},
920 {"object", required_argument
, 0, OPTION_OBJECT
},
921 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
924 c
= getopt_long(argc
, argv
, ":f:ht:b:dpq",
931 missing_argument(argv
[optind
- 1]);
934 unrecognized_option(argv
[optind
- 1]);
959 case OPTION_OBJECT
: {
961 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
967 case OPTION_IMAGE_OPTS
:
973 /* Progress is not shown in Quiet mode */
978 if (optind
!= argc
- 1) {
979 error_exit("Expecting one image file name");
981 filename
= argv
[optind
++];
983 if (qemu_opts_foreach(&qemu_object_opts
,
984 user_creatable_add_opts_foreach
,
989 flags
= BDRV_O_RDWR
| BDRV_O_UNMAP
;
990 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
992 error_report("Invalid cache option: %s", cache
);
996 blk
= img_open(image_opts
, filename
, fmt
, flags
, writethrough
, quiet
,
1003 qemu_progress_init(progress
, 1.f
);
1004 qemu_progress_print(0.f
, 100);
1007 base_bs
= bdrv_find_backing_image(bs
, base
);
1009 error_setg(&local_err
,
1010 "Did not find '%s' in the backing chain of '%s'",
1015 /* This is different from QMP, which by default uses the deepest file in
1016 * the backing chain (i.e., the very base); however, the traditional
1017 * behavior of qemu-img commit is using the immediate backing file. */
1018 base_bs
= backing_bs(bs
);
1020 error_setg(&local_err
, "Image does not have a backing file");
1025 cbi
= (CommonBlockJobCBInfo
){
1030 aio_context
= bdrv_get_aio_context(bs
);
1031 aio_context_acquire(aio_context
);
1032 commit_active_start("commit", bs
, base_bs
, JOB_DEFAULT
, 0,
1033 BLOCKDEV_ON_ERROR_REPORT
, NULL
, common_block_job_cb
,
1034 &cbi
, false, &local_err
);
1035 aio_context_release(aio_context
);
1040 /* When the block job completes, the BlockBackend reference will point to
1041 * the old backing file. In order to avoid that the top image is already
1042 * deleted, so we can still empty it afterwards, increment the reference
1043 * counter here preemptively. */
1048 job
= block_job_get("commit");
1049 run_block_job(job
, &local_err
);
1054 if (!drop
&& bs
->drv
->bdrv_make_empty
) {
1055 ret
= bs
->drv
->bdrv_make_empty(bs
);
1057 error_setg_errno(&local_err
, -ret
, "Could not empty %s",
1069 qemu_progress_end();
1074 error_report_err(local_err
);
1078 qprintf(quiet
, "Image committed.\n");
1083 * Returns -1 if 'buf' contains only zeroes, otherwise the byte index
1084 * of the first sector boundary within buf where the sector contains a
1085 * non-zero byte. This function is robust to a buffer that is not
1088 static int64_t find_nonzero(const uint8_t *buf
, int64_t n
)
1091 int64_t end
= QEMU_ALIGN_DOWN(n
, BDRV_SECTOR_SIZE
);
1093 for (i
= 0; i
< end
; i
+= BDRV_SECTOR_SIZE
) {
1094 if (!buffer_is_zero(buf
+ i
, BDRV_SECTOR_SIZE
)) {
1098 if (i
< n
&& !buffer_is_zero(buf
+ i
, n
- end
)) {
1105 * Returns true iff the first sector pointed to by 'buf' contains at least
1108 * 'pnum' is set to the number of sectors (including and immediately following
1109 * the first one) that are known to be in the same allocated/unallocated state.
1111 static int is_allocated_sectors(const uint8_t *buf
, int n
, int *pnum
)
1120 is_zero
= buffer_is_zero(buf
, 512);
1121 for(i
= 1; i
< n
; i
++) {
1123 if (is_zero
!= buffer_is_zero(buf
, 512)) {
1132 * Like is_allocated_sectors, but if the buffer starts with a used sector,
1133 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1134 * breaking up write requests for only small sparse areas.
1136 static int is_allocated_sectors_min(const uint8_t *buf
, int n
, int *pnum
,
1140 int num_checked
, num_used
;
1146 ret
= is_allocated_sectors(buf
, n
, pnum
);
1152 buf
+= BDRV_SECTOR_SIZE
* *pnum
;
1154 num_checked
= num_used
;
1157 ret
= is_allocated_sectors(buf
, n
, pnum
);
1159 buf
+= BDRV_SECTOR_SIZE
* *pnum
;
1161 num_checked
+= *pnum
;
1163 num_used
= num_checked
;
1164 } else if (*pnum
>= min
) {
1174 * Compares two buffers sector by sector. Returns 0 if the first
1175 * sector of each buffer matches, non-zero otherwise.
1177 * pnum is set to the sector-aligned size of the buffer prefix that
1178 * has the same matching status as the first sector.
1180 static int compare_buffers(const uint8_t *buf1
, const uint8_t *buf2
,
1181 int64_t bytes
, int64_t *pnum
)
1184 int64_t i
= MIN(bytes
, BDRV_SECTOR_SIZE
);
1188 res
= !!memcmp(buf1
, buf2
, i
);
1190 int64_t len
= MIN(bytes
- i
, BDRV_SECTOR_SIZE
);
1192 if (!!memcmp(buf1
+ i
, buf2
+ i
, len
) != res
) {
1202 #define IO_BUF_SIZE (2 * 1024 * 1024)
1205 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1207 * Intended for use by 'qemu-img compare': Returns 0 in case sectors are
1208 * filled with 0, 1 if sectors contain non-zero data (this is a comparison
1209 * failure), and 4 on error (the exit status for read errors), after emitting
1212 * @param blk: BlockBackend for the image
1213 * @param offset: Starting offset to check
1214 * @param bytes: Number of bytes to check
1215 * @param filename: Name of disk file we are checking (logging purpose)
1216 * @param buffer: Allocated buffer for storing read data
1217 * @param quiet: Flag for quiet mode
1219 static int check_empty_sectors(BlockBackend
*blk
, int64_t offset
,
1220 int64_t bytes
, const char *filename
,
1221 uint8_t *buffer
, bool quiet
)
1226 ret
= blk_pread(blk
, offset
, buffer
, bytes
);
1228 error_report("Error while reading offset %" PRId64
" of %s: %s",
1229 offset
, filename
, strerror(-ret
));
1232 idx
= find_nonzero(buffer
, bytes
);
1234 qprintf(quiet
, "Content mismatch at offset %" PRId64
"!\n",
1243 * Compares two images. Exit codes:
1245 * 0 - Images are identical
1247 * >1 - Error occurred
1249 static int img_compare(int argc
, char **argv
)
1251 const char *fmt1
= NULL
, *fmt2
= NULL
, *cache
, *filename1
, *filename2
;
1252 BlockBackend
*blk1
, *blk2
;
1253 BlockDriverState
*bs1
, *bs2
;
1254 int64_t total_size1
, total_size2
;
1255 uint8_t *buf1
= NULL
, *buf2
= NULL
;
1256 int64_t pnum1
, pnum2
;
1257 int allocated1
, allocated2
;
1258 int ret
= 0; /* return value - 0 Ident, 1 Different, >1 Error */
1259 bool progress
= false, quiet
= false, strict
= false;
1266 uint64_t progress_base
;
1267 bool image_opts
= false;
1268 bool force_share
= false;
1270 cache
= BDRV_DEFAULT_CACHE
;
1272 static const struct option long_options
[] = {
1273 {"help", no_argument
, 0, 'h'},
1274 {"object", required_argument
, 0, OPTION_OBJECT
},
1275 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
1276 {"force-share", no_argument
, 0, 'U'},
1279 c
= getopt_long(argc
, argv
, ":hf:F:T:pqsU",
1280 long_options
, NULL
);
1286 missing_argument(argv
[optind
- 1]);
1289 unrecognized_option(argv
[optind
- 1]);
1315 case OPTION_OBJECT
: {
1317 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
1324 case OPTION_IMAGE_OPTS
:
1330 /* Progress is not shown in Quiet mode */
1336 if (optind
!= argc
- 2) {
1337 error_exit("Expecting two image file names");
1339 filename1
= argv
[optind
++];
1340 filename2
= argv
[optind
++];
1342 if (qemu_opts_foreach(&qemu_object_opts
,
1343 user_creatable_add_opts_foreach
,
1349 /* Initialize before goto out */
1350 qemu_progress_init(progress
, 2.0);
1353 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
1355 error_report("Invalid source cache option: %s", cache
);
1360 blk1
= img_open(image_opts
, filename1
, fmt1
, flags
, writethrough
, quiet
,
1367 blk2
= img_open(image_opts
, filename2
, fmt2
, flags
, writethrough
, quiet
,
1376 buf1
= blk_blockalign(blk1
, IO_BUF_SIZE
);
1377 buf2
= blk_blockalign(blk2
, IO_BUF_SIZE
);
1378 total_size1
= blk_getlength(blk1
);
1379 if (total_size1
< 0) {
1380 error_report("Can't get size of %s: %s",
1381 filename1
, strerror(-total_size1
));
1385 total_size2
= blk_getlength(blk2
);
1386 if (total_size2
< 0) {
1387 error_report("Can't get size of %s: %s",
1388 filename2
, strerror(-total_size2
));
1392 total_size
= MIN(total_size1
, total_size2
);
1393 progress_base
= MAX(total_size1
, total_size2
);
1395 qemu_progress_print(0, 100);
1397 if (strict
&& total_size1
!= total_size2
) {
1399 qprintf(quiet
, "Strict mode: Image size mismatch!\n");
1403 while (offset
< total_size
) {
1404 int status1
, status2
;
1406 status1
= bdrv_block_status_above(bs1
, NULL
, offset
,
1407 total_size1
- offset
, &pnum1
, NULL
,
1411 error_report("Sector allocation test failed for %s", filename1
);
1414 allocated1
= status1
& BDRV_BLOCK_ALLOCATED
;
1416 status2
= bdrv_block_status_above(bs2
, NULL
, offset
,
1417 total_size2
- offset
, &pnum2
, NULL
,
1421 error_report("Sector allocation test failed for %s", filename2
);
1424 allocated2
= status2
& BDRV_BLOCK_ALLOCATED
;
1426 assert(pnum1
&& pnum2
);
1427 chunk
= MIN(pnum1
, pnum2
);
1430 if (status1
!= status2
) {
1432 qprintf(quiet
, "Strict mode: Offset %" PRId64
1433 " block status mismatch!\n", offset
);
1437 if ((status1
& BDRV_BLOCK_ZERO
) && (status2
& BDRV_BLOCK_ZERO
)) {
1439 } else if (allocated1
== allocated2
) {
1443 chunk
= MIN(chunk
, IO_BUF_SIZE
);
1444 ret
= blk_pread(blk1
, offset
, buf1
, chunk
);
1446 error_report("Error while reading offset %" PRId64
1448 offset
, filename1
, strerror(-ret
));
1452 ret
= blk_pread(blk2
, offset
, buf2
, chunk
);
1454 error_report("Error while reading offset %" PRId64
1456 offset
, filename2
, strerror(-ret
));
1460 ret
= compare_buffers(buf1
, buf2
, chunk
, &pnum
);
1461 if (ret
|| pnum
!= chunk
) {
1462 qprintf(quiet
, "Content mismatch at offset %" PRId64
"!\n",
1463 offset
+ (ret
? 0 : pnum
));
1469 chunk
= MIN(chunk
, IO_BUF_SIZE
);
1471 ret
= check_empty_sectors(blk1
, offset
, chunk
,
1472 filename1
, buf1
, quiet
);
1474 ret
= check_empty_sectors(blk2
, offset
, chunk
,
1475 filename2
, buf1
, quiet
);
1482 qemu_progress_print(((float) chunk
/ progress_base
) * 100, 100);
1485 if (total_size1
!= total_size2
) {
1486 BlockBackend
*blk_over
;
1487 const char *filename_over
;
1489 qprintf(quiet
, "Warning: Image size mismatch!\n");
1490 if (total_size1
> total_size2
) {
1492 filename_over
= filename1
;
1495 filename_over
= filename2
;
1498 while (offset
< progress_base
) {
1499 ret
= bdrv_block_status_above(blk_bs(blk_over
), NULL
, offset
,
1500 progress_base
- offset
, &chunk
,
1504 error_report("Sector allocation test failed for %s",
1509 if (ret
& BDRV_BLOCK_ALLOCATED
&& !(ret
& BDRV_BLOCK_ZERO
)) {
1510 chunk
= MIN(chunk
, IO_BUF_SIZE
);
1511 ret
= check_empty_sectors(blk_over
, offset
, chunk
,
1512 filename_over
, buf1
, quiet
);
1518 qemu_progress_print(((float) chunk
/ progress_base
) * 100, 100);
1522 qprintf(quiet
, "Images are identical.\n");
1532 qemu_progress_end();
1537 enum ImgConvertBlockStatus
{
1543 #define MAX_COROUTINES 16
1545 typedef struct ImgConvertState
{
1547 int64_t *src_sectors
;
1549 int64_t total_sectors
;
1550 int64_t allocated_sectors
;
1551 int64_t allocated_done
;
1554 enum ImgConvertBlockStatus status
;
1555 int64_t sector_next_status
;
1556 BlockBackend
*target
;
1559 bool unallocated_blocks_are_zero
;
1560 bool target_has_backing
;
1561 int64_t target_backing_sectors
; /* negative if unknown */
1565 size_t cluster_sectors
;
1567 long num_coroutines
;
1568 int running_coroutines
;
1569 Coroutine
*co
[MAX_COROUTINES
];
1570 int64_t wait_sector_num
[MAX_COROUTINES
];
1575 static void convert_select_part(ImgConvertState
*s
, int64_t sector_num
,
1576 int *src_cur
, int64_t *src_cur_offset
)
1579 *src_cur_offset
= 0;
1580 while (sector_num
- *src_cur_offset
>= s
->src_sectors
[*src_cur
]) {
1581 *src_cur_offset
+= s
->src_sectors
[*src_cur
];
1583 assert(*src_cur
< s
->src_num
);
1587 static int convert_iteration_sectors(ImgConvertState
*s
, int64_t sector_num
)
1589 int64_t src_cur_offset
;
1590 int ret
, n
, src_cur
;
1591 bool post_backing_zero
= false;
1593 convert_select_part(s
, sector_num
, &src_cur
, &src_cur_offset
);
1595 assert(s
->total_sectors
> sector_num
);
1596 n
= MIN(s
->total_sectors
- sector_num
, BDRV_REQUEST_MAX_SECTORS
);
1598 if (s
->target_backing_sectors
>= 0) {
1599 if (sector_num
>= s
->target_backing_sectors
) {
1600 post_backing_zero
= s
->unallocated_blocks_are_zero
;
1601 } else if (sector_num
+ n
> s
->target_backing_sectors
) {
1602 /* Split requests around target_backing_sectors (because
1603 * starting from there, zeros are handled differently) */
1604 n
= s
->target_backing_sectors
- sector_num
;
1608 if (s
->sector_next_status
<= sector_num
) {
1609 int64_t count
= n
* BDRV_SECTOR_SIZE
;
1611 if (s
->target_has_backing
) {
1613 ret
= bdrv_block_status(blk_bs(s
->src
[src_cur
]),
1614 (sector_num
- src_cur_offset
) *
1616 count
, &count
, NULL
, NULL
);
1618 ret
= bdrv_block_status_above(blk_bs(s
->src
[src_cur
]), NULL
,
1619 (sector_num
- src_cur_offset
) *
1621 count
, &count
, NULL
, NULL
);
1626 n
= DIV_ROUND_UP(count
, BDRV_SECTOR_SIZE
);
1628 if (ret
& BDRV_BLOCK_ZERO
) {
1629 s
->status
= post_backing_zero
? BLK_BACKING_FILE
: BLK_ZERO
;
1630 } else if (ret
& BDRV_BLOCK_DATA
) {
1631 s
->status
= BLK_DATA
;
1633 s
->status
= s
->target_has_backing
? BLK_BACKING_FILE
: BLK_DATA
;
1636 s
->sector_next_status
= sector_num
+ n
;
1639 n
= MIN(n
, s
->sector_next_status
- sector_num
);
1640 if (s
->status
== BLK_DATA
) {
1641 n
= MIN(n
, s
->buf_sectors
);
1644 /* We need to write complete clusters for compressed images, so if an
1645 * unallocated area is shorter than that, we must consider the whole
1646 * cluster allocated. */
1647 if (s
->compressed
) {
1648 if (n
< s
->cluster_sectors
) {
1649 n
= MIN(s
->cluster_sectors
, s
->total_sectors
- sector_num
);
1650 s
->status
= BLK_DATA
;
1652 n
= QEMU_ALIGN_DOWN(n
, s
->cluster_sectors
);
1659 static int coroutine_fn
convert_co_read(ImgConvertState
*s
, int64_t sector_num
,
1660 int nb_sectors
, uint8_t *buf
)
1666 assert(nb_sectors
<= s
->buf_sectors
);
1667 while (nb_sectors
> 0) {
1670 int64_t bs_sectors
, src_cur_offset
;
1672 /* In the case of compression with multiple source files, we can get a
1673 * nb_sectors that spreads into the next part. So we must be able to
1674 * read across multiple BDSes for one convert_read() call. */
1675 convert_select_part(s
, sector_num
, &src_cur
, &src_cur_offset
);
1676 blk
= s
->src
[src_cur
];
1677 bs_sectors
= s
->src_sectors
[src_cur
];
1679 n
= MIN(nb_sectors
, bs_sectors
- (sector_num
- src_cur_offset
));
1681 iov
.iov_len
= n
<< BDRV_SECTOR_BITS
;
1682 qemu_iovec_init_external(&qiov
, &iov
, 1);
1684 ret
= blk_co_preadv(
1685 blk
, (sector_num
- src_cur_offset
) << BDRV_SECTOR_BITS
,
1686 n
<< BDRV_SECTOR_BITS
, &qiov
, 0);
1693 buf
+= n
* BDRV_SECTOR_SIZE
;
1700 static int coroutine_fn
convert_co_write(ImgConvertState
*s
, int64_t sector_num
,
1701 int nb_sectors
, uint8_t *buf
,
1702 enum ImgConvertBlockStatus status
)
1708 while (nb_sectors
> 0) {
1710 BdrvRequestFlags flags
= s
->compressed
? BDRV_REQ_WRITE_COMPRESSED
: 0;
1713 case BLK_BACKING_FILE
:
1714 /* If we have a backing file, leave clusters unallocated that are
1715 * unallocated in the source image, so that the backing file is
1716 * visible at the respective offset. */
1717 assert(s
->target_has_backing
);
1721 /* If we're told to keep the target fully allocated (-S 0) or there
1722 * is real non-zero data, we must write it. Otherwise we can treat
1723 * it as zero sectors.
1724 * Compressed clusters need to be written as a whole, so in that
1725 * case we can only save the write if the buffer is completely
1727 if (!s
->min_sparse
||
1729 is_allocated_sectors_min(buf
, n
, &n
, s
->min_sparse
)) ||
1731 !buffer_is_zero(buf
, n
* BDRV_SECTOR_SIZE
)))
1734 iov
.iov_len
= n
<< BDRV_SECTOR_BITS
;
1735 qemu_iovec_init_external(&qiov
, &iov
, 1);
1737 ret
= blk_co_pwritev(s
->target
, sector_num
<< BDRV_SECTOR_BITS
,
1738 n
<< BDRV_SECTOR_BITS
, &qiov
, flags
);
1747 if (s
->has_zero_init
) {
1748 assert(!s
->target_has_backing
);
1751 ret
= blk_co_pwrite_zeroes(s
->target
,
1752 sector_num
<< BDRV_SECTOR_BITS
,
1753 n
<< BDRV_SECTOR_BITS
, 0);
1762 buf
+= n
* BDRV_SECTOR_SIZE
;
1768 static int coroutine_fn
convert_co_copy_range(ImgConvertState
*s
, int64_t sector_num
,
1773 while (nb_sectors
> 0) {
1776 int64_t bs_sectors
, src_cur_offset
;
1779 convert_select_part(s
, sector_num
, &src_cur
, &src_cur_offset
);
1780 offset
= (sector_num
- src_cur_offset
) << BDRV_SECTOR_BITS
;
1781 blk
= s
->src
[src_cur
];
1782 bs_sectors
= s
->src_sectors
[src_cur
];
1784 n
= MIN(nb_sectors
, bs_sectors
- (sector_num
- src_cur_offset
));
1786 ret
= blk_co_copy_range(blk
, offset
, s
->target
,
1787 sector_num
<< BDRV_SECTOR_BITS
,
1788 n
<< BDRV_SECTOR_BITS
, 0);
1799 static void coroutine_fn
convert_co_do_copy(void *opaque
)
1801 ImgConvertState
*s
= opaque
;
1802 uint8_t *buf
= NULL
;
1806 for (i
= 0; i
< s
->num_coroutines
; i
++) {
1807 if (s
->co
[i
] == qemu_coroutine_self()) {
1814 s
->running_coroutines
++;
1815 buf
= blk_blockalign(s
->target
, s
->buf_sectors
* BDRV_SECTOR_SIZE
);
1820 enum ImgConvertBlockStatus status
;
1823 qemu_co_mutex_lock(&s
->lock
);
1824 if (s
->ret
!= -EINPROGRESS
|| s
->sector_num
>= s
->total_sectors
) {
1825 qemu_co_mutex_unlock(&s
->lock
);
1828 n
= convert_iteration_sectors(s
, s
->sector_num
);
1830 qemu_co_mutex_unlock(&s
->lock
);
1834 /* save current sector and allocation status to local variables */
1835 sector_num
= s
->sector_num
;
1837 if (!s
->min_sparse
&& s
->status
== BLK_ZERO
) {
1838 n
= MIN(n
, s
->buf_sectors
);
1840 /* increment global sector counter so that other coroutines can
1841 * already continue reading beyond this request */
1843 qemu_co_mutex_unlock(&s
->lock
);
1845 if (status
== BLK_DATA
|| (!s
->min_sparse
&& status
== BLK_ZERO
)) {
1846 s
->allocated_done
+= n
;
1847 qemu_progress_print(100.0 * s
->allocated_done
/
1848 s
->allocated_sectors
, 0);
1852 copy_range
= s
->copy_range
&& s
->status
== BLK_DATA
;
1853 if (status
== BLK_DATA
&& !copy_range
) {
1854 ret
= convert_co_read(s
, sector_num
, n
, buf
);
1856 error_report("error while reading sector %" PRId64
1857 ": %s", sector_num
, strerror(-ret
));
1860 } else if (!s
->min_sparse
&& status
== BLK_ZERO
) {
1862 memset(buf
, 0x00, n
* BDRV_SECTOR_SIZE
);
1865 if (s
->wr_in_order
) {
1866 /* keep writes in order */
1867 while (s
->wr_offs
!= sector_num
&& s
->ret
== -EINPROGRESS
) {
1868 s
->wait_sector_num
[index
] = sector_num
;
1869 qemu_coroutine_yield();
1871 s
->wait_sector_num
[index
] = -1;
1874 if (s
->ret
== -EINPROGRESS
) {
1876 ret
= convert_co_copy_range(s
, sector_num
, n
);
1878 s
->copy_range
= false;
1882 ret
= convert_co_write(s
, sector_num
, n
, buf
, status
);
1885 error_report("error while writing sector %" PRId64
1886 ": %s", sector_num
, strerror(-ret
));
1891 if (s
->wr_in_order
) {
1892 /* reenter the coroutine that might have waited
1893 * for this write to complete */
1894 s
->wr_offs
= sector_num
+ n
;
1895 for (i
= 0; i
< s
->num_coroutines
; i
++) {
1896 if (s
->co
[i
] && s
->wait_sector_num
[i
] == s
->wr_offs
) {
1898 * A -> B -> A cannot occur because A has
1899 * s->wait_sector_num[i] == -1 during A -> B. Therefore
1900 * B will never enter A during this time window.
1902 qemu_coroutine_enter(s
->co
[i
]);
1910 s
->co
[index
] = NULL
;
1911 s
->running_coroutines
--;
1912 if (!s
->running_coroutines
&& s
->ret
== -EINPROGRESS
) {
1913 /* the convert job finished successfully */
1918 static int convert_do_copy(ImgConvertState
*s
)
1921 int64_t sector_num
= 0;
1923 /* Check whether we have zero initialisation or can get it efficiently */
1924 s
->has_zero_init
= s
->min_sparse
&& !s
->target_has_backing
1925 ? bdrv_has_zero_init(blk_bs(s
->target
))
1928 if (!s
->has_zero_init
&& !s
->target_has_backing
&&
1929 bdrv_can_write_zeroes_with_unmap(blk_bs(s
->target
)))
1931 ret
= blk_make_zero(s
->target
, BDRV_REQ_MAY_UNMAP
);
1933 s
->has_zero_init
= true;
1937 /* Allocate buffer for copied data. For compressed images, only one cluster
1938 * can be copied at a time. */
1939 if (s
->compressed
) {
1940 if (s
->cluster_sectors
<= 0 || s
->cluster_sectors
> s
->buf_sectors
) {
1941 error_report("invalid cluster size");
1944 s
->buf_sectors
= s
->cluster_sectors
;
1947 while (sector_num
< s
->total_sectors
) {
1948 n
= convert_iteration_sectors(s
, sector_num
);
1952 if (s
->status
== BLK_DATA
|| (!s
->min_sparse
&& s
->status
== BLK_ZERO
))
1954 s
->allocated_sectors
+= n
;
1960 s
->sector_next_status
= 0;
1961 s
->ret
= -EINPROGRESS
;
1963 qemu_co_mutex_init(&s
->lock
);
1964 for (i
= 0; i
< s
->num_coroutines
; i
++) {
1965 s
->co
[i
] = qemu_coroutine_create(convert_co_do_copy
, s
);
1966 s
->wait_sector_num
[i
] = -1;
1967 qemu_coroutine_enter(s
->co
[i
]);
1970 while (s
->running_coroutines
) {
1971 main_loop_wait(false);
1974 if (s
->compressed
&& !s
->ret
) {
1975 /* signal EOF to align */
1976 ret
= blk_pwrite_compressed(s
->target
, 0, NULL
, 0);
1985 static int img_convert(int argc
, char **argv
)
1987 int c
, bs_i
, flags
, src_flags
= 0;
1988 const char *fmt
= NULL
, *out_fmt
= NULL
, *cache
= "unsafe",
1989 *src_cache
= BDRV_DEFAULT_CACHE
, *out_baseimg
= NULL
,
1990 *out_filename
, *out_baseimg_param
, *snapshot_name
= NULL
;
1991 BlockDriver
*drv
= NULL
, *proto_drv
= NULL
;
1992 BlockDriverInfo bdi
;
1993 BlockDriverState
*out_bs
;
1994 QemuOpts
*opts
= NULL
, *sn_opts
= NULL
;
1995 QemuOptsList
*create_opts
= NULL
;
1996 char *options
= NULL
;
1997 Error
*local_err
= NULL
;
1998 bool writethrough
, src_writethrough
, quiet
= false, image_opts
= false,
1999 skip_create
= false, progress
= false, tgt_image_opts
= false;
2000 int64_t ret
= -EINVAL
;
2001 bool force_share
= false;
2003 ImgConvertState s
= (ImgConvertState
) {
2004 /* Need at least 4k of zeros for sparse detection */
2007 .buf_sectors
= IO_BUF_SIZE
/ BDRV_SECTOR_SIZE
,
2008 .wr_in_order
= true,
2009 .num_coroutines
= 8,
2013 static const struct option long_options
[] = {
2014 {"help", no_argument
, 0, 'h'},
2015 {"object", required_argument
, 0, OPTION_OBJECT
},
2016 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
2017 {"force-share", no_argument
, 0, 'U'},
2018 {"target-image-opts", no_argument
, 0, OPTION_TARGET_IMAGE_OPTS
},
2021 c
= getopt_long(argc
, argv
, ":hf:O:B:co:s:l:S:pt:T:qnm:WU",
2022 long_options
, NULL
);
2028 missing_argument(argv
[optind
- 1]);
2031 unrecognized_option(argv
[optind
- 1]);
2043 out_baseimg
= optarg
;
2046 s
.compressed
= true;
2047 s
.copy_range
= false;
2050 if (!is_valid_option_list(optarg
)) {
2051 error_report("Invalid option list: %s", optarg
);
2055 options
= g_strdup(optarg
);
2057 char *old_options
= options
;
2058 options
= g_strdup_printf("%s,%s", options
, optarg
);
2059 g_free(old_options
);
2063 snapshot_name
= optarg
;
2066 if (strstart(optarg
, SNAPSHOT_OPT_BASE
, NULL
)) {
2067 sn_opts
= qemu_opts_parse_noisily(&internal_snapshot_opts
,
2070 error_report("Failed in parsing snapshot param '%s'",
2075 snapshot_name
= optarg
;
2082 sval
= cvtnum(optarg
);
2084 error_report("Invalid minimum zero buffer size for sparse output specified");
2088 s
.min_sparse
= sval
/ BDRV_SECTOR_SIZE
;
2089 s
.copy_range
= false;
2108 if (qemu_strtol(optarg
, NULL
, 0, &s
.num_coroutines
) ||
2109 s
.num_coroutines
< 1 || s
.num_coroutines
> MAX_COROUTINES
) {
2110 error_report("Invalid number of coroutines. Allowed number of"
2111 " coroutines is between 1 and %d", MAX_COROUTINES
);
2116 s
.wr_in_order
= false;
2121 case OPTION_OBJECT
: {
2122 QemuOpts
*object_opts
;
2123 object_opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
2130 case OPTION_IMAGE_OPTS
:
2133 case OPTION_TARGET_IMAGE_OPTS
:
2134 tgt_image_opts
= true;
2139 if (!out_fmt
&& !tgt_image_opts
) {
2143 if (qemu_opts_foreach(&qemu_object_opts
,
2144 user_creatable_add_opts_foreach
,
2149 if (!s
.wr_in_order
&& s
.compressed
) {
2150 error_report("Out of order write and compress are mutually exclusive");
2154 if (tgt_image_opts
&& !skip_create
) {
2155 error_report("--target-image-opts requires use of -n flag");
2159 s
.src_num
= argc
- optind
- 1;
2160 out_filename
= s
.src_num
>= 1 ? argv
[argc
- 1] : NULL
;
2162 if (options
&& has_help_option(options
)) {
2164 ret
= print_block_option_help(out_filename
, out_fmt
);
2167 error_report("Option help requires a format be specified");
2172 if (s
.src_num
< 1) {
2173 error_report("Must specify image file name");
2178 /* ret is still -EINVAL until here */
2179 ret
= bdrv_parse_cache_mode(src_cache
, &src_flags
, &src_writethrough
);
2181 error_report("Invalid source cache option: %s", src_cache
);
2185 /* Initialize before goto out */
2189 qemu_progress_init(progress
, 1.0);
2190 qemu_progress_print(0, 100);
2192 s
.src
= g_new0(BlockBackend
*, s
.src_num
);
2193 s
.src_sectors
= g_new(int64_t, s
.src_num
);
2195 for (bs_i
= 0; bs_i
< s
.src_num
; bs_i
++) {
2196 s
.src
[bs_i
] = img_open(image_opts
, argv
[optind
+ bs_i
],
2197 fmt
, src_flags
, src_writethrough
, quiet
,
2203 s
.src_sectors
[bs_i
] = blk_nb_sectors(s
.src
[bs_i
]);
2204 if (s
.src_sectors
[bs_i
] < 0) {
2205 error_report("Could not get size of %s: %s",
2206 argv
[optind
+ bs_i
], strerror(-s
.src_sectors
[bs_i
]));
2210 s
.total_sectors
+= s
.src_sectors
[bs_i
];
2214 bdrv_snapshot_load_tmp(blk_bs(s
.src
[0]),
2215 qemu_opt_get(sn_opts
, SNAPSHOT_OPT_ID
),
2216 qemu_opt_get(sn_opts
, SNAPSHOT_OPT_NAME
),
2218 } else if (snapshot_name
!= NULL
) {
2219 if (s
.src_num
> 1) {
2220 error_report("No support for concatenating multiple snapshot");
2225 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s
.src
[0]), snapshot_name
,
2229 error_reportf_err(local_err
, "Failed to load snapshot: ");
2235 /* Find driver and parse its options */
2236 drv
= bdrv_find_format(out_fmt
);
2238 error_report("Unknown file format '%s'", out_fmt
);
2243 proto_drv
= bdrv_find_protocol(out_filename
, true, &local_err
);
2245 error_report_err(local_err
);
2250 if (!drv
->create_opts
) {
2251 error_report("Format driver '%s' does not support image creation",
2257 if (!proto_drv
->create_opts
) {
2258 error_report("Protocol driver '%s' does not support image creation",
2259 proto_drv
->format_name
);
2264 create_opts
= qemu_opts_append(create_opts
, drv
->create_opts
);
2265 create_opts
= qemu_opts_append(create_opts
, proto_drv
->create_opts
);
2267 opts
= qemu_opts_create(create_opts
, NULL
, 0, &error_abort
);
2269 qemu_opts_do_parse(opts
, options
, NULL
, &local_err
);
2271 error_report_err(local_err
);
2277 qemu_opt_set_number(opts
, BLOCK_OPT_SIZE
, s
.total_sectors
* 512,
2279 ret
= add_old_style_options(out_fmt
, opts
, out_baseimg
, NULL
);
2285 /* Get backing file name if -o backing_file was used */
2286 out_baseimg_param
= qemu_opt_get(opts
, BLOCK_OPT_BACKING_FILE
);
2287 if (out_baseimg_param
) {
2288 out_baseimg
= out_baseimg_param
;
2290 s
.target_has_backing
= (bool) out_baseimg
;
2292 if (s
.src_num
> 1 && out_baseimg
) {
2293 error_report("Having a backing file for the target makes no sense when "
2294 "concatenating multiple input images");
2299 /* Check if compression is supported */
2302 qemu_opt_get_bool(opts
, BLOCK_OPT_ENCRYPT
, false);
2303 const char *encryptfmt
=
2304 qemu_opt_get(opts
, BLOCK_OPT_ENCRYPT_FORMAT
);
2305 const char *preallocation
=
2306 qemu_opt_get(opts
, BLOCK_OPT_PREALLOC
);
2308 if (drv
&& !drv
->bdrv_co_pwritev_compressed
) {
2309 error_report("Compression not supported for this file format");
2314 if (encryption
|| encryptfmt
) {
2315 error_report("Compression and encryption not supported at "
2322 && strcmp(preallocation
, "off"))
2324 error_report("Compression and preallocation not supported at "
2332 /* Create the new image */
2333 ret
= bdrv_create(drv
, out_filename
, opts
, &local_err
);
2335 error_reportf_err(local_err
, "%s: error while converting %s: ",
2336 out_filename
, out_fmt
);
2341 flags
= s
.min_sparse
? (BDRV_O_RDWR
| BDRV_O_UNMAP
) : BDRV_O_RDWR
;
2342 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
2344 error_report("Invalid cache option: %s", cache
);
2349 s
.target
= img_open(tgt_image_opts
, out_filename
, out_fmt
,
2350 flags
, writethrough
, quiet
, false);
2352 /* TODO ultimately we should allow --target-image-opts
2353 * to be used even when -n is not given.
2354 * That has to wait for bdrv_create to be improved
2355 * to allow filenames in option syntax
2357 s
.target
= img_open_new_file(out_filename
, opts
, out_fmt
,
2358 flags
, writethrough
, quiet
, false);
2364 out_bs
= blk_bs(s
.target
);
2366 if (s
.compressed
&& !out_bs
->drv
->bdrv_co_pwritev_compressed
) {
2367 error_report("Compression not supported for this file format");
2372 /* increase bufsectors from the default 4096 (2M) if opt_transfer
2373 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
2375 s
.buf_sectors
= MIN(32768,
2377 MAX(out_bs
->bl
.opt_transfer
>> BDRV_SECTOR_BITS
,
2378 out_bs
->bl
.pdiscard_alignment
>>
2379 BDRV_SECTOR_BITS
)));
2382 int64_t output_sectors
= blk_nb_sectors(s
.target
);
2383 if (output_sectors
< 0) {
2384 error_report("unable to get output image length: %s",
2385 strerror(-output_sectors
));
2388 } else if (output_sectors
< s
.total_sectors
) {
2389 error_report("output file is smaller than input file");
2395 if (s
.target_has_backing
) {
2396 /* Errors are treated as "backing length unknown" (which means
2397 * s.target_backing_sectors has to be negative, which it will
2398 * be automatically). The backing file length is used only
2399 * for optimizations, so such a case is not fatal. */
2400 s
.target_backing_sectors
= bdrv_nb_sectors(out_bs
->backing
->bs
);
2402 s
.target_backing_sectors
= -1;
2405 ret
= bdrv_get_info(out_bs
, &bdi
);
2408 error_report("could not get block driver info");
2412 s
.compressed
= s
.compressed
|| bdi
.needs_compressed_writes
;
2413 s
.cluster_sectors
= bdi
.cluster_size
/ BDRV_SECTOR_SIZE
;
2414 s
.unallocated_blocks_are_zero
= bdi
.unallocated_blocks_are_zero
;
2417 ret
= convert_do_copy(&s
);
2420 qemu_progress_print(100, 0);
2422 qemu_progress_end();
2423 qemu_opts_del(opts
);
2424 qemu_opts_free(create_opts
);
2425 qemu_opts_del(sn_opts
);
2426 blk_unref(s
.target
);
2428 for (bs_i
= 0; bs_i
< s
.src_num
; bs_i
++) {
2429 blk_unref(s
.src
[bs_i
]);
2433 g_free(s
.src_sectors
);
2441 static void dump_snapshots(BlockDriverState
*bs
)
2443 QEMUSnapshotInfo
*sn_tab
, *sn
;
2446 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
2449 printf("Snapshot list:\n");
2450 bdrv_snapshot_dump(fprintf
, stdout
, NULL
);
2452 for(i
= 0; i
< nb_sns
; i
++) {
2454 bdrv_snapshot_dump(fprintf
, stdout
, sn
);
2460 static void dump_json_image_info_list(ImageInfoList
*list
)
2464 Visitor
*v
= qobject_output_visitor_new(&obj
);
2466 visit_type_ImageInfoList(v
, NULL
, &list
, &error_abort
);
2467 visit_complete(v
, &obj
);
2468 str
= qobject_to_json_pretty(obj
);
2469 assert(str
!= NULL
);
2470 printf("%s\n", qstring_get_str(str
));
2476 static void dump_json_image_info(ImageInfo
*info
)
2480 Visitor
*v
= qobject_output_visitor_new(&obj
);
2482 visit_type_ImageInfo(v
, NULL
, &info
, &error_abort
);
2483 visit_complete(v
, &obj
);
2484 str
= qobject_to_json_pretty(obj
);
2485 assert(str
!= NULL
);
2486 printf("%s\n", qstring_get_str(str
));
2492 static void dump_human_image_info_list(ImageInfoList
*list
)
2494 ImageInfoList
*elem
;
2497 for (elem
= list
; elem
; elem
= elem
->next
) {
2503 bdrv_image_info_dump(fprintf
, stdout
, elem
->value
);
2507 static gboolean
str_equal_func(gconstpointer a
, gconstpointer b
)
2509 return strcmp(a
, b
) == 0;
2513 * Open an image file chain and return an ImageInfoList
2515 * @filename: topmost image filename
2516 * @fmt: topmost image format (may be NULL to autodetect)
2517 * @chain: true - enumerate entire backing file chain
2518 * false - only topmost image file
2520 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2521 * image file. If there was an error a message will have been printed to
2524 static ImageInfoList
*collect_image_info_list(bool image_opts
,
2525 const char *filename
,
2527 bool chain
, bool force_share
)
2529 ImageInfoList
*head
= NULL
;
2530 ImageInfoList
**last
= &head
;
2531 GHashTable
*filenames
;
2534 filenames
= g_hash_table_new_full(g_str_hash
, str_equal_func
, NULL
, NULL
);
2538 BlockDriverState
*bs
;
2540 ImageInfoList
*elem
;
2542 if (g_hash_table_lookup_extended(filenames
, filename
, NULL
, NULL
)) {
2543 error_report("Backing file '%s' creates an infinite loop.",
2547 g_hash_table_insert(filenames
, (gpointer
)filename
, NULL
);
2549 blk
= img_open(image_opts
, filename
, fmt
,
2550 BDRV_O_NO_BACKING
| BDRV_O_NO_IO
, false, false,
2557 bdrv_query_image_info(bs
, &info
, &err
);
2559 error_report_err(err
);
2564 elem
= g_new0(ImageInfoList
, 1);
2571 filename
= fmt
= NULL
;
2573 if (info
->has_full_backing_filename
) {
2574 filename
= info
->full_backing_filename
;
2575 } else if (info
->has_backing_filename
) {
2576 error_report("Could not determine absolute backing filename,"
2577 " but backing filename '%s' present",
2578 info
->backing_filename
);
2581 if (info
->has_backing_filename_format
) {
2582 fmt
= info
->backing_filename_format
;
2586 g_hash_table_destroy(filenames
);
2590 qapi_free_ImageInfoList(head
);
2591 g_hash_table_destroy(filenames
);
2595 static int img_info(int argc
, char **argv
)
2598 OutputFormat output_format
= OFORMAT_HUMAN
;
2600 const char *filename
, *fmt
, *output
;
2601 ImageInfoList
*list
;
2602 bool image_opts
= false;
2603 bool force_share
= false;
2608 int option_index
= 0;
2609 static const struct option long_options
[] = {
2610 {"help", no_argument
, 0, 'h'},
2611 {"format", required_argument
, 0, 'f'},
2612 {"output", required_argument
, 0, OPTION_OUTPUT
},
2613 {"backing-chain", no_argument
, 0, OPTION_BACKING_CHAIN
},
2614 {"object", required_argument
, 0, OPTION_OBJECT
},
2615 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
2616 {"force-share", no_argument
, 0, 'U'},
2619 c
= getopt_long(argc
, argv
, ":f:hU",
2620 long_options
, &option_index
);
2626 missing_argument(argv
[optind
- 1]);
2629 unrecognized_option(argv
[optind
- 1]);
2643 case OPTION_BACKING_CHAIN
:
2646 case OPTION_OBJECT
: {
2648 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
2654 case OPTION_IMAGE_OPTS
:
2659 if (optind
!= argc
- 1) {
2660 error_exit("Expecting one image file name");
2662 filename
= argv
[optind
++];
2664 if (output
&& !strcmp(output
, "json")) {
2665 output_format
= OFORMAT_JSON
;
2666 } else if (output
&& !strcmp(output
, "human")) {
2667 output_format
= OFORMAT_HUMAN
;
2668 } else if (output
) {
2669 error_report("--output must be used with human or json as argument.");
2673 if (qemu_opts_foreach(&qemu_object_opts
,
2674 user_creatable_add_opts_foreach
,
2679 list
= collect_image_info_list(image_opts
, filename
, fmt
, chain
,
2685 switch (output_format
) {
2687 dump_human_image_info_list(list
);
2691 dump_json_image_info_list(list
);
2693 dump_json_image_info(list
->value
);
2698 qapi_free_ImageInfoList(list
);
2702 static void dump_map_entry(OutputFormat output_format
, MapEntry
*e
,
2705 switch (output_format
) {
2707 if (e
->data
&& !e
->has_offset
) {
2708 error_report("File contains external, encrypted or compressed clusters.");
2711 if (e
->data
&& !e
->zero
) {
2712 printf("%#-16"PRIx64
"%#-16"PRIx64
"%#-16"PRIx64
"%s\n",
2713 e
->start
, e
->length
,
2714 e
->has_offset
? e
->offset
: 0,
2715 e
->has_filename
? e
->filename
: "");
2717 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2718 * Modify the flags here to allow more coalescing.
2720 if (next
&& (!next
->data
|| next
->zero
)) {
2726 printf("%s{ \"start\": %"PRId64
", \"length\": %"PRId64
","
2727 " \"depth\": %"PRId64
", \"zero\": %s, \"data\": %s",
2728 (e
->start
== 0 ? "[" : ",\n"),
2729 e
->start
, e
->length
, e
->depth
,
2730 e
->zero
? "true" : "false",
2731 e
->data
? "true" : "false");
2732 if (e
->has_offset
) {
2733 printf(", \"offset\": %"PRId64
"", e
->offset
);
2744 static int get_block_status(BlockDriverState
*bs
, int64_t offset
,
2745 int64_t bytes
, MapEntry
*e
)
2749 BlockDriverState
*file
;
2753 /* As an optimization, we could cache the current range of unallocated
2754 * clusters in each file of the chain, and avoid querying the same
2760 ret
= bdrv_block_status(bs
, offset
, bytes
, &bytes
, &map
, &file
);
2765 if (ret
& (BDRV_BLOCK_ZERO
|BDRV_BLOCK_DATA
)) {
2768 bs
= backing_bs(bs
);
2777 has_offset
= !!(ret
& BDRV_BLOCK_OFFSET_VALID
);
2782 .data
= !!(ret
& BDRV_BLOCK_DATA
),
2783 .zero
= !!(ret
& BDRV_BLOCK_ZERO
),
2785 .has_offset
= has_offset
,
2787 .has_filename
= file
&& has_offset
,
2788 .filename
= file
&& has_offset
? file
->filename
: NULL
,
2794 static inline bool entry_mergeable(const MapEntry
*curr
, const MapEntry
*next
)
2796 if (curr
->length
== 0) {
2799 if (curr
->zero
!= next
->zero
||
2800 curr
->data
!= next
->data
||
2801 curr
->depth
!= next
->depth
||
2802 curr
->has_filename
!= next
->has_filename
||
2803 curr
->has_offset
!= next
->has_offset
) {
2806 if (curr
->has_filename
&& strcmp(curr
->filename
, next
->filename
)) {
2809 if (curr
->has_offset
&& curr
->offset
+ curr
->length
!= next
->offset
) {
2815 static int img_map(int argc
, char **argv
)
2818 OutputFormat output_format
= OFORMAT_HUMAN
;
2820 BlockDriverState
*bs
;
2821 const char *filename
, *fmt
, *output
;
2823 MapEntry curr
= { .length
= 0 }, next
;
2825 bool image_opts
= false;
2826 bool force_share
= false;
2831 int option_index
= 0;
2832 static const struct option long_options
[] = {
2833 {"help", no_argument
, 0, 'h'},
2834 {"format", required_argument
, 0, 'f'},
2835 {"output", required_argument
, 0, OPTION_OUTPUT
},
2836 {"object", required_argument
, 0, OPTION_OBJECT
},
2837 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
2838 {"force-share", no_argument
, 0, 'U'},
2841 c
= getopt_long(argc
, argv
, ":f:hU",
2842 long_options
, &option_index
);
2848 missing_argument(argv
[optind
- 1]);
2851 unrecognized_option(argv
[optind
- 1]);
2865 case OPTION_OBJECT
: {
2867 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
2873 case OPTION_IMAGE_OPTS
:
2878 if (optind
!= argc
- 1) {
2879 error_exit("Expecting one image file name");
2881 filename
= argv
[optind
];
2883 if (output
&& !strcmp(output
, "json")) {
2884 output_format
= OFORMAT_JSON
;
2885 } else if (output
&& !strcmp(output
, "human")) {
2886 output_format
= OFORMAT_HUMAN
;
2887 } else if (output
) {
2888 error_report("--output must be used with human or json as argument.");
2892 if (qemu_opts_foreach(&qemu_object_opts
,
2893 user_creatable_add_opts_foreach
,
2898 blk
= img_open(image_opts
, filename
, fmt
, 0, false, false, force_share
);
2904 if (output_format
== OFORMAT_HUMAN
) {
2905 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2908 length
= blk_getlength(blk
);
2909 while (curr
.start
+ curr
.length
< length
) {
2910 int64_t offset
= curr
.start
+ curr
.length
;
2913 /* Probe up to 1 GiB at a time. */
2914 n
= QEMU_ALIGN_DOWN(MIN(1 << 30, length
- offset
), BDRV_SECTOR_SIZE
);
2915 ret
= get_block_status(bs
, offset
, n
, &next
);
2918 error_report("Could not read file metadata: %s", strerror(-ret
));
2922 if (entry_mergeable(&curr
, &next
)) {
2923 curr
.length
+= next
.length
;
2927 if (curr
.length
> 0) {
2928 dump_map_entry(output_format
, &curr
, &next
);
2933 dump_map_entry(output_format
, &curr
, NULL
);
2940 #define SNAPSHOT_LIST 1
2941 #define SNAPSHOT_CREATE 2
2942 #define SNAPSHOT_APPLY 3
2943 #define SNAPSHOT_DELETE 4
2945 static int img_snapshot(int argc
, char **argv
)
2948 BlockDriverState
*bs
;
2949 QEMUSnapshotInfo sn
;
2950 char *filename
, *snapshot_name
= NULL
;
2951 int c
, ret
= 0, bdrv_oflags
;
2956 bool image_opts
= false;
2957 bool force_share
= false;
2959 bdrv_oflags
= BDRV_O_RDWR
;
2960 /* Parse commandline parameters */
2962 static const struct option long_options
[] = {
2963 {"help", no_argument
, 0, 'h'},
2964 {"object", required_argument
, 0, OPTION_OBJECT
},
2965 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
2966 {"force-share", no_argument
, 0, 'U'},
2969 c
= getopt_long(argc
, argv
, ":la:c:d:hqU",
2970 long_options
, NULL
);
2976 missing_argument(argv
[optind
- 1]);
2979 unrecognized_option(argv
[optind
- 1]);
2986 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
2989 action
= SNAPSHOT_LIST
;
2990 bdrv_oflags
&= ~BDRV_O_RDWR
; /* no need for RW */
2994 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
2997 action
= SNAPSHOT_APPLY
;
2998 snapshot_name
= optarg
;
3002 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
3005 action
= SNAPSHOT_CREATE
;
3006 snapshot_name
= optarg
;
3010 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
3013 action
= SNAPSHOT_DELETE
;
3014 snapshot_name
= optarg
;
3022 case OPTION_OBJECT
: {
3024 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
3030 case OPTION_IMAGE_OPTS
:
3036 if (optind
!= argc
- 1) {
3037 error_exit("Expecting one image file name");
3039 filename
= argv
[optind
++];
3041 if (qemu_opts_foreach(&qemu_object_opts
,
3042 user_creatable_add_opts_foreach
,
3047 /* Open the image */
3048 blk
= img_open(image_opts
, filename
, NULL
, bdrv_oflags
, false, quiet
,
3055 /* Perform the requested action */
3061 case SNAPSHOT_CREATE
:
3062 memset(&sn
, 0, sizeof(sn
));
3063 pstrcpy(sn
.name
, sizeof(sn
.name
), snapshot_name
);
3065 qemu_gettimeofday(&tv
);
3066 sn
.date_sec
= tv
.tv_sec
;
3067 sn
.date_nsec
= tv
.tv_usec
* 1000;
3069 ret
= bdrv_snapshot_create(bs
, &sn
);
3071 error_report("Could not create snapshot '%s': %d (%s)",
3072 snapshot_name
, ret
, strerror(-ret
));
3076 case SNAPSHOT_APPLY
:
3077 ret
= bdrv_snapshot_goto(bs
, snapshot_name
, &err
);
3079 error_reportf_err(err
, "Could not apply snapshot '%s': ",
3084 case SNAPSHOT_DELETE
:
3085 bdrv_snapshot_delete_by_id_or_name(bs
, snapshot_name
, &err
);
3087 error_reportf_err(err
, "Could not delete snapshot '%s': ",
3102 static int img_rebase(int argc
, char **argv
)
3104 BlockBackend
*blk
= NULL
, *blk_old_backing
= NULL
, *blk_new_backing
= NULL
;
3105 uint8_t *buf_old
= NULL
;
3106 uint8_t *buf_new
= NULL
;
3107 BlockDriverState
*bs
= NULL
;
3109 const char *fmt
, *cache
, *src_cache
, *out_basefmt
, *out_baseimg
;
3110 int c
, flags
, src_flags
, ret
;
3111 bool writethrough
, src_writethrough
;
3113 bool force_share
= false;
3116 Error
*local_err
= NULL
;
3117 bool image_opts
= false;
3119 /* Parse commandline parameters */
3121 cache
= BDRV_DEFAULT_CACHE
;
3122 src_cache
= BDRV_DEFAULT_CACHE
;
3126 static const struct option long_options
[] = {
3127 {"help", no_argument
, 0, 'h'},
3128 {"object", required_argument
, 0, OPTION_OBJECT
},
3129 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
3130 {"force-share", no_argument
, 0, 'U'},
3133 c
= getopt_long(argc
, argv
, ":hf:F:b:upt:T:qU",
3134 long_options
, NULL
);
3140 missing_argument(argv
[optind
- 1]);
3143 unrecognized_option(argv
[optind
- 1]);
3152 out_basefmt
= optarg
;
3155 out_baseimg
= optarg
;
3172 case OPTION_OBJECT
: {
3174 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
3180 case OPTION_IMAGE_OPTS
:
3193 if (optind
!= argc
- 1) {
3194 error_exit("Expecting one image file name");
3196 if (!unsafe
&& !out_baseimg
) {
3197 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
3199 filename
= argv
[optind
++];
3201 if (qemu_opts_foreach(&qemu_object_opts
,
3202 user_creatable_add_opts_foreach
,
3207 qemu_progress_init(progress
, 2.0);
3208 qemu_progress_print(0, 100);
3210 flags
= BDRV_O_RDWR
| (unsafe
? BDRV_O_NO_BACKING
: 0);
3211 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
3213 error_report("Invalid cache option: %s", cache
);
3218 ret
= bdrv_parse_cache_mode(src_cache
, &src_flags
, &src_writethrough
);
3220 error_report("Invalid source cache option: %s", src_cache
);
3224 /* The source files are opened read-only, don't care about WCE */
3225 assert((src_flags
& BDRV_O_RDWR
) == 0);
3226 (void) src_writethrough
;
3231 * Ignore the old backing file for unsafe rebase in case we want to correct
3232 * the reference to a renamed or moved backing file.
3234 blk
= img_open(image_opts
, filename
, fmt
, flags
, writethrough
, quiet
,
3242 if (out_basefmt
!= NULL
) {
3243 if (bdrv_find_format(out_basefmt
) == NULL
) {
3244 error_report("Invalid format name: '%s'", out_basefmt
);
3250 /* For safe rebasing we need to compare old and new backing file */
3252 char backing_name
[PATH_MAX
];
3253 QDict
*options
= NULL
;
3255 if (bs
->backing_format
[0] != '\0') {
3256 options
= qdict_new();
3257 qdict_put_str(options
, "driver", bs
->backing_format
);
3262 options
= qdict_new();
3264 qdict_put_bool(options
, BDRV_OPT_FORCE_SHARE
, true);
3266 bdrv_get_backing_filename(bs
, backing_name
, sizeof(backing_name
));
3267 blk_old_backing
= blk_new_open(backing_name
, NULL
,
3268 options
, src_flags
, &local_err
);
3269 if (!blk_old_backing
) {
3270 error_reportf_err(local_err
,
3271 "Could not open old backing file '%s': ",
3277 if (out_baseimg
[0]) {
3278 const char *overlay_filename
;
3279 char *out_real_path
;
3281 options
= qdict_new();
3283 qdict_put_str(options
, "driver", out_basefmt
);
3286 qdict_put_bool(options
, BDRV_OPT_FORCE_SHARE
, true);
3289 overlay_filename
= bs
->exact_filename
[0] ? bs
->exact_filename
3291 out_real_path
= g_malloc(PATH_MAX
);
3293 bdrv_get_full_backing_filename_from_filename(overlay_filename
,
3299 error_reportf_err(local_err
,
3300 "Could not resolve backing filename: ");
3302 g_free(out_real_path
);
3306 blk_new_backing
= blk_new_open(out_real_path
, NULL
,
3307 options
, src_flags
, &local_err
);
3308 g_free(out_real_path
);
3309 if (!blk_new_backing
) {
3310 error_reportf_err(local_err
,
3311 "Could not open new backing file '%s': ",
3320 * Check each unallocated cluster in the COW file. If it is unallocated,
3321 * accesses go to the backing file. We must therefore compare this cluster
3322 * in the old and new backing file, and if they differ we need to copy it
3323 * from the old backing file into the COW file.
3325 * If qemu-img crashes during this step, no harm is done. The content of
3326 * the image is the same as the original one at any time.
3330 int64_t old_backing_size
;
3331 int64_t new_backing_size
= 0;
3334 float local_progress
= 0;
3336 buf_old
= blk_blockalign(blk
, IO_BUF_SIZE
);
3337 buf_new
= blk_blockalign(blk
, IO_BUF_SIZE
);
3339 size
= blk_getlength(blk
);
3341 error_report("Could not get size of '%s': %s",
3342 filename
, strerror(-size
));
3346 old_backing_size
= blk_getlength(blk_old_backing
);
3347 if (old_backing_size
< 0) {
3348 char backing_name
[PATH_MAX
];
3350 bdrv_get_backing_filename(bs
, backing_name
, sizeof(backing_name
));
3351 error_report("Could not get size of '%s': %s",
3352 backing_name
, strerror(-old_backing_size
));
3356 if (blk_new_backing
) {
3357 new_backing_size
= blk_getlength(blk_new_backing
);
3358 if (new_backing_size
< 0) {
3359 error_report("Could not get size of '%s': %s",
3360 out_baseimg
, strerror(-new_backing_size
));
3367 local_progress
= (float)100 / (size
/ MIN(size
, IO_BUF_SIZE
));
3370 for (offset
= 0; offset
< size
; offset
+= n
) {
3371 /* How many bytes can we handle with the next read? */
3372 n
= MIN(IO_BUF_SIZE
, size
- offset
);
3374 /* If the cluster is allocated, we don't need to take action */
3375 ret
= bdrv_is_allocated(bs
, offset
, n
, &n
);
3377 error_report("error while reading image metadata: %s",
3386 * Read old and new backing file and take into consideration that
3387 * backing files may be smaller than the COW image.
3389 if (offset
>= old_backing_size
) {
3390 memset(buf_old
, 0, n
);
3392 if (offset
+ n
> old_backing_size
) {
3393 n
= old_backing_size
- offset
;
3396 ret
= blk_pread(blk_old_backing
, offset
, buf_old
, n
);
3398 error_report("error while reading from old backing file");
3403 if (offset
>= new_backing_size
|| !blk_new_backing
) {
3404 memset(buf_new
, 0, n
);
3406 if (offset
+ n
> new_backing_size
) {
3407 n
= new_backing_size
- offset
;
3410 ret
= blk_pread(blk_new_backing
, offset
, buf_new
, n
);
3412 error_report("error while reading from new backing file");
3417 /* If they differ, we need to write to the COW file */
3418 uint64_t written
= 0;
3420 while (written
< n
) {
3423 if (compare_buffers(buf_old
+ written
, buf_new
+ written
,
3424 n
- written
, &pnum
))
3426 ret
= blk_pwrite(blk
, offset
+ written
,
3427 buf_old
+ written
, pnum
, 0);
3429 error_report("Error while writing to COW image: %s",
3437 qemu_progress_print(local_progress
, 100);
3442 * Change the backing file. All clusters that are different from the old
3443 * backing file are overwritten in the COW file now, so the visible content
3444 * doesn't change when we switch the backing file.
3446 if (out_baseimg
&& *out_baseimg
) {
3447 ret
= bdrv_change_backing_file(bs
, out_baseimg
, out_basefmt
);
3449 ret
= bdrv_change_backing_file(bs
, NULL
, NULL
);
3452 if (ret
== -ENOSPC
) {
3453 error_report("Could not change the backing file to '%s': No "
3454 "space left in the file header", out_baseimg
);
3455 } else if (ret
< 0) {
3456 error_report("Could not change the backing file to '%s': %s",
3457 out_baseimg
, strerror(-ret
));
3460 qemu_progress_print(100, 0);
3462 * TODO At this point it is possible to check if any clusters that are
3463 * allocated in the COW file are the same in the backing file. If so, they
3464 * could be dropped from the COW file. Don't do this before switching the
3465 * backing file, in case of a crash this would lead to corruption.
3468 qemu_progress_end();
3471 blk_unref(blk_old_backing
);
3472 blk_unref(blk_new_backing
);
3474 qemu_vfree(buf_old
);
3475 qemu_vfree(buf_new
);
3484 static int img_resize(int argc
, char **argv
)
3487 int c
, ret
, relative
;
3488 const char *filename
, *fmt
, *size
;
3489 int64_t n
, total_size
, current_size
, new_size
;
3491 BlockBackend
*blk
= NULL
;
3492 PreallocMode prealloc
= PREALLOC_MODE_OFF
;
3495 static QemuOptsList resize_options
= {
3496 .name
= "resize_options",
3497 .head
= QTAILQ_HEAD_INITIALIZER(resize_options
.head
),
3500 .name
= BLOCK_OPT_SIZE
,
3501 .type
= QEMU_OPT_SIZE
,
3502 .help
= "Virtual disk size"
3508 bool image_opts
= false;
3509 bool shrink
= false;
3511 /* Remove size from argv manually so that negative numbers are not treated
3512 * as options by getopt. */
3514 error_exit("Not enough arguments");
3518 size
= argv
[--argc
];
3520 /* Parse getopt arguments */
3523 static const struct option long_options
[] = {
3524 {"help", no_argument
, 0, 'h'},
3525 {"object", required_argument
, 0, OPTION_OBJECT
},
3526 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
3527 {"preallocation", required_argument
, 0, OPTION_PREALLOCATION
},
3528 {"shrink", no_argument
, 0, OPTION_SHRINK
},
3531 c
= getopt_long(argc
, argv
, ":f:hq",
3532 long_options
, NULL
);
3538 missing_argument(argv
[optind
- 1]);
3541 unrecognized_option(argv
[optind
- 1]);
3552 case OPTION_OBJECT
: {
3554 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
3560 case OPTION_IMAGE_OPTS
:
3563 case OPTION_PREALLOCATION
:
3564 prealloc
= qapi_enum_parse(&PreallocMode_lookup
, optarg
,
3565 PREALLOC_MODE__MAX
, NULL
);
3566 if (prealloc
== PREALLOC_MODE__MAX
) {
3567 error_report("Invalid preallocation mode '%s'", optarg
);
3576 if (optind
!= argc
- 1) {
3577 error_exit("Expecting image file name and size");
3579 filename
= argv
[optind
++];
3581 if (qemu_opts_foreach(&qemu_object_opts
,
3582 user_creatable_add_opts_foreach
,
3587 /* Choose grow, shrink, or absolute resize mode */
3603 param
= qemu_opts_create(&resize_options
, NULL
, 0, &error_abort
);
3604 qemu_opt_set(param
, BLOCK_OPT_SIZE
, size
, &err
);
3606 error_report_err(err
);
3608 qemu_opts_del(param
);
3611 n
= qemu_opt_get_size(param
, BLOCK_OPT_SIZE
, 0);
3612 qemu_opts_del(param
);
3614 blk
= img_open(image_opts
, filename
, fmt
,
3615 BDRV_O_RDWR
| BDRV_O_RESIZE
, false, quiet
,
3622 current_size
= blk_getlength(blk
);
3623 if (current_size
< 0) {
3624 error_report("Failed to inquire current image length: %s",
3625 strerror(-current_size
));
3631 total_size
= current_size
+ n
* relative
;
3635 if (total_size
<= 0) {
3636 error_report("New image size must be positive");
3641 if (total_size
<= current_size
&& prealloc
!= PREALLOC_MODE_OFF
) {
3642 error_report("Preallocation can only be used for growing images");
3647 if (total_size
< current_size
&& !shrink
) {
3648 warn_report("Shrinking an image will delete all data beyond the "
3649 "shrunken image's end. Before performing such an "
3650 "operation, make sure there is no important data there.");
3652 if (g_strcmp0(bdrv_get_format_name(blk_bs(blk
)), "raw") != 0) {
3654 "Use the --shrink option to perform a shrink operation.");
3658 warn_report("Using the --shrink option will suppress this message. "
3659 "Note that future versions of qemu-img may refuse to "
3660 "shrink images without this option.");
3664 ret
= blk_truncate(blk
, total_size
, prealloc
, &err
);
3666 error_report_err(err
);
3670 new_size
= blk_getlength(blk
);
3672 error_report("Failed to verify truncated image length: %s",
3673 strerror(-new_size
));
3678 /* Some block drivers implement a truncation method, but only so
3679 * the user can cause qemu to refresh the image's size from disk.
3680 * The idea is that the user resizes the image outside of qemu and
3681 * then invokes block_resize to inform qemu about it.
3682 * (This includes iscsi and file-posix for device files.)
3683 * Of course, that is not the behavior someone invoking
3684 * qemu-img resize would find useful, so we catch that behavior
3685 * here and tell the user. */
3686 if (new_size
!= total_size
&& new_size
== current_size
) {
3687 error_report("Image was not resized; resizing may not be supported "
3693 if (new_size
!= total_size
) {
3694 warn_report("Image should have been resized to %" PRIi64
3695 " bytes, but was resized to %" PRIi64
" bytes",
3696 total_size
, new_size
);
3699 qprintf(quiet
, "Image resized.\n");
3709 static void amend_status_cb(BlockDriverState
*bs
,
3710 int64_t offset
, int64_t total_work_size
,
3713 qemu_progress_print(100.f
* offset
/ total_work_size
, 0);
3716 static int print_amend_option_help(const char *format
)
3720 /* Find driver and parse its options */
3721 drv
= bdrv_find_format(format
);
3723 error_report("Unknown file format '%s'", format
);
3727 if (!drv
->bdrv_amend_options
) {
3728 error_report("Format driver '%s' does not support option amendment",
3733 /* Every driver supporting amendment must have create_opts */
3734 assert(drv
->create_opts
);
3736 printf("Creation options for '%s':\n", format
);
3737 qemu_opts_print_help(drv
->create_opts
);
3738 printf("\nNote that not all of these options may be amendable.\n");
3742 static int img_amend(int argc
, char **argv
)
3746 char *options
= NULL
;
3747 QemuOptsList
*create_opts
= NULL
;
3748 QemuOpts
*opts
= NULL
;
3749 const char *fmt
= NULL
, *filename
, *cache
;
3752 bool quiet
= false, progress
= false;
3753 BlockBackend
*blk
= NULL
;
3754 BlockDriverState
*bs
= NULL
;
3755 bool image_opts
= false;
3757 cache
= BDRV_DEFAULT_CACHE
;
3759 static const struct option long_options
[] = {
3760 {"help", no_argument
, 0, 'h'},
3761 {"object", required_argument
, 0, OPTION_OBJECT
},
3762 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
3765 c
= getopt_long(argc
, argv
, ":ho:f:t:pq",
3766 long_options
, NULL
);
3773 missing_argument(argv
[optind
- 1]);
3776 unrecognized_option(argv
[optind
- 1]);
3782 if (!is_valid_option_list(optarg
)) {
3783 error_report("Invalid option list: %s", optarg
);
3785 goto out_no_progress
;
3788 options
= g_strdup(optarg
);
3790 char *old_options
= options
;
3791 options
= g_strdup_printf("%s,%s", options
, optarg
);
3792 g_free(old_options
);
3808 opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
3812 goto out_no_progress
;
3815 case OPTION_IMAGE_OPTS
:
3822 error_exit("Must specify options (-o)");
3825 if (qemu_opts_foreach(&qemu_object_opts
,
3826 user_creatable_add_opts_foreach
,
3829 goto out_no_progress
;
3835 qemu_progress_init(progress
, 1.0);
3837 filename
= (optind
== argc
- 1) ? argv
[argc
- 1] : NULL
;
3838 if (fmt
&& has_help_option(options
)) {
3839 /* If a format is explicitly specified (and possibly no filename is
3840 * given), print option help here */
3841 ret
= print_amend_option_help(fmt
);
3845 if (optind
!= argc
- 1) {
3846 error_report("Expecting one image file name");
3851 flags
= BDRV_O_RDWR
;
3852 ret
= bdrv_parse_cache_mode(cache
, &flags
, &writethrough
);
3854 error_report("Invalid cache option: %s", cache
);
3858 blk
= img_open(image_opts
, filename
, fmt
, flags
, writethrough
, quiet
,
3866 fmt
= bs
->drv
->format_name
;
3868 if (has_help_option(options
)) {
3869 /* If the format was auto-detected, print option help here */
3870 ret
= print_amend_option_help(fmt
);
3874 if (!bs
->drv
->bdrv_amend_options
) {
3875 error_report("Format driver '%s' does not support option amendment",
3881 /* Every driver supporting amendment must have create_opts */
3882 assert(bs
->drv
->create_opts
);
3884 create_opts
= qemu_opts_append(create_opts
, bs
->drv
->create_opts
);
3885 opts
= qemu_opts_create(create_opts
, NULL
, 0, &error_abort
);
3886 qemu_opts_do_parse(opts
, options
, NULL
, &err
);
3888 error_report_err(err
);
3893 /* In case the driver does not call amend_status_cb() */
3894 qemu_progress_print(0.f
, 0);
3895 ret
= bdrv_amend_options(bs
, opts
, &amend_status_cb
, NULL
, &err
);
3896 qemu_progress_print(100.f
, 0);
3898 error_report_err(err
);
3903 qemu_progress_end();
3907 qemu_opts_del(opts
);
3908 qemu_opts_free(create_opts
);
3917 typedef struct BenchData
{
3919 uint64_t image_size
;
3926 bool drain_on_flush
;
3935 static void bench_undrained_flush_cb(void *opaque
, int ret
)
3938 error_report("Failed flush request: %s", strerror(-ret
));
3943 static void bench_cb(void *opaque
, int ret
)
3945 BenchData
*b
= opaque
;
3949 error_report("Failed request: %s", strerror(-ret
));
3954 /* Just finished a flush with drained queue: Start next requests */
3955 assert(b
->in_flight
== 0);
3956 b
->in_flush
= false;
3957 } else if (b
->in_flight
> 0) {
3958 int remaining
= b
->n
- b
->in_flight
;
3963 /* Time for flush? Drain queue if requested, then flush */
3964 if (b
->flush_interval
&& remaining
% b
->flush_interval
== 0) {
3965 if (!b
->in_flight
|| !b
->drain_on_flush
) {
3966 BlockCompletionFunc
*cb
;
3968 if (b
->drain_on_flush
) {
3972 cb
= bench_undrained_flush_cb
;
3975 acb
= blk_aio_flush(b
->blk
, cb
, b
);
3977 error_report("Failed to issue flush request");
3981 if (b
->drain_on_flush
) {
3987 while (b
->n
> b
->in_flight
&& b
->in_flight
< b
->nrreq
) {
3988 int64_t offset
= b
->offset
;
3989 /* blk_aio_* might look for completed I/Os and kick bench_cb
3990 * again, so make sure this operation is counted by in_flight
3991 * and b->offset is ready for the next submission.
3994 b
->offset
+= b
->step
;
3995 b
->offset
%= b
->image_size
;
3997 acb
= blk_aio_pwritev(b
->blk
, offset
, b
->qiov
, 0, bench_cb
, b
);
3999 acb
= blk_aio_preadv(b
->blk
, offset
, b
->qiov
, 0, bench_cb
, b
);
4002 error_report("Failed to issue request");
4008 static int img_bench(int argc
, char **argv
)
4011 const char *fmt
= NULL
, *filename
;
4013 bool image_opts
= false;
4014 bool is_write
= false;
4018 size_t bufsize
= 4096;
4021 int flush_interval
= 0;
4022 bool drain_on_flush
= true;
4024 BlockBackend
*blk
= NULL
;
4025 BenchData data
= {};
4027 bool writethrough
= false;
4028 struct timeval t1
, t2
;
4030 bool force_share
= false;
4034 static const struct option long_options
[] = {
4035 {"help", no_argument
, 0, 'h'},
4036 {"flush-interval", required_argument
, 0, OPTION_FLUSH_INTERVAL
},
4037 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
4038 {"pattern", required_argument
, 0, OPTION_PATTERN
},
4039 {"no-drain", no_argument
, 0, OPTION_NO_DRAIN
},
4040 {"force-share", no_argument
, 0, 'U'},
4043 c
= getopt_long(argc
, argv
, ":hc:d:f:no:qs:S:t:wU", long_options
, NULL
);
4050 missing_argument(argv
[optind
- 1]);
4053 unrecognized_option(argv
[optind
- 1]);
4062 if (qemu_strtoul(optarg
, NULL
, 0, &res
) < 0 || res
> INT_MAX
) {
4063 error_report("Invalid request count specified");
4073 if (qemu_strtoul(optarg
, NULL
, 0, &res
) < 0 || res
> INT_MAX
) {
4074 error_report("Invalid queue depth specified");
4084 flags
|= BDRV_O_NATIVE_AIO
;
4088 offset
= cvtnum(optarg
);
4090 error_report("Invalid offset specified");
4103 sval
= cvtnum(optarg
);
4104 if (sval
< 0 || sval
> INT_MAX
) {
4105 error_report("Invalid buffer size specified");
4116 sval
= cvtnum(optarg
);
4117 if (sval
< 0 || sval
> INT_MAX
) {
4118 error_report("Invalid step size specified");
4126 ret
= bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
);
4128 error_report("Invalid cache mode");
4134 flags
|= BDRV_O_RDWR
;
4140 case OPTION_PATTERN
:
4144 if (qemu_strtoul(optarg
, NULL
, 0, &res
) < 0 || res
> 0xff) {
4145 error_report("Invalid pattern byte specified");
4151 case OPTION_FLUSH_INTERVAL
:
4155 if (qemu_strtoul(optarg
, NULL
, 0, &res
) < 0 || res
> INT_MAX
) {
4156 error_report("Invalid flush interval specified");
4159 flush_interval
= res
;
4162 case OPTION_NO_DRAIN
:
4163 drain_on_flush
= false;
4165 case OPTION_IMAGE_OPTS
:
4171 if (optind
!= argc
- 1) {
4172 error_exit("Expecting one image file name");
4174 filename
= argv
[argc
- 1];
4176 if (!is_write
&& flush_interval
) {
4177 error_report("--flush-interval is only available in write tests");
4181 if (flush_interval
&& flush_interval
< depth
) {
4182 error_report("Flush interval can't be smaller than depth");
4187 blk
= img_open(image_opts
, filename
, fmt
, flags
, writethrough
, quiet
,
4194 image_size
= blk_getlength(blk
);
4195 if (image_size
< 0) {
4200 data
= (BenchData
) {
4202 .image_size
= image_size
,
4204 .step
= step
?: bufsize
,
4209 .flush_interval
= flush_interval
,
4210 .drain_on_flush
= drain_on_flush
,
4212 printf("Sending %d %s requests, %d bytes each, %d in parallel "
4213 "(starting at offset %" PRId64
", step size %d)\n",
4214 data
.n
, data
.write
? "write" : "read", data
.bufsize
, data
.nrreq
,
4215 data
.offset
, data
.step
);
4216 if (flush_interval
) {
4217 printf("Sending flush every %d requests\n", flush_interval
);
4220 buf_size
= data
.nrreq
* data
.bufsize
;
4221 data
.buf
= blk_blockalign(blk
, buf_size
);
4222 memset(data
.buf
, pattern
, data
.nrreq
* data
.bufsize
);
4224 blk_register_buf(blk
, data
.buf
, buf_size
);
4226 data
.qiov
= g_new(QEMUIOVector
, data
.nrreq
);
4227 for (i
= 0; i
< data
.nrreq
; i
++) {
4228 qemu_iovec_init(&data
.qiov
[i
], 1);
4229 qemu_iovec_add(&data
.qiov
[i
],
4230 data
.buf
+ i
* data
.bufsize
, data
.bufsize
);
4233 gettimeofday(&t1
, NULL
);
4236 while (data
.n
> 0) {
4237 main_loop_wait(false);
4239 gettimeofday(&t2
, NULL
);
4241 printf("Run completed in %3.3f seconds.\n",
4242 (t2
.tv_sec
- t1
.tv_sec
)
4243 + ((double)(t2
.tv_usec
- t1
.tv_usec
) / 1000000));
4247 blk_unregister_buf(blk
, data
.buf
);
4249 qemu_vfree(data
.buf
);
4270 int bsz
; /* Block size */
4278 int (*f
)(const char *, struct DdIo
*, struct DdIo
*, struct DdInfo
*);
4282 static int img_dd_bs(const char *arg
,
4283 struct DdIo
*in
, struct DdIo
*out
,
4290 if (res
<= 0 || res
> INT_MAX
) {
4291 error_report("invalid number: '%s'", arg
);
4294 in
->bsz
= out
->bsz
= res
;
4299 static int img_dd_count(const char *arg
,
4300 struct DdIo
*in
, struct DdIo
*out
,
4303 dd
->count
= cvtnum(arg
);
4305 if (dd
->count
< 0) {
4306 error_report("invalid number: '%s'", arg
);
4313 static int img_dd_if(const char *arg
,
4314 struct DdIo
*in
, struct DdIo
*out
,
4317 in
->filename
= g_strdup(arg
);
4322 static int img_dd_of(const char *arg
,
4323 struct DdIo
*in
, struct DdIo
*out
,
4326 out
->filename
= g_strdup(arg
);
4331 static int img_dd_skip(const char *arg
,
4332 struct DdIo
*in
, struct DdIo
*out
,
4335 in
->offset
= cvtnum(arg
);
4337 if (in
->offset
< 0) {
4338 error_report("invalid number: '%s'", arg
);
4345 static int img_dd(int argc
, char **argv
)
4350 BlockDriver
*drv
= NULL
, *proto_drv
= NULL
;
4351 BlockBackend
*blk1
= NULL
, *blk2
= NULL
;
4352 QemuOpts
*opts
= NULL
;
4353 QemuOptsList
*create_opts
= NULL
;
4354 Error
*local_err
= NULL
;
4355 bool image_opts
= false;
4357 const char *out_fmt
= "raw";
4358 const char *fmt
= NULL
;
4360 int64_t block_count
= 0, out_pos
, in_pos
;
4361 bool force_share
= false;
4362 struct DdInfo dd
= {
4367 .bsz
= 512, /* Block size is by default 512 bytes */
4379 const struct DdOpts options
[] = {
4380 { "bs", img_dd_bs
, C_BS
},
4381 { "count", img_dd_count
, C_COUNT
},
4382 { "if", img_dd_if
, C_IF
},
4383 { "of", img_dd_of
, C_OF
},
4384 { "skip", img_dd_skip
, C_SKIP
},
4387 const struct option long_options
[] = {
4388 { "help", no_argument
, 0, 'h'},
4389 { "object", required_argument
, 0, OPTION_OBJECT
},
4390 { "image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
4391 { "force-share", no_argument
, 0, 'U'},
4395 while ((c
= getopt_long(argc
, argv
, ":hf:O:U", long_options
, NULL
))) {
4407 missing_argument(argv
[optind
- 1]);
4410 unrecognized_option(argv
[optind
- 1]);
4419 if (!qemu_opts_parse_noisily(&qemu_object_opts
, optarg
, true)) {
4424 case OPTION_IMAGE_OPTS
:
4430 for (i
= optind
; i
< argc
; i
++) {
4432 arg
= g_strdup(argv
[i
]);
4434 tmp
= strchr(arg
, '=');
4436 error_report("unrecognized operand %s", arg
);
4443 for (j
= 0; options
[j
].name
!= NULL
; j
++) {
4444 if (!strcmp(arg
, options
[j
].name
)) {
4448 if (options
[j
].name
== NULL
) {
4449 error_report("unrecognized operand %s", arg
);
4454 if (options
[j
].f(tmp
, &in
, &out
, &dd
) != 0) {
4458 dd
.flags
|= options
[j
].flag
;
4463 if (!(dd
.flags
& C_IF
&& dd
.flags
& C_OF
)) {
4464 error_report("Must specify both input and output files");
4469 if (qemu_opts_foreach(&qemu_object_opts
,
4470 user_creatable_add_opts_foreach
,
4476 blk1
= img_open(image_opts
, in
.filename
, fmt
, 0, false, false,
4484 drv
= bdrv_find_format(out_fmt
);
4486 error_report("Unknown file format");
4490 proto_drv
= bdrv_find_protocol(out
.filename
, true, &local_err
);
4493 error_report_err(local_err
);
4497 if (!drv
->create_opts
) {
4498 error_report("Format driver '%s' does not support image creation",
4503 if (!proto_drv
->create_opts
) {
4504 error_report("Protocol driver '%s' does not support image creation",
4505 proto_drv
->format_name
);
4509 create_opts
= qemu_opts_append(create_opts
, drv
->create_opts
);
4510 create_opts
= qemu_opts_append(create_opts
, proto_drv
->create_opts
);
4512 opts
= qemu_opts_create(create_opts
, NULL
, 0, &error_abort
);
4514 size
= blk_getlength(blk1
);
4516 error_report("Failed to get size for '%s'", in
.filename
);
4521 if (dd
.flags
& C_COUNT
&& dd
.count
<= INT64_MAX
/ in
.bsz
&&
4522 dd
.count
* in
.bsz
< size
) {
4523 size
= dd
.count
* in
.bsz
;
4526 /* Overflow means the specified offset is beyond input image's size */
4527 if (dd
.flags
& C_SKIP
&& (in
.offset
> INT64_MAX
/ in
.bsz
||
4528 size
< in
.bsz
* in
.offset
)) {
4529 qemu_opt_set_number(opts
, BLOCK_OPT_SIZE
, 0, &error_abort
);
4531 qemu_opt_set_number(opts
, BLOCK_OPT_SIZE
,
4532 size
- in
.bsz
* in
.offset
, &error_abort
);
4535 ret
= bdrv_create(drv
, out
.filename
, opts
, &local_err
);
4537 error_reportf_err(local_err
,
4538 "%s: error while creating output image: ",
4544 /* TODO, we can't honour --image-opts for the target,
4545 * since it needs to be given in a format compatible
4546 * with the bdrv_create() call above which does not
4547 * support image-opts style.
4549 blk2
= img_open_file(out
.filename
, NULL
, out_fmt
, BDRV_O_RDWR
,
4550 false, false, false);
4557 if (dd
.flags
& C_SKIP
&& (in
.offset
> INT64_MAX
/ in
.bsz
||
4558 size
< in
.offset
* in
.bsz
)) {
4559 /* We give a warning if the skip option is bigger than the input
4560 * size and create an empty output disk image (i.e. like dd(1)).
4562 error_report("%s: cannot skip to specified offset", in
.filename
);
4565 in_pos
= in
.offset
* in
.bsz
;
4568 in
.buf
= g_new(uint8_t, in
.bsz
);
4570 for (out_pos
= 0; in_pos
< size
; block_count
++) {
4571 int in_ret
, out_ret
;
4573 if (in_pos
+ in
.bsz
> size
) {
4574 in_ret
= blk_pread(blk1
, in_pos
, in
.buf
, size
- in_pos
);
4576 in_ret
= blk_pread(blk1
, in_pos
, in
.buf
, in
.bsz
);
4579 error_report("error while reading from input image file: %s",
4586 out_ret
= blk_pwrite(blk2
, out_pos
, in
.buf
, in_ret
, 0);
4589 error_report("error while writing to output image file: %s",
4590 strerror(-out_ret
));
4599 qemu_opts_del(opts
);
4600 qemu_opts_free(create_opts
);
4603 g_free(in
.filename
);
4604 g_free(out
.filename
);
4614 static void dump_json_block_measure_info(BlockMeasureInfo
*info
)
4618 Visitor
*v
= qobject_output_visitor_new(&obj
);
4620 visit_type_BlockMeasureInfo(v
, NULL
, &info
, &error_abort
);
4621 visit_complete(v
, &obj
);
4622 str
= qobject_to_json_pretty(obj
);
4623 assert(str
!= NULL
);
4624 printf("%s\n", qstring_get_str(str
));
4630 static int img_measure(int argc
, char **argv
)
4632 static const struct option long_options
[] = {
4633 {"help", no_argument
, 0, 'h'},
4634 {"image-opts", no_argument
, 0, OPTION_IMAGE_OPTS
},
4635 {"object", required_argument
, 0, OPTION_OBJECT
},
4636 {"output", required_argument
, 0, OPTION_OUTPUT
},
4637 {"size", required_argument
, 0, OPTION_SIZE
},
4638 {"force-share", no_argument
, 0, 'U'},
4641 OutputFormat output_format
= OFORMAT_HUMAN
;
4642 BlockBackend
*in_blk
= NULL
;
4644 const char *filename
= NULL
;
4645 const char *fmt
= NULL
;
4646 const char *out_fmt
= "raw";
4647 char *options
= NULL
;
4648 char *snapshot_name
= NULL
;
4649 bool force_share
= false;
4650 QemuOpts
*opts
= NULL
;
4651 QemuOpts
*object_opts
= NULL
;
4652 QemuOpts
*sn_opts
= NULL
;
4653 QemuOptsList
*create_opts
= NULL
;
4654 bool image_opts
= false;
4655 uint64_t img_size
= UINT64_MAX
;
4656 BlockMeasureInfo
*info
= NULL
;
4657 Error
*local_err
= NULL
;
4661 while ((c
= getopt_long(argc
, argv
, "hf:O:o:l:U",
4662 long_options
, NULL
)) != -1) {
4675 if (!is_valid_option_list(optarg
)) {
4676 error_report("Invalid option list: %s", optarg
);
4680 options
= g_strdup(optarg
);
4682 char *old_options
= options
;
4683 options
= g_strdup_printf("%s,%s", options
, optarg
);
4684 g_free(old_options
);
4688 if (strstart(optarg
, SNAPSHOT_OPT_BASE
, NULL
)) {
4689 sn_opts
= qemu_opts_parse_noisily(&internal_snapshot_opts
,
4692 error_report("Failed in parsing snapshot param '%s'",
4697 snapshot_name
= optarg
;
4704 object_opts
= qemu_opts_parse_noisily(&qemu_object_opts
,
4710 case OPTION_IMAGE_OPTS
:
4714 if (!strcmp(optarg
, "json")) {
4715 output_format
= OFORMAT_JSON
;
4716 } else if (!strcmp(optarg
, "human")) {
4717 output_format
= OFORMAT_HUMAN
;
4719 error_report("--output must be used with human or json "
4728 sval
= cvtnum(optarg
);
4730 if (sval
== -ERANGE
) {
4731 error_report("Image size must be less than 8 EiB!");
4733 error_report("Invalid image size specified! You may use "
4734 "k, M, G, T, P or E suffixes for ");
4735 error_report("kilobytes, megabytes, gigabytes, terabytes, "
4736 "petabytes and exabytes.");
4740 img_size
= (uint64_t)sval
;
4746 if (qemu_opts_foreach(&qemu_object_opts
,
4747 user_creatable_add_opts_foreach
,
4752 if (argc
- optind
> 1) {
4753 error_report("At most one filename argument is allowed.");
4755 } else if (argc
- optind
== 1) {
4756 filename
= argv
[optind
];
4760 (object_opts
|| image_opts
|| fmt
|| snapshot_name
|| sn_opts
)) {
4761 error_report("--object, --image-opts, -f, and -l "
4762 "require a filename argument.");
4765 if (filename
&& img_size
!= UINT64_MAX
) {
4766 error_report("--size N cannot be used together with a filename.");
4769 if (!filename
&& img_size
== UINT64_MAX
) {
4770 error_report("Either --size N or one filename must be specified.");
4775 in_blk
= img_open(image_opts
, filename
, fmt
, 0,
4776 false, false, force_share
);
4782 bdrv_snapshot_load_tmp(blk_bs(in_blk
),
4783 qemu_opt_get(sn_opts
, SNAPSHOT_OPT_ID
),
4784 qemu_opt_get(sn_opts
, SNAPSHOT_OPT_NAME
),
4786 } else if (snapshot_name
!= NULL
) {
4787 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk
),
4788 snapshot_name
, &local_err
);
4791 error_reportf_err(local_err
, "Failed to load snapshot: ");
4796 drv
= bdrv_find_format(out_fmt
);
4798 error_report("Unknown file format '%s'", out_fmt
);
4801 if (!drv
->create_opts
) {
4802 error_report("Format driver '%s' does not support image creation",
4807 create_opts
= qemu_opts_append(create_opts
, drv
->create_opts
);
4808 create_opts
= qemu_opts_append(create_opts
, bdrv_file
.create_opts
);
4809 opts
= qemu_opts_create(create_opts
, NULL
, 0, &error_abort
);
4811 qemu_opts_do_parse(opts
, options
, NULL
, &local_err
);
4813 error_report_err(local_err
);
4814 error_report("Invalid options for file format '%s'", out_fmt
);
4818 if (img_size
!= UINT64_MAX
) {
4819 qemu_opt_set_number(opts
, BLOCK_OPT_SIZE
, img_size
, &error_abort
);
4822 info
= bdrv_measure(drv
, opts
, in_blk
? blk_bs(in_blk
) : NULL
, &local_err
);
4824 error_report_err(local_err
);
4828 if (output_format
== OFORMAT_HUMAN
) {
4829 printf("required size: %" PRIu64
"\n", info
->required
);
4830 printf("fully allocated size: %" PRIu64
"\n", info
->fully_allocated
);
4832 dump_json_block_measure_info(info
);
4838 qapi_free_BlockMeasureInfo(info
);
4839 qemu_opts_del(object_opts
);
4840 qemu_opts_del(opts
);
4841 qemu_opts_del(sn_opts
);
4842 qemu_opts_free(create_opts
);
4848 static const img_cmd_t img_cmds
[] = {
4849 #define DEF(option, callback, arg_string) \
4850 { option, callback },
4851 #include "qemu-img-cmds.h"
4856 int main(int argc
, char **argv
)
4858 const img_cmd_t
*cmd
;
4859 const char *cmdname
;
4860 Error
*local_error
= NULL
;
4861 char *trace_file
= NULL
;
4863 static const struct option long_options
[] = {
4864 {"help", no_argument
, 0, 'h'},
4865 {"version", no_argument
, 0, 'V'},
4866 {"trace", required_argument
, NULL
, 'T'},
4871 signal(SIGPIPE
, SIG_IGN
);
4874 module_call_init(MODULE_INIT_TRACE
);
4875 error_set_progname(argv
[0]);
4876 qemu_init_exec_dir(argv
[0]);
4878 if (qemu_init_main_loop(&local_error
)) {
4879 error_report_err(local_error
);
4883 qcrypto_init(&error_fatal
);
4885 module_call_init(MODULE_INIT_QOM
);
4888 error_exit("Not enough arguments");
4891 qemu_add_opts(&qemu_object_opts
);
4892 qemu_add_opts(&qemu_source_opts
);
4893 qemu_add_opts(&qemu_trace_opts
);
4895 while ((c
= getopt_long(argc
, argv
, "+:hVT:", long_options
, NULL
)) != -1) {
4898 missing_argument(argv
[optind
- 1]);
4901 unrecognized_option(argv
[optind
- 1]);
4907 printf(QEMU_IMG_VERSION
);
4911 trace_file
= trace_opt_parse(optarg
);
4916 cmdname
= argv
[optind
];
4918 /* reset getopt_long scanning */
4926 if (!trace_init_backends()) {
4929 trace_init_file(trace_file
);
4930 qemu_set_log(LOG_TRACE
);
4932 /* find the command */
4933 for (cmd
= img_cmds
; cmd
->name
!= NULL
; cmd
++) {
4934 if (!strcmp(cmdname
, cmd
->name
)) {
4935 return cmd
->handler(argc
, argv
);
4940 error_exit("Command not found: %s", cmdname
);