]> git.proxmox.com Git - mirror_qemu.git/blame - qemu-img.c
qsp: Simplify how qsp_report() prints
[mirror_qemu.git] / qemu-img.c
CommitLineData
ea2384d3 1/*
fb43f4dd 2 * QEMU disk image utility
5fafdf24 3 *
68d0f70e 4 * Copyright (c) 2003-2008 Fabrice Bellard
5fafdf24 5 *
ea2384d3
FB
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
23 */
452fcdbc 24
80c71a24 25#include "qemu/osdep.h"
c2a3d7da
EB
26#include <getopt.h>
27
67a1de0d 28#include "qemu-version.h"
da34e65c 29#include "qapi/error.h"
9af23989 30#include "qapi/qapi-visit-block-core.h"
b3db211f 31#include "qapi/qobject-output-visitor.h"
7b1b5d19 32#include "qapi/qmp/qjson.h"
452fcdbc 33#include "qapi/qmp/qdict.h"
fc81fa1e 34#include "qapi/qmp/qstring.h"
f348b6d1 35#include "qemu/cutils.h"
3babeb15 36#include "qemu/config-file.h"
1de7afc9
PB
37#include "qemu/option.h"
38#include "qemu/error-report.h"
06a1e0c1 39#include "qemu/log.h"
3babeb15 40#include "qom/object_interfaces.h"
9c17d615 41#include "sysemu/sysemu.h"
26f54e9a 42#include "sysemu/block-backend.h"
737e150e 43#include "block/block_int.h"
d4a3238a 44#include "block/blockjob.h"
f364ec65 45#include "block/qapi.h"
c2297088 46#include "crypto/init.h"
06a1e0c1 47#include "trace/control.h"
e8445331 48
7e563bfb 49#define QEMU_IMG_VERSION "qemu-img version " QEMU_FULL_VERSION \
0781dd6e 50 "\n" QEMU_COPYRIGHT "\n"
5f6979cb 51
c227f099 52typedef struct img_cmd_t {
153859be
SB
53 const char *name;
54 int (*handler)(int argc, char **argv);
c227f099 55} img_cmd_t;
153859be 56
8599ea4c
FS
57enum {
58 OPTION_OUTPUT = 256,
59 OPTION_BACKING_CHAIN = 257,
3babeb15 60 OPTION_OBJECT = 258,
eb769f74 61 OPTION_IMAGE_OPTS = 259,
b6495fa8 62 OPTION_PATTERN = 260,
55d539c8
KW
63 OPTION_FLUSH_INTERVAL = 261,
64 OPTION_NO_DRAIN = 262,
305b4c60 65 OPTION_TARGET_IMAGE_OPTS = 263,
fd03c2b8 66 OPTION_SIZE = 264,
dc5f690b 67 OPTION_PREALLOCATION = 265,
4ffca890 68 OPTION_SHRINK = 266,
8599ea4c
FS
69};
70
71typedef enum OutputFormat {
72 OFORMAT_JSON,
73 OFORMAT_HUMAN,
74} OutputFormat;
75
e6996143 76/* Default to cache=writeback as data integrity is not important for qemu-img */
661a0f71 77#define BDRV_DEFAULT_CACHE "writeback"
137519ce 78
00c6d403 79static void format_print(void *opaque, const char *name)
ea2384d3 80{
00c6d403 81 printf(" %s", name);
ea2384d3
FB
82}
83
ac1307ab
FZ
84static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
85{
86 va_list ap;
87
ac1307ab 88 va_start(ap, fmt);
e9e1d92d 89 error_vreport(fmt, ap);
ac1307ab
FZ
90 va_end(ap);
91
e9e1d92d 92 error_printf("Try 'qemu-img --help' for more information\n");
ac1307ab
FZ
93 exit(EXIT_FAILURE);
94}
95
c9192973
SH
96static void QEMU_NORETURN missing_argument(const char *option)
97{
98 error_exit("missing argument for option '%s'", option);
99}
100
101static void QEMU_NORETURN unrecognized_option(const char *option)
102{
103 error_exit("unrecognized option '%s'", option);
104}
105
d2c639d6 106/* Please keep in synch with qemu-img.texi */
ac1307ab 107static void QEMU_NORETURN help(void)
ea2384d3 108{
e00291c0 109 const char *help_msg =
5f6979cb 110 QEMU_IMG_VERSION
10985131 111 "usage: qemu-img [standard options] command [command options]\n"
3f020d70 112 "QEMU disk image utility\n"
113 "\n"
10985131
DL
114 " '-h', '--help' display this help and exit\n"
115 " '-V', '--version' output version information and exit\n"
06a1e0c1
DL
116 " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
117 " specify tracing options\n"
10985131 118 "\n"
3f020d70 119 "Command syntax:\n"
153859be
SB
120#define DEF(option, callback, arg_string) \
121 " " arg_string "\n"
122#include "qemu-img-cmds.h"
123#undef DEF
3f020d70 124 "\n"
125 "Command parameters:\n"
126 " 'filename' is a disk image filename\n"
3babeb15
DB
127 " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
128 " manual page for a description of the object properties. The most common\n"
129 " object type is a 'secret', which is used to supply passwords and/or\n"
130 " encryption keys.\n"
3f020d70 131 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
661a0f71 132 " 'cache' is the cache mode used to write the output disk image, the valid\n"
80ccf93b
LY
133 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
134 " 'directsync' and 'unsafe' (default for convert)\n"
bb87fdf8
SH
135 " 'src_cache' is the cache mode used to read input disk images, the valid\n"
136 " options are the same as for the 'cache' option\n"
3f020d70 137 " 'size' is the disk image size in bytes. Optional suffixes\n"
5e00984a
KW
138 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
139 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
140 " supported. 'b' is ignored.\n"
3f020d70 141 " 'output_filename' is the destination disk image filename\n"
142 " 'output_fmt' is the destination format\n"
143 " 'options' is a comma separated list of format specific options in a\n"
144 " name=value format. Use -o ? for an overview of the options supported by the\n"
145 " used format\n"
ef80654d
WX
146 " 'snapshot_param' is param used for internal snapshot, format\n"
147 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
148 " '[ID_OR_NAME]'\n"
3f020d70 149 " '-c' indicates that target image must be compressed (qcow format only)\n"
6e6e55f5
JS
150 " '-u' allows unsafe backing chains. For rebasing, it is assumed that old and\n"
151 " new backing file match exactly. The image doesn't need a working\n"
152 " backing file before rebasing in this case (useful for renaming the\n"
153 " backing file). For image creation, allow creating without attempting\n"
154 " to open the backing file.\n"
3f020d70 155 " '-h' with or without a command shows this help and lists the supported formats\n"
6b837bc4 156 " '-p' show progress of command (only certain commands)\n"
f382d43a 157 " '-q' use Quiet mode - do not print any output (except errors)\n"
11b6699a
PL
158 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
159 " contain only zeros for qemu-img to create a sparse image during\n"
160 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
161 " unallocated or zero sectors, and the destination image will always be\n"
162 " fully allocated\n"
c054b3fd 163 " '--output' takes the format in which the output must be done (human or json)\n"
b2e10493
AD
164 " '-n' skips the target volume creation (useful if the volume is created\n"
165 " prior to running qemu-img)\n"
3f020d70 166 "\n"
4534ff54
KW
167 "Parameters to check subcommand:\n"
168 " '-r' tries to repair any inconsistencies that are found during the check.\n"
169 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
170 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
0546b8c2 171 " hiding corruption that has already occurred.\n"
4534ff54 172 "\n"
2d9187bc
PL
173 "Parameters to convert subcommand:\n"
174 " '-m' specifies how many coroutines work in parallel during the convert\n"
175 " process (defaults to 8)\n"
176 " '-W' allow to write to the target out of order rather than sequential\n"
177 "\n"
3f020d70 178 "Parameters to snapshot subcommand:\n"
179 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
180 " '-a' applies a snapshot (revert disk to saved state)\n"
181 " '-c' creates a snapshot\n"
182 " '-d' deletes a snapshot\n"
d14ed18c
MR
183 " '-l' lists all snapshots in the given image\n"
184 "\n"
185 "Parameters to compare subcommand:\n"
186 " '-f' first image format\n"
187 " '-F' second image format\n"
86ce1f6e
RS
188 " '-s' run in Strict mode - fail on different image size or sector allocation\n"
189 "\n"
190 "Parameters to dd subcommand:\n"
191 " 'bs=BYTES' read and write up to BYTES bytes at a time "
192 "(default: 512)\n"
193 " 'count=N' copy only N input blocks\n"
194 " 'if=FILE' read from FILE\n"
f7c15533
RS
195 " 'of=FILE' write to FILE\n"
196 " 'skip=N' skip N bs-sized blocks at the start of input\n";
e00291c0
PB
197
198 printf("%s\nSupported formats:", help_msg);
9ac404c5 199 bdrv_iterate_format(format_print, NULL, false);
f5048cb7 200 printf("\n\n" QEMU_HELP_BOTTOM "\n");
ac1307ab 201 exit(EXIT_SUCCESS);
ea2384d3
FB
202}
203
3babeb15
DB
204static QemuOptsList qemu_object_opts = {
205 .name = "object",
206 .implied_opt_name = "qom-type",
207 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
208 .desc = {
209 { }
210 },
211};
212
eb769f74
DB
213static QemuOptsList qemu_source_opts = {
214 .name = "source",
215 .implied_opt_name = "file",
216 .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head),
217 .desc = {
218 { }
219 },
220};
221
7c30f657 222static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
f382d43a
MR
223{
224 int ret = 0;
225 if (!quiet) {
226 va_list args;
227 va_start(args, fmt);
228 ret = vprintf(fmt, args);
229 va_end(args);
230 }
231 return ret;
232}
233
ea2384d3 234
4ac8aacd
JS
235static int print_block_option_help(const char *filename, const char *fmt)
236{
237 BlockDriver *drv, *proto_drv;
83d0521a 238 QemuOptsList *create_opts = NULL;
b65a5e12 239 Error *local_err = NULL;
4ac8aacd
JS
240
241 /* Find driver and parse its options */
242 drv = bdrv_find_format(fmt);
243 if (!drv) {
15654a6d 244 error_report("Unknown file format '%s'", fmt);
4ac8aacd
JS
245 return 1;
246 }
247
d402b6a2
HR
248 if (!drv->create_opts) {
249 error_report("Format driver '%s' does not support image creation", fmt);
250 return 1;
251 }
252
c282e1fd 253 create_opts = qemu_opts_append(create_opts, drv->create_opts);
a283cb6e 254 if (filename) {
b65a5e12 255 proto_drv = bdrv_find_protocol(filename, true, &local_err);
a283cb6e 256 if (!proto_drv) {
2867ce4a 257 error_report_err(local_err);
83d0521a 258 qemu_opts_free(create_opts);
a283cb6e
KW
259 return 1;
260 }
d402b6a2 261 if (!proto_drv->create_opts) {
f0998879 262 error_report("Protocol driver '%s' does not support image creation",
d402b6a2 263 proto_drv->format_name);
3ecd5a4f 264 qemu_opts_free(create_opts);
d402b6a2
HR
265 return 1;
266 }
c282e1fd 267 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
a283cb6e
KW
268 }
269
7f3fb001 270 printf("Supported options:\n");
63898712 271 qemu_opts_print_help(create_opts, false);
83d0521a 272 qemu_opts_free(create_opts);
4ac8aacd
JS
273 return 0;
274}
275
eb769f74 276
efaa7c4e 277static BlockBackend *img_open_opts(const char *optstr,
ce099547 278 QemuOpts *opts, int flags, bool writethrough,
335e9937 279 bool quiet, bool force_share)
eb769f74
DB
280{
281 QDict *options;
282 Error *local_err = NULL;
283 BlockBackend *blk;
284 options = qemu_opts_to_qdict(opts, NULL);
335e9937
FZ
285 if (force_share) {
286 if (qdict_haskey(options, BDRV_OPT_FORCE_SHARE)
4615f878 287 && strcmp(qdict_get_str(options, BDRV_OPT_FORCE_SHARE), "on")) {
335e9937 288 error_report("--force-share/-U conflicts with image options");
cb3e7f08 289 qobject_unref(options);
335e9937
FZ
290 return NULL;
291 }
4615f878 292 qdict_put_str(options, BDRV_OPT_FORCE_SHARE, "on");
335e9937 293 }
efaa7c4e 294 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
eb769f74 295 if (!blk) {
143605a2 296 error_reportf_err(local_err, "Could not open '%s': ", optstr);
eb769f74
DB
297 return NULL;
298 }
ce099547 299 blk_set_enable_write_cache(blk, !writethrough);
eb769f74 300
eb769f74
DB
301 return blk;
302}
303
efaa7c4e 304static BlockBackend *img_open_file(const char *filename,
29cf9336 305 QDict *options,
eb769f74 306 const char *fmt, int flags,
335e9937
FZ
307 bool writethrough, bool quiet,
308 bool force_share)
eb769f74
DB
309{
310 BlockBackend *blk;
34b5d2c6 311 Error *local_err = NULL;
ad717139 312
29cf9336
DB
313 if (!options) {
314 options = qdict_new();
315 }
75c23805 316 if (fmt) {
46f5ac20 317 qdict_put_str(options, "driver", fmt);
75c23805 318 }
b9eaf9ec 319
335e9937 320 if (force_share) {
579cf1d1 321 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
335e9937 322 }
efaa7c4e 323 blk = blk_new_open(filename, NULL, options, flags, &local_err);
5bd31326 324 if (!blk) {
c29b77f9 325 error_reportf_err(local_err, "Could not open '%s': ", filename);
eb769f74 326 return NULL;
75c23805 327 }
ce099547 328 blk_set_enable_write_cache(blk, !writethrough);
b9eaf9ec 329
eb769f74
DB
330 return blk;
331}
332
333
29cf9336
DB
334static int img_add_key_secrets(void *opaque,
335 const char *name, const char *value,
336 Error **errp)
337{
338 QDict *options = opaque;
339
340 if (g_str_has_suffix(name, "key-secret")) {
187f47e9 341 qdict_put_str(options, name, value);
29cf9336
DB
342 }
343
344 return 0;
345}
346
29cf9336 347
efaa7c4e 348static BlockBackend *img_open(bool image_opts,
eb769f74 349 const char *filename,
ce099547 350 const char *fmt, int flags, bool writethrough,
335e9937 351 bool quiet, bool force_share)
eb769f74
DB
352{
353 BlockBackend *blk;
354 if (image_opts) {
355 QemuOpts *opts;
356 if (fmt) {
357 error_report("--image-opts and --format are mutually exclusive");
358 return NULL;
359 }
360 opts = qemu_opts_parse_noisily(qemu_find_opts("source"),
361 filename, true);
362 if (!opts) {
363 return NULL;
364 }
335e9937
FZ
365 blk = img_open_opts(filename, opts, flags, writethrough, quiet,
366 force_share);
eb769f74 367 } else {
29cf9336 368 blk = img_open_file(filename, NULL, fmt, flags, writethrough, quiet,
335e9937 369 force_share);
75c23805 370 }
7e7d56d9 371 return blk;
75c23805
FB
372}
373
eb769f74 374
83d0521a 375static int add_old_style_options(const char *fmt, QemuOpts *opts,
eec77d9e
JS
376 const char *base_filename,
377 const char *base_fmt)
efa84d43 378{
6750e795
MA
379 Error *err = NULL;
380
efa84d43 381 if (base_filename) {
f43e47db 382 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err);
6750e795 383 if (err) {
15654a6d
JS
384 error_report("Backing file not supported for file format '%s'",
385 fmt);
6750e795 386 error_free(err);
c2abccec 387 return -1;
efa84d43
KW
388 }
389 }
390 if (base_fmt) {
f43e47db 391 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err);
6750e795 392 if (err) {
15654a6d
JS
393 error_report("Backing file format not supported for file "
394 "format '%s'", fmt);
6750e795 395 error_free(err);
c2abccec 396 return -1;
efa84d43
KW
397 }
398 }
c2abccec 399 return 0;
efa84d43
KW
400}
401
606caa0a
MA
402static int64_t cvtnum(const char *s)
403{
f17fd4fd 404 int err;
f46bfdbf 405 uint64_t value;
606caa0a 406
f17fd4fd
MA
407 err = qemu_strtosz(s, NULL, &value);
408 if (err < 0) {
409 return err;
410 }
f46bfdbf
MA
411 if (value > INT64_MAX) {
412 return -ERANGE;
413 }
f17fd4fd 414 return value;
606caa0a
MA
415}
416
ea2384d3
FB
417static int img_create(int argc, char **argv)
418{
a9300911 419 int c;
1da7cfbd 420 uint64_t img_size = -1;
ea2384d3 421 const char *fmt = "raw";
9230eaf6 422 const char *base_fmt = NULL;
ea2384d3
FB
423 const char *filename;
424 const char *base_filename = NULL;
9ea2ea71 425 char *options = NULL;
9b37525a 426 Error *local_err = NULL;
f382d43a 427 bool quiet = false;
6e6e55f5 428 int flags = 0;
3b46e624 429
ea2384d3 430 for(;;) {
3babeb15
DB
431 static const struct option long_options[] = {
432 {"help", no_argument, 0, 'h'},
433 {"object", required_argument, 0, OPTION_OBJECT},
434 {0, 0, 0, 0}
435 };
6e6e55f5 436 c = getopt_long(argc, argv, ":F:b:f:ho:qu",
3babeb15 437 long_options, NULL);
b8fb60da 438 if (c == -1) {
ea2384d3 439 break;
b8fb60da 440 }
ea2384d3 441 switch(c) {
c9192973
SH
442 case ':':
443 missing_argument(argv[optind - 1]);
444 break;
ef87394c 445 case '?':
c9192973
SH
446 unrecognized_option(argv[optind - 1]);
447 break;
ea2384d3
FB
448 case 'h':
449 help();
450 break;
9230eaf6
AL
451 case 'F':
452 base_fmt = optarg;
453 break;
ea2384d3
FB
454 case 'b':
455 base_filename = optarg;
456 break;
457 case 'f':
458 fmt = optarg;
459 break;
9ea2ea71 460 case 'o':
77386bf6
KW
461 if (!is_valid_option_list(optarg)) {
462 error_report("Invalid option list: %s", optarg);
463 goto fail;
464 }
465 if (!options) {
466 options = g_strdup(optarg);
467 } else {
468 char *old_options = options;
469 options = g_strdup_printf("%s,%s", options, optarg);
470 g_free(old_options);
471 }
9ea2ea71 472 break;
f382d43a
MR
473 case 'q':
474 quiet = true;
475 break;
6e6e55f5
JS
476 case 'u':
477 flags |= BDRV_O_NO_BACKING;
478 break;
3babeb15
DB
479 case OPTION_OBJECT: {
480 QemuOpts *opts;
481 opts = qemu_opts_parse_noisily(&qemu_object_opts,
482 optarg, true);
483 if (!opts) {
484 goto fail;
485 }
486 } break;
ea2384d3
FB
487 }
488 }
9230eaf6 489
b50cbabc 490 /* Get the filename */
a283cb6e
KW
491 filename = (optind < argc) ? argv[optind] : NULL;
492 if (options && has_help_option(options)) {
493 g_free(options);
494 return print_block_option_help(filename, fmt);
495 }
496
b8fb60da 497 if (optind >= argc) {
ac1307ab 498 error_exit("Expecting image file name");
b8fb60da 499 }
a283cb6e 500 optind++;
b50cbabc 501
3babeb15
DB
502 if (qemu_opts_foreach(&qemu_object_opts,
503 user_creatable_add_opts_foreach,
334c43e2 504 NULL, &error_fatal)) {
3babeb15
DB
505 goto fail;
506 }
507
1da7cfbd
JS
508 /* Get image size, if specified */
509 if (optind < argc) {
70b4f4bb 510 int64_t sval;
606caa0a
MA
511
512 sval = cvtnum(argv[optind++]);
513 if (sval < 0) {
79443397
LG
514 if (sval == -ERANGE) {
515 error_report("Image size must be less than 8 EiB!");
516 } else {
517 error_report("Invalid image size specified! You may use k, M, "
5e00984a
KW
518 "G, T, P or E suffixes for ");
519 error_report("kilobytes, megabytes, gigabytes, terabytes, "
520 "petabytes and exabytes.");
79443397 521 }
77386bf6 522 goto fail;
1da7cfbd
JS
523 }
524 img_size = (uint64_t)sval;
525 }
fc11eb26 526 if (optind != argc) {
ac1307ab 527 error_exit("Unexpected argument: %s", argv[optind]);
fc11eb26 528 }
1da7cfbd 529
9b37525a 530 bdrv_img_create(filename, fmt, base_filename, base_fmt,
6e6e55f5 531 options, img_size, flags, quiet, &local_err);
84d18f06 532 if (local_err) {
c29b77f9 533 error_reportf_err(local_err, "%s: ", filename);
77386bf6 534 goto fail;
c2abccec 535 }
a9300911 536
77386bf6 537 g_free(options);
ea2384d3 538 return 0;
77386bf6
KW
539
540fail:
541 g_free(options);
542 return 1;
ea2384d3
FB
543}
544
f382d43a 545static void dump_json_image_check(ImageCheck *check, bool quiet)
8599ea4c 546{
8599ea4c 547 QString *str;
8599ea4c 548 QObject *obj;
7d5e199a 549 Visitor *v = qobject_output_visitor_new(&obj);
3b098d56
EB
550
551 visit_type_ImageCheck(v, NULL, &check, &error_abort);
552 visit_complete(v, &obj);
8599ea4c
FS
553 str = qobject_to_json_pretty(obj);
554 assert(str != NULL);
f382d43a 555 qprintf(quiet, "%s\n", qstring_get_str(str));
cb3e7f08 556 qobject_unref(obj);
3b098d56 557 visit_free(v);
cb3e7f08 558 qobject_unref(str);
8599ea4c
FS
559}
560
f382d43a 561static void dump_human_image_check(ImageCheck *check, bool quiet)
8599ea4c
FS
562{
563 if (!(check->corruptions || check->leaks || check->check_errors)) {
f382d43a 564 qprintf(quiet, "No errors were found on the image.\n");
8599ea4c
FS
565 } else {
566 if (check->corruptions) {
f382d43a
MR
567 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
568 "Data may be corrupted, or further writes to the image "
569 "may corrupt it.\n",
570 check->corruptions);
8599ea4c
FS
571 }
572
573 if (check->leaks) {
f382d43a
MR
574 qprintf(quiet,
575 "\n%" PRId64 " leaked clusters were found on the image.\n"
576 "This means waste of disk space, but no harm to data.\n",
577 check->leaks);
8599ea4c
FS
578 }
579
580 if (check->check_errors) {
f382d43a
MR
581 qprintf(quiet,
582 "\n%" PRId64
583 " internal errors have occurred during the check.\n",
584 check->check_errors);
8599ea4c
FS
585 }
586 }
587
588 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
f382d43a
MR
589 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
590 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
591 check->allocated_clusters, check->total_clusters,
592 check->allocated_clusters * 100.0 / check->total_clusters,
593 check->fragmented_clusters * 100.0 / check->allocated_clusters,
594 check->compressed_clusters * 100.0 /
595 check->allocated_clusters);
8599ea4c
FS
596 }
597
598 if (check->image_end_offset) {
f382d43a
MR
599 qprintf(quiet,
600 "Image end offset: %" PRId64 "\n", check->image_end_offset);
8599ea4c
FS
601 }
602}
603
604static int collect_image_check(BlockDriverState *bs,
605 ImageCheck *check,
606 const char *filename,
607 const char *fmt,
608 int fix)
609{
610 int ret;
611 BdrvCheckResult result;
612
613 ret = bdrv_check(bs, &result, fix);
614 if (ret < 0) {
615 return ret;
616 }
617
618 check->filename = g_strdup(filename);
619 check->format = g_strdup(bdrv_get_format_name(bs));
620 check->check_errors = result.check_errors;
621 check->corruptions = result.corruptions;
622 check->has_corruptions = result.corruptions != 0;
623 check->leaks = result.leaks;
624 check->has_leaks = result.leaks != 0;
625 check->corruptions_fixed = result.corruptions_fixed;
626 check->has_corruptions_fixed = result.corruptions != 0;
627 check->leaks_fixed = result.leaks_fixed;
628 check->has_leaks_fixed = result.leaks != 0;
629 check->image_end_offset = result.image_end_offset;
630 check->has_image_end_offset = result.image_end_offset != 0;
631 check->total_clusters = result.bfi.total_clusters;
632 check->has_total_clusters = result.bfi.total_clusters != 0;
633 check->allocated_clusters = result.bfi.allocated_clusters;
634 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
635 check->fragmented_clusters = result.bfi.fragmented_clusters;
636 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
e6439d78
SH
637 check->compressed_clusters = result.bfi.compressed_clusters;
638 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
8599ea4c
FS
639
640 return 0;
641}
642
e076f338
KW
643/*
644 * Checks an image for consistency. Exit codes:
645 *
d6635c4d
HR
646 * 0 - Check completed, image is good
647 * 1 - Check not completed because of internal errors
648 * 2 - Check completed, image is corrupted
649 * 3 - Check completed, image has leaked clusters, but is good otherwise
650 * 63 - Checks are not supported by the image format
e076f338 651 */
1585969c
AL
652static int img_check(int argc, char **argv)
653{
654 int c, ret;
8599ea4c 655 OutputFormat output_format = OFORMAT_HUMAN;
40055951 656 const char *filename, *fmt, *output, *cache;
26f54e9a 657 BlockBackend *blk;
1585969c 658 BlockDriverState *bs;
4534ff54 659 int fix = 0;
ce099547
KW
660 int flags = BDRV_O_CHECK;
661 bool writethrough;
7e7d56d9 662 ImageCheck *check;
f382d43a 663 bool quiet = false;
eb769f74 664 bool image_opts = false;
335e9937 665 bool force_share = false;
1585969c
AL
666
667 fmt = NULL;
8599ea4c 668 output = NULL;
40055951 669 cache = BDRV_DEFAULT_CACHE;
ce099547 670
1585969c 671 for(;;) {
8599ea4c
FS
672 int option_index = 0;
673 static const struct option long_options[] = {
674 {"help", no_argument, 0, 'h'},
675 {"format", required_argument, 0, 'f'},
4fd6a984 676 {"repair", required_argument, 0, 'r'},
8599ea4c 677 {"output", required_argument, 0, OPTION_OUTPUT},
3babeb15 678 {"object", required_argument, 0, OPTION_OBJECT},
eb769f74 679 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
335e9937 680 {"force-share", no_argument, 0, 'U'},
8599ea4c
FS
681 {0, 0, 0, 0}
682 };
335e9937 683 c = getopt_long(argc, argv, ":hf:r:T:qU",
8599ea4c 684 long_options, &option_index);
b8fb60da 685 if (c == -1) {
1585969c 686 break;
b8fb60da 687 }
1585969c 688 switch(c) {
c9192973
SH
689 case ':':
690 missing_argument(argv[optind - 1]);
691 break;
ef87394c 692 case '?':
c9192973
SH
693 unrecognized_option(argv[optind - 1]);
694 break;
1585969c
AL
695 case 'h':
696 help();
697 break;
698 case 'f':
699 fmt = optarg;
700 break;
4534ff54
KW
701 case 'r':
702 flags |= BDRV_O_RDWR;
703
704 if (!strcmp(optarg, "leaks")) {
705 fix = BDRV_FIX_LEAKS;
706 } else if (!strcmp(optarg, "all")) {
707 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
708 } else {
ac1307ab
FZ
709 error_exit("Unknown option value for -r "
710 "(expecting 'leaks' or 'all'): %s", optarg);
4534ff54
KW
711 }
712 break;
8599ea4c
FS
713 case OPTION_OUTPUT:
714 output = optarg;
715 break;
40055951
HR
716 case 'T':
717 cache = optarg;
718 break;
f382d43a
MR
719 case 'q':
720 quiet = true;
721 break;
335e9937
FZ
722 case 'U':
723 force_share = true;
724 break;
3babeb15
DB
725 case OPTION_OBJECT: {
726 QemuOpts *opts;
727 opts = qemu_opts_parse_noisily(&qemu_object_opts,
728 optarg, true);
729 if (!opts) {
730 return 1;
731 }
732 } break;
eb769f74
DB
733 case OPTION_IMAGE_OPTS:
734 image_opts = true;
735 break;
1585969c
AL
736 }
737 }
fc11eb26 738 if (optind != argc - 1) {
ac1307ab 739 error_exit("Expecting one image file name");
b8fb60da 740 }
1585969c
AL
741 filename = argv[optind++];
742
8599ea4c
FS
743 if (output && !strcmp(output, "json")) {
744 output_format = OFORMAT_JSON;
745 } else if (output && !strcmp(output, "human")) {
746 output_format = OFORMAT_HUMAN;
747 } else if (output) {
748 error_report("--output must be used with human or json as argument.");
749 return 1;
750 }
751
3babeb15
DB
752 if (qemu_opts_foreach(&qemu_object_opts,
753 user_creatable_add_opts_foreach,
334c43e2 754 NULL, &error_fatal)) {
3babeb15
DB
755 return 1;
756 }
757
ce099547 758 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
40055951
HR
759 if (ret < 0) {
760 error_report("Invalid source cache option: %s", cache);
761 return 1;
762 }
763
335e9937
FZ
764 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
765 force_share);
7e7d56d9
MA
766 if (!blk) {
767 return 1;
c2abccec 768 }
7e7d56d9 769 bs = blk_bs(blk);
8599ea4c
FS
770
771 check = g_new0(ImageCheck, 1);
772 ret = collect_image_check(bs, check, filename, fmt, fix);
e076f338
KW
773
774 if (ret == -ENOTSUP) {
55d492d7 775 error_report("This image format does not support checks");
fefddf95 776 ret = 63;
8599ea4c 777 goto fail;
e076f338
KW
778 }
779
8599ea4c
FS
780 if (check->corruptions_fixed || check->leaks_fixed) {
781 int corruptions_fixed, leaks_fixed;
ccf34716 782
8599ea4c
FS
783 leaks_fixed = check->leaks_fixed;
784 corruptions_fixed = check->corruptions_fixed;
e076f338 785
8599ea4c 786 if (output_format == OFORMAT_HUMAN) {
f382d43a
MR
787 qprintf(quiet,
788 "The following inconsistencies were found and repaired:\n\n"
789 " %" PRId64 " leaked clusters\n"
790 " %" PRId64 " corruptions\n\n"
791 "Double checking the fixed image now...\n",
792 check->leaks_fixed,
793 check->corruptions_fixed);
e076f338
KW
794 }
795
8599ea4c 796 ret = collect_image_check(bs, check, filename, fmt, 0);
1585969c 797
8599ea4c
FS
798 check->leaks_fixed = leaks_fixed;
799 check->corruptions_fixed = corruptions_fixed;
f8111c24
DXW
800 }
801
832390a5
HR
802 if (!ret) {
803 switch (output_format) {
804 case OFORMAT_HUMAN:
805 dump_human_image_check(check, quiet);
806 break;
807 case OFORMAT_JSON:
808 dump_json_image_check(check, quiet);
809 break;
810 }
c6bb9ad1
FS
811 }
812
8599ea4c 813 if (ret || check->check_errors) {
832390a5
HR
814 if (ret) {
815 error_report("Check failed: %s", strerror(-ret));
816 } else {
817 error_report("Check failed");
818 }
8599ea4c
FS
819 ret = 1;
820 goto fail;
c2abccec 821 }
e076f338 822
8599ea4c
FS
823 if (check->corruptions) {
824 ret = 2;
825 } else if (check->leaks) {
826 ret = 3;
e076f338 827 } else {
8599ea4c 828 ret = 0;
e076f338 829 }
8599ea4c
FS
830
831fail:
832 qapi_free_ImageCheck(check);
26f54e9a 833 blk_unref(blk);
8599ea4c 834 return ret;
1585969c
AL
835}
836
d4a3238a
HR
837typedef struct CommonBlockJobCBInfo {
838 BlockDriverState *bs;
839 Error **errp;
840} CommonBlockJobCBInfo;
841
842static void common_block_job_cb(void *opaque, int ret)
843{
844 CommonBlockJobCBInfo *cbi = opaque;
845
846 if (ret < 0) {
847 error_setg_errno(cbi->errp, -ret, "Block job failed");
848 }
d4a3238a
HR
849}
850
851static void run_block_job(BlockJob *job, Error **errp)
852{
b75536c9 853 AioContext *aio_context = blk_get_aio_context(job->blk);
4172a003 854 int ret = 0;
d4a3238a 855
9e944cb4 856 aio_context_acquire(aio_context);
80fa2c75 857 job_ref(&job->job);
d4a3238a 858 do {
30a5c887 859 float progress = 0.0f;
d4a3238a 860 aio_poll(aio_context, true);
30a5c887
KW
861 if (job->job.progress_total) {
862 progress = (float)job->job.progress_current /
863 job->job.progress_total * 100.f;
864 }
865 qemu_progress_print(progress, 0);
df956ae2 866 } while (!job_is_ready(&job->job) && !job_is_completed(&job->job));
d4a3238a 867
dbe5e6c1 868 if (!job_is_completed(&job->job)) {
3d70ff53 869 ret = job_complete_sync(&job->job, errp);
4172a003 870 } else {
4ad35181 871 ret = job->job.ret;
4172a003 872 }
80fa2c75 873 job_unref(&job->job);
9e944cb4 874 aio_context_release(aio_context);
687fa1d8 875
4172a003
SJ
876 /* publish completion progress only when success */
877 if (!ret) {
878 qemu_progress_print(100.f, 0);
879 }
d4a3238a
HR
880}
881
ea2384d3
FB
882static int img_commit(int argc, char **argv)
883{
661a0f71 884 int c, ret, flags;
1b22bffd 885 const char *filename, *fmt, *cache, *base;
26f54e9a 886 BlockBackend *blk;
d4a3238a 887 BlockDriverState *bs, *base_bs;
4ef85a9c 888 BlockJob *job;
687fa1d8 889 bool progress = false, quiet = false, drop = false;
ce099547 890 bool writethrough;
d4a3238a
HR
891 Error *local_err = NULL;
892 CommonBlockJobCBInfo cbi;
eb769f74 893 bool image_opts = false;
9e944cb4 894 AioContext *aio_context;
ea2384d3
FB
895
896 fmt = NULL;
661a0f71 897 cache = BDRV_DEFAULT_CACHE;
1b22bffd 898 base = NULL;
ea2384d3 899 for(;;) {
3babeb15
DB
900 static const struct option long_options[] = {
901 {"help", no_argument, 0, 'h'},
902 {"object", required_argument, 0, OPTION_OBJECT},
eb769f74 903 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
3babeb15
DB
904 {0, 0, 0, 0}
905 };
c9192973 906 c = getopt_long(argc, argv, ":f:ht:b:dpq",
3babeb15 907 long_options, NULL);
b8fb60da 908 if (c == -1) {
ea2384d3 909 break;
b8fb60da 910 }
ea2384d3 911 switch(c) {
c9192973
SH
912 case ':':
913 missing_argument(argv[optind - 1]);
914 break;
ef87394c 915 case '?':
c9192973
SH
916 unrecognized_option(argv[optind - 1]);
917 break;
ea2384d3
FB
918 case 'h':
919 help();
920 break;
921 case 'f':
922 fmt = optarg;
923 break;
661a0f71
FS
924 case 't':
925 cache = optarg;
926 break;
1b22bffd
HR
927 case 'b':
928 base = optarg;
929 /* -b implies -d */
930 drop = true;
931 break;
9a86fe48
HR
932 case 'd':
933 drop = true;
934 break;
687fa1d8
HR
935 case 'p':
936 progress = true;
937 break;
f382d43a
MR
938 case 'q':
939 quiet = true;
940 break;
3babeb15
DB
941 case OPTION_OBJECT: {
942 QemuOpts *opts;
943 opts = qemu_opts_parse_noisily(&qemu_object_opts,
944 optarg, true);
945 if (!opts) {
946 return 1;
947 }
948 } break;
eb769f74
DB
949 case OPTION_IMAGE_OPTS:
950 image_opts = true;
951 break;
ea2384d3
FB
952 }
953 }
687fa1d8
HR
954
955 /* Progress is not shown in Quiet mode */
956 if (quiet) {
957 progress = false;
958 }
959
fc11eb26 960 if (optind != argc - 1) {
ac1307ab 961 error_exit("Expecting one image file name");
b8fb60da 962 }
ea2384d3
FB
963 filename = argv[optind++];
964
3babeb15
DB
965 if (qemu_opts_foreach(&qemu_object_opts,
966 user_creatable_add_opts_foreach,
334c43e2 967 NULL, &error_fatal)) {
3babeb15
DB
968 return 1;
969 }
970
9a86fe48 971 flags = BDRV_O_RDWR | BDRV_O_UNMAP;
ce099547 972 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
661a0f71
FS
973 if (ret < 0) {
974 error_report("Invalid cache option: %s", cache);
a3981eb9 975 return 1;
661a0f71
FS
976 }
977
335e9937
FZ
978 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
979 false);
7e7d56d9
MA
980 if (!blk) {
981 return 1;
c2abccec 982 }
7e7d56d9
MA
983 bs = blk_bs(blk);
984
687fa1d8
HR
985 qemu_progress_init(progress, 1.f);
986 qemu_progress_print(0.f, 100);
987
1b22bffd
HR
988 if (base) {
989 base_bs = bdrv_find_backing_image(bs, base);
990 if (!base_bs) {
6b33f3ae
HR
991 error_setg(&local_err,
992 "Did not find '%s' in the backing chain of '%s'",
993 base, filename);
1b22bffd
HR
994 goto done;
995 }
996 } else {
997 /* This is different from QMP, which by default uses the deepest file in
998 * the backing chain (i.e., the very base); however, the traditional
999 * behavior of qemu-img commit is using the immediate backing file. */
760e0063 1000 base_bs = backing_bs(bs);
1b22bffd
HR
1001 if (!base_bs) {
1002 error_setg(&local_err, "Image does not have a backing file");
1003 goto done;
1004 }
d4a3238a
HR
1005 }
1006
1007 cbi = (CommonBlockJobCBInfo){
1008 .errp = &local_err,
1009 .bs = bs,
1010 };
1011
9e944cb4
PB
1012 aio_context = bdrv_get_aio_context(bs);
1013 aio_context_acquire(aio_context);
bb02b65c 1014 commit_active_start("commit", bs, base_bs, JOB_DEFAULT, 0,
0db832f4 1015 BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb,
78bbd910 1016 &cbi, false, &local_err);
9e944cb4 1017 aio_context_release(aio_context);
d4a3238a
HR
1018 if (local_err) {
1019 goto done;
ea2384d3
FB
1020 }
1021
3f09bfbc
KW
1022 /* When the block job completes, the BlockBackend reference will point to
1023 * the old backing file. In order to avoid that the top image is already
1024 * deleted, so we can still empty it afterwards, increment the reference
1025 * counter here preemptively. */
9a86fe48 1026 if (!drop) {
3f09bfbc 1027 bdrv_ref(bs);
9a86fe48
HR
1028 }
1029
4ef85a9c 1030 job = block_job_get("commit");
2e2db260 1031 assert(job);
4ef85a9c 1032 run_block_job(job, &local_err);
9a86fe48
HR
1033 if (local_err) {
1034 goto unref_backing;
1035 }
1036
3f09bfbc
KW
1037 if (!drop && bs->drv->bdrv_make_empty) {
1038 ret = bs->drv->bdrv_make_empty(bs);
9a86fe48
HR
1039 if (ret) {
1040 error_setg_errno(&local_err, -ret, "Could not empty %s",
1041 filename);
1042 goto unref_backing;
1043 }
1044 }
1045
1046unref_backing:
1047 if (!drop) {
3f09bfbc 1048 bdrv_unref(bs);
9a86fe48 1049 }
d4a3238a
HR
1050
1051done:
687fa1d8
HR
1052 qemu_progress_end();
1053
26f54e9a 1054 blk_unref(blk);
d4a3238a
HR
1055
1056 if (local_err) {
6936f299 1057 error_report_err(local_err);
c2abccec
MK
1058 return 1;
1059 }
d4a3238a
HR
1060
1061 qprintf(quiet, "Image committed.\n");
ea2384d3
FB
1062 return 0;
1063}
1064
debb38a4
EB
1065/*
1066 * Returns -1 if 'buf' contains only zeroes, otherwise the byte index
1067 * of the first sector boundary within buf where the sector contains a
1068 * non-zero byte. This function is robust to a buffer that is not
1069 * sector-aligned.
1070 */
1071static int64_t find_nonzero(const uint8_t *buf, int64_t n)
1072{
1073 int64_t i;
1074 int64_t end = QEMU_ALIGN_DOWN(n, BDRV_SECTOR_SIZE);
1075
1076 for (i = 0; i < end; i += BDRV_SECTOR_SIZE) {
1077 if (!buffer_is_zero(buf + i, BDRV_SECTOR_SIZE)) {
1078 return i;
1079 }
1080 }
1081 if (i < n && !buffer_is_zero(buf + i, n - end)) {
1082 return i;
1083 }
1084 return -1;
1085}
1086
f58c7b35
TS
1087/*
1088 * Returns true iff the first sector pointed to by 'buf' contains at least
1089 * a non-NUL byte.
1090 *
1091 * 'pnum' is set to the number of sectors (including and immediately following
1092 * the first one) that are known to be in the same allocated/unallocated state.
8dcd3c9b
PL
1093 * The function will try to align the end offset to alignment boundaries so
1094 * that the request will at least end aligned and consequtive requests will
1095 * also start at an aligned offset.
f58c7b35 1096 */
8dcd3c9b
PL
1097static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum,
1098 int64_t sector_num, int alignment)
ea2384d3 1099{
1a6d39fd 1100 bool is_zero;
8dcd3c9b 1101 int i, tail;
ea2384d3
FB
1102
1103 if (n <= 0) {
1104 *pnum = 0;
1105 return 0;
1106 }
1a6d39fd 1107 is_zero = buffer_is_zero(buf, 512);
ea2384d3
FB
1108 for(i = 1; i < n; i++) {
1109 buf += 512;
1a6d39fd 1110 if (is_zero != buffer_is_zero(buf, 512)) {
ea2384d3 1111 break;
1a6d39fd 1112 }
ea2384d3 1113 }
8dcd3c9b
PL
1114
1115 tail = (sector_num + i) & (alignment - 1);
1116 if (tail) {
1117 if (is_zero && i <= tail) {
1118 /* treat unallocated areas which only consist
1119 * of a small tail as allocated. */
1120 is_zero = false;
1121 }
1122 if (!is_zero) {
1123 /* align up end offset of allocated areas. */
1124 i += alignment - tail;
1125 i = MIN(i, n);
1126 } else {
1127 /* align down end offset of zero areas. */
1128 i -= tail;
1129 }
1130 }
ea2384d3 1131 *pnum = i;
1a6d39fd 1132 return !is_zero;
ea2384d3
FB
1133}
1134
a22f123c
KW
1135/*
1136 * Like is_allocated_sectors, but if the buffer starts with a used sector,
1137 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1138 * breaking up write requests for only small sparse areas.
1139 */
1140static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
8dcd3c9b 1141 int min, int64_t sector_num, int alignment)
a22f123c
KW
1142{
1143 int ret;
1144 int num_checked, num_used;
1145
1146 if (n < min) {
1147 min = n;
1148 }
1149
8dcd3c9b 1150 ret = is_allocated_sectors(buf, n, pnum, sector_num, alignment);
a22f123c
KW
1151 if (!ret) {
1152 return ret;
1153 }
1154
1155 num_used = *pnum;
1156 buf += BDRV_SECTOR_SIZE * *pnum;
1157 n -= *pnum;
8dcd3c9b 1158 sector_num += *pnum;
a22f123c
KW
1159 num_checked = num_used;
1160
1161 while (n > 0) {
8dcd3c9b 1162 ret = is_allocated_sectors(buf, n, pnum, sector_num, alignment);
a22f123c
KW
1163
1164 buf += BDRV_SECTOR_SIZE * *pnum;
1165 n -= *pnum;
8dcd3c9b 1166 sector_num += *pnum;
a22f123c
KW
1167 num_checked += *pnum;
1168 if (ret) {
1169 num_used = num_checked;
1170 } else if (*pnum >= min) {
1171 break;
1172 }
1173 }
1174
1175 *pnum = num_used;
1176 return 1;
1177}
1178
3e85c6fd 1179/*
dc61cd3b
EB
1180 * Compares two buffers sector by sector. Returns 0 if the first
1181 * sector of each buffer matches, non-zero otherwise.
3e85c6fd 1182 *
dc61cd3b
EB
1183 * pnum is set to the sector-aligned size of the buffer prefix that
1184 * has the same matching status as the first sector.
3e85c6fd 1185 */
dc61cd3b
EB
1186static int compare_buffers(const uint8_t *buf1, const uint8_t *buf2,
1187 int64_t bytes, int64_t *pnum)
3e85c6fd 1188{
8c1ac475 1189 bool res;
dc61cd3b 1190 int64_t i = MIN(bytes, BDRV_SECTOR_SIZE);
3e85c6fd 1191
dc61cd3b 1192 assert(bytes > 0);
3e85c6fd 1193
dc61cd3b
EB
1194 res = !!memcmp(buf1, buf2, i);
1195 while (i < bytes) {
1196 int64_t len = MIN(bytes - i, BDRV_SECTOR_SIZE);
3e85c6fd 1197
dc61cd3b 1198 if (!!memcmp(buf1 + i, buf2 + i, len) != res) {
3e85c6fd
KW
1199 break;
1200 }
dc61cd3b 1201 i += len;
3e85c6fd
KW
1202 }
1203
1204 *pnum = i;
1205 return res;
1206}
1207
80ee15a6 1208#define IO_BUF_SIZE (2 * 1024 * 1024)
ea2384d3 1209
d14ed18c
MR
1210/*
1211 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1212 *
0608e40e
EB
1213 * Intended for use by 'qemu-img compare': Returns 0 in case sectors are
1214 * filled with 0, 1 if sectors contain non-zero data (this is a comparison
1215 * failure), and 4 on error (the exit status for read errors), after emitting
1216 * an error message.
d14ed18c 1217 *
f1d3cd79 1218 * @param blk: BlockBackend for the image
c41508ed
EB
1219 * @param offset: Starting offset to check
1220 * @param bytes: Number of bytes to check
d14ed18c
MR
1221 * @param filename: Name of disk file we are checking (logging purpose)
1222 * @param buffer: Allocated buffer for storing read data
1223 * @param quiet: Flag for quiet mode
1224 */
c41508ed
EB
1225static int check_empty_sectors(BlockBackend *blk, int64_t offset,
1226 int64_t bytes, const char *filename,
d14ed18c
MR
1227 uint8_t *buffer, bool quiet)
1228{
debb38a4
EB
1229 int ret = 0;
1230 int64_t idx;
1231
c41508ed 1232 ret = blk_pread(blk, offset, buffer, bytes);
d14ed18c
MR
1233 if (ret < 0) {
1234 error_report("Error while reading offset %" PRId64 " of %s: %s",
c41508ed 1235 offset, filename, strerror(-ret));
0608e40e 1236 return 4;
d14ed18c 1237 }
c41508ed 1238 idx = find_nonzero(buffer, bytes);
debb38a4 1239 if (idx >= 0) {
d14ed18c 1240 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
c41508ed 1241 offset + idx);
d14ed18c
MR
1242 return 1;
1243 }
1244
1245 return 0;
1246}
1247
1248/*
1249 * Compares two images. Exit codes:
1250 *
1251 * 0 - Images are identical
1252 * 1 - Images differ
1253 * >1 - Error occurred
1254 */
1255static int img_compare(int argc, char **argv)
1256{
40055951 1257 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
26f54e9a 1258 BlockBackend *blk1, *blk2;
d14ed18c 1259 BlockDriverState *bs1, *bs2;
033d9fc2 1260 int64_t total_size1, total_size2;
d14ed18c 1261 uint8_t *buf1 = NULL, *buf2 = NULL;
31826642 1262 int64_t pnum1, pnum2;
d14ed18c
MR
1263 int allocated1, allocated2;
1264 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1265 bool progress = false, quiet = false, strict = false;
40055951 1266 int flags;
ce099547 1267 bool writethrough;
033d9fc2
EB
1268 int64_t total_size;
1269 int64_t offset = 0;
1270 int64_t chunk;
dc61cd3b 1271 int c;
d14ed18c 1272 uint64_t progress_base;
eb769f74 1273 bool image_opts = false;
335e9937 1274 bool force_share = false;
d14ed18c 1275
40055951 1276 cache = BDRV_DEFAULT_CACHE;
d14ed18c 1277 for (;;) {
3babeb15
DB
1278 static const struct option long_options[] = {
1279 {"help", no_argument, 0, 'h'},
1280 {"object", required_argument, 0, OPTION_OBJECT},
eb769f74 1281 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
335e9937 1282 {"force-share", no_argument, 0, 'U'},
3babeb15
DB
1283 {0, 0, 0, 0}
1284 };
335e9937 1285 c = getopt_long(argc, argv, ":hf:F:T:pqsU",
3babeb15 1286 long_options, NULL);
d14ed18c
MR
1287 if (c == -1) {
1288 break;
1289 }
1290 switch (c) {
c9192973
SH
1291 case ':':
1292 missing_argument(argv[optind - 1]);
1293 break;
d14ed18c 1294 case '?':
c9192973
SH
1295 unrecognized_option(argv[optind - 1]);
1296 break;
d14ed18c
MR
1297 case 'h':
1298 help();
1299 break;
1300 case 'f':
1301 fmt1 = optarg;
1302 break;
1303 case 'F':
1304 fmt2 = optarg;
1305 break;
40055951
HR
1306 case 'T':
1307 cache = optarg;
1308 break;
d14ed18c
MR
1309 case 'p':
1310 progress = true;
1311 break;
1312 case 'q':
1313 quiet = true;
1314 break;
1315 case 's':
1316 strict = true;
1317 break;
335e9937
FZ
1318 case 'U':
1319 force_share = true;
1320 break;
3babeb15
DB
1321 case OPTION_OBJECT: {
1322 QemuOpts *opts;
1323 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1324 optarg, true);
1325 if (!opts) {
1326 ret = 2;
1327 goto out4;
1328 }
1329 } break;
eb769f74
DB
1330 case OPTION_IMAGE_OPTS:
1331 image_opts = true;
1332 break;
d14ed18c
MR
1333 }
1334 }
1335
1336 /* Progress is not shown in Quiet mode */
1337 if (quiet) {
1338 progress = false;
1339 }
1340
1341
fc11eb26 1342 if (optind != argc - 2) {
ac1307ab 1343 error_exit("Expecting two image file names");
d14ed18c
MR
1344 }
1345 filename1 = argv[optind++];
1346 filename2 = argv[optind++];
1347
3babeb15
DB
1348 if (qemu_opts_foreach(&qemu_object_opts,
1349 user_creatable_add_opts_foreach,
334c43e2 1350 NULL, &error_fatal)) {
3babeb15
DB
1351 ret = 2;
1352 goto out4;
1353 }
1354
cbda016d
SH
1355 /* Initialize before goto out */
1356 qemu_progress_init(progress, 2.0);
1357
ce099547
KW
1358 flags = 0;
1359 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
40055951
HR
1360 if (ret < 0) {
1361 error_report("Invalid source cache option: %s", cache);
1362 ret = 2;
1363 goto out3;
1364 }
1365
335e9937
FZ
1366 blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet,
1367 force_share);
7e7d56d9 1368 if (!blk1) {
d14ed18c 1369 ret = 2;
7e7d56d9 1370 goto out3;
d14ed18c
MR
1371 }
1372
335e9937
FZ
1373 blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet,
1374 force_share);
7e7d56d9 1375 if (!blk2) {
d14ed18c 1376 ret = 2;
7e7d56d9 1377 goto out2;
d14ed18c 1378 }
eb769f74 1379 bs1 = blk_bs(blk1);
7e7d56d9 1380 bs2 = blk_bs(blk2);
d14ed18c 1381
f1d3cd79
HR
1382 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1383 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
033d9fc2
EB
1384 total_size1 = blk_getlength(blk1);
1385 if (total_size1 < 0) {
52bf1e72 1386 error_report("Can't get size of %s: %s",
033d9fc2 1387 filename1, strerror(-total_size1));
52bf1e72
MA
1388 ret = 4;
1389 goto out;
1390 }
033d9fc2
EB
1391 total_size2 = blk_getlength(blk2);
1392 if (total_size2 < 0) {
52bf1e72 1393 error_report("Can't get size of %s: %s",
033d9fc2 1394 filename2, strerror(-total_size2));
52bf1e72
MA
1395 ret = 4;
1396 goto out;
1397 }
033d9fc2
EB
1398 total_size = MIN(total_size1, total_size2);
1399 progress_base = MAX(total_size1, total_size2);
d14ed18c
MR
1400
1401 qemu_progress_print(0, 100);
1402
033d9fc2 1403 if (strict && total_size1 != total_size2) {
d14ed18c
MR
1404 ret = 1;
1405 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1406 goto out;
1407 }
1408
033d9fc2 1409 while (offset < total_size) {
31826642 1410 int status1, status2;
67a0fd2a 1411
033d9fc2
EB
1412 status1 = bdrv_block_status_above(bs1, NULL, offset,
1413 total_size1 - offset, &pnum1, NULL,
1414 NULL);
25ad8e6e 1415 if (status1 < 0) {
d14ed18c
MR
1416 ret = 3;
1417 error_report("Sector allocation test failed for %s", filename1);
1418 goto out;
1419 }
25ad8e6e 1420 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
d14ed18c 1421
033d9fc2
EB
1422 status2 = bdrv_block_status_above(bs2, NULL, offset,
1423 total_size2 - offset, &pnum2, NULL,
1424 NULL);
25ad8e6e 1425 if (status2 < 0) {
d14ed18c
MR
1426 ret = 3;
1427 error_report("Sector allocation test failed for %s", filename2);
1428 goto out;
1429 }
25ad8e6e 1430 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
7daddc61
EB
1431
1432 assert(pnum1 && pnum2);
033d9fc2 1433 chunk = MIN(pnum1, pnum2);
d14ed18c 1434
25ad8e6e 1435 if (strict) {
31826642 1436 if (status1 != status2) {
25ad8e6e
FZ
1437 ret = 1;
1438 qprintf(quiet, "Strict mode: Offset %" PRId64
033d9fc2 1439 " block status mismatch!\n", offset);
25ad8e6e
FZ
1440 goto out;
1441 }
1442 }
1443 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
7daddc61 1444 /* nothing to do */
25ad8e6e 1445 } else if (allocated1 == allocated2) {
d14ed18c 1446 if (allocated1) {
dc61cd3b
EB
1447 int64_t pnum;
1448
033d9fc2
EB
1449 chunk = MIN(chunk, IO_BUF_SIZE);
1450 ret = blk_pread(blk1, offset, buf1, chunk);
d14ed18c 1451 if (ret < 0) {
033d9fc2
EB
1452 error_report("Error while reading offset %" PRId64
1453 " of %s: %s",
1454 offset, filename1, strerror(-ret));
d14ed18c
MR
1455 ret = 4;
1456 goto out;
1457 }
033d9fc2 1458 ret = blk_pread(blk2, offset, buf2, chunk);
d14ed18c
MR
1459 if (ret < 0) {
1460 error_report("Error while reading offset %" PRId64
033d9fc2
EB
1461 " of %s: %s",
1462 offset, filename2, strerror(-ret));
d14ed18c
MR
1463 ret = 4;
1464 goto out;
1465 }
033d9fc2
EB
1466 ret = compare_buffers(buf1, buf2, chunk, &pnum);
1467 if (ret || pnum != chunk) {
d14ed18c 1468 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
033d9fc2 1469 offset + (ret ? 0 : pnum));
36452f12 1470 ret = 1;
d14ed18c
MR
1471 goto out;
1472 }
1473 }
1474 } else {
033d9fc2 1475 chunk = MIN(chunk, IO_BUF_SIZE);
d14ed18c 1476 if (allocated1) {
033d9fc2 1477 ret = check_empty_sectors(blk1, offset, chunk,
d14ed18c
MR
1478 filename1, buf1, quiet);
1479 } else {
033d9fc2 1480 ret = check_empty_sectors(blk2, offset, chunk,
d14ed18c
MR
1481 filename2, buf1, quiet);
1482 }
1483 if (ret) {
d14ed18c
MR
1484 goto out;
1485 }
1486 }
033d9fc2
EB
1487 offset += chunk;
1488 qemu_progress_print(((float) chunk / progress_base) * 100, 100);
d14ed18c
MR
1489 }
1490
033d9fc2 1491 if (total_size1 != total_size2) {
f1d3cd79 1492 BlockBackend *blk_over;
d14ed18c
MR
1493 const char *filename_over;
1494
1495 qprintf(quiet, "Warning: Image size mismatch!\n");
033d9fc2 1496 if (total_size1 > total_size2) {
f1d3cd79 1497 blk_over = blk1;
d14ed18c
MR
1498 filename_over = filename1;
1499 } else {
f1d3cd79 1500 blk_over = blk2;
d14ed18c
MR
1501 filename_over = filename2;
1502 }
1503
033d9fc2
EB
1504 while (offset < progress_base) {
1505 ret = bdrv_block_status_above(blk_bs(blk_over), NULL, offset,
1506 progress_base - offset, &chunk,
1507 NULL, NULL);
d14ed18c
MR
1508 if (ret < 0) {
1509 ret = 3;
1510 error_report("Sector allocation test failed for %s",
1511 filename_over);
1512 goto out;
1513
1514 }
391cb1aa 1515 if (ret & BDRV_BLOCK_ALLOCATED && !(ret & BDRV_BLOCK_ZERO)) {
033d9fc2
EB
1516 chunk = MIN(chunk, IO_BUF_SIZE);
1517 ret = check_empty_sectors(blk_over, offset, chunk,
d14ed18c
MR
1518 filename_over, buf1, quiet);
1519 if (ret) {
d14ed18c
MR
1520 goto out;
1521 }
1522 }
033d9fc2
EB
1523 offset += chunk;
1524 qemu_progress_print(((float) chunk / progress_base) * 100, 100);
d14ed18c
MR
1525 }
1526 }
1527
1528 qprintf(quiet, "Images are identical.\n");
1529 ret = 0;
1530
1531out:
d14ed18c
MR
1532 qemu_vfree(buf1);
1533 qemu_vfree(buf2);
26f54e9a 1534 blk_unref(blk2);
d14ed18c 1535out2:
26f54e9a 1536 blk_unref(blk1);
d14ed18c
MR
1537out3:
1538 qemu_progress_end();
3babeb15 1539out4:
d14ed18c
MR
1540 return ret;
1541}
1542
690c7301
KW
1543enum ImgConvertBlockStatus {
1544 BLK_DATA,
1545 BLK_ZERO,
1546 BLK_BACKING_FILE,
1547};
1548
2d9187bc
PL
1549#define MAX_COROUTINES 16
1550
690c7301
KW
1551typedef struct ImgConvertState {
1552 BlockBackend **src;
1553 int64_t *src_sectors;
2d9187bc 1554 int src_num;
690c7301
KW
1555 int64_t total_sectors;
1556 int64_t allocated_sectors;
2d9187bc
PL
1557 int64_t allocated_done;
1558 int64_t sector_num;
1559 int64_t wr_offs;
690c7301
KW
1560 enum ImgConvertBlockStatus status;
1561 int64_t sector_next_status;
1562 BlockBackend *target;
1563 bool has_zero_init;
1564 bool compressed;
351c8eff 1565 bool unallocated_blocks_are_zero;
690c7301 1566 bool target_has_backing;
351c8eff 1567 int64_t target_backing_sectors; /* negative if unknown */
2d9187bc 1568 bool wr_in_order;
ee5306d0 1569 bool copy_range;
690c7301 1570 int min_sparse;
8dcd3c9b 1571 int alignment;
690c7301
KW
1572 size_t cluster_sectors;
1573 size_t buf_sectors;
9fd77f99 1574 long num_coroutines;
2d9187bc
PL
1575 int running_coroutines;
1576 Coroutine *co[MAX_COROUTINES];
1577 int64_t wait_sector_num[MAX_COROUTINES];
1578 CoMutex lock;
1579 int ret;
690c7301
KW
1580} ImgConvertState;
1581
2d9187bc
PL
1582static void convert_select_part(ImgConvertState *s, int64_t sector_num,
1583 int *src_cur, int64_t *src_cur_offset)
690c7301 1584{
2d9187bc
PL
1585 *src_cur = 0;
1586 *src_cur_offset = 0;
1587 while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) {
1588 *src_cur_offset += s->src_sectors[*src_cur];
1589 (*src_cur)++;
1590 assert(*src_cur < s->src_num);
690c7301
KW
1591 }
1592}
1593
1594static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1595{
31826642
EB
1596 int64_t src_cur_offset;
1597 int ret, n, src_cur;
351c8eff 1598 bool post_backing_zero = false;
690c7301 1599
2d9187bc 1600 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
690c7301
KW
1601
1602 assert(s->total_sectors > sector_num);
1603 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1604
351c8eff
HR
1605 if (s->target_backing_sectors >= 0) {
1606 if (sector_num >= s->target_backing_sectors) {
1607 post_backing_zero = s->unallocated_blocks_are_zero;
1608 } else if (sector_num + n > s->target_backing_sectors) {
1609 /* Split requests around target_backing_sectors (because
1610 * starting from there, zeros are handled differently) */
1611 n = s->target_backing_sectors - sector_num;
1612 }
1613 }
1614
690c7301 1615 if (s->sector_next_status <= sector_num) {
31826642
EB
1616 int64_t count = n * BDRV_SECTOR_SIZE;
1617
9f1b92ad 1618 if (s->target_has_backing) {
237d78f8
EB
1619
1620 ret = bdrv_block_status(blk_bs(s->src[src_cur]),
1621 (sector_num - src_cur_offset) *
1622 BDRV_SECTOR_SIZE,
1623 count, &count, NULL, NULL);
9f1b92ad 1624 } else {
31826642
EB
1625 ret = bdrv_block_status_above(blk_bs(s->src[src_cur]), NULL,
1626 (sector_num - src_cur_offset) *
1627 BDRV_SECTOR_SIZE,
1628 count, &count, NULL, NULL);
9f1b92ad 1629 }
690c7301 1630 if (ret < 0) {
2058c2ad
EB
1631 error_report("error while reading block status of sector %" PRId64
1632 ": %s", sector_num, strerror(-ret));
690c7301
KW
1633 return ret;
1634 }
31826642 1635 n = DIV_ROUND_UP(count, BDRV_SECTOR_SIZE);
690c7301
KW
1636
1637 if (ret & BDRV_BLOCK_ZERO) {
351c8eff 1638 s->status = post_backing_zero ? BLK_BACKING_FILE : BLK_ZERO;
690c7301
KW
1639 } else if (ret & BDRV_BLOCK_DATA) {
1640 s->status = BLK_DATA;
690c7301 1641 } else {
9f1b92ad 1642 s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA;
690c7301
KW
1643 }
1644
1645 s->sector_next_status = sector_num + n;
1646 }
1647
1648 n = MIN(n, s->sector_next_status - sector_num);
1649 if (s->status == BLK_DATA) {
1650 n = MIN(n, s->buf_sectors);
1651 }
1652
1653 /* We need to write complete clusters for compressed images, so if an
1654 * unallocated area is shorter than that, we must consider the whole
1655 * cluster allocated. */
1656 if (s->compressed) {
1657 if (n < s->cluster_sectors) {
1658 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1659 s->status = BLK_DATA;
1660 } else {
1661 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1662 }
1663 }
1664
1665 return n;
1666}
1667
2d9187bc
PL
1668static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num,
1669 int nb_sectors, uint8_t *buf)
690c7301 1670{
2d9187bc
PL
1671 int n, ret;
1672 QEMUIOVector qiov;
690c7301 1673
690c7301
KW
1674 assert(nb_sectors <= s->buf_sectors);
1675 while (nb_sectors > 0) {
1676 BlockBackend *blk;
2d9187bc
PL
1677 int src_cur;
1678 int64_t bs_sectors, src_cur_offset;
690c7301
KW
1679
1680 /* In the case of compression with multiple source files, we can get a
1681 * nb_sectors that spreads into the next part. So we must be able to
1682 * read across multiple BDSes for one convert_read() call. */
2d9187bc
PL
1683 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1684 blk = s->src[src_cur];
1685 bs_sectors = s->src_sectors[src_cur];
1686
1687 n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
c075a0af 1688 qemu_iovec_init_buf(&qiov, buf, n << BDRV_SECTOR_BITS);
2d9187bc
PL
1689
1690 ret = blk_co_preadv(
1691 blk, (sector_num - src_cur_offset) << BDRV_SECTOR_BITS,
1692 n << BDRV_SECTOR_BITS, &qiov, 0);
690c7301
KW
1693 if (ret < 0) {
1694 return ret;
1695 }
1696
1697 sector_num += n;
1698 nb_sectors -= n;
1699 buf += n * BDRV_SECTOR_SIZE;
1700 }
1701
1702 return 0;
1703}
1704
2d9187bc
PL
1705
1706static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
1707 int nb_sectors, uint8_t *buf,
1708 enum ImgConvertBlockStatus status)
690c7301
KW
1709{
1710 int ret;
2d9187bc 1711 QEMUIOVector qiov;
690c7301
KW
1712
1713 while (nb_sectors > 0) {
1714 int n = nb_sectors;
db933fbe
LC
1715 BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0;
1716
2d9187bc 1717 switch (status) {
690c7301
KW
1718 case BLK_BACKING_FILE:
1719 /* If we have a backing file, leave clusters unallocated that are
1720 * unallocated in the source image, so that the backing file is
1721 * visible at the respective offset. */
1722 assert(s->target_has_backing);
1723 break;
1724
1725 case BLK_DATA:
db933fbe
LC
1726 /* If we're told to keep the target fully allocated (-S 0) or there
1727 * is real non-zero data, we must write it. Otherwise we can treat
1728 * it as zero sectors.
1729 * Compressed clusters need to be written as a whole, so in that
1730 * case we can only save the write if the buffer is completely
1731 * zeroed. */
690c7301 1732 if (!s->min_sparse ||
db933fbe 1733 (!s->compressed &&
8dcd3c9b
PL
1734 is_allocated_sectors_min(buf, n, &n, s->min_sparse,
1735 sector_num, s->alignment)) ||
db933fbe
LC
1736 (s->compressed &&
1737 !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)))
690c7301 1738 {
c075a0af 1739 qemu_iovec_init_buf(&qiov, buf, n << BDRV_SECTOR_BITS);
2d9187bc
PL
1740
1741 ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS,
db933fbe 1742 n << BDRV_SECTOR_BITS, &qiov, flags);
690c7301
KW
1743 if (ret < 0) {
1744 return ret;
1745 }
1746 break;
1747 }
1748 /* fall-through */
1749
1750 case BLK_ZERO:
1751 if (s->has_zero_init) {
db933fbe 1752 assert(!s->target_has_backing);
690c7301
KW
1753 break;
1754 }
2d9187bc
PL
1755 ret = blk_co_pwrite_zeroes(s->target,
1756 sector_num << BDRV_SECTOR_BITS,
a3d6ae22
NS
1757 n << BDRV_SECTOR_BITS,
1758 BDRV_REQ_MAY_UNMAP);
690c7301
KW
1759 if (ret < 0) {
1760 return ret;
1761 }
1762 break;
1763 }
1764
1765 sector_num += n;
1766 nb_sectors -= n;
1767 buf += n * BDRV_SECTOR_SIZE;
1768 }
1769
1770 return 0;
1771}
1772
ee5306d0
FZ
1773static int coroutine_fn convert_co_copy_range(ImgConvertState *s, int64_t sector_num,
1774 int nb_sectors)
1775{
1776 int n, ret;
1777
1778 while (nb_sectors > 0) {
1779 BlockBackend *blk;
1780 int src_cur;
1781 int64_t bs_sectors, src_cur_offset;
1782 int64_t offset;
1783
1784 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1785 offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS;
1786 blk = s->src[src_cur];
1787 bs_sectors = s->src_sectors[src_cur];
1788
1789 n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1790
1791 ret = blk_co_copy_range(blk, offset, s->target,
1792 sector_num << BDRV_SECTOR_BITS,
67b51fb9 1793 n << BDRV_SECTOR_BITS, 0, 0);
ee5306d0
FZ
1794 if (ret < 0) {
1795 return ret;
1796 }
1797
1798 sector_num += n;
1799 nb_sectors -= n;
1800 }
1801 return 0;
1802}
1803
2d9187bc 1804static void coroutine_fn convert_co_do_copy(void *opaque)
690c7301 1805{
2d9187bc 1806 ImgConvertState *s = opaque;
690c7301 1807 uint8_t *buf = NULL;
2d9187bc
PL
1808 int ret, i;
1809 int index = -1;
1810
1811 for (i = 0; i < s->num_coroutines; i++) {
1812 if (s->co[i] == qemu_coroutine_self()) {
1813 index = i;
1814 break;
1815 }
1816 }
1817 assert(index >= 0);
1818
1819 s->running_coroutines++;
1820 buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1821
1822 while (1) {
1823 int n;
1824 int64_t sector_num;
1825 enum ImgConvertBlockStatus status;
ee5306d0 1826 bool copy_range;
2d9187bc
PL
1827
1828 qemu_co_mutex_lock(&s->lock);
1829 if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) {
1830 qemu_co_mutex_unlock(&s->lock);
b91127ed 1831 break;
2d9187bc
PL
1832 }
1833 n = convert_iteration_sectors(s, s->sector_num);
1834 if (n < 0) {
1835 qemu_co_mutex_unlock(&s->lock);
1836 s->ret = n;
b91127ed 1837 break;
2d9187bc
PL
1838 }
1839 /* save current sector and allocation status to local variables */
1840 sector_num = s->sector_num;
1841 status = s->status;
1842 if (!s->min_sparse && s->status == BLK_ZERO) {
1843 n = MIN(n, s->buf_sectors);
1844 }
1845 /* increment global sector counter so that other coroutines can
1846 * already continue reading beyond this request */
1847 s->sector_num += n;
1848 qemu_co_mutex_unlock(&s->lock);
1849
1850 if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) {
1851 s->allocated_done += n;
1852 qemu_progress_print(100.0 * s->allocated_done /
1853 s->allocated_sectors, 0);
1854 }
1855
ee5306d0
FZ
1856retry:
1857 copy_range = s->copy_range && s->status == BLK_DATA;
1858 if (status == BLK_DATA && !copy_range) {
2d9187bc
PL
1859 ret = convert_co_read(s, sector_num, n, buf);
1860 if (ret < 0) {
1861 error_report("error while reading sector %" PRId64
1862 ": %s", sector_num, strerror(-ret));
1863 s->ret = ret;
2d9187bc
PL
1864 }
1865 } else if (!s->min_sparse && status == BLK_ZERO) {
1866 status = BLK_DATA;
1867 memset(buf, 0x00, n * BDRV_SECTOR_SIZE);
1868 }
1869
1870 if (s->wr_in_order) {
1871 /* keep writes in order */
b91127ed 1872 while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) {
2d9187bc
PL
1873 s->wait_sector_num[index] = sector_num;
1874 qemu_coroutine_yield();
1875 }
1876 s->wait_sector_num[index] = -1;
1877 }
1878
b91127ed 1879 if (s->ret == -EINPROGRESS) {
ee5306d0
FZ
1880 if (copy_range) {
1881 ret = convert_co_copy_range(s, sector_num, n);
1882 if (ret) {
1883 s->copy_range = false;
1884 goto retry;
1885 }
1886 } else {
1887 ret = convert_co_write(s, sector_num, n, buf, status);
1888 }
b91127ed
AN
1889 if (ret < 0) {
1890 error_report("error while writing sector %" PRId64
1891 ": %s", sector_num, strerror(-ret));
1892 s->ret = ret;
1893 }
2d9187bc
PL
1894 }
1895
1896 if (s->wr_in_order) {
1897 /* reenter the coroutine that might have waited
1898 * for this write to complete */
1899 s->wr_offs = sector_num + n;
1900 for (i = 0; i < s->num_coroutines; i++) {
1901 if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) {
1902 /*
1903 * A -> B -> A cannot occur because A has
1904 * s->wait_sector_num[i] == -1 during A -> B. Therefore
1905 * B will never enter A during this time window.
1906 */
1907 qemu_coroutine_enter(s->co[i]);
1908 break;
1909 }
1910 }
1911 }
1912 }
1913
2d9187bc
PL
1914 qemu_vfree(buf);
1915 s->co[index] = NULL;
1916 s->running_coroutines--;
1917 if (!s->running_coroutines && s->ret == -EINPROGRESS) {
1918 /* the convert job finished successfully */
1919 s->ret = 0;
1920 }
1921}
1922
1923static int convert_do_copy(ImgConvertState *s)
1924{
1925 int ret, i, n;
1926 int64_t sector_num = 0;
690c7301
KW
1927
1928 /* Check whether we have zero initialisation or can get it efficiently */
1929 s->has_zero_init = s->min_sparse && !s->target_has_backing
1930 ? bdrv_has_zero_init(blk_bs(s->target))
1931 : false;
1932
1933 if (!s->has_zero_init && !s->target_has_backing &&
1934 bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
1935 {
c9fdcf20 1936 ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK);
690c7301
KW
1937 if (ret == 0) {
1938 s->has_zero_init = true;
1939 }
1940 }
1941
1942 /* Allocate buffer for copied data. For compressed images, only one cluster
1943 * can be copied at a time. */
1944 if (s->compressed) {
1945 if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
1946 error_report("invalid cluster size");
2d9187bc 1947 return -EINVAL;
690c7301
KW
1948 }
1949 s->buf_sectors = s->cluster_sectors;
1950 }
690c7301 1951
690c7301
KW
1952 while (sector_num < s->total_sectors) {
1953 n = convert_iteration_sectors(s, sector_num);
1954 if (n < 0) {
2d9187bc 1955 return n;
690c7301 1956 }
aad15de4
HR
1957 if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
1958 {
690c7301
KW
1959 s->allocated_sectors += n;
1960 }
1961 sector_num += n;
1962 }
1963
1964 /* Do the copy */
690c7301 1965 s->sector_next_status = 0;
2d9187bc 1966 s->ret = -EINPROGRESS;
690c7301 1967
2d9187bc
PL
1968 qemu_co_mutex_init(&s->lock);
1969 for (i = 0; i < s->num_coroutines; i++) {
1970 s->co[i] = qemu_coroutine_create(convert_co_do_copy, s);
1971 s->wait_sector_num[i] = -1;
1972 qemu_coroutine_enter(s->co[i]);
1973 }
690c7301 1974
b91127ed 1975 while (s->running_coroutines) {
2d9187bc 1976 main_loop_wait(false);
690c7301
KW
1977 }
1978
2d9187bc 1979 if (s->compressed && !s->ret) {
690c7301 1980 /* signal EOF to align */
fe5c1355 1981 ret = blk_pwrite_compressed(s->target, 0, NULL, 0);
690c7301 1982 if (ret < 0) {
2d9187bc 1983 return ret;
690c7301
KW
1984 }
1985 }
1986
2d9187bc 1987 return s->ret;
690c7301
KW
1988}
1989
6360ab27
PL
1990#define MAX_BUF_SECTORS 32768
1991
ea2384d3
FB
1992static int img_convert(int argc, char **argv)
1993{
9fd77f99 1994 int c, bs_i, flags, src_flags = 0;
305b4c60 1995 const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe",
9fd77f99
PL
1996 *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL,
1997 *out_filename, *out_baseimg_param, *snapshot_name = NULL;
305b4c60 1998 BlockDriver *drv = NULL, *proto_drv = NULL;
faea38e7 1999 BlockDriverInfo bdi;
9fd77f99
PL
2000 BlockDriverState *out_bs;
2001 QemuOpts *opts = NULL, *sn_opts = NULL;
83d0521a 2002 QemuOptsList *create_opts = NULL;
8d65a3cc 2003 QDict *open_opts = NULL;
efa84d43 2004 char *options = NULL;
cc84d90f 2005 Error *local_err = NULL;
9fd77f99 2006 bool writethrough, src_writethrough, quiet = false, image_opts = false,
305b4c60 2007 skip_create = false, progress = false, tgt_image_opts = false;
9fd77f99 2008 int64_t ret = -EINVAL;
335e9937 2009 bool force_share = false;
e11ce12f 2010 bool explict_min_sparse = false;
9fd77f99
PL
2011
2012 ImgConvertState s = (ImgConvertState) {
2013 /* Need at least 4k of zeros for sparse detection */
2014 .min_sparse = 8,
e11ce12f 2015 .copy_range = false,
9fd77f99
PL
2016 .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
2017 .wr_in_order = true,
2018 .num_coroutines = 8,
2019 };
ea2384d3 2020
ea2384d3 2021 for(;;) {
3babeb15
DB
2022 static const struct option long_options[] = {
2023 {"help", no_argument, 0, 'h'},
2024 {"object", required_argument, 0, OPTION_OBJECT},
eb769f74 2025 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
335e9937 2026 {"force-share", no_argument, 0, 'U'},
305b4c60 2027 {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
3babeb15
DB
2028 {0, 0, 0, 0}
2029 };
e11ce12f 2030 c = getopt_long(argc, argv, ":hf:O:B:Cco:l:S:pt:T:qnm:WU",
3babeb15 2031 long_options, NULL);
b8fb60da 2032 if (c == -1) {
ea2384d3 2033 break;
b8fb60da 2034 }
ea2384d3 2035 switch(c) {
c9192973
SH
2036 case ':':
2037 missing_argument(argv[optind - 1]);
2038 break;
ef87394c 2039 case '?':
c9192973
SH
2040 unrecognized_option(argv[optind - 1]);
2041 break;
ea2384d3
FB
2042 case 'h':
2043 help();
2044 break;
2045 case 'f':
2046 fmt = optarg;
2047 break;
2048 case 'O':
2049 out_fmt = optarg;
2050 break;
f58c7b35
TS
2051 case 'B':
2052 out_baseimg = optarg;
2053 break;
e11ce12f
FZ
2054 case 'C':
2055 s.copy_range = true;
2056 break;
ea2384d3 2057 case 'c':
9fd77f99 2058 s.compressed = true;
ea2384d3 2059 break;
efa84d43 2060 case 'o':
2dc8328b
KW
2061 if (!is_valid_option_list(optarg)) {
2062 error_report("Invalid option list: %s", optarg);
64bb01aa 2063 goto fail_getopt;
2dc8328b
KW
2064 }
2065 if (!options) {
2066 options = g_strdup(optarg);
2067 } else {
2068 char *old_options = options;
2069 options = g_strdup_printf("%s,%s", options, optarg);
2070 g_free(old_options);
2071 }
efa84d43 2072 break;
ef80654d
WX
2073 case 'l':
2074 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
70b94331
MA
2075 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
2076 optarg, false);
ef80654d
WX
2077 if (!sn_opts) {
2078 error_report("Failed in parsing snapshot param '%s'",
2079 optarg);
64bb01aa 2080 goto fail_getopt;
ef80654d
WX
2081 }
2082 } else {
2083 snapshot_name = optarg;
2084 }
2085 break;
a22f123c
KW
2086 case 'S':
2087 {
2088 int64_t sval;
606caa0a
MA
2089
2090 sval = cvtnum(optarg);
6360ab27
PL
2091 if (sval < 0 || sval & (BDRV_SECTOR_SIZE - 1) ||
2092 sval / BDRV_SECTOR_SIZE > MAX_BUF_SECTORS) {
2093 error_report("Invalid buffer size for sparse output specified. "
2094 "Valid sizes are multiples of %llu up to %llu. Select "
2095 "0 to disable sparse detection (fully allocates output).",
2096 BDRV_SECTOR_SIZE, MAX_BUF_SECTORS * BDRV_SECTOR_SIZE);
64bb01aa 2097 goto fail_getopt;
a22f123c
KW
2098 }
2099
9fd77f99 2100 s.min_sparse = sval / BDRV_SECTOR_SIZE;
e11ce12f 2101 explict_min_sparse = true;
a22f123c
KW
2102 break;
2103 }
6b837bc4 2104 case 'p':
9fd77f99 2105 progress = true;
6b837bc4 2106 break;
661a0f71
FS
2107 case 't':
2108 cache = optarg;
2109 break;
40055951
HR
2110 case 'T':
2111 src_cache = optarg;
2112 break;
f382d43a
MR
2113 case 'q':
2114 quiet = true;
2115 break;
b2e10493 2116 case 'n':
9fd77f99 2117 skip_create = true;
b2e10493 2118 break;
2d9187bc 2119 case 'm':
9fd77f99
PL
2120 if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) ||
2121 s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) {
2d9187bc
PL
2122 error_report("Invalid number of coroutines. Allowed number of"
2123 " coroutines is between 1 and %d", MAX_COROUTINES);
2d9187bc
PL
2124 goto fail_getopt;
2125 }
2126 break;
2127 case 'W':
9fd77f99 2128 s.wr_in_order = false;
2d9187bc 2129 break;
335e9937
FZ
2130 case 'U':
2131 force_share = true;
2132 break;
3258b911
HR
2133 case OPTION_OBJECT: {
2134 QemuOpts *object_opts;
2135 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
2136 optarg, true);
2137 if (!object_opts) {
3babeb15
DB
2138 goto fail_getopt;
2139 }
2140 break;
3258b911 2141 }
eb769f74
DB
2142 case OPTION_IMAGE_OPTS:
2143 image_opts = true;
2144 break;
305b4c60
DB
2145 case OPTION_TARGET_IMAGE_OPTS:
2146 tgt_image_opts = true;
2147 break;
ea2384d3
FB
2148 }
2149 }
3b46e624 2150
305b4c60
DB
2151 if (!out_fmt && !tgt_image_opts) {
2152 out_fmt = "raw";
2153 }
2154
3babeb15
DB
2155 if (qemu_opts_foreach(&qemu_object_opts,
2156 user_creatable_add_opts_foreach,
334c43e2 2157 NULL, &error_fatal)) {
3babeb15
DB
2158 goto fail_getopt;
2159 }
2160
e11ce12f
FZ
2161 if (s.compressed && s.copy_range) {
2162 error_report("Cannot enable copy offloading when -c is used");
2163 goto fail_getopt;
2164 }
2165
2166 if (explict_min_sparse && s.copy_range) {
2167 error_report("Cannot enable copy offloading when -S is used");
2168 goto fail_getopt;
2169 }
2170
305b4c60
DB
2171 if (tgt_image_opts && !skip_create) {
2172 error_report("--target-image-opts requires use of -n flag");
2173 goto fail_getopt;
2174 }
2175
9fd77f99
PL
2176 s.src_num = argc - optind - 1;
2177 out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;
f58c7b35 2178
2dc8328b 2179 if (options && has_help_option(options)) {
305b4c60
DB
2180 if (out_fmt) {
2181 ret = print_block_option_help(out_filename, out_fmt);
2182 goto fail_getopt;
2183 } else {
2184 error_report("Option help requires a format be specified");
2185 goto fail_getopt;
2186 }
4ac8aacd
JS
2187 }
2188
9fd77f99
PL
2189 if (s.src_num < 1) {
2190 error_report("Must specify image file name");
2191 goto fail_getopt;
a283cb6e
KW
2192 }
2193
2194
9fd77f99 2195 /* ret is still -EINVAL until here */
ce099547 2196 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
40055951
HR
2197 if (ret < 0) {
2198 error_report("Invalid source cache option: %s", src_cache);
9fd77f99 2199 goto fail_getopt;
40055951
HR
2200 }
2201
9fd77f99
PL
2202 /* Initialize before goto out */
2203 if (quiet) {
2204 progress = false;
2205 }
2206 qemu_progress_init(progress, 1.0);
6b837bc4
JS
2207 qemu_progress_print(0, 100);
2208
9fd77f99
PL
2209 s.src = g_new0(BlockBackend *, s.src_num);
2210 s.src_sectors = g_new(int64_t, s.src_num);
926c2d23 2211
9fd77f99
PL
2212 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2213 s.src[bs_i] = img_open(image_opts, argv[optind + bs_i],
335e9937
FZ
2214 fmt, src_flags, src_writethrough, quiet,
2215 force_share);
9fd77f99 2216 if (!s.src[bs_i]) {
c2abccec
MK
2217 ret = -1;
2218 goto out;
2219 }
9fd77f99
PL
2220 s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]);
2221 if (s.src_sectors[bs_i] < 0) {
52bf1e72 2222 error_report("Could not get size of %s: %s",
9fd77f99 2223 argv[optind + bs_i], strerror(-s.src_sectors[bs_i]));
52bf1e72
MA
2224 ret = -1;
2225 goto out;
2226 }
9fd77f99 2227 s.total_sectors += s.src_sectors[bs_i];
926c2d23 2228 }
ea2384d3 2229
ef80654d 2230 if (sn_opts) {
9fd77f99 2231 bdrv_snapshot_load_tmp(blk_bs(s.src[0]),
10d6eda1
PM
2232 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
2233 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
2234 &local_err);
ef80654d 2235 } else if (snapshot_name != NULL) {
9fd77f99 2236 if (s.src_num > 1) {
6daf194d 2237 error_report("No support for concatenating multiple snapshot");
51ef6727 2238 ret = -1;
2239 goto out;
2240 }
7b4c4781 2241
9fd77f99
PL
2242 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name,
2243 &local_err);
ef80654d 2244 }
84d18f06 2245 if (local_err) {
c29b77f9 2246 error_reportf_err(local_err, "Failed to load snapshot: ");
ef80654d
WX
2247 ret = -1;
2248 goto out;
51ef6727 2249 }
2250
305b4c60
DB
2251 if (!skip_create) {
2252 /* Find driver and parse its options */
2253 drv = bdrv_find_format(out_fmt);
2254 if (!drv) {
2255 error_report("Unknown file format '%s'", out_fmt);
2256 ret = -1;
2257 goto out;
2258 }
efa84d43 2259
305b4c60
DB
2260 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
2261 if (!proto_drv) {
2262 error_report_err(local_err);
2263 ret = -1;
2264 goto out;
2265 }
b50cbabc 2266
2e024cde
HR
2267 if (!drv->create_opts) {
2268 error_report("Format driver '%s' does not support image creation",
2269 drv->format_name);
2270 ret = -1;
2271 goto out;
2272 }
f75613cf 2273
2e024cde
HR
2274 if (!proto_drv->create_opts) {
2275 error_report("Protocol driver '%s' does not support image creation",
2276 proto_drv->format_name);
2277 ret = -1;
2278 goto out;
2279 }
f75613cf 2280
2e024cde
HR
2281 create_opts = qemu_opts_append(create_opts, drv->create_opts);
2282 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
db08adf5 2283
2e024cde 2284 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
dc523cd3
MA
2285 if (options) {
2286 qemu_opts_do_parse(opts, options, NULL, &local_err);
2287 if (local_err) {
97a2ca7a 2288 error_report_err(local_err);
dc523cd3
MA
2289 ret = -1;
2290 goto out;
2291 }
2e024cde 2292 }
efa84d43 2293
9fd77f99 2294 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s.total_sectors * 512,
39101f25 2295 &error_abort);
2e024cde
HR
2296 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
2297 if (ret < 0) {
2298 goto out;
2299 }
c2abccec 2300 }
efa84d43 2301
a18953fb 2302 /* Get backing file name if -o backing_file was used */
83d0521a 2303 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
a18953fb 2304 if (out_baseimg_param) {
83d0521a 2305 out_baseimg = out_baseimg_param;
a18953fb 2306 }
9fd77f99 2307 s.target_has_backing = (bool) out_baseimg;
a18953fb 2308
48758a84
HR
2309 if (s.src_num > 1 && out_baseimg) {
2310 error_report("Having a backing file for the target makes no sense when "
2311 "concatenating multiple input images");
2312 ret = -1;
2313 goto out;
2314 }
2315
efa84d43 2316 /* Check if compression is supported */
9fd77f99 2317 if (s.compressed) {
83d0521a
CL
2318 bool encryption =
2319 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
0cb8d47b
DB
2320 const char *encryptfmt =
2321 qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT);
83d0521a
CL
2322 const char *preallocation =
2323 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
efa84d43 2324
305b4c60 2325 if (drv && !drv->bdrv_co_pwritev_compressed) {
15654a6d 2326 error_report("Compression not supported for this file format");
c2abccec
MK
2327 ret = -1;
2328 goto out;
efa84d43
KW
2329 }
2330
0cb8d47b 2331 if (encryption || encryptfmt) {
15654a6d
JS
2332 error_report("Compression and encryption not supported at "
2333 "the same time");
c2abccec
MK
2334 ret = -1;
2335 goto out;
efa84d43 2336 }
41521fa4 2337
83d0521a
CL
2338 if (preallocation
2339 && strcmp(preallocation, "off"))
41521fa4
KW
2340 {
2341 error_report("Compression and preallocation not supported at "
2342 "the same time");
2343 ret = -1;
2344 goto out;
2345 }
efa84d43
KW
2346 }
2347
8d65a3cc
DB
2348 /*
2349 * The later open call will need any decryption secrets, and
2350 * bdrv_create() will purge "opts", so extract them now before
2351 * they are lost.
2352 */
2353 if (!skip_create) {
2354 open_opts = qdict_new();
2355 qemu_opt_foreach(opts, img_add_key_secrets, open_opts, &error_abort);
2356 }
2357
b2e10493
AD
2358 if (!skip_create) {
2359 /* Create the new image */
c282e1fd 2360 ret = bdrv_create(drv, out_filename, opts, &local_err);
b2e10493 2361 if (ret < 0) {
c29b77f9
MA
2362 error_reportf_err(local_err, "%s: error while converting %s: ",
2363 out_filename, out_fmt);
b2e10493 2364 goto out;
ea2384d3
FB
2365 }
2366 }
3b46e624 2367
9fd77f99 2368 flags = s.min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
ce099547 2369 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
661a0f71
FS
2370 if (ret < 0) {
2371 error_report("Invalid cache option: %s", cache);
bb9cd2ee 2372 goto out;
661a0f71
FS
2373 }
2374
305b4c60
DB
2375 if (skip_create) {
2376 s.target = img_open(tgt_image_opts, out_filename, out_fmt,
2377 flags, writethrough, quiet, false);
2378 } else {
2379 /* TODO ultimately we should allow --target-image-opts
2380 * to be used even when -n is not given.
2381 * That has to wait for bdrv_create to be improved
2382 * to allow filenames in option syntax
2383 */
8d65a3cc
DB
2384 s.target = img_open_file(out_filename, open_opts, out_fmt,
2385 flags, writethrough, quiet, false);
2386 open_opts = NULL; /* blk_new_open will have freed it */
305b4c60 2387 }
9fd77f99 2388 if (!s.target) {
c2abccec
MK
2389 ret = -1;
2390 goto out;
2391 }
9fd77f99 2392 out_bs = blk_bs(s.target);
ea2384d3 2393
305b4c60
DB
2394 if (s.compressed && !out_bs->drv->bdrv_co_pwritev_compressed) {
2395 error_report("Compression not supported for this file format");
2396 ret = -1;
2397 goto out;
2398 }
2399
5def6b80 2400 /* increase bufsectors from the default 4096 (2M) if opt_transfer
6360ab27
PL
2401 * or discard_alignment of the out_bs is greater. Limit to
2402 * MAX_BUF_SECTORS as maximum which is currently 32768 (16MB). */
2403 s.buf_sectors = MIN(MAX_BUF_SECTORS,
9fd77f99
PL
2404 MAX(s.buf_sectors,
2405 MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS,
2406 out_bs->bl.pdiscard_alignment >>
2407 BDRV_SECTOR_BITS)));
f2521c90 2408
8dcd3c9b
PL
2409 /* try to align the write requests to the destination to avoid unnecessary
2410 * RMW cycles. */
2411 s.alignment = MAX(pow2floor(s.min_sparse),
2412 DIV_ROUND_UP(out_bs->bl.request_alignment,
2413 BDRV_SECTOR_SIZE));
2414 assert(is_power_of_2(s.alignment));
2415
b2e10493 2416 if (skip_create) {
9fd77f99 2417 int64_t output_sectors = blk_nb_sectors(s.target);
43716fa8 2418 if (output_sectors < 0) {
eec5eb42 2419 error_report("unable to get output image length: %s",
43716fa8 2420 strerror(-output_sectors));
b2e10493
AD
2421 ret = -1;
2422 goto out;
9fd77f99 2423 } else if (output_sectors < s.total_sectors) {
b2e10493
AD
2424 error_report("output file is smaller than input file");
2425 ret = -1;
2426 goto out;
2427 }
2428 }
2429
351c8eff
HR
2430 if (s.target_has_backing) {
2431 /* Errors are treated as "backing length unknown" (which means
2432 * s.target_backing_sectors has to be negative, which it will
2433 * be automatically). The backing file length is used only
2434 * for optimizations, so such a case is not fatal. */
2435 s.target_backing_sectors = bdrv_nb_sectors(out_bs->backing->bs);
2436 } else {
2437 s.target_backing_sectors = -1;
2438 }
2439
24f833cd
PL
2440 ret = bdrv_get_info(out_bs, &bdi);
2441 if (ret < 0) {
9fd77f99 2442 if (s.compressed) {
15654a6d 2443 error_report("could not get block driver info");
c2abccec
MK
2444 goto out;
2445 }
24f833cd 2446 } else {
9fd77f99
PL
2447 s.compressed = s.compressed || bdi.needs_compressed_writes;
2448 s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
351c8eff 2449 s.unallocated_blocks_are_zero = bdi.unallocated_blocks_are_zero;
9fd77f99 2450 }
802c3d4c 2451
9fd77f99 2452 ret = convert_do_copy(&s);
c2abccec 2453out:
13c28af8
PL
2454 if (!ret) {
2455 qemu_progress_print(100, 0);
2456 }
6b837bc4 2457 qemu_progress_end();
83d0521a
CL
2458 qemu_opts_del(opts);
2459 qemu_opts_free(create_opts);
fbf28a43 2460 qemu_opts_del(sn_opts);
8d65a3cc 2461 qobject_unref(open_opts);
9fd77f99
PL
2462 blk_unref(s.target);
2463 if (s.src) {
2464 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2465 blk_unref(s.src[bs_i]);
26f54e9a 2466 }
9fd77f99 2467 g_free(s.src);
26f54e9a 2468 }
9fd77f99 2469 g_free(s.src_sectors);
64bb01aa
KW
2470fail_getopt:
2471 g_free(options);
2472
9fd77f99 2473 return !!ret;
ea2384d3
FB
2474}
2475
57d1a2b6 2476
faea38e7
FB
2477static void dump_snapshots(BlockDriverState *bs)
2478{
2479 QEMUSnapshotInfo *sn_tab, *sn;
2480 int nb_sns, i;
faea38e7
FB
2481
2482 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2483 if (nb_sns <= 0)
2484 return;
2485 printf("Snapshot list:\n");
5b917044
WX
2486 bdrv_snapshot_dump(fprintf, stdout, NULL);
2487 printf("\n");
faea38e7
FB
2488 for(i = 0; i < nb_sns; i++) {
2489 sn = &sn_tab[i];
5b917044
WX
2490 bdrv_snapshot_dump(fprintf, stdout, sn);
2491 printf("\n");
faea38e7 2492 }
7267c094 2493 g_free(sn_tab);
faea38e7
FB
2494}
2495
9699bf0d
SH
2496static void dump_json_image_info_list(ImageInfoList *list)
2497{
9699bf0d 2498 QString *str;
9699bf0d 2499 QObject *obj;
7d5e199a 2500 Visitor *v = qobject_output_visitor_new(&obj);
3b098d56
EB
2501
2502 visit_type_ImageInfoList(v, NULL, &list, &error_abort);
2503 visit_complete(v, &obj);
9699bf0d
SH
2504 str = qobject_to_json_pretty(obj);
2505 assert(str != NULL);
2506 printf("%s\n", qstring_get_str(str));
cb3e7f08 2507 qobject_unref(obj);
3b098d56 2508 visit_free(v);
cb3e7f08 2509 qobject_unref(str);
9699bf0d
SH
2510}
2511
c054b3fd
BC
2512static void dump_json_image_info(ImageInfo *info)
2513{
c054b3fd 2514 QString *str;
c054b3fd 2515 QObject *obj;
7d5e199a 2516 Visitor *v = qobject_output_visitor_new(&obj);
3b098d56
EB
2517
2518 visit_type_ImageInfo(v, NULL, &info, &error_abort);
2519 visit_complete(v, &obj);
c054b3fd
BC
2520 str = qobject_to_json_pretty(obj);
2521 assert(str != NULL);
2522 printf("%s\n", qstring_get_str(str));
cb3e7f08 2523 qobject_unref(obj);
3b098d56 2524 visit_free(v);
cb3e7f08 2525 qobject_unref(str);
c054b3fd
BC
2526}
2527
9699bf0d
SH
2528static void dump_human_image_info_list(ImageInfoList *list)
2529{
2530 ImageInfoList *elem;
2531 bool delim = false;
2532
2533 for (elem = list; elem; elem = elem->next) {
2534 if (delim) {
2535 printf("\n");
2536 }
2537 delim = true;
2538
5b917044 2539 bdrv_image_info_dump(fprintf, stdout, elem->value);
9699bf0d
SH
2540 }
2541}
2542
2543static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2544{
2545 return strcmp(a, b) == 0;
2546}
2547
2548/**
2549 * Open an image file chain and return an ImageInfoList
2550 *
2551 * @filename: topmost image filename
2552 * @fmt: topmost image format (may be NULL to autodetect)
2553 * @chain: true - enumerate entire backing file chain
2554 * false - only topmost image file
2555 *
2556 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2557 * image file. If there was an error a message will have been printed to
2558 * stderr.
2559 */
eb769f74
DB
2560static ImageInfoList *collect_image_info_list(bool image_opts,
2561 const char *filename,
9699bf0d 2562 const char *fmt,
335e9937 2563 bool chain, bool force_share)
9699bf0d
SH
2564{
2565 ImageInfoList *head = NULL;
2566 ImageInfoList **last = &head;
2567 GHashTable *filenames;
43526ec8 2568 Error *err = NULL;
9699bf0d
SH
2569
2570 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2571
2572 while (filename) {
26f54e9a 2573 BlockBackend *blk;
9699bf0d
SH
2574 BlockDriverState *bs;
2575 ImageInfo *info;
2576 ImageInfoList *elem;
2577
2578 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2579 error_report("Backing file '%s' creates an infinite loop.",
2580 filename);
2581 goto err;
2582 }
2583 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2584
efaa7c4e 2585 blk = img_open(image_opts, filename, fmt,
335e9937
FZ
2586 BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false,
2587 force_share);
7e7d56d9 2588 if (!blk) {
9699bf0d
SH
2589 goto err;
2590 }
7e7d56d9 2591 bs = blk_bs(blk);
9699bf0d 2592
43526ec8 2593 bdrv_query_image_info(bs, &info, &err);
84d18f06 2594 if (err) {
565f65d2 2595 error_report_err(err);
26f54e9a 2596 blk_unref(blk);
43526ec8 2597 goto err;
fb0ed453 2598 }
9699bf0d
SH
2599
2600 elem = g_new0(ImageInfoList, 1);
2601 elem->value = info;
2602 *last = elem;
2603 last = &elem->next;
2604
26f54e9a 2605 blk_unref(blk);
9699bf0d
SH
2606
2607 filename = fmt = NULL;
2608 if (chain) {
2609 if (info->has_full_backing_filename) {
2610 filename = info->full_backing_filename;
2611 } else if (info->has_backing_filename) {
92d617ab
JS
2612 error_report("Could not determine absolute backing filename,"
2613 " but backing filename '%s' present",
2614 info->backing_filename);
2615 goto err;
9699bf0d
SH
2616 }
2617 if (info->has_backing_filename_format) {
2618 fmt = info->backing_filename_format;
2619 }
2620 }
2621 }
2622 g_hash_table_destroy(filenames);
2623 return head;
2624
2625err:
2626 qapi_free_ImageInfoList(head);
2627 g_hash_table_destroy(filenames);
2628 return NULL;
2629}
2630
c054b3fd
BC
2631static int img_info(int argc, char **argv)
2632{
2633 int c;
2634 OutputFormat output_format = OFORMAT_HUMAN;
9699bf0d 2635 bool chain = false;
c054b3fd 2636 const char *filename, *fmt, *output;
9699bf0d 2637 ImageInfoList *list;
eb769f74 2638 bool image_opts = false;
335e9937 2639 bool force_share = false;
c054b3fd 2640
ea2384d3 2641 fmt = NULL;
c054b3fd 2642 output = NULL;
ea2384d3 2643 for(;;) {
c054b3fd
BC
2644 int option_index = 0;
2645 static const struct option long_options[] = {
2646 {"help", no_argument, 0, 'h'},
2647 {"format", required_argument, 0, 'f'},
2648 {"output", required_argument, 0, OPTION_OUTPUT},
9699bf0d 2649 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
3babeb15 2650 {"object", required_argument, 0, OPTION_OBJECT},
eb769f74 2651 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
335e9937 2652 {"force-share", no_argument, 0, 'U'},
c054b3fd
BC
2653 {0, 0, 0, 0}
2654 };
335e9937 2655 c = getopt_long(argc, argv, ":f:hU",
c054b3fd 2656 long_options, &option_index);
b8fb60da 2657 if (c == -1) {
ea2384d3 2658 break;
b8fb60da 2659 }
ea2384d3 2660 switch(c) {
c9192973
SH
2661 case ':':
2662 missing_argument(argv[optind - 1]);
2663 break;
ef87394c 2664 case '?':
c9192973
SH
2665 unrecognized_option(argv[optind - 1]);
2666 break;
ea2384d3
FB
2667 case 'h':
2668 help();
2669 break;
2670 case 'f':
2671 fmt = optarg;
2672 break;
335e9937
FZ
2673 case 'U':
2674 force_share = true;
2675 break;
c054b3fd
BC
2676 case OPTION_OUTPUT:
2677 output = optarg;
2678 break;
9699bf0d
SH
2679 case OPTION_BACKING_CHAIN:
2680 chain = true;
2681 break;
3babeb15
DB
2682 case OPTION_OBJECT: {
2683 QemuOpts *opts;
2684 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2685 optarg, true);
2686 if (!opts) {
2687 return 1;
2688 }
2689 } break;
eb769f74
DB
2690 case OPTION_IMAGE_OPTS:
2691 image_opts = true;
2692 break;
ea2384d3
FB
2693 }
2694 }
fc11eb26 2695 if (optind != argc - 1) {
ac1307ab 2696 error_exit("Expecting one image file name");
b8fb60da 2697 }
ea2384d3
FB
2698 filename = argv[optind++];
2699
c054b3fd
BC
2700 if (output && !strcmp(output, "json")) {
2701 output_format = OFORMAT_JSON;
2702 } else if (output && !strcmp(output, "human")) {
2703 output_format = OFORMAT_HUMAN;
2704 } else if (output) {
2705 error_report("--output must be used with human or json as argument.");
c2abccec
MK
2706 return 1;
2707 }
c054b3fd 2708
3babeb15
DB
2709 if (qemu_opts_foreach(&qemu_object_opts,
2710 user_creatable_add_opts_foreach,
334c43e2 2711 NULL, &error_fatal)) {
3babeb15
DB
2712 return 1;
2713 }
2714
335e9937
FZ
2715 list = collect_image_info_list(image_opts, filename, fmt, chain,
2716 force_share);
9699bf0d 2717 if (!list) {
c2abccec 2718 return 1;
faea38e7 2719 }
c054b3fd 2720
c054b3fd
BC
2721 switch (output_format) {
2722 case OFORMAT_HUMAN:
9699bf0d 2723 dump_human_image_info_list(list);
c054b3fd
BC
2724 break;
2725 case OFORMAT_JSON:
9699bf0d
SH
2726 if (chain) {
2727 dump_json_image_info_list(list);
2728 } else {
2729 dump_json_image_info(list->value);
2730 }
c054b3fd 2731 break;
faea38e7 2732 }
c054b3fd 2733
9699bf0d 2734 qapi_free_ImageInfoList(list);
ea2384d3
FB
2735 return 0;
2736}
2737
30065d14
EB
2738static int dump_map_entry(OutputFormat output_format, MapEntry *e,
2739 MapEntry *next)
4c93a13b
PB
2740{
2741 switch (output_format) {
2742 case OFORMAT_HUMAN:
16b0d555 2743 if (e->data && !e->has_offset) {
4c93a13b 2744 error_report("File contains external, encrypted or compressed clusters.");
30065d14 2745 return -1;
4c93a13b 2746 }
16b0d555 2747 if (e->data && !e->zero) {
4c93a13b 2748 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
16b0d555
FZ
2749 e->start, e->length,
2750 e->has_offset ? e->offset : 0,
2751 e->has_filename ? e->filename : "");
4c93a13b
PB
2752 }
2753 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2754 * Modify the flags here to allow more coalescing.
2755 */
16b0d555
FZ
2756 if (next && (!next->data || next->zero)) {
2757 next->data = false;
2758 next->zero = true;
4c93a13b
PB
2759 }
2760 break;
2761 case OFORMAT_JSON:
16b0d555
FZ
2762 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2763 " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
4c93a13b
PB
2764 (e->start == 0 ? "[" : ",\n"),
2765 e->start, e->length, e->depth,
16b0d555
FZ
2766 e->zero ? "true" : "false",
2767 e->data ? "true" : "false");
2768 if (e->has_offset) {
c745bfb4 2769 printf(", \"offset\": %"PRId64"", e->offset);
4c93a13b
PB
2770 }
2771 putchar('}');
2772
2773 if (!next) {
2774 printf("]\n");
2775 }
2776 break;
2777 }
30065d14 2778 return 0;
4c93a13b
PB
2779}
2780
5e344dd8
EB
2781static int get_block_status(BlockDriverState *bs, int64_t offset,
2782 int64_t bytes, MapEntry *e)
4c93a13b 2783{
237d78f8 2784 int ret;
4c93a13b 2785 int depth;
67a0fd2a 2786 BlockDriverState *file;
2875645b 2787 bool has_offset;
237d78f8 2788 int64_t map;
f30c66ba 2789 char *filename = NULL;
4c93a13b
PB
2790
2791 /* As an optimization, we could cache the current range of unallocated
2792 * clusters in each file of the chain, and avoid querying the same
2793 * range repeatedly.
2794 */
2795
2796 depth = 0;
2797 for (;;) {
237d78f8 2798 ret = bdrv_block_status(bs, offset, bytes, &bytes, &map, &file);
4c93a13b
PB
2799 if (ret < 0) {
2800 return ret;
2801 }
237d78f8 2802 assert(bytes);
4c93a13b
PB
2803 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2804 break;
2805 }
760e0063 2806 bs = backing_bs(bs);
4c93a13b
PB
2807 if (bs == NULL) {
2808 ret = 0;
2809 break;
2810 }
2811
2812 depth++;
2813 }
2814
2875645b
JS
2815 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2816
f30c66ba
HR
2817 if (file && has_offset) {
2818 bdrv_refresh_filename(file);
2819 filename = file->filename;
2820 }
2821
2875645b 2822 *e = (MapEntry) {
5e344dd8 2823 .start = offset,
237d78f8 2824 .length = bytes,
2875645b
JS
2825 .data = !!(ret & BDRV_BLOCK_DATA),
2826 .zero = !!(ret & BDRV_BLOCK_ZERO),
237d78f8 2827 .offset = map,
2875645b
JS
2828 .has_offset = has_offset,
2829 .depth = depth,
f30c66ba
HR
2830 .has_filename = filename,
2831 .filename = filename,
2875645b
JS
2832 };
2833
4c93a13b
PB
2834 return 0;
2835}
2836
16b0d555
FZ
2837static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2838{
2839 if (curr->length == 0) {
2840 return false;
2841 }
2842 if (curr->zero != next->zero ||
2843 curr->data != next->data ||
2844 curr->depth != next->depth ||
2845 curr->has_filename != next->has_filename ||
2846 curr->has_offset != next->has_offset) {
2847 return false;
2848 }
2849 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2850 return false;
2851 }
2852 if (curr->has_offset && curr->offset + curr->length != next->offset) {
2853 return false;
2854 }
2855 return true;
2856}
2857
4c93a13b
PB
2858static int img_map(int argc, char **argv)
2859{
2860 int c;
2861 OutputFormat output_format = OFORMAT_HUMAN;
26f54e9a 2862 BlockBackend *blk;
4c93a13b
PB
2863 BlockDriverState *bs;
2864 const char *filename, *fmt, *output;
2865 int64_t length;
2866 MapEntry curr = { .length = 0 }, next;
2867 int ret = 0;
eb769f74 2868 bool image_opts = false;
335e9937 2869 bool force_share = false;
4c93a13b
PB
2870
2871 fmt = NULL;
2872 output = NULL;
2873 for (;;) {
2874 int option_index = 0;
2875 static const struct option long_options[] = {
2876 {"help", no_argument, 0, 'h'},
2877 {"format", required_argument, 0, 'f'},
2878 {"output", required_argument, 0, OPTION_OUTPUT},
3babeb15 2879 {"object", required_argument, 0, OPTION_OBJECT},
eb769f74 2880 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
335e9937 2881 {"force-share", no_argument, 0, 'U'},
4c93a13b
PB
2882 {0, 0, 0, 0}
2883 };
335e9937 2884 c = getopt_long(argc, argv, ":f:hU",
4c93a13b
PB
2885 long_options, &option_index);
2886 if (c == -1) {
2887 break;
2888 }
2889 switch (c) {
c9192973
SH
2890 case ':':
2891 missing_argument(argv[optind - 1]);
2892 break;
4c93a13b 2893 case '?':
c9192973
SH
2894 unrecognized_option(argv[optind - 1]);
2895 break;
4c93a13b
PB
2896 case 'h':
2897 help();
2898 break;
2899 case 'f':
2900 fmt = optarg;
2901 break;
335e9937
FZ
2902 case 'U':
2903 force_share = true;
2904 break;
4c93a13b
PB
2905 case OPTION_OUTPUT:
2906 output = optarg;
2907 break;
3babeb15
DB
2908 case OPTION_OBJECT: {
2909 QemuOpts *opts;
2910 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2911 optarg, true);
2912 if (!opts) {
2913 return 1;
2914 }
2915 } break;
eb769f74
DB
2916 case OPTION_IMAGE_OPTS:
2917 image_opts = true;
2918 break;
4c93a13b
PB
2919 }
2920 }
ac1307ab
FZ
2921 if (optind != argc - 1) {
2922 error_exit("Expecting one image file name");
4c93a13b 2923 }
ac1307ab 2924 filename = argv[optind];
4c93a13b
PB
2925
2926 if (output && !strcmp(output, "json")) {
2927 output_format = OFORMAT_JSON;
2928 } else if (output && !strcmp(output, "human")) {
2929 output_format = OFORMAT_HUMAN;
2930 } else if (output) {
2931 error_report("--output must be used with human or json as argument.");
2932 return 1;
2933 }
2934
3babeb15
DB
2935 if (qemu_opts_foreach(&qemu_object_opts,
2936 user_creatable_add_opts_foreach,
334c43e2 2937 NULL, &error_fatal)) {
3babeb15
DB
2938 return 1;
2939 }
2940
335e9937 2941 blk = img_open(image_opts, filename, fmt, 0, false, false, force_share);
7e7d56d9
MA
2942 if (!blk) {
2943 return 1;
4c93a13b 2944 }
7e7d56d9 2945 bs = blk_bs(blk);
4c93a13b
PB
2946
2947 if (output_format == OFORMAT_HUMAN) {
2948 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2949 }
2950
f1d3cd79 2951 length = blk_getlength(blk);
4c93a13b 2952 while (curr.start + curr.length < length) {
5e344dd8
EB
2953 int64_t offset = curr.start + curr.length;
2954 int64_t n;
4c93a13b
PB
2955
2956 /* Probe up to 1 GiB at a time. */
e0b371ed 2957 n = MIN(1 << 30, length - offset);
5e344dd8 2958 ret = get_block_status(bs, offset, n, &next);
4c93a13b
PB
2959
2960 if (ret < 0) {
2961 error_report("Could not read file metadata: %s", strerror(-ret));
2962 goto out;
2963 }
2964
16b0d555 2965 if (entry_mergeable(&curr, &next)) {
4c93a13b
PB
2966 curr.length += next.length;
2967 continue;
2968 }
2969
2970 if (curr.length > 0) {
30065d14
EB
2971 ret = dump_map_entry(output_format, &curr, &next);
2972 if (ret < 0) {
2973 goto out;
2974 }
4c93a13b
PB
2975 }
2976 curr = next;
2977 }
2978
30065d14 2979 ret = dump_map_entry(output_format, &curr, NULL);
4c93a13b
PB
2980
2981out:
26f54e9a 2982 blk_unref(blk);
4c93a13b
PB
2983 return ret < 0;
2984}
2985
f7b4a940
AL
2986#define SNAPSHOT_LIST 1
2987#define SNAPSHOT_CREATE 2
2988#define SNAPSHOT_APPLY 3
2989#define SNAPSHOT_DELETE 4
2990
153859be 2991static int img_snapshot(int argc, char **argv)
f7b4a940 2992{
26f54e9a 2993 BlockBackend *blk;
f7b4a940
AL
2994 BlockDriverState *bs;
2995 QEMUSnapshotInfo sn;
2996 char *filename, *snapshot_name = NULL;
c2abccec 2997 int c, ret = 0, bdrv_oflags;
f7b4a940
AL
2998 int action = 0;
2999 qemu_timeval tv;
f382d43a 3000 bool quiet = false;
a89d89d3 3001 Error *err = NULL;
eb769f74 3002 bool image_opts = false;
335e9937 3003 bool force_share = false;
f7b4a940 3004
ce099547 3005 bdrv_oflags = BDRV_O_RDWR;
f7b4a940
AL
3006 /* Parse commandline parameters */
3007 for(;;) {
3babeb15
DB
3008 static const struct option long_options[] = {
3009 {"help", no_argument, 0, 'h'},
3010 {"object", required_argument, 0, OPTION_OBJECT},
eb769f74 3011 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
335e9937 3012 {"force-share", no_argument, 0, 'U'},
3babeb15
DB
3013 {0, 0, 0, 0}
3014 };
335e9937 3015 c = getopt_long(argc, argv, ":la:c:d:hqU",
3babeb15 3016 long_options, NULL);
b8fb60da 3017 if (c == -1) {
f7b4a940 3018 break;
b8fb60da 3019 }
f7b4a940 3020 switch(c) {
c9192973
SH
3021 case ':':
3022 missing_argument(argv[optind - 1]);
3023 break;
ef87394c 3024 case '?':
c9192973
SH
3025 unrecognized_option(argv[optind - 1]);
3026 break;
f7b4a940
AL
3027 case 'h':
3028 help();
153859be 3029 return 0;
f7b4a940
AL
3030 case 'l':
3031 if (action) {
ac1307ab 3032 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
153859be 3033 return 0;
f7b4a940
AL
3034 }
3035 action = SNAPSHOT_LIST;
f5edb014 3036 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
f7b4a940
AL
3037 break;
3038 case 'a':
3039 if (action) {
ac1307ab 3040 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
153859be 3041 return 0;
f7b4a940
AL
3042 }
3043 action = SNAPSHOT_APPLY;
3044 snapshot_name = optarg;
3045 break;
3046 case 'c':
3047 if (action) {
ac1307ab 3048 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
153859be 3049 return 0;
f7b4a940
AL
3050 }
3051 action = SNAPSHOT_CREATE;
3052 snapshot_name = optarg;
3053 break;
3054 case 'd':
3055 if (action) {
ac1307ab 3056 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
153859be 3057 return 0;
f7b4a940
AL
3058 }
3059 action = SNAPSHOT_DELETE;
3060 snapshot_name = optarg;
3061 break;
f382d43a
MR
3062 case 'q':
3063 quiet = true;
3064 break;
335e9937
FZ
3065 case 'U':
3066 force_share = true;
3067 break;
3babeb15
DB
3068 case OPTION_OBJECT: {
3069 QemuOpts *opts;
3070 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3071 optarg, true);
3072 if (!opts) {
3073 return 1;
3074 }
3075 } break;
eb769f74
DB
3076 case OPTION_IMAGE_OPTS:
3077 image_opts = true;
3078 break;
f7b4a940
AL
3079 }
3080 }
3081
fc11eb26 3082 if (optind != argc - 1) {
ac1307ab 3083 error_exit("Expecting one image file name");
b8fb60da 3084 }
f7b4a940
AL
3085 filename = argv[optind++];
3086
3babeb15
DB
3087 if (qemu_opts_foreach(&qemu_object_opts,
3088 user_creatable_add_opts_foreach,
334c43e2 3089 NULL, &error_fatal)) {
3babeb15
DB
3090 return 1;
3091 }
3092
f7b4a940 3093 /* Open the image */
335e9937
FZ
3094 blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet,
3095 force_share);
7e7d56d9
MA
3096 if (!blk) {
3097 return 1;
c2abccec 3098 }
7e7d56d9 3099 bs = blk_bs(blk);
f7b4a940
AL
3100
3101 /* Perform the requested action */
3102 switch(action) {
3103 case SNAPSHOT_LIST:
3104 dump_snapshots(bs);
3105 break;
3106
3107 case SNAPSHOT_CREATE:
3108 memset(&sn, 0, sizeof(sn));
3109 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
3110
3111 qemu_gettimeofday(&tv);
3112 sn.date_sec = tv.tv_sec;
3113 sn.date_nsec = tv.tv_usec * 1000;
3114
3115 ret = bdrv_snapshot_create(bs, &sn);
b8fb60da 3116 if (ret) {
15654a6d 3117 error_report("Could not create snapshot '%s': %d (%s)",
f7b4a940 3118 snapshot_name, ret, strerror(-ret));
b8fb60da 3119 }
f7b4a940
AL
3120 break;
3121
3122 case SNAPSHOT_APPLY:
0b62bcbc 3123 ret = bdrv_snapshot_goto(bs, snapshot_name, &err);
b8fb60da 3124 if (ret) {
0b62bcbc
KW
3125 error_reportf_err(err, "Could not apply snapshot '%s': ",
3126 snapshot_name);
b8fb60da 3127 }
f7b4a940
AL
3128 break;
3129
3130 case SNAPSHOT_DELETE:
8c04093c
DHB
3131 ret = bdrv_snapshot_find(bs, &sn, snapshot_name);
3132 if (ret < 0) {
3133 error_report("Could not delete snapshot '%s': snapshot not "
3134 "found", snapshot_name);
a89d89d3 3135 ret = 1;
8c04093c
DHB
3136 } else {
3137 ret = bdrv_snapshot_delete(bs, sn.id_str, sn.name, &err);
3138 if (ret < 0) {
3139 error_reportf_err(err, "Could not delete snapshot '%s': ",
3140 snapshot_name);
3141 ret = 1;
3142 }
b8fb60da 3143 }
f7b4a940
AL
3144 break;
3145 }
3146
3147 /* Cleanup */
26f54e9a 3148 blk_unref(blk);
c2abccec
MK
3149 if (ret) {
3150 return 1;
3151 }
153859be 3152 return 0;
f7b4a940
AL
3153}
3154
3e85c6fd
KW
3155static int img_rebase(int argc, char **argv)
3156{
26f54e9a 3157 BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
396374ca
PB
3158 uint8_t *buf_old = NULL;
3159 uint8_t *buf_new = NULL;
f1d3cd79 3160 BlockDriverState *bs = NULL;
3e85c6fd 3161 char *filename;
40055951
HR
3162 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
3163 int c, flags, src_flags, ret;
ce099547 3164 bool writethrough, src_writethrough;
3e85c6fd 3165 int unsafe = 0;
335e9937 3166 bool force_share = false;
6b837bc4 3167 int progress = 0;
f382d43a 3168 bool quiet = false;
34b5d2c6 3169 Error *local_err = NULL;
eb769f74 3170 bool image_opts = false;
3e85c6fd
KW
3171
3172 /* Parse commandline parameters */
e53dbee0 3173 fmt = NULL;
661a0f71 3174 cache = BDRV_DEFAULT_CACHE;
40055951 3175 src_cache = BDRV_DEFAULT_CACHE;
3e85c6fd
KW
3176 out_baseimg = NULL;
3177 out_basefmt = NULL;
3e85c6fd 3178 for(;;) {
3babeb15
DB
3179 static const struct option long_options[] = {
3180 {"help", no_argument, 0, 'h'},
3181 {"object", required_argument, 0, OPTION_OBJECT},
eb769f74 3182 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
335e9937 3183 {"force-share", no_argument, 0, 'U'},
3babeb15
DB
3184 {0, 0, 0, 0}
3185 };
335e9937 3186 c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU",
3babeb15 3187 long_options, NULL);
b8fb60da 3188 if (c == -1) {
3e85c6fd 3189 break;
b8fb60da 3190 }
3e85c6fd 3191 switch(c) {
c9192973
SH
3192 case ':':
3193 missing_argument(argv[optind - 1]);
3194 break;
ef87394c 3195 case '?':
c9192973
SH
3196 unrecognized_option(argv[optind - 1]);
3197 break;
3e85c6fd
KW
3198 case 'h':
3199 help();
3200 return 0;
e53dbee0
KW
3201 case 'f':
3202 fmt = optarg;
3203 break;
3e85c6fd
KW
3204 case 'F':
3205 out_basefmt = optarg;
3206 break;
3207 case 'b':
3208 out_baseimg = optarg;
3209 break;
3210 case 'u':
3211 unsafe = 1;
3212 break;
6b837bc4
JS
3213 case 'p':
3214 progress = 1;
3215 break;
661a0f71
FS
3216 case 't':
3217 cache = optarg;
3218 break;
40055951
HR
3219 case 'T':
3220 src_cache = optarg;
3221 break;
f382d43a
MR
3222 case 'q':
3223 quiet = true;
3224 break;
3babeb15
DB
3225 case OPTION_OBJECT: {
3226 QemuOpts *opts;
3227 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3228 optarg, true);
3229 if (!opts) {
3230 return 1;
3231 }
3232 } break;
eb769f74
DB
3233 case OPTION_IMAGE_OPTS:
3234 image_opts = true;
3235 break;
335e9937
FZ
3236 case 'U':
3237 force_share = true;
3238 break;
3e85c6fd
KW
3239 }
3240 }
3241
f382d43a
MR
3242 if (quiet) {
3243 progress = 0;
3244 }
3245
ac1307ab
FZ
3246 if (optind != argc - 1) {
3247 error_exit("Expecting one image file name");
3248 }
3249 if (!unsafe && !out_baseimg) {
3250 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
b8fb60da 3251 }
3e85c6fd
KW
3252 filename = argv[optind++];
3253
3babeb15
DB
3254 if (qemu_opts_foreach(&qemu_object_opts,
3255 user_creatable_add_opts_foreach,
334c43e2 3256 NULL, &error_fatal)) {
3babeb15
DB
3257 return 1;
3258 }
3259
6b837bc4
JS
3260 qemu_progress_init(progress, 2.0);
3261 qemu_progress_print(0, 100);
3262
661a0f71 3263 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
ce099547 3264 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
661a0f71
FS
3265 if (ret < 0) {
3266 error_report("Invalid cache option: %s", cache);
40ed35a3 3267 goto out;
661a0f71
FS
3268 }
3269
ce099547
KW
3270 src_flags = 0;
3271 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
40055951
HR
3272 if (ret < 0) {
3273 error_report("Invalid source cache option: %s", src_cache);
40ed35a3 3274 goto out;
40055951
HR
3275 }
3276
ce099547
KW
3277 /* The source files are opened read-only, don't care about WCE */
3278 assert((src_flags & BDRV_O_RDWR) == 0);
3279 (void) src_writethrough;
3280
3e85c6fd
KW
3281 /*
3282 * Open the images.
3283 *
3284 * Ignore the old backing file for unsafe rebase in case we want to correct
3285 * the reference to a renamed or moved backing file.
3286 */
335e9937
FZ
3287 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3288 false);
7e7d56d9 3289 if (!blk) {
40ed35a3
SH
3290 ret = -1;
3291 goto out;
c2abccec 3292 }
7e7d56d9 3293 bs = blk_bs(blk);
3e85c6fd 3294
3e85c6fd 3295 if (out_basefmt != NULL) {
644483d9 3296 if (bdrv_find_format(out_basefmt) == NULL) {
15654a6d 3297 error_report("Invalid format name: '%s'", out_basefmt);
c2abccec
MK
3298 ret = -1;
3299 goto out;
3e85c6fd
KW
3300 }
3301 }
3302
3303 /* For safe rebasing we need to compare old and new backing file */
40ed35a3 3304 if (!unsafe) {
9a29e18f 3305 char backing_name[PATH_MAX];
644483d9
HR
3306 QDict *options = NULL;
3307
3308 if (bs->backing_format[0] != '\0') {
3309 options = qdict_new();
46f5ac20 3310 qdict_put_str(options, "driver", bs->backing_format);
644483d9 3311 }
3e85c6fd 3312
335e9937
FZ
3313 if (force_share) {
3314 if (!options) {
3315 options = qdict_new();
3316 }
579cf1d1 3317 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
335e9937 3318 }
3e85c6fd 3319 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
efaa7c4e 3320 blk_old_backing = blk_new_open(backing_name, NULL,
644483d9
HR
3321 options, src_flags, &local_err);
3322 if (!blk_old_backing) {
c29b77f9
MA
3323 error_reportf_err(local_err,
3324 "Could not open old backing file '%s': ",
3325 backing_name);
e84a0dd5 3326 ret = -1;
c2abccec 3327 goto out;
3e85c6fd 3328 }
644483d9 3329
a616673d 3330 if (out_baseimg[0]) {
d16699b6
HR
3331 const char *overlay_filename;
3332 char *out_real_path;
3333
335e9937 3334 options = qdict_new();
644483d9 3335 if (out_basefmt) {
46f5ac20 3336 qdict_put_str(options, "driver", out_basefmt);
335e9937
FZ
3337 }
3338 if (force_share) {
3339 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
644483d9
HR
3340 }
3341
f30c66ba 3342 bdrv_refresh_filename(bs);
d16699b6
HR
3343 overlay_filename = bs->exact_filename[0] ? bs->exact_filename
3344 : bs->filename;
645ae7d8
HR
3345 out_real_path =
3346 bdrv_get_full_backing_filename_from_filename(overlay_filename,
3347 out_baseimg,
3348 &local_err);
d16699b6
HR
3349 if (local_err) {
3350 error_reportf_err(local_err,
3351 "Could not resolve backing filename: ");
3352 ret = -1;
d16699b6
HR
3353 goto out;
3354 }
3355
3356 blk_new_backing = blk_new_open(out_real_path, NULL,
644483d9 3357 options, src_flags, &local_err);
d16699b6 3358 g_free(out_real_path);
644483d9 3359 if (!blk_new_backing) {
c29b77f9
MA
3360 error_reportf_err(local_err,
3361 "Could not open new backing file '%s': ",
3362 out_baseimg);
e84a0dd5 3363 ret = -1;
a616673d
AB
3364 goto out;
3365 }
3e85c6fd
KW
3366 }
3367 }
3368
3369 /*
3370 * Check each unallocated cluster in the COW file. If it is unallocated,
3371 * accesses go to the backing file. We must therefore compare this cluster
3372 * in the old and new backing file, and if they differ we need to copy it
3373 * from the old backing file into the COW file.
3374 *
3375 * If qemu-img crashes during this step, no harm is done. The content of
3376 * the image is the same as the original one at any time.
3377 */
3378 if (!unsafe) {
41536287
EB
3379 int64_t size;
3380 int64_t old_backing_size;
3381 int64_t new_backing_size = 0;
3382 uint64_t offset;
3383 int64_t n;
1f710495 3384 float local_progress = 0;
d6771bfa 3385
f1d3cd79
HR
3386 buf_old = blk_blockalign(blk, IO_BUF_SIZE);
3387 buf_new = blk_blockalign(blk, IO_BUF_SIZE);
3e85c6fd 3388
41536287
EB
3389 size = blk_getlength(blk);
3390 if (size < 0) {
52bf1e72 3391 error_report("Could not get size of '%s': %s",
41536287 3392 filename, strerror(-size));
52bf1e72
MA
3393 ret = -1;
3394 goto out;
3395 }
41536287
EB
3396 old_backing_size = blk_getlength(blk_old_backing);
3397 if (old_backing_size < 0) {
9a29e18f 3398 char backing_name[PATH_MAX];
52bf1e72
MA
3399
3400 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
3401 error_report("Could not get size of '%s': %s",
41536287 3402 backing_name, strerror(-old_backing_size));
52bf1e72
MA
3403 ret = -1;
3404 goto out;
3405 }
f1d3cd79 3406 if (blk_new_backing) {
41536287
EB
3407 new_backing_size = blk_getlength(blk_new_backing);
3408 if (new_backing_size < 0) {
52bf1e72 3409 error_report("Could not get size of '%s': %s",
41536287 3410 out_baseimg, strerror(-new_backing_size));
52bf1e72
MA
3411 ret = -1;
3412 goto out;
3413 }
a616673d 3414 }
3e85c6fd 3415
41536287
EB
3416 if (size != 0) {
3417 local_progress = (float)100 / (size / MIN(size, IO_BUF_SIZE));
1f710495
KW
3418 }
3419
41536287
EB
3420 for (offset = 0; offset < size; offset += n) {
3421 /* How many bytes can we handle with the next read? */
3422 n = MIN(IO_BUF_SIZE, size - offset);
3e85c6fd
KW
3423
3424 /* If the cluster is allocated, we don't need to take action */
41536287 3425 ret = bdrv_is_allocated(bs, offset, n, &n);
d663640c
PB
3426 if (ret < 0) {
3427 error_report("error while reading image metadata: %s",
3428 strerror(-ret));
3429 goto out;
3430 }
cc60e327 3431 if (ret) {
3e85c6fd
KW
3432 continue;
3433 }
3434
87a1b3e3
KW
3435 /*
3436 * Read old and new backing file and take into consideration that
3437 * backing files may be smaller than the COW image.
3438 */
41536287
EB
3439 if (offset >= old_backing_size) {
3440 memset(buf_old, 0, n);
87a1b3e3 3441 } else {
41536287
EB
3442 if (offset + n > old_backing_size) {
3443 n = old_backing_size - offset;
87a1b3e3
KW
3444 }
3445
41536287 3446 ret = blk_pread(blk_old_backing, offset, buf_old, n);
87a1b3e3
KW
3447 if (ret < 0) {
3448 error_report("error while reading from old backing file");
3449 goto out;
3450 }
3e85c6fd 3451 }
87a1b3e3 3452
41536287
EB
3453 if (offset >= new_backing_size || !blk_new_backing) {
3454 memset(buf_new, 0, n);
87a1b3e3 3455 } else {
41536287
EB
3456 if (offset + n > new_backing_size) {
3457 n = new_backing_size - offset;
87a1b3e3
KW
3458 }
3459
41536287 3460 ret = blk_pread(blk_new_backing, offset, buf_new, n);
87a1b3e3
KW
3461 if (ret < 0) {
3462 error_report("error while reading from new backing file");
3463 goto out;
3464 }
3e85c6fd
KW
3465 }
3466
3467 /* If they differ, we need to write to the COW file */
3468 uint64_t written = 0;
3469
41536287 3470 while (written < n) {
dc61cd3b 3471 int64_t pnum;
3e85c6fd 3472
41536287
EB
3473 if (compare_buffers(buf_old + written, buf_new + written,
3474 n - written, &pnum))
3e85c6fd 3475 {
41536287 3476 ret = blk_pwrite(blk, offset + written,
dc61cd3b 3477 buf_old + written, pnum, 0);
3e85c6fd 3478 if (ret < 0) {
15654a6d 3479 error_report("Error while writing to COW image: %s",
3e85c6fd 3480 strerror(-ret));
c2abccec 3481 goto out;
3e85c6fd
KW
3482 }
3483 }
3484
3485 written += pnum;
3486 }
6b837bc4 3487 qemu_progress_print(local_progress, 100);
3e85c6fd
KW
3488 }
3489 }
3490
3491 /*
3492 * Change the backing file. All clusters that are different from the old
3493 * backing file are overwritten in the COW file now, so the visible content
3494 * doesn't change when we switch the backing file.
3495 */
a616673d
AB
3496 if (out_baseimg && *out_baseimg) {
3497 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3498 } else {
3499 ret = bdrv_change_backing_file(bs, NULL, NULL);
3500 }
3501
3e85c6fd 3502 if (ret == -ENOSPC) {
15654a6d
JS
3503 error_report("Could not change the backing file to '%s': No "
3504 "space left in the file header", out_baseimg);
3e85c6fd 3505 } else if (ret < 0) {
15654a6d 3506 error_report("Could not change the backing file to '%s': %s",
3e85c6fd
KW
3507 out_baseimg, strerror(-ret));
3508 }
3509
6b837bc4 3510 qemu_progress_print(100, 0);
3e85c6fd
KW
3511 /*
3512 * TODO At this point it is possible to check if any clusters that are
3513 * allocated in the COW file are the same in the backing file. If so, they
3514 * could be dropped from the COW file. Don't do this before switching the
3515 * backing file, in case of a crash this would lead to corruption.
3516 */
c2abccec 3517out:
6b837bc4 3518 qemu_progress_end();
3e85c6fd
KW
3519 /* Cleanup */
3520 if (!unsafe) {
26f54e9a 3521 blk_unref(blk_old_backing);
26f54e9a 3522 blk_unref(blk_new_backing);
3e85c6fd 3523 }
396374ca
PB
3524 qemu_vfree(buf_old);
3525 qemu_vfree(buf_new);
3e85c6fd 3526
26f54e9a 3527 blk_unref(blk);
c2abccec
MK
3528 if (ret) {
3529 return 1;
3530 }
3e85c6fd
KW
3531 return 0;
3532}
3533
ae6b0ed6
SH
3534static int img_resize(int argc, char **argv)
3535{
6750e795 3536 Error *err = NULL;
ae6b0ed6
SH
3537 int c, ret, relative;
3538 const char *filename, *fmt, *size;
5279b303 3539 int64_t n, total_size, current_size, new_size;
f382d43a 3540 bool quiet = false;
26f54e9a 3541 BlockBackend *blk = NULL;
dc5f690b 3542 PreallocMode prealloc = PREALLOC_MODE_OFF;
20caf0f7 3543 QemuOpts *param;
3babeb15 3544
20caf0f7
DXW
3545 static QemuOptsList resize_options = {
3546 .name = "resize_options",
3547 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3548 .desc = {
3549 {
3550 .name = BLOCK_OPT_SIZE,
3551 .type = QEMU_OPT_SIZE,
3552 .help = "Virtual disk size"
3553 }, {
3554 /* end of list */
3555 }
ae6b0ed6 3556 },
ae6b0ed6 3557 };
eb769f74 3558 bool image_opts = false;
4ffca890 3559 bool shrink = false;
ae6b0ed6 3560
e80fec7f
KW
3561 /* Remove size from argv manually so that negative numbers are not treated
3562 * as options by getopt. */
3563 if (argc < 3) {
ac1307ab 3564 error_exit("Not enough arguments");
e80fec7f
KW
3565 return 1;
3566 }
3567
3568 size = argv[--argc];
3569
3570 /* Parse getopt arguments */
ae6b0ed6
SH
3571 fmt = NULL;
3572 for(;;) {
3babeb15
DB
3573 static const struct option long_options[] = {
3574 {"help", no_argument, 0, 'h'},
3575 {"object", required_argument, 0, OPTION_OBJECT},
eb769f74 3576 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
dc5f690b 3577 {"preallocation", required_argument, 0, OPTION_PREALLOCATION},
4ffca890 3578 {"shrink", no_argument, 0, OPTION_SHRINK},
3babeb15
DB
3579 {0, 0, 0, 0}
3580 };
c9192973 3581 c = getopt_long(argc, argv, ":f:hq",
3babeb15 3582 long_options, NULL);
ae6b0ed6
SH
3583 if (c == -1) {
3584 break;
3585 }
3586 switch(c) {
c9192973
SH
3587 case ':':
3588 missing_argument(argv[optind - 1]);
3589 break;
ef87394c 3590 case '?':
c9192973
SH
3591 unrecognized_option(argv[optind - 1]);
3592 break;
ae6b0ed6
SH
3593 case 'h':
3594 help();
3595 break;
3596 case 'f':
3597 fmt = optarg;
3598 break;
f382d43a
MR
3599 case 'q':
3600 quiet = true;
3601 break;
3babeb15
DB
3602 case OPTION_OBJECT: {
3603 QemuOpts *opts;
3604 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3605 optarg, true);
3606 if (!opts) {
3607 return 1;
3608 }
3609 } break;
eb769f74
DB
3610 case OPTION_IMAGE_OPTS:
3611 image_opts = true;
3612 break;
dc5f690b 3613 case OPTION_PREALLOCATION:
f7abe0ec 3614 prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg,
06c60b6c 3615 PREALLOC_MODE__MAX, NULL);
dc5f690b
HR
3616 if (prealloc == PREALLOC_MODE__MAX) {
3617 error_report("Invalid preallocation mode '%s'", optarg);
3618 return 1;
3619 }
3620 break;
4ffca890
PB
3621 case OPTION_SHRINK:
3622 shrink = true;
3623 break;
ae6b0ed6
SH
3624 }
3625 }
fc11eb26 3626 if (optind != argc - 1) {
be8fbd47 3627 error_exit("Expecting image file name and size");
ae6b0ed6
SH
3628 }
3629 filename = argv[optind++];
ae6b0ed6 3630
3babeb15
DB
3631 if (qemu_opts_foreach(&qemu_object_opts,
3632 user_creatable_add_opts_foreach,
334c43e2 3633 NULL, &error_fatal)) {
3babeb15
DB
3634 return 1;
3635 }
3636
ae6b0ed6
SH
3637 /* Choose grow, shrink, or absolute resize mode */
3638 switch (size[0]) {
3639 case '+':
3640 relative = 1;
3641 size++;
3642 break;
3643 case '-':
3644 relative = -1;
3645 size++;
3646 break;
3647 default:
3648 relative = 0;
3649 break;
3650 }
3651
3652 /* Parse size */
87ea75d5 3653 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
f43e47db 3654 qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
6750e795
MA
3655 if (err) {
3656 error_report_err(err);
2a81998a 3657 ret = -1;
20caf0f7 3658 qemu_opts_del(param);
2a81998a 3659 goto out;
ae6b0ed6 3660 }
20caf0f7
DXW
3661 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3662 qemu_opts_del(param);
ae6b0ed6 3663
efaa7c4e 3664 blk = img_open(image_opts, filename, fmt,
335e9937
FZ
3665 BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet,
3666 false);
7e7d56d9 3667 if (!blk) {
2a81998a
JS
3668 ret = -1;
3669 goto out;
c2abccec 3670 }
ae6b0ed6 3671
dc5f690b
HR
3672 current_size = blk_getlength(blk);
3673 if (current_size < 0) {
3674 error_report("Failed to inquire current image length: %s",
3675 strerror(-current_size));
3676 ret = -1;
3677 goto out;
3678 }
3679
ae6b0ed6 3680 if (relative) {
dc5f690b 3681 total_size = current_size + n * relative;
ae6b0ed6
SH
3682 } else {
3683 total_size = n;
3684 }
3685 if (total_size <= 0) {
15654a6d 3686 error_report("New image size must be positive");
c2abccec
MK
3687 ret = -1;
3688 goto out;
ae6b0ed6
SH
3689 }
3690
dc5f690b
HR
3691 if (total_size <= current_size && prealloc != PREALLOC_MODE_OFF) {
3692 error_report("Preallocation can only be used for growing images");
3693 ret = -1;
3694 goto out;
3695 }
3696
4ffca890
PB
3697 if (total_size < current_size && !shrink) {
3698 warn_report("Shrinking an image will delete all data beyond the "
3699 "shrunken image's end. Before performing such an "
3700 "operation, make sure there is no important data there.");
3701
3702 if (g_strcmp0(bdrv_get_format_name(blk_bs(blk)), "raw") != 0) {
3703 error_report(
3704 "Use the --shrink option to perform a shrink operation.");
3705 ret = -1;
3706 goto out;
3707 } else {
3708 warn_report("Using the --shrink option will suppress this message. "
3709 "Note that future versions of qemu-img may refuse to "
3710 "shrink images without this option.");
3711 }
3712 }
3713
dc5f690b 3714 ret = blk_truncate(blk, total_size, prealloc, &err);
5279b303 3715 if (ret < 0) {
ed3d2ec9 3716 error_report_err(err);
5279b303
HR
3717 goto out;
3718 }
3719
3720 new_size = blk_getlength(blk);
3721 if (new_size < 0) {
3722 error_report("Failed to verify truncated image length: %s",
3723 strerror(-new_size));
3724 ret = -1;
3725 goto out;
ae6b0ed6 3726 }
5279b303
HR
3727
3728 /* Some block drivers implement a truncation method, but only so
3729 * the user can cause qemu to refresh the image's size from disk.
3730 * The idea is that the user resizes the image outside of qemu and
3731 * then invokes block_resize to inform qemu about it.
3732 * (This includes iscsi and file-posix for device files.)
3733 * Of course, that is not the behavior someone invoking
3734 * qemu-img resize would find useful, so we catch that behavior
3735 * here and tell the user. */
3736 if (new_size != total_size && new_size == current_size) {
3737 error_report("Image was not resized; resizing may not be supported "
3738 "for this image");
3739 ret = -1;
3740 goto out;
3741 }
3742
3743 if (new_size != total_size) {
3744 warn_report("Image should have been resized to %" PRIi64
3745 " bytes, but was resized to %" PRIi64 " bytes",
3746 total_size, new_size);
3747 }
3748
3749 qprintf(quiet, "Image resized.\n");
3750
c2abccec 3751out:
26f54e9a 3752 blk_unref(blk);
c2abccec
MK
3753 if (ret) {
3754 return 1;
3755 }
ae6b0ed6
SH
3756 return 0;
3757}
3758
76a3a34d 3759static void amend_status_cb(BlockDriverState *bs,
8b13976d
HR
3760 int64_t offset, int64_t total_work_size,
3761 void *opaque)
76a3a34d
HR
3762{
3763 qemu_progress_print(100.f * offset / total_work_size, 0);
3764}
3765
51641351
HR
3766static int print_amend_option_help(const char *format)
3767{
3768 BlockDriver *drv;
3769
3770 /* Find driver and parse its options */
3771 drv = bdrv_find_format(format);
3772 if (!drv) {
3773 error_report("Unknown file format '%s'", format);
3774 return 1;
3775 }
3776
3777 if (!drv->bdrv_amend_options) {
3778 error_report("Format driver '%s' does not support option amendment",
3779 format);
3780 return 1;
3781 }
3782
3783 /* Every driver supporting amendment must have create_opts */
3784 assert(drv->create_opts);
3785
3786 printf("Creation options for '%s':\n", format);
63898712 3787 qemu_opts_print_help(drv->create_opts, false);
51641351
HR
3788 printf("\nNote that not all of these options may be amendable.\n");
3789 return 0;
3790}
3791
6f176b48
HR
3792static int img_amend(int argc, char **argv)
3793{
dc523cd3 3794 Error *err = NULL;
6f176b48
HR
3795 int c, ret = 0;
3796 char *options = NULL;
83d0521a
CL
3797 QemuOptsList *create_opts = NULL;
3798 QemuOpts *opts = NULL;
bd39e6ed
HR
3799 const char *fmt = NULL, *filename, *cache;
3800 int flags;
ce099547 3801 bool writethrough;
76a3a34d 3802 bool quiet = false, progress = false;
26f54e9a 3803 BlockBackend *blk = NULL;
6f176b48 3804 BlockDriverState *bs = NULL;
eb769f74 3805 bool image_opts = false;
6f176b48 3806
bd39e6ed 3807 cache = BDRV_DEFAULT_CACHE;
6f176b48 3808 for (;;) {
3babeb15
DB
3809 static const struct option long_options[] = {
3810 {"help", no_argument, 0, 'h'},
3811 {"object", required_argument, 0, OPTION_OBJECT},
eb769f74 3812 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
3babeb15
DB
3813 {0, 0, 0, 0}
3814 };
c9192973 3815 c = getopt_long(argc, argv, ":ho:f:t:pq",
3babeb15 3816 long_options, NULL);
6f176b48
HR
3817 if (c == -1) {
3818 break;
3819 }
3820
3821 switch (c) {
c9192973
SH
3822 case ':':
3823 missing_argument(argv[optind - 1]);
3824 break;
f7077624 3825 case '?':
c9192973
SH
3826 unrecognized_option(argv[optind - 1]);
3827 break;
3828 case 'h':
f7077624
SH
3829 help();
3830 break;
3831 case 'o':
3832 if (!is_valid_option_list(optarg)) {
3833 error_report("Invalid option list: %s", optarg);
3834 ret = -1;
3835 goto out_no_progress;
3836 }
3837 if (!options) {
3838 options = g_strdup(optarg);
3839 } else {
3840 char *old_options = options;
3841 options = g_strdup_printf("%s,%s", options, optarg);
3842 g_free(old_options);
3843 }
3844 break;
3845 case 'f':
3846 fmt = optarg;
3847 break;
3848 case 't':
3849 cache = optarg;
3850 break;
3851 case 'p':
3852 progress = true;
3853 break;
3854 case 'q':
3855 quiet = true;
3856 break;
3857 case OPTION_OBJECT:
3858 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3859 optarg, true);
3860 if (!opts) {
3861 ret = -1;
3862 goto out_no_progress;
3863 }
3864 break;
3865 case OPTION_IMAGE_OPTS:
3866 image_opts = true;
3867 break;
6f176b48
HR
3868 }
3869 }
3870
a283cb6e 3871 if (!options) {
ac1307ab 3872 error_exit("Must specify options (-o)");
6f176b48
HR
3873 }
3874
3babeb15
DB
3875 if (qemu_opts_foreach(&qemu_object_opts,
3876 user_creatable_add_opts_foreach,
334c43e2 3877 NULL, &error_fatal)) {
3babeb15
DB
3878 ret = -1;
3879 goto out_no_progress;
3880 }
3881
76a3a34d
HR
3882 if (quiet) {
3883 progress = false;
3884 }
3885 qemu_progress_init(progress, 1.0);
3886
a283cb6e
KW
3887 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3888 if (fmt && has_help_option(options)) {
3889 /* If a format is explicitly specified (and possibly no filename is
3890 * given), print option help here */
51641351 3891 ret = print_amend_option_help(fmt);
a283cb6e 3892 goto out;
6f176b48
HR
3893 }
3894
a283cb6e 3895 if (optind != argc - 1) {
b2f27e44
HR
3896 error_report("Expecting one image file name");
3897 ret = -1;
3898 goto out;
a283cb6e 3899 }
6f176b48 3900
ce099547
KW
3901 flags = BDRV_O_RDWR;
3902 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
bd39e6ed
HR
3903 if (ret < 0) {
3904 error_report("Invalid cache option: %s", cache);
3905 goto out;
3906 }
3907
335e9937
FZ
3908 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3909 false);
7e7d56d9 3910 if (!blk) {
6f176b48
HR
3911 ret = -1;
3912 goto out;
3913 }
7e7d56d9 3914 bs = blk_bs(blk);
6f176b48
HR
3915
3916 fmt = bs->drv->format_name;
3917
626f84f3 3918 if (has_help_option(options)) {
a283cb6e 3919 /* If the format was auto-detected, print option help here */
51641351 3920 ret = print_amend_option_help(fmt);
6f176b48
HR
3921 goto out;
3922 }
3923
1f996683
HR
3924 if (!bs->drv->bdrv_amend_options) {
3925 error_report("Format driver '%s' does not support option amendment",
b2439d26
HR
3926 fmt);
3927 ret = -1;
3928 goto out;
3929 }
3930
1f996683
HR
3931 /* Every driver supporting amendment must have create_opts */
3932 assert(bs->drv->create_opts);
3933
c282e1fd 3934 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
83d0521a 3935 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
ece9086e
PB
3936 qemu_opts_do_parse(opts, options, NULL, &err);
3937 if (err) {
3938 error_report_err(err);
3939 ret = -1;
3940 goto out;
6f176b48
HR
3941 }
3942
76a3a34d
HR
3943 /* In case the driver does not call amend_status_cb() */
3944 qemu_progress_print(0.f, 0);
d1402b50 3945 ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL, &err);
76a3a34d 3946 qemu_progress_print(100.f, 0);
6f176b48 3947 if (ret < 0) {
d1402b50 3948 error_report_err(err);
6f176b48
HR
3949 goto out;
3950 }
3951
3952out:
76a3a34d
HR
3953 qemu_progress_end();
3954
e814dffc 3955out_no_progress:
26f54e9a 3956 blk_unref(blk);
83d0521a
CL
3957 qemu_opts_del(opts);
3958 qemu_opts_free(create_opts);
626f84f3
KW
3959 g_free(options);
3960
6f176b48
HR
3961 if (ret) {
3962 return 1;
3963 }
3964 return 0;
3965}
3966
b6133b8c
KW
3967typedef struct BenchData {
3968 BlockBackend *blk;
3969 uint64_t image_size;
b6495fa8 3970 bool write;
b6133b8c 3971 int bufsize;
83de9be0 3972 int step;
b6133b8c
KW
3973 int nrreq;
3974 int n;
55d539c8
KW
3975 int flush_interval;
3976 bool drain_on_flush;
b6133b8c
KW
3977 uint8_t *buf;
3978 QEMUIOVector *qiov;
3979
3980 int in_flight;
55d539c8 3981 bool in_flush;
b6133b8c
KW
3982 uint64_t offset;
3983} BenchData;
3984
55d539c8
KW
3985static void bench_undrained_flush_cb(void *opaque, int ret)
3986{
3987 if (ret < 0) {
df3c286c 3988 error_report("Failed flush request: %s", strerror(-ret));
55d539c8
KW
3989 exit(EXIT_FAILURE);
3990 }
3991}
3992
b6133b8c
KW
3993static void bench_cb(void *opaque, int ret)
3994{
3995 BenchData *b = opaque;
3996 BlockAIOCB *acb;
3997
3998 if (ret < 0) {
df3c286c 3999 error_report("Failed request: %s", strerror(-ret));
b6133b8c
KW
4000 exit(EXIT_FAILURE);
4001 }
55d539c8
KW
4002
4003 if (b->in_flush) {
4004 /* Just finished a flush with drained queue: Start next requests */
4005 assert(b->in_flight == 0);
4006 b->in_flush = false;
4007 } else if (b->in_flight > 0) {
4008 int remaining = b->n - b->in_flight;
4009
b6133b8c
KW
4010 b->n--;
4011 b->in_flight--;
55d539c8
KW
4012
4013 /* Time for flush? Drain queue if requested, then flush */
4014 if (b->flush_interval && remaining % b->flush_interval == 0) {
4015 if (!b->in_flight || !b->drain_on_flush) {
4016 BlockCompletionFunc *cb;
4017
4018 if (b->drain_on_flush) {
4019 b->in_flush = true;
4020 cb = bench_cb;
4021 } else {
4022 cb = bench_undrained_flush_cb;
4023 }
4024
4025 acb = blk_aio_flush(b->blk, cb, b);
4026 if (!acb) {
4027 error_report("Failed to issue flush request");
4028 exit(EXIT_FAILURE);
4029 }
4030 }
4031 if (b->drain_on_flush) {
4032 return;
4033 }
4034 }
b6133b8c
KW
4035 }
4036
4037 while (b->n > b->in_flight && b->in_flight < b->nrreq) {
4baaa8c3
PB
4038 int64_t offset = b->offset;
4039 /* blk_aio_* might look for completed I/Os and kick bench_cb
4040 * again, so make sure this operation is counted by in_flight
4041 * and b->offset is ready for the next submission.
4042 */
4043 b->in_flight++;
4044 b->offset += b->step;
4045 b->offset %= b->image_size;
b6495fa8 4046 if (b->write) {
4baaa8c3 4047 acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b);
b6495fa8 4048 } else {
4baaa8c3 4049 acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b);
b6495fa8 4050 }
b6133b8c
KW
4051 if (!acb) {
4052 error_report("Failed to issue request");
4053 exit(EXIT_FAILURE);
4054 }
b6133b8c
KW
4055 }
4056}
4057
4058static int img_bench(int argc, char **argv)
4059{
4060 int c, ret = 0;
4061 const char *fmt = NULL, *filename;
4062 bool quiet = false;
4063 bool image_opts = false;
b6495fa8 4064 bool is_write = false;
b6133b8c
KW
4065 int count = 75000;
4066 int depth = 64;
d3199a31 4067 int64_t offset = 0;
b6133b8c 4068 size_t bufsize = 4096;
b6495fa8 4069 int pattern = 0;
83de9be0 4070 size_t step = 0;
55d539c8
KW
4071 int flush_interval = 0;
4072 bool drain_on_flush = true;
b6133b8c
KW
4073 int64_t image_size;
4074 BlockBackend *blk = NULL;
4075 BenchData data = {};
4076 int flags = 0;
604e8613 4077 bool writethrough = false;
b6133b8c
KW
4078 struct timeval t1, t2;
4079 int i;
335e9937 4080 bool force_share = false;
79d46583 4081 size_t buf_size;
b6133b8c
KW
4082
4083 for (;;) {
4084 static const struct option long_options[] = {
4085 {"help", no_argument, 0, 'h'},
55d539c8 4086 {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL},
b6133b8c 4087 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
b6495fa8 4088 {"pattern", required_argument, 0, OPTION_PATTERN},
55d539c8 4089 {"no-drain", no_argument, 0, OPTION_NO_DRAIN},
335e9937 4090 {"force-share", no_argument, 0, 'U'},
b6133b8c
KW
4091 {0, 0, 0, 0}
4092 };
335e9937 4093 c = getopt_long(argc, argv, ":hc:d:f:no:qs:S:t:wU", long_options, NULL);
b6133b8c
KW
4094 if (c == -1) {
4095 break;
4096 }
4097
4098 switch (c) {
c9192973
SH
4099 case ':':
4100 missing_argument(argv[optind - 1]);
4101 break;
b6133b8c 4102 case '?':
c9192973
SH
4103 unrecognized_option(argv[optind - 1]);
4104 break;
4105 case 'h':
b6133b8c
KW
4106 help();
4107 break;
4108 case 'c':
4109 {
8b3c6792
PM
4110 unsigned long res;
4111
4112 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
b6133b8c
KW
4113 error_report("Invalid request count specified");
4114 return 1;
4115 }
8b3c6792 4116 count = res;
b6133b8c
KW
4117 break;
4118 }
4119 case 'd':
4120 {
8b3c6792
PM
4121 unsigned long res;
4122
4123 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
b6133b8c
KW
4124 error_report("Invalid queue depth specified");
4125 return 1;
4126 }
8b3c6792 4127 depth = res;
b6133b8c
KW
4128 break;
4129 }
4130 case 'f':
4131 fmt = optarg;
4132 break;
4133 case 'n':
4134 flags |= BDRV_O_NATIVE_AIO;
4135 break;
d3199a31
KW
4136 case 'o':
4137 {
606caa0a
MA
4138 offset = cvtnum(optarg);
4139 if (offset < 0) {
d3199a31
KW
4140 error_report("Invalid offset specified");
4141 return 1;
4142 }
4143 break;
4144 }
4145 break;
b6133b8c
KW
4146 case 'q':
4147 quiet = true;
4148 break;
4149 case 's':
4150 {
4151 int64_t sval;
b6133b8c 4152
606caa0a
MA
4153 sval = cvtnum(optarg);
4154 if (sval < 0 || sval > INT_MAX) {
b6133b8c
KW
4155 error_report("Invalid buffer size specified");
4156 return 1;
4157 }
4158
4159 bufsize = sval;
4160 break;
4161 }
83de9be0
KW
4162 case 'S':
4163 {
4164 int64_t sval;
83de9be0 4165
606caa0a
MA
4166 sval = cvtnum(optarg);
4167 if (sval < 0 || sval > INT_MAX) {
83de9be0
KW
4168 error_report("Invalid step size specified");
4169 return 1;
4170 }
4171
4172 step = sval;
4173 break;
4174 }
b6133b8c
KW
4175 case 't':
4176 ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
4177 if (ret < 0) {
4178 error_report("Invalid cache mode");
4179 ret = -1;
4180 goto out;
4181 }
4182 break;
b6495fa8
KW
4183 case 'w':
4184 flags |= BDRV_O_RDWR;
4185 is_write = true;
4186 break;
335e9937
FZ
4187 case 'U':
4188 force_share = true;
4189 break;
b6495fa8
KW
4190 case OPTION_PATTERN:
4191 {
8b3c6792
PM
4192 unsigned long res;
4193
4194 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
b6495fa8
KW
4195 error_report("Invalid pattern byte specified");
4196 return 1;
4197 }
8b3c6792 4198 pattern = res;
b6495fa8
KW
4199 break;
4200 }
55d539c8
KW
4201 case OPTION_FLUSH_INTERVAL:
4202 {
8b3c6792
PM
4203 unsigned long res;
4204
4205 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
55d539c8
KW
4206 error_report("Invalid flush interval specified");
4207 return 1;
4208 }
8b3c6792 4209 flush_interval = res;
55d539c8
KW
4210 break;
4211 }
4212 case OPTION_NO_DRAIN:
4213 drain_on_flush = false;
4214 break;
b6133b8c
KW
4215 case OPTION_IMAGE_OPTS:
4216 image_opts = true;
4217 break;
4218 }
4219 }
4220
4221 if (optind != argc - 1) {
4222 error_exit("Expecting one image file name");
4223 }
4224 filename = argv[argc - 1];
4225
55d539c8
KW
4226 if (!is_write && flush_interval) {
4227 error_report("--flush-interval is only available in write tests");
4228 ret = -1;
4229 goto out;
4230 }
4231 if (flush_interval && flush_interval < depth) {
4232 error_report("Flush interval can't be smaller than depth");
4233 ret = -1;
4234 goto out;
4235 }
4236
335e9937
FZ
4237 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
4238 force_share);
b6133b8c
KW
4239 if (!blk) {
4240 ret = -1;
4241 goto out;
4242 }
4243
4244 image_size = blk_getlength(blk);
4245 if (image_size < 0) {
4246 ret = image_size;
4247 goto out;
4248 }
4249
4250 data = (BenchData) {
55d539c8
KW
4251 .blk = blk,
4252 .image_size = image_size,
4253 .bufsize = bufsize,
4254 .step = step ?: bufsize,
4255 .nrreq = depth,
4256 .n = count,
4257 .offset = offset,
4258 .write = is_write,
4259 .flush_interval = flush_interval,
4260 .drain_on_flush = drain_on_flush,
b6133b8c 4261 };
d3199a31 4262 printf("Sending %d %s requests, %d bytes each, %d in parallel "
83de9be0 4263 "(starting at offset %" PRId64 ", step size %d)\n",
d3199a31 4264 data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
83de9be0 4265 data.offset, data.step);
55d539c8
KW
4266 if (flush_interval) {
4267 printf("Sending flush every %d requests\n", flush_interval);
4268 }
b6133b8c 4269
79d46583
FZ
4270 buf_size = data.nrreq * data.bufsize;
4271 data.buf = blk_blockalign(blk, buf_size);
b6495fa8
KW
4272 memset(data.buf, pattern, data.nrreq * data.bufsize);
4273
79d46583
FZ
4274 blk_register_buf(blk, data.buf, buf_size);
4275
b6133b8c
KW
4276 data.qiov = g_new(QEMUIOVector, data.nrreq);
4277 for (i = 0; i < data.nrreq; i++) {
4278 qemu_iovec_init(&data.qiov[i], 1);
4279 qemu_iovec_add(&data.qiov[i],
4280 data.buf + i * data.bufsize, data.bufsize);
4281 }
4282
4283 gettimeofday(&t1, NULL);
4284 bench_cb(&data, 0);
4285
4286 while (data.n > 0) {
4287 main_loop_wait(false);
4288 }
4289 gettimeofday(&t2, NULL);
4290
4291 printf("Run completed in %3.3f seconds.\n",
4292 (t2.tv_sec - t1.tv_sec)
4293 + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
4294
4295out:
79d46583
FZ
4296 if (data.buf) {
4297 blk_unregister_buf(blk, data.buf);
4298 }
b6133b8c
KW
4299 qemu_vfree(data.buf);
4300 blk_unref(blk);
4301
4302 if (ret) {
86ce1f6e
RS
4303 return 1;
4304 }
4305 return 0;
4306}
4307
4308#define C_BS 01
4309#define C_COUNT 02
4310#define C_IF 04
4311#define C_OF 010
f7c15533 4312#define C_SKIP 020
86ce1f6e
RS
4313
4314struct DdInfo {
4315 unsigned int flags;
4316 int64_t count;
4317};
4318
4319struct DdIo {
4320 int bsz; /* Block size */
4321 char *filename;
4322 uint8_t *buf;
f7c15533 4323 int64_t offset;
86ce1f6e
RS
4324};
4325
4326struct DdOpts {
4327 const char *name;
4328 int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *);
4329 unsigned int flag;
4330};
4331
4332static int img_dd_bs(const char *arg,
4333 struct DdIo *in, struct DdIo *out,
4334 struct DdInfo *dd)
4335{
86ce1f6e
RS
4336 int64_t res;
4337
606caa0a 4338 res = cvtnum(arg);
86ce1f6e 4339
606caa0a 4340 if (res <= 0 || res > INT_MAX) {
86ce1f6e
RS
4341 error_report("invalid number: '%s'", arg);
4342 return 1;
4343 }
4344 in->bsz = out->bsz = res;
4345
4346 return 0;
4347}
4348
4349static int img_dd_count(const char *arg,
4350 struct DdIo *in, struct DdIo *out,
4351 struct DdInfo *dd)
4352{
606caa0a 4353 dd->count = cvtnum(arg);
86ce1f6e 4354
606caa0a 4355 if (dd->count < 0) {
86ce1f6e
RS
4356 error_report("invalid number: '%s'", arg);
4357 return 1;
4358 }
4359
4360 return 0;
4361}
4362
4363static int img_dd_if(const char *arg,
4364 struct DdIo *in, struct DdIo *out,
4365 struct DdInfo *dd)
4366{
4367 in->filename = g_strdup(arg);
4368
4369 return 0;
4370}
4371
4372static int img_dd_of(const char *arg,
4373 struct DdIo *in, struct DdIo *out,
4374 struct DdInfo *dd)
4375{
4376 out->filename = g_strdup(arg);
4377
4378 return 0;
4379}
4380
f7c15533
RS
4381static int img_dd_skip(const char *arg,
4382 struct DdIo *in, struct DdIo *out,
4383 struct DdInfo *dd)
4384{
606caa0a 4385 in->offset = cvtnum(arg);
f7c15533 4386
606caa0a 4387 if (in->offset < 0) {
f7c15533
RS
4388 error_report("invalid number: '%s'", arg);
4389 return 1;
4390 }
4391
4392 return 0;
4393}
4394
86ce1f6e
RS
4395static int img_dd(int argc, char **argv)
4396{
4397 int ret = 0;
4398 char *arg = NULL;
4399 char *tmp;
4400 BlockDriver *drv = NULL, *proto_drv = NULL;
4401 BlockBackend *blk1 = NULL, *blk2 = NULL;
4402 QemuOpts *opts = NULL;
4403 QemuOptsList *create_opts = NULL;
4404 Error *local_err = NULL;
4405 bool image_opts = false;
4406 int c, i;
4407 const char *out_fmt = "raw";
4408 const char *fmt = NULL;
4409 int64_t size = 0;
4410 int64_t block_count = 0, out_pos, in_pos;
335e9937 4411 bool force_share = false;
86ce1f6e
RS
4412 struct DdInfo dd = {
4413 .flags = 0,
4414 .count = 0,
4415 };
4416 struct DdIo in = {
4417 .bsz = 512, /* Block size is by default 512 bytes */
4418 .filename = NULL,
f7c15533
RS
4419 .buf = NULL,
4420 .offset = 0
86ce1f6e
RS
4421 };
4422 struct DdIo out = {
4423 .bsz = 512,
4424 .filename = NULL,
f7c15533
RS
4425 .buf = NULL,
4426 .offset = 0
86ce1f6e
RS
4427 };
4428
4429 const struct DdOpts options[] = {
4430 { "bs", img_dd_bs, C_BS },
4431 { "count", img_dd_count, C_COUNT },
4432 { "if", img_dd_if, C_IF },
4433 { "of", img_dd_of, C_OF },
f7c15533 4434 { "skip", img_dd_skip, C_SKIP },
86ce1f6e
RS
4435 { NULL, NULL, 0 }
4436 };
4437 const struct option long_options[] = {
4438 { "help", no_argument, 0, 'h'},
83d4bf94 4439 { "object", required_argument, 0, OPTION_OBJECT},
86ce1f6e 4440 { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
335e9937 4441 { "force-share", no_argument, 0, 'U'},
86ce1f6e
RS
4442 { 0, 0, 0, 0 }
4443 };
4444
335e9937 4445 while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) {
86ce1f6e
RS
4446 if (c == EOF) {
4447 break;
4448 }
4449 switch (c) {
4450 case 'O':
4451 out_fmt = optarg;
4452 break;
4453 case 'f':
4454 fmt = optarg;
4455 break;
c9192973
SH
4456 case ':':
4457 missing_argument(argv[optind - 1]);
4458 break;
86ce1f6e 4459 case '?':
c9192973
SH
4460 unrecognized_option(argv[optind - 1]);
4461 break;
86ce1f6e
RS
4462 case 'h':
4463 help();
4464 break;
335e9937
FZ
4465 case 'U':
4466 force_share = true;
4467 break;
2a245709
SH
4468 case OPTION_OBJECT:
4469 if (!qemu_opts_parse_noisily(&qemu_object_opts, optarg, true)) {
83d4bf94
DB
4470 ret = -1;
4471 goto out;
4472 }
2a245709 4473 break;
86ce1f6e
RS
4474 case OPTION_IMAGE_OPTS:
4475 image_opts = true;
4476 break;
4477 }
4478 }
4479
4480 for (i = optind; i < argc; i++) {
4481 int j;
4482 arg = g_strdup(argv[i]);
4483
4484 tmp = strchr(arg, '=');
4485 if (tmp == NULL) {
4486 error_report("unrecognized operand %s", arg);
4487 ret = -1;
4488 goto out;
4489 }
4490
4491 *tmp++ = '\0';
4492
4493 for (j = 0; options[j].name != NULL; j++) {
4494 if (!strcmp(arg, options[j].name)) {
4495 break;
4496 }
4497 }
4498 if (options[j].name == NULL) {
4499 error_report("unrecognized operand %s", arg);
4500 ret = -1;
4501 goto out;
4502 }
4503
4504 if (options[j].f(tmp, &in, &out, &dd) != 0) {
4505 ret = -1;
4506 goto out;
4507 }
4508 dd.flags |= options[j].flag;
4509 g_free(arg);
4510 arg = NULL;
4511 }
4512
4513 if (!(dd.flags & C_IF && dd.flags & C_OF)) {
4514 error_report("Must specify both input and output files");
4515 ret = -1;
4516 goto out;
4517 }
83d4bf94
DB
4518
4519 if (qemu_opts_foreach(&qemu_object_opts,
4520 user_creatable_add_opts_foreach,
334c43e2 4521 NULL, &error_fatal)) {
83d4bf94
DB
4522 ret = -1;
4523 goto out;
4524 }
4525
335e9937
FZ
4526 blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
4527 force_share);
86ce1f6e
RS
4528
4529 if (!blk1) {
4530 ret = -1;
4531 goto out;
4532 }
4533
4534 drv = bdrv_find_format(out_fmt);
4535 if (!drv) {
4536 error_report("Unknown file format");
4537 ret = -1;
4538 goto out;
4539 }
4540 proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
4541
4542 if (!proto_drv) {
4543 error_report_err(local_err);
4544 ret = -1;
4545 goto out;
4546 }
4547 if (!drv->create_opts) {
4548 error_report("Format driver '%s' does not support image creation",
4549 drv->format_name);
4550 ret = -1;
4551 goto out;
4552 }
4553 if (!proto_drv->create_opts) {
4554 error_report("Protocol driver '%s' does not support image creation",
4555 proto_drv->format_name);
4556 ret = -1;
4557 goto out;
4558 }
4559 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4560 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
4561
4562 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4563
4564 size = blk_getlength(blk1);
4565 if (size < 0) {
4566 error_report("Failed to get size for '%s'", in.filename);
4567 ret = -1;
4568 goto out;
4569 }
4570
4571 if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
4572 dd.count * in.bsz < size) {
4573 size = dd.count * in.bsz;
4574 }
4575
f7c15533
RS
4576 /* Overflow means the specified offset is beyond input image's size */
4577 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4578 size < in.bsz * in.offset)) {
4579 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
4580 } else {
4581 qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
4582 size - in.bsz * in.offset, &error_abort);
4583 }
86ce1f6e
RS
4584
4585 ret = bdrv_create(drv, out.filename, opts, &local_err);
4586 if (ret < 0) {
4587 error_reportf_err(local_err,
4588 "%s: error while creating output image: ",
4589 out.filename);
4590 ret = -1;
4591 goto out;
4592 }
4593
ea204dda
DB
4594 /* TODO, we can't honour --image-opts for the target,
4595 * since it needs to be given in a format compatible
4596 * with the bdrv_create() call above which does not
4597 * support image-opts style.
4598 */
29cf9336 4599 blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
ea204dda 4600 false, false, false);
86ce1f6e
RS
4601
4602 if (!blk2) {
4603 ret = -1;
4604 goto out;
4605 }
4606
f7c15533
RS
4607 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4608 size < in.offset * in.bsz)) {
4609 /* We give a warning if the skip option is bigger than the input
4610 * size and create an empty output disk image (i.e. like dd(1)).
4611 */
4612 error_report("%s: cannot skip to specified offset", in.filename);
4613 in_pos = size;
4614 } else {
4615 in_pos = in.offset * in.bsz;
4616 }
4617
86ce1f6e
RS
4618 in.buf = g_new(uint8_t, in.bsz);
4619
f7c15533 4620 for (out_pos = 0; in_pos < size; block_count++) {
86ce1f6e
RS
4621 int in_ret, out_ret;
4622
4623 if (in_pos + in.bsz > size) {
4624 in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos);
4625 } else {
4626 in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
4627 }
4628 if (in_ret < 0) {
4629 error_report("error while reading from input image file: %s",
4630 strerror(-in_ret));
4631 ret = -1;
4632 goto out;
4633 }
4634 in_pos += in_ret;
4635
4636 out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
4637
4638 if (out_ret < 0) {
4639 error_report("error while writing to output image file: %s",
4640 strerror(-out_ret));
4641 ret = -1;
4642 goto out;
4643 }
4644 out_pos += out_ret;
4645 }
4646
4647out:
4648 g_free(arg);
4649 qemu_opts_del(opts);
4650 qemu_opts_free(create_opts);
4651 blk_unref(blk1);
4652 blk_unref(blk2);
4653 g_free(in.filename);
4654 g_free(out.filename);
4655 g_free(in.buf);
4656 g_free(out.buf);
4657
4658 if (ret) {
b6133b8c
KW
4659 return 1;
4660 }
4661 return 0;
4662}
4663
fd03c2b8
SH
4664static void dump_json_block_measure_info(BlockMeasureInfo *info)
4665{
4666 QString *str;
4667 QObject *obj;
4668 Visitor *v = qobject_output_visitor_new(&obj);
4669
4670 visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort);
4671 visit_complete(v, &obj);
4672 str = qobject_to_json_pretty(obj);
4673 assert(str != NULL);
4674 printf("%s\n", qstring_get_str(str));
cb3e7f08 4675 qobject_unref(obj);
fd03c2b8 4676 visit_free(v);
cb3e7f08 4677 qobject_unref(str);
fd03c2b8
SH
4678}
4679
4680static int img_measure(int argc, char **argv)
4681{
4682 static const struct option long_options[] = {
4683 {"help", no_argument, 0, 'h'},
4684 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
4685 {"object", required_argument, 0, OPTION_OBJECT},
4686 {"output", required_argument, 0, OPTION_OUTPUT},
4687 {"size", required_argument, 0, OPTION_SIZE},
4688 {"force-share", no_argument, 0, 'U'},
4689 {0, 0, 0, 0}
4690 };
4691 OutputFormat output_format = OFORMAT_HUMAN;
4692 BlockBackend *in_blk = NULL;
4693 BlockDriver *drv;
4694 const char *filename = NULL;
4695 const char *fmt = NULL;
4696 const char *out_fmt = "raw";
4697 char *options = NULL;
4698 char *snapshot_name = NULL;
4699 bool force_share = false;
4700 QemuOpts *opts = NULL;
4701 QemuOpts *object_opts = NULL;
4702 QemuOpts *sn_opts = NULL;
4703 QemuOptsList *create_opts = NULL;
4704 bool image_opts = false;
4705 uint64_t img_size = UINT64_MAX;
4706 BlockMeasureInfo *info = NULL;
4707 Error *local_err = NULL;
4708 int ret = 1;
4709 int c;
4710
4711 while ((c = getopt_long(argc, argv, "hf:O:o:l:U",
4712 long_options, NULL)) != -1) {
4713 switch (c) {
4714 case '?':
4715 case 'h':
4716 help();
4717 break;
4718 case 'f':
4719 fmt = optarg;
4720 break;
4721 case 'O':
4722 out_fmt = optarg;
4723 break;
4724 case 'o':
4725 if (!is_valid_option_list(optarg)) {
4726 error_report("Invalid option list: %s", optarg);
4727 goto out;
4728 }
4729 if (!options) {
4730 options = g_strdup(optarg);
4731 } else {
4732 char *old_options = options;
4733 options = g_strdup_printf("%s,%s", options, optarg);
4734 g_free(old_options);
4735 }
4736 break;
4737 case 'l':
4738 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
4739 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
4740 optarg, false);
4741 if (!sn_opts) {
4742 error_report("Failed in parsing snapshot param '%s'",
4743 optarg);
4744 goto out;
4745 }
4746 } else {
4747 snapshot_name = optarg;
4748 }
4749 break;
4750 case 'U':
4751 force_share = true;
4752 break;
4753 case OPTION_OBJECT:
4754 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
4755 optarg, true);
4756 if (!object_opts) {
4757 goto out;
4758 }
4759 break;
4760 case OPTION_IMAGE_OPTS:
4761 image_opts = true;
4762 break;
4763 case OPTION_OUTPUT:
4764 if (!strcmp(optarg, "json")) {
4765 output_format = OFORMAT_JSON;
4766 } else if (!strcmp(optarg, "human")) {
4767 output_format = OFORMAT_HUMAN;
4768 } else {
4769 error_report("--output must be used with human or json "
4770 "as argument.");
4771 goto out;
4772 }
4773 break;
4774 case OPTION_SIZE:
4775 {
4776 int64_t sval;
4777
4778 sval = cvtnum(optarg);
4779 if (sval < 0) {
4780 if (sval == -ERANGE) {
4781 error_report("Image size must be less than 8 EiB!");
4782 } else {
4783 error_report("Invalid image size specified! You may use "
4784 "k, M, G, T, P or E suffixes for ");
4785 error_report("kilobytes, megabytes, gigabytes, terabytes, "
4786 "petabytes and exabytes.");
4787 }
4788 goto out;
4789 }
4790 img_size = (uint64_t)sval;
4791 }
4792 break;
4793 }
4794 }
4795
4796 if (qemu_opts_foreach(&qemu_object_opts,
4797 user_creatable_add_opts_foreach,
334c43e2 4798 NULL, &error_fatal)) {
fd03c2b8
SH
4799 goto out;
4800 }
4801
4802 if (argc - optind > 1) {
4803 error_report("At most one filename argument is allowed.");
4804 goto out;
4805 } else if (argc - optind == 1) {
4806 filename = argv[optind];
4807 }
4808
4809 if (!filename &&
4810 (object_opts || image_opts || fmt || snapshot_name || sn_opts)) {
4811 error_report("--object, --image-opts, -f, and -l "
4812 "require a filename argument.");
4813 goto out;
4814 }
4815 if (filename && img_size != UINT64_MAX) {
4816 error_report("--size N cannot be used together with a filename.");
4817 goto out;
4818 }
4819 if (!filename && img_size == UINT64_MAX) {
4820 error_report("Either --size N or one filename must be specified.");
4821 goto out;
4822 }
4823
4824 if (filename) {
4825 in_blk = img_open(image_opts, filename, fmt, 0,
4826 false, false, force_share);
4827 if (!in_blk) {
4828 goto out;
4829 }
4830
4831 if (sn_opts) {
4832 bdrv_snapshot_load_tmp(blk_bs(in_blk),
4833 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
4834 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
4835 &local_err);
4836 } else if (snapshot_name != NULL) {
4837 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk),
4838 snapshot_name, &local_err);
4839 }
4840 if (local_err) {
4841 error_reportf_err(local_err, "Failed to load snapshot: ");
4842 goto out;
4843 }
4844 }
4845
4846 drv = bdrv_find_format(out_fmt);
4847 if (!drv) {
4848 error_report("Unknown file format '%s'", out_fmt);
4849 goto out;
4850 }
4851 if (!drv->create_opts) {
4852 error_report("Format driver '%s' does not support image creation",
4853 drv->format_name);
4854 goto out;
4855 }
4856
4857 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4858 create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts);
4859 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4860 if (options) {
4861 qemu_opts_do_parse(opts, options, NULL, &local_err);
4862 if (local_err) {
4863 error_report_err(local_err);
4864 error_report("Invalid options for file format '%s'", out_fmt);
4865 goto out;
4866 }
4867 }
4868 if (img_size != UINT64_MAX) {
4869 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort);
4870 }
4871
4872 info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err);
4873 if (local_err) {
4874 error_report_err(local_err);
4875 goto out;
4876 }
4877
4878 if (output_format == OFORMAT_HUMAN) {
4879 printf("required size: %" PRIu64 "\n", info->required);
4880 printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated);
4881 } else {
4882 dump_json_block_measure_info(info);
4883 }
4884
4885 ret = 0;
4886
4887out:
4888 qapi_free_BlockMeasureInfo(info);
4889 qemu_opts_del(object_opts);
4890 qemu_opts_del(opts);
4891 qemu_opts_del(sn_opts);
4892 qemu_opts_free(create_opts);
4893 g_free(options);
4894 blk_unref(in_blk);
4895 return ret;
4896}
b6133b8c 4897
c227f099 4898static const img_cmd_t img_cmds[] = {
153859be
SB
4899#define DEF(option, callback, arg_string) \
4900 { option, callback },
4901#include "qemu-img-cmds.h"
4902#undef DEF
153859be
SB
4903 { NULL, NULL, },
4904};
4905
ea2384d3
FB
4906int main(int argc, char **argv)
4907{
c227f099 4908 const img_cmd_t *cmd;
153859be 4909 const char *cmdname;
2f78e491 4910 Error *local_error = NULL;
06a1e0c1 4911 char *trace_file = NULL;
7db1689c 4912 int c;
7db1689c
JC
4913 static const struct option long_options[] = {
4914 {"help", no_argument, 0, 'h'},
10985131 4915 {"version", no_argument, 0, 'V'},
06a1e0c1 4916 {"trace", required_argument, NULL, 'T'},
7db1689c
JC
4917 {0, 0, 0, 0}
4918 };
ea2384d3 4919
526eda14
MK
4920#ifdef CONFIG_POSIX
4921 signal(SIGPIPE, SIG_IGN);
4922#endif
4923
f5852efa 4924 error_init(argv[0]);
fe4db84d 4925 module_call_init(MODULE_INIT_TRACE);
10f5bff6 4926 qemu_init_exec_dir(argv[0]);
53f76e58 4927
2f78e491 4928 if (qemu_init_main_loop(&local_error)) {
565f65d2 4929 error_report_err(local_error);
2f78e491
CN
4930 exit(EXIT_FAILURE);
4931 }
4932
e8f2d272 4933 qcrypto_init(&error_fatal);
c2297088 4934
064097d9 4935 module_call_init(MODULE_INIT_QOM);
ea2384d3 4936 bdrv_init();
ac1307ab
FZ
4937 if (argc < 2) {
4938 error_exit("Not enough arguments");
4939 }
153859be 4940
3babeb15 4941 qemu_add_opts(&qemu_object_opts);
eb769f74 4942 qemu_add_opts(&qemu_source_opts);
06a1e0c1 4943 qemu_add_opts(&qemu_trace_opts);
3babeb15 4944
c9192973 4945 while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) {
10985131 4946 switch (c) {
c9192973
SH
4947 case ':':
4948 missing_argument(argv[optind - 1]);
4949 return 0;
4581c16f 4950 case '?':
c9192973
SH
4951 unrecognized_option(argv[optind - 1]);
4952 return 0;
4953 case 'h':
10985131
DL
4954 help();
4955 return 0;
4956 case 'V':
4957 printf(QEMU_IMG_VERSION);
4958 return 0;
06a1e0c1
DL
4959 case 'T':
4960 g_free(trace_file);
4961 trace_file = trace_opt_parse(optarg);
4962 break;
153859be 4963 }
ea2384d3 4964 }
153859be 4965
10985131 4966 cmdname = argv[optind];
7db1689c 4967
10985131
DL
4968 /* reset getopt_long scanning */
4969 argc -= optind;
4970 if (argc < 1) {
5f6979cb
JC
4971 return 0;
4972 }
10985131 4973 argv += optind;
d339d766 4974 qemu_reset_optind();
10985131 4975
06a1e0c1
DL
4976 if (!trace_init_backends()) {
4977 exit(1);
4978 }
4979 trace_init_file(trace_file);
4980 qemu_set_log(LOG_TRACE);
4981
10985131
DL
4982 /* find the command */
4983 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
4984 if (!strcmp(cmdname, cmd->name)) {
4985 return cmd->handler(argc, argv);
4986 }
4987 }
7db1689c 4988
153859be 4989 /* not found */
ac1307ab 4990 error_exit("Command not found: %s", cmdname);
ea2384d3 4991}