wiki:linux/kernel/modules

Linux Kernel modules

If your kernel is built with loadable module support (CONFIG_MODULES=y) you can load and unload pre-built kernel modules to add, remove, and alter functionality of a running kernel.

The userspace support for doing this comes from a set of commands typically packaged in a 'kmod' package name consisting of:

  • lsmod - show the status of modules currently loaded in the kernel (uses info from /proc/modules)
  • insmod - manually insert a module into the kernel
  • rmmod - manually remove a module from the kernel
  • modinfo - show info about a module
  • depmod - create a modules.dep and map file describing module dependencies
  • modprobe - add or remove a module from the kernel (handling dependencies and finding modules in the /lib/modules/$(uname -r) directory) taking into account module parameters from optional configuration files in /etc/modprobe.d

Examples:

  • load a module:
    insmod ./mymod.ko # by path
    modprobe mymod # from /lib/modules using dependencies
    
  • remove a module:
    rmmod mymod
    
  • show loaded modules:
    lsmod
    
  • show kernel params for a given module:
    $ modinfo g_ether | grep parm
    parm:           idVendor:USB Vendor ID (ushort)
    parm:           idProduct:USB Product ID (ushort)
    parm:           bcdDevice:USB Device version (BCD) (ushort)
    parm:           iSerialNumber:SerialNumber string (charp)
    parm:           iManufacturer:USB Manufacturer string (charp)
    parm:           iProduct:USB Product string (charp)
    parm:           qmult:queue length multiplier at high/super speed (uint)
    parm:           dev_addr:Device Ethernet Address (charp)
    parm:           host_addr:Host Ethernet Address (charp)
    parm:           use_eem:use CDC EEM mode (bool)
    
  • show vermagic for a given module:
    $ modinfo g_ether | grep vermagic
    vermagic:       6.12.0-g530fc2d305a3 SMP mod_unload aarch64
    

Linux Kernel vermagic

Vermagic is a magic string present in the Linux Kernel and added into the .modinfo section of the Linux Kernel Modules.

This is used to verify whether the kernel module was compiled for the particular kernel version or not.

This string is generated by the kernel configuration via the following in include/linux/vermagic.h:

#define VERMAGIC_STRING                                                 \
        UTS_RELEASE " "                                                 \
        MODULE_VERMAGIC_SMP MODULE_VERMAGIC_PREEMPT                     \
        MODULE_VERMAGIC_MODULE_UNLOAD MODULE_VERMAGIC_MODVERSIONS       \
        MODULE_ARCH_VERMAGIC                                            \
        MODULE_RANDSTRUCT

A typical VERMAGIC_STRING for an aarch64 kernel would look something like:6.12.0-g530fc2d305a3 SMP mod_unload aarch64

While you can easily find the kernel version and arch of a running kenrel, perhaps the best way to see this exact string would be to use modinfo command on an existing module:

# modinfo $(find /lib/modules/$(uname -r)/ -name "*.ko" | head -n1) | grep vermagic
vermagic:       6.12.0-g530fc2d305a3 SMP mod_unload aarch64

The vermagic string is checked upon module loading and if the string in the module does not match the string in the kernel the module loading will fail by design:

# insmod mymod.ko
mymod: version magic '6.12.0-00017-g530fc2d305a3 SMP mod_unload aarch64' should be '6.12.0-g530fc2d305a3 SMP mod_unload aarch64'
insmod: ERROR: could not insert module ./mymod.ko: Invalid module format

There is no easy way to disable the kernel module loading vermagic string unless you disable a lot of security measures and the vermagic check is there for a very important reason: to disallow loading of modules that are not compatible with the specific version of kernel running.

There are times however as a developer building an out-of-tree kernel module that you may wish to avoid this check. For example, if your running kernel version varies only slightly in ways you know are compatible. In such a case where you assume full responsibility for breaking something you could use the setvermagic.sh script to modify the vermagic string:

$ wget https://dev.gateworks.com/sources/setvermagic.sh
$ chmod +x setvermagic.sh
$ modinfo mymod.ko | grep vermagic
vermagic:       6.6.8-00035-gc6ef8b5e47a0 SMP mod_unload aarch64
Show quoted text
$ ./setvermagic.sh mymod.ko "6.6.8-gc6ef8b5e47a0 SMP mod_unload aarch64"
Current vermagic: 6.6.8-00035-gc6ef8b5e47a0 SMP mod_unload aarch64
New vermagic    : 6.6.8-gc6ef8b5e47a0 SMP mod_unload aarch64
Modified module saved as: mymod-modified.ko
You can load it with: insmod mymod-modified.ko
Last modified 10 days ago Last modified on 04/25/2025 03:35:47 PM
Note: See TracWiki for help on using the wiki.