42d1a9
#! /bin/bash
42d1a9
# shellcheck disable=SC2164
42d1a9
42d1a9
rpm_buildroot="$1"
42d1a9
module_dir="$2"
42d1a9
module_list="$3"
42d1a9
42d1a9
blacklist_conf_files="$(mktemp)"
42d1a9
42d1a9
blacklist()
42d1a9
{
42d1a9
	mkdir -p "$rpm_buildroot/etc/modprobe.d/"
42d1a9
	cat > "$rpm_buildroot/etc/modprobe.d/$1-blacklist.conf" <<-__EOF__
42d1a9
	# This kernel module can be automatically loaded by non-root users. To
42d1a9
	# enhance system security, the module is blacklisted by default to ensure
42d1a9
	# system administrators make the module available for use as needed.
42d1a9
	# See https://access.redhat.com/articles/3760101 for more details.
42d1a9
	#
42d1a9
	# Remove the blacklist by adding a comment # at the start of the line.
42d1a9
	blacklist $1
42d1a9
__EOF__
42d1a9
	echo "%config(noreplace) /etc/modprobe.d/$1-blacklist.conf" >> "$blacklist_conf_files"
42d1a9
}
42d1a9
42d1a9
check_blacklist()
42d1a9
{
42d1a9
	mod="$rpm_buildroot/$1"
42d1a9
	[ ! "$mod" ] && return 0
42d1a9
	if modinfo "$mod" | grep -q '^alias:\s\+net-'; then
42d1a9
		mod="${1##*/}"
42d1a9
		mod="${mod%.ko*}"
42d1a9
		echo "$mod has an alias that allows auto-loading. Blacklisting."
42d1a9
		blacklist "$mod"
42d1a9
	fi
42d1a9
}
42d1a9
42d1a9
foreachp()
42d1a9
{
42d1a9
	P=$(nproc)
42d1a9
	bgcount=0
42d1a9
	while read -r mod; do
42d1a9
		$1 "$mod" &
42d1a9
42d1a9
		bgcount=$((bgcount + 1))
42d1a9
		if [ $bgcount -eq "$P" ]; then
42d1a9
			wait -n
42d1a9
			bgcount=$((bgcount - 1))
42d1a9
		fi
42d1a9
	done
42d1a9
42d1a9
	wait
42d1a9
}
42d1a9
42d1a9
# Many BIOS-es export a PNP-id which causes the floppy driver to autoload
42d1a9
# even though most modern systems don't have a 3.5" floppy driver anymore
42d1a9
# this replaces the old die_floppy_die.patch which removed the PNP-id from
42d1a9
# the module
42d1a9
42d1a9
floppylist=("$rpm_buildroot"/"$module_dir"/kernel/drivers/block/floppy.ko*)
42d1a9
if [[ -n ${floppylist[0]} && -f ${floppylist[0]} ]]; then
42d1a9
     blacklist "floppy"
42d1a9
fi
42d1a9
42d1a9
foreachp check_blacklist < "$module_list"
42d1a9
42d1a9
cat "$blacklist_conf_files" >> "$module_list"
42d1a9
rm -f "$blacklist_conf_files"