CentOS 5.6下将linux内核2.6.18升级到2.6.30,并编译动态加载linux下第一个模块

来源:互联网 发布:台州淘宝司法拍卖网 编辑:程序博客网 时间:2024/05/16 10:24

希望各位大神勿喷,本人新手:

1.升级linux内核

1).cd  /usr/src 

从www.kernel.org/pub/linux/v2.6/linux-2.6.30.tar.bz2,

tar  xjvf  linux-2.6.30.tar.b2z 在当前目录下就会自动生成一个linux-2.6.30的目录,

cd linux-2.6.30

make mrproper 清除环境变量

make menuconfig 选择要编译的内核模块(可参考鸟哥的linux的私房菜,根据自己需要,选项越多,编译时间越长)

make clean 确保所以东西都是最新的

make bzImage 生成内核文件

make modules 编译内核模块

make modules_install 安装内核模块

make install 这一步可以自动将产生的镜像复制到/boot目录下

mkinitrd /boot/initrd-2.6.30.img   2.6.30 根据内核版本和指定参数生成映像文件

cp arch/x86/boot/bzImage   /boot/vmlinuz-2.6.30

cp /usr/src/linux-2.6.30/System.map   /boot/System.map-2.6.30

2).编辑/etc/grub.conf,添加如下的信息,并将default =1改为default =0

# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/VolGroup00/LogVol00
#          initrd /initrd-version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.18-194.el5)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-194.el5 ro root=/dev/VolGroup00/LogVol00 rhgb quiet
        initrd /initrd-2.6.18-194.el5.img


title CentOS (2.6.30)
        root (hd0,0)
        kernel /vmlinuz-2.6.30 ro root=/dev/VolGroup00/LogVol00 rhgb quiet
        initrd /initrd-2.6.30.img

3).init 6,重启。

4).uname -r,显示的是2.6.30

升级成功!!!!!

2.编译和加载linux的一个内核模块

1).准备源代码

__module_address

#include <linux/modules.h>

#include <linux/init.h>

MODULES_LICENSE("GPL");

static int  __init  __module_address_init(void);

static void __exit __module_address_exit(void);

int a_module()

{

return 0;

int  __init  __module_address_init(void)

{

struct  module *ret;

unsigned long addr = (unsigned long)a_module;

preempt_disable();

ret = __module_address(addr);

preempt_enable();

if(ret != NULL)

{

printk("<0>ret->name:%s\n",ret->name);

}

else 

{

printk("<0>__module_address return NULL!\n");

}

return 0;

}

void __exit __module_address_exit(void)

{

printk("<0>module exit ok!\n");

}

module_init(__module_address_init);

module_exit(__module_address_exit);

2).编写Makefile文件

Makefile

if($(KERNELRELEASE),)

obj-m := module_address.o

module_address-objs := __module_address.o

else

KVER :=  $(shell uname -r)

KDIR :=  /lib/modules/$(KVER)/build

PWD := $(shell pwd)

default:

$(MAKE)  -C$(KDIR) M=$(PWD)modules

clean:

rm -rf *.o *.mod.c *.mod.o*.order *.symvers*markers

endif

3).编译模块

进入源代码所在的目录直接下make命令即可。

4).加载和卸载模块

insmod module_address.ko

这时回到一个输出内容

rmmod module_address

也会看到提示内容

5).你也可以在/proc/modules下找到你加载的模块

cat /proc/modules