Allow the compression algorithm used by zram01.sh to be specified at
runtime rather than being hardcoded to 'lzo'.

- zram.sh: add -a <algorithm> option (default: lzo) and forward the
  chosen algorithm to zram01.sh
- zram01.sh: accept the algorithm as a positional argument, falling
  back to lzo when not provided
- zram_lib.sh: add zram_check_alg_support() helper that inspects
  /sys/block/zramN/comp_algorithm and exits with ksft_skip when the
  requested algorithm is not supported by the running kernel

This makes it straightforward to test non-default compressors (e.g.
zstd, 842) without modifying the test scripts.

Signed-off-by: Sachin Sant <[email protected]>
---
 tools/testing/selftests/zram/README      | 18 +++++++++++++++---
 tools/testing/selftests/zram/zram.sh     | 21 ++++++++++++++++++++-
 tools/testing/selftests/zram/zram01.sh   |  5 ++++-
 tools/testing/selftests/zram/zram_lib.sh | 20 ++++++++++++++++++++
 4 files changed, 59 insertions(+), 5 deletions(-)

diff --git a/tools/testing/selftests/zram/README 
b/tools/testing/selftests/zram/README
index 82921c75681c..e3a230afb8c7 100644
--- a/tools/testing/selftests/zram/README
+++ b/tools/testing/selftests/zram/README
@@ -18,11 +18,23 @@ CONFIG_ZSMALLOC=y
 
 ZRAM Testcases
 --------------
-zram_lib.sh: create library with initialization/cleanup functions
-zram.sh: For sanity check of CONFIG_ZRAM and to run zram01 and zram02
+zram_lib.sh: library with initialization/cleanup functions and helpers;
+             includes zram_check_alg_support() which verifies a requested
+             compression algorithm is available and exits with ksft_skip
+             when it is not.
+zram.sh: sanity check of CONFIG_ZRAM; runs zram01 and zram02.
+         Accepts an optional -a <algorithm> flag (default: lzo) to select
+         the compression algorithm forwarded to zram01.sh.
+         Usage: zram.sh [-a <algorithm>]
+           -a <algorithm>  compression algorithm to test (default: lzo)
+                           must be listed in /sys/block/zramN/comp_algorithm
+           -h              show help message
 
 Two functional tests: zram01 and zram02:
-zram01.sh: creates general purpose ram disks with ext4 filesystems
+zram01.sh: creates general purpose ram disks with ext4 filesystems.
+           Accepts the compression algorithm as an optional first positional
+           argument (default: lzo).  Verifies kernel support for the
+           requested algorithm before running.
 zram02.sh: creates block device for swap
 
 Commands required for testing:
diff --git a/tools/testing/selftests/zram/zram.sh 
b/tools/testing/selftests/zram/zram.sh
index b0b91d9b0dc2..a8a32a184adb 100755
--- a/tools/testing/selftests/zram/zram.sh
+++ b/tools/testing/selftests/zram/zram.sh
@@ -4,11 +4,30 @@ TCID="zram.sh"
 
 . ./zram_lib.sh
 
+usage()
+{
+       echo "Usage: $0 [-a <algorithm>]"
+       echo "  -a <algorithm>  compression algorithm to test (default: lzo)"
+       echo "                  must be listed in 
/sys/block/zramN/comp_algorithm"
+       echo "  -h              show this help message"
+}
+
+ZRAM_ALG="lzo"
+
+while getopts "a:h" opt; do
+       case "$opt" in
+       a) ZRAM_ALG="$OPTARG" ;;
+       h) usage; exit 0 ;;
+       *) echo "Unknown option: $opt" >&2; usage; exit 1 ;;
+       esac
+done
+shift $((OPTIND - 1))
+
 run_zram () {
 echo "--------------------"
 echo "running zram tests"
 echo "--------------------"
-./zram01.sh
+./zram01.sh "$ZRAM_ALG"
 echo ""
 ./zram02.sh
 }
diff --git a/tools/testing/selftests/zram/zram01.sh 
b/tools/testing/selftests/zram/zram01.sh
index 8f4affe34f3e..070cc4162384 100755
--- a/tools/testing/selftests/zram/zram01.sh
+++ b/tools/testing/selftests/zram/zram01.sh
@@ -29,7 +29,9 @@ zram_max_streams="2"
 zram_sizes="2097152" # 2MB
 zram_mem_limits="2M"
 zram_filesystems="ext4"
-zram_algs="lzo"
+# Accept the compression algorithm as the first positional argument;
+# fall back to lzo when not provided.
+zram_algs="${1:-lzo}"
 
 zram_fill_fs()
 {
@@ -58,6 +60,7 @@ zram_fill_fs()
 
 check_prereqs
 zram_load
+zram_check_alg_support "$zram_algs" "$dev_start"
 zram_max_streams
 zram_compress_alg
 zram_set_disksizes
diff --git a/tools/testing/selftests/zram/zram_lib.sh 
b/tools/testing/selftests/zram/zram_lib.sh
index 21ec1966de76..d3762b497f91 100755
--- a/tools/testing/selftests/zram/zram_lib.sh
+++ b/tools/testing/selftests/zram/zram_lib.sh
@@ -30,6 +30,26 @@ check_prereqs()
        fi
 }
 
+# zram_check_alg_support <alg> <dev_idx>
+# Verify <alg> appears in comp_algorithm of zram device <dev_idx>.
+# Must be called after zram_load so the device already exists.
+# Exits with ksft_skip if the algorithm is not supported by this kernel build.
+zram_check_alg_support()
+{
+       local alg=$1
+       local idx=$2
+
+       [ -z "$alg" ] && { echo "skip: no compression algorithm specified" >&2; 
\
+               zram_cleanup; exit $ksft_skip; }
+
+       if ! tr -d '[]' < /sys/block/zram${idx}/comp_algorithm 2>/dev/null \
+                       | grep -qw "${alg}"; then
+               echo "skip: compression algorithm '$alg' is not available" >&2
+               zram_cleanup
+               exit $ksft_skip
+       fi
+}
+
 kernel_gte()
 {
        major=${1%.*}
-- 
2.39.1


Reply via email to