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