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