6d8bbf
#! /bin/bash
6d8bbf
fbd9e9
RpmDir=$1
fbd9e9
ModDir=$2
fbd9e9
Dir="$1/$2"
fbd9e9
# Note the list filename must have the format mod-[PACKAGE].list, for example,
fbd9e9
# mod-internal.list or mod-extra.list.  The PACKAGE is used to create a
fbd9e9
# override directory for the modules.
fbd9e9
List=$3
fbd9e9
Dest="$4"
fbd9e9
fbd9e9
blacklist()
fbd9e9
{
fbd9e9
	cat > "$RpmDir/etc/modprobe.d/$1-blacklist.conf" <<-__EOF__
fbd9e9
	# This kernel module can be automatically loaded by non-root users. To
fbd9e9
	# enhance system security, the module is blacklisted by default to ensure
fbd9e9
	# system administrators make the module available for use as needed.
fbd9e9
	# See https://access.redhat.com/articles/3760101 for more details.
fbd9e9
	#
fbd9e9
	# Remove the blacklist by adding a comment # at the start of the line.
fbd9e9
	blacklist $1
fbd9e9
__EOF__
fbd9e9
}
fbd9e9
fbd9e9
check_blacklist()
fbd9e9
{
fbd9e9
	mod=$(find $RpmDir/$ModDir -name "$1")
fbd9e9
	[ ! "$mod" ] && return 0
fbd9e9
	if modinfo $mod | grep -q '^alias:\s\+net-'; then
fbd9e9
		mod="${1##*/}"
fbd9e9
		mod="${mod%.ko*}"
fbd9e9
		echo "$mod has an alias that allows auto-loading. Blacklisting."
fbd9e9
		blacklist "$mod"
fbd9e9
	fi
fbd9e9
}
fbd9e9
fbd9e9
find_depends()
fbd9e9
{
fbd9e9
	dep=$1
fbd9e9
	depends=`modinfo $dep | sed -n -e "/^depends/ s/^depends:[ \t]*//p"`
fbd9e9
	[ -z "$depends" ] && exit
fbd9e9
	for mod in ${depends//,/ }
fbd9e9
	do
fbd9e9
		match=$(grep "^$mod.ko" "$ListName")
fbd9e9
		[ -z "$match" ] && continue
fbd9e9
		# check if the module we are looking at is in mod-* too.
fbd9e9
		# if so we do not need to mark the dep as required.
fbd9e9
		mod2=${dep##*/}  # same as `basename $dep`, but faster
fbd9e9
		match2=$(grep "^$mod2" "$ListName")
fbd9e9
		if [ -n "$match2" ]
fbd9e9
		then
fbd9e9
			#echo $mod2 >> notreq.list
fbd9e9
			continue
fbd9e9
		fi
fbd9e9
		echo $mod.ko >> req.list
fbd9e9
	done
fbd9e9
}
fbd9e9
fbd9e9
foreachp()
fbd9e9
{
fbd9e9
	P=$(nproc)
fbd9e9
	bgcount=0
fbd9e9
	while read mod; do
fbd9e9
		$1 "$mod" &
fbd9e9
fbd9e9
		bgcount=$((bgcount + 1))
fbd9e9
		if [ $bgcount -eq $P ]; then
fbd9e9
			wait -n
fbd9e9
			bgcount=$((bgcount - 1))
fbd9e9
		fi
fbd9e9
	done
fbd9e9
fbd9e9
	wait
fbd9e9
}
6d8bbf
6d8bbf
# Destination was specified on the command line
fbd9e9
test -n "$4" && echo "$0: Override Destination $Dest has been specified."
6d8bbf
6d8bbf
pushd $Dir
fbd9e9
fbd9e9
OverrideDir=$(basename $List)
fbd9e9
OverrideDir=${OverrideDir%.*}
fbd9e9
OverrideDir=${OverrideDir#*-}
fbd9e9
mkdir -p $OverrideDir
fbd9e9
6d8bbf
rm -rf modnames
6d8bbf
find . -name "*.ko" -type f > modnames
6d8bbf
# Look through all of the modules, and throw any that have a dependency in
6d8bbf
# our list into the list as well.
6d8bbf
rm -rf dep.list dep2.list
6d8bbf
rm -rf req.list req2.list
6d8bbf
touch dep.list req.list
6d8bbf
cp "$List" .
6d8bbf
6d8bbf
# This variable needs to be exported because it is used in sub-script
6d8bbf
# executed by xargs
6d8bbf
export ListName=$(basename "$List")
6d8bbf
fbd9e9
foreachp find_depends < modnames
6d8bbf
6d8bbf
sort -u req.list > req2.list
6d8bbf
sort -u "$ListName" > modules2.list
6d8bbf
join -v 1 modules2.list req2.list > modules3.list
6d8bbf
6d8bbf
for mod in $(cat modules3.list)
6d8bbf
do
6d8bbf
  # get the path for the module
6d8bbf
  modpath=`grep /$mod modnames`
6d8bbf
  [ -z "$modpath" ] && continue
6d8bbf
  echo $modpath >> dep.list
6d8bbf
done
6d8bbf
6d8bbf
sort -u dep.list > dep2.list
6d8bbf
fbd9e9
if [ -n "$Dest" ]; then
fbd9e9
	# now move the modules into the $Dest directory
fbd9e9
	for mod in `cat dep2.list`
fbd9e9
	do
fbd9e9
	  newpath=`dirname $mod | sed -e "s/kernel\\//$Dest\//"`
fbd9e9
	  mkdir -p $newpath
fbd9e9
	  mv $mod $newpath
fbd9e9
	  echo $mod | sed -e "s/kernel\\//$Dest\//" | sed -e "s|^.|${ModDir}|g" >> $RpmDir/$ListName
fbd9e9
	done
fbd9e9
fi
6d8bbf
6d8bbf
popd
6d8bbf
6d8bbf
# If we're signing modules, we can't leave the .mod files for the .ko files
6d8bbf
# we've moved in .tmp_versions/.  Remove them so the Kbuild 'modules_sign'
6d8bbf
# target doesn't try to sign a non-existent file.  This is kinda ugly, but
fbd9e9
# so are the modules-* packages.
6d8bbf
6d8bbf
for mod in `cat ${Dir}/dep2.list`
6d8bbf
do
6d8bbf
  modfile=`basename $mod | sed -e 's/.ko/.mod/'`
6d8bbf
  rm .tmp_versions/$modfile
6d8bbf
done
6d8bbf
fbd9e9
if [ ! -n "$Dest" ]; then
fbd9e9
	sed -e "s|^.|${ModDir}|g" ${Dir}/dep2.list > $RpmDir/$ListName
fbd9e9
	echo "./$RpmDir/$ListName created."
fbd9e9
	[ -d "$RpmDir/etc/modprobe.d/" ] || mkdir -p "$RpmDir/etc/modprobe.d/"
fbd9e9
	foreachp check_blacklist < $List
fbd9e9
fi
fbd9e9
fbd9e9
# avoid an empty kernel-extra package
fbd9e9
echo "$ModDir/$OverrideDir" >> $RpmDir/$ListName
fbd9e9
6d8bbf
pushd $Dir
6d8bbf
rm modnames dep.list dep2.list req.list req2.list
6d8bbf
rm "$ListName" modules2.list modules3.list
6d8bbf
popd