hitmoon / rpms / kernel

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