[1] It is necessary to query the Linux Kernel
# Uname-R
2.6.28-11-generic
# Ls/usr/src/
Linux-headers-2.6.28-11 linux-headers-2.6.28-11-generic
It can be seen that the kernel version is consistent with the kernel header file version, both 2.6.28-11. (If they are inconsistent, an error will occur in the insmod step:
Error inserting './Hello. Ko':-1 invalid module format
There is a way to correct this error on the Internet, but it feels like speculation-avoiding kernel version checks; I encountered a mismatch between header files and kernel versions when installing Ubuntu 8.04, later, we installed Ubuntu 9.04 ).
[2] Write hello. c
Create your own working directory, for example:
# Mkdir/home/WK/Hello
Write hello. c
# Cd/home/WK/Hello
# Gedit hello. c
Add the following content:
// Begin --- hello. c
# Include </usr/src/linux-headers-2.6.28-11/include/Linux/init. h>
# Include </usr/src/linux-headers-2.6.28-11/include/Linux/module. h>
Module_license ("GPL ");
// Printk (kern_alert "begin \ n ");
Static int hello_init (void)
{
Printk (KERN_ALERT "Hello World! \ N ");
Return 0;
}
Static void hello_exit (void)
{
Printk (KERN_ALERT "Good bye, ubuntu \ n ");
// Return 0;
}
Module_init (hello_init );
Module_exit (hello_exit );
// End --- hello. c
Note the first line
# Include </usr/src/linux-headers-2.6.28-11/include/linux/init. h>
The location must be correct; or you only need to write
# Include <linux/init. h>
Save and exit (Ctrl + Q ).
[3] compile Makefile
# Cd/home/wk/hello
# Gedit Makefile
Note the case sensitivity.
# Begin --- Makefile
KERNELDIR =/lib/modules/2.6.28-11-generic/build
PWD: = $ (shell pwd)
INSTALLDIR =/home/wk/hello/install
Obj-m: = hello. o
Modules:
$ (MAKE)-C $ (KERNELDIR) M = $ (PWD) modules
Modules_install:
Cp hello. ko $ (INSTALLDIR)
Clean:
Rm-rf *. o *~ Core. depend. *. cmd *. ko *. mod. c. tmp_versions
. PHONY: modules modules_install clean
# End --- Makefile
If you have a detailed explanation of Makefile on the internet, I will not go over it.
Save and exit.
[4] Start make
# Make
Output messages similar to the following:
Make-C/lib/modules/2.6.28-11-generic/build M =/home/wk/hello modules
Make [1]: Entering directory '/usr/src/linux-headers-2.6.28-11-generic'
Building modules, stage 2.
MODPOST 1 modules
Make [1]: Leaving directory '/usr/src/linux-headers-2.6.28-11-generic'
The compilation is successful.
# Ls
Check that a bunch of files are generated, such:
Hello. c hello. mod. c install Makefile1 Module. symvers
Hello. c ~ Hello. mod. o Makefile Module. markers
Hello. ko hello. o Makefile ~ Modules. order
[5] installation module:
# Insmod hello. ko
Or
# Insmod./hello. ko
Meaning.
This step is complete and does not respond. Next, try again:
# Insmod hello. ko
Error message:
Insmod: error inserting 'hello. ko ':-1 File exists
The hello module is successfully installed.
Use
# Lsmod
You can also view the hello module.
Avoid printing too much. Use:
# Lsmod | head-5
Only the first five rows are displayed. For example:
Module Size Used
Hello 9344 0
Binfmt_misc 16776 1
Bridge 56340 0
STPs 10500 1 bridge
See the hello module ~~~
But why not print it? Don't talk about this first...
[6] uninstall the module
# Rmmod hello. ko
Reuse
# Lsmod | head-5
The hello module does not exist.
[7] About printk
Now let's talk about this question: During insmod, what does printk ask if "Hello World" is not displayed on the terminal? Because it is clearly written in hello. c:
Static int hello_init (void)
{
Printk (KERN_ALERT "Hello World! \ N ");
Return 0;
}
The answer is only one sentence: printk is not displayed on a super terminal! Depend! Why? I don't know either...
[8] remedy for printing debugging information of printk
Run the following command:
# Dmesg | tail-8
The command format is similar to lsmod (only the last eight lines are displayed ).
[2, 95.586960] Good bye, UBUNTU
[1, 96.483964] Hello world!
[2, 97.031787] Good bye, UBUNTU
[1, 97.594151] Hello world!
[2, 98.109896] Good bye, UBUNTU
[1, 98.615569] Hello world!
[2, 99.098943] Good bye, UBUNTU
[1, 1893.976170] Hello world!
The above information shows that I made rmmod hello. KO in 95.586960 and insmod hello. KO in 96.483964 ......
The number in [] indicates the time when the module is installed/uninstalled, for example, 96.483964 S (about one and a half hours later ).
[9] Where does this information exist?
It is said that the default value is in the/Val/log/messages directory; but sometimes you see it, no;
Take a look at another directory:
# Cd/etc
# Ls
You will find the file containing syslog, such:
Syslog. conf
That's it.
# Cat syslog. conf | head-20
Similarly, only the first 20 rows of stuff are displayed. You can see something similar to the following:
#/Etc/syslog. conf Configuration file for syslogd.
#
# For more information see syslog. conf (5)
# Manpage.
#
# First some standard logfiles. Log by facility.
#
Auth, authpriv. */var/log/auth. log
*. *; Auth, authpriv. none-/var/log/syslog
# Cron. */var/log/cron. log
Daemon. *-/var/log/daemon. log
Kern. *-/var/log/kern. log
Lpr. *-/var/log/lpr. log
Mail. *-/var/log/mail. log
User. *-/var/log/user. log
#
# Logging for the mail system. Split it up so that
...
See kern ~ See/var/log/kern. log! I guess the message is under this.
So,
# Cat/var/log/kern. log | tail-10
Display:
Aug 4 23:40:53 ubuntu kernel: [38.848634] eth0: no IPv6 routers present
Aug 4 23:41:47 ubuntu kernel: [92.893834] Hello World!
Aug 4 23:41:50 ubuntu kernel: [95.586960] Good bye, ubuntu
Aug 4 23:41:51 ubuntu kernel: [96.483964] Hello World!
Aug 4 23:41:51 ubuntu kernel: [97.031787] Good bye, ubuntu
Aug 4 23:41:52 ubuntu kernel: [97.594151] Hello World!
Aug 4 23:41:52 ubuntu kernel: [98.109896] Good bye, ubuntu
Aug 4 23:41:53 ubuntu kernel: [98.615569] Hello World!
Aug 4 23:41:53 ubuntu kernel: [99.098943] Good bye, ubuntu
Aug 5 00:11:48 ubuntu kernel: [1893.976170] Hello World!
See it! It is more complete and similar to the content displayed in the previous dmesg!