Commit f58c7b354595228e10f023e86b80a7bab527186a

Authored by ths
1 parent ff7ab59f

New qemu-img convert -B option, by Marc Bevand.


git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@4672 c046a42c-6fe2-441c-8c8c-71466251a162
... ... @@ -884,6 +884,33 @@ void bdrv_flush(BlockDriverState *bs)
884 884 bdrv_flush(bs->backing_hd);
885 885 }
886 886  
  887 +/*
  888 + * Returns true iff the specified sector is present in the disk image. Drivers
  889 + * not implementing the functionality are assumed to not support backing files,
  890 + * hence all their sectors are reported as allocated.
  891 + *
  892 + * 'pnum' is set to the number of sectors (including and immediately following
  893 + * the specified sector) that are known to be in the same
  894 + * allocated/unallocated state.
  895 + *
  896 + * 'nb_sectors' is the max value 'pnum' should be set to.
  897 + */
  898 +int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
  899 + int *pnum)
  900 +{
  901 + int64_t n;
  902 + if (!bs->drv->bdrv_is_allocated) {
  903 + if (sector_num >= bs->total_sectors) {
  904 + *pnum = 0;
  905 + return 0;
  906 + }
  907 + n = bs->total_sectors - sector_num;
  908 + *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
  909 + return 1;
  910 + }
  911 + return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
  912 +}
  913 +
887 914 #ifndef QEMU_IMG
888 915 void bdrv_info(void)
889 916 {
... ...
... ... @@ -99,6 +99,8 @@ int qemu_key_check(BlockDriverState *bs, const char *name);
99 99  
100 100 /* Ensure contents are flushed to disk. */
101 101 void bdrv_flush(BlockDriverState *bs);
  102 +int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
  103 + int *pnum);
102 104  
103 105 #define BDRV_TYPE_HD 0
104 106 #define BDRV_TYPE_CDROM 1
... ...
qemu-img.c
... ... @@ -55,13 +55,17 @@ static void help(void)
55 55 "Command syntax:\n"
56 56 " create [-e] [-6] [-b base_image] [-f fmt] filename [size]\n"
57 57 " commit [-f fmt] filename\n"
58   - " convert [-c] [-e] [-6] [-f fmt] [-O output_fmt] filename [filename2 [...]] output_filename\n"
  58 + " convert [-c] [-e] [-6] [-f fmt] [-O output_fmt] [-B output_base_image] filename [filename2 [...]] output_filename\n"
59 59 " info [-f fmt] filename\n"
60 60 "\n"
61 61 "Command parameters:\n"
62 62 " 'filename' is a disk image filename\n"
63 63 " 'base_image' is the read-only disk image which is used as base for a copy on\n"
64 64 " write image; the copy on write image only stores the modified data\n"
  65 + " 'output_base_image' forces the output image to be created as a copy on write\n"
  66 + " image of the specified base image; 'output_base_image' should have the same\n"
  67 + " content as the input's base image, however the path, image format, etc may\n"
  68 + " differ\n"
65 69 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
66 70 " 'size' is the disk image size in kilobytes. Optional suffixes 'M' (megabyte)\n"
67 71 " and 'G' (gigabyte) are supported\n"
... ... @@ -350,6 +354,13 @@ static int is_not_zero(const uint8_t *sector, int len)
350 354 return 0;
351 355 }
352 356  
  357 +/*
  358 + * Returns true iff the first sector pointed to by 'buf' contains at least
  359 + * a non-NUL byte.
  360 + *
  361 + * 'pnum' is set to the number of sectors (including and immediately following
  362 + * the first one) that are known to be in the same allocated/unallocated state.
  363 + */
353 364 static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
354 365 {
355 366 int v, i;
... ... @@ -373,7 +384,7 @@ static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
373 384 static int img_convert(int argc, char **argv)
374 385 {
375 386 int c, ret, n, n1, bs_n, bs_i, flags, cluster_size, cluster_sectors;
376   - const char *fmt, *out_fmt, *out_filename;
  387 + const char *fmt, *out_fmt, *out_baseimg, *out_filename;
377 388 BlockDriver *drv;
378 389 BlockDriverState **bs, *out_bs;
379 390 int64_t total_sectors, nb_sectors, sector_num, bs_offset;
... ... @@ -384,9 +395,10 @@ static int img_convert(int argc, char **argv)
384 395  
385 396 fmt = NULL;
386 397 out_fmt = "raw";
  398 + out_baseimg = NULL;
387 399 flags = 0;
388 400 for(;;) {
389   - c = getopt(argc, argv, "f:O:hce6");
  401 + c = getopt(argc, argv, "f:O:B:hce6");
390 402 if (c == -1)
391 403 break;
392 404 switch(c) {
... ... @@ -399,6 +411,9 @@ static int img_convert(int argc, char **argv)
399 411 case 'O':
400 412 out_fmt = optarg;
401 413 break;
  414 + case 'B':
  415 + out_baseimg = optarg;
  416 + break;
402 417 case 'c':
403 418 flags |= BLOCK_FLAG_COMPRESS;
404 419 break;
... ... @@ -415,6 +430,9 @@ static int img_convert(int argc, char **argv)
415 430 if (bs_n < 1) help();
416 431  
417 432 out_filename = argv[argc - 1];
  433 +
  434 + if (bs_n > 1 && out_baseimg)
  435 + error("-B makes no sense when concatenating multiple input images");
418 436  
419 437 bs = calloc(bs_n, sizeof(BlockDriverState *));
420 438 if (!bs)
... ... @@ -441,7 +459,7 @@ static int img_convert(int argc, char **argv)
441 459 if (flags & BLOCK_FLAG_ENCRYPT && flags & BLOCK_FLAG_COMPRESS)
442 460 error("Compression and encryption not supported at the same time");
443 461  
444   - ret = bdrv_create(drv, out_filename, total_sectors, NULL, flags);
  462 + ret = bdrv_create(drv, out_filename, total_sectors, out_baseimg, flags);
445 463 if (ret < 0) {
446 464 if (ret == -ENOTSUP) {
447 465 error("Formatting not supported for file format '%s'", fmt);
... ... @@ -520,7 +538,7 @@ static int img_convert(int argc, char **argv)
520 538 /* signal EOF to align */
521 539 bdrv_write_compressed(out_bs, 0, NULL, 0);
522 540 } else {
523   - sector_num = 0;
  541 + sector_num = 0; // total number of sectors converted so far
524 542 for(;;) {
525 543 nb_sectors = total_sectors - sector_num;
526 544 if (nb_sectors <= 0)
... ... @@ -543,6 +561,20 @@ static int img_convert(int argc, char **argv)
543 561 if (n > bs_offset + bs_sectors - sector_num)
544 562 n = bs_offset + bs_sectors - sector_num;
545 563  
  564 + /* If the output image is being created as a copy on write image,
  565 + assume that sectors which are unallocated in the input image
  566 + are present in both the output's and input's base images (no
  567 + need to copy them). */
  568 + if (out_baseimg) {
  569 + if (!bdrv_is_allocated(bs[bs_i], sector_num - bs_offset, n, &n1)) {
  570 + sector_num += n1;
  571 + continue;
  572 + }
  573 + /* The next 'n1' sectors are allocated in the input image. Copy
  574 + only those as they may be followed by unallocated sectors. */
  575 + n = n1;
  576 + }
  577 +
546 578 if (bdrv_read(bs[bs_i], sector_num - bs_offset, buf, n) < 0)
547 579 error("error while reading");
548 580 /* NOTE: at the same time we convert, we do not write zero
... ... @@ -550,7 +582,10 @@ static int img_convert(int argc, char **argv)
550 582 should add a specific call to have the info to go faster */
551 583 buf1 = buf;
552 584 while (n > 0) {
553   - if (is_allocated_sectors(buf1, n, &n1)) {
  585 + /* If the output image is being created as a copy on write image,
  586 + copy all sectors even the ones containing only NUL bytes,
  587 + because they may differ from the sectors in the base image. */
  588 + if (out_baseimg || is_allocated_sectors(buf1, n, &n1)) {
554 589 if (bdrv_write(out_bs, sector_num, buf1, n1) < 0)
555 590 error("error while writing");
556 591 }
... ...
qemu-img.texi
... ... @@ -10,7 +10,7 @@ The following commands are supported:
10 10 @table @option
11 11 @item create [-e] [-6] [-b @var{base_image}] [-f @var{fmt}] @var{filename} [@var{size}]
12 12 @item commit [-f @var{fmt}] @var{filename}
13   -@item convert [-c] [-e] [-6] [-f @var{fmt}] [-O @var{output_fmt}] @var{filename} [@var{filename2} [...]] @var{output_filename}
  13 +@item convert [-c] [-e] [-6] [-f @var{fmt}] [-O @var{output_fmt}] [-B @var{output_base_image}] @var{filename} [@var{filename2} [...]] @var{output_filename}
14 14 @item info [-f @var{fmt}] @var{filename}
15 15 @end table
16 16  
... ... @@ -21,7 +21,11 @@ Command parameters:
21 21 @item base_image
22 22 is the read-only disk image which is used as base for a copy on
23 23 write image; the copy on write image only stores the modified data
24   -
  24 +@item output_base_image
  25 +forces the output image to be created as a copy on write
  26 +image of the specified base image; @code{output_base_image} should have the same
  27 +content as the input's base image, however the path, image format, etc may
  28 +differ
25 29 @item fmt
26 30 is the disk image format. It is guessed automatically in most cases. The following formats are supported:
27 31  
... ...