Guest User

file

a guest
Mar 23rd, 2010
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.99 KB | None | 0 0
  1. #!/bin/bash
  2. # Implement blacklisting for udev-loaded modules
  3.  
  4. [ $# -ne 1 ] && exit 1
  5.  
  6. . /etc/rc.conf
  7.  
  8. # grab modules from rc.conf
  9. BLACKLIST="${MOD_BLACKLIST[@]}"
  10. MODPROBE="/sbin/modprobe"
  11. LOGGER="/usr/bin/logger"
  12. RESOLVEALIAS="${MODPROBE} --resolve-alias"
  13. USEBLACKLIST="--use-blacklist"
  14.  
  15. if [ -f /proc/cmdline ]; then
  16.     for cmd in $(cat /proc/cmdline); do
  17.         case $cmd in
  18.             disablemodules=*) eval $cmd ;;
  19.             load_modules=off) exit ;;
  20.         esac
  21.     done
  22.     #parse cmdline entries of the form "disablemodules=x,y,z"
  23.     if [ -n "$disablemodules" ]; then
  24.         BLACKLIST="$BLACKLIST $(echo $disablemodules | sed 's|,| |g')"
  25.     fi
  26. fi
  27.  
  28. #MODULES entries in rc.conf that begin with ! are blacklisted
  29. for mod in ${MODULES[@]}; do
  30.     if [ "${mod}" != "${mod#!}" ]; then
  31.         BLACKLIST="$BLACKLIST ${mod#!}"
  32.     fi
  33. done
  34.  
  35. if [ "$MOD_AUTOLOAD" = "yes" -o "$MOD_AUTOLOAD" = "YES" ]; then
  36.   if [ -n "${BLACKLIST}" ]; then
  37.     # If an alias name is on the blacklist, load no modules for this device
  38.     if echo "${BLACKLIST}" | /bin/grep -q -e " $1 " -e "^$1 " -e " $1\$"; then
  39.       $LOGGER -p info -t "$(basename $0)" "Not loading module alias '$1' because it is blacklisted"
  40.       exit
  41.     fi
  42.     #sanitize the blacklist
  43.     BLACKLIST="$(echo "$BLACKLIST" | sed -e 's|-|_|g')"
  44.     # Try to find all modules for the alias
  45.     mods=$($RESOLVEALIAS $1)
  46.     # If no modules could be found, try if the alias name is a module name
  47.     # In that case, omit the --use-blacklist parameter to imitate normal modprobe behaviour
  48.     [ -z "${mods}" ] && $MODPROBE -qni $1 && mods="$1" && USEBLACKLIST=""
  49.     [ -z "${mods}" ] && $LOGGER -p info -t "$(basename $0)" "'$1' is not a valid module or alias name"
  50.     for mod in ${mods}; do
  51.       # Find the module and all its dependencies
  52.       deps="$($MODPROBE -i --show-depends ${mod})"
  53.       [ $? -ne 0 ] && continue
  54.  
  55.       #sanitize the module names
  56.       deps="$(echo "$deps" | sed \
  57.              -e "s#^insmod /lib.*/\(.*\)\.ko.*#\1#g" \
  58.               -e 's|-|_|g')"
  59.  
  60.      # If the module or any of its dependencies is blacklisted, don't load it
  61.      for dep in $deps; do
  62.        if echo "${BLACKLIST}" | /bin/grep -q -e " ${dep} " -e "^${dep} " -e " ${dep}\$"; then
  63.          if [ "${dep}" = "${mod}" ]; then
  64.            $LOGGER -p info -t "$(basename $0)" "Not loading module '${mod}' for alias '$1' because it is blacklisted"
  65.          else
  66.            $LOGGER -p info -t "$(basename $0)" "Not loading module '${mod}' for alias '$1' because its dependency '${dep}' is blacklisted"
  67.          fi
  68.          continue 2
  69.        fi
  70.      done
  71.      # modprobe usually uses the "blacklist" statements from modprobe.conf only to blacklist all aliases
  72.      # of a module, but not the module itself. We use --use-blacklist here so that modprobe also blacklists
  73.      # module names if we resolved alias names manually above
  74.      $MODPROBE $USEBLACKLIST ${mod}
  75.    done
  76.  else
  77.    $MODPROBE $1
  78.  fi
  79. fi
  80. # vim: set et ts=4:
Advertisement
Add Comment
Please, Sign In to add comment