Skip to contentSkip to conversation

qemu-img: refuse to compress files with invalid size

PATCH In review Stefan Hajnoczi 4 participants 4 messages Apr 12, 2013 – Apr 15, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qemu-img: refuse to compress files with invalid size

Image file compression works at cluster granularity. It is not possible to compress less than a cluster of data at a time.

Print an error when attempting qemu-img convert -c with an input file that is not a multiple of the cluster size.

I considered automatically adjusting the output file size but think it's better to be explicit. This avoids confusion when users notice that image file size changed after conversion.

---
 qemu-img.c | 7 +++++++
 1 file changed, 7 insertions(+)
qemu-img.c +7 -0
index 31627b0..2273851 100644
@@ -1370,6 +1370,13 @@ static int img_convert(int argc, char **argv)
             goto out;
         }
         cluster_sectors = cluster_size >> 9;
+        if (total_sectors % cluster_sectors) {
+            error_report("compression requires that input file size is a "
+                         "multiple of %d bytes",
+                         cluster_size);
+            ret = -1;
+            goto out;
+        }
         sector_num = 0;
 
         nb_sectors = total_sectors;
Signature · 1 line
1.8.1.4
Reviewed-by Benoit Canet on v1 1/1
#1 lore raw
Quoted 37 lines · Stefan Hajnoczi

Image file compression works at cluster granularity. It is not possible to compress less than a cluster of data at a time.

Print an error when attempting qemu-img convert -c with an input file that is not a multiple of the cluster size.

I considered automatically adjusting the output file size but think it's better to be explicit. This avoids confusion when users notice that image file size changed after conversion.

---
 qemu-img.c | 7 +++++++
 1 file changed, 7 insertions(+)
qemu-img.c +7 -0
index 31627b0..2273851 100644
@@ -1370,6 +1370,13 @@ static int img_convert(int argc, char **argv)
             goto out;
         }
         cluster_sectors = cluster_size >> 9;
+        if (total_sectors % cluster_sectors) {
+            error_report("compression requires that input file size is a "
+                         "multiple of %d bytes",
+                         cluster_size);
+            ret = -1;
+            goto out;
+        }
         sector_num = 0;
 
         nb_sectors = total_sectors;
Signature · 1 line
1.8.1.4
#2 lore raw
Quoted 3 lines · Benoît Canet
Quoted 2 lines · Stefan Hajnoczi

Image file compression works at cluster granularity. It is not possible to compress less than a cluster of data at a time.

Shouldn't it be possible to treat a file that is not rounded to a cluster boundary as though the rest of the final cluster is all NUL bytes, and compress that? As long as you know the original size, this operation is reversible, instead of having to error out just because the original wasn't a complete multiple.

Quoted 3 lines
Quoted 3 lines

I considered automatically adjusting the output file size but think it's better to be explicit. This avoids confusion when users notice that image file size changed after conversion.

I agree that changing the final image size on a round trip through compression is not appropriate, but does that mean we have to reject the file completely if it wasn't sized to a multiple to begin with?

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#3 lore raw
Quoted 11 lines · Eric Blake
Quoted 3 lines · Benoît Canet
Quoted 2 lines · Stefan Hajnoczi

Image file compression works at cluster granularity. It is not possible to compress less than a cluster of data at a time.

Shouldn't it be possible to treat a file that is not rounded to a cluster boundary as though the rest of the final cluster is all NUL bytes, and compress that? As long as you know the original size, this operation is reversible, instead of having to error out just because the original wasn't a complete multiple.

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines

I considered automatically adjusting the output file size but think it's better to be explicit. This avoids confusion when users notice that image file size changed after conversion.

I agree that changing the final image size on a round trip through compression is not appropriate, but does that mean we have to reject the file completely if it wasn't sized to a multiple to begin with?

I will send a new patch that does this. kwolf also suggested it on Friday and it sounds like a good idea.

Stefan