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