Skip to content

Instantly share code, notes, and snippets.

@fernandolopez
Last active December 14, 2020 19:50
Show Gist options
  • Save fernandolopez/6058830 to your computer and use it in GitHub Desktop.
Save fernandolopez/6058830 to your computer and use it in GitHub Desktop.
unload_mod.sh unloads the selected linux kernel module and all the other modules that depends on it.
#!/bin/bash
###############################################################################
#
# Copyright 2013 Fernando E. M. López
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
###############################################################################
# If you are using experimental, propietary drivers, or propietary firmwares
# you may want to unload some modules when the driver fails, but sometimes
# there are lots of modules related to a device's driver, to unload all find
# the most general of the modules (the one the others rely on to work) and
# pass it as argument to this script.
#
# For example in order to unload the drivers of a usb device you may want
# to run:
# ./unload_mod.sh usbcore
#
# If you want to reload some of the modules you can modify this script to
# invoke the funcion reload_modules at the end.
if [ $# -ne 1 ]; then
echo "Uso: $0 [ modulo | --dvb ]"
exit 1
fi
WRITE=$(mktemp)
chmod +x "$WRITE"
cat > "$WRITE" <<EOC
#!/bin/sh
FILE=\$1
shift
echo "\$*" > \$FILE
EOC
reload_modules(){
# This function tries to make udev to reload some of the modules
find /sys/devices/ -name uevent -exec "$WRITE" {} change \;
}
# Add your own options below:
case "$1" in
--dvb)
$0 usbcore
$0 usb_common
$0 dvb_core
$0 ir_core
$0 i2c_core
read -p "¡Listo! [Enter] para recargar los módulos, Ctrl-C para terminar"
reload_modules
exit 0
;;
esac
dependencies(){
# FIXME DEAD CODE
lsmod | grep -E '(,| )'"$1"'(,|$)' | tr -s ' ' | cut -d ' ' -f1
}
dependents(){
# Dependents of the $1 module
lsmod | grep -E "^$1 " | tr -s ' ' | cut -d' ' -f4 | tr ',' ' '
}
rrdeps(){
# Reverse recursive dependencies
# loads the dependency tree in $DEPENDENCIES
local module="$1"
for dep in $(dependents "$module"); do
rrdeps "$dep"
done
DEPENDENCIES="$DEPENDENCIES $module"
}
MODULE=$1
rrdeps "$MODULE"
echo "### Descargando: $DEPENDENCIES ###"
for dep in $DEPENDENCIES; do
rmmod $dep
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment