Blame SOURCES/kmodtool

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