Blame SOURCES/kmodtool

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