Compile and install the Linux kernel 2.6.24 and write a simple kernel driver for testing.

Source: Internet
Author: User

I haven't compiled the kernel for a long time. Recently I want to get the driver. I checked that there are two folders under/usr/src, which are empty and do not have any kernel source code.
I used centos 5.1. In fact, the Red Hat series did not include the Linux kernel source code after the appearance of redora, so these headers won't be available at the moment when you have installed the system.
Of course, the following methods should apply no matter what version of Linux is released.
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:
#! /Bin/bash
Mkdir-P/home/name/build/kernel
CD/usr/src/Linux-Kernel
Make mrproper
Make o =/home/name/build/kernel menuconfig
Make o =/home/name/build/kernel
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 grub. conf file to enable later systems to start with the new kernel:
Vim/etc/grub. conf
The content is as follows:
# Grub. conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# Notice: you do not have a/boot partition. This means that
# All kernel and initrd paths are relative to/, eg.
# Root (hd0, 6)
# Kernel/boot/vmlinuz-version Ro root =/dev/sda7
# Initrd/boot/initrd-version.img
# Boot =/dev/SDA
Default = 2
Timeout = 5
Splashimage = (hd0, 6)/boot/GRUB/splash.xpm.gz
Hiddenmenu
Title centos (2.6.24.4)
Root (hd0, 6)
Kernel/boot/vmlinuz-2.6.24.4 Ro root = label =/rhgb quiet
Initrd/boot/initrd-2.6.24.4.img
Title centos (2.6.18-53. EL5)
Root (hd0, 6)
Kernel/boot/vmlinuz-2.6.18-53.el5 Ro root = label =/rhgb quiet
Initrd/boot/initrd-2.6.18-53.el5.img
Title Windows XP
Rootnoverify (hd0, 0)
Chainloader + 1

Comment out this part:
Title centos (2.6.18-53. EL5)
Root (hd0, 6)
Kernel/boot/vmlinuz-2.6.18-53.el5 Ro root = label =/rhgb quiet
Initrd/boot/initrd-2.6.18-53.el5.img
Change
# Title centos (2.6.18-53. EL5)
# Root (hd0, 6)
# Kernel/boot/vmlinuz-2.6.18-53.el5 Ro root = label =/rhgb quiet
# Initrd/boot/initrd-2.6.18-53.el5.img

Modify default = 0 again. Here 0 corresponds to the first title, and so on.
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:

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

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!

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.