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