c1a53c
#!/bin/bash -
08ac4b
########################################################################
c1a53c
#
c1a53c
# akmods - Rebuilds and install akmod RPMs
c1a53c
# Copyright (c) 2007, 2008 Thorsten Leemhuis <fedora@leemhuis.info>
c6ed5b
# Copyright (c) 2018 Nicolas Chauvet <kwizart@gmail.com>
c1a53c
#
c1a53c
# Permission is hereby granted, free of charge, to any person obtaining
c1a53c
# a copy of this software and associated documentation files (the
c1a53c
# "Software"), to deal in the Software without restriction, including
c1a53c
# without limitation the rights to use, copy, modify, merge, publish,
c1a53c
# distribute, sublicense, and/or sell copies of the Software, and to
c1a53c
# permit persons to whom the Software is furnished to do so, subject to
c1a53c
# the following conditions:
c1a53c
#
c1a53c
# The above copyright notice and this permission notice shall be
c1a53c
# included in all copies or substantial portions of the Software.
c1a53c
#
c1a53c
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
c1a53c
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
c1a53c
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
c1a53c
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
c1a53c
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
c1a53c
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
c1a53c
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
c1a53c
#
08ac4b
########################################################################
c1a53c
#
c1a53c
# ToDo:
c1a53c
# - use yum/dnf to install required kernel-devel packages?
c1a53c
# - better way to detect if a earlier build failed or succeeded
c1a53c
# - special kernel "all" (all that are installed with a matching -devel package; could be called from posttrans in akmods packages)
c1a53c
# - manpage
c1a53c
# - make it configurable if kmod building is done with nohup
c1a53c
# - check on shutdown if akmods is still running and let it finish before continuing
c1a53c
# - make it configurable if kmods from the repo replace local ones
c1a53c
c1a53c
# global vars
c1a53c
myprog="akmods"
c1a53c
myver="0.5.6"
c1a53c
kmodlogfile=
c1a53c
continue_line=""
c1a53c
tmpdir=
c1a53c
kernels=
c1a53c
verboselevel=2
6a3213
# We cannot differenciate from a code failure to shutdown kill9 oom etc
6a3213
# So we always retry anyway
6a3213
alwaystry=1
c1a53c
c1a53c
akmods_echo()
c1a53c
{
c1a53c
	# where to output
c1a53c
	local this_fd=${1}
c1a53c
	shift
c1a53c
c1a53c
	# verboselevel
c1a53c
	local this_verbose=${1}
c1a53c
	shift
c1a53c
c1a53c
	# output to console
c1a53c
	if (( ${verboselevel} >= ${this_verbose} )) ; then
c1a53c
		if [[ "${1}" == "--success" ]] ; then
c1a53c
			echo_success
c1a53c
			continue_line=""
c1a53c
			echo
c1a53c
			return 0
08ac4b
		elif [[ "${1}" == "--failure" ]] ; then
c1a53c
			echo_failure
c1a53c
			echo
c1a53c
			continue_line=""
c1a53c
			return 0
08ac4b
		elif [[ "${1}" == "--warning" ]] ; then
c1a53c
			echo_warning
c1a53c
			echo
c1a53c
			continue_line=""
c1a53c
			return 0
08ac4b
		elif [[ "${1}" == "-n" ]] ; then
c1a53c
			continue_line="true"
c1a53c
		fi
c1a53c
		echo "$@" >&${this_fd}
c1a53c
	fi
c1a53c
c1a53c
	# no need to print the status flags in the logs
08ac4b
	if [[ "${1}" == "--success" ]] || [[ "${1}" == "--failure" ]] || [[ "${1}" == "--warning" ]] ; then
c1a53c
		return 0
c1a53c
	fi
c1a53c
c1a53c
	# no need to continues in the log
08ac4b
	if [[ "${1}" == "-n" ]] ; then
c1a53c
		shift
c1a53c
	fi
c1a53c
c1a53c
	# global logfile
c1a53c
	echo "$(date +%Y/%m/%d\ %H:%M:%S) akmods: $@" >> "/var/cache/akmods/akmods.log"
c1a53c
c1a53c
	# the kmods logfile as well, if we work on a kmod
08ac4b
	if [[ "${kmodlogfile}" ]] ; then
c1a53c
		echo "$(date +%Y/%m/%d\ %H:%M:%S) akmods: $@" >> "${kmodlogfile}"
c1a53c
	fi
c1a53c
}
c1a53c
c1a53c
finally()
c1a53c
{
c1a53c
	# remove tmpfiles
c1a53c
	remove_tmpdir
c1a53c
c1a53c
	# remove lockfile
c1a53c
	rm -f /var/cache/akmods/.lockfile
c1a53c
c1a53c
	exit ${1:-128}
c1a53c
}
c1a53c
c1a53c
# Make sure finally() is run regardless of reason for exiting.
c1a53c
trap "finally" ABRT HUP INT QUIT
c1a53c
c1a53c
create_tmpdir()
c1a53c
{
c1a53c
	if ! tmpdir="$(mktemp -d -p /tmp ${myprog}.XXXXXXXX)/" ; then
08ac4b
		akmods_echo 2 1 "ERROR: failed to create tmpdir."
08ac4b
		akmods_echo 2 1 --failure ; return 1
c1a53c
	fi
c1a53c
	if ! mkdir "${tmpdir}"results ; then
08ac4b
		akmods_echo 2 1 "ERROR: failed to create result tmpdir."
08ac4b
		akmods_echo 2 1 --failure ; return 1
c1a53c
	fi
c1a53c
}
c1a53c
c1a53c
remove_tmpdir()
c1a53c
{
c1a53c
	# remove tmpfiles
08ac4b
	if [[ "${tmpdir}" ]] && [[ -d "${tmpdir}" ]] ; then
c1a53c
		rm -f "${tmpdir}"results/* "${tmpdir}"*.log
c1a53c
		rmdir "${tmpdir}"results/ "${tmpdir}"
c1a53c
	fi
c1a53c
}
c1a53c
c1a53c
cleanup_cachedir ()
c1a53c
{
c1a53c
	create_tmpdir
c1a53c
	find /boot/ -maxdepth 1 -name 'vmlinuz*' | sed 's|/boot/vmlinuz-||' > "${tmpdir}"results/kernels
c1a53c
	find "/var/cache/akmods/" -maxdepth 2 -mtime +14 -type f \( -name '*.rpm' -or -name '*.log' \) | grep -v --file "${tmpdir}"results/kernels | xargs --no-run-if-empty rm
c1a53c
	remove_tmpdir
c1a53c
}
c1a53c
c1a53c
init ()
c1a53c
{
c1a53c
	# some security provisions
c1a53c
	\export PATH='/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin'
c1a53c
	\unalias -a
c1a53c
	hash -r
c1a53c
	# https://bugzilla.rpmfusion.org/show_bug.cgi?id=4023
c1a53c
	#ulimit -H -c 0 --
c1a53c
	IFS=$' \t\n'
c1a53c
	UMASK=022
c1a53c
	umask ${UMASK}
c1a53c
c1a53c
	# fall back to current kernel if user didn't provide one
08ac4b
	if [[ ! "${kernels}" ]] ; then
c1a53c
		kernels="$(uname -r)"
c1a53c
	fi
c1a53c
c1a53c
	# we get the echo_{success,failure} stuff from there
08ac4b
	if [[ -r /etc/rc.d/init.d/functions ]] ; then
c1a53c
		source /etc/rc.d/init.d/functions
c1a53c
	else
1a3120
		# Use our own simple replacements
1a3120
		echo_success() {
1a3120
			echo -ne " [  OK  ]\r"
1a3120
			return 0
1a3120
		}
1a3120
		echo_failure() {
1a3120
			echo -ne " [FAILED]\r"
1a3120
			return 1
1a3120
		}
1a3120
		echo_warning() {
1a3120
			echo -ne " [WARNING]\r"
1a3120
			return 1
1a3120
		}
c1a53c
	fi
c1a53c
c1a53c
	# needs root permissions
08ac4b
	if [[ ! -w /var ]] ; then
c1a53c
		echo -n "Needs to run as root to be able to install rpms." >&2
08ac4b
		echo_failure ; echo ; exit 1
c1a53c
	fi
c1a53c
c1a53c
	# no akmods
c1a53c
	if [[ ! -d "/usr/src/akmods/" ]] ; then
c1a53c
		echo -n "/usr/src/akmods/ not found." >&2
08ac4b
		echo_failure ; echo ; exit 1
c1a53c
	fi
c1a53c
c1a53c
	# if there are no akmod packages installed there is nothing to do for us
c1a53c
	if ! ls /usr/src/akmods/*-kmod.latest &> /dev/null ; then
c1a53c
		echo -n "No akmod packages found, nothing to do." >&2
08ac4b
		echo_success ; echo ; exit 0
c1a53c
	fi
c1a53c
c1a53c
c1a53c
	# now that we know that we're root make sure our dir for logging and results is available
c1a53c
	if [[ ! -d "/var/cache/akmods/" ]] ; then
c1a53c
		if ! mkdir -p "/var/cache/akmods/" ; then
c1a53c
			echo -n "/var/cache/akmods/ not found and could not be created" >&2
08ac4b
			echo_failure ; echo ; exit 1
c1a53c
		fi
c1a53c
	fi
c1a53c
	if [[ ! -w "/var/cache/akmods/" ]] ; then
afd9df
		echo -n "/var/cache/akmods/ not writable" >&2
08ac4b
		echo_failure ; echo ; exit 1
c1a53c
	fi
c1a53c
c1a53c
	# tools needed
08ac4b
	for tool in akmodsbuild chown flock sed rpmdev-vercmp ; do
c1a53c
		if ! which "${tool}" &> /dev/null ; then
c1a53c
			echo -n "${tool} not found" >&2
08ac4b
			echo_failure ; echo ; exit 1
c1a53c
		fi
c1a53c
	done
c1a53c
c1a53c
	# create lockfile and wait till we get it
c1a53c
	exec 99>/var/lock/subsys/akmods
c1a53c
	flock -w 900 99
c1a53c
}
c1a53c
c1a53c
buildinstall_kmod()
c1a53c
{
c1a53c
	local this_kernelver=${1}
c1a53c
	local this_kmodname=${2}
c1a53c
	local this_kmodsrpm=${3}
c1a53c
	local this_kmodverrel=${4}
c1a53c
08ac4b
	if [[ ! -r "${this_kmodsrpm}" ]] ; then
c1a53c
		akmods_echo 2 1 "ERROR: ${this_kmodsrpm} not found."
08ac4b
		akmods_echo 2 1 --failure ; return 1
c1a53c
	fi
c1a53c
c1a53c
c1a53c
	# result and logdir
08ac4b
	if [[ ! -d "/var/cache/akmods/${this_kmodname}" ]] ; then
c1a53c
		if ! mkdir "/var/cache/akmods/${this_kmodname}" ; then
08ac4b
			akmods_echo 2 1 "ERROR: could not create /var/cache/akmods/${this_kmodname}."
08ac4b
			akmods_echo 2 1 --failure ; return 1
c1a53c
		fi
c1a53c
	fi
c1a53c
c1a53c
	## preparations
c1a53c
	# tmpdir
c1a53c
	create_tmpdir
c1a53c
c1a53c
	# akmods needs to write there (and nobody else, but mktemp takes care of that!)
c1a53c
	chown akmods "${tmpdir}" "${tmpdir}"results
c1a53c
c1a53c
	# remove old logfiles if they exist
c1a53c
	rm -f "/var/cache/akmods/${this_kmodname}/${this_kmodverrel}-for-${this_kernelver}.log" "/var/cache/akmods/${this_kmodname}/.last.log"
c1a53c
c1a53c
	# create a per kmod logfile
c1a53c
	if ! touch "/var/cache/akmods/${this_kmodname}/.last.log" ; then
08ac4b
		akmods_echo 2 1 "ERROR: failed to create kmod specific logfile."
c1a53c
		return 1
c1a53c
	fi
c1a53c
c1a53c
	# akmods_echo will log to this file from now on as well
c1a53c
	kmodlogfile="/var/cache/akmods/${this_kmodname}/.last.log"
c1a53c
c1a53c
	# Unset TMPDIR since it is misused by "runuser"
c1a53c
	# https://bugzilla.rpmfusion.org/show_bug.cgi?id=2596
c1a53c
	unset TMPDIR
c1a53c
c1a53c
	# build module using akmod
12937a
	akmods_echo 1 4 "Building RPM using the command '$(which akmodsbuild) --kernels ${this_kernelver} ${this_kmodsrpm}'"
12937a
	/sbin/runuser -s /bin/bash -c "$(which akmodsbuild) --quiet --kernels ${this_kernelver} --outputdir ${tmpdir}results --logfile ${tmpdir}/akmodsbuild.log ${this_kmodsrpm}" akmods >> "${kmodlogfile}" 2>&1
c1a53c
	local returncode=$?
c1a53c
c1a53c
	# copy rpmbuild log to kmod specific logfile
08ac4b
	if [[ -s "${tmpdir}"/akmodsbuild.log ]] ; then
c1a53c
		while read line ; do
c1a53c
			echo "$(date +%Y/%m/%d\ %H:%M:%S) akmodsbuild: ${line}" >> "${kmodlogfile}"
c1a53c
		done < "${tmpdir}"/akmodsbuild.log
c1a53c
	fi
c1a53c
c1a53c
	# result
08ac4b
	if (( ! ${returncode} == 0 )) ; then
08ac4b
		if [[ "${continue_line}" ]] ; then
c1a53c
			akmods_echo 1 2 --failure
c1a53c
		fi
08ac4b
		akmods_echo 2 1 "Building rpms failed; see /var/cache/akmods/${this_kmodname}/${this_kmodverrel}-for-${this_kernelver}.failed.log for details"
c1a53c
		cp -fl "${kmodlogfile}" "/var/cache/akmods/${this_kmodname}/${this_kmodverrel}-for-${this_kernelver}.failed.log"
c1a53c
		kmodlogfile=""
c1a53c
		remove_tmpdir
c1a53c
		return 4
c1a53c
	fi
c1a53c
c1a53c
	# dnf/yum install - repository disabled on purpose see rfbz#3350
c1a53c
	akmods_echo 1 4 "Installing newly built rpms"
08ac4b
	if [[ -f /usr/bin/dnf ]] ; then
c1a53c
		akmods_echo 1 4 "DNF detected"
c1a53c
		dnf -y install --disablerepo='*' $(find "${tmpdir}results" -type f -name '*.rpm' | grep -v debuginfo) >> "${kmodlogfile}" 2>&1
c1a53c
	else
c1a53c
		akmods_echo 1 4 "DNF not found, using YUM instead."
c1a53c
		yum -y install --disablerepo='*' $(find "${tmpdir}results" -type f -name '*.rpm' | grep -v debuginfo) >> "${kmodlogfile}" 2>&1
c1a53c
	fi
c1a53c
	local returncode=$?
c1a53c
c1a53c
	# place the newly built rpms where user expects them
c1a53c
	cp "${tmpdir}results/"* "/var/cache/akmods/${this_kmodname}/"
c1a53c
c1a53c
	# everything fine?
08ac4b
	if (( ${returncode} != 0 )) ; then
08ac4b
		if [[ "${continue_line}" ]] ; then
c1a53c
			akmods_echo 1 2 --failure
c1a53c
		fi
0710e8
		akmods_echo 2 1 "Could not install newly built RPMs. You can find them and the logfile in:"
0710e8
		akmods_echo 2 1 "/var/cache/akmods/${this_kmodname}/${this_kmodverrel}-for-${this_kernelver}.failed.log"
c1a53c
		cp -fl "${kmodlogfile}" "/var/cache/akmods/${this_kmodname}/${this_kmodverrel}-for-${this_kernelver}.failed.log"
c1a53c
		kmodlogfile=""
c1a53c
		remove_tmpdir
c1a53c
		return 8
c1a53c
	fi
c1a53c
c1a53c
	# finish
c1a53c
	akmods_echo 1 4 "Successful."
c1a53c
	cp -fl "${kmodlogfile}" "/var/cache/akmods/${this_kmodname}/${this_kmodverrel}-for-${this_kernelver}.log"
c1a53c
	kmodlogfile=""
c1a53c
	remove_tmpdir
c1a53c
c1a53c
	return 0
c1a53c
}
c1a53c
c1a53c
check_kmod_up2date()
c1a53c
{
c1a53c
	local this_kernelver=${1}
c1a53c
	local this_kmodname=${2}
c1a53c
c1a53c
	# kmod present?
950a1d
	if [[ ! -d /lib/modules/${this_kernelver}/extra/${this_kmodname}/ ]] ; then
c1a53c
		# build it
c1a53c
		return 1
c1a53c
	fi
c1a53c
c1a53c
	# kmod up2date?
950a1d
	local kmodpackage="$(rpm -qf /lib/modules/${this_kernelver}/extra/${this_kmodname}/ 2> /dev/null)"
08ac4b
	if [[ ! "${kmodpackage}" ]] ; then
c1a53c
		# seems we didn't get what we wanted
c1a53c
		# well, better to do nothing in this case
950a1d
		akmods_echo 1 2 -n "Warning: Could not determine what package owns /lib/modules/${this_kernelver}/extra/${this_kmodname}/"
c1a53c
		return 0
c1a53c
	fi
c1a53c
	local kmodver=$(rpm -q --qf '%{EPOCH}:%{VERSION}-%{RELEASE}\n' "${kmodpackage}" | sed 's|(none)|0|; s!\.\(fc\|lvn\)[0-9]*!!g')
c1a53c
	local akmodver=$(rpm -qp --qf '%{EPOCH}:%{VERSION}-%{RELEASE}\n' /usr/src/akmods/"${this_kmodname}"-kmod.latest | sed 's|(none)|0|; s!\.\(fc\|lvn\)[0-9]*!!g')
c1a53c
c1a53c
	rpmdev-vercmp "${kmodver}" "${akmodver}" &>/dev/null
c1a53c
	local retvalue=$?
08ac4b
	if [[ "$retvalue" == 0 ]] ; then
c1a53c
		# Versions are the same. Nothing to do.
c1a53c
		return 0
08ac4b
	elif [[ "$retvalue" == 11 ]] ; then
c1a53c
		# kmod is newer, nothing to do.
c1a53c
		return 0
08ac4b
	elif [[ "$retvalue" == 12 ]] ; then
c1a53c
		# akmod is newer, need to build kmod.
c1a53c
		return 1
c1a53c
	else
c1a53c
		# Something went wrong
c1a53c
		akmods_echo 1 2 -n "Error: Could not determine if akmod is newer than the installed kmod"
c1a53c
		akmods_echo 1 2 --failure
c1a53c
		return 0
c1a53c
	fi
c1a53c
}
c1a53c
c1a53c
check_kmods()
c1a53c
{
c1a53c
	local this_kernelver="${1}"
c1a53c
c1a53c
	akmods_echo 1 2 -n "Checking kmods exist for ${this_kernelver}"
c1a53c
	for akmods_kmodfile in /usr/src/akmods/*-kmod.latest ; do
c1a53c
		local this_kmodname="$(basename ${akmods_kmodfile%%-kmod.latest})"
08ac4b
c1a53c
		# actually check this akmod?
08ac4b
		if [[ "${akmods}" ]] ; then
c1a53c
			for akmod in ${akmods} ; do
c1a53c
				if [[ "${this_kmodname}" != "${akmod}" ]] ; then
c1a53c
					# ignore this one
c1a53c
					continue 2
c1a53c
				fi
c1a53c
			done
c1a53c
		fi
c1a53c
c1a53c
		# go
c1a53c
		if ! check_kmod_up2date ${this_kernelver} ${this_kmodname} ; then
c1a53c
			# okay, kmod wasn't found or is not up2date
08ac4b
			if [[ "${continue_line}" ]] ; then
08ac4b
				akmods_echo 1 2 --success
c1a53c
				# if the files for building modules are not available don't even try to build modules
c1a53c
				if [[ ! -r /usr/src/kernels/"${this_kernelver}"/Makefile ]] && \
08ac4b
				   [[ ! -r /lib/modules/"${this_kernelver}"/build/Makefile ]] ; then
c1a53c
					akmods_echo 1 2 "Files needed for building modules against kernel"
c1a53c
					akmods_echo 1 2 "${this_kernelver} could not be found as the following"
08ac4b
					akmods_echo 1 2 "directories are missing:"
c1a53c
					akmods_echo 1 2 "/usr/src/kernels/${this_kernelver}/"
950a1d
					akmods_echo 1 2 -n "/lib/modules/${this_kernelver}/build/"
c1a53c
					akmods_echo 1 2 -n "Is the correct kernel-devel package installed?"
c1a53c
					akmods_echo 1 2 --failure
c1a53c
					return 1
c1a53c
				fi
c1a53c
			fi
c1a53c
c1a53c
			local this_kmodverrel="$(rpm -qp --qf '%{VERSION}-%{RELEASE}' "${akmods_kmodfile}" | sed 's!\.\(fc\|lvn\)[0-9]*!!g' )"
08ac4b
			if [[ ! "${alwaystry}" ]] && [[ -e "/var/cache/akmods/${this_kmodname}/${this_kmodverrel}-for-${this_kernelver}".failed.log ]] ; then
c1a53c
				akmods_echo 1 2 -n "Ignoring ${this_kmodname}-kmod as it failed earlier"
c1a53c
				akmods_echo 1 2 --warning
c1a53c
				local someignored="true"
c1a53c
			else
c1a53c
				akmods_echo 1 2 -n "Building and installing ${this_kmodname}-kmod"
c1a53c
			 	buildinstall_kmod ${this_kernelver} ${this_kmodname} ${akmods_kmodfile} ${this_kmodverrel}
08ac4b
				local returncode=$?
08ac4b
				if [[ "$returncode" == "0" ]] ; then
08ac4b
					akmods_echo 1 2 --success
fd6e35
					local somesucceeded="true"
08ac4b
				elif [[ "$returncode" == "8" ]] ; then
08ac4b
					akmods_echo 1 2 --failure "New kmod RPM was built but could not be installed."
c1a53c
				else
c1a53c
					local somefailed="true"
c1a53c
				fi
c1a53c
			fi
c1a53c
		fi
c1a53c
	done
c1a53c
08ac4b
	if [[ "${continue_line}" ]] ; then
c1a53c
		akmods_echo 1 2 --success
c1a53c
	elif [[ "${someignored}" ]] || [[ "${somefailed}" ]] ; then
c1a53c
		echo
c1a53c
		akmods_echo 1 2 "Hint: Some kmods were ignored or failed to build or install."
c1a53c
		akmods_echo 1 2 "You can try to rebuild and install them by by calling"
c1a53c
		akmods_echo 1 2 "'/usr/sbin/akmods --force' as root."
c1a53c
		echo
c1a53c
		sleep 2
c1a53c
	fi
c1a53c
fd6e35
	# akmods for newly installed akmod rpms as wells as akmods.service run
fd6e35
	# after udev and systemd-modules-load.service have tried to load modules
08ac4b
	if [[ "${somesucceeded}" ]] && [[ ${this_kernelver} = "$(uname -r)" ]] ; then
3b4a49
		find /sys/devices -name modalias -print0 | xargs -0 cat | xargs modprobe -a -b -q
08ac4b
		if [ -f /usr/bin/systemctl ] ; then
69ecb7
			systemctl restart systemd-modules-load.service
69ecb7
		fi
fd6e35
	fi
c1a53c
}
c1a53c
c1a53c
myprog_help ()
c1a53c
{
c1a53c
	echo "Checks the akmod packages and rebuilds them if needed"
c1a53c
	echo $'\n'"Usage: ${myprog} [OPTIONS]"
c1a53c
	echo $'\n'"Options:"
c1a53c
	echo " --force             -- try all, even if they failed earlier"
c1a53c
	echo " --kernels <kernel>  -- build and install only for kernel <kernel>"
c1a53c
	echo "                        (formatted the same as 'uname -r' would produce)"
c1a53c
	echo " --akmod <akmod>     -- build and install only akmod <akmod>"
c1a53c
}
c1a53c
c1a53c
c1a53c
# first parse command line options
c1a53c
while [ "${1}" ] ; do
c1a53c
	case "${1}" in
c1a53c
		--kernel|--kernels)
c1a53c
			shift
c1a53c
			if [[ ! "${1}" ]] ; then
c1a53c
				echo "ERROR: Please provide the kernel-version to build for together with --kernel" >&2
c1a53c
				exit 1
c1a53c
			elif [[ ! -r /usr/src/kernels/"${1}"/Makefile ]] && \
08ac4b
				 [[ ! -r /lib/modules/"${1}"/build/Makefile ]] ; then
c1a53c
				echo "Could not find files needed to compile modules for ${1}"
c1a53c
				echo "Are the development files for kernel ${1} or the appropriate kernel-devel package installed?"
c1a53c
				exit 1
c1a53c
			elif [[ -r /usr/src/kernels/"${1}"/Makefile ]] && \
572164
				 [[ ! -d /lib/modules/"${1}" ]] ; then
c1a53c
				# this is a red hat / fedora kernel-devel package, but the kernel for it is not installed
08ac4b
				# kmodtool would add a dep on that kernel when building; thus when we'd try to install the
c1a53c
				# rpms we'd run into a missing-dep problem. Thus we prevent that case
c1a53c
				echo "Kernel ${1} not installed"
c1a53c
				exit 1
c1a53c
			fi
c1a53c
			# overwrites the default:
c1a53c
			kernels="${kernels}${1}"
c1a53c
			# an try to build, even if we tried already
c1a53c
			alwaystry=true
c1a53c
			shift
c1a53c
			;;
c1a53c
		--akmod|--kmod)
c1a53c
			shift
c1a53c
			if [[ ! "${1}" ]] ; then
c1a53c
				echo "ERROR: Please provide a name of a akmod package together with --akmods" >&2
c1a53c
				exit 1
08ac4b
			elif [[ -r /usr/src/akmods/"${1}"-kmod.latest ]] ; then
c1a53c
				akmods="${akmods}${1} "
08ac4b
			elif [[ -r /usr/src/akmods/"${1}".latest ]] ; then
c1a53c
				akmods="${akmods}${1%%-kmod} "
c1a53c
			else
c1a53c
				echo "Could not find akmod ${1}"
c1a53c
				exit 1
c1a53c
			fi
c1a53c
			shift
c1a53c
			;;
c1a53c
		--force)
c1a53c
			alwaystry=true
c1a53c
			shift
c1a53c
			;;
c1a53c
		--from-init)
c1a53c
			# just in case: remove stale lockfile if it exists:
c1a53c
			rm -f /var/cache/akmods/.lockfile
c1a53c
			shift
c1a53c
			;;
c1a53c
		--from-posttrans|--from-kernel-posttrans|--from-akmod-posttrans)
c1a53c
			# ignored
c1a53c
			shift
c1a53c
			;;
c1a53c
		--verbose)
c1a53c
			let verboselevel++
c1a53c
			shift
c1a53c
			;;
c1a53c
		--quiet)
c1a53c
			let verboselevel--
c1a53c
			shift
c1a53c
			;;
c1a53c
		--help)
c1a53c
			myprog_help
c1a53c
			exit 0
c1a53c
			;;
c1a53c
		--version)
c1a53c
			echo "${myprog} ${myver}"
c1a53c
			exit 0
c1a53c
			;;
c1a53c
		*)
c1a53c
			echo "Error: Unknown option '${1}'." >&2
c1a53c
			myprog_help >&2
c1a53c
			exit 2
c1a53c
			;;
c1a53c
	esac
c1a53c
done
c1a53c
c1a53c
# sanity checks
c1a53c
init
c1a53c
c1a53c
# go
c1a53c
for kernel in ${kernels} ; do
c1a53c
	check_kmods ${kernel}
c1a53c
done
c1a53c
c1a53c
# finished :)
c1a53c
finally 0