≡ Menu

linux kernel module

How to display or show information about a Linux Kernel module or drivers

I was just browsing our forum and come across the following question:

How do I find out detailed information about a Linux Kernel module or device drivers?

You need to use modinfo command to display or show information about a Linux Kernel loaded modules. Use lsmod command to obtain list of loaded modules.

modinfo extracts information from the Linux Kernel modules given on the command line. This is not just useful to find out about information about loaded modules but it can be used to identify modules inserted by crackers/hackers (try less /proc/modules). Also following is a good way to verify output reported by ps ax :
# ps ax | wc -l
# ls -d /proc/* | grep [0-9]|wc -l

Usually rootkit will install their own ps command, which hides kernel modules. If second output is larger than the first (ps ax) command output take a closer look at your server.

Back to our main question, the modinfo command outputs following information for each module:
=> Module Author
=> Module description
=> License
=> Parameter and filename

Task: Display list of modules

$ lsmod
$ less /proc/modules

Task: Display information about ide-cd module

$ /sbin/modinfo ide-cd

filename:       /lib/modules/2.6.8-2-686/kernel/drivers/ide/ide-cd.ko
description:    ATAPI CD-ROM Driver
license:        GPL
vermagic:       2.6.8-2-686 preempt 686 gcc-3.3
depends:        ide-core,cdrom

Task: Display information about rt61 module

$ /sbin/modinfo rt61

filename:       /lib/modules/2.6.8-2-686/kernel/net/wireless/rt61.ko
author:         Paul Lin 
description:    RT61 Wireless Lan Linux Driver
vermagic:       2.6.8-2-686 preempt 686 gcc-3.3
alias:          pci:v00001814d00000301sv*sd*bc*sc*i*
alias:          pci:v00001814d00000302sv*sd*bc*sc*i*
alias:          pci:v00001814d00000401sv*sd*bc*sc*i*

You can load modules automatically during Linux system boot sequence by adding a module name to /etc/modules.conf (Red Hat and friends) or /etc/modules (Debian / Ubuntu Linux) file.

Howto: Build Linux Kernel Module Against Installed Kernel w/o Full Kernel Source Tree

Recently I received a question via email:

How do I build Linux kernel module against installed or running Linux kernel? Do I need to install new kernel source tree from kernel.org?

To be frank you do not need a new full source tree in order to just compile or build module against the running kernel i.e an exploded source tree is not required to build kernel driver or module. The instruction outlined below will benefit immensely to a developer and power user.

Linux Kernel headers

This is essential because if you just want to compile and install driver for new hardware such as Wireless card or SCSI device etc. With following method, you will save the time, as you are not going to compile entire Linux kernel.

Please note that to work with this hack you just need the Linux kernel headers and not the full kernel source tree. Install the linux-kernel-headers package which provides headers from the Linux kernel. These headers are used by the installed headers for GNU glibc and other system libraries as well as compiling modules. Use following command to install kernel headers:
# apt-get install kernel-headers-2.6.xx.xx.xx

Replace xx.xx with your actual running kernel version (e.g. 2.6.8.-2) and architecture name (e.g. 686/em64t/amd64). Use uname -r command to get actual kernel version name. Please note that above command will only install kernel headers and not the entire kernel source-code tree.

A more generic (recommend) and accurate way is as follows:
# apt-get install kernel-headers-$(uname -r)

All you need to do is change Makefile to use current kernel build directory. You can obtain this directory name by typing following command:
$ ls -d /lib/modules/$(uname -r)/build
Sample output:


Let, say you have .c source code file called hello.c. Now create a Makefile as follows in the directory containing hello.c program / file:
$ vi Makefile
Append following text:

obj-m := hello.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
        $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules

Save and close the file. Type the following command to build the hello.ko module:
$ make

To load Linux kernel module type the command:
# modprobe hello

A complete example

Create test directory (you can download following Makefile and .c file here):
$ mkdir ~/test
$ cd ~/test

Create hello.c kernel module file:

#include <linux/module.h>	
#include <linux/kernel.h>	
int init_module(void)
	printk(KERN_INFO "init_module() called\n");
	return 0;
void cleanup_module(void)
	printk(KERN_INFO "cleanup_module() called\n");

Create a Makefile:

obj-m += hello.o
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clea

To build kernel module enter:
$ make
Sample output:

make -C /lib/modules/2.6.27-7-generic/build M=/tmp/test2 modules
make[1]: Entering directory `/usr/src/linux-headers-2.6.27-7-generic'
  CC [M]  /tmp/test2/hello.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /tmp/test2/hello.mod.o
  LD [M]  /tmp/test2/hello.ko
make[1]: Leaving directory `/usr/src/linux-headers-2.6.27-7-generic'

Run ls command to see newly build kernel module:
$ ls
Sample output:

hello.c  hello.ko  hello.mod.c	hello.mod.o  hello.o  Makefile	Module.markers	modules.order  Module.symvers

hello.ko is kernel module file. To see information about module, enter:
$ modinfo hello.ko
Sample output:

filename:       hello.ko
srcversion:     4F856ABA1F3290D5F81D961
vermagic:       2.6.27-7-generic SMP mod_unload modversions 586 

To load kernel module, enter:
$ sudo insmod hello.ko
$ sudo modprobe hello
To list installed Linux kernel module, enter:
$ lsmod
$ lsmod | grep hello

To remove hello Linux kernel module, enter:
$ rmmod hello
This module just logs message to a log file called /var/log/messages (/var/log/syslog), enter:
$ tail -f /var/log/messages
Sample output:

Nov  5 00:36:36 vivek-desktop kernel: [52488.923000] init_module() called
Nov  5 00:36:50 vivek-desktop kernel: [52503.065252] cleanup_module() called

Download above examples in zip format

See a detailed and completeexample with C source code about Compiling Linux kernel module for more information.