c1a53c
#!/bin/bash
c1a53c
#
c1a53c
# akmodbuild - Helper script for building kernel module SRPMs
c1a53c
# Copyright (c) 2007 Thorsten Leemhuis <fedora@leemhuis.info>
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
#
c1a53c
myprog="akmodsbuild"
c1a53c
myver="0.5.6"
c1a53c
c1a53c
# defaults that might get overwritten by user:
c1a53c
kernels="$(uname -r)"
c1a53c
target="$(uname -m)"
08ac4b
if [[ "${target}" == "armv7l" ]] ; then
08ac4b
	target="armv7hl"
c1a53c
fi
c1a53c
numberofjobs=$(grep -c processor /proc/cpuinfo 2> /dev/null)
c1a53c
verboselevel=2
c1a53c
outputdir="${PWD}"
c1a53c
srpms=
c1a53c
c1a53c
init ()
c1a53c
{
c1a53c
	## startup checks
c1a53c
	# prevent root-usage
08ac4b
	if [[ -w /var ]] ; then
c1a53c
		echo "ERROR: Not to be used as root; start as user or '${myprog}' instead." >&2
08ac4b
		exit 1
c1a53c
	fi
c1a53c
c1a53c
	# do we have everything we need to build for the kernels in question?
c1a53c
	for kernel in ${kernels}; do
08ac4b
		if [[ ! -e /usr/src/kernels/${kernel}/Makefile ]] && [[ ! -e /usr/lib/modules/${kernel}/build/Makefile ]] ; then
c1a53c
			echo "ERROR: Files needed for building modules against kernel" >&2
c1a53c
			echo " ${kernel} could not be found as the following" >&2
c1a53c
			echo " directories are missing:"
c1a53c
			echo " /usr/src/kernels/${kernel}/" >&2
c1a53c
			echo " /usr/lib/modules/${kernel}/build/" >&2
c1a53c
			exit 2
c1a53c
		fi
c1a53c
	done
c1a53c
08ac4b
	if [[ ! "${srpms}" ]] ; then
08ac4b
		echo "ERROR: Please provide a list of SRPM-files to build."
08ac4b
		exit 2
c1a53c
	fi
c1a53c
c1a53c
	# SRPMS available?
c1a53c
	for srpm in ${srpms}; do
08ac4b
		if [[ ! -r ${srpm} ]] ; then
c1a53c
			echo "ERROR: Can't find SRPM ${srpm}"
c1a53c
			exit 1
c1a53c
		fi
c1a53c
	done
c1a53c
c1a53c
	# room to save things
08ac4b
	if [[ ! -d "${outputdir}" ]] ; then
c1a53c
		echo "ERROR: ${outputdir} is not a directory" >&2
c1a53c
		exit 1
08ac4b
	elif [[ ! -w "${outputdir}" ]] ; then
c1a53c
		echo "ERROR: ${outputdir} is not a writable" >&2
c1a53c
		exit 1
c1a53c
	fi
c1a53c
c1a53c
c1a53c
	# make sure this is a number
08ac4b
	if ! (( ${numberofjobs} > 0 )) ; then
c1a53c
		echo "Warning: using hardcoded defaut value for number of jobs"
c1a53c
		numberofjobs=2
c1a53c
	fi
c1a53c
c1a53c
	## preparations
c1a53c
	# tmpdir
08ac4b
	if ! tmpdir="$(mktemp -d -p /tmp ${myprog}.XXXXXXXX)" ; then
08ac4b
		echo "ERROR: Could create tempdir."
08ac4b
		exit 1
c1a53c
	fi
c1a53c
c1a53c
	# buildtreee
c1a53c
	mkdir "${tmpdir}"/{BUILD,SOURCES,SPECS,SRPMS,RPMS,RPMS/"${target}"}
c1a53c
c1a53c
	# logfile
c1a53c
	if [[ ! "${logfile}" ]] ; then
c1a53c
		logfile="${tmpdir}/logfile"
c1a53c
	fi
c1a53c
c1a53c
	if ( [[ -e "${logfile}" ]] && [[ ! -w "${logfile}" ]] ) || ! touch "${logfile}" ; then
08ac4b
		echo "ERROR: Could not write logfile."
08ac4b
		finally
08ac4b
		exit 1
c1a53c
	fi
c1a53c
}
c1a53c
c1a53c
c1a53c
finally()
c1a53c
{
c1a53c
	# kill background jobs if needed
08ac4b
	if [[ "${watch_jobid}" ]] ; then
c1a53c
		kill "${watch_jobid}"
c1a53c
	fi
08ac4b
	if [[ "${rpmbuild_jobid}" ]] ; then
c1a53c
		kill "${rpmbuild_jobid}"
c1a53c
	fi
c1a53c
c1a53c
	# remove tmpfiles
08ac4b
	if [[ -d "${tmpdir}" ]] ; then
c1a53c
		rm -rf "${tmpdir}"
c1a53c
	fi
c1a53c
}
c1a53c
trap "finally" 2
c1a53c
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
08ac4b
	if [[ "${1}" == "--not-logfile" ]] ; then
c1a53c
		local notlogfile=true
c1a53c
		shift
c1a53c
	fi
c1a53c
c1a53c
	# output to console
c1a53c
	if (( ${verboselevel} >= ${this_verbose} )) ; then
c1a53c
		echo "$@" >&${this_fd}
c1a53c
	fi
c1a53c
c1a53c
	# global logfile
08ac4b
	if [[ ! ${notlogfile} ]] ; then
c1a53c
		echo "$@" >> "${logfile}"
c1a53c
	fi
c1a53c
}
c1a53c
c1a53c
c1a53c
watch_rpmbuild()
c1a53c
{
c1a53c
	# background function to show rpmbuild progress
c1a53c
	# does't use akmods_echo here; this stage handles the output on its own
c1a53c
	# (seperate process and there is no need to log this)
08ac4b
	if (( ${verboselevel} == 2 )) ; then
08ac4b
		tail --pid ${1} -n +1 -s 0.1 -f ${2} 2>/dev/null | grep --line-buffered -e '%prep' -e '%build' -e '%install' -e '%clean' | while read line ; do
08ac4b
			if [[ "${line}" != "${line##*prep}" ]] ; then
c1a53c
				echo -n "prep "
08ac4b
			elif [[ "${line}" != "${line##*build}" ]] ; then
c1a53c
				echo -n "build "
08ac4b
			elif [[ "${line}" != "${line##*install}" ]] ; then
c1a53c
				echo -n "install "
08ac4b
			elif [[ "${line}" != "${line##*clean}" ]] ; then
c1a53c
				echo -n "clean; "
c1a53c
				# last linefeed is done by the caller
c1a53c
			fi
c1a53c
		done
08ac4b
	elif (( ${verboselevel} > 2 )) ; then
c1a53c
		tail --pid ${1} -n +1 -s 0.1 -f ${2}
c1a53c
	fi
c1a53c
}
c1a53c
c1a53c
process_srpm()
c1a53c
{
c1a53c
	local source_rpm="${1}"
c1a53c
c1a53c
	# status info
c1a53c
	akmods_echo 1 2 -n "* Rebuilding ${source_rpm} for kernel(s) ${kernels}: "
c1a53c
c1a53c
	# kick off rebuild into background
c1a53c
	/usr/bin/time --format='%x' --output="${tmpdir}/.jobexit" rpmbuild \
c1a53c
		--define "_topdir     ${tmpdir}/" \
c1a53c
		--define "_buildtree  ${tmpdir}/BUILD" \
c1a53c
		--define "_specdir    ${tmpdir}/SPECS" \
c1a53c
		--define "_sourcedir  ${tmpdir}/SOURCES" \
c1a53c
		--define "_srcrpmdir  ${tmpdir}/SRPMS" \
c1a53c
		--define "_rpmdir     ${tmpdir}/RPMS" \
c1a53c
		--define "_smp_mflags -j${numberofjobs}" \
c1a53c
		--define "kernels     ${kernels}" \
c1a53c
		--target ${target} \
c1a53c
		--rebuild "${source_rpm}" 2>&1 | tee -a "${logfile}" > "${tmpdir}/.joblog"  &
c1a53c
c1a53c
	local rpmbuild_jobid=$!
c1a53c
c1a53c
	# show progress
08ac4b
	if (( ${verboselevel} >= 2 )) ; then
c1a53c
		watch_rpmbuild ${rpmbuild_jobid} "${tmpdir}/.joblog" 2> /dev/null &
c1a53c
		local watch_jobid=$!
c1a53c
	fi
c1a53c
c1a53c
	# wait for rpmbuild
c1a53c
	wait ${rpmbuild_jobid}
c1a53c
	local rpmbuild_returncode=$(tail -n 1 "${tmpdir}/.jobexit")
c1a53c
	unset rpmbuild_jobid
c1a53c
c1a53c
	# give watch_rpmbuild a moment to catch up; kill it if it does not
08ac4b
	if (( ${verboselevel} >= 2 )) ; then
c1a53c
		sleep 0.5
c1a53c
		kill ${watch_jobid} &> /dev/null
c1a53c
		unset watch_jobid
c1a53c
	fi
c1a53c
c1a53c
	# did rpmbuild succeed?
08ac4b
	if (( ${rpmbuild_returncode} != 0 )) ; then
c1a53c
		# linefeed:
c1a53c
		akmods_echo 1 2 ""
c1a53c
c1a53c
		akmods_echo 2 2 --not-logfile "rpmbuild failed with errorcode ${rpmbuild_returncode}; last 35 Lines of log:"
c1a53c
		akmods_echo 2 2 --not-logfile "--- "
c1a53c
		tail -n 35 "${tmpdir}/.joblog" >&2
c1a53c
		akmods_echo 2 2  --not-logfile "---"
c1a53c
		return ${rpmbuild_returncode}
c1a53c
	fi
c1a53c
c1a53c
	# finish status for watch_rpmbuild
08ac4b
	if (( ${verboselevel} >= 2 )) ; then
c1a53c
		akmods_echo 1 2 -n "Successfull; "
c1a53c
	fi
c1a53c
c1a53c
	local rpms_built="$(cd "${tmpdir}"/RPMS/"${target}" ; echo *)"
c1a53c
c1a53c
	if ! mv "${tmpdir}/RPMS/${target}/"* "${outputdir}" ; then
c1a53c
		# linefeed:
c1a53c
		akmods_echo 1 2 ""
c1a53c
c1a53c
		akmods_echo 2 2 "Failed to move ${tmpdir}/RPMS/${target}/"* "to ${outputdir}"
c1a53c
		return 128
c1a53c
	fi
c1a53c
08ac4b
	if (( ${verboselevel} == 1 )) ; then
c1a53c
		for rpm in ${rpms_built}; do
c1a53c
			echo "${outputdir%%/}/${rpm}"
c1a53c
		done
08ac4b
	elif (( ${verboselevel} >= 2 )) ; then
c1a53c
		akmods_echo 1 2  "Saved ${rpms_built} in ${outputdir%%/}/"
c1a53c
	fi
c1a53c
c1a53c
c1a53c
	# finished
c1a53c
	return 0
c1a53c
}
c1a53c
c1a53c
myprog_help ()
c1a53c
{
c1a53c
	echo "Rebuilds kmod SRPM(s)"
c1a53c
	echo $'\n'"Usage: ${myprog} [OPTIONS] <srpms>"
c1a53c
	echo $'\n'"Options:"
c1a53c
	echo " -k, --kernels         -- build for kernel-versions (output from 'uname -r')"
064385
	echo " -l, --logfile <file>  -- save rpmbuild output to <file>"
c1a53c
	echo " -o, --outputdir <dir> -- save rpms and logs here (current directory)"
c1a53c
	echo " -t, --target          -- target-arch (output from 'uname -m')"
c1a53c
	echo " -v, --verbose         -- increase verboseness"
c1a53c
	echo " -q, --quiet           -- be more quiet"
c1a53c
	echo " -h, --help            -- show usage"
c1a53c
	echo " -V, --version         -- show version"
c1a53c
}
c1a53c
c1a53c
while [ "${1}" ] ; do
c1a53c
	case "${1}" in
c1a53c
		-k|--kernels)
c1a53c
			shift
c1a53c
			if [[ ! "${1}" ]] ; then
c1a53c
				echo "ERROR: Please provide kernel-version(s) to build for together with --kernel" >&2
c1a53c
				exit 1
c1a53c
			fi
c1a53c
			kernels="${1}"
c1a53c
			shift
c1a53c
			;;
c1a53c
		-l|--logfile)
c1a53c
			shift
08ac4b
			if [[ ! "${1}" ]] ; then
c1a53c
				echo "ERROR: Please provide a filename together with --logfile" >&2
c1a53c
				exit 1
c1a53c
			fi
c1a53c
			logfile="${1}"
c1a53c
			shift
c1a53c
			;;
c1a53c
		-o|--outputdir)
c1a53c
			shift
08ac4b
			if [[ ! "${1}" ]] ; then
c1a53c
				echo "ERROR: Please provide the output directory together with --outputdir" >&2
c1a53c
				exit 1
c1a53c
			fi
c1a53c
			outputdir="${1}"
c1a53c
			shift
c1a53c
			;;
c1a53c
		-t|--target)
c1a53c
			shift
c1a53c
			if [[ ! "${1}" ]] ; then
c1a53c
				echo "ERROR: Please provide the target-arch together with --target" >&2
c1a53c
				exit 1
c1a53c
			fi
c1a53c
			target="${1}"
c1a53c
			shift
c1a53c
			;;
c1a53c
		-v|--verbose)
c1a53c
			let verboselevel++
c1a53c
			shift
c1a53c
			;;
c1a53c
		-q|--quiet)
c1a53c
			let verboselevel--
c1a53c
			shift
c1a53c
			;;
c1a53c
		-h|--help)
c1a53c
			myprog_help
c1a53c
			exit 0
c1a53c
			;;
c1a53c
		-V|--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
		*)
c1a53c
			srpms="${srpms} ${1}"
c1a53c
			shift
c1a53c
			;;
c1a53c
	esac
c1a53c
done
c1a53c
c1a53c
# sanity checks
c1a53c
init
c1a53c
c1a53c
# go
c1a53c
for srpm in ${srpms}; do
c1a53c
	process_srpm ${srpm}
c1a53c
	returncode=$?
c1a53c
08ac4b
	if (( ${returncode} != 0 )) ; then
c1a53c
		finally
c1a53c
		exit ${returncode}
c1a53c
	fi
c1a53c
done
c1a53c
c1a53c
# finished
c1a53c
finally
c1a53c
c1a53c
exit 0