57

I am just curious is there any way to determine if a particular module is loaded/installed.

$ lsmod lists all modules (device driver loaded).

Is there any way to check or a command that returns true/false boolean output if a module name is polled. For eg. if keyboard.o exists return true else false. I need this tip to complete my driver auto refresh program.

PS: tried modinfo. I am using busybox client in my test DUT so can you give some alternatives other than modinfo?

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Dennis Ninj
  • 829
  • 2
  • 8
  • 15
  • 1
    The question is a bit ambiguous. Are you trying to check if the driver is loaded into memory or installed on the system? modinfo would help with the latter but not the former. – presto8 Jul 16 '13 at 22:31

10 Answers10

44

The modinfo module method does not work well for me. I prefer this method that is similar to the alternative method proposed:

#!/bin/sh

MODULE="$1"

if lsmod | grep -wq "$MODULE"; then
  echo "$MODULE is loaded!"
  exit 0
else
  echo "$MODULE is not loaded!"
  exit 1
fi
hansfn
  • 530
  • 4
  • 18
basfest
  • 581
  • 5
  • 7
28

not sure if modinfo modname and checking $? will work for you, just a suggestion.

/tmp$ sudo modinfo e1000
/tmp$ echo $?
0
/tmp$ sudo modinfo keyboard
ERROR: modinfo: could not find module keyboard
/tmp$ echo $?
1

alternatively you also grep /proc/modules

Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
johnshen64
  • 3,734
  • 1
  • 21
  • 17
  • 17
    Just a note: the `modinfo` approach doesn't seem to actually work for the _loaded_ check (as in the question title). It shows info regardless of whether the module is loaded. – BeeOnRope Nov 14 '16 at 18:26
  • 2
    This is not a good solution. modinfo shows information of a kernel module installed on the rootfs, but it doesn't check if the module is loaded into the kernel or not. So this solution should not be used to solve the issue. – aicastell Aug 27 '20 at 08:33
  • 3
    Only `lsmod` and `/proc/modules` show what modules are **loaded**. – CMCDragonkai Sep 09 '20 at 12:56
18

The --first-time flag causes modprobe to fail if the module is already loaded. That in conjunction with the --dry-run (or the shorthand -n) flag makes a nice test:

modprobe -n --first-time $MODULE && echo "Not loaded" || echo "Loaded"

Edit 1: As @Nobody pointed out this also prints Loaded if the module does not exist. We can fix this by combining it with modinfo:

modinfo $MODULE >/dev/null 2>/dev/null &&
! modprobe -n --first-time $MODULE 2>/dev/null &&
echo "Loaded" || echo "Not loaded"

Edit 2: On some systems modprobe lives in /usr/sbin, which is not in the $PATH unless you are root. In that case you have to substitute modprobe for /usr/sbin/modprobe in the above.

Julia Path
  • 2,356
  • 15
  • 21
  • 3
    Problem: Needs root and when you enter bullshit for $MODULE it claims that $MODULE is loaded. – Nobody Nov 15 '16 at 16:05
  • @Nobody It does not require root on my system. – Julia Path Dec 03 '17 at 13:13
  • 2
    Oh, sorry, I was only half-right (or maybe quarter-right. Shame on you comment-up-voters). On Debian, modprobe is not in $PATH for normal users, so just copy&pasting your command only works as root. But when calling modprobe with its full path, it's executable for normal users and only the actual inserting operation fails, so your solution works in principle. I still think querying /proc/modules is more elegant, but that's a matter of taste. Had I downvoted your answer, I would have removed the vote now (but I didn't). – Nobody Dec 03 '17 at 13:30
9

I wrote this:

MODULE=snd_aloop # for example
test -n "$(grep -e "^$MODULE " /proc/modules)" && echo "Loaded" || echo "Not loaded"

It checks in /proc/modules. If the module is mentioned there, it's assumed to be loaded, otherwise not.

The others seemed too long to me (the other short one requires root, this does not). Of course it's just written out what was already mentioned as "alternatives".

Caution: modprobe accepts some variants of module names other than the primary listed in /proc/modules. For example loading snd-aloop works, but the module is named snd_aloop and is listed as such in /proc/modules and when using rmmod that's also the only name that will work.

Nobody
  • 192
  • 3
  • 12
  • 3
    You may want to match for `"^$MODULE\>"` instead to make sure you aren't matching just part of the module name. – Julia Path Dec 03 '17 at 16:23
9

My short way to find if a given module is actually loaded:

cat /proc/modules | grep -c nfnetlink

which outputs

2

That 2 (TWO) means the module is LOADED. The actual output without -c shows all loaded modules with MODULENAME - -c counts the lines that contain MODULENAME. So if you have 0 (ZERO) lines as output then the module is not loaded

1000Gbps
  • 1,455
  • 1
  • 29
  • 34
  • 1
    You can use it with if. `if cat /proc/modules | grep -c nfnetlink; then echo "module loaded" fi` – Abc Xyz Oct 10 '20 at 13:17
7

The better idea is to create a bash function:

#!/bin/sh
function moduleExist(){
  MODULE="$1"
  if lsmod | grep "$MODULE" &> /dev/null ; then
    return 0
  else
    return 1
  fi
}


if moduleExist "module name"; then
  #do somthing
fi
Mostafa Barmshory
  • 1,849
  • 24
  • 39
2
 !/bin/sh
 # Module
 MODULE="scsi_dh_rdac"

 #Variables check if module loaded or not
 MODEXIST=/sbin/lsmod | grep "$MODULE"

 if [ -z "$MODEXIST" ]; then
       /sbin/modprobe "$MODULE" >/dev/null 2>&1
 fi
Santosh Joshi
  • 3,290
  • 5
  • 36
  • 49
ronald
  • 21
  • 1
  • Although your answer looks good, you should include a little explanation... plain code may be good, but code *and* explanation is better – Barranka May 15 '14 at 21:05
0
module list 

Returns:

Currently Loaded Modulefiles:
  1) /coverm/0.3.0        2) /parallel/20180222
MeggyClay
  • 21
  • 2
0
grep -wEq "^${module%.o}" /proc/modules

returns true (e.g. can be used in an if) whether you ask for keyboard or keyboard.o

usretc
  • 723
  • 4
  • 9
0

grep -q $pattern against lsmod or /proc/modules, which are available on most systems and a standard "source of truth"

lsmod | grep -q $pattern
$ lsmod | grep -q msr
$ echo $?
0
$ lsmod | grep -q duediligencemuch
$ echo $?
1