Adding modules to linux

Adding Module To The Kernel

We will go through the process or steps required to add a kernel module while the kernel is running. We will also discuss the few useful Linux utilities available. These utilities can be employed to get info on the module already present in the Linux kernel, add a new module, and get the info on the provided module.

Description

Let’s start with the kernel module; the kernel module is the logical separate functionality which can be added at a later stage in the running kernel. This extends the Linux kernel features/functions.

These modules are files with the extension as “.ko” , which means kernel objects. If we come across any file with “.ko” as an extension, it clearly indicates that it is a kernel module.

A few example names of modules are : i2c-modules.ko , hello-world.ko, etc.

The process of adding a kernel module to the kernel is also known as the loading of a kernel module. The kernel module can only be loaded if it is compatible with the kernel source tree. This means the kernel module is not platform-independent. These can be loaded only on the kernel with the same source tree and build configuration.

Adding/Loading Of Kernel Module

Linux provides a utility known as “insmod”. This is the utility which can be used to load the kernel module at the running kernel.

To load the kernel module, just execute the command as insmod followed by the module file name.

Let us take an example of hello-world.ko , a module which is the classic example and just prints the hello world message. To load the hello-world kernel module, below is the command which can be used :

A sample snapshot is attached below; highlighted file is the kernel module:

With the above command’s successful execution, our module is added to the Linux kernel, and we will see the print message from the hello-world kernel module in the printk buffer. To check the message from the hello-world, use the dmesg command. If the command results in no error message, then it can be considered module addition is successful. As we are extending the kernel functions, so it requires super user privileges to execute the “insmod”. Example output of the dmesg command is as below:

Читайте также:  Linux on memory card

sushilrathore- 2 :~ / hello-world$ sudo insmod hello-world.ko

sushilrathore- 2 :~ / hello-world$ dmesg

[ 10500712.434672 ] Hello world

cienauser @ haxv-srathore- 2 :~ / hello-world$

Confirming The Module Is Loaded

To confirm if the module is loaded and present among the other modules in the Linux kernel. The “lsmod” command can be used to list all the kernel modules. Issue the command at the Linux shell, and we will see the complete list of the loaded modules in the Linux kernel. Issuing the lsmod on my system, I see the below output at the console:

sushilrathore- 2 :~ / hello-world$ lsmod

nf_conntrack_ipv6 20480 0

nf_defrag_ipv6 20480 1 nf_conntrack_ipv6

ip6_tables 28672 1 ip6table_filter

nf_conntrack_ipv4 16384 0

nf_defrag_ipv4 16384 1 nf_conntrack_ipv4

nf_conntrack 135168 3 xt_conntrack,nf_conntrack_ipv6,nf_conntrack_ipv4

ipmi_msghandler 53248 1 ipmi_devintf

As we can see in the logs above, there are many modules listed, and our module hello-world is also on the list; and I have highlighted it for easy spotting.

So we confirmed that our module is loaded in the kernel successfully.

Removing/Unloading The Kernel Module

To remove or unload the kernel module, we can use the Linux command “rmmod”. This is the command which is used to remove the loaded kernel module and listed by lsmod. This operation also requires superuser privileges. Going back to our hello-world example, if we wish to remove the hello-world kernel module which we have loaded previously. We need to issue the below command:

sushilrathore- 2 :~ / hello-world$ sudo rmmod hello_world

sushilrathore- 2 :~ / hello-world$

After the command execution, if nothing is seen on the console, i.e. there is no error message. This means the unloading/removal of the kernel module is successful.

Confirming The Removal/Unloading Of The Module

Again to confirm if the module is unloaded successfully, we can use the lsmod command. After the removal of the kernel module, we should not see the module present in the list of modules provided by “lsmod”.

Following is the example output from my system:

sushilrathore- 2 :~ / hello-world$ lsmod

nf_conntrack_ipv6 20480 0

nf_defrag_ipv6 20480 1 nf_conntrack_ipv6

ip6_tables 28672 1 ip6table_filter

nf_conntrack_ipv4 16384 0

nf_defrag_ipv4 16384 1 nf_conntrack_ipv4

nf_conntrack 135168 3 xt_conntrack,nf_conntrack_ipv6,nf_conntrack_ipv4

ipmi_msghandler 53248 1 ipmi_devintf

vmw_vsock_vmci_transport 32768 1

vsock 36864 2 vmw_vsock_vmci_transport

vmw_vmci 69632 2 vmw_balloon,vmw_vsock_vmci_transport

In the above list, if we check, we will not find the hello-world kernel module. This double confirm that the module is removed from the system.

There is one more very important utility offered, which can be used to get the info of the kernel module file. “modinfo” is the command provided to get the details of the already present kernel module.

Читайте также:  Права доступа 174 linux

Executing the “modinfo” with the hello-world kernel module we get the below output:

sushilrathore- 2 :~ / hello-world$ modinfo hello-world.ko

filename: / home / sushilrathore / hello-world / hello-world.ko

vermagic: 4.15.0- 163 -generic SMP mod_unload modversions

cienauser @ haxv-srathore- 2 :~ / hello-world$

The above information is the details of the kernel module. The important information to note is it provides you with the srcversion and vermagic. This information can be used to identify the kernel with which this module is compatible and can be loaded into. If we try to add the kernel module compiled for other Linux sources or kernel, then we will get the incompatible error from the insmod command.

Below is the sample code and Makefile code we have used in our discussion above:

hello-world.c

MODULE_LICENSE ( «GPL» ) ;
MODULE_DESCRIPTION ( «Hello world!» ) ;

static int __inithello_init ( void )
{
printk ( KERN_INFO «Hello world \n » ) ;
return 0 ;
}

static void __exit hello_exit ( void )
{
printk ( KERN_INFO «Goodbye world \n » ) ;
}

module_init ( hello_init ) ;
module_exit ( hello_exit ) ;

Источник

What are loadable modules (drivers) ?

Essentially, modules are to Linux as drivers are to Windows.

Unlike Windows drivers, which are usually supplied by the hardware manufacturer, most modules come supplied with each Linux distribution.

  • Recompilation of the kernel with new capabilities permanently «compiled-in» and subsequent booting to the new kernel;
  • Building of a kernel with loadable modules for occasional use. In order to use these modules’ features, the modules must be added to the kernel- this can be done either automatically or manually. When a module is no longer wanted, it may be removed from the custom kernel manually or it can disappear automatically.

During kernel configuration and building, features specified with a ‘y’ will have the necessary software to support those features as part of the kernel (the features will be «compiled into» the kernel and will consume memory permanently). If a feature is specified with an ‘m’ then the feature will be a loadable module that may be loaded and unloaded at will (and will use memory only if the module is loaded). If an ‘n’ is specified, then the feature will not be enabled in the kernel at all and will not be available.

Using loadable modules

The file /etc/modules configures which loadable modules are automatically loaded. Here is a sample:

# /etc/modules: kernel modules to load at boot time. # # This file contains the names of kernel modules that should be loaded # at boot time, one per line. Lines beginning with "#" are ignored. loop lp fuse r8169

Changing /etc/modules: Let’s say your eepro100 Ethernet device breaks and you buy a new Ethernet card that uses the tulip driver. In this case, the relevant line in /etc/modules file should be changed to:

Читайте также:  Kali linux portable usb

In most cases, when you reboot with your new Ethernet card, Ubuntu’s configuration manager should automatically notice a new Ethernet card is installed, and change the /etc/modules file for you.

To view a list of loadable kernel modules on a system, as well as their status, run:

Module Size Used by Not tainted i810_audio 26408 2 (autoclean) ac97_codec 13768 0 (autoclean) [i810_audio] soundcore 7108 2 (autoclean) [i810_audio] scanner 10716 0 (unused) mousedev 5688 0 (unused) keybdev 2944 0 (unused) input 6176 0 [mousedev keybdev] hid 11804 0 (unused) usb-uhci 27436 0 (unused) usbcore 81088 1 [scanner hid usb-uhci]

To see a list of the module files:

modprobe --list # for those who don't have the `--list' or `-l' option, use the command below: # find /lib/modules/`uname -r` -type f -name "*.ko"
/lib/modules/2.4.20/kernel/drivers/block/floppy.o /lib/modules/2.4.20/kernel/drivers/block/loop.o /lib/modules/2.4.20/kernel/drivers/char/rtc.o /lib/modules/2.4.20/kernel/drivers/input/input.o /lib/modules/2.4.20/kernel/drivers/input/keybdev.o /lib/modules/2.4.20/kernel/drivers/input/mousedev.o /lib/modules/2.4.20/kernel/drivers/net/dummy.o /lib/modules/2.4.20/kernel/drivers/scsi/atp870u.o /lib/modules/2.4.20/kernel/drivers/scsi/scsi_mod.o /lib/modules/2.4.20/kernel/drivers/scsi/st.o /lib/modules/2.4.20/kernel/drivers/sound/ac97_codec.o /lib/modules/2.4.20/kernel/drivers/sound/i810_audio.o /lib/modules/2.4.20/kernel/drivers/sound/soundcore.o /lib/modules/2.4.20/kernel/drivers/usb/dc2xx.o /lib/modules/2.4.20/kernel/drivers/usb/hid.o /lib/modules/2.4.20/kernel/drivers/usb/scanner.o /lib/modules/2.4.20/kernel/drivers/usb/usb-uhci.o /lib/modules/2.4.20/kernel/drivers/usb/usbcore.o /lib/modules/2.4.20/kernel/fs/fat/fat.o /lib/modules/2.4.20/kernel/fs/msdos/msdos.o /lib/modules/2.4.20/kernel/fs/vfat/vfat.o
/lib/modules/2.6.24-16-server/kernel/drivers/net/r'''8169'''.ko

To load a module, and any modules that it depends on, use modprobe:

To remove a loaded module, use rmmod:

To view information about a module, use modinfo:

filename: /lib/modules/2.4.20/kernel/drivers/scsi/st.o description: "SCSI Tape Driver" author: "Kai Makisara" license: "GPL" parm: buffer_kbs int, description "Default driver buffer size (KB; 32)" parm: write_threshold_kbs int, description "Asynchronous write threshold (KB; 30)" parm: max_buffers int, description "Maximum number of buffer allocated at initialisation (4)" parm: max_sg_segs int, description "Maximum number of scatter/gather segments to use (32)" parm: blocking_open int, description "Block in open if not ready an no O_NONBLOCK (0)"

Blacklisting Modules

For various reasons it may be desirable to stop a module from loading. In this case a module can be blacklisted in /etc/modprobe.d/blacklist

nano -w /etc/modprobe.d/blacklist

should be added to prevent the ‘e100’ module (one of many Ethernet modules) from loading.

Sometimes it is needed to update the drivers cache after editing the blacklist.conf file. To do this, run:

Loadable_Modules (последним исправлял пользователь 82 2014-03-11 01:05:53)

The material on this wiki is available under a free license, see Copyright / License for details
You can contribute to this wiki, see Wiki Guide for details

Источник

Оцените статью
Adblock
detector