efivarfs is mounted read-only when the firmware does not provide a
working runtime SetVariable() service. This happens, for example, on
systems where the EFI runtime is provided by U-Boot's efi_loader (a
common case on RISC-V under QEMU booting via OpenSBI -> U-Boot -> GRUB),
or when runtime services are disabled (efi=noruntime, lockdown, etc).

In that situation every test that creates, modifies or deletes an EFI
variable is bound to fail, producing spurious test failures that do not
reflect a real kernel bug.

Detect the mount mode in check_prereqs() and store it in the global
efivarfs_mode ("ro" or "rw"). Group all tests that require a writable
efivarfs into a single "if [ "$efivarfs_mode" = "rw" ]" block, so they
are only run when efivarfs is writable; otherwise print a single message
explaining that they were skipped.

test_invalid_filenames is left to run unconditionally, since its
expectation still holds on a read-only mount.

Assisted-by: Copilot:claude-opus-4-8
Signed-off-by: Hui Wang <[email protected]>
---
In the v3: drop the change in the test_create_empty, and move this
           testcase to rw group as suggested in the v2.

 tools/testing/selftests/efivarfs/efivarfs.sh | 41 ++++++++++++++------
 1 file changed, 29 insertions(+), 12 deletions(-)

diff --git a/tools/testing/selftests/efivarfs/efivarfs.sh 
b/tools/testing/selftests/efivarfs/efivarfs.sh
index c62544b966ae..9a5ad818c63b 100755
--- a/tools/testing/selftests/efivarfs/efivarfs.sh
+++ b/tools/testing/selftests/efivarfs/efivarfs.sh
@@ -26,6 +26,14 @@ check_prereqs()
                echo $msg efivarfs is not mounted on $efivarfs_mount >&2
                exit $ksft_skip
        fi
+
+       # Determine whether efivarfs is mounted read-only or read-write
+       # and store the result ("ro" or "rw") in the global efivarfs_mode.
+       if grep -q "^\S\+ $efivarfs_mount efivarfs ro[, ]" /proc/mounts; then
+               efivarfs_mode=ro
+       else
+               efivarfs_mode=rw
+       fi
 }
 
 run_test()
@@ -361,18 +369,27 @@ check_prereqs
 
 rc=0
 
-run_test test_create
-run_test test_create_empty
-run_test test_create_read
-run_test test_delete
-run_test test_zero_size_delete
-run_test test_open_unlink
-run_test test_valid_filenames
+# Tests that are also valid on a read-only efivarfs run unconditionally.
 run_test test_invalid_filenames
-run_test test_no_set_size
-setup_test_multiple
-run_test test_multiple_zero_size
-run_test test_multiple_create
-run_test test_multiple_delete_on_write
+
+# These tests need to create, modify or delete EFI variables, so they
+# require a writable efivarfs. Skip them when it is mounted read-only.
+if [ "$efivarfs_mode" = "rw" ]; then
+       run_test test_create
+       run_test test_create_empty
+       run_test test_create_read
+       run_test test_delete
+       run_test test_zero_size_delete
+       run_test test_open_unlink
+       run_test test_valid_filenames
+       run_test test_no_set_size
+       setup_test_multiple
+       run_test test_multiple_zero_size
+       run_test test_multiple_create
+       run_test test_multiple_delete_on_write
+else
+       echo "efivarfs is mounted read-only on $efivarfs_mount;" \
+            "tests that require write access were skipped" >&2
+fi
 
 exit $rc
-- 
2.43.0


Reply via email to