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