Thursday, June 19, 2014

Kernel Module

What Is A Kernel Module?

  •  Modules are pieces of code that can be loaded and unloaded into the kernel upon demand.
  •  They extend the functionality of the kernel without the need to reboot the system.
  •  For example, one type of module is the device driver, which allows the kernel to access hardware connected to the system.
  •  Without modules, we would have to build monolithic kernels and add new functionality directly into the kernel image. 
  • Besides having larger kernels, this has the disadvantage of requiring us to rebuild and reboot the kernel every time we want new functionality.



How Do Modules Get Into The Kernel?

  • We can see what modules are already loaded into the kernel by running lsmod, which gets its information by reading the file /proc/modules.
  • How do these modules find their way into the kernel? When the kernel needs a feature that is not resident in the kernel, the kernel module daemon kmod execs modprobe to load the module in. modprobe is passed a string in one of two forms:
  • A module name like softdog or ppp.
  • A more generic identifier like char-major-10-30.
  • If modprobe is handed a generic identifier, it first looks for that string in the file /etc/modprobe.conf. If it finds an alias line like:

alias char-major-10-30 softdog
 
it knows that the generic identifier refers to the module softdog.ko.

  • Next, modprobe looks through the file /lib/modules/version/modules.dep, to see if other modules must be loaded before the requested module may be loaded. 
  • This file is created by depmod -a and contains module dependencies.
  •  For example, msdos.ko requires the fat.ko module to be already loaded into the kernel. The requested module has a dependency on another module if the other module defines symbols (variables or functions) that the requested module uses.
  • Lastly, modprobe uses insmod to first load any prerequisite modules into the kernel, and then the requested module.
  •  modprobe directs insmod to /lib/modules/version/, the standard directory for modules.
  •  insmod is intended to be fairly dumb about the location of modules, whereas modprobe is aware of the default location of modules, knows how to figure out the dependencies and load the modules in the right order. So for example, if you wanted to load the msdos module, you'd have to either run:

insmod /lib/modules/2.6.11/kernel/fs/fat/fat.ko
insmod /lib/modules/2.6.11/kernel/fs/msdos/msdos.ko
 
or:

modprobe msdos
 
  • What we've seen here is: insmod requires us to pass it the full pathname and to insert the modules in the right order, while modprobe just takes the name, without any extension, and figures out all it needs to know by parsing /lib/modules/version/modules.dep.
  • Linux distros provide modprobe, insmod and depmod as a package called module-init-tools.





Some Important points-



1.Modversioning

  • A module compiled for one kernel won't load if we boot a different kernel unless we enable CONFIG_MODVERSIONS in the kernel.
2. Using X
  • Modules can't print to the screen like printf() can, but they can log information and warnings, which ends up being printed on our screen, but only on a console. If we insmod a module from an xterm, the information and warnings will be logged, but only to your log files.We  won't see it unless we look through our log files. To have immediate access to this information, we must do all our work from the console.










No comments:

Post a Comment