Passing command line Arguments / Parameters to Linux kernel module

In Our previous post, “Writing first Linux kernel Module and understanding Kernel module compilation, loading and unloading” We learned how to write a simple Linux kernel module.

Lets see an example of how to pass the loading time parameters to the kernel module. The load time parameter passing is very helpful to enable / disable some code in runtime without recompiling the module. Lets say, we want to enable some debugging information to debug our driver, we can pass a flag like “enable_debug=1” during insmod / modprobe to enable that respective code, check the below code,

 vim hello.c 

#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/init.h> /* Needed for the macros */

static int enable_debug;
module_param(enable_debug, int , 0);
MODULE_PARM_DESC(enable_debug, “A 0/1 flag for enable debugging in loadtime”);

static int __init hello_init(void) {
     printk(KERN_INFO “Hello, world\n”);
     if(enable_debug)
          printk(KERN_INFO “Enabling debugging in driver…\n”);
     return 0;
}

static void __exit hello_exit(void) {
     printk(KERN_INFO “Goodbye, world\n”);
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_AUTHOR(“DEVBEE”);
MODULE_DESCRIPTION(“Hello World Example”);
MODULE_LICENSE(“GPL”);

In this code, check the highlighted code, the code is very self explanatory, it sets/ unsets a flag to be used to make necessary decision in runtime to enable or disable debugging.

 $ make 
 $ insmod ./hello.ko enable_debug=1 

In dmesg, it will print the following message, indicating it has enbled debugging,

 $ dmesg
[ 2864.782079] hello: module verification failed: signature and/or required key missing - tainting kernel
[ 2864.782529] Hello, world
[ 2864.782536] Enabling debugging in driver...

We will see what is verification warning later.

Now, check the modinfo of this module as,

 $ modinfo hello.ko
filename: /home/devbee/devlab/module/hello.ko
license: GPL
description: Hello World Example
author: DEVBEE
srcversion: 4EAF61EDA9E7BFB11A04EDF
depends:
vermagic: 3.19.0-30-generic SMP mod_unload modversions 686
parm: enable_debug:A 0/1 flag for enable debugging in loadtime (int)

Above highlighted information can be corelated with the source.

SHUFFLED :   how initrd is mounted by linux kernel

More Read – Check TLDP

Android Android Commands Android Java Applications Application Libraries Bash / Shell Scripts Bluetooth driver Build Frameworks Commands and Packages Core Kernel C Programs Development Environment Setup Documents / Books Errors & Failures File Systems Framebuffer / Display Driver git Go Language Programs Hardware Platforms Home JAVA Programs Kernel & Device Drivers Kernel Booting and Porting Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL NDK / Middleware / HAL Network Driver OS Concepts PHP Procfs Filesystem Programming Languages RaspberryPi Scripting and Automation Search Engine Optimisation ( SEO ) Socurce Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Userspace Utilities Web design and development Wordpress Yocto / Bitbake / Openembedded

Leave a Reply