Blame SOURCES/kmodtool

67e50c
#!/bin/bash
67e50c
67e50c
# kmodtool - Helper script for building kernel module RPMs
936cd0
#            An original version appeared in Fedora. This version is
936cd0
#            generally called only by the %kernel_module_package RPM macro
936cd0
#            during the process of building Driver Update Packages (which
936cd0
#            are also known as "kmods" in the Fedora community).
936cd0
#
936cd0
# Copyright (c) 2003-2010 Ville Skyttä <ville.skytta@iki.fi>,
67e50c
#                         Thorsten Leemhuis <fedora@leemhuis.info>
67e50c
#                         Jon Masters <jcm@redhat.com>
67e50c
#
67e50c
# Permission is hereby granted, free of charge, to any person obtaining
67e50c
# a copy of this software and associated documentation files (the
67e50c
# "Software"), to deal in the Software without restriction, including
67e50c
# without limitation the rights to use, copy, modify, merge, publish,
67e50c
# distribute, sublicense, and/or sell copies of the Software, and to
67e50c
# permit persons to whom the Software is furnished to do so, subject to
67e50c
# the following conditions:
67e50c
#
67e50c
# The above copyright notice and this permission notice shall be
67e50c
# included in all copies or substantial portions of the Software.
67e50c
#
67e50c
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
67e50c
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
67e50c
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
67e50c
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
67e50c
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
67e50c
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
67e50c
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
67e50c
936cd0
# Changelog:
936cd0
#
936cd0
#            2010/07/28 - Add fixes for filelists in line with LF standard
936cd0
#			- Remove now defunct "framepointer" kernel variant
936cd0
#			- Change version to "rhel6-rh2" as a consequence.
936cd0
#
936cd0
#            2010/01/10 - Simplified for RHEL6. We are working on upstream
936cd0
#                         moving to a newer format and in any case do not
936cd0
#                         need to retain support for really old systems.
936cd0
67e50c
shopt -s extglob
67e50c
67e50c
myprog="kmodtool"
936cd0
myver="0.10.10_rhel9"
936cd0
knownvariants=@(debug|kdump|zfcpdump)
67e50c
kmod_name=
67e50c
kver=
67e50c
verrel=
67e50c
variant=
67e50c
67e50c
get_verrel ()
67e50c
{
67e50c
  verrel=${1:-$(uname -r)}
936cd0
  verrel=${verrel/%[.+]$knownvariants/}
67e50c
}
67e50c
67e50c
print_verrel ()
67e50c
{
936cd0
  get_verrel "$@"
67e50c
  echo "${verrel}"
67e50c
}
67e50c
67e50c
get_variant ()
67e50c
{
936cd0
  get_verrel "$@"
67e50c
  variant=${1:-$(uname -r)}
936cd0
  variant=${variant/#$verrel?(.+)/}
67e50c
  variant=${variant:-'""'}
67e50c
}
67e50c
67e50c
print_variant ()
67e50c
{
67e50c
  get_variant $@
67e50c
  echo "${variant}"
67e50c
}
67e50c
936cd0
# Detect flavor separator character. We have to do that due to
936cd0
# a systemd-tailored patch for kernel spec[1][2] introduced in Fedora and then
936cd0
# imported in RHEL 8 that broke all OOT kmod infrastructure for the flavored
936cd0
# kernels.
936cd0
#
936cd0
# [1] https://lists.fedoraproject.org/pipermail/kernel/2013-June/004262.html
936cd0
# [2] https://src.fedoraproject.org/rpms/kernel/c/faf25207dc86666a611c45ae3ffaf385c170bd2a
936cd0
#
936cd0
# $1 - kver
936cd0
# $2 - variant
936cd0
get_variant_char ()
936cd0
{
936cd0
  variant="$2"
936cd0
  [ "$variant" != "default" ] || variant=""
936cd0
936cd0
  get_verrel "$1"
936cd0
936cd0
  variant_char=""
936cd0
  [ -n "$variant" ] || return 0
936cd0
936cd0
  # We expect that the flavored kernel is already installed in the buildroot
936cd0
  variant_char="+"
936cd0
  [ -e "/usr/src/kernels/${verrel}+${variant}" ] && return 0
936cd0
936cd0
  variant_char="."
936cd0
}
936cd0
936cd0
print_variant_char ()
936cd0
{
936cd0
  get_variant_char "$@"
936cd0
  echo "${variant_char}"
936cd0
}
936cd0
936cd0
print_kernel_source ()
936cd0
{
936cd0
  get_variant_char "$@"
936cd0
  echo "/usr/src/kernels/${verrel}${variant_char}${variant}"
936cd0
}
936cd0
936cd0
get_filelist() {
936cd0
	local IFS=$'\n'
936cd0
	filelist=($(cat))
936cd0
936cd0
	if [ ${#filelist[@]} -gt 0 ];
936cd0
	then
936cd0
		for ((n = 0; n < ${#filelist[@]}; n++));
936cd0
		do
936cd0
			line="${filelist[n]}"
936cd0
			line=$(echo "$line" \
936cd0
				| sed -e "s/%verrel/$verrel/g" \
936cd0
				| sed -e "s/%variant/$variant/g" \
936cd0
				| sed -e "s/%dashvariant/$dashvariant/g" \
936cd0
				| sed -e "s/%dotvariant/$dotvariant/g" \
936cd0
				| sed -e "s/\+%1/$dotvariant/g" \
936cd0
				| sed -e "s/\.%1/$dotvariant/g" \
936cd0
				| sed -e "s/\-%1/$dotvariant/g" \
936cd0
				| sed -e "s/%2/$verrel/g")
936cd0
			echo "$line"
936cd0
		done
936cd0
	else
936cd0
		echo "%defattr(644,root,root,755)"
936cd0
		echo "/lib/modules/${verrel}${dotvariant}"
936cd0
	fi
936cd0
}
936cd0
936cd0
67e50c
get_rpmtemplate ()
67e50c
{
67e50c
    local variant="${1}"
936cd0
936cd0
    get_variant_char "${verrel}" "${variant}"
936cd0
67e50c
    local dashvariant="${variant:+-${variant}}"
936cd0
    local dotvariant="${variant:+${variant_char}${variant}}"
67e50c
67e50c
    echo "%package       -n kmod-${kmod_name}${dashvariant}"
67e50c
936cd0
    if [ -z "$kmod_provides_summary" ]; then
67e50c
        echo "Summary:          ${kmod_name} kernel module(s)"
67e50c
    fi
67e50c
936cd0
    if [ -z "$kmod_provides_group" ]; then
67e50c
        echo "Group:            System Environment/Kernel"
67e50c
    fi
67e50c
936cd0
    if [ ! -z "$kmod_version" ]; then
936cd0
        echo "Version: %{kmod_version}"
67e50c
    fi
67e50c
936cd0
    if [ ! -z "$kmod_release" ]; then
936cd0
        echo "Release: %{kmod_release}"
67e50c
    fi
67e50c
67e50c
    cat <
936cd0
Provides:         kernel-modules >= ${verrel}${dotvariant}
936cd0
Provides:         kernel${dashvariant}-modules >= ${verrel}
67e50c
Provides:         ${kmod_name}-kmod = %{?epoch:%{epoch}:}%{version}-%{release}
936cd0
Requires(post):   /usr/sbin/depmod
936cd0
Requires(postun): /usr/sbin/depmod
936cd0
Requires(post):   /usr/sbin/weak-modules
936cd0
Requires(postun): /usr/sbin/weak-modules
67e50c
EOF
67e50c
936cd0
    if [ "yes" != "$nobuildreqs" ]
936cd0
    then
936cd0
        cat <
936cd0
BuildRequires:    kernel${dashvariant}-devel
936cd0
BuildRequires:    kernel-abi-stablelists
936cd0
BuildRequires:    redhat-rpm-config kernel-rpm-macros
936cd0
BuildRequires:    elfutils-libelf-devel kmod
936cd0
EOF
67e50c
    fi
67e50c
936cd0
    if [ "" != "$override_preamble" ]
936cd0
    then
936cd0
        cat "$override_preamble"
936cd0
    fi
67e50c
936cd0
cat <
936cd0
%description   -n kmod-${kmod_name}${dashvariant}
936cd0
This package provides the ${kmod_name} kernel modules built for
936cd0
the Linux kernel ${verrel}${dotvariant} for the %{_target_cpu}
936cd0
family of processors.
67e50c
EOF
67e50c
936cd0
##############################################################################
936cd0
## The following are not part of this script directly, they are scripts     ##
936cd0
## that will be executed by RPM during various stages of package processing ##
936cd0
##############################################################################
67e50c
67e50c
cat <
67e50c
%post          -n kmod-${kmod_name}${dashvariant}
936cd0
if [ -e "/boot/System.map-${verrel}${dotvariant}" ]; then
936cd0
    /usr/sbin/depmod -aeF "/boot/System.map-${verrel}${dotvariant}" "${verrel}${dotvariant}" > /dev/null || :
67e50c
fi
67e50c
936cd0
modules=( \$(find /lib/modules/${verrel}${dotvariant}/extra/${kmod_name} | grep -E '\.ko(\.gz|\.bz2|\.xz|\.zst)?$') )
936cd0
if [ -x "/usr/sbin/weak-modules" ]; then
67e50c
    printf '%s\n' "\${modules[@]}" \
936cd0
    | /usr/sbin/weak-modules --add-modules
67e50c
fi
67e50c
EOF
67e50c
936cd0
cat <
936cd0
%preun         -n kmod-${kmod_name}${dashvariant}
936cd0
rpm -ql kmod-${kmod_name}${dashvariant}-%{kmod_version}-%{kmod_release}.$(arch) | grep -E '\.ko(\.gz|\.bz2|\.xz|\.zst)?$' > /var/run/rpm-kmod-${kmod_name}${dashvariant}-modules
936cd0
EOF
67e50c
936cd0
cat <
67e50c
%postun        -n kmod-${kmod_name}${dashvariant}
936cd0
if [ -e "/boot/System.map-${verrel}${dotvariant}" ]; then
936cd0
    /usr/sbin/depmod -aeF "/boot/System.map-${verrel}${dotvariant}" "${verrel}${dotvariant}" > /dev/null || :
936cd0
fi
67e50c
67e50c
modules=( \$(cat /var/run/rpm-kmod-${kmod_name}${dashvariant}-modules) )
936cd0
rm /var/run/rpm-kmod-${kmod_name}${dashvariant}-modules
936cd0
if [ -x "/usr/sbin/weak-modules" ]; then
67e50c
    printf '%s\n' "\${modules[@]}" \
936cd0
    | /usr/sbin/weak-modules --remove-modules
67e50c
fi
67e50c
EOF
67e50c
67e50c
echo "%files         -n kmod-${kmod_name}${dashvariant}"
67e50c
936cd0
if [ "" == "$override_filelist" ];
67e50c
then
67e50c
    echo "%defattr(644,root,root,755)"
936cd0
    echo "/lib/modules/${verrel}${dotvariant}"
67e50c
else
936cd0
    cat "$override_filelist" | get_filelist
67e50c
fi
67e50c
}
67e50c
67e50c
print_rpmtemplate ()
67e50c
{
67e50c
  kmod_name="${1}"
67e50c
  shift
67e50c
  kver="${1}"
67e50c
  get_verrel "${1}"
67e50c
  shift
67e50c
  if [ -z "${kmod_name}" ] ; then
67e50c
    echo "Please provide the kmodule-name as first parameter." >&2
67e50c
    exit 2
67e50c
  elif [ -z "${kver}" ] ; then
67e50c
    echo "Please provide the kver as second parameter." >&2
67e50c
    exit 2
67e50c
  elif [ -z "${verrel}" ] ; then
67e50c
    echo "Couldn't find out the verrel." >&2
67e50c
    exit 2
67e50c
  fi
67e50c
67e50c
  for variant in "$@" ; do
67e50c
      if [ "default" == "$variant" ];
67e50c
      then
67e50c
            get_rpmtemplate ""
67e50c
      else
67e50c
            get_rpmtemplate "${variant}"
67e50c
      fi
67e50c
  done
67e50c
}
67e50c
67e50c
usage ()
67e50c
{
67e50c
  cat <
67e50c
You called: ${invocation}
67e50c
67e50c
Usage: ${myprog} <command> <option>+
67e50c
 Commands:
67e50c
  verrel <uname>
67e50c
    - Get "base" version-release.
67e50c
  variant <uname>
67e50c
    - Get variant from uname.
936cd0
  variant_char <uname> <variant>
936cd0
    - Get kernel variant separator character.
936cd0
  kernel_source <uname> <variant>
936cd0
    - Get path to kernel source directory.
67e50c
  rpmtemplate <mainpgkname> <uname> <variants>
67e50c
    - Return a template for use in a source RPM
67e50c
  version
67e50c
    - Output version number and exit.
67e50c
EOF
67e50c
}
67e50c
67e50c
invocation="$(basename ${0}) $@"
67e50c
while [ "${1}" ] ; do
67e50c
  case "${1}" in
67e50c
    verrel)
67e50c
      shift
936cd0
      print_verrel "$@"
67e50c
      exit $?
67e50c
      ;;
67e50c
    variant)
67e50c
      shift
936cd0
      print_variant "$@"
67e50c
      exit $?
67e50c
      ;;
936cd0
    variant_char)
67e50c
      shift
936cd0
      print_variant_char "$@"
67e50c
      exit $?
67e50c
      ;;
936cd0
    kernel_source)
936cd0
      shift
936cd0
      print_kernel_source "$@"
936cd0
      exit $?
936cd0
      ;;
936cd0
    rpmtemplate)
67e50c
      shift
67e50c
      print_rpmtemplate "$@"
67e50c
      exit $?
67e50c
      ;;
67e50c
    version)
67e50c
      echo "${myprog} ${myver}"
67e50c
      exit 0
67e50c
      ;;
67e50c
    *)
67e50c
      echo "Error: Unknown option '${1}'." >&2
67e50c
      usage >&2
67e50c
      exit 2
67e50c
      ;;
67e50c
  esac
67e50c
done
67e50c
67e50c
# Local variables:
67e50c
# mode: sh
67e50c
# sh-indentation: 2
67e50c
# indent-tabs-mode: nil
67e50c
# End:
67e50c
# ex: ts=2 sw=2 et