b38b0f
From b897ede11c7b47cc9db8334ca44dd960d3001309 Mon Sep 17 00:00:00 2001
b38b0f
From: Thomas Huth <thuth@redhat.com>
b38b0f
Date: Fri, 30 Aug 2019 12:56:26 +0100
b38b0f
Subject: [PATCH 08/10] iotests: Filter 175's allocation information
b38b0f
b38b0f
RH-Author: Thomas Huth <thuth@redhat.com>
b38b0f
Message-id: <20190830125628.23668-4-thuth@redhat.com>
b38b0f
Patchwork-id: 90211
b38b0f
O-Subject: [RHEL-8.1.0 qemu-kvm PATCH v2 3/5] iotests: Filter 175's allocation information
b38b0f
Bugzilla: 1738839
b38b0f
RH-Acked-by: Cornelia Huck <cohuck@redhat.com>
b38b0f
RH-Acked-by: Max Reitz <mreitz@redhat.com>
b38b0f
RH-Acked-by: David Hildenbrand <david@redhat.com>
b38b0f
b38b0f
From: Max Reitz <mreitz@redhat.com>
b38b0f
b38b0f
It is possible for an empty file to take up blocks on a filesystem, for
b38b0f
example:
b38b0f
b38b0f
$ qemu-img create -f raw test.img 1G
b38b0f
Formatting 'test.img', fmt=raw size=1073741824
b38b0f
$ mkfs.ext4 -I 128 -q test.img
b38b0f
$ mkdir test-mount
b38b0f
$ sudo mount -o loop test.img test-mount
b38b0f
$ sudo touch test-mount/test-file
b38b0f
$ stat -c 'blocks=%b' test-mount/test-file
b38b0f
blocks=8
b38b0f
b38b0f
These extra blocks (one cluster) are apparently used for metadata,
b38b0f
because they are always there, on top of blocks used for data:
b38b0f
b38b0f
$ sudo dd if=/dev/zero of=test-mount/test-file bs=1M count=1
b38b0f
1+0 records in
b38b0f
1+0 records out
b38b0f
1048576 bytes (1.0 MB, 1.0 MiB) copied, 0.00135339 s, 775 MB/s
b38b0f
$ stat -c 'blocks=%b' test-mount/test-file
b38b0f
blocks=2056
b38b0f
b38b0f
Make iotest 175 take this into account.
b38b0f
b38b0f
Reported-by: Thomas Huth <thuth@redhat.com>
b38b0f
Signed-off-by: Max Reitz <mreitz@redhat.com>
b38b0f
Reviewed-by: Eric Blake <eblake@redhat.com>
b38b0f
Reviewed-by: Nir Soffer <nsoffer@redhat.com>
b38b0f
Message-id: 20190516144319.12570-1-mreitz@redhat.com
b38b0f
Signed-off-by: Max Reitz <mreitz@redhat.com>
b38b0f
(cherry picked from commit a3bd71b5773a3664692601e6e181f108e1e4aa41)
b38b0f
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
b38b0f
---
b38b0f
 tests/qemu-iotests/175     | 26 ++++++++++++++++++++++----
b38b0f
 tests/qemu-iotests/175.out |  8 ++++----
b38b0f
 2 files changed, 26 insertions(+), 8 deletions(-)
b38b0f
b38b0f
diff --git a/tests/qemu-iotests/175 b/tests/qemu-iotests/175
b38b0f
index ca56e82..2e37c9a 100755
b38b0f
--- a/tests/qemu-iotests/175
b38b0f
+++ b/tests/qemu-iotests/175
b38b0f
@@ -29,10 +29,25 @@ status=1	# failure is the default!
b38b0f
 
b38b0f
 _cleanup()
b38b0f
 {
b38b0f
-	_cleanup_test_img
b38b0f
+    _cleanup_test_img
b38b0f
+    rm -f "$TEST_DIR/empty"
b38b0f
 }
b38b0f
 trap "_cleanup; exit \$status" 0 1 2 3 15
b38b0f
 
b38b0f
+# Some file systems sometimes allocate extra blocks independently of
b38b0f
+# the file size.  This function hides the resulting difference in the
b38b0f
+# stat -c '%b' output.
b38b0f
+# Parameter 1: Number of blocks an empty file occupies
b38b0f
+# Parameter 2: Image size in bytes
b38b0f
+_filter_blocks()
b38b0f
+{
b38b0f
+    extra_blocks=$1
b38b0f
+    img_size=$2
b38b0f
+
b38b0f
+    sed -e "s/blocks=$extra_blocks\\(\$\\|[^0-9]\\)/nothing allocated/" \
b38b0f
+        -e "s/blocks=$((extra_blocks + img_size / 512))\\(\$\\|[^0-9]\\)/everything allocated/"
b38b0f
+}
b38b0f
+
b38b0f
 # get standard environment, filters and checks
b38b0f
 . ./common.rc
b38b0f
 . ./common.filter
b38b0f
@@ -41,18 +56,21 @@ _supported_fmt raw
b38b0f
 _supported_proto file
b38b0f
 _supported_os Linux
b38b0f
 
b38b0f
-size=1m
b38b0f
+size=$((1 * 1024 * 1024))
b38b0f
+
b38b0f
+touch "$TEST_DIR/empty"
b38b0f
+extra_blocks=$(stat -c '%b' "$TEST_DIR/empty")
b38b0f
 
b38b0f
 echo
b38b0f
 echo "== creating image with default preallocation =="
b38b0f
 _make_test_img $size | _filter_imgfmt
b38b0f
-stat -c "size=%s, blocks=%b" $TEST_IMG
b38b0f
+stat -c "size=%s, blocks=%b" $TEST_IMG | _filter_blocks $extra_blocks $size
b38b0f
 
b38b0f
 for mode in off full falloc; do
b38b0f
     echo
b38b0f
     echo "== creating image with preallocation $mode =="
b38b0f
     IMGOPTS=preallocation=$mode _make_test_img $size | _filter_imgfmt
b38b0f
-    stat -c "size=%s, blocks=%b" $TEST_IMG
b38b0f
+    stat -c "size=%s, blocks=%b" $TEST_IMG | _filter_blocks $extra_blocks $size
b38b0f
 done
b38b0f
 
b38b0f
 # success, all done
b38b0f
diff --git a/tests/qemu-iotests/175.out b/tests/qemu-iotests/175.out
b38b0f
index 76c02c6..6d9a5ed 100644
b38b0f
--- a/tests/qemu-iotests/175.out
b38b0f
+++ b/tests/qemu-iotests/175.out
b38b0f
@@ -2,17 +2,17 @@ QA output created by 175
b38b0f
 
b38b0f
 == creating image with default preallocation ==
b38b0f
 Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1048576
b38b0f
-size=1048576, blocks=0
b38b0f
+size=1048576, nothing allocated
b38b0f
 
b38b0f
 == creating image with preallocation off ==
b38b0f
 Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1048576 preallocation=off
b38b0f
-size=1048576, blocks=0
b38b0f
+size=1048576, nothing allocated
b38b0f
 
b38b0f
 == creating image with preallocation full ==
b38b0f
 Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1048576 preallocation=full
b38b0f
-size=1048576, blocks=2048
b38b0f
+size=1048576, everything allocated
b38b0f
 
b38b0f
 == creating image with preallocation falloc ==
b38b0f
 Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1048576 preallocation=falloc
b38b0f
-size=1048576, blocks=2048
b38b0f
+size=1048576, everything allocated
b38b0f
  *** done
b38b0f
-- 
b38b0f
1.8.3.1
b38b0f