5

I'm wondering if I can use the directory listing of /sys/module instead of lsmod to get a list of currently loaded modules.

Is that the list of loaded modules only? Or maybe that combined with /sys/module/*/initstate?

1 Answers1

11

Each loaded module has an entry in /sys/module. But there are also kernel components with an entry in /sys/module that are not loaded as modules. Each kernel component¹ that can be built as a module has an entry in /sys/module, whether it is compiled and loaded as a module or compiled as part of the main kernel image.

lsmod gets the list of loaded modules from /proc/modules.

I think that only loaded modules have an initstate file in their /sys/module directory, so you can use that too.

¹ That's each component of the loaded kernel. The kernel doesn't know or care what modules you may have in files on your hard disk. The kernel doesn't care what modules were built at the same time of the kernel image, either; it may show that via /proc/config but it doesn't use that information for anything.

  • 1
    A fun side effect is that you can't insmod modules that are named exactly like built-in modules and you become totally clueless why: https://unix.stackexchange.com/questions/364956/how-can-insmod-fail-with-kernel-module-is-already-loaded-even-is-lsmod-does-not – Ciro Santilli OurBigBook.com Apr 16 '18 at 14:21
  • @Gilles I was wondering if you could clarify "Each kernel component that can be built as a module has an entry in /sys/module". On my system: lsmod | wc -l => 116. ls /sys/module | wc -l = >201. grep '=m' /boot/config-$(uname -r) => 4934. – el_tigro Feb 25 '19 at 17:30
  • @catanman That's the loaded modules. Usually you would only load a small number of modules compared to the number that were built, and you may load modules that were built separately. – Gilles 'SO- stop being evil' Feb 25 '19 at 17:58
  • @Gilles Thanks. Just to make sure I get it: If I do grep '=y' /boot/config-$(uname -r) | wc -l => 2389. Out of all 2389 components statically compiled into the kernel, only 201-116=85 of them could have been compiled/loaded as modules. 2389-85=2304 could not be loaded as modules (therefore they can only be set to y or n in the config file). Does this make sense? – el_tigro Feb 25 '19 at 18:15
  • 2
    No. You have 2389 options set to y, not all of which are components (some are yes/no settings but not a distinct component). You have 4934 components available as modules. You have 116 loaded modules, and 201-116=85 components that could be loaded as modules, but are built in. – Gilles 'SO- stop being evil' Feb 25 '19 at 18:41
  • Got it! Couple more questions :). 1) For options set to y how would I go about determining which are components vs simple yes/no settings. 2) For each options set to m, there is one corresponding ".ko" file that is built (stored at /lib /modules/$(uname -r) on my system) 3) Even if a component is set to n at build time, it is possible to load that component as a module if you obtain the ".ko" file. – el_tigro Feb 25 '19 at 19:07
  • 1
    @catanman 1) Look in the Kconfig files in the kernel source (either directly or via a kernel configuration tool). 2) Yes. 3) Yes (but you'd need to have a compatible .ko file, which is hard to ensure unless the file was built from the same kernel version with the same options). – Gilles 'SO- stop being evil' Feb 25 '19 at 19:31
  • It untrue that all modules show up in /sys/modules. On Ubuntu 20.04, ext4 is builtin and there is no corresponding /sys/module/ext4. – Melab Sep 17 '20 at 03:36
  • @Melab Is this a recent change? I'm reading this on Ubuntu 18.04 and it does have /sys/module/ext4. – Gilles 'SO- stop being evil' Sep 17 '20 at 08:27