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