Blame SOURCES/kmodtool

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