|
Linux kernel source tree to establish load module hello
Before loading the module, the book said he needed to build the kernel source tree, then, how to build the kernel source tree yet?
First, we must first know the kernel version of your OS, you can get this with uname -r
In / url / src / directory you can see the corresponding release directory
If not you can get the kernel using apt-cache search linux-source
With sudo apt-get install linux-source-3.13.0 download the kernel
After the download is complete, generate a corresponding compressed file linux-source-3.13.0.tar.bz2 in that folder
Then extract the files from tar jxvf linux-source-3.13.0.tar.bz2
Enter the unpacked directory
Then enter root privileges su (where entry failures prompted Authentication failure), enter the following command in Terminal:
sudo passwd
Password: your current password
Enter new UNIX password: This is the root password
Retype new UNIX password: Repeat root password
You are then prompted successful.
Enter the command make oldconfig start configuring the kernel.
Before check the internet to see the kernel configuration spend an hour or so, but after I entered the command output
Information that will generate a new directory under / lib / modules After the configuration /lib/modules/3.16.0-30-generic/
Ls in the directory, I found that already exists in this directory = =! Is the original kernel source tree already exists.
Since the kernel source tree already exists, here we have to do is load the hello module into the kernel.
Here is the source code for hello
#include < linux / init.h>
#include < linux / module.h>
MODULE_LICENSE ( "Dual BSD / GPL");
static int hello_init (void)
{
printk (KERN_ALERT "Hello, world \ n");
return 0;
}
static void hello_exit (void)
{
printk (KERN_ALERT "Goodbye, cruel world \ n");
}
module_init (hello_init);
module_exit (hello_exit);
For coding the Makefile
obj-m: = hello.o
KERNELDIR: = /lib/modules/3.16.0-30/build
PWD: = $ (shell pwd)
modules:
$ (MAKE) -C $ (KERNELDIR) M = $ (PWD) modules
modules_install:
$ (MAKE) -C $ (KERNELDIR) M = $ (PWD) modules_install
Run make
Generate the corresponding file hello.ko
Then load hello.ko module, pay attention to in root mode to load and then loaded modules lsmod View (book that will output hello world in the console, but I did not console output)
Unloading module
Although there is no console output, but you can view the output hello module in the / var / log / syslog |
|
|
|