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