ignatenkobrain / rpms / kernel

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