{"id":177620,"date":"2022-05-16T04:26:21","date_gmt":"2022-05-16T11:26:21","guid":{"rendered":"https:\/\/linuxhint.com\/?p=177620"},"modified":"2022-05-25T04:03:11","modified_gmt":"2022-05-25T11:03:11","slug":"adding-kernel-module","status":"publish","type":"post","link":"https:\/\/linuxhint.com\/adding-kernel-module\/","title":{"rendered":"Adding Module To The Kernel"},"content":{"rendered":"<div id=\"wpbody\">\nWe 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.<\/p>\n<h2><strong>Description<\/strong><\/h2>\n<p>Let\u2019s 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.<\/p>\n<p>These modules are files with the extension as \u201c.ko\u201d , which means kernel objects. If we come across any file with \u201c.ko\u201d as an extension, it clearly indicates that it is a kernel module.<\/p>\n<p>A few example names of modules are : i2c-modules.ko , hello-world.ko, etc.<\/p>\n<p>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.<\/p>\n<h2><strong>Adding\/Loading Of Kernel Module<\/strong><\/h2>\n<p>Linux provides a utility known as \u201cinsmod\u201d. This is the utility which can be used to load the kernel module at the running kernel.<\/p>\n<p>To load the kernel module, just execute the command as insmod followed by the module file name.<\/p>\n<p>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 :<\/p>\n<p>A sample snapshot is attached below; highlighted file is the kernel module:<\/p>\n<p><img decoding=\"async\" width=\"1185\" height=\"67\" class=\"wp-image-177625\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2022\/05\/word-image-742.png\" srcset=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2022\/05\/word-image-742.png 1185w, https:\/\/linuxhint.com\/wp-content\/uploads\/2022\/05\/word-image-742-300x17.png 300w, https:\/\/linuxhint.com\/wp-content\/uploads\/2022\/05\/word-image-742-1024x58.png 1024w, https:\/\/linuxhint.com\/wp-content\/uploads\/2022\/05\/word-image-742-768x43.png 768w, https:\/\/linuxhint.com\/wp-content\/uploads\/2022\/05\/word-image-742-810x46.png 810w, https:\/\/linuxhint.com\/wp-content\/uploads\/2022\/05\/word-image-742-1140x64.png 1140w\" sizes=\"(max-width: 1185px) 100vw, 1185px\" \/><\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw2\">sudo<\/span> insmod hello-world.ko<\/div><\/div>\n<p>Example snapshot:<\/p>\n<p><img decoding=\"async\" width=\"690\" height=\"54\" class=\"wp-image-177627\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2022\/05\/word-image-743.png\" srcset=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2022\/05\/word-image-743.png 690w, https:\/\/linuxhint.com\/wp-content\/uploads\/2022\/05\/word-image-743-300x23.png 300w\" sizes=\"(max-width: 690px) 100vw, 690px\" \/><\/p>\n<p>With the above command\u2019s 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 \u201cinsmod\u201d. Example output of the dmesg command is as below:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">sushilrathore-<span class=\"nu0\">2<\/span>:~<span class=\"sy0\">\/<\/span>hello-world$ <span class=\"kw2\">sudo<\/span> insmod hello-world.ko<br \/>\n<br \/>\nsushilrathore-<span class=\"nu0\">2<\/span>:~<span class=\"sy0\">\/<\/span>hello-world$ <span class=\"kw2\">dmesg<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#91;<\/span><span class=\"nu0\">10500712.434672<\/span><span class=\"br0\">&#93;<\/span> Hello world<br \/>\n<br \/>\ncienauser<span class=\"sy0\">@<\/span>haxv-srathore-<span class=\"nu0\">2<\/span>:~<span class=\"sy0\">\/<\/span>hello-world$<\/div><\/div>\n<h2><strong>Confirming The Module Is Loaded<\/strong><\/h2>\n<p>To confirm if the module is loaded and present among the other modules in the Linux kernel. The \u201clsmod\u201d 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:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;height:100%;\"><div class=\"bash codecolorer\">sushilrathore-<span class=\"nu0\">2<\/span>:~<span class=\"sy0\">\/<\/span>hello-world$ <span class=\"kw2\">lsmod<\/span><br \/>\n<br \/>\nModule Size Used by<br \/>\n<br \/>\nhello_world <span class=\"nu0\">16384<\/span> <span class=\"nu0\">0<\/span><br \/>\n<br \/>\nnf_conntrack_ipv6 <span class=\"nu0\">20480<\/span> <span class=\"nu0\">0<\/span><br \/>\n<br \/>\nnf_defrag_ipv6 <span class=\"nu0\">20480<\/span> <span class=\"nu0\">1<\/span> nf_conntrack_ipv6<br \/>\n<br \/>\nip6table_filter <span class=\"nu0\">16384<\/span> <span class=\"nu0\">0<\/span><br \/>\n<br \/>\nip6_tables <span class=\"nu0\">28672<\/span> <span class=\"nu0\">1<\/span> ip6table_filter<br \/>\n<br \/>\nnf_conntrack_ipv4 <span class=\"nu0\">16384<\/span> <span class=\"nu0\">0<\/span><br \/>\n<br \/>\nnf_defrag_ipv4 <span class=\"nu0\">16384<\/span> <span class=\"nu0\">1<\/span> nf_conntrack_ipv4<br \/>\n<br \/>\nxt_recent <span class=\"nu0\">20480<\/span> <span class=\"nu0\">0<\/span><br \/>\n<br \/>\nxt_conntrack <span class=\"nu0\">16384<\/span> <span class=\"nu0\">0<\/span><br \/>\n<br \/>\nnf_conntrack <span class=\"nu0\">135168<\/span> <span class=\"nu0\">3<\/span> xt_conntrack,nf_conntrack_ipv6,nf_conntrack_ipv4<br \/>\n<br \/>\ncpuid <span class=\"nu0\">16384<\/span> <span class=\"nu0\">0<\/span><br \/>\n<br \/>\niptable_filter <span class=\"nu0\">16384<\/span> <span class=\"nu0\">1<\/span><br \/>\n<br \/>\nipmi_devintf <span class=\"nu0\">20480<\/span> <span class=\"nu0\">0<\/span><br \/>\n<br \/>\nipmi_msghandler <span class=\"nu0\">53248<\/span> <span class=\"nu0\">1<\/span> ipmi_devintf<\/div><\/div>\n<p>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.<\/p>\n<p>So we confirmed that our module is loaded in the kernel successfully.<\/p>\n<h2><strong>Removing\/Unloading The Kernel Module<\/strong><\/h2>\n<p>To remove or unload the kernel module, we can use the Linux command \u201crmmod\u201d. 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:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">sushilrathore-<span class=\"nu0\">2<\/span>:~<span class=\"sy0\">\/<\/span>hello-world$ <span class=\"kw2\">sudo<\/span> rmmod hello_world<br \/>\n<br \/>\nsushilrathore-<span class=\"nu0\">2<\/span>:~<span class=\"sy0\">\/<\/span>hello-world$<\/div><\/div>\n<p>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.<\/p>\n<h2><strong>Confirming The Removal\/Unloading Of The Module<\/strong><\/h2>\n<p>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 \u201clsmod\u201d.<\/p>\n<p>Following is the example output from my system:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;height:100%;\"><div class=\"bash codecolorer\">sushilrathore-<span class=\"nu0\">2<\/span>:~<span class=\"sy0\">\/<\/span>hello-world$ <span class=\"kw2\">lsmod<\/span><br \/>\n<br \/>\nModule Size Used by<br \/>\n<br \/>\nnf_conntrack_ipv6 <span class=\"nu0\">20480<\/span> <span class=\"nu0\">0<\/span><br \/>\n<br \/>\nnf_defrag_ipv6 <span class=\"nu0\">20480<\/span> <span class=\"nu0\">1<\/span> nf_conntrack_ipv6<br \/>\n<br \/>\nip6table_filter <span class=\"nu0\">16384<\/span> <span class=\"nu0\">0<\/span><br \/>\n<br \/>\nip6_tables <span class=\"nu0\">28672<\/span> <span class=\"nu0\">1<\/span> ip6table_filter<br \/>\n<br \/>\nnf_conntrack_ipv4 <span class=\"nu0\">16384<\/span> <span class=\"nu0\">0<\/span><br \/>\n<br \/>\nnf_defrag_ipv4 <span class=\"nu0\">16384<\/span> <span class=\"nu0\">1<\/span> nf_conntrack_ipv4<br \/>\n<br \/>\nxt_recent <span class=\"nu0\">20480<\/span> <span class=\"nu0\">0<\/span><br \/>\n<br \/>\nxt_conntrack <span class=\"nu0\">16384<\/span> <span class=\"nu0\">0<\/span><br \/>\n<br \/>\nnf_conntrack <span class=\"nu0\">135168<\/span> <span class=\"nu0\">3<\/span> xt_conntrack,nf_conntrack_ipv6,nf_conntrack_ipv4<br \/>\n<br \/>\ncpuid <span class=\"nu0\">16384<\/span> <span class=\"nu0\">0<\/span><br \/>\n<br \/>\niptable_filter <span class=\"nu0\">16384<\/span> <span class=\"nu0\">1<\/span><br \/>\n<br \/>\nipmi_devintf <span class=\"nu0\">20480<\/span> <span class=\"nu0\">0<\/span><br \/>\n<br \/>\nipmi_msghandler <span class=\"nu0\">53248<\/span> <span class=\"nu0\">1<\/span> ipmi_devintf<br \/>\n<br \/>\nvmw_vsock_vmci_transport <span class=\"nu0\">32768<\/span> <span class=\"nu0\">1<\/span><br \/>\n<br \/>\nvsock <span class=\"nu0\">36864<\/span> <span class=\"nu0\">2<\/span> vmw_vsock_vmci_transport<br \/>\n<br \/>\nbinfmt_misc <span class=\"nu0\">20480<\/span> <span class=\"nu0\">1<\/span><br \/>\n<br \/>\nintel_rapl_perf <span class=\"nu0\">16384<\/span> <span class=\"nu0\">0<\/span><br \/>\n<br \/>\njoydev <span class=\"nu0\">24576<\/span> <span class=\"nu0\">0<\/span><br \/>\n<br \/>\ninput_leds <span class=\"nu0\">16384<\/span> <span class=\"nu0\">0<\/span><br \/>\n<br \/>\nvmw_balloon <span class=\"nu0\">20480<\/span> <span class=\"nu0\">0<\/span><br \/>\n<br \/>\nserio_raw <span class=\"nu0\">16384<\/span> <span class=\"nu0\">0<\/span><br \/>\n<br \/>\nshpchp <span class=\"nu0\">36864<\/span> <span class=\"nu0\">0<\/span><br \/>\n<br \/>\nvmw_vmci <span class=\"nu0\">69632<\/span> <span class=\"nu0\">2<\/span> vmw_balloon,vmw_vsock_vmci_transport<\/div><\/div>\n<p>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.<\/p>\n<p>There is one more very important utility offered, which can be used to get the info of the kernel module file. \u201cmodinfo\u201d is the command provided to get the details of the already present kernel module.<\/p>\n<p>Executing the \u201cmodinfo\u201d with the hello-world kernel module we get the below output:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">sushilrathore-<span class=\"nu0\">2<\/span>:~<span class=\"sy0\">\/<\/span>hello-world$ modinfo hello-world.ko<br \/>\n<br \/>\nfilename: <span class=\"sy0\">\/<\/span>home<span class=\"sy0\">\/<\/span>sushilrathore<span class=\"sy0\">\/<\/span>hello-world<span class=\"sy0\">\/<\/span>hello-world.ko<br \/>\n<br \/>\ndescription: Hello world<span class=\"sy0\">!<\/span><br \/>\n<br \/>\nlicense: GPL<br \/>\n<br \/>\nsrcversion: BBD5A60063B15C8D80048FD<br \/>\n<br \/>\ndepends:<br \/>\n<br \/>\nretpoline: Y<br \/>\n<br \/>\nname: hello_world<br \/>\n<br \/>\nvermagic: 4.15.0-<span class=\"nu0\">163<\/span>-generic SMP mod_unload modversions<br \/>\n<br \/>\ncienauser<span class=\"sy0\">@<\/span>haxv-srathore-<span class=\"nu0\">2<\/span>:~<span class=\"sy0\">\/<\/span>hello-world$<\/div><\/div>\n<p>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.<\/p>\n<p>Below is the sample code and Makefile code we have used in our discussion above:<\/p>\n<p><strong>hello-world.c<\/strong><\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;height:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;linux\/module.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"co2\">#include &lt;linux\/kernel.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"co2\">#include &lt;linux\/init.h&gt;<\/span><br \/>\n&nbsp; &nbsp; <br \/>\nMODULE_LICENSE<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;GPL&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\nMODULE_DESCRIPTION<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Hello world!&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">static<\/span> <span class=\"kw4\">int<\/span> __inithello_init<span class=\"br0\">&#40;<\/span><span class=\"kw4\">void<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; printk<span class=\"br0\">&#40;<\/span>KERN_INFO <span class=\"st0\">&quot;Hello world<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">return<\/span> <span class=\"nu0\">0<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">static<\/span> <span class=\"kw4\">void<\/span> __exit hello_exit<span class=\"br0\">&#40;<\/span><span class=\"kw4\">void<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; printk<span class=\"br0\">&#40;<\/span>KERN_INFO <span class=\"st0\">&quot;Goodbye world<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\nmodule_init<span class=\"br0\">&#40;<\/span>hello_init<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\nmodule_exit<span class=\"br0\">&#40;<\/span>hello_exit<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n<p><strong>Makefile<\/strong><\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\">obj<span class=\"sy0\">-<\/span>m <span class=\"sy0\">=<\/span> hello<span class=\"sy0\">-<\/span>world.<span class=\"me1\">o<\/span><\/div><\/div>\n<p>all:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\">make <span class=\"sy0\">-<\/span>C <span class=\"sy0\">\/<\/span>lib<span class=\"sy0\">\/<\/span>modules<span class=\"sy0\">\/<\/span>$<span class=\"br0\">&#40;<\/span>shell uname <span class=\"sy0\">-<\/span>r<span class=\"br0\">&#41;<\/span><span class=\"sy0\">\/<\/span>build<span class=\"sy0\">\/<\/span> M<span class=\"sy0\">=<\/span>$<span class=\"br0\">&#40;<\/span>PWD<span class=\"br0\">&#41;<\/span> modules<\/div><\/div>\n<p>clean:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\">make <span class=\"sy0\">-<\/span>C <span class=\"sy0\">\/<\/span>lib<span class=\"sy0\">\/<\/span>modules<span class=\"sy0\">\/<\/span>$<span class=\"br0\">&#40;<\/span>shell uname <span class=\"sy0\">-<\/span>r<span class=\"br0\">&#41;<\/span><span class=\"sy0\">\/<\/span>build M<span class=\"sy0\">=<\/span>$<span class=\"br0\">&#40;<\/span>PWD<span class=\"br0\">&#41;<\/span> clean<\/div><\/div>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>So far, we have discussed about the loadable kernel modules process\/steps to add the kernel module. Insmod is the command we discussed for loading the kernel module. To confirm if the module is loaded successfully, we can use the lsmod command and finally, to remove the rmmod command can be used. There is one more utility modinfo which can be used to print the details of the kernel module.\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The kernel module is the logical separate functionality that can be added later in the running kernel. Adding module to Kernal is discussed in this article.<\/p>\n","protected":false},"author":143,"featured_media":180869,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_wp_convertkit_post_meta":{"form":"-1","landing_page":"","tag":"0"},"footnotes":""},"categories":[569],"tags":[],"class_list":["post-177620","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux-kernel"],"_links":{"self":[{"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/posts\/177620","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/users\/143"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/comments?post=177620"}],"version-history":[{"count":0,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/posts\/177620\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/media\/180869"}],"wp:attachment":[{"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/media?parent=177620"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/categories?post=177620"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/tags?post=177620"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}