jfaracco / rpms / kernel-auto

Forked from rpms/kernel-auto 3 years ago
Clone

Blame SOURCES/mod-denylist.sh

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