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