qemu-img.c 131 KB
Newer Older
bellard's avatar
bellard committed
1
/*
bellard's avatar
bellard committed
2
 * QEMU disk image utility
3
 *
bellard's avatar
bellard committed
4
 * Copyright (c) 2003-2008 Fabrice Bellard
5
 *
bellard's avatar
bellard committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
Peter Maydell's avatar
Peter Maydell committed
24
#include "qemu/osdep.h"
25
#include "qemu-version.h"
26
#include "qapi/error.h"
27
#include "qapi-visit.h"
28
#include "qapi/qobject-output-visitor.h"
29
#include "qapi/qmp/qerror.h"
30
#include "qapi/qmp/qjson.h"
31
#include "qapi/qmp/qbool.h"
32
#include "qemu/cutils.h"
33
#include "qemu/config-file.h"
34 35
#include "qemu/option.h"
#include "qemu/error-report.h"
36
#include "qemu/log.h"
37
#include "qom/object_interfaces.h"
38
#include "sysemu/sysemu.h"
39
#include "sysemu/block-backend.h"
40
#include "block/block_int.h"
41
#include "block/blockjob.h"
42
#include "block/qapi.h"
43
#include "crypto/init.h"
44
#include "trace/control.h"
45
#include <getopt.h>
46

47
#define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION QEMU_PKGVERSION \
48
                          "\n" QEMU_COPYRIGHT "\n"
49

50
typedef struct img_cmd_t {
51 52
    const char *name;
    int (*handler)(int argc, char **argv);
53
} img_cmd_t;
54

55 56 57
enum {
    OPTION_OUTPUT = 256,
    OPTION_BACKING_CHAIN = 257,
58
    OPTION_OBJECT = 258,
59
    OPTION_IMAGE_OPTS = 259,
60
    OPTION_PATTERN = 260,
61 62
    OPTION_FLUSH_INTERVAL = 261,
    OPTION_NO_DRAIN = 262,
63
    OPTION_TARGET_IMAGE_OPTS = 263,
64 65 66 67 68 69 70
};

typedef enum OutputFormat {
    OFORMAT_JSON,
    OFORMAT_HUMAN,
} OutputFormat;

71
/* Default to cache=writeback as data integrity is not important for qemu-img */
72
#define BDRV_DEFAULT_CACHE "writeback"
73

74
static void format_print(void *opaque, const char *name)
bellard's avatar
bellard committed
75
{
76
    printf(" %s", name);
bellard's avatar
bellard committed
77 78
}

79 80 81 82 83 84 85 86 87 88 89 90 91 92
static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
{
    va_list ap;

    error_printf("qemu-img: ");

    va_start(ap, fmt);
    error_vprintf(fmt, ap);
    va_end(ap);

    error_printf("\nTry 'qemu-img --help' for more information\n");
    exit(EXIT_FAILURE);
}

93 94 95 96 97 98 99 100 101 102
static void QEMU_NORETURN missing_argument(const char *option)
{
    error_exit("missing argument for option '%s'", option);
}

static void QEMU_NORETURN unrecognized_option(const char *option)
{
    error_exit("unrecognized option '%s'", option);
}

blueswir1's avatar
blueswir1 committed
103
/* Please keep in synch with qemu-img.texi */
104
static void QEMU_NORETURN help(void)
bellard's avatar
bellard committed
105
{
106
    const char *help_msg =
107
           QEMU_IMG_VERSION
108
           "usage: qemu-img [standard options] command [command options]\n"
109 110
           "QEMU disk image utility\n"
           "\n"
111 112
           "    '-h', '--help'       display this help and exit\n"
           "    '-V', '--version'    output version information and exit\n"
113 114
           "    '-T', '--trace'      [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
           "                         specify tracing options\n"
115
           "\n"
116
           "Command syntax:\n"
117 118 119 120 121
#define DEF(option, callback, arg_string)        \
           "  " arg_string "\n"
#include "qemu-img-cmds.h"
#undef DEF
#undef GEN_DOCS
122 123 124
           "\n"
           "Command parameters:\n"
           "  'filename' is a disk image filename\n"
125 126 127 128
           "  'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
           "    manual page for a description of the object properties. The most common\n"
           "    object type is a 'secret', which is used to supply passwords and/or\n"
           "    encryption keys.\n"
129
           "  'fmt' is the disk image format. It is guessed automatically in most cases\n"
130
           "  'cache' is the cache mode used to write the output disk image, the valid\n"
131 132
           "    options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
           "    'directsync' and 'unsafe' (default for convert)\n"
133 134
           "  'src_cache' is the cache mode used to read input disk images, the valid\n"
           "    options are the same as for the 'cache' option\n"
135
           "  'size' is the disk image size in bytes. Optional suffixes\n"
136 137 138
           "    'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
           "    'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P)  are\n"
           "    supported. 'b' is ignored.\n"
139 140 141 142 143
           "  'output_filename' is the destination disk image filename\n"
           "  'output_fmt' is the destination format\n"
           "  'options' is a comma separated list of format specific options in a\n"
           "    name=value format. Use -o ? for an overview of the options supported by the\n"
           "    used format\n"
144 145 146 147 148
           "  'snapshot_param' is param used for internal snapshot, format\n"
           "    is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
           "    '[ID_OR_NAME]'\n"
           "  'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n"
           "    instead\n"
149 150 151 152 153
           "  '-c' indicates that target image must be compressed (qcow format only)\n"
           "  '-u' enables unsafe rebasing. It is assumed that old and new backing file\n"
           "       match exactly. The image doesn't need a working backing file before\n"
           "       rebasing in this case (useful for renaming the backing file)\n"
           "  '-h' with or without a command shows this help and lists the supported formats\n"
154
           "  '-p' show progress of command (only certain commands)\n"
155
           "  '-q' use Quiet mode - do not print any output (except errors)\n"
156 157 158 159 160
           "  '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
           "       contain only zeros for qemu-img to create a sparse image during\n"
           "       conversion. If the number of bytes is 0, the source will not be scanned for\n"
           "       unallocated or zero sectors, and the destination image will always be\n"
           "       fully allocated\n"
161
           "  '--output' takes the format in which the output must be done (human or json)\n"
162 163
           "  '-n' skips the target volume creation (useful if the volume is created\n"
           "       prior to running qemu-img)\n"
164
           "\n"
165 166 167 168
           "Parameters to check subcommand:\n"
           "  '-r' tries to repair any inconsistencies that are found during the check.\n"
           "       '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
           "       kinds of errors, with a higher risk of choosing the wrong fix or\n"
169
           "       hiding corruption that has already occurred.\n"
170
           "\n"
171 172 173 174 175
           "Parameters to convert subcommand:\n"
           "  '-m' specifies how many coroutines work in parallel during the convert\n"
           "       process (defaults to 8)\n"
           "  '-W' allow to write to the target out of order rather than sequential\n"
           "\n"
176 177 178 179 180
           "Parameters to snapshot subcommand:\n"
           "  'snapshot' is the name of the snapshot to create, apply or delete\n"
           "  '-a' applies a snapshot (revert disk to saved state)\n"
           "  '-c' creates a snapshot\n"
           "  '-d' deletes a snapshot\n"
181 182 183 184 185
           "  '-l' lists all snapshots in the given image\n"
           "\n"
           "Parameters to compare subcommand:\n"
           "  '-f' first image format\n"
           "  '-F' second image format\n"
186 187 188 189 190 191 192
           "  '-s' run in Strict mode - fail on different image size or sector allocation\n"
           "\n"
           "Parameters to dd subcommand:\n"
           "  'bs=BYTES' read and write up to BYTES bytes at a time "
           "(default: 512)\n"
           "  'count=N' copy only N input blocks\n"
           "  'if=FILE' read from FILE\n"
193 194
           "  'of=FILE' write to FILE\n"
           "  'skip=N' skip N bs-sized blocks at the start of input\n";
195 196

    printf("%s\nSupported formats:", help_msg);
197
    bdrv_iterate_format(format_print, NULL);
bellard's avatar
bellard committed
198
    printf("\n");
199
    exit(EXIT_SUCCESS);
bellard's avatar
bellard committed
200 201
}

202 203 204 205 206 207 208 209 210
static QemuOptsList qemu_object_opts = {
    .name = "object",
    .implied_opt_name = "qom-type",
    .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
    .desc = {
        { }
    },
};

211 212 213 214 215 216 217 218 219
static QemuOptsList qemu_source_opts = {
    .name = "source",
    .implied_opt_name = "file",
    .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head),
    .desc = {
        { }
    },
};

220
static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
221 222 223 224 225 226 227 228 229 230 231
{
    int ret = 0;
    if (!quiet) {
        va_list args;
        va_start(args, fmt);
        ret = vprintf(fmt, args);
        va_end(args);
    }
    return ret;
}

bellard's avatar
bellard committed
232

233 234 235
static int print_block_option_help(const char *filename, const char *fmt)
{
    BlockDriver *drv, *proto_drv;
236
    QemuOptsList *create_opts = NULL;
237
    Error *local_err = NULL;
238 239 240 241

    /* Find driver and parse its options */
    drv = bdrv_find_format(fmt);
    if (!drv) {
242
        error_report("Unknown file format '%s'", fmt);
243 244 245
        return 1;
    }

246
    create_opts = qemu_opts_append(create_opts, drv->create_opts);
247
    if (filename) {
248
        proto_drv = bdrv_find_protocol(filename, true, &local_err);
249
        if (!proto_drv) {
250
            error_report_err(local_err);
251
            qemu_opts_free(create_opts);
252 253
            return 1;
        }
254
        create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
255 256
    }

257 258
    qemu_opts_print_help(create_opts);
    qemu_opts_free(create_opts);
259 260 261
    return 0;
}

262 263

static int img_open_password(BlockBackend *blk, const char *filename,
264
                             int flags, bool quiet)
265 266 267
{
    BlockDriverState *bs;
    char password[256];
268 269

    bs = blk_bs(blk);
270 271
    if (bdrv_is_encrypted(bs) && bdrv_key_required(bs) &&
        !(flags & BDRV_O_NO_IO)) {
272 273 274 275 276 277 278 279 280 281 282 283 284 285
        qprintf(quiet, "Disk image '%s' is encrypted.\n", filename);
        if (qemu_read_password(password, sizeof(password)) < 0) {
            error_report("No password given");
            return -1;
        }
        if (bdrv_set_key(bs, password) < 0) {
            error_report("invalid password");
            return -1;
        }
    }
    return 0;
}


286
static BlockBackend *img_open_opts(const char *optstr,
287
                                   QemuOpts *opts, int flags, bool writethrough,
288
                                   bool quiet, bool force_share)
289 290 291 292 293
{
    QDict *options;
    Error *local_err = NULL;
    BlockBackend *blk;
    options = qemu_opts_to_qdict(opts, NULL);
294 295 296 297
    if (force_share) {
        if (qdict_haskey(options, BDRV_OPT_FORCE_SHARE)
            && !qdict_get_bool(options, BDRV_OPT_FORCE_SHARE)) {
            error_report("--force-share/-U conflicts with image options");
298
            QDECREF(options);
299 300
            return NULL;
        }
301
        qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
302
    }
303
    blk = blk_new_open(NULL, NULL, options, flags, &local_err);
304
    if (!blk) {
305
        error_reportf_err(local_err, "Could not open '%s': ", optstr);
306 307
        return NULL;
    }
308
    blk_set_enable_write_cache(blk, !writethrough);
309

310
    if (img_open_password(blk, optstr, flags, quiet) < 0) {
311 312 313 314 315 316
        blk_unref(blk);
        return NULL;
    }
    return blk;
}

317
static BlockBackend *img_open_file(const char *filename,
318
                                   QDict *options,
319
                                   const char *fmt, int flags,
320 321
                                   bool writethrough, bool quiet,
                                   bool force_share)
322 323
{
    BlockBackend *blk;
324
    Error *local_err = NULL;
325

326 327 328
    if (!options) {
        options = qdict_new();
    }
329
    if (fmt) {
330
        qdict_put_str(options, "driver", fmt);
331
    }
332

333
    if (force_share) {
334
        qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
335
    }
336
    blk = blk_new_open(filename, NULL, options, flags, &local_err);
337
    if (!blk) {
338
        error_reportf_err(local_err, "Could not open '%s': ", filename);
339
        return NULL;
340
    }
341
    blk_set_enable_write_cache(blk, !writethrough);
342

343
    if (img_open_password(blk, filename, flags, quiet) < 0) {
344 345 346 347 348 349 350
        blk_unref(blk);
        return NULL;
    }
    return blk;
}


351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
static int img_add_key_secrets(void *opaque,
                               const char *name, const char *value,
                               Error **errp)
{
    QDict *options = opaque;

    if (g_str_has_suffix(name, "key-secret")) {
        qdict_put(options, name, qstring_from_str(value));
    }

    return 0;
}

static BlockBackend *img_open_new_file(const char *filename,
                                       QemuOpts *create_opts,
                                       const char *fmt, int flags,
                                       bool writethrough, bool quiet,
                                       bool force_share)
{
    QDict *options = NULL;

    options = qdict_new();
    qemu_opt_foreach(create_opts, img_add_key_secrets, options, &error_abort);

    return img_open_file(filename, options, fmt, flags, writethrough, quiet,
                         force_share);
}


380
static BlockBackend *img_open(bool image_opts,
381
                              const char *filename,
382
                              const char *fmt, int flags, bool writethrough,
383
                              bool quiet, bool force_share)
384 385 386 387 388 389 390 391 392 393 394 395 396
{
    BlockBackend *blk;
    if (image_opts) {
        QemuOpts *opts;
        if (fmt) {
            error_report("--image-opts and --format are mutually exclusive");
            return NULL;
        }
        opts = qemu_opts_parse_noisily(qemu_find_opts("source"),
                                       filename, true);
        if (!opts) {
            return NULL;
        }
397 398
        blk = img_open_opts(filename, opts, flags, writethrough, quiet,
                            force_share);
399
    } else {
400
        blk = img_open_file(filename, NULL, fmt, flags, writethrough, quiet,
401
                            force_share);
402
    }
403
    return blk;
404 405
}

406

407
static int add_old_style_options(const char *fmt, QemuOpts *opts,
408 409
                                 const char *base_filename,
                                 const char *base_fmt)
410
{
411 412
    Error *err = NULL;

413
    if (base_filename) {
414
        qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err);
415
        if (err) {
416 417
            error_report("Backing file not supported for file format '%s'",
                         fmt);
418
            error_free(err);
419
            return -1;
420 421 422
        }
    }
    if (base_fmt) {
423
        qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err);
424
        if (err) {
425 426
            error_report("Backing file format not supported for file "
                         "format '%s'", fmt);
427
            error_free(err);
428
            return -1;
429 430
        }
    }
431
    return 0;
432 433
}

434 435
static int64_t cvtnum(const char *s)
{
436
    int err;
437
    uint64_t value;
438

439 440 441 442
    err = qemu_strtosz(s, NULL, &value);
    if (err < 0) {
        return err;
    }
443 444 445
    if (value > INT64_MAX) {
        return -ERANGE;
    }
446
    return value;
447 448
}

bellard's avatar
bellard committed
449 450
static int img_create(int argc, char **argv)
{
451
    int c;
452
    uint64_t img_size = -1;
bellard's avatar
bellard committed
453
    const char *fmt = "raw";
454
    const char *base_fmt = NULL;
bellard's avatar
bellard committed
455 456
    const char *filename;
    const char *base_filename = NULL;
457
    char *options = NULL;
458
    Error *local_err = NULL;
459
    bool quiet = false;
460

bellard's avatar
bellard committed
461
    for(;;) {
462 463 464 465 466
        static const struct option long_options[] = {
            {"help", no_argument, 0, 'h'},
            {"object", required_argument, 0, OPTION_OBJECT},
            {0, 0, 0, 0}
        };
467
        c = getopt_long(argc, argv, ":F:b:f:he6o:q",
468
                        long_options, NULL);
469
        if (c == -1) {
bellard's avatar
bellard committed
470
            break;
471
        }
bellard's avatar
bellard committed
472
        switch(c) {
473 474 475
        case ':':
            missing_argument(argv[optind - 1]);
            break;
476
        case '?':
477 478
            unrecognized_option(argv[optind - 1]);
            break;
bellard's avatar
bellard committed
479 480 481
        case 'h':
            help();
            break;
482 483 484
        case 'F':
            base_fmt = optarg;
            break;
bellard's avatar
bellard committed
485 486 487 488 489 490 491
        case 'b':
            base_filename = optarg;
            break;
        case 'f':
            fmt = optarg;
            break;
        case 'e':
492
            error_report("option -e is deprecated, please use \'-o "
493
                  "encryption\' instead!");
494
            goto fail;
495
        case '6':
496
            error_report("option -6 is deprecated, please use \'-o "
497
                  "compat6\' instead!");
498
            goto fail;
499
        case 'o':
500 501 502 503 504 505 506 507 508 509 510
            if (!is_valid_option_list(optarg)) {
                error_report("Invalid option list: %s", optarg);
                goto fail;
            }
            if (!options) {
                options = g_strdup(optarg);
            } else {
                char *old_options = options;
                options = g_strdup_printf("%s,%s", options, optarg);
                g_free(old_options);
            }
511
            break;
512 513 514
        case 'q':
            quiet = true;
            break;
515 516 517 518 519 520 521 522
        case OPTION_OBJECT: {
            QemuOpts *opts;
            opts = qemu_opts_parse_noisily(&qemu_object_opts,
                                           optarg, true);
            if (!opts) {
                goto fail;
            }
        }   break;
bellard's avatar
bellard committed
523 524
        }
    }
525

526
    /* Get the filename */
527 528 529 530 531 532
    filename = (optind < argc) ? argv[optind] : NULL;
    if (options && has_help_option(options)) {
        g_free(options);
        return print_block_option_help(filename, fmt);
    }

533
    if (optind >= argc) {
534
        error_exit("Expecting image file name");
535
    }
536
    optind++;
537

538 539
    if (qemu_opts_foreach(&qemu_object_opts,
                          user_creatable_add_opts_foreach,
540
                          NULL, NULL)) {
541 542 543
        goto fail;
    }

544 545
    /* Get image size, if specified */
    if (optind < argc) {
546
        int64_t sval;
547 548 549

        sval = cvtnum(argv[optind++]);
        if (sval < 0) {
550 551 552 553
            if (sval == -ERANGE) {
                error_report("Image size must be less than 8 EiB!");
            } else {
                error_report("Invalid image size specified! You may use k, M, "
554 555 556
                      "G, T, P or E suffixes for ");
                error_report("kilobytes, megabytes, gigabytes, terabytes, "
                             "petabytes and exabytes.");
557
            }
558
            goto fail;
559 560 561
        }
        img_size = (uint64_t)sval;
    }
562
    if (optind != argc) {
563
        error_exit("Unexpected argument: %s", argv[optind]);
564
    }
565

566
    bdrv_img_create(filename, fmt, base_filename, base_fmt,
567
                    options, img_size, 0, quiet, &local_err);
568
    if (local_err) {
569
        error_reportf_err(local_err, "%s: ", filename);
570
        goto fail;
571
    }
572

573
    g_free(options);
bellard's avatar
bellard committed
574
    return 0;
575 576 577 578

fail:
    g_free(options);
    return 1;
bellard's avatar
bellard committed
579 580
}

581
static void dump_json_image_check(ImageCheck *check, bool quiet)
582 583 584
{
    QString *str;
    QObject *obj;
585
    Visitor *v = qobject_output_visitor_new(&obj);
586 587 588

    visit_type_ImageCheck(v, NULL, &check, &error_abort);
    visit_complete(v, &obj);
589 590
    str = qobject_to_json_pretty(obj);
    assert(str != NULL);
591
    qprintf(quiet, "%s\n", qstring_get_str(str));
592
    qobject_decref(obj);
593
    visit_free(v);
594 595 596
    QDECREF(str);
}

597
static void dump_human_image_check(ImageCheck *check, bool quiet)
598 599
{
    if (!(check->corruptions || check->leaks || check->check_errors)) {
600
        qprintf(quiet, "No errors were found on the image.\n");
601 602
    } else {
        if (check->corruptions) {
603 604 605 606
            qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
                    "Data may be corrupted, or further writes to the image "
                    "may corrupt it.\n",
                    check->corruptions);
607 608 609
        }

        if (check->leaks) {
610 611 612 613
            qprintf(quiet,
                    "\n%" PRId64 " leaked clusters were found on the image.\n"
                    "This means waste of disk space, but no harm to data.\n",
                    check->leaks);
614 615 616
        }

        if (check->check_errors) {
617 618 619 620
            qprintf(quiet,
                    "\n%" PRId64
                    " internal errors have occurred during the check.\n",
                    check->check_errors);
621 622 623 624
        }
    }

    if (check->total_clusters != 0 && check->allocated_clusters != 0) {
625 626 627 628 629 630 631
        qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
                "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
                check->allocated_clusters, check->total_clusters,
                check->allocated_clusters * 100.0 / check->total_clusters,
                check->fragmented_clusters * 100.0 / check->allocated_clusters,
                check->compressed_clusters * 100.0 /
                check->allocated_clusters);
632 633 634
    }

    if (check->image_end_offset) {
635 636
        qprintf(quiet,
                "Image end offset: %" PRId64 "\n", check->image_end_offset);
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
    }
}

static int collect_image_check(BlockDriverState *bs,
                   ImageCheck *check,
                   const char *filename,
                   const char *fmt,
                   int fix)
{
    int ret;
    BdrvCheckResult result;

    ret = bdrv_check(bs, &result, fix);
    if (ret < 0) {
        return ret;
    }

    check->filename                 = g_strdup(filename);
    check->format                   = g_strdup(bdrv_get_format_name(bs));
    check->check_errors             = result.check_errors;
    check->corruptions              = result.corruptions;
    check->has_corruptions          = result.corruptions != 0;
    check->leaks                    = result.leaks;
    check->has_leaks                = result.leaks != 0;
    check->corruptions_fixed        = result.corruptions_fixed;
    check->has_corruptions_fixed    = result.corruptions != 0;
    check->leaks_fixed              = result.leaks_fixed;
    check->has_leaks_fixed          = result.leaks != 0;
    check->image_end_offset         = result.image_end_offset;
    check->has_image_end_offset     = result.image_end_offset != 0;
    check->total_clusters           = result.bfi.total_clusters;
    check->has_total_clusters       = result.bfi.total_clusters != 0;
    check->allocated_clusters       = result.bfi.allocated_clusters;
    check->has_allocated_clusters   = result.bfi.allocated_clusters != 0;
    check->fragmented_clusters      = result.bfi.fragmented_clusters;
    check->has_fragmented_clusters  = result.bfi.fragmented_clusters != 0;
673 674
    check->compressed_clusters      = result.bfi.compressed_clusters;
    check->has_compressed_clusters  = result.bfi.compressed_clusters != 0;
675 676 677 678

    return 0;
}

679 680 681
/*
 * Checks an image for consistency. Exit codes:
 *
682 683 684 685 686
 *  0 - Check completed, image is good
 *  1 - Check not completed because of internal errors
 *  2 - Check completed, image is corrupted
 *  3 - Check completed, image has leaked clusters, but is good otherwise
 * 63 - Checks are not supported by the image format
687
 */
688 689 690
static int img_check(int argc, char **argv)
{
    int c, ret;
691
    OutputFormat output_format = OFORMAT_HUMAN;
692
    const char *filename, *fmt, *output, *cache;
693
    BlockBackend *blk;
694
    BlockDriverState *bs;
695
    int fix = 0;
696 697
    int flags = BDRV_O_CHECK;
    bool writethrough;
698
    ImageCheck *check;
699
    bool quiet = false;
700
    bool image_opts = false;
701
    bool force_share = false;
702 703

    fmt = NULL;
704
    output = NULL;
705
    cache = BDRV_DEFAULT_CACHE;
706

707
    for(;;) {
708 709 710 711
        int option_index = 0;
        static const struct option long_options[] = {
            {"help", no_argument, 0, 'h'},
            {"format", required_argument, 0, 'f'},
712
            {"repair", required_argument, 0, 'r'},
713
            {"output", required_argument, 0, OPTION_OUTPUT},
714
            {"object", required_argument, 0, OPTION_OBJECT},
715
            {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
716
            {"force-share", no_argument, 0, 'U'},
717 718
            {0, 0, 0, 0}
        };
719
        c = getopt_long(argc, argv, ":hf:r:T:qU",
720
                        long_options, &option_index);
721
        if (c == -1) {
722
            break;
723
        }
724
        switch(c) {
725 726 727
        case ':':
            missing_argument(argv[optind - 1]);
            break;
728
        case '?':
729 730
            unrecognized_option(argv[optind - 1]);
            break;
731 732 733 734 735 736
        case 'h':
            help();
            break;
        case 'f':
            fmt = optarg;
            break;
737 738 739 740 741 742 743 744
        case 'r':
            flags |= BDRV_O_RDWR;

            if (!strcmp(optarg, "leaks")) {
                fix = BDRV_FIX_LEAKS;
            } else if (!strcmp(optarg, "all")) {
                fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
            } else {
745 746
                error_exit("Unknown option value for -r "
                           "(expecting 'leaks' or 'all'): %s", optarg);
747 748
            }
            break;
749 750 751
        case OPTION_OUTPUT:
            output = optarg;
            break;
752 753 754
        case 'T':
            cache = optarg;
            break;
755 756 757
        case 'q':
            quiet = true;
            break;
758 759 760
        case 'U':
            force_share = true;
            break;
761 762 763 764 765 766 767 768
        case OPTION_OBJECT: {
            QemuOpts *opts;
            opts = qemu_opts_parse_noisily(&qemu_object_opts,
                                           optarg, true);
            if (!opts) {
                return 1;
            }
        }   break;
769 770 771
        case OPTION_IMAGE_OPTS:
            image_opts = true;
            break;
772 773
        }
    }
774
    if (optind != argc - 1) {
775
        error_exit("Expecting one image file name");
776
    }
777 778
    filename = argv[optind++];

779 780 781 782 783 784 785 786 787
    if (output && !strcmp(output, "json")) {
        output_format = OFORMAT_JSON;
    } else if (output && !strcmp(output, "human")) {
        output_format = OFORMAT_HUMAN;
    } else if (output) {
        error_report("--output must be used with human or json as argument.");
        return 1;
    }

788 789
    if (qemu_opts_foreach(&qemu_object_opts,
                          user_creatable_add_opts_foreach,
790
                          NULL, NULL)) {
791 792 793
        return 1;
    }

794
    ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
795 796 797 798 799
    if (ret < 0) {
        error_report("Invalid source cache option: %s", cache);
        return 1;
    }

800 801
    blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
                   force_share);
802 803
    if (!blk) {
        return 1;
804
    }
805
    bs = blk_bs(blk);
806 807 808

    check = g_new0(ImageCheck, 1);
    ret = collect_image_check(bs, check, filename, fmt, fix);
809 810

    if (ret == -ENOTSUP) {
811
        error_report("This image format does not support checks");
812
        ret = 63;
813
        goto fail;
814 815
    }

816 817
    if (check->corruptions_fixed || check->leaks_fixed) {
        int corruptions_fixed, leaks_fixed;
818

819 820
        leaks_fixed         = check->leaks_fixed;
        corruptions_fixed   = check->corruptions_fixed;
821

822
        if (output_format == OFORMAT_HUMAN) {
823 824 825 826 827 828 829
            qprintf(quiet,
                    "The following inconsistencies were found and repaired:\n\n"
                    "    %" PRId64 " leaked clusters\n"
                    "    %" PRId64 " corruptions\n\n"
                    "Double checking the fixed image now...\n",
                    check->leaks_fixed,
                    check->corruptions_fixed);
830 831
        }

832
        ret = collect_image_check(bs, check, filename, fmt, 0);
833

834 835
        check->leaks_fixed          = leaks_fixed;
        check->corruptions_fixed    = corruptions_fixed;
836 837
    }

838 839 840 841 842 843 844 845 846
    if (!ret) {
        switch (output_format) {
        case OFORMAT_HUMAN:
            dump_human_image_check(check, quiet);
            break;
        case OFORMAT_JSON:
            dump_json_image_check(check, quiet);
            break;
        }
847 848
    }

849
    if (ret || check->check_errors) {
850 851 852 853 854
        if (ret) {
            error_report("Check failed: %s", strerror(-ret));
        } else {
            error_report("Check failed");
        }
855 856
        ret = 1;
        goto fail;
857
    }
858

859 860 861 862
    if (check->corruptions) {
        ret = 2;
    } else if (check->leaks) {
        ret = 3;
863
    } else {
864
        ret = 0;
865
    }
866 867 868

fail:
    qapi_free_ImageCheck(check);
869
    blk_unref(blk);
870
    return ret;
871 872
}

873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
typedef struct CommonBlockJobCBInfo {
    BlockDriverState *bs;
    Error **errp;
} CommonBlockJobCBInfo;

static void common_block_job_cb(void *opaque, int ret)
{
    CommonBlockJobCBInfo *cbi = opaque;

    if (ret < 0) {
        error_setg_errno(cbi->errp, -ret, "Block job failed");
    }
}

static void run_block_job(BlockJob *job, Error **errp)
{
889
    AioContext *aio_context = blk_get_aio_context(job->blk);
890

891 892
    /* FIXME In error cases, the job simply goes away and we access a dangling
     * pointer below. */
893
    aio_context_acquire(aio_context);
894 895
    do {
        aio_poll(aio_context, true);
896 897
        qemu_progress_print(job->len ?
                            ((float)job->offset / job->len * 100.f) : 0.0f, 0);
898 899 900
    } while (!job->ready);

    block_job_complete_sync(job, errp);
901
    aio_context_release(aio_context);
902 903 904 905

    /* A block job may finish instantaneously without publishing any progress,
     * so just signal completion here */
    qemu_progress_print(100.f, 0);
906 907
}

bellard's avatar
bellard committed
908 909
static int img_commit(int argc, char **argv)
{
910
    int c, ret, flags;
911
    const char *filename, *fmt, *cache, *base;
912
    BlockBackend *blk;
913
    BlockDriverState *bs, *base_bs;
914
    BlockJob *job;
915
    bool progress = false, quiet = false, drop = false;
916
    bool writethrough;
917 918
    Error *local_err = NULL;
    CommonBlockJobCBInfo cbi;
919
    bool image_opts = false;
920
    AioContext *aio_context;
bellard's avatar
bellard committed
921 922

    fmt = NULL;
923
    cache = BDRV_DEFAULT_CACHE;
924
    base = NULL;
bellard's avatar
bellard committed
925
    for(;;) {
926 927 928
        static const struct option long_options[] = {
            {"help", no_argument, 0, 'h'},
            {"object", required_argument, 0, OPTION_OBJECT},
929
            {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
930 931
            {0, 0, 0, 0}
        };
932
        c = getopt_long(argc, argv, ":f:ht:b:dpq",
933
                        long_options, NULL);
934
        if (c == -1) {
bellard's avatar
bellard committed
935
            break;
936
        }
bellard's avatar
bellard committed
937
        switch(c) {
938 939 940
        case ':':
            missing_argument(argv[optind - 1]);
            break;
941
        case '?':
942 943
            unrecognized_option(argv[optind - 1]);
            break;
bellard's avatar
bellard committed
944 945 946 947 948 949
        case 'h':
            help();
            break;
        case 'f':
            fmt = optarg;
            break;
950 951 952
        case 't':
            cache = optarg;
            break;
953 954 955 956 957
        case 'b':
            base = optarg;
            /* -b implies -d */
            drop = true;
            break;
958 959 960
        case 'd':
            drop = true;
            break;
961 962 963
        case 'p':
            progress = true;
            break;
964 965 966
        case 'q':
            quiet = true;
            break;
967 968 969 970 971 972 973 974
        case OPTION_OBJECT: {
            QemuOpts *opts;
            opts = qemu_opts_parse_noisily(&qemu_object_opts,
                                           optarg, true);
            if (!opts) {
                return 1;
            }
        }   break;
975 976 977
        case OPTION_IMAGE_OPTS:
            image_opts = true;
            break;
bellard's avatar
bellard committed
978 979
        }
    }
980 981 982 983 984 985

    /* Progress is not shown in Quiet mode */
    if (quiet) {
        progress = false;
    }

986
    if (optind != argc - 1) {
987
        error_exit("Expecting one image file name");
988
    }
bellard's avatar
bellard committed
989 990
    filename = argv[optind++];

991 992
    if (qemu_opts_foreach(&qemu_object_opts,
                          user_creatable_add_opts_foreach,
993
                          NULL, NULL)) {
994 995 996
        return 1;
    }

997
    flags = BDRV_O_RDWR | BDRV_O_UNMAP;
998
    ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
999 1000
    if (ret < 0) {
        error_report("Invalid cache option: %s", cache);
1001
        return 1;
1002 1003
    }

1004 1005
    blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
                   false);
1006 1007
    if (!blk) {
        return 1;
1008
    }
1009 1010
    bs = blk_bs(blk);

1011 1012 1013
    qemu_progress_init(progress, 1.f);
    qemu_progress_print(0.f, 100);

1014 1015 1016
    if (base) {
        base_bs = bdrv_find_backing_image(bs, base);
        if (!base_bs) {
1017 1018 1019
            error_setg(&local_err,
                       "Did not find '%s' in the backing chain of '%s'",
                       base, filename);
1020 1021 1022 1023 1024 1025
            goto done;
        }
    } else {
        /* This is different from QMP, which by default uses the deepest file in
         * the backing chain (i.e., the very base); however, the traditional
         * behavior of qemu-img commit is using the immediate backing file. */
1026
        base_bs = backing_bs(bs);
1027 1028 1029 1030
        if (!base_bs) {
            error_setg(&local_err, "Image does not have a backing file");
            goto done;
        }
1031 1032 1033 1034 1035 1036 1037
    }

    cbi = (CommonBlockJobCBInfo){
        .errp = &local_err,
        .bs   = bs,
    };

1038 1039
    aio_context = bdrv_get_aio_context(bs);
    aio_context_acquire(aio_context);
1040
    commit_active_start("commit", bs, base_bs, BLOCK_JOB_DEFAULT, 0,
1041
                        BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb,
1042
                        &cbi, false, &local_err);
1043
    aio_context_release(aio_context);
1044 1045
    if (local_err) {
        goto done;
bellard's avatar
bellard committed
1046 1047
    }

1048 1049 1050 1051
    /* When the block job completes, the BlockBackend reference will point to
     * the old backing file. In order to avoid that the top image is already
     * deleted, so we can still empty it afterwards, increment the reference
     * counter here preemptively. */
1052
    if (!drop) {
1053
        bdrv_ref(bs);
1054 1055
    }

1056 1057
    job = block_job_get("commit");
    run_block_job(job, &local_err);
1058 1059 1060 1061
    if (local_err) {
        goto unref_backing;
    }

1062 1063
    if (!drop && bs->drv->bdrv_make_empty) {
        ret = bs->drv->bdrv_make_empty(bs);
1064 1065 1066 1067 1068 1069 1070 1071 1072
        if (ret) {
            error_setg_errno(&local_err, -ret, "Could not empty %s",
                             filename);
            goto unref_backing;
        }
    }

unref_backing:
    if (!drop) {
1073
        bdrv_unref(bs);
1074
    }
1075 1076

done:
1077 1078
    qemu_progress_end();

1079
    blk_unref(blk);
1080 1081

    if (local_err) {
1082
        error_report_err(local_err);
1083 1084
        return 1;
    }
1085 1086

    qprintf(quiet, "Image committed.\n");
bellard's avatar
bellard committed
1087 1088 1089
    return 0;
}

1090 1091 1092 1093 1094 1095 1096
/*
 * Returns true iff the first sector pointed to by 'buf' contains at least
 * a non-NUL byte.
 *
 * 'pnum' is set to the number of sectors (including and immediately following
 * the first one) that are known to be in the same allocated/unallocated state.
 */
bellard's avatar
bellard committed
1097 1098
static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
{
1099 1100
    bool is_zero;
    int i;
bellard's avatar
bellard committed
1101 1102 1103 1104 1105

    if (n <= 0) {
        *pnum = 0;
        return 0;
    }
1106
    is_zero = buffer_is_zero(buf, 512);
bellard's avatar
bellard committed
1107 1108
    for(i = 1; i < n; i++) {
        buf += 512;
1109
        if (is_zero != buffer_is_zero(buf, 512)) {
bellard's avatar
bellard committed
1110
            break;
1111
        }
bellard's avatar
bellard committed
1112 1113
    }
    *pnum = i;
1114
    return !is_zero;
bellard's avatar
bellard committed
1115 1116
}

1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
/*
 * Like is_allocated_sectors, but if the buffer starts with a used sector,
 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
 * breaking up write requests for only small sparse areas.
 */
static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
    int min)
{
    int ret;
    int num_checked, num_used;

    if (n < min) {
        min = n;
    }

    ret = is_allocated_sectors(buf, n, pnum);
    if (!ret) {
        return ret;
    }

    num_used = *pnum;
    buf += BDRV_SECTOR_SIZE * *pnum;
    n -= *pnum;
    num_checked = num_used;

    while (n > 0) {
        ret = is_allocated_sectors(buf, n, pnum);

        buf += BDRV_SECTOR_SIZE * *pnum;
        n -= *pnum;
        num_checked += *pnum;
        if (ret) {
            num_used = num_checked;
        } else if (*pnum >= min) {
            break;
        }
    }

    *pnum = num_used;
    return 1;
}

Kevin Wolf's avatar
Kevin Wolf committed
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
/*
 * Compares two buffers sector by sector. Returns 0 if the first sector of both
 * buffers matches, non-zero otherwise.
 *
 * pnum is set to the number of sectors (including and immediately following
 * the first one) that are known to have the same comparison result
 */
static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
    int *pnum)
{
1169 1170
    bool res;
    int i;
Kevin Wolf's avatar
Kevin Wolf committed
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190

    if (n <= 0) {
        *pnum = 0;
        return 0;
    }

    res = !!memcmp(buf1, buf2, 512);
    for(i = 1; i < n; i++) {
        buf1 += 512;
        buf2 += 512;

        if (!!memcmp(buf1, buf2, 512) != res) {
            break;
        }
    }

    *pnum = i;
    return res;
}

1191
#define IO_BUF_SIZE (2 * 1024 * 1024)
bellard's avatar
bellard committed
1192

1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
static int64_t sectors_to_bytes(int64_t sectors)
{
    return sectors << BDRV_SECTOR_BITS;
}

static int64_t sectors_to_process(int64_t total, int64_t from)
{
    return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
}

/*
 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
 *
 * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero
 * data and negative value on error.
 *
1209
 * @param blk:  BlockBackend for the image
1210 1211 1212 1213 1214 1215
 * @param sect_num: Number of first sector to check
 * @param sect_count: Number of sectors to check
 * @param filename: Name of disk file we are checking (logging purpose)
 * @param buffer: Allocated buffer for storing read data
 * @param quiet: Flag for quiet mode
 */
1216
static int check_empty_sectors(BlockBackend *blk, int64_t sect_num,
1217 1218 1219 1220
                               int sect_count, const char *filename,
                               uint8_t *buffer, bool quiet)
{
    int pnum, ret = 0;
1221 1222
    ret = blk_pread(blk, sect_num << BDRV_SECTOR_BITS, buffer,
                    sect_count << BDRV_SECTOR_BITS);
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
    if (ret < 0) {
        error_report("Error while reading offset %" PRId64 " of %s: %s",
                     sectors_to_bytes(sect_num), filename, strerror(-ret));
        return ret;
    }
    ret = is_allocated_sectors(buffer, sect_count, &pnum);
    if (ret || pnum != sect_count) {
        qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
                sectors_to_bytes(ret ? sect_num : sect_num + pnum));
        return 1;
    }

    return 0;
}

/*
 * Compares two images. Exit codes:
 *
 * 0 - Images are identical
 * 1 - Images differ
 * >1 - Error occurred
 */
static int img_compare(int argc, char **argv)
{
1247
    const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
1248
    BlockBackend *blk1, *blk2;
1249 1250 1251 1252 1253 1254 1255
    BlockDriverState *bs1, *bs2;
    int64_t total_sectors1, total_sectors2;
    uint8_t *buf1 = NULL, *buf2 = NULL;
    int pnum1, pnum2;
    int allocated1, allocated2;
    int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
    bool progress = false, quiet = false, strict = false;
1256
    int flags;
1257
    bool writethrough;
1258 1259 1260 1261 1262
    int64_t total_sectors;
    int64_t sector_num = 0;
    int64_t nb_sectors;
    int c, pnum;
    uint64_t progress_base;
1263
    bool image_opts = false;
1264
    bool force_share = false;
1265

1266
    cache = BDRV_DEFAULT_CACHE;
1267
    for (;;) {
1268 1269 1270
        static const struct option long_options[] = {
            {"help", no_argument, 0, 'h'},
            {"object", required_argument, 0, OPTION_OBJECT},
1271
            {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
1272