diff --git a/find-provides.ksyms b/find-provides.ksyms new file mode 100755 index 0000000..0526cd7 --- /dev/null +++ b/find-provides.ksyms @@ -0,0 +1,24 @@ +#! /bin/bash + +IFS=$'\n' + +for module in $(grep -E '/lib/modules/.+\.ko$'); do + if [[ -n $(nm $module | sed -r -ne 's:^0*([0-9a-f]+) A __crc_(.+):0x\1 \2:p') ]]; then + nm $module \ + | sed -r -ne 's:^0*([0-9a-f]+) A __crc_(.+):0x\1 \2:p' \ + | awk --non-decimal-data '{printf("ksym(%s) = 0x%08x\n", $2, $1)}' \ + | LC_ALL=C sort -u + else + ELFRODATA=$(readelf -R .rodata $module | awk '/0x/{printf $2$3$4$5}') + if [[ -n $(readelf -h $module | grep "little endian") ]]; then + RODATA=$(echo $ELFRODATA | sed 's/\(..\)\(..\)\(..\)\(..\)/\4\3\2\1/g') + else + RODATA=$ELFRODATA + fi + for sym in $(nm $module | sed -r -ne 's:^0*([0-9a-f]+) R __crc_(.+):0x\1 \2:p'); do + echo $sym $RODATA + done \ + | awk --non-decimal-data '{printf("ksym(%s) = 0x%08s\n", $2, substr($3,($1*2)+1,8))}' \ + | LC_ALL=C sort -u + fi +done diff --git a/find-requires.ksyms b/find-requires.ksyms new file mode 100755 index 0000000..b11c7bb --- /dev/null +++ b/find-requires.ksyms @@ -0,0 +1,74 @@ +#! /bin/bash + +IFS=$'\n' + +# Extract all of the symbols provided by this module. +all_provides() { + if [[ -n $(nm "$@" | sed -r -ne 's:^0*([0-9a-f]+) A __crc_(.+):0x\1 \2:p') ]]; then + nm "$@" \ + | sed -r -ne 's:^0*([0-9a-f]+) A __crc_(.+):0x\1 \2:p' \ + | awk --non-decimal-data '{printf("0x%08x\t%s\n", $1, $2)}' \ + | LC_ALL=C sort -k2,2 -u + else + ELFRODATA=$(readelf -R .rodata "$@" | awk '/0x/{printf $2$3$4$5}') + if [[ -n $(readelf -h "$@" | grep "little endian") ]]; then + RODATA=$(echo $ELFRODATA | sed 's/\(..\)\(..\)\(..\)\(..\)/\4\3\2\1/g') + else + RODATA=$ELFRODATA + fi + for sym in $(nm "$@" | sed -r -ne 's:^0*([0-9a-f]+) R __crc_(.+):0x\1 \2:p'); do + echo $sym $RODATA + done \ + | awk --non-decimal-data '{printf("0x%08s\t%s\n", substr($3,($1*2)+1,8), $2)}' \ + | LC_ALL=C sort -k2,2 -u + fi +} + +# Extract all of the requirements of this module. +all_requires() { + for module in "$@"; do + set -- $(/sbin/modinfo -F vermagic "$module" | sed -e 's: .*::' -e q) + /sbin/modprobe --dump-modversions "$module" \ + | awk --non-decimal-data ' + BEGIN { FS = "\t" ; OFS = "\t" } + {printf("0x%08x\t%s\n", $1, $2)}' \ + | sed -r -e 's:$:\t'"$1"':' + done \ + | LC_ALL=C sort -k2,2 -u +} + +# Filter out requirements fulfilled by the module itself. +mod_requires() { + LC_ALL=C join -t $'\t' -j 2 -v 1 \ + <(all_requires "$@") \ + <(all_provides "$@") \ + | LC_ALL=C sort -k1,1 -u +} + +if ! [ -e /sbin/modinfo -a -e /sbin/modprobe ]; then + cat > /dev/null + exit 0 +fi + +modules=($(grep -E '/lib/modules/.+\.ko$')) +if [ ${#modules[@]} -gt 0 ]; then + kernel=$(/sbin/modinfo -F vermagic "${modules[0]}" | sed -e 's: .*::' -e q) + + # get all that kernel provides + symvers=$(mktemp -t ${0##*/}.XXXXX) + + cat /usr/src/kernels/$kernel/Module.symvers | awk ' + BEGIN { FS = "\t" ; OFS = "\t" } + { print $2 "\t" $1 } + ' \ + | sed -r -e 's:$:\t'"$kernel"':' \ + | LC_ALL=C sort -k1,1 -u > $symvers + + # Symbols matching with the kernel get a "kernel" dependency + LC_ALL=C join -t $'\t' -j 1 $symvers <(mod_requires "${modules[@]}") | LC_ALL=C sort -u \ + | awk '{ FS = "\t" ; OFS = "\t" } { print "kernel(" $1 ") = " $2 }' + + # Symbols from elsewhere get a "ksym" dependency + LC_ALL=C join -t $'\t' -j 1 -v 2 $symvers <(mod_requires "${modules[@]}") | LC_ALL=C sort -u \ + | awk '{ FS = "\t" ; OFS = "\t" } { print "ksym(" $1 ") = " $2 }' +fi diff --git a/firmware.prov b/firmware.prov new file mode 100644 index 0000000..3614369 --- /dev/null +++ b/firmware.prov @@ -0,0 +1,14 @@ +#!/bin/sh +# +# firmware.prov - Automatically extract any and all firmware dependencies from +# kernel object (.ko) files and add to RPM deps. + +IFS=$'\n' + +for module in $(grep -E '/lib/modules/.+\.ko$') $*; +do + for firmware in `/sbin/modinfo -F firmware $module`; + do + echo "firmware($firmware)" + done +done diff --git a/kernel-srpm-macros.spec b/kernel-srpm-macros.spec index 28cc72a..d9a3b1e 100644 --- a/kernel-srpm-macros.spec +++ b/kernel-srpm-macros.spec @@ -1,6 +1,6 @@ Name: kernel-srpm-macros Version: 1.0 -Release: 4%{?dist} +Release: 5%{?dist} Summary: RPM macros that list arches the full kernel is built on # This package only exist in Fedora repositories # The license is the standard (MIT) specified in @@ -8,17 +8,45 @@ Summary: RPM macros that list arches the full kernel is built on # and as URL we provide dist-git URL License: MIT URL: https://src.fedoraproject.org/rpms/kernel-srpm-macros -Source0: macros.kernel-srpm BuildArch: noarch +# Macros +Source0: macros.kernel-srpm +Source1: macros.kmp + +# Dependency generator scripts (deprecated) +Source100: find-provides.ksyms +Source101: find-requires.ksyms +Source102: firmware.prov +Source103: modalias.prov + +# Misc helper scripts +Source200: kmodtool +Source201: rpmsort +Source202: symset-table + +%global rrcdir /usr/lib/rpm/redhat + %description This packages contains the rpm macro that list what arches the full kernel is built on. The variable to use is kernel_arches. +%package -n kernel-rpm-macros +Version: 185 +Release: %{release}%{?dist} +Summary: Macros and scripts for building kernel module packages +Requires: redhat-rpm-config >= 13 + +%description -n kernel-rpm-macros +Macros and scripts for building kernel module packages. + %prep -# nothing to do +# Not strictly necessary but allows working on file names instead +# of source numbers in install section +%setup -c -T +cp -p %{sources} . %build @@ -27,17 +55,38 @@ The variable to use is kernel_arches. %install mkdir -p %{buildroot}/%{_rpmconfigdir}/macros.d -install -p -m 0644 -t %{buildroot}/%{_rpmconfigdir}/macros.d %{SOURCE0} +install -p -m 0644 -t %{buildroot}/%{_rpmconfigdir}/macros.d macros.kernel-srpm %if 0%{?rhel} >= 8 sed -i 's/^%%kernel_arches.*/%%kernel_arches x86_64 s390x ppc64le aarch64/' \ %{buildroot}/%{_rpmconfigdir}/macros.d/macros.kernel-srpm %endif +mkdir -p %{buildroot}%{rrcdir}/find-provides.d +mkdir -p %{buildroot}%{_fileattrsdir} +install -p -m 755 -t %{buildroot}%{rrcdir} kmodtool rpmsort symset-table +install -p -m 755 -t %{buildroot}%{rrcdir} find-provides.ksyms find-requires.ksyms +install -p -m 644 -t %{buildroot}%{rrcdir}/find-provides.d firmware.prov modalias.prov +install -p -m 644 -t %{buildroot}%{_rpmconfigdir}/macros.d macros.kmp + %files -%{_rpmconfigdir}/macros.d/* +%{_rpmconfigdir}/macros.d/macros.kernel-srpm + +%files -n kernel-rpm-macros +%{_rpmconfigdir}/macros.d/macros.kmp +%dir %{rrcdir}/find-provides.d +%{rrcdir}/kmodtool +%{rrcdir}/rpmsort +%{rrcdir}/symset-table +%{rrcdir}/find-provides.ksyms +%{rrcdir}/find-requires.ksyms +%{rrcdir}/find-provides.d/firmware.prov +%{rrcdir}/find-provides.d/modalias.prov %changelog +* Thu Jun 03 2021 Michal Domonkos - 1.0-5 +- Adopt kernel-rpm-macros subpackage from redhat-rpm-config + * Tue Jan 26 2021 Fedora Release Engineering - 1.0-4 - Rebuilt for https://fedoraproject.org/wiki/Fedora_34_Mass_Rebuild diff --git a/kmodtool b/kmodtool new file mode 100755 index 0000000..1308af2 --- /dev/null +++ b/kmodtool @@ -0,0 +1,267 @@ +#!/bin/bash + +# kmodtool - Helper script for building kernel module RPMs +# Copyright (c) 2003-2006 Ville Skyttä , +# Thorsten Leemhuis +# Jon Masters +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +shopt -s extglob + +myprog="kmodtool" +myver="0.10.10_kmp2" +knownvariants=@(BOOT|PAE|@(big|huge)mem|debug|enterprise|kdump|?(large)smp|uml|xen[0U]?(-PAE)|xen) +kmod_name= +kver= +verrel= +variant= +kmp= + +get_verrel () +{ + verrel=${1:-$(uname -r)} + verrel=${verrel%%$knownvariants} +} + +print_verrel () +{ + get_verrel $@ + echo "${verrel}" +} + +get_variant () +{ + get_verrel $@ + variant=${1:-$(uname -r)} + variant=${variant##$verrel} + variant=${variant:-'""'} +} + +print_variant () +{ + get_variant $@ + echo "${variant}" +} + +get_rpmtemplate () +{ + local variant="${1}" + local dashvariant="${variant:+-${variant}}" + case "$verrel" in + *.el*) kdep="kernel${dashvariant}-%{_target_cpu} = ${verrel}" ;; + *.EL*) kdep="kernel${dashvariant}-%{_target_cpu} = ${verrel}" ;; + *) kdep="kernel-%{_target_cpu} = ${verrel}${variant}" ;; + esac + + echo "%package -n kmod-${kmod_name}${dashvariant}" + + if [ -z "$kmp_provides_summary" ]; then + echo "Summary: ${kmod_name} kernel module(s)" + fi + + if [ -z "$kmp_provides_group" ]; then + echo "Group: System Environment/Kernel" + fi + + if [ ! -z "$kmp_version" ]; then + echo "Version: %{kmp_version}" + fi + + if [ ! -z "$kmp_release" ]; then + echo "Release: %{kmp_release}" + fi + + if [ ! -z "$kmp" ]; then + echo "%global _use_internal_dependency_generator 0" + fi + + cat <= %{?epoch:%{epoch}:}%{version} +# + + cat < /dev/null || : +fi +EOF + + if [ ! -z "$kmp" ]; then + cat < /var/run/rpm-kmod-${kmod_name}${dashvariant}-modules +EOF + + fi + + cat < /dev/null || : +EOF + + if [ ! -z "$kmp" ]; then + cat <&2 + exit 2 + elif [ -z "${kver}" ] ; then + echo "Please provide the kver as second parameter." >&2 + exit 2 + elif [ -z "${verrel}" ] ; then + echo "Couldn't find out the verrel." >&2 + exit 2 + fi + + for variant in "$@" ; do + if [ "default" == "$variant" ]; + then + get_rpmtemplate "" + else + get_rpmtemplate "${variant}" + fi + done +} + +usage () +{ + cat <