#!/usr/bin/env bash # shellcheck disable=SC2086 # kmodtool - Helper script for building kernel module RPMs # Copyright (c) 2003-2012 Ville Skyttä , # Thorsten Leemhuis # Nicolas Chauvet # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. shopt -s extglob myprog="kmodtool-${repo}" myver="0.12.1" kmodname= build_kernels="current" kernels_known_variants= kernel_versions= kernel_versions_to_build_for= prefix= filterfile= target= buildroot= dashvariant= error_out() { local errorlevel=${1} shift echo "Error: $*" >&2 # the next line is not multi-line safe -- not needed *yet* echo "%global kmodtool_check echo \"kmodtool error: $*\"; exit ${errorlevel};" exit "${errorlevel}" } print_rpmtemplate_header() { echo echo "%global kmodinstdir_prefix ${prefix}/lib/modules/" echo "%global kmodinstdir_postfix /extra/${kmodname}/" echo "%global kernel_versions ${kernel_versions}" echo } print_akmodtemplate () { echo cat <= %{?epoch:%{epoch}:}%{version} Provides: ${kmodname}-kmod = %{?epoch:%{epoch}:}%{version}-%{release} EOF if [ -n "${obsolete_name}" ]; then echo "Provides: akmod-${obsolete_name} = ${obsolete_version}" echo "Obsoletes: akmod-${obsolete_name} < ${obsolete_version}" fi cat < /dev/null & %files -n akmod-${kmodname} %defattr(-,root,root,-) %{_usrsrc}/akmods/* EOF } print_akmodmeta () { cat <= %{?epoch:%{epoch}:}%{version} %if 0%{?rhel} == 6 || 0%{?centos} == 6 Requires(post): module-init-tools Requires(postun): module-init-tools %else Requires(post): kmod Requires(postun): kmod %endif EOF if [ -n "${obsolete_name}" ]; then echo "Provides: kmod-${obsolete_name}-${kernel_uname_r} = ${obsolete_version}" echo "Obsoletes: kmod-${obsolete_name}-${kernel_uname_r} < ${obsolete_version}" fi # second part if [ -z "${customkernel}" ]; then cat < /dev/null || : elif [ -f "/lib/modules/${kernel_uname_r}/System.map" ]; then ${prefix}${depmod_path} -aeF /lib/modules/${kernel_uname_r}/System.map ${kernel_uname_r} > /dev/null || : else ${prefix}${depmod_path} -ae ${kernel_uname_r} &> /dev/null || : fi %postun -n kmod-${kmodname}-${kernel_uname_r} if [ -f "/boot/System.map-${kernel_uname_r}" ]; then ${prefix}${depmod_path} -aF /boot/System.map-${kernel_uname_r} ${kernel_uname_r} &> /dev/null || : elif [ -f "/lib/modules/${kernel_uname_r}/System.map" ]; then ${prefix}${depmod_path} -aF /lib/modules/${kernel_uname_r}/System.map ${kernel_uname_r} &> /dev/null || : else ${prefix}${depmod_path} -a ${kernel_uname_r} &> /dev/null || : fi EOF else cat < /dev/null || : %postun -n kmod-${kmodname}-${kernel_uname_r} [ "\$(uname -r)" = "${kernel_uname_r}" ] && ${prefix}${depmod_path} -a > /dev/null || : EOF fi # third part cat <= %{?epoch:%{epoch}:}%{version}-%{release}" fi if [ -n "${obsolete_name}" ]; then echo "Provides: kmod-${obsolete_name}-devel = ${obsolete_version}" echo "Obsoletes: kmod-${obsolete_name}-devel < ${obsolete_version}" fi cat < objects for the newest kernel. %files -n kmod-${kmodname}-devel %defattr(644,root,root,755) %{_usrsrc}/${kmodname}-%{version} EOF for kernel in ${1}; do local kernel_uname_r=${kernel} echo "%exclude %{_usrsrc}/${kmodname}-%{version}/${kernel_uname_r}" done echo echo } print_rpmtemplate_per_kmoddevelpkg () { if [ "${1}" = "--custom" ]; then shift local customkernel=true elif [ "${1}" = "--redhat" ]; then # this is needed for akmods shift local redhatkernel=true fi local kernel_uname_r=${1} local kernel_variant="${2:+-${2}}" # first part cat <= %{?epoch:%{epoch}:}%{version}-%{release} %{?KmodsMetaRequires:Requires: %{?KmodsMetaRequires}} EOF if [ -n "${obsolete_name}" ]; then echo "Provides: kmod-${obsolete_name}${kernel_variant} = ${obsolete_version}" echo "Obsoletes: kmod-${obsolete_name}${kernel_variant} < ${obsolete_version}" fi cat < -- filter the results with grep --file " echo " --for-kernels -- created templates only for these kernels" echo " --kmodname -- name of the kmod (required)" echo " --devel -- make kmod-devel package" echo " --noakmod -- no akmod package" echo " --repo -- use buildsys-build--kerneldevpkgs" echo " --target -- target-arch (required)" echo " --buildroot -- Build root (place to look for build files)" } while [ -n "${1}" ] ; do case "${1}" in --filterfile) shift if [ -z "${1}" ] ; then error_out 2 "Please provide path to a filter-file together with --filterfile" >&2 elif [ ! -e "${1}" ]; then error_out 2 "Filterfile ${1} not found" >&2 fi filterfile="${1}" shift ;; --kmodname) shift if [ -z "${1}" ] ; then error_out 2 "Please provide the name of the kmod together with --kmodname" >&2 fi # strip pending -kmod kmodname="${1%%-kmod}" shift ;; --devel) shift devel="true" ;; --prefix) shift if [ -z "${1}" ] ; then error_out 2 "Please provide a prefix with --prefix" >&2 fi prefix="${1}" shift ;; --repo) shift if [ -z "${1}" ] ; then error_out 2 "Please provide the name of the repo together with --repo" >&2 fi repo=${1} shift ;; --for-kernels) shift if [ -z "${1}" ] ; then error_out 2 "Please provide the name of the kmod together with --kmodname" >&2 fi for_kernels="${1}" shift ;; --noakmod) shift noakmod="true" ;; --obsolete-name) shift if [ -z "${1}" ] ; then error_out 2 "Please provide the name of the kmod to obsolete together with --obsolete-name" >&2 fi obsolete_name="${1}" shift ;; --obsolete-version) shift if [ -z "${1}" ] ; then error_out 2 "Please provide the version of the kmod to obsolete together with --obsolete-version" >&2 fi obsolete_version="${1}" shift ;; --target) shift target="${1}" shift ;; --akmod) shift build_kernels="akmod" ;; --newest) shift build_kernels="newest" ;; --current) shift build_kernels="current" ;; --buildroot) shift buildroot="${1}" shift ;; --help) myprog_help exit 0 ;; --version) echo "${myprog} ${myver}" exit 0 ;; *) echo "Error: Unknown option '${1}'." >&2 usage >&2 exit 2 ;; esac done if [ -e ./kmodtool-kernel-variants ]; then kernels_known_variants="$(cat ./kmodtool-kernel-variants)" elif [ -e /usr/share/kmodtool/kernel-variants ] ; then kernels_known_variants="$(cat /usr/share/kmodtool/kernel-variants)" else kernels_known_variants="@(smp?(-debug)|PAE?(-debug)|debug|kdump|xen|kirkwood|highbank|imx|omap|tegra)" fi # general sanity checks if [ -z "${target}" ]; then error_out 2 "please pass target arch with --target" elif [ -z "${kmodname}" ]; then error_out 2 "please pass kmodname with --kmodname" elif [ -z "${kernels_known_variants}" ] ; then error_out 2 "could not determine known variants" elif { [ -n "${obsolete_name}" ] && [ -z "${obsolete_version}" ]; } || { [ -z "${obsolete_name}" ] && [ -n "${obsolete_version}" ]; } ; then error_out 2 "you need to provide both --obsolete-name and --obsolete-version" fi # go if [ -n "${for_kernels}" ]; then # this is easy: print_customrpmtemplate "${for_kernels}" elif [ "${build_kernels}" = "akmod" ]; then # do only a akmod package print_akmodtemplate print_akmodmeta else # seems we are on out own to decide for which kernels to build # we need more sanity checks in this case if [ -z "${repo}" ]; then error_out 2 "please provide repo name with --repo" elif ! command -v "buildsys-build-${repo}-kerneldevpkgs" > /dev/null 2>&1; then error_out 2 "buildsys-build-${repo}-kerneldevpkgs not found" fi # call buildsys-build-${repo}-kerneldevpkgs to get the list of kernels cmdoptions="--target ${target}" # filterfile to filter list of kernels? if [ -n "${filterfile}" ] ; then cmdoptions="${cmdoptions} --filterfile ${filterfile}" fi kernel_versions_to_build_for=$(buildsys-build-${repo}-kerneldevpkgs "--${build_kernels}" ${cmdoptions}) returncode=$? if [ "$returncode" -ne 0 ]; then error_out 2 "buildsys-build-${repo}-kerneldevpkgs failed: ${kernel_versions_to_build_for}" fi if [ "${build_kernels}" = "current" ] && [ -z "${noakmod}" ]; then print_akmodtemplate fi print_rpmtemplate fi