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