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