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