Errors and workarounds when building out-of-tree kernel module RPMs using %kernel_module_package macros

Updated -

An attempt to use %kernel_module_package macros for building an out-of-tree kernel module RPM results in the following errors:

  1. Inability to properly determine kernel package version, resulting in an incorrect -C argument in the make call.

  2. Incorrectly defined %kernel_source macro, causing doubly-appended CPU architecture to the kernel source directory.

  3. Inability to use kernel flavors other than the default (for example debug or zfcpdump) due to changes in the versioning scheme for the flavoured kernels, resulting in an incorrect -C argument in the make call.

  4. Incorrect generation of the default file list, leading to inability to collect the files for the RPM.

  5. Failing debuginfo machinery due to debuginfo not being generated for the *.ko files. This is expected because the package is arch-specific.

To overcome these problems:

  • Provide kernel version explicitly before the %kernel_module_package call to work around problem 1.:

    %global kernel_version 5.14.0-2.el9.%{_target_cpu}
    %kernel_module_package ...
    
  • Supply the correct definition for the %kernel_source macro after the %kernel_module_package call to work around problem 2. and 3.:

    %kernel_module_package ...
    %global kernel_source() $([ default = "%{1}" ] && echo "/usr/src/kernels/%kverrel" || %{kmodtool} kernel_source "%{kverrel}" "%{1}" 2>/dev/null || { ls -Ud \"/usr/src/kernels/%{kverrel}"[.+]"%{1}" | sort -V | tail -n 1; } || echo "/usr/src/kernels/%kverrel.%1")
    
  • Explicitly bundle a file list to work around problem 4.:

    $ cat SOURCES/kmod-kmp_test.filelist 
    /lib/modules/
    
    $ grep -i source1 SPECS/kmod-test.spec 
    Source1:        %{name}.filelist
    %kernel_module_package -f "%{SOURCE1}" default
    

    This step conflicts with the ability to build kernel modules for multiple kernel flavors. Another option is to create the missing firmware directory during the %install stage:

    mkdir -p "$RPM_BUILD_ROOT/lib/firmware"
    
  • Override the %debug_package macro definition to work around the problem 5.:

    %global debug_package %{nil}
    

    Another option is to set the executable bit to the *.ko files after they are produced. This can conflict with some implementations of kernel module signing:

    find . -name "*.ko" -type f -exec chmod u+x '{}' +