Jeremy Katz feffba
#!/bin/bash
Jeremy Katz feffba
Jeremy Katz feffba
# kmodtool - Helper script for building kernel module RPMs
Jeremy Katz feffba
# Copyright (c) 2003-2006 Ville Skyttä <ville.skytta@iki.fi>,
Jeremy Katz feffba
#                         Thorsten Leemhuis <fedora@leemhuis.info>
Jeremy Katz feffba
#                         Jon Masters <jcm@redhat.com>
Jeremy Katz feffba
#
Jeremy Katz feffba
# Permission is hereby granted, free of charge, to any person obtaining
Jeremy Katz feffba
# a copy of this software and associated documentation files (the
Jeremy Katz feffba
# "Software"), to deal in the Software without restriction, including
Jeremy Katz feffba
# without limitation the rights to use, copy, modify, merge, publish,
Jeremy Katz feffba
# distribute, sublicense, and/or sell copies of the Software, and to
Jeremy Katz feffba
# permit persons to whom the Software is furnished to do so, subject to
Jeremy Katz feffba
# the following conditions:
Jeremy Katz feffba
#
Jeremy Katz feffba
# The above copyright notice and this permission notice shall be
Jeremy Katz feffba
# included in all copies or substantial portions of the Software.
Jeremy Katz feffba
#
Jeremy Katz feffba
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Jeremy Katz feffba
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Jeremy Katz feffba
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Jeremy Katz feffba
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
Jeremy Katz feffba
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
Jeremy Katz feffba
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
Jeremy Katz feffba
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Jeremy Katz feffba
Jeremy Katz feffba
shopt -s extglob
Jeremy Katz feffba
Jeremy Katz feffba
myprog="kmodtool"
Jeremy Katz feffba
myver="0.10.10_kmp2"
Jeremy Katz feffba
knownvariants=@(BOOT|PAE|@(big|huge)mem|debug|enterprise|kdump|?(large)smp|uml|xen[0U]?(-PAE)|xen)
Jeremy Katz feffba
kmod_name=
Jeremy Katz feffba
kver=
Jeremy Katz feffba
verrel=
Jeremy Katz feffba
variant=
Jeremy Katz feffba
kmp=
Jeremy Katz feffba
Jeremy Katz feffba
get_verrel ()
Jeremy Katz feffba
{
Jeremy Katz feffba
  verrel=${1:-$(uname -r)}
Jeremy Katz feffba
  verrel=${verrel%%$knownvariants}
Jeremy Katz feffba
}
Jeremy Katz feffba
Jeremy Katz feffba
print_verrel ()
Jeremy Katz feffba
{
Jeremy Katz feffba
  get_verrel $@
Jeremy Katz feffba
  echo "${verrel}"
Jeremy Katz feffba
}
Jeremy Katz feffba
Jeremy Katz feffba
get_variant ()
Jeremy Katz feffba
{
Jeremy Katz feffba
  get_verrel $@
Jeremy Katz feffba
  variant=${1:-$(uname -r)}
Jeremy Katz feffba
  variant=${variant##$verrel}
Jeremy Katz feffba
  variant=${variant:-'""'}
Jeremy Katz feffba
}
Jeremy Katz feffba
Jeremy Katz feffba
print_variant ()
Jeremy Katz feffba
{
Jeremy Katz feffba
  get_variant $@
Jeremy Katz feffba
  echo "${variant}"
Jeremy Katz feffba
}
Jeremy Katz feffba
Jeremy Katz feffba
get_rpmtemplate ()
Jeremy Katz feffba
{
Jeremy Katz feffba
    local variant="${1}"
Jeremy Katz feffba
    local dashvariant="${variant:+-${variant}}"
Jeremy Katz feffba
    case "$verrel" in
Jeremy Katz feffba
        *.el*) kdep="kernel${dashvariant}-%{_target_cpu} = ${verrel}" ;;
Jeremy Katz feffba
        *.EL*) kdep="kernel${dashvariant}-%{_target_cpu} = ${verrel}" ;;
Jeremy Katz feffba
        *)     kdep="kernel-%{_target_cpu} = ${verrel}${variant}"     ;;
Jeremy Katz feffba
    esac
Jeremy Katz feffba
Jeremy Katz feffba
    echo "%package       -n kmod-${kmod_name}${dashvariant}"
Jeremy Katz feffba
Jeremy Katz feffba
    if [ -z "$kmp_provides_summary" ]; then
Jeremy Katz feffba
        echo "Summary:          ${kmod_name} kernel module(s)"
Jeremy Katz feffba
    fi
Jeremy Katz feffba
Jeremy Katz feffba
    if [ -z "$kmp_provides_group" ]; then
Jeremy Katz feffba
        echo "Group:            System Environment/Kernel"
Jeremy Katz feffba
    fi
Jeremy Katz feffba
Jon Masters 83405d
    if [ ! -z "$kmp_version" ]; then
Jeremy Katz feffba
        echo "Version: %{kmp_version}"
Jon Masters 83405d
    fi
Jon Masters 83405d
Jon Masters 83405d
    if [ ! -z "$kmp_release" ]; then
Jeremy Katz feffba
        echo "Release: %{kmp_release}"
Jeremy Katz feffba
    fi
20a456
Jeremy Katz feffba
    if [ ! -z "$kmp" ]; then
Jeremy Katz feffba
        echo "%global _use_internal_dependency_generator 0"
Jeremy Katz feffba
    fi
20a456
Jeremy Katz feffba
    cat <
Jeremy Katz feffba
Provides:         kernel-modules = ${verrel}${variant}
Jeremy Katz feffba
Provides:         ${kmod_name}-kmod = %{?epoch:%{epoch}:}%{version}-%{release}
Jeremy Katz feffba
EOF
20a456
Jeremy Katz feffba
    if [ -z "$kmp" ]; then
Jeremy Katz feffba
        echo "Requires:         ${kdep}"
Jeremy Katz feffba
    fi
Jon Masters 83405d
Jon Masters 83405d
#
Jon Masters 83405d
# RHEL5 - Remove common package requirement on general kmod packages.
Jon Masters 83405d
# Requires: ${kmod_name}-kmod-common >= %{?epoch:%{epoch}:}%{version}
Jon Masters 83405d
#
Jon Masters 83405d
Jeremy Katz feffba
    cat <
Jeremy Katz feffba
Requires(post):   /sbin/depmod
Jeremy Katz feffba
Requires(postun): /sbin/depmod
Jeremy Katz feffba
EOF
Jeremy Katz feffba
Jeremy Katz feffba
if [ "no" != "$kmp_nobuildreqs" ]
Jeremy Katz feffba
then
Jeremy Katz feffba
    echo "BuildRequires: kernel${dashvariant}-devel-%{_target_cpu} = ${verrel}"
Jeremy Katz feffba
fi
Jeremy Katz feffba
Jeremy Katz feffba
if [ "" != "$kmp_override_preamble" ]
Jeremy Katz feffba
then
Jeremy Katz feffba
    cat "$kmp_override_preamble"
Jeremy Katz feffba
fi
Jeremy Katz feffba
Jeremy Katz feffba
cat <
Jeremy Katz feffba
%description   -n kmod-${kmod_name}${dashvariant}
Jeremy Katz feffba
This package provides the ${kmod_name} kernel modules built for the Linux
Jeremy Katz feffba
kernel ${verrel}${variant} for the %{_target_cpu} family of processors.
Jeremy Katz feffba
%post          -n kmod-${kmod_name}${dashvariant}
Jeremy Katz feffba
if [ -e "/boot/System.map-${verrel}${variant}" ]; then
Jeremy Katz feffba
    /sbin/depmod -aeF "/boot/System.map-${verrel}${variant}" "${verrel}${variant}" > /dev/null || :
Jeremy Katz feffba
fi
Jeremy Katz feffba
EOF
20a456
Jeremy Katz feffba
    if [ ! -z "$kmp" ]; then
Jeremy Katz feffba
        cat <
Jon Masters 83405d
Jon Masters 83405d
modules=( \$(find /lib/modules/${verrel}${variant}/extra/${kmod_name}) )
Jeremy Katz feffba
if [ -x "/sbin/weak-modules" ]; then
Jeremy Katz feffba
    printf '%s\n' "\${modules[@]}" \
Jeremy Katz feffba
    | /sbin/weak-modules --add-modules
Jeremy Katz feffba
fi
Jeremy Katz feffba
%preun         -n kmod-${kmod_name}${dashvariant}
Jon Masters 83405d
rpm -ql kmod-${kmod_name}${dashvariant} | grep '\.ko$' \
Jeremy Katz feffba
    > /var/run/rpm-kmod-${kmod_name}${dashvariant}-modules
Jeremy Katz feffba
EOF
20a456
Jeremy Katz feffba
    fi
20a456
Jeremy Katz feffba
    cat <
Jeremy Katz feffba
%postun        -n kmod-${kmod_name}${dashvariant}
Jeremy Katz feffba
/sbin/depmod -aF /boot/System.map-${verrel}${variant} ${verrel}${variant} &> /dev/null || :
Jeremy Katz feffba
EOF
20a456
Jeremy Katz feffba
    if [ ! -z "$kmp" ]; then
Jeremy Katz feffba
        cat <
Jeremy Katz feffba
modules=( \$(cat /var/run/rpm-kmod-${kmod_name}${dashvariant}-modules) )
Jeremy Katz feffba
#rm /var/run/rpm-kmod-${kmod_name}${dashvariant}-modules
Jeremy Katz feffba
if [ -x "/sbin/weak-modules" ]; then
Jeremy Katz feffba
    printf '%s\n' "\${modules[@]}" \
Jeremy Katz feffba
    | /sbin/weak-modules --remove-modules
Jeremy Katz feffba
fi
Jeremy Katz feffba
EOF
Jeremy Katz feffba
    fi
Jeremy Katz feffba
Jeremy Katz feffba
echo "%files         -n kmod-${kmod_name}${dashvariant}"
Jeremy Katz feffba
Jeremy Katz feffba
if [ "" == "$kmp_override_filelist" ];
Jeremy Katz feffba
then
Jeremy Katz feffba
    echo "%defattr(644,root,root,755)"
Jeremy Katz feffba
    echo "/lib/modules/${verrel}${variant}/"
Jon Masters 83405d
    echo "/lib/firmware/"
Jeremy Katz feffba
else
Jeremy Katz feffba
    cat "$kmp_override_filelist"
Jeremy Katz feffba
fi
Jeremy Katz feffba
}
Jeremy Katz feffba
Jeremy Katz feffba
print_rpmtemplate ()
Jeremy Katz feffba
{
Jeremy Katz feffba
  kmod_name="${1}"
Jeremy Katz feffba
  shift
Jeremy Katz feffba
  kver="${1}"
Jeremy Katz feffba
  get_verrel "${1}"
Jeremy Katz feffba
  shift
Jeremy Katz feffba
  if [ -z "${kmod_name}" ] ; then
Jeremy Katz feffba
    echo "Please provide the kmodule-name as first parameter." >&2
Jeremy Katz feffba
    exit 2
Jeremy Katz feffba
  elif [ -z "${kver}" ] ; then
Jeremy Katz feffba
    echo "Please provide the kver as second parameter." >&2
Jeremy Katz feffba
    exit 2
Jeremy Katz feffba
  elif [ -z "${verrel}" ] ; then
Jeremy Katz feffba
    echo "Couldn't find out the verrel." >&2
Jeremy Katz feffba
    exit 2
Jeremy Katz feffba
  fi
20a456
Jeremy Katz feffba
  for variant in "$@" ; do
Jeremy Katz feffba
      if [ "default" == "$variant" ];
Jeremy Katz feffba
      then
Jeremy Katz feffba
            get_rpmtemplate ""
Jeremy Katz feffba
      else
Jeremy Katz feffba
            get_rpmtemplate "${variant}"
Jeremy Katz feffba
      fi
Jeremy Katz feffba
  done
Jeremy Katz feffba
}
Jeremy Katz feffba
Jeremy Katz feffba
usage ()
Jeremy Katz feffba
{
Jeremy Katz feffba
  cat <
Jeremy Katz feffba
You called: ${invocation}
Jeremy Katz feffba
Jeremy Katz feffba
Usage: ${myprog} <command> <option>+
Jeremy Katz feffba
 Commands:
20a456
  verrel <uname>
Jeremy Katz feffba
    - Get "base" version-release.
20a456
  variant <uname>
Jeremy Katz feffba
    - Get variant from uname.
20a456
  rpmtemplate <mainpgkname> <uname> <variants>
Jeremy Katz feffba
    - Return a template for use in a source RPM
Jeremy Katz feffba
  rpmtemplate_kmp <mainpgkname> <uname> <variants>
Jeremy Katz feffba
    - Return a template for use in a source RPM with KMP dependencies
20a456
  version
Jeremy Katz feffba
    - Output version number and exit.
Jeremy Katz feffba
EOF
Jeremy Katz feffba
}
Jeremy Katz feffba
Jeremy Katz feffba
invocation="$(basename ${0}) $@"
Jeremy Katz feffba
while [ "${1}" ] ; do
Jeremy Katz feffba
  case "${1}" in
Jeremy Katz feffba
    verrel)
Jeremy Katz feffba
      shift
Jeremy Katz feffba
      print_verrel $@
Jeremy Katz feffba
      exit $?
Jeremy Katz feffba
      ;;
Jeremy Katz feffba
    variant)
Jeremy Katz feffba
      shift
Jeremy Katz feffba
      print_variant $@
Jeremy Katz feffba
      exit $?
Jeremy Katz feffba
      ;;
Jeremy Katz feffba
    rpmtemplate)
Jeremy Katz feffba
      shift
Jeremy Katz feffba
      print_rpmtemplate "$@"
Jeremy Katz feffba
      exit $?
Jeremy Katz feffba
      ;;
Jeremy Katz feffba
    rpmtemplate_kmp)
Jeremy Katz feffba
      shift
Jeremy Katz feffba
      kmp=1
Jeremy Katz feffba
      print_rpmtemplate "$@"
Jeremy Katz feffba
      exit $?
Jeremy Katz feffba
      ;;
Jeremy Katz feffba
    version)
Jeremy Katz feffba
      echo "${myprog} ${myver}"
Jeremy Katz feffba
      exit 0
Jeremy Katz feffba
      ;;
Jeremy Katz feffba
    *)
Jeremy Katz feffba
      echo "Error: Unknown option '${1}'." >&2
Jeremy Katz feffba
      usage >&2
Jeremy Katz feffba
      exit 2
Jeremy Katz feffba
      ;;
Jeremy Katz feffba
  esac
Jeremy Katz feffba
done
Jeremy Katz feffba
Jeremy Katz feffba
# Local variables:
Jeremy Katz feffba
# mode: sh
Jeremy Katz feffba
# sh-indentation: 2
Jeremy Katz feffba
# indent-tabs-mode: nil
Jeremy Katz feffba
# End:
Jeremy Katz feffba
# ex: ts=2 sw=2 et