Skip to content

Commit

Permalink
The simplest Linux kernel module.
Browse files Browse the repository at this point in the history
  • Loading branch information
selbyk committed Apr 8, 2015
1 parent 1f8bbf2 commit 868859f
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
11 changes: 11 additions & 0 deletions hello-kernel/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Makefile for hello world kernel module
obj-m := hello.o # Module Name is hello.c

KDIR := /lib/modules/$(shell uname -r)/build

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

clean:
$(MAKE) -C $(KDIR) M=$(PWD) clean
$(RM) Module.markers modules.order
25 changes: 25 additions & 0 deletions hello-kernel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
hello.c
---

The simplest kernel module.

### Getting started

Hmmm...

### Building

Yeah... try `make`

### Testing

`./test.sh`

Expected output: http://showterm.io/b0ef83393d4ba67b40480

### Credits

Thanks to [this tutorial](http://www.tldp.org/LDP/lkmpg/2.6/html/) by [Peter Jay Salzman](http://dirac.org/).

And some thanks to my friend [Dimitris Zervas](http://dzervas.gr/) for helping me realize what a Makefile
requires to build a kernel module.
21 changes: 21 additions & 0 deletions hello-kernel/hello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* hello.c - The simplest Linux kernel module.
*/

#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_ALERT */

int init_module(void)
{
printk(KERN_ALERT "Hello from the Kernel!\n");

// A non 0 return means init_module failed; module can't be loaded.
return 0;
}

void cleanup_module(void)
{
printk(KERN_ALERT "Goodbye, I'm out.\n");
}

MODULE_LICENSE("GPL");
34 changes: 34 additions & 0 deletions hello-kernel/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash
# Tests the kernel module
echo 'Begin tests'

# Build
echo 'Running "make all" to build module'
make all

# Load module
echo 'Running "sudo insmod hello.ko" to load module'
sudo insmod hello.ko

# Test if module loaded
echo 'Running "lsmod | grep 'hello'" to test if module loaded properly'
lsmod | grep 'hello'

# Unload module
echo 'Running "sudo rmmod hello" to unload module'
sudo rmmod hello

# Test if module unloaded
echo 'Running "lsmod | grep 'hello'" to test if module unloaded properly'
lsmod | grep 'hello'

# Clean up like a good kid
echo 'Running "make clean" to clean everything up'
make clean

# Check module
echo 'Running "tail -n 10 /var/log/syslog" to see if module did what it was supposed to'
tail -n 10 /var/log/syslog
journalctl -r

echo 'Tests finished'

0 comments on commit 868859f

Please sign in to comment.