Skip to main content

How to add a new Linux Kernel Module (LKM)

To demonstrate, let us goto a simple “Hello World” as a module and inserted in to the kernel

Step 1
Open any editor like vi or gedit in a shell prompt and the type the following
/* Name of the file is hello.c */
#include <linux/module.h>
#include <linux/kernel.h>
int init_module(void)
{
printk(KERN_INFO “Hello world.n”);
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO “Goodbye worldn”);
}


The above is a simple C program that displays “Hello world” when the module is inserted and displays “Goodbye world” when the module is removed.
Step 2
The program can be compiled using gcc compiler in the shell prompt itself, but we need to write the commands twice or more. So to compile shortly let us write a makefile to compile the above program
The make file should be named as Makefile (See the First letter M is uppercase and there should not be any space between Make and file)
Copy and paste the following lines in an editor (vi or gedit)
obj-m += hello.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Step 3
after this, execute the following command
# make
You will get the following output which indicates there is no error
make -C /lib/modules/2.6.27.5-117.fc10.i686/build M=/home/pradeepkumar/lsp modules
make[1]: Entering directory `/usr/src/kernels/2.6.27.5-117.fc10.i686′
Building modules, stage 2.
MODPOST 1 modules
make[1]: Leaving directory `/usr/src/kernels/2.6.27.5-117.fc10.i686′
The make command creates new set of files like hello.ko, hello.mod.o, modules.order, etc
Step 4
The .ko indicates it is a kernel object, but ordinary C compiler generates only an object file..
go to the shell prompt (open the terminal) execute the command one by one
# su
The above command waits for the root password, please provide the password
# insmod
inserting the module
# lsmod
listing all the modules running under the kernel, you can see the first module will be listed as hello
# dmesg
you can see thelast line says hello world
# rmmod
removing the module
# dmesg
you can see the last line says goodbye world
Like this we can write many modules and can be inserted to the kernel..

Comments

  1. bhasker bhashwaram28 January 2010 at 16:47

    thanks for the simple steps for inserting a module. i could do it. sir i am recently doing a course in TIFAC on wireless and embedded system 16bit. please suggest one project in this field so that learning would become more easy and interesting

    bhasker bhashwaram
    07bce079
    b batch
    cse

    ReplyDelete
  2. Sir,
    this is not working in fedora 12.
    Even kernel compilation didnt work successfuly on fedora 12
    kindly help on this

    kumar akshay
    07bce165
    c batch

    ReplyDelete

Post a comment

Popular posts from this blog

Implementing a new system call in Kernel version 2.6.32

A system call is used by application or user programs to request service from the operating systems. Since the user programs does not have direct access to the kernel whereas the OS has the direct access. OS can access the hardware through system calls only. The following files has to be modified for implementing a system call /usr/src/linux-2.6.32.5/arch/x86/kernel/syscall_table_32.S /usr/src/linux-2.6.32.5/arch/x86/include/asm/unistd_32.h /usr/src/linux-2.6.32.5/include/linux/syscalls.h /usr/src/linux-2.6.32.5/Makefile New set of files to be created Create a new directory newcall/ inside the path “ /usr/src/linux-2.6.32.5/ ” Create new files Makefile, newcall.c and put them in the /usr/src/linux-2.6.32.5/newcall/ folder Create new user files (in any folder of Linux) to test the system call testnewcall.c, testnewcall.h (created in /home/pradeepkumar ) syscall_table_32.S Find the file /usr/src/linux-2.6.32.5/arch/x86/kernel/syscall_tab

How to Access MOODLE in Intranet and Internet

When Moodle is accessed either in Intranet or internet, there will not be any issue. But occasions when the MOODLE Site has to be accessed both in the intranet and in the Internet, here is a simple trick Server Used: IBM Blade Servers Operating system: Windows Server 2008 Moodle Version: 2.4 WAMP Server is used. Number of Users: 3000 (Students) + 200 (Faculty) Open the config.php from ~/moodle/config.php include these lines $CFG->wwwroot = 'http://'.$server_id.'/vitcc'; $CFG->dataroot  = 'C:\\wamp\\moodledata'; before the following line $CFG->directorypermissions = 07xx; Restart the WAMP Server and you can Check MOODLE Site both in Internet and Intranet. The above Image tells the intranet Link and the internet link can be opened outside the campus network

Electrical Machine Design (equations)

Factors DC Machine Transformers Induction Machines Synchronous Machines Output Equation P a =C o D 2 Ln, where Pa=P/h for generators, Pa=P for motors For Single Phase Q=2.22 f B m A i K w A w d 10 -3 For Three Phase Q=3.33 f B m A i K w A w d 10 -3 Q=C o D 2 L n s KVA Input Q= HP * 0.746 / Cos f * h Q=C o D 2 L n s KVA Input Q= HP * 0.746 / Cos f * h For Turbo alternators Q=1.11B av ac K ws V a 2 L 10 -3 /n s Output Coefficient C o =B av ac* 10 -3 where Bav-magnetic loading and ac - electric loading DNA C o =11 K ws B av ac 10 -3 C o =11 K ws B av ac 10 -3 Choice of Magnetic Loading Flux Density in Teeth Frequency of Flux Reversals Size of machine DNA Magnetizing current, Flux Density, Iron loss Iron loss, Stability, Voltage Rating, Parallel Operation, Transient ShortCircuit current