自己從新安裝一個就是,現下載:
www.kernel.org下載,
下載後解壓到/usr/src下:
bzip2 -dc linux-2.6.24.4.tar.bz2 | tar xvf -
把文件夾linux-2.6.24.4改名字爲:linux-kernel
隨便在哪個文件夾下面,編寫一個shell文件名叫a.sh,內容如下:
- #! /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
bash a.sh
shell文件裏面O=/home/name/build/kernel表示編譯配置在=號後面的文件夾裏面進行,中間可以自己查看一下。
後面的過程除了在配置內核的時候,都可以不管了。
這個過程會自動生成新的的內核啓動映象,並且自動複製到/boot目錄下面去,不用手動複製了。
之後修改menu.lst文件,讓以後的系統用新的的內核啓動:
vim /etc/grub/menu.lst
內容如下:
- 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
- 把這一部分註釋掉:
- 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 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
- 再修改default = 0,這裏0對應第一個title,下面一次類推
- 重啓就可以了。
編譯內核的輸出文件太大了,就是開始的/home/name/build/kernel,把這個文件夾可以刪除了最後。要寫驅動的話,不要刪除。
寫一個最簡單的驅動程序: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");
然後寫一個Makefile
如下:
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
打開終端進入到hello.c路徑下make,2.6的內核好像不支持用gcc直接編譯了,要用make,如下:
[root@BTazuo hello]# dir
hello.c Makefile
[root@BTazuo hello]# make //編譯
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/hello.o
Building modules, stage 2.
MODPOST 1 modules
CC /azuo/hello/hello.mod.o
LD [M] /azuo/hello/hello.ko
make[1]: Leaving directory `/usr/src/linux-2.6.24.4'
[root@BTazuo hello]# dir //編譯成功
hello.c hello.ko hello.mod.c hello.mod.o hello.o Makefile Module.symvers
[root@BTazuo hello]#
加載和卸載驅動:
打開/var/log/messages文件可以看到,最後有內核加載和卸載的信息:
Hello World enter
Hello World exit
表示內核加載和卸載成功!
這個是從別的地方截過來的,看了對現在學習和安裝Linux的內核有些幫助!學習一下。
- [root@BTazuo hello]# insmod ./hello.ko
- [root@BTazuo hello]# rmmod ./hello.ko