For the first time, compile the kernel module-general Linux technology-Linux technology and application information. The following is a detailed description. The structure and running environment of the kernel module are very different from those of common programs.
The syntax written by the module is almost the same as the C syntax. However, if you can really understand the features of the module running, you also need to know the hardware and operating system knowledge.
My first kernel module compilation experience:
[Email = lzel @ lzel-laptop :~ /Module $] lzel @ lzel-laptop :~ /Module $ [/email] ls
Hellomod. c Makefile modutils-2.4.27
[Email = lzel @ lzel-laptop :~ /Module $] lzel @ lzel-laptop :~ /Module $ [/email]
[Email = lzel @ lzel-laptop :~ /Module $] lzel @ lzel-laptop :~ /Module $ [/email] cat hellomod. c
# Include
# Include
# Include
Static int _ init lkp_init (void)
{
Printk ("<1> Hello, World! From the kernel space... \ n ");
Return 0;
}
Static void _ exit lkp_cleanup (void)
{
Printk ("<1> Goodbye, World! Leaving kernel space .. \ n ");
}
Module_init (lkp_init );
Module_exit (lkp_cleanup );
MODULE_LICENSE ("GPL ");
[Email = lzel @ lzel-laptop :~ /Module $] lzel @ lzel-laptop :~ /Module $ [/email] cat Makefile
Obj-m + = hellomod. o
All:
Make-C/usr/src/linux M = $ (PWD) modules
Clean:
Make-C/usr/src/linux M = $ (PWD) clean
[Email = lzel @ lzel-laptop :~ /Module $] lzel @ lzel-laptop :~ /Module $ [/email] ls/usr/src/
Gcc-4.2 linux-headers-2.6.22-14 linux-headers-2.6.22-14-generic
[Email = lzel @ lzel-laptop :~ /Module $] lzel @ lzel-laptop :~ /Module $ [/email] sudo make-C/usr/src/linux-headers-2.6.22-14-generic SUBDIRS = $ PWD modules
Make: Entering directory '/usr/src/linux-headers-2.6.22-14-generic'
CC [M]/home/lzel/Module/hellomod. o
Building modules, stage 2.
MODPOST 1 modules
CC/home/lzel/Module/hellomod. mod. o
LD [M]/home/lzel/Module/hellomod. ko
Make: Leaving directory '/usr/src/linux-headers-2.6.22-14-generic'
[Email = lzel @ lzel-laptop :~ /Module $] lzel @ lzel-laptop :~ /Module $ [/email] ls
Hellomod. ko hellomod. mod. o Makefile modutils-2.4.27
Hellomod. c hellomod. mod. c hellomod. o Module. symvers
[Email = lzel @ lzel-laptop :~ /Module $] lzel @ lzel-laptop :~ /Module $ [/email] sudo insmod hellomod. ko
[Email = lzel @ lzel-laptop :~ /Module $] lzel @ lzel-laptop :~ /Module $ [/email] lsmod
Module Size Used
Hellomod0 2560
Nls_iso8859_1 5120 1
.....................................
[Email = lzel @ lzel-laptop :~ /Module $] lzel @ lzel-laptop :~ /Module $ [/email] dmesg
.....................................
[1, 1121.812000] Hello, World! From the kernel space...
[Email = lzel @ lzel-laptop :~ /Module $] lzel @ lzel-laptop :~ /Module $ [/email] sudo rmmod hellomod
[Email = lzel @ lzel-laptop :~ /Module $] lzel @ lzel-laptop :~ /Module $ [/email] lsmod | less
Module Size Used
Nls_iso8859_1 5120 1
....................................
[Email = lzel @ lzel-laptop :~ /Module $] lzel @ lzel-laptop :~ /Module $ [/email] dmesg
[1331.104000] Goodbye, World! Leaving kernel space ..
[Email = lzel @ lzel-laptop :~ /Module $] lzel @ lzel-laptop :~ /Module $ [/email] cat/var/log/syslog | less
......................................
Mar 21 14:04:48 lzel-laptop kernel: [1317.028000] Hello, World! From the kernel space...
Mar 21 14:05:02 lzel-laptop kernel: [1331.104000] Goodbye, World! Leaving kernel space ..
Just not so smooth:
1. The modutils tool is not installed. insmod, rmmod, and lsmod cannot be used.
Modutiles-2.4.27:
Http://down1.chinaunix.net/distfiles/modutils-2.4.27.tar.bz2
2. Pay special attention to the kernel version of your linux operating system.
Use ls/usr/src/
Under Ubuntu:
Linux-headers-2.6.22-14 linux-headers-2.6.22-14-generic
Under Redhat 6.0:
Kernels Redhat
Must enter kernels
Ls/usr/src/kernels/
2.6.22.6-1-i686
Then compile with make-C/kernel path SUBDIRS = $ PWD modules (Note: superuser permission)
3. insert modules into the kernel and clear the kernel
Use insmod [module. ko (version 2.6 or later)] and rmmod [module]
4. view the running status:
1) dmesg command
2) cat or tail or head/var/log/syslog (message)
For details, refer to the above operations. For the first time you compile the kernel, there are still many things you don't know.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service