Blame SOURCES/kmodtool

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