Skip to contentSkip to conversation

qemu-nbd: Shrink image size by specified offset

PATCH In review Tomáš Golembiovský 3 participants 5 messages Sep 20, 2016 – Oct 3, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 qemu-nbd: Shrink image size by specified offset

When --offset is set the apparent device size has to be adjusted accordingly. Otherwise client may request read/write beyond the file end which would fail.

---
 qemu-nbd.c | 7 +++++++
 1 file changed, 7 insertions(+)
qemu-nbd.c +7 -0
index 99297a5..629bce1 100644
@@ -901,6 +901,13 @@ int main(int argc, char **argv)
         exit(EXIT_FAILURE);
     }
 
+    if (dev_offset >= fd_size) {
+        error_report("Offset (%lu) has to be smaller than the image size (%lu)",
+                     dev_offset, fd_size);
+        exit(EXIT_FAILURE);
+    }
+    fd_size -= dev_offset;
+
     if (partition != -1) {
         ret = find_partition(blk, partition, &dev_offset, &fd_size);
         if (ret < 0) {
Signature · 1 line
2.9.3
Reviewed-by on v1 1/2
#1 lore raw
Quoted 18 lines · Tomáš Golembiovský

When --offset is set the apparent device size has to be adjusted accordingly. Otherwise client may request read/write beyond the file end which would fail.

---
 qemu-nbd.c | 7 +++++++
 1 file changed, 7 insertions(+)
qemu-nbd.c +0 -0
index 99297a5..629bce1 100644
@@ -901,6 +901,13 @@ int main(int argc, char **argv)
         exit(EXIT_FAILURE);
     }
 

qemu-nbd.c int main(int argc, char **argv) · line 913

Quoted 10 lines
qemu-nbd.c +7 -0
+    if (dev_offset >= fd_size) {
+        error_report("Offset (%lu) has to be smaller than the image size (%lu)",
+                     dev_offset, fd_size);
+        exit(EXIT_FAILURE);
+    }
+    fd_size -= dev_offset;
+
     if (partition != -1) {
         ret = find_partition(blk, partition, &dev_offset, &fd_size);
         if (ret < 0) {
#2 lore raw
In reply to Tomáš Golembiovský

On 09/20/2016 04:37 AM, Tomáš Golembiovský wrote:

[meta-comment]: Your series came through without any threading (you sent three threads, instead of patch 1 and 2 being marked In-Reply-To the 0/2 cover letter).

Quoted 14 lines

When --offset is set the apparent device size has to be adjusted accordingly. Otherwise client may request read/write beyond the file end which would fail.

---
 qemu-nbd.c | 7 +++++++
 1 file changed, 7 insertions(+)
qemu-nbd.c +0 -0
index 99297a5..629bce1 100644

qemu-nbd.c int main(int argc, char **argv) · line 902

Quoted 2 lines
qemu-nbd.c +0 -0
@@ -901,6 +901,13 @@ int main(int argc, char **argv)
         exit(EXIT_FAILURE);
     }

Additional context:

    off_t dev_offset = 0;

    off_t fd_size;

qemu-nbd.c int main(int argc, char **argv) · line 906

Quoted 4 lines
qemu-nbd.c +3 -0
+    if (dev_offset >= fd_size) {
+        error_report("Offset (%lu) has to be smaller than the image size (%lu)",
+                     dev_offset, fd_size);

Whoops, this fails to compile on 32-bit platforms. %lu is not necessarily synonymous with off_t values.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#3 lore raw

Whops, somehow I completely forgot about this.

Quoted 5 lines · On Tue, 20 Sep 2016 09:09:59 -0500 Eric Blake <[email protected]> wrote:

On 09/20/2016 04:37 AM, Tomáš Golembiovský wrote:

[meta-comment]: Your series came through without any threading (you sent three threads, instead of patch 1 and 2 being marked In-Reply-To the 0/2 cover letter).

Thanks for the comment. Unfortunately it was my email client interfering. It should be better next time.

Quoted 24 lines
Quoted 14 lines

When --offset is set the apparent device size has to be adjusted accordingly. Otherwise client may request read/write beyond the file end which would fail.

---
 qemu-nbd.c | 7 +++++++
 1 file changed, 7 insertions(+)
qemu-nbd.c +0 -0
index 99297a5..629bce1 100644

qemu-nbd.c int main(int argc, char **argv) · line 902

Quoted 2 lines
qemu-nbd.c +0 -0
@@ -901,6 +901,13 @@ int main(int argc, char **argv)
         exit(EXIT_FAILURE);
     }  

Additional context:

    off_t dev_offset = 0;

    off_t fd_size;
Quoted 1 line
Quoted 6 lines

qemu-nbd.c int main(int argc, char **argv) · line 906

Quoted 3 lines
qemu-nbd.c +3 -0
+    if (dev_offset >= fd_size) {
+        error_report("Offset (%lu) has to be smaller than the image size (%lu)",
+                     dev_offset, fd_size);  

Whoops, this fails to compile on 32-bit platforms. %lu is not necessarily synonymous with off_t values.

After some digging I figured off_t is in fact signed type. That makes the formatting wrong everywhere. Unfortunately I didn't find any good definition of the type. Any suggestion what format flag should I use? Or should I just use a temporary variable of known size for that?

Thanks,

Tomas

Signature · 1 line
Tomáš Golembiovský <[email protected]>
#4 lore raw
Quoted 11 lines · Tomáš Golembiovský
Quoted 11 lines

Additional context:

    off_t dev_offset = 0;

    off_t fd_size;
Quoted 4 lines
qemu-nbd.c +3 -0
+    if (dev_offset >= fd_size) {
+        error_report("Offset (%lu) has to be smaller than the image size (%lu)",
+                     dev_offset, fd_size);  
Quoted 7 lines
Quoted 2 lines

Whoops, this fails to compile on 32-bit platforms. %lu is not necessarily synonymous with off_t values.

After some digging I figured off_t is in fact signed type. That makes the formatting wrong everywhere. Unfortunately I didn't find any good definition of the type. Any suggestion what format flag should I use? Or should I just use a temporary variable of known size for that?

Easiest is probably casting to a type with an easier format flag, as in either of:

error_report("offset %lld ...", (long long) dev_offset) error_report("offset %jd ...", (intmax_t) dev_offset)

off_t is particularly problematic because there is no magic % sequence reserved for it, nothing in <inttypes.h> for it, and there are 32-bit compilation environments where it is still 32 bits (although qemu prefers to explicitly request large-file compilation so that off_t is always 64 bits)

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org