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

前端 未结 9 1589
谎友^
谎友^ 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 15:06

    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.

    0 讨论(0)
  • 2021-02-01 15:09
     !/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
    
    0 讨论(0)
  • 2021-02-01 15:10

    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

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