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