Justin Vreeland 794d92
#! /bin/bash
Justin Vreeland 794d92
Justin Vreeland 794d92
Dir=$1
Justin Vreeland 794d92
List=$2
Justin Vreeland 794d92
Dest="extra"
Justin Vreeland 794d92
Justin Vreeland 794d92
# Destination was specified on the command line
Justin Vreeland 794d92
test -n "$3" && Dest="$3"
Justin Vreeland 794d92
Justin Vreeland 794d92
pushd $Dir
Justin Vreeland 794d92
rm -rf modnames
Justin Vreeland 794d92
find . -name "*.ko" -type f > modnames
Justin Vreeland 794d92
# Look through all of the modules, and throw any that have a dependency in
Justin Vreeland 794d92
# our list into the list as well.
Justin Vreeland 794d92
rm -rf dep.list dep2.list
Justin Vreeland 794d92
rm -rf req.list req2.list
Justin Vreeland 794d92
touch dep.list req.list
Justin Vreeland 794d92
cp "$List" .
Justin Vreeland 794d92
Justin Vreeland 794d92
# This variable needs to be exported because it is used in sub-script
Justin Vreeland 794d92
# executed by xargs
Justin Vreeland 794d92
export ListName=$(basename "$List")
Justin Vreeland 794d92
Justin Vreeland 794d92
# NB: this loop runs 2000+ iterations. Try to be fast.
Justin Vreeland 794d92
NPROC=`nproc`
Justin Vreeland 794d92
[ -z "$NPROC" ] && NPROC=1
Justin Vreeland 794d92
cat modnames | xargs -r -n1 -P $NPROC sh -c '
Justin Vreeland 794d92
  dep=$1
Justin Vreeland 794d92
  depends=`modinfo $dep | sed -n -e "/^depends/ s/^depends:[ \t]*//p"`
Justin Vreeland 794d92
  [ -z "$depends" ] && exit
Justin Vreeland 794d92
  for mod in ${depends//,/ }
Justin Vreeland 794d92
  do
Justin Vreeland 794d92
    match=$(grep "^$mod.ko" "$ListName")
Justin Vreeland 794d92
    [ -z "$match" ] && continue
Justin Vreeland 794d92
    # check if the module we are looking at is in mod-extra too.
Justin Vreeland 794d92
    # if so we do not need to mark the dep as required.
Justin Vreeland 794d92
    mod2=${dep##*/}  # same as `basename $dep`, but faster
Justin Vreeland 794d92
    match2=$(grep "^$mod2" "$ListName")
Justin Vreeland 794d92
    if [ -n "$match2" ]
Justin Vreeland 794d92
    then
Justin Vreeland 794d92
      #echo $mod2 >> notreq.list
Justin Vreeland 794d92
      continue
Justin Vreeland 794d92
    fi
Justin Vreeland 794d92
    echo $mod.ko >> req.list
Justin Vreeland 794d92
  done
Justin Vreeland 794d92
' DUMMYARG0   # xargs appends MODNAME, which becomes $dep in the script above
Justin Vreeland 794d92
Justin Vreeland 794d92
sort -u req.list > req2.list
Justin Vreeland 794d92
sort -u "$ListName" > modules2.list
Justin Vreeland 794d92
join -v 1 modules2.list req2.list > modules3.list
Justin Vreeland 794d92
Justin Vreeland 794d92
for mod in $(cat modules3.list)
Justin Vreeland 794d92
do
Justin Vreeland 794d92
  # get the path for the module
Justin Vreeland 794d92
  modpath=`grep /$mod modnames`
Justin Vreeland 794d92
  [ -z "$modpath" ] && continue
Justin Vreeland 794d92
  echo $modpath >> dep.list
Justin Vreeland 794d92
done
Justin Vreeland 794d92
Justin Vreeland 794d92
sort -u dep.list > dep2.list
Justin Vreeland 794d92
Justin Vreeland 794d92
# now move the modules into the extra/ directory
Justin Vreeland 794d92
for mod in `cat dep2.list`
Justin Vreeland 794d92
do
Justin Vreeland 794d92
  newpath=`dirname $mod | sed -e "s/kernel\\//$Dest\//"`
Justin Vreeland 794d92
  mkdir -p $newpath
Justin Vreeland 794d92
  mv $mod $newpath
Justin Vreeland 794d92
done
Justin Vreeland 794d92
Justin Vreeland 794d92
popd
Justin Vreeland 794d92
Justin Vreeland 794d92
# If we're signing modules, we can't leave the .mod files for the .ko files
Justin Vreeland 794d92
# we've moved in .tmp_versions/.  Remove them so the Kbuild 'modules_sign'
Justin Vreeland 794d92
# target doesn't try to sign a non-existent file.  This is kinda ugly, but
Justin Vreeland 794d92
# so is modules-extra.
Justin Vreeland 794d92
Justin Vreeland 794d92
for mod in `cat ${Dir}/dep2.list`
Justin Vreeland 794d92
do
Justin Vreeland 794d92
  modfile=`basename $mod | sed -e 's/.ko/.mod/'`
Justin Vreeland 794d92
  rm .tmp_versions/$modfile
Justin Vreeland 794d92
done
Justin Vreeland 794d92
Justin Vreeland 794d92
pushd $Dir
Justin Vreeland 794d92
rm modnames dep.list dep2.list req.list req2.list
Justin Vreeland 794d92
rm "$ListName" modules2.list modules3.list
Justin Vreeland 794d92
popd