File kselftests.spec of Package kselftests
# Copyright (c) 2025 SUSE LINUX GmbH, Nuernberg, Germany.
# Avoid removal of debugging symbols from binaries, as some tests expect them to be in-place
%global __strip /usr/bin/true
%define targets bpf cgroup livepatch net
%define installdir %{_datadir}/kselftests
%define mod_dirs bpf/test_kmods livepatch/test_modules
%if !0%{?is_opensuse} && 0%{?sle_version} == 150700
%define mod_dirs bpf/bpf_testmod livepatch/test_modules
%endif
%if !0%{?is_opensuse} && 0%{?suse_version} == 1600
%define mod_dirs livepatch/test_modules bpf/bpf_test_modorder_x bpf/bpf_test_modorder_y bpf/bpf_test_no_cfi bpf/bpf_testmod
%endif
%define llvm_version 20
%if !0%{?is_opensuse}
%define llvm_version 19
%endif
%define kver %(rpm -q --qf '%%{VERSION}' kernel-source)
%define krel %(rpm -q --qf '%%{RELEASE}' kernel-default)
# Redefinition from /usr/lib/rpm/kernel/kernel-spec-macros
%define source_rel %(echo %krel | sed -r 's/\\.[0-9]+($|\\.[^.]*[^.0-9][^.]*$)/\\1/')
Name: kselftests
Version: 1
Release: 0
License: GPL-2.0-only
Summary: Kernel Selftests
Group: Development/Tools/Other
Url: http://www.kernel.org/
Requires: %{name}-kmp-default
Requires: which
BuildRequires: %kernel_module_package_buildreqs
BuildRequires: kernel-source
BuildRequires: kernel-default
BuildRequires: gcc
BuildRequires: gcc-c++
BuildRequires: clang%{llvm_version}
BuildRequires: llvm%{llvm_version}
BuildRequires: llvm%{llvm_version}-devel
BuildRequires: lld%{llvm_version}
BuildRequires: pesign-obs-integration
BuildRequires: make
BuildRequires: tar
BuildRequires: rsync
BuildRequires: fdupes
BuildRequires: bison
BuildRequires: bc
BuildRequires: flex
# bpf
Requires: iproute2
Requires: iputils
Requires: iptables
Requires: openssl
Requires: fsverity-utils
Requires: keyutils
Requires: ethtool
Requires: netcat
BuildRequires: pahole
BuildRequires: libcap-devel
BuildRequires: libelf-devel
BuildRequires: libopenssl-devel
BuildRequires: python3-docutils
Provides: liburandom_read.so(LIBURANDOM_READ_1.0.0)(64bit)
Provides: liburandom_read.so(LIBURANDOM_READ_2.0.0)(64bit)
# bpf:test_tc_tunnel.sh needs mpls_iptunnel and mpls_gso
%if !0%{?is_opensuse}
Requires: kernel-default-extra
%endif
# net
BuildRequires: python3-PyYAML
BuildRequires: python3-jsonschema
BuildRequires: libnuma-devel
BuildRequires: libmnl-devel
%kernel_module_package
%description
This package contains the upstream Linux kernel selftests found in the tools/testing/selftests directory.
The tests are intended to be supported only in testing and QA environments, they are not intended to be run on production systems.
%prep
rsync -a /usr/src/linux/ ./
# BPF: Remove use of rpath in liburandom_read.so
sed -i 's/TEST_GEN_PROGS = /export LD_LIBRARY_PATH = $(OUTPUT)\nTEST_GEN_PROGS = /' tools/testing/selftests/bpf/Makefile
sed -i 's/-Wl,-rpath=. \+//' tools/testing/selftests/bpf/Makefile
# Configure an auxiliary vmlinux so that all required symbols are there for the tests binaries
for target in %{targets}; do
# Don't fail build on warnings
sed -i 's/-Werror//g' tools/testing/selftests/$target/Makefile
# Use upstream configs
if [ -f tools/testing/selftests/$target/config ]; then
cat tools/testing/selftests/$target/config >> .config
fi
arch_config=tools/testing/selftests/$target/config."$(uname -i)"
if [ -f $arch_config ]; then
cat $arch_config >> .config
fi
done
# Livepatch: Add missing requirements
scripts/config -e FTRACE -e FUNCTION_TRACER -e DYNAMIC_FTRACE -e DYNAMIC_FTRACE_WITH_REGS -e DEBUG_KERNEL -e KALLSYMS_ALL
scripts/config -e SUSE_PRODUCT_OPENSUSE_TUMBLEWEED -e MODULES
%build
# Build auxiliary vmlinux
%make_build olddefconfig
%make_build
%make_build -C tools/testing/selftests SKIP_TARGETS= TARGETS="%{targets}" KDIR="$(pwd)"
%make_build -C tools/testing/selftests SKIP_TARGETS= TARGETS="%{targets}" KDIR="$(pwd)" install
find tools/testing/selftests/kselftest_install -name '*.ko' -print -delete
# Now that the selftests have been built, let's bring the running vmlinux
# instead of using `make -C /lib/modules/$(uname -r)/build modules M=...`, since we need BTF info
rsync -a --exclude=Makefile /lib/modules/"$(uname -r)"/build/ ./
rm vmlinux
%if 0%{?sle_version} == 150700
cp /boot/vmlinux-"$(uname -r)".gz .
gzip -d vmlinux-"$(uname -r)".gz
mv vmlinux-"$(uname -r)" vmlinux
%else
cp /lib/modules/"$(uname -r)"/vmlinux.xz .
unxz vmlinux.xz
%endif
for mod_dir in %{mod_dirs}; do
# Remove test modules built against auxiliary vmlinux
%make_build -C tools/testing/selftests/$mod_dir clean
# They need to be built and signed against kernel-default
%make_build modules KDIR="$(pwd)" M="$(realpath tools/testing/selftests/$mod_dir)"
done
# Make up a list of test modules which need to be within installation directory
find tools/testing/selftests -name '*.ko' ! -name '*.tmp.ko' -printf './%%P\n' > modlist
# BPF: liburandom_read also needs to be built against the running vmlinux
find . -name 'liburandom_read.so' -print -delete
%make_build -C tools/testing/selftests/bpf OUTPUT=$(pwd) $(pwd)/liburandom_read.so
%install
cp LICENSES/preferred/GPL-2.0 .
install -m 755 -d %{buildroot}/%{installdir}
rsync -a tools/testing/selftests/kselftest_install/ %{buildroot}/%{installdir}
for target in %{targets}; do
cp tools/testing/selftests/$target/config* %{buildroot}/%{installdir}/$target || true
done
%fdupes -s %{buildroot}/%{installdir}
install -m 755 -d %{buildroot}/%{_libdir}
install -m 755 liburandom_read.so %{buildroot}/%{_libdir}
# Install test modules in the system path
export BRP_PESIGN_FILES='*.ko'
for mod_dir in %{mod_dirs}; do
%make_build modules_install KDIR="$(pwd)" M="$(realpath tools/testing/selftests/$mod_dir)" INSTALL_MOD_PATH=%{buildroot} INSTALL_MOD_DIR=updates
done
# Some selftests collections expect the test modules to be within the install directory,
# but we need them signed therefore they must really be in /lib/modules
while IFS= read -r mod; do
mkdir -p %{buildroot}/%{installdir}/"$(dirname $mod)"
ln -sf %{kernel_module_directory}/%{kver}-%{source_rel}-default/updates/"$(basename $mod)" %{buildroot}/%{installdir}/$mod
done < modlist
# BPF: Copy needed files that `install` left behind, e.g.
# $ find . -maxdepth 3 -name bpftool
# ./bpf/no_alu32/bpftool
# ./bpf/cpuv4/bpftool
# ./bpf/bpftool
# ./kselftest_install/bpf/bpftool
cd tools/testing/selftests/bpf
ln -s ../bpftool %{buildroot}/%{installdir}/bpf/cpuv4
ln -s ../bpftool %{buildroot}/%{installdir}/bpf/no_alu32
for missing in btf_dump_test_case*.c ima_setup.sh uprobe_multi urandom_read xdp_synproxy verify_sig_setup.sh sign-file; do
find . -name "$missing" -exec cp {} %{buildroot}/%{installdir}/bpf/{} \;
done
cd -
# BPF: Link local liburandom_read to the one in the system
find %{buildroot}/%{installdir}/bpf -type d -exec ln -sf %{_libdir}/liburandom_read.so {} \;
%post
%if !0%{?is_opensuse} && 0%{?sle_version} == 150700
# SLE15-SP7 is pre-usrmerge, but some tests might need it there, e.g.:
# # libbpf: prog 'handle_uprobe_byname2': failed to resolve full path for 'libc.so.6': -2
# # test_uprobe_lib:FAIL:attach_uprobe_byname2 unexpected error: -2
[[ ! -f /usr/lib64/libc.so.6 && ! -L /usr/lib64/libc.so.6 ]] && ln -s /lib64/libc.so.6 /usr/lib64/ || true
%endif
%postun
%if !0%{?is_opensuse} && 0%{?sle_version} == 150700
[[ -L /usr/lib64/libc.so.6 ]] && unlink /usr/lib64/libc.so.6 || true
%endif
%files
%license GPL-2.0
%{installdir}
%{_libdir}/liburandom_read.so
%changelog