Blame SOURCES/kmodtool

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