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