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