I have read some linux driver materials over the past few days and have a general understanding of its structure and principles. Then I started to practice it. First, re-compile the kernel to build the Linux kernel tree. I am a little white, I searched a lot on the internet, tried it several times, and finally succeeded. Step 1: install the software required to compile the kernel: build-essential, autoconf, automake, cvs, subversion $ apt-getinstallbuild-ess
I have read some linux driver materials over the past few days and have a general understanding of its structure and principles. Then I started to practice it.
First, re-compile the kernel to build the Linux kernel tree. I am a little white, I searched a lot on the internet, tried it several times, and finally succeeded. The procedure is as follows:
1. install the software required to compile the kernel
Include: build-essential, autoconf, automake, cvs, and subversion
$ Apt-get install build-essential kernel-package libncurses5-dev
Libncurses5 is used when menuconfig is used to configure the kernel.
2. Download the kernel source code
First check the Linux kernel version: $ uname-r
Use the apt-cache search linux-source command to list some optional source code packages. Check the version number of your kernel and select the one with Ubuntu patche.
Finally download it with the apt-get install linux-source-2.6.32. Decompress the source code package and go to the decompressed source code directory.
Before compilation, we need a configuration file of the original Ubuntu kernel. This is the file preview under my/usr/src directory: ls-al
Drwxr-xr-x 4 root 4096 2010-09-04 fglrx-8.723.1
Drwxr-xr-x 24 root 4096 2010-09-04 linux-headers-2.6.32-25
Drwxr-xr-x 7 root 4096 2010-09-04 20:35 linux-headers-2.6.32-25-generic
Drwxr-xr-x 25 root 4096 2010-09-16 21:39 linux-source-2.6.32
-Rw-r -- 1 root 65846876 2010-09-01 linux-source-2.6.32.tar.bz2
Now we need the. config file under the linux-headers-2.6.32-25-generic directory, www.linuxidc.com we copy it to the directory we just decompressed, that is, the linux-source-2.6.32
Sudo cp/usr/src/linux-headers-2.6.32-25-generic/. config/usr/src/linux-2.6.32.
Next, switch to the root user.
Sudo-I
Cd/usr/src/linux-2.6.32
Make menuconfig
A configuration page is displayed.
There are two final items: load a alternative kernel configuration...
Save a alternative configuration...
Select load a kernel configuration to save, and then save and exit after selecting save akernel configuration, and exit the configuration environment.
Next we will start compiling.
# Linux-2.6.32/cd/usr/src/
# Make
Remember that if the administrator account is running, this process will take a long time. If your cpu is dual-core, you can add a parameter after make, make-j4.
# Make bzImage after execution, you can see that a new file is generated in the current directory: vmlinux, whose attribute is-rwxr-xr-x.
# Make modules/* Compilation module */
# Make modules_install this command can generate a directory under the/lib/modules directory
If everything goes smoothly and there is no error during the compilation process, we can start the helloworld of the linux module.
I created two text files hello. c Makefile in the/home/xxxx/linux_modules/directory.
// Hello. c
# Include
# Include
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 );
// Makefile
Obj-m: = hello. o
KERNELDIR: =/lib/modules/2.6.32-21-generic/build
PWD: = $ (shell pwd)
Modules:
$ (MAKE)-C $ (KERNELDIR) M = $ (PWD) modules
Modules_install:
$ (MAKE)-C $ (KERNELDIR) M = $ (PWD) modules_install
Clean:
Rm-rf *. o *~ Core. depend. *. cmd *. ko *. mod. c. tmp_versions $ (TARGET)
Note that a tab must be added before the makefile format $ (MAKE.
Make compilation. If no error occurs, use ls-al to view the linux_modules directory and generate the following file:
Hello. c hello. mod. c hello. o modules. order
Hello. ko hello. mod. o Makefile Module. symvers
Hello. ko is a module file that can be loaded.
Now we can load the compiled module helloworld into the kernel.
# Insmod./hello. ko // This command loads hello. ko to the kernel
# Lsmod | grep hello // lsmod command to view all current driver modules. The result should be displayed as hello 680 0
# Rmmod hello // This command removes the hello module.
The output result of the program can be found in
/Var/log/syslog File View
Hello, World
Goodbye, cruel world
This is the output of the program.