ferdnyc / rpms / akmods

Forked from rpms/akmods 5 years ago
Clone
Blob Blame History Raw
#!/bin/bash -
############################################################################
#
# akmods - Rebuilds and install akmod RPMs
# Copyright (c) 2007, 2008 Thorsten Leemhuis <fedora@leemhuis.info>
# Copyright (c) 2018 Nicolas Chauvet <kwizart@gmail.com>
#
# 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.
#
############################################################################

myprog="akmods-post"
tmpdir=

# Only do %post builds in ostree
if ! grep -q OSTREE_VERSION= /etc/os-release && ! test -f /run/ostree-booted; then
	exit 0
fi

kmodname=$1
srpm=$2


finally()
{
	# remove tmpfiles
	remove_tmpdir

	exit ${1:-128}
}

# Make sure finally() is run regardless of reason for exiting.
trap "finally" ABRT HUP INT QUIT

create_tmpdir()
{
	if ! tmpdir="$(mktemp -d -p /tmp ${myprog}.XXXXXXXX)/" ; then
		echo "ERROR: failed to create tmpdir." >&2
		finally 1
	fi
	if ! mkdir "${tmpdir}"results ; then
		echo  "ERROR: failed to create result tmpdir." >&2
		finally 1
	fi
}

remove_tmpdir()
{
	# remove tmpfiles
	if [[ "${tmpdir}" ]] && [[ -d "${tmpdir}" ]]; then
		rm -rf "${tmpdir}"
	fi
}

# This is an ostree build, so do build for all
# deployed kernels in the %post
kernels="$(ls /lib/modules)"

create_tmpdir

for kernel in ${kernels} ; do
	echo "Building ${srpm} for kernel ${kernel}"
	# Note: This builds as root, but this is pretty safe because its happening in the ostree %post sandbox.
	#       In fact, given that /usr is a rofiles-fuse mount no other user can access /usr in this sandbox anyway.
	akmodsbuild --quiet --kernels ${kernel} --outputdir ${tmpdir}results --logfile "${tmpdir}/akmodsbuild.log" "${srpm}"  2>&1
	returncode=$?
	if (( ! ${returncode} == 0 )); then
		finally 1
	fi
done

for f in $(find "${tmpdir}results" -type f -name '*.rpm' | grep -v debuginfo) ; do
	rpm2cpio $f | cpio --quiet  -D / -id
	returncode=$?
	if (( ! ${returncode} == 0 )); then
		echo "Extracting $f failed:" 2>&1
		finally 1
	fi
done

for kernel in ${kernels} ; do
   depmod -v ${kernel} 2>&1
done

finally 0