common/rc: Enable _format_swapfile to return the swap size
authorCarlos Maiolino <cmaiolino@redhat.com>
Wed, 10 Nov 2021 12:41:18 +0000 (13:41 +0100)
committerEryu Guan <guaneryu@gmail.com>
Sun, 14 Nov 2021 12:18:59 +0000 (20:18 +0800)
Once the kernel is free to not map the full swap file during a swapon call,
it can be useful to know the exact size of the swap area created during
_format_swapfile().

To achieve this, it is needed to change other _format_swapfile() callers to drop
the return value if not required, otherwise, it will be printed to stdout making
such tests to fail.

Signed-off-by: Carlos Maiolino <cmaiolino@redhat.com>
Reviewed-by: Eryu Guan <guaneryu@gmail.com>
Signed-off-by: Eryu Guan <guaneryu@gmail.com>
12 files changed:
common/rc
tests/btrfs/173
tests/btrfs/174
tests/btrfs/175
tests/btrfs/176
tests/btrfs/177
tests/generic/356
tests/generic/357
tests/generic/493
tests/generic/494
tests/generic/554
tests/generic/569

index 89e90d9b933048f01d2c3f086d08be9bbfc433ee..de5be4aa4bd278fe0b7d5d27b8b5b4a2d7896f36 100644 (file)
--- a/common/rc
+++ b/common/rc
@@ -2593,9 +2593,11 @@ _require_odirect()
        rm -f $testfile 2>&1 > /dev/null
 }
 
+# Format a swapfile and return its size in bytes
 _format_swapfile() {
        local fname="$1"
        local sz="$2"
+       local swap_log=""
 
        rm -f "$fname"
        touch "$fname"
@@ -2604,8 +2606,10 @@ _format_swapfile() {
        $CHATTR_PROG +C "$fname" > /dev/null 2>&1
        _pwrite_byte 0x61 0 "$sz" "$fname" >> $seqres.full
        # Ignore permission complaints on filesystems that don't support perms
-       $MKSWAP_PROG "$fname" 2>&1 >> $seqres.full | \
-               grep -v "insecure permission"
+       swap_log=$($MKSWAP_PROG "$fname" 2>&1 | grep -v "insecure permission")
+       echo $swap_log >> $seqres.full
+
+       echo $swap_log | grep -oP '\w+(?= bytes)'
 }
 
 _swapon_file() {
@@ -2637,7 +2641,7 @@ _require_scratch_swapfile()
        _scratch_mount
 
        # Minimum size for mkswap is 10 pages
-       _format_swapfile "$SCRATCH_MNT/swap" $(($(get_page_size) * 10))
+       _format_swapfile "$SCRATCH_MNT/swap" $(($(get_page_size) * 10)) > /dev/null
 
        # ext* has supported all variants of swap files since their
        # introduction, so swapon should not fail.
index 1252587cf926942c679ea9cc42490388ac467cfb..9f53143eccc4dfae43b23c411fede59809ca7125 100755 (executable)
@@ -31,7 +31,7 @@ swapoff "$SCRATCH_MNT/swap" >/dev/null 2>&1
 
 echo "Compressed file"
 rm -f "$SCRATCH_MNT/swap"
-_format_swapfile "$SCRATCH_MNT/swap" $(($(get_page_size) * 10))
+_format_swapfile "$SCRATCH_MNT/swap" $(($(get_page_size) * 10)) > /dev/null
 $CHATTR_PROG +c "$SCRATCH_MNT/swap" 2>&1 | grep -o "Invalid argument while setting flags"
 
 status=0
index 8bf856ae9e8d28a0eb00668537dbb93e5e1b1501..3bb5e7f91809e73fe09f3577cf37c2adce6550c6 100755 (executable)
@@ -20,7 +20,7 @@ _scratch_mount
 
 $BTRFS_UTIL_PROG subvolume create "$SCRATCH_MNT/swapvol" >> $seqres.full
 swapfile="$SCRATCH_MNT/swapvol/swap"
-_format_swapfile "$swapfile" $(($(get_page_size) * 10))
+_format_swapfile "$swapfile" $(($(get_page_size) * 10)) > /dev/null
 swapon "$swapfile"
 
 # Turning off nocow doesn't do anything because the file is not empty, not
index bf0ede257f87e386675ba2490560f5b8b2f746ae..6f7832a5602f40827951b56dfcc5aba53a2ad7ce 100755 (executable)
@@ -18,7 +18,7 @@ _check_minimal_fs_size $((1024 * 1024 * 1024))
 
 cycle_swapfile() {
        local sz=${1:-$(($(get_page_size) * 10))}
-       _format_swapfile "$SCRATCH_MNT/swap" "$sz"
+       _format_swapfile "$SCRATCH_MNT/swap" "$sz" > /dev/null
        swapon "$SCRATCH_MNT/swap" 2>&1 | _filter_scratch
        swapoff "$SCRATCH_MNT/swap" > /dev/null 2>&1
 }
@@ -47,7 +47,7 @@ _scratch_mkfs >> $seqres.full 2>&1
 _scratch_mount
 # Create the swap file, then add the device. That way we know it's all on one
 # device.
-_format_swapfile "$SCRATCH_MNT/swap" $(($(get_page_size) * 10))
+_format_swapfile "$SCRATCH_MNT/swap" $(($(get_page_size) * 10)) > /dev/null
 scratch_dev2="$(echo "${SCRATCH_DEV_POOL}" | awk '{ print $2 }')"
 $BTRFS_UTIL_PROG device add -f "$scratch_dev2" "$SCRATCH_MNT"
 swapon "$SCRATCH_MNT/swap" 2>&1 | _filter_scratch
index 41b02eb6054055422fbb858dd93339352209ac65..8d624d5a8e1414376f09cc92f0446c1f749609b4 100755 (executable)
@@ -29,7 +29,7 @@ scratch_dev3="$(echo "${SCRATCH_DEV_POOL}" | awk '{ print $3 }')"
 echo "Remove device"
 _scratch_mkfs >> $seqres.full 2>&1
 _scratch_mount
-_format_swapfile "$SCRATCH_MNT/swap" $(($(get_page_size) * 10))
+_format_swapfile "$SCRATCH_MNT/swap" $(($(get_page_size) * 10)) > /dev/null
 $BTRFS_UTIL_PROG device add -f "$scratch_dev2" "$SCRATCH_MNT"
 swapon "$SCRATCH_MNT/swap" 2>&1 | _filter_scratch
 # We know the swap file is on device 1 because we added device 2 after it was
index b20502368bd269ea322b09720584acde16c7a98b..2fd11e89ca350364287d7ce6a7d7352e5e410d10 100755 (executable)
@@ -63,7 +63,7 @@ dd if=/dev/zero of="$SCRATCH_MNT/refill" bs=4096 >> $seqres.full 2>&1
 # of the filesystem was used, so the swap file must be in the new part of the
 # filesystem.
 $BTRFS_UTIL_PROG filesystem resize $((3 * fssize)) "$SCRATCH_MNT" | convert_resize_output
-_format_swapfile "$swapfile" $((32 * 1024 * 1024))
+_format_swapfile "$swapfile" $((32 * 1024 * 1024)) > /dev/null
 swapon "$swapfile"
 
 # Free up the first 1GB of the filesystem.
index fa6c058580a00ff2a8e7d5957ca228c76c3db8b5..ffc7bed5334cfc82ba021321198c5867d90313e1 100755 (executable)
@@ -39,7 +39,7 @@ blocks=160
 blksz=65536
 
 echo "Initialize file"
-_format_swapfile "$testdir/file1" $((blocks * blksz))
+_format_swapfile "$testdir/file1" $((blocks * blksz)) > /dev/null
 swapon $testdir/file1
 
 touch "$testdir/file2"
index dc9a91a4388879939f14603591d1ac5c0ff4d752..ce748f854327e534751dacd34a7d83f193a43056 100755 (executable)
@@ -39,7 +39,7 @@ blocks=160
 blksz=65536
 
 echo "Initialize file"
-_format_swapfile "$testdir/file1" $((blocks * blksz))
+_format_swapfile "$testdir/file1" $((blocks * blksz)) > /dev/null
 touch "$testdir/file2"
 $CHATTR_PROG +C "$testdir/file2" >/dev/null 2>&1
 _cp_reflink $testdir/file1 $testdir/file2 2>&1 | _filter_scratch
index a26a908d5faf7238c1869a5aa1202ad101331f5e..c2fd72f64af77818221ff528885aff3706df7703 100755 (executable)
@@ -27,7 +27,7 @@ blocks=160
 blksz=65536
 
 echo "Initialize file"
-_format_swapfile "$testdir/file1" $((blocks * blksz))
+_format_swapfile "$testdir/file1" $((blocks * blksz)) > /dev/null
 swapon "$testdir/file1"
 
 touch "$testdir/file2"
index 2e14fab8c19acf1d49624a53bd45970fbf027ba1..b41c938d14f47b0781b7134aabfef8811ebc78c3 100755 (executable)
@@ -26,7 +26,7 @@ blocks=160
 blksz=65536
 
 echo "Initialize file"
-_format_swapfile "$testdir/file1" $((blocks * blksz))
+_format_swapfile "$testdir/file1" $((blocks * blksz)) > /dev/null
 swapon "$testdir/file1"
 
 echo "Try to truncate"
index c19842859b9e0dfbe51c074384ad6eae0a2c039d..b9efee0d2c2fe3895d09956d31669857bfac3da9 100755 (executable)
@@ -30,7 +30,7 @@ _scratch_mount
 $XFS_IO_PROG -f -c "pwrite -S 0x61 0 128k" $SCRATCH_MNT/file >> $seqres.full 2>&1
 
 echo swap files return ETXTBUSY
-_format_swapfile $SCRATCH_MNT/swapfile 16m
+_format_swapfile $SCRATCH_MNT/swapfile 16m > /dev/null
 _swapon_file $SCRATCH_MNT/swapfile
 
 $XFS_IO_PROG -f -c "copy_range -l 32k $SCRATCH_MNT/file" $SCRATCH_MNT/swapfile
index 1dc54da8d32e04d6052467fdf136a39226915d83..c5dcfefe2fda593d262bc501683518983a1e4883 100755 (executable)
@@ -31,7 +31,7 @@ _scratch_mount >> $seqres.full 2>&1
 
 testfile=$SCRATCH_MNT/$seq.swap
 
-_format_swapfile $testfile 20m
+_format_swapfile $testfile 20m > /dev/null
 
 # Can you modify the swapfile via previously open file descriptors?
 for verb in 1 2 3 4; do