Skip to content

Instantly share code, notes, and snippets.

@liquuid
Created July 17, 2020 02:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liquuid/09ac38903eabf709194bf769384dc0e9 to your computer and use it in GitHub Desktop.
Save liquuid/09ac38903eabf709194bf769384dc0e9 to your computer and use it in GitHub Desktop.
helper script to detect used linux modules
#!/bin/sh
#
# Find all modules and drivers for a given class device.
#
if [ $# != "1" ] ; then
echo
echo "Script to display the drivers and modules for a specified sysfs
class device"
echo "usage: $0 <CLASS_NAME>"
echo
echo "example usage:"
echo "
$0 sda"
echo "Will show all drivers and modules for the sda block device."
echo
exit 1
fi
DEV=$1
if test -e "$1"; then
DEVPATH=$1
else
# find sysfs device directory for device
DEVPATH=$(find /sys/class -name "$1" | head -1)
test -z "$DEVPATH" && DEVPATH=$(find /sys/block -name "$1" | head -1)
test -z "$DEVPATH" && DEVPATH=$(find /sys/bus -name "$1" | head -1)
if ! test -e "$DEVPATH"; then
echo "no device found"
exit 1
fi
fi
echo "looking at sysfs device: $DEVPATH"
if test -L "$DEVPATH"; then
# resolve class device link to device directory
DEVPATH=$(readlink -f $DEVPATH)
echo "resolve link to: $DEVPATH"
fi
if test -d "$DEVPATH"; then
# resolve old-style "device" link to the parent device
PARENT="$DEVPATH";
while test "$PARENT" != "/"; do
if test -L "$PARENT/device"; then
DEVPATH=$(readlink -f $PARENT/device)
echo "follow 'device' link to parent: $DEVPATH"
break
fi
PARENT=$(dirname $PARENT)
done
fi
while test "$DEVPATH" != "/"; do
DRIVERPATH=
DRIVER=
MODULEPATH=
MODULE=
if test -e $DEVPATH/driver; then
DRIVERPATH=$(readlink -f $DEVPATH/driver)
DRIVER=$(basename $DRIVERPATH)
echo -n "found driver: $DRIVER"
if test -e $DRIVERPATH/module; then
MODULEPATH=$(readlink -f $DRIVERPATH/module)
MODULE=$(basename $MODULEPATH)
echo -n " from module: $MODULE"
fi
echo
fi
DEVPATH=$(dirname $DEVPATH)
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment