Skip to content

Instantly share code, notes, and snippets.

@robsears
Created September 22, 2013 06:33
Show Gist options
  • Save robsears/6657283 to your computer and use it in GitHub Desktop.
Save robsears/6657283 to your computer and use it in GitHub Desktop.
A simple BASH script to get a list of kernel modules and their description and dependencies.
#!/bin/bash
# This code should not be run as `sh find_all_modules.sh`, as some distros
# link sh to dash, which is POSIX-compliant and doesn't do arrays.
# Adapted from Linux Kernel in a Nutshell: http://www.kroah.com/lkn/
# By Rob Sears, Sept 22, 2013.
# Get a newline-separated list of modules in use:
MODLIST=`for i in $(find /sys/ -name modalias -exec cat {} \; 2>/dev/null); do /sbin/modprobe --config /dev/null --show-depends $i 2> /dev/null;done | rev | cut -f 1 -d '/' | rev | sort -u`
# Convert the newline-separated list to an array:
IFS=$'\n' read -d '' -r -a MODULES <<< "${MODLIST}"
# Iterate over the array and print out details:
for i in "${MODULES[@]}"
do
# Get the module name (turn "module.ko" to "module"):
MOD=`echo "${i}" | sed -e "s/\([0-9a-zA-Z]\)\.ko/\1/"`
# Get the module description:
DESC=`modinfo $MOD | grep description | sed -r "s/[\ ]+{2,}/\ /g; " | sed -e "s/^description\: \([0-9a-zA-Z]\)/\1/"`
# Get the module's dependancies:
DEPS=`modinfo $MOD | grep depends | sed -r "s/[\ ]+{2,}/\ /g; " | sed -e "s/^depends\: \([0-9a-zA-Z]\)/\1/"`
# Determine whether to print a description:
if [ -z "${DESC}" ]; then
DESCRIPTION=""
else
DESCRIPTION="${DESC} "
fi
# Determine whether to print a dependancy list:
if [[ "${DEPS}" =~ "depends:" ]]; then
DEPENDANCIES=""
else
DEPENDANCIES="Depends on ${DEPS}"
fi
# Generate the notes to display:
NOTES="${DESCRIPTION}${DEPENDANCIES}"
if [ -z "${NOTES}" ]; then
MESSAGE="$i: No information"
else
MESSAGE="$i (${NOTES})"
fi
# Print the module name, description and dependancies:
echo "${MESSAGE}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment