xuexiaolei / rpms / kernel-rt

Forked from rpms/kernel-rt 4 years ago
Clone

Blame SOURCES/mod-blacklist.sh

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