Commit d032044fb97d525ae93fb4c96713a5c467044c17

Authored by Akkarit Sangpetch
Committed by Anthony Liguori
1 parent b171271a

qmu-img: fix qemu-img convert to generate a valid image when the source referenced a backing file

Make 'qemu-img convert' copies unallocated parts of the source image
when -B option was not specified.

Signed-off-by: Akkarit Sangpetch <asangpet@andrew.cmu.edu>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Showing 1 changed file with 13 additions and 7 deletions
qemu-img.c
@@ -747,14 +747,20 @@ static int img_convert(int argc, char **argv) @@ -747,14 +747,20 @@ static int img_convert(int argc, char **argv)
747 n = bs_offset + bs_sectors - sector_num; 747 n = bs_offset + bs_sectors - sector_num;
748 748
749 if (strcmp(drv->format_name, "host_device")) { 749 if (strcmp(drv->format_name, "host_device")) {
750 - if (!bdrv_is_allocated(bs[bs_i], sector_num - bs_offset,  
751 - n, &n1)) {  
752 - sector_num += n1;  
753 - continue; 750 + /* If the output image is being created as a copy on write image,
  751 + assume that sectors which are unallocated in the input image
  752 + are present in both the output's and input's base images (no
  753 + need to copy them). */
  754 + if (out_baseimg) {
  755 + if (!bdrv_is_allocated(bs[bs_i], sector_num - bs_offset,
  756 + n, &n1)) {
  757 + sector_num += n1;
  758 + continue;
  759 + }
  760 + /* The next 'n1' sectors are allocated in the input image. Copy
  761 + only those as they may be followed by unallocated sectors. */
  762 + n = n1;
754 } 763 }
755 - /* The next 'n1' sectors are allocated in the input image. Copy  
756 - only those as they may be followed by unallocated sectors. */  
757 - n = n1;  
758 } else { 764 } else {
759 n1 = n; 765 n1 = n;
760 } 766 }