Revision 661a0f71 qemu-img.c

b/qemu-img.c
40 40

  
41 41
/* Default to cache=writeback as data integrity is not important for qemu-tcg. */
42 42
#define BDRV_O_FLAGS BDRV_O_CACHE_WB
43
#define BDRV_DEFAULT_CACHE "writeback"
43 44

  
44 45
static void format_print(void *opaque, const char *name)
45 46
{
......
64 65
           "Command parameters:\n"
65 66
           "  'filename' is a disk image filename\n"
66 67
           "  'fmt' is the disk image format. It is guessed automatically in most cases\n"
68
           "  'cache' is the cache mode used to write the output disk image, the valid\n"
69
           "    options are: 'none', 'writeback' (default), 'writethrough' and 'unsafe'\n"
67 70
           "  'size' is the disk image size in bytes. Optional suffixes\n"
68 71
           "    'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M)\n"
69 72
           "    and T (terabyte, 1024G) are supported. 'b' is ignored.\n"
......
180 183
}
181 184
#endif
182 185

  
186
static int set_cache_flag(const char *mode, int *flags)
187
{
188
    *flags &= ~BDRV_O_CACHE_MASK;
189

  
190
    if (!strcmp(mode, "none") || !strcmp(mode, "off")) {
191
        *flags |= BDRV_O_CACHE_WB;
192
        *flags |= BDRV_O_NOCACHE;
193
    } else if (!strcmp(mode, "writeback")) {
194
        *flags |= BDRV_O_CACHE_WB;
195
    } else if (!strcmp(mode, "unsafe")) {
196
        *flags |= BDRV_O_CACHE_WB;
197
        *flags |= BDRV_O_NO_FLUSH;
198
    } else if (!strcmp(mode, "writethrough")) {
199
        /* this is the default */
200
    } else {
201
        return -1;
202
    }
203

  
204
    return 0;
205
}
206

  
183 207
static int print_block_option_help(const char *filename, const char *fmt)
184 208
{
185 209
    BlockDriver *drv, *proto_drv;
......
441 465

  
442 466
static int img_commit(int argc, char **argv)
443 467
{
444
    int c, ret;
445
    const char *filename, *fmt;
468
    int c, ret, flags;
469
    const char *filename, *fmt, *cache;
446 470
    BlockDriverState *bs;
447 471

  
448 472
    fmt = NULL;
473
    cache = BDRV_DEFAULT_CACHE;
449 474
    for(;;) {
450
        c = getopt(argc, argv, "f:h");
475
        c = getopt(argc, argv, "f:ht:");
451 476
        if (c == -1) {
452 477
            break;
453 478
        }
......
459 484
        case 'f':
460 485
            fmt = optarg;
461 486
            break;
487
        case 't':
488
            cache = optarg;
489
            break;
462 490
        }
463 491
    }
464 492
    if (optind >= argc) {
......
466 494
    }
467 495
    filename = argv[optind++];
468 496

  
469
    bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR);
497
    flags = BDRV_O_RDWR;
498
    ret = set_cache_flag(cache, &flags);
499
    if (ret < 0) {
500
        error_report("Invalid cache option: %s", cache);
501
        return -1;
502
    }
503

  
504
    bs = bdrv_new_open(filename, fmt, flags);
470 505
    if (!bs) {
471 506
        return 1;
472 507
    }
......
591 626
static int img_convert(int argc, char **argv)
592 627
{
593 628
    int c, ret = 0, n, n1, bs_n, bs_i, compress, cluster_size, cluster_sectors;
594
    int progress = 0;
595
    const char *fmt, *out_fmt, *out_baseimg, *out_filename;
629
    int progress = 0, flags;
630
    const char *fmt, *out_fmt, *cache, *out_baseimg, *out_filename;
596 631
    BlockDriver *drv, *proto_drv;
597 632
    BlockDriverState **bs = NULL, *out_bs = NULL;
598 633
    int64_t total_sectors, nb_sectors, sector_num, bs_offset;
......
608 643

  
609 644
    fmt = NULL;
610 645
    out_fmt = "raw";
646
    cache = "unsafe";
611 647
    out_baseimg = NULL;
612 648
    compress = 0;
613 649
    for(;;) {
614
        c = getopt(argc, argv, "f:O:B:s:hce6o:p");
650
        c = getopt(argc, argv, "f:O:B:s:hce6o:pt:");
615 651
        if (c == -1) {
616 652
            break;
617 653
        }
......
649 685
        case 'p':
650 686
            progress = 1;
651 687
            break;
688
        case 't':
689
            cache = optarg;
690
            break;
652 691
        }
653 692
    }
654 693

  
......
779 818
        goto out;
780 819
    }
781 820

  
782
    out_bs = bdrv_new_open(out_filename, out_fmt,
783
        BDRV_O_FLAGS | BDRV_O_RDWR | BDRV_O_NO_FLUSH);
821
    flags = BDRV_O_RDWR;
822
    ret = set_cache_flag(cache, &flags);
823
    if (ret < 0) {
824
        error_report("Invalid cache option: %s", cache);
825
        return -1;
826
    }
827

  
828
    out_bs = bdrv_new_open(out_filename, out_fmt, flags);
784 829
    if (!out_bs) {
785 830
        ret = -1;
786 831
        goto out;
......
1225 1270
    BlockDriverState *bs, *bs_old_backing = NULL, *bs_new_backing = NULL;
1226 1271
    BlockDriver *old_backing_drv, *new_backing_drv;
1227 1272
    char *filename;
1228
    const char *fmt, *out_basefmt, *out_baseimg;
1273
    const char *fmt, *cache, *out_basefmt, *out_baseimg;
1229 1274
    int c, flags, ret;
1230 1275
    int unsafe = 0;
1231 1276
    int progress = 0;
1232 1277

  
1233 1278
    /* Parse commandline parameters */
1234 1279
    fmt = NULL;
1280
    cache = BDRV_DEFAULT_CACHE;
1235 1281
    out_baseimg = NULL;
1236 1282
    out_basefmt = NULL;
1237

  
1238 1283
    for(;;) {
1239
        c = getopt(argc, argv, "uhf:F:b:p");
1284
        c = getopt(argc, argv, "uhf:F:b:pt:");
1240 1285
        if (c == -1) {
1241 1286
            break;
1242 1287
        }
......
1260 1305
        case 'p':
1261 1306
            progress = 1;
1262 1307
            break;
1308
        case 't':
1309
            cache = optarg;
1310
            break;
1263 1311
        }
1264 1312
    }
1265 1313

  
......
1271 1319
    qemu_progress_init(progress, 2.0);
1272 1320
    qemu_progress_print(0, 100);
1273 1321

  
1322
    flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
1323
    ret = set_cache_flag(cache, &flags);
1324
    if (ret < 0) {
1325
        error_report("Invalid cache option: %s", cache);
1326
        return -1;
1327
    }
1328

  
1274 1329
    /*
1275 1330
     * Open the images.
1276 1331
     *
1277 1332
     * Ignore the old backing file for unsafe rebase in case we want to correct
1278 1333
     * the reference to a renamed or moved backing file.
1279 1334
     */
1280
    flags = BDRV_O_FLAGS | BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
1281 1335
    bs = bdrv_new_open(filename, fmt, flags);
1282 1336
    if (!bs) {
1283 1337
        return 1;

Also available in: Unified diff