Skip to main content

Compiling Linux Kernel modules on OLinuXino (ArchLinux ARM)

So if you have an OLinuXino and have installed the development tools on the board and successfully compiled userspace programs on it (refer this blog post), and are feeling adventurous, you can try compiling kernel modules as well. (Make sure you have gcc setup – refer to the blog post linked to above)

So at the OLinuXino console (Serial or SSH) logged in as root:
  1. Install kernel headers:
    pacman -S linux-armv5-headers
  2. In your home directory, create the main.c source file:
    nano main.c

    and type the following and save the file:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    #include <linux/module.h> //Needed by all modules
    #include <linux/kernel.h> //Needed for KERN_INFO
    int init_module(void) {
     printk(KERN_INFO "Hello world\n");
    
     //A non 0 return means init_module failed; module can't be loaded.
     return 0;
    }
    
    void cleanup_module(void) {
     printk(KERN_INFO "Goodbye world\n");
    }
    
    MODULE_AUTHOR("Anurag Chugh");
    MODULE_DESCRIPTION("Hello World");
    MODULE_LICENSE("GPL v2");
    
  3. While still in your home directory, create the makefile:
    nano Makefile

    and type the following into it:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    obj-m += hello.o
    
    hello-objs := main.o
    
    all: uninstall-kernel-module
     make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
     -sudo insmod hello.ko
     sudo dmesg -c 
    
    clean: uninstall-kernel-module
     make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
     sudo dmesg -c
    
    #the '-' in front of the command tells make to continue with the next command incase an error is encountered 
    install-kernel-module:
     -sudo insmod hello.ko
     
    uninstall-kernel-module:
     -sudo rmmod hello
    
  4. Issue the command to clear the kernel message buffer:
    dmesg –c
  5. Build the kernel module, the make file will automatically load the module as well and the kernel messages will report its execution:
    make

image
Successfully compiled and loaded the hellow world kernel module


Comments