Skip to content

Commit

Permalink
qemu-img: Saner printing of large file sizes
Browse files Browse the repository at this point in the history
Disk sizes close to INT64_MAX cause overflow, for some pretty
ridiculous output:

  $ ./nbdkit -U - memory size=$((2**63 - 512)) --run 'qemu-img info $nbd'
  image: nbd+unix://?socket=/tmp/nbdkitHSAzNz/socket
  file format: raw
  virtual size: -8388607T (9223372036854775296 bytes)
  disk size: unavailable

But there's no reason to have two separate implementations of integer
to human-readable abbreviation, where one has overflow and stops at
'T', while the other avoids overflow and goes all the way to 'E'. With
this patch, the output now claims 8EiB instead of -8388607T, which
really is the correct rounding of largest file size supported by qemu
(we could go 511 bytes larger if we used byte-accurate sizing instead
of rounding up to the next sector boundary, but that wouldn't change
the human-readable result).

Quite a few iotests need updates to expected output to match.

Reported-by: Richard W.M. Jones <rjones@redhat.com>
Signed-off-by: Eric Blake <eblake@redhat.com>
Tested-by: Richard W.M. Jones <rjones@redhat.com>
Reviewed-by: Alberto Garcia <berto@igalia.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Tested-by: Max Reitz <mreitz@redhat.com>
  • Loading branch information
ebblake authored and kevmw committed Apr 30, 2019
1 parent 754da86 commit de38b50
Show file tree
Hide file tree
Showing 29 changed files with 123 additions and 150 deletions.
49 changes: 11 additions & 38 deletions block/qapi.c
Expand Up @@ -631,42 +631,13 @@ BlockStatsList *qmp_query_blockstats(bool has_query_nodes,
return head;
}

#define NB_SUFFIXES 4

static char *get_human_readable_size(char *buf, int buf_size, int64_t size)
{
static const char suffixes[NB_SUFFIXES] = {'K', 'M', 'G', 'T'};
int64_t base;
int i;

if (size <= 999) {
snprintf(buf, buf_size, "%" PRId64, size);
} else {
base = 1024;
for (i = 0; i < NB_SUFFIXES; i++) {
if (size < (10 * base)) {
snprintf(buf, buf_size, "%0.1f%c",
(double)size / base,
suffixes[i]);
break;
} else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
snprintf(buf, buf_size, "%" PRId64 "%c",
((size + (base >> 1)) / base),
suffixes[i]);
break;
}
base = base * 1024;
}
}
return buf;
}

void bdrv_snapshot_dump(QEMUSnapshotInfo *sn)
{
char buf1[128], date_buf[128], clock_buf[128];
char date_buf[128], clock_buf[128];
struct tm tm;
time_t ti;
int64_t secs;
char *sizing = NULL;

if (!sn) {
qemu_printf("%-10s%-20s%7s%20s%15s",
Expand All @@ -683,13 +654,14 @@ void bdrv_snapshot_dump(QEMUSnapshotInfo *sn)
(int)((secs / 60) % 60),
(int)(secs % 60),
(int)((sn->vm_clock_nsec / 1000000) % 1000));
sizing = size_to_str(sn->vm_state_size);
qemu_printf("%-10s%-20s%7s%20s%15s",
sn->id_str, sn->name,
get_human_readable_size(buf1, sizeof(buf1),
sn->vm_state_size),
sizing,
date_buf,
clock_buf);
}
g_free(sizing);
}

static void dump_qdict(int indentation, QDict *dict);
Expand Down Expand Up @@ -787,21 +759,22 @@ void bdrv_image_info_specific_dump(ImageInfoSpecific *info_spec)

void bdrv_image_info_dump(ImageInfo *info)
{
char size_buf[128], dsize_buf[128];
char *size_buf, *dsize_buf;
if (!info->has_actual_size) {
snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
dsize_buf = g_strdup("unavailable");
} else {
get_human_readable_size(dsize_buf, sizeof(dsize_buf),
info->actual_size);
dsize_buf = size_to_str(info->actual_size);
}
get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size);
size_buf = size_to_str(info->virtual_size);
qemu_printf("image: %s\n"
"file format: %s\n"
"virtual size: %s (%" PRId64 " bytes)\n"
"disk size: %s\n",
info->filename, info->format, size_buf,
info->virtual_size,
dsize_buf);
g_free(size_buf);
g_free(dsize_buf);

if (info->has_encrypted && info->encrypted) {
qemu_printf("encrypted: yes\n");
Expand Down
6 changes: 3 additions & 3 deletions tests/qemu-iotests/043.out
Expand Up @@ -22,19 +22,19 @@ Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=134217728 backing_file=TEST_DIR/
== finite chain of length 3 (human) ==
image: TEST_DIR/t.IMGFMT
file format: IMGFMT
virtual size: 128M (134217728 bytes)
virtual size: 128 MiB (134217728 bytes)
cluster_size: 65536
backing file: TEST_DIR/t.IMGFMT.2.base

image: TEST_DIR/t.IMGFMT.2.base
file format: IMGFMT
virtual size: 128M (134217728 bytes)
virtual size: 128 MiB (134217728 bytes)
cluster_size: 65536
backing file: TEST_DIR/t.IMGFMT.1.base

image: TEST_DIR/t.IMGFMT.1.base
file format: IMGFMT
virtual size: 128M (134217728 bytes)
virtual size: 128 MiB (134217728 bytes)
cluster_size: 65536

== finite chain of length 3 (json) ==
Expand Down
2 changes: 1 addition & 1 deletion tests/qemu-iotests/053.out
Expand Up @@ -9,7 +9,7 @@ wrote 512/512 bytes at offset 0
No errors were found on the image.

== Checking compressed image virtual disk size ==
virtual size: 512 (512 bytes)
virtual size: 512 B (512 bytes)

== Verifying the compressed image ==
read 512/512 bytes at offset 0
Expand Down
10 changes: 5 additions & 5 deletions tests/qemu-iotests/059.out
Expand Up @@ -16,7 +16,7 @@ can't open device TEST_DIR/t.vmdk: L1 size too big
Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=2147483648 subformat=monolithicFlat
image: TEST_DIR/t.IMGFMT
file format: IMGFMT
virtual size: 2.0G (2147483648 bytes)
virtual size: 2 GiB (2147483648 bytes)

=== Testing monolithicFlat with zeroed_grain ===
qemu-img: TEST_DIR/t.IMGFMT: Flat image can't enable zeroed grain
Expand All @@ -26,8 +26,8 @@ Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=2147483648 subformat=monolithicF
Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1073741824000 subformat=twoGbMaxExtentFlat
image: TEST_DIR/t.vmdk
file format: vmdk
virtual size: 1.0T (1073741824000 bytes)
disk size: 16K
virtual size: 0.977 TiB (1073741824000 bytes)
disk size: 16 KiB
Format specific information:
cid: XXXXXXXX
parent cid: XXXXXXXX
Expand Down Expand Up @@ -2055,7 +2055,7 @@ can't open: Cannot use relative extent paths with VMDK descriptor file 'json:{"i
=== Testing version 3 ===
image: TEST_DIR/iotest-version3.IMGFMT
file format: IMGFMT
virtual size: 16G (17179869184 bytes)
virtual size: 16 GiB (17179869184 bytes)
cluster_size: 65536
read 512/512 bytes at offset 0
512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
Expand Down Expand Up @@ -2262,7 +2262,7 @@ read 512/512 bytes at offset 64931328
Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=4398046511104 subformat=monolithicFlat
image: TEST_DIR/t.IMGFMT
file format: IMGFMT
virtual size: 4.0T (4398046511104 bytes)
virtual size: 4 TiB (4398046511104 bytes)
wrote 512/512 bytes at offset 966367641600
512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
e100000000: 0a 0a 0a 0a 0a 0a 0a 0a 0a 0a 0a 0a 0a 0a 0a 0a ................
Expand Down
10 changes: 5 additions & 5 deletions tests/qemu-iotests/060.out
Expand Up @@ -13,7 +13,7 @@ write failed: Input/output error
incompatible_features 0x2
image: TEST_DIR/t.IMGFMT
file format: IMGFMT
virtual size: 64M (67108864 bytes)
virtual size: 64 MiB (67108864 bytes)
cluster_size: 65536
Format specific information:
compat: 1.1
Expand Down Expand Up @@ -364,21 +364,21 @@ Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864
qcow2: Marking image as corrupt: Refblock at 0xffffff00000000 is not covered by the refcount structures; further corruption events will be suppressed
qemu-img: Failed to discard unused refblocks: Input/output error
--- Checking and retrying ---
virtual size: 64M (67108864 bytes)
virtual size: 64 MiB (67108864 bytes)
No errors were found on the image.
Image resized.
virtual size: 32M (33554432 bytes)
virtual size: 32 MiB (33554432 bytes)

=== Discarding a non-covered in-bounds refblock ===

Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864
qcow2: Marking image as corrupt: Refblock at 0x1000000000 is not covered by the refcount structures; further corruption events will be suppressed
qemu-img: Failed to discard unused refblocks: Input/output error
--- Checking and retrying ---
virtual size: 64M (67108864 bytes)
virtual size: 64 MiB (67108864 bytes)
No errors were found on the image.
Image resized.
virtual size: 32M (33554432 bytes)
virtual size: 32 MiB (33554432 bytes)

=== Discarding a refblock covered by an unaligned refblock ===

Expand Down
12 changes: 6 additions & 6 deletions tests/qemu-iotests/061.out
Expand Up @@ -495,7 +495,7 @@ Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864 data_file=TEST_DIR/t.IM
qemu-img: Cannot downgrade an image with a data file
image: TEST_DIR/t.IMGFMT
file format: IMGFMT
virtual size: 64M (67108864 bytes)
virtual size: 64 MiB (67108864 bytes)
cluster_size: 65536
Format specific information:
compat: 1.1
Expand All @@ -515,7 +515,7 @@ Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864 data_file=TEST_DIR/t.IM
qemu-img: Could not open 'TEST_DIR/t.IMGFMT': Could not open 'foo': No such file or directory
image: TEST_DIR/t.IMGFMT
file format: IMGFMT
virtual size: 64M (67108864 bytes)
virtual size: 64 MiB (67108864 bytes)
cluster_size: 65536
Format specific information:
compat: 1.1
Expand All @@ -528,7 +528,7 @@ Format specific information:
qemu-img: Could not open 'TEST_DIR/t.IMGFMT': 'data-file' is required for this image
image: TEST_DIR/t.IMGFMT
file format: IMGFMT
virtual size: 64M (67108864 bytes)
virtual size: 64 MiB (67108864 bytes)
cluster_size: 65536
Format specific information:
compat: 1.1
Expand All @@ -542,7 +542,7 @@ Format specific information:
Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864 data_file=TEST_DIR/t.IMGFMT.data data_file_raw=on
image: TEST_DIR/t.IMGFMT
file format: IMGFMT
virtual size: 64M (67108864 bytes)
virtual size: 64 MiB (67108864 bytes)
cluster_size: 65536
Format specific information:
compat: 1.1
Expand All @@ -554,7 +554,7 @@ Format specific information:
No errors were found on the image.
image: TEST_DIR/t.IMGFMT
file format: IMGFMT
virtual size: 64M (67108864 bytes)
virtual size: 64 MiB (67108864 bytes)
cluster_size: 65536
Format specific information:
compat: 1.1
Expand All @@ -567,7 +567,7 @@ No errors were found on the image.
qemu-img: data-file-raw cannot be set on existing images
image: TEST_DIR/t.IMGFMT
file format: IMGFMT
virtual size: 64M (67108864 bytes)
virtual size: 64 MiB (67108864 bytes)
cluster_size: 65536
Format specific information:
compat: 1.1
Expand Down
2 changes: 1 addition & 1 deletion tests/qemu-iotests/070.out
Expand Up @@ -22,6 +22,6 @@ read 18874368/18874368 bytes at offset 0
=== Verify image created by Disk2VHD can be opened ===
image: TEST_DIR/test-disk2vhd.IMGFMT
file format: IMGFMT
virtual size: 256M (268435456 bytes)
virtual size: 256 MiB (268435456 bytes)
cluster_size: 2097152
*** done
26 changes: 13 additions & 13 deletions tests/qemu-iotests/082.out
Expand Up @@ -6,14 +6,14 @@ Testing: create -f foo -f qcow2 TEST_DIR/t.qcow2 128M
Formatting 'TEST_DIR/t.qcow2', fmt=qcow2 size=134217728 cluster_size=65536 lazy_refcounts=off refcount_bits=16
image: TEST_DIR/t.IMGFMT
file format: IMGFMT
virtual size: 128M (134217728 bytes)
virtual size: 128 MiB (134217728 bytes)
cluster_size: 65536

Testing: create -f qcow2 -o cluster_size=4k -o lazy_refcounts=on TEST_DIR/t.qcow2 128M
Formatting 'TEST_DIR/t.qcow2', fmt=qcow2 size=134217728 cluster_size=4096 lazy_refcounts=on refcount_bits=16
image: TEST_DIR/t.IMGFMT
file format: IMGFMT
virtual size: 128M (134217728 bytes)
virtual size: 128 MiB (134217728 bytes)
cluster_size: 4096
Format specific information:
compat: 1.1
Expand All @@ -25,7 +25,7 @@ Testing: create -f qcow2 -o cluster_size=4k -o lazy_refcounts=on -o cluster_size
Formatting 'TEST_DIR/t.qcow2', fmt=qcow2 size=134217728 cluster_size=8192 lazy_refcounts=on refcount_bits=16
image: TEST_DIR/t.IMGFMT
file format: IMGFMT
virtual size: 128M (134217728 bytes)
virtual size: 128 MiB (134217728 bytes)
cluster_size: 8192
Format specific information:
compat: 1.1
Expand All @@ -37,7 +37,7 @@ Testing: create -f qcow2 -o cluster_size=4k,cluster_size=8k TEST_DIR/t.qcow2 128
Formatting 'TEST_DIR/t.qcow2', fmt=qcow2 size=134217728 cluster_size=8192 lazy_refcounts=off refcount_bits=16
image: TEST_DIR/t.IMGFMT
file format: IMGFMT
virtual size: 128M (134217728 bytes)
virtual size: 128 MiB (134217728 bytes)
cluster_size: 8192

=== create: help for -o ===
Expand Down Expand Up @@ -278,18 +278,18 @@ Formatting 'TEST_DIR/t.qcow2', fmt=qcow2 size=134217728 cluster_size=65536 lazy_
Testing: convert -f foo -f qcow2 TEST_DIR/t.qcow2 TEST_DIR/t.qcow2.base
image: TEST_DIR/t.IMGFMT.base
file format: raw
virtual size: 128M (134217728 bytes)
virtual size: 128 MiB (134217728 bytes)

Testing: convert -O foo -O qcow2 TEST_DIR/t.qcow2 TEST_DIR/t.qcow2.base
image: TEST_DIR/t.IMGFMT.base
file format: IMGFMT
virtual size: 128M (134217728 bytes)
virtual size: 128 MiB (134217728 bytes)
cluster_size: 65536

Testing: convert -O qcow2 -o cluster_size=4k -o lazy_refcounts=on TEST_DIR/t.qcow2 TEST_DIR/t.qcow2.base
image: TEST_DIR/t.IMGFMT.base
file format: IMGFMT
virtual size: 128M (134217728 bytes)
virtual size: 128 MiB (134217728 bytes)
cluster_size: 4096
Format specific information:
compat: 1.1
Expand All @@ -300,7 +300,7 @@ Format specific information:
Testing: convert -O qcow2 -o cluster_size=4k -o lazy_refcounts=on -o cluster_size=8k TEST_DIR/t.qcow2 TEST_DIR/t.qcow2.base
image: TEST_DIR/t.IMGFMT.base
file format: IMGFMT
virtual size: 128M (134217728 bytes)
virtual size: 128 MiB (134217728 bytes)
cluster_size: 8192
Format specific information:
compat: 1.1
Expand All @@ -311,7 +311,7 @@ Format specific information:
Testing: convert -O qcow2 -o cluster_size=4k,cluster_size=8k TEST_DIR/t.qcow2 TEST_DIR/t.qcow2.base
image: TEST_DIR/t.IMGFMT.base
file format: IMGFMT
virtual size: 128M (134217728 bytes)
virtual size: 128 MiB (134217728 bytes)
cluster_size: 8192

=== convert: help for -o ===
Expand Down Expand Up @@ -560,7 +560,7 @@ qemu-img: Cannot enable copy offloading when -c is used
Testing: amend -f foo -f qcow2 -o lazy_refcounts=on TEST_DIR/t.qcow2
image: TEST_DIR/t.IMGFMT
file format: IMGFMT
virtual size: 128M (134217728 bytes)
virtual size: 128 MiB (134217728 bytes)
cluster_size: 65536
Format specific information:
compat: 1.1
Expand All @@ -571,7 +571,7 @@ Format specific information:
Testing: amend -f qcow2 -o size=130M -o lazy_refcounts=off TEST_DIR/t.qcow2
image: TEST_DIR/t.IMGFMT
file format: IMGFMT
virtual size: 130M (136314880 bytes)
virtual size: 130 MiB (136314880 bytes)
cluster_size: 65536
Format specific information:
compat: 1.1
Expand All @@ -582,7 +582,7 @@ Format specific information:
Testing: amend -f qcow2 -o size=8M -o lazy_refcounts=on -o size=132M TEST_DIR/t.qcow2
image: TEST_DIR/t.IMGFMT
file format: IMGFMT
virtual size: 132M (138412032 bytes)
virtual size: 132 MiB (138412032 bytes)
cluster_size: 65536
Format specific information:
compat: 1.1
Expand All @@ -593,7 +593,7 @@ Format specific information:
Testing: amend -f qcow2 -o size=4M,size=148M TEST_DIR/t.qcow2
image: TEST_DIR/t.IMGFMT
file format: IMGFMT
virtual size: 148M (155189248 bytes)
virtual size: 148 MiB (155189248 bytes)
cluster_size: 65536

=== amend: help for -o ===
Expand Down

0 comments on commit de38b50

Please sign in to comment.