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