A basic Linux kernel module tested on remote machine

I will demonstrate the process in building a basic Linux kernel module and test it on a remote machine by logging in using ssh.

1. Create a file hello.c and copy the below code

#undef __KERNEL__
#define __KERNEL__

#undef MODULE
#define MODULE

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>

static int __init hello_init(void)
{
    printk(KERN_INFO "Hello world!\n");
    return 0;
}

static void __exit hello_cleanup(void)
{
    printk(KERN_INFO "Cleaning up module.\n");
}

module_init(hello_init);
module_exit(hello_cleanup);

2. Create a file Makefile and copy the below code. Also don’t forget to update the KDIR with the kernel path if you are using a kernel different from the development machine. In my case since development and test machine are running the same kernel, I use the path as shown

obj-m := hello.o
#KDIR := /* update with path of kernel code here */
KDIR:=  /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)

all:
	$(MAKE) -C $(KDIR) M=$(PWD) modules

clean:
	$(MAKE) -C $(KDIR) M=$(PWD) clean

3. Build the module using the command

$ make

4. Transfer the module using SCP to other machine using the command

$ scp hello.ko test@192.10.10.101:

Do not forget the “:”. Where test is the user-name and 192.10.10.101 is its ip

5. You can login to the machine using

$ ssh test@192.10.10.101

where test is the user-name and 192.10.10.101 is its ip
and insert the module using the command

$ sudo insmod hello.ko

6. Verify the prints using the command

$ dmesg

or

$ lsmod
Advertisements

One thought on “A basic Linux kernel module tested on remote machine

  1. Pingback: An easy kernel development environment | Prajosh Premdas

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s