Compile and install Linux kernel 2.6.x, a simple kernel driver test

Source: Internet
Author: User

One of the new installation items is: Download now:
Download www.kernel.org,

Decompress the package to/usr/src:
Bzip2-DC linux-2.6.24.4.tar.bz2 | tar xvf-
Rename the folder linux-2.6.24.4 as: Linux-Kernel

Under any folder, write a shell file named A. Sh with the following content:

  1. #! /Bin/bash
  2. Mkdir-P/home/name/build/kernel
  3. CD/usr/src/Linux-Kernel
  4. Make mrproper
  5. Make o =/home/name/build/kernel menuconfig
  6. Make o =/home/name/build/kernel
  7. Sudo make o =/home/name/build/kernel modules_install install

Then open a terminal and execute
Bash A. Sh
In the shell file, "O =/home/name/build/kernel" indicates that the compilation configuration is carried out in the folder after "=". You can check it yourself.
In addition to Kernel configuration, you can skip the subsequent process.
In this process, a new kernel startup image is automatically generated and copied to the/boot directory without manual copying.

Then modify the menu. LST file to enable the system to start with the new kernel:
Vim/etc/GRUB/menu. lst
The content is as follows:

  1. Default = 2
  2. Timeout = 5
  3. Splashimage = (hd0, 6)/boot/GRUB/splash.xpm.gz
  4. Hiddenmenu
  5. Title centos (2.6.24.4)
  6. Root (hd0, 6)
  7. Kernel/boot/vmlinuz-2.6.24.4 Ro root = label =/rhgb quiet
  8. Initrd/boot/initrd-2.6.24.4.img
  9. Title centos (2.6.18-53. EL5)
  10. Root (hd0, 6)
  11. Kernel/boot/vmlinuz-2.6.18-53.el5 Ro root = label =/rhgb quiet
  12. Initrd/boot/initrd-2.6.18-53.el5.img
  13. Title Windows XP
  14. Rootnoverify (hd0, 0)
  15. Chainloader + 1
  16. Comment out this part:
  17. Title centos (2.6.18-53. EL5)
  18. Root (hd0, 6)
  19. Kernel/boot/vmlinuz-2.6.18-53.el5 Ro root = label =/rhgb quiet
  20. Initrd/boot/initrd-2.6.18-53.el5.img
  21. Change
  22. # Title centos (2.6.18-53. EL5)
  23. # Root (hd0, 6)
  24. # Kernel/boot/vmlinuz-2.6.18-53.el5 Ro root = label =/rhgb quiet
  25. # Initrd/boot/initrd-2.6.18-53.el5.img
  26. Modify default = 0 again. Here 0 corresponds to the first title, and so on.
  27. Restart.

The output file for compiling the kernel is too large, that is,/home/name/build/kernel, which can be deleted at the end. Do not delete the driver.

Write the simplest DRIVER: Hello. c

 

/* ===================================================== ======================================
A simple kernel module: "Hello World"
========================================================== ===================================== */
# Include <Linux/init. h>
# Include <Linux/module. h>
Module_license ("dual BSD/GPL ");

Static int hello_init (void)
{
Printk (kern_alert "Hello world enter ");
Return 0;
}

Static void hello_exit (void)
{
Printk (kern_alert "Hello World exit ");
}

Module_init (hello_init );
Module_exit (hello_exit );
Module_author ("ztz0223 ");
Module_description ("a simple hello World module ");
Module_alias ("a simplest module ");

 

Then write a makefile

As follows:

 

Pwd = $ (shell PWD)
Kernel_src =/usr/src/linux-2.6.24.4/

OBJ-M: = Hello. o
Module-objs: = Hello. o

ALL:
$ (Make)-C $ (kernel_src) M = $ (PWD) Modules

Clean:
RM *. Ko
RM *. o

 

Open the terminal and go to make in the hello. c path. The 2.6 kernel does not seem to support GCC compilation. Use make as follows:

[Root @ btazuo azuo] # cd Hello // enter the driver path. This part is a comment
[Root @ btazuo Hello] # dir
Hello. c makefile
[Root @ btazuo Hello] # Make // compile
Make-C/lib/modules/2.6.24.4/build M =/azuo/Hello modules
Make [1]: Entering directory '/usr/src/linux-2.6.24.4'
CC [m]/azuo/Hello. o
Building modules, stage 2.
Modpost 1 modules
CC/azuo/Hello. Mod. o
LD [m]/azuo/Hello. Ko
Make [1]: Leaving directory '/usr/src/linux-2.6.24.4'
[Root @ btazuo Hello] # dir // compiled successfully
Hello. c hello. Ko hello. Mod. c hello. Mod. O hello. O makefile module. symvers
[Root @ btazuo Hello] #

 

Load and uninstall drivers:

 

Open the/var/log/messages file and you can see the kernel loading and uninstalling information:

Hello world enter
Hello World exit
Indicates that the kernel is loaded and uninstalled successfully!

 

This is taken from other places. I have read some help for learning and installing the Linux kernel! Learn more.

  1. [Root @ btazuo Hello] # insmod./Hello. Ko
  2. [Root @ btazuo Hello] # rmmod./Hello. Ko

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.