Linux kernel compiling, updating and kernel module compiling, loading and removing

Brief intro to Linux kernel compiling and updating, as well as Linux kernel module compiling, loading and removing. The Linux kernel we are playing with is 3.8.4.

1. Linux kernel compiling and updating
Ubuntu:
sudo apt-get install wget
sudo apt-get install gcc
sudo apt-get install libncurses5-dev
sudo apt-get update
sudo apt-get upgrade
wget https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.8.4.tar.xz (NOTE: this URL is from http://www.kernel.org)
sudo tar -xvf linux-3.8.4.tar.xz -C /usr/src/
cd /usr/src/linux-3.8.4/
sudo make menuconfig
sudo make oldconfig
sudo make
sudo make modules_install install
reboot

Fedora (using root login):
yum install wget
yum install gcc
yum install ncurses-devel
yum update (NOTE: yum may update the kernel eventually…if it does, skip this cmd!)
wget https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.8.4.tar.xz
tar -xvf linux-3.8.4.tar.xz -C /usr/src/kernels/
cd /usr/src/kernels/linux-3.8.4
make menuconfig
make oldconfig
make
make modules_install install
reboot

Reference:
http://www.howopensource.com/2011/08/how-to-compile-and-install-linux-kernel-3-0-in-ubuntu-11-04-10-10-and-10-04/
http://www.howopensource.com/2011/08/how-to-install-compile-linux-kernel-3-0-in-fedora-15-and-14/

2. Linux kernel module compiling, loading and removing
[root@daveti ch2]# cat simple.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>

/* This function is called when the module is loaded. */
int simple_init(void)
{
       printk(KERN_INFO “Loading Modulen”);

       return 0;
}

/* This function is called when the module is removed. */
void simple_exit(void) {
    printk(KERN_INFO “Removing Modulen”);
}

/* Macros for registering module entry and exit points. */
module_init( simple_init );
module_exit( simple_exit );

MODULE_LICENSE(“GPL”);
MODULE_DESCRIPTION(“Simple Module”);
MODULE_AUTHOR(“SGG”);

[root@daveti ch2]# cat Makefile
obj-m += simple.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
[root@daveti ch2]#

make (build the module)
insmod simple.ko (Load the module)
lsmod | grep simple (You will find the module – simple)
dmesg (You will find the log ‘Loading Module’)
rmmod simple (remove the module)
lsmod | grep simple (You will NOT find the module – simple)
dmesg (You will find the log ‘Removing Module’)

Reference:
http://www.os-book.com/

About daveti

Interested in kernel hacking, compilers, machine learning and guitars.
This entry was posted in OS and tagged , , . Bookmark the permalink.

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