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

前端 未结 9 1588
谎友^
谎友^ 2021-02-01 14:28

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

相关标签:
9条回答
  • 2021-02-01 14:47

    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
    
    0 讨论(0)
  • 2021-02-01 14:47
    module list 
    

    Returns:

    Currently Loaded Modulefiles:
      1) /coverm/0.3.0        2) /parallel/20180222
    
    0 讨论(0)
  • 2021-02-01 14:50

    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
    
    0 讨论(0)
  • 2021-02-01 14:52
    grep -wEq "^${module%.o}" /proc/modules
    

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

    0 讨论(0)
  • 2021-02-01 14:56

    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.

    0 讨论(0)
  • 2021-02-01 15:01

    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

    0 讨论(0)
提交回复
热议问题