I rely on it, this is really a tough guy (first ROAR ). No one told me that I had to build an environment for driver development. With the book "Linux Device Driver", I was blinded at the beginning. Really! I don't know if I have to build a kernel tree. I wonder if the Linux operating system has been installed? I don't know why I need to build a kernel tree on my own. Two and a half days ago, I just set it up without doing anything. Ah.
1. I installed Linux Kernel on my machine. This is the source code package. If the version is inconsistent, there will be problems (brother is depressed ).
Then decompress it to the/usr/src directory: tar-zxvf xx.tar.gz or tar-jxvf xx.tar.bz2 (XX is like: linux-2.6.32.7 ).
2. Compile the kernel code:
Step 1: Make oldconfig (or make menuconfig)
Step 2: Make
This step takes a long time. It may take about 40 minutes. In addition, make always reports that the system time is not set at the beginning: clock error ..... Ah, me? It's killing me.
Step 3: Make bzimage
Step 4: Make modules
Step 5: Make modules_install
Step 6: make install
3. Modify the Boot Image File
Enter/boot, you will find an additional Vmlinuz-2.6.32.7 file, and initramfs-2.6.32.7.img files, this can be just compiled out of the kernel ah.
Then we will go to/boot/grub and open the menu. List document. You will find that there will be root =, which will delete the original system and keep the compiled kernel. Otherwise (uname-R) is the default version of the original system. (I don't know why another character string after the original version is 2.6.32-71. el6.i686 ).
4. Now we can start Hello, world.
The example is in the book:
// Hello. c
# 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! \ N ");
}
Module_init (hello_init );
Module_exit (hello_exit );
Then write a makefile document:
OBJ-M: = Hello. o
Save to the same directory:/usr/drivermodules
CD to this directory and then:
Make-C/lib/modules/(uname-R)/build subdirs = $ PWD modules (ubame-R is actually the directory of your kernel version number)
Check if there are several more files? Yes! Next step:
OK! Load insmod hello. Ko.
Run dmesg again and you will see Hello, world.
Rmmod hello. Ko, see Goodbye, cruel world
Of course, the previous command is a little long. We can directly uninstall the MAKEFILE file:
# MARK start
OBJ-M: = Hello. c
Kerneldir =/lib/modules/$ (shell uname-R)/build
PWD: = $ (shell PWD)
Default:
$ (Make)-C $ (kerneldir) M = $ (PWD) Modules
Clean:
$ (RM) *. O *. Ko *. Mod. C Module. symvers
# Make end
Finished!
Read the full text
category: View comments in Linux