How to determine if a specific module is loaded in linux kernel

≯℡__Kan透↙ 提交于 2019-12-03 06:34:26

问题


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 ?


回答1:


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




回答2:


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 "$MODULE" &> /dev/null ; then
  echo "$MODULE is loaded!"
  exit 0
else
  echo "$MODULE is not loaded!"
  exit 1
fi



回答3:


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.




回答4:


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.




回答5:


The better idea is to create a bash function:

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

checkModule
if $?; then
  #do somthing
fi



回答6:


 !/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



回答7:


module list 

Returns:

Currently Loaded Modulefiles:
  1) /coverm/0.3.0        2) /parallel/20180222


来源:https://stackoverflow.com/questions/9845877/how-to-determine-if-a-specific-module-is-loaded-in-linux-kernel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!