git: a01ce3b87d83 - main - swapon: Improve tests for non-standard kernel page size and small media
Date: Thu, 31 Jul 2025 00:24:28 UTC
The branch main has been updated by olivier:
URL: https://cgit.FreeBSD.org/src/commit/?id=a01ce3b87d83d4a2644b0d806318d09442817d3b
commit a01ce3b87d83d4a2644b0d806318d09442817d3b
Author: Olivier Cochard <olivier@FreeBSD.org>
AuthorDate: 2025-07-31 00:10:56 +0000
Commit: Olivier Cochard <olivier@FreeBSD.org>
CommitDate: 2025-07-31 00:23:13 +0000
swapon: Improve tests for non-standard kernel page size and small media
Add a test to verify swapon's behavior when attaching to media that's too small.
This also adapts existing tests to accommodate kernel page sizes larger than
the default 4KB.
Approved by: kib, chs (previous version)
Sponsored by: Netflix
Differential Revision: https://reviews.freebsd.org/D51641
---
sbin/swapon/tests/swapon_test.sh | 60 ++++++++++++++++++++++++++++++++++------
1 file changed, 52 insertions(+), 8 deletions(-)
diff --git a/sbin/swapon/tests/swapon_test.sh b/sbin/swapon/tests/swapon_test.sh
index b6d31ecaeed0..a04bb36cc49e 100755
--- a/sbin/swapon/tests/swapon_test.sh
+++ b/sbin/swapon/tests/swapon_test.sh
@@ -31,7 +31,10 @@ attach_mdX_head()
attach_mdX_body()
{
# if the swapfile is too small (like 1k) then mdconfig hangs looking up the md
- atf_check -s exit:0 -x "truncate -s 10k swapfile"
+ # but need a swapfile bigger than one page kernel page size
+ pagesize=$(sysctl -n hw.pagesize)
+ minsize=$(( pagesize * 2 ))
+ atf_check -s exit:0 -x "truncate -s $minsize swapfile"
atf_check -s exit:0 -o save:fstab.out -x "echo 'md31 none swap sw,file=swapfile 0 0'"
atf_check -s exit:0 -o match:"swapon: adding /dev/md31 as swap device" -x "swapon -F fstab.out -a"
}
@@ -49,7 +52,10 @@ attach_dev_mdX_head()
attach_dev_mdX_body()
{
# if the swapfile is too small (like 1k) then mdconfig hangs looking up the md
- atf_check -s exit:0 -x "truncate -s 10k swapfile"
+ # but need a swapfile bigger than one page kernel page size
+ pagesize=$(sysctl -n hw.pagesize)
+ minsize=$(( pagesize * 2 ))
+ atf_check -s exit:0 -x "truncate -s $minsize swapfile"
atf_check -s exit:0 -o save:fstab.out -x "echo '/dev/md32 none swap sw,file=swapfile 0 0'"
atf_check -s exit:0 -o match:"swapon: adding /dev/md32 as swap device" -x "swapon -F fstab.out -a"
}
@@ -67,7 +73,10 @@ attach_md_head()
attach_md_body()
{
# if the swapfile is too small (like 1k) then mdconfig hangs looking up the md
- atf_check -s exit:0 -x "truncate -s 10k swapfile"
+ # but need a swapfile bigger than one page kernel page size
+ pagesize=$(sysctl -n hw.pagesize)
+ minsize=$(( pagesize * 2 ))
+ atf_check -s exit:0 -x "truncate -s $minsize swapfile"
atf_check -s exit:0 -o save:fstab.out -x "echo 'md none swap sw,file=swapfile 0 0'"
atf_check -s exit:0 -o match:"swapon: adding /dev/md[0-9][0-9]* as swap device" -x "swapon -F fstab.out -a"
}
@@ -85,7 +94,10 @@ attach_dev_md_head()
attach_dev_md_body()
{
# if the swapfile is too small (like 1k) then mdconfig hangs looking up the md
- atf_check -s exit:0 -x "truncate -s 10k swapfile"
+ # but need a swapfile bigger than one page kernel page size
+ pagesize=$(sysctl -n hw.pagesize)
+ minsize=$(( pagesize * 2 ))
+ atf_check -s exit:0 -x "truncate -s $minsize swapfile"
atf_check -s exit:0 -o save:fstab.out -x "echo '/dev/md none swap sw,file=swapfile 0 0'"
atf_check -s exit:0 -o match:"swapon: adding /dev/md[0-9][0-9]* as swap device" -x "swapon -F fstab.out -a"
}
@@ -103,7 +115,10 @@ attach_mdX_eli_head()
attach_mdX_eli_body()
{
# if the swapfile is too small (like 1k) then mdconfig hangs looking up the md
- atf_check -s exit:0 -x "truncate -s 10k swapfile"
+ # but need a swapfile bigger than one page kernel page size
+ pagesize=$(sysctl -n hw.pagesize)
+ minsize=$(( pagesize * 2 ))
+ atf_check -s exit:0 -x "truncate -s $minsize swapfile"
atf_check -s exit:0 -o save:fstab.out -x "echo 'md33.eli none swap sw,file=swapfile 0 0'"
atf_check -s exit:0 -o match:"swapon: adding /dev/md33.eli as swap device" -x "swapon -F fstab.out -a"
}
@@ -121,7 +136,10 @@ attach_dev_mdX_eli_head()
attach_dev_mdX_eli_body()
{
# if the swapfile is too small (like 1k) then mdconfig hangs looking up the md
- atf_check -s exit:0 -x "truncate -s 10k swapfile"
+ # but need a swapfile bigger than one page kernel page size
+ pagesize=$(sysctl -n hw.pagesize)
+ minsize=$(( pagesize * 2 ))
+ atf_check -s exit:0 -x "truncate -s $minsize swapfile"
atf_check -s exit:0 -o save:fstab.out -x "echo '/dev/md34.eli none swap sw,file=swapfile 0 0'"
atf_check -s exit:0 -o match:"swapon: adding /dev/md34.eli as swap device" -x "swapon -F fstab.out -a"
}
@@ -139,7 +157,10 @@ attach_md_eli_head()
attach_md_eli_body()
{
# if the swapfile is too small (like 1k) then mdconfig hangs looking up the md
- atf_check -s exit:0 -x "truncate -s 10k swapfile"
+ # but need a swapfile bigger than one page kernel page size
+ pagesize=$(sysctl -n hw.pagesize)
+ minsize=$(( pagesize * 2 ))
+ atf_check -s exit:0 -x "truncate -s $minsize swapfile"
atf_check -s exit:0 -o save:fstab.out -x "echo 'md.eli none swap sw,file=swapfile 0 0'"
atf_check -s exit:0 -o match:"swapon: adding /dev/md[0-9][0-9]*.eli as swap device" -x "swapon -F fstab.out -a"
}
@@ -157,7 +178,10 @@ attach_dev_md_eli_head()
attach_dev_md_eli_body()
{
# if the swapfile is too small (like 1k) then mdconfig hangs looking up the md
- atf_check -s exit:0 -x "truncate -s 10k swapfile"
+ # but need a swapfile bigger than one page kernel page size
+ pagesize=$(sysctl -n hw.pagesize)
+ minsize=$(( pagesize * 2 ))
+ atf_check -s exit:0 -x "truncate -s $minsize swapfile"
atf_check -s exit:0 -o save:fstab.out -x "echo '/dev/md.eli none swap sw,file=swapfile 0 0'"
atf_check -s exit:0 -o match:"swapon: adding /dev/md[0-9][0-9]*.eli as swap device" -x "swapon -F fstab.out -a"
}
@@ -166,6 +190,24 @@ attach_dev_md_eli_cleanup()
swapoff -F fstab.out -a
}
+###
+
+atf_test_case attach_too_small
+attach_too_small_head()
+{
+ atf_set "descr" "should refuse to attach if smaller than one kernel page size"
+}
+attach_too_small_body()
+{
+ # Need to use smaller than kernel page size
+ pagesize=$(sysctl -n hw.pagesize)
+ minsize=$(( pagesize / 2 ))
+ atf_check -s exit:0 -x "truncate -s $minsize swapfile"
+ atf_check -s exit:0 -o save:fstab.out -x "echo 'md35 none swap sw,file=swapfile 0 0'"
+ atf_check -s exit:1 -e match:"swapon: /dev/md35: NSWAPDEV limit reached" -x "swapon -F fstab.out -a"
+ atf_check -s exit:0 -x "mdconfig -d -u 35"
+}
+
###
atf_init_test_cases()
{
@@ -178,4 +220,6 @@ atf_init_test_cases()
atf_add_test_case attach_dev_mdX_eli
atf_add_test_case attach_md_eli
atf_add_test_case attach_dev_md_eli
+
+ atf_add_test_case attach_too_small
}