
编辑Makefile版本信息
定义内核特性,生成配置文件.config,用于编译:make xconfig
编译内核:make
安装内核:make install
安装模块:make modules_install
具体步骤如下:
内核配置
先定义内核需要什么特性,并进行配置。内核构建系统(The kernel build system)远不是简单用来构建整个内核和模块,想了解更多的高级内核构建选项,你可以查看 Documentation/kbuild 目录内的内核文档。
可用的配置命令和方式:
make menuconfig
命令:make menuconfig
编译内核
编译和安装内核
编译步骤:
$ cd /usr/src/linux2.6
$ make
安装步骤 (logged as
$ make install
$ make modules_install
提升编译速度
多花一些时间在内核配置上,并且只编译那些你硬件需要的模块。这样可以把编译时间缩短为原来的1/30,并且节省数百MB的空间。另外,你还可以并行编译多个文件:
$ make -j <number>
make 可以并行执行多个目标(target)(KEMIN:前提是目标规则间没有交叉依赖项,这个怎么做到的?)
$ make -j 4
即便是在单处理器的工作站上也会很快,读写文件的时间被节省下来了。多线程让CPU保持忙碌。
number大于4不见得有效了,因为上下文切换过多反而降低的工作的速度。
make -j <4*number_of_processors>
内核编译tips
查看完整的 (gcc, ld)命令行: $ make V=1
清理所有的生成文件 (to create patches...): $ make mrproper
部分编译:$ make M=drivers/usb/serial
单独模块编译:$ make drivers/usb/serial/visor.ko
最终生成的文件
vmlinux 原始内核镜像,非压缩的
arch/<arch>/boot/zImage zlib压缩的内核镜像(Default image on arm)
arch/<arch>/boot/bzImage bzip2压缩的内核镜像。通常很小,足够放入一张软盘(Default image on i386)
编写helloworld.c及其对应的Makefile。helloworld.c:
#include <linux/module.h>#include <linux/kernel.h>int init_hello_module(void)
{
printk("***************Start***************\n")
printk("Hello World! Start of hello world module!\n") return 0
}void exit_hello_module(void)
{
printk("***************End***************\n")
printk("Hello World! End of hello world module!\n")
}
MODULE_LICENSE("Dual BSD/GPL")
module_init(init_hello_module)
module_exit(exit_hello_module)1234567891011121314151617181920
Makefile:
# To build modules outside of the kernel tree, we run "make"# in the kernel source treethe Makefile these then includes this# Makefile once again.# This conditional selects whether we are being included from the# kernel Makefile or not.# called from kernel build system: just declare what our modules areobj-m := helloworld.oCROSS_COMPILE =
CC= gcc# Assume the source tree is where the running kernel was built
# You should set KERNELDIR in the environment if it's elsewhere
KERNELDIR ?= /usr/src/linux-headers-$(shell uname -r)# The current directory is passed to sub-makes as argument
PWD := $(shell pwd)all: modulesmodules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modulesclean:
rm -rf *.o *~ core .depend *.symvers .*.cmd *.ko *.mod.c .tmp_versions $(TARGET)
在Makefile中,在obj-m := helloworld.o这句中,.o的文件名要与编译的.c文件名一致。
KERNELDIR ?= /usr/src/linux-headers-$(shell uname -r)指示当前linux系统内核的源码位置。
linux内核配置与编译相关流程1、清除临时文件、中间文件和配置文件make
clean
不删除配置文件。
make
mrproper
make
distclean
删除编辑的backup文件、补丁文件等2、确定目标系统的软硬件配置情况,比如CPU的类型,网卡的型号,所需要支持的网络协议。3、使用命令配置内核
make
config
基于文本模式的交互配置。
make
menuconfig
基于文本模式的菜单配置。
make
oldconfig
使用已有的配置文件(.config),但是会询问新增的配置选项。
make
xconfig
图形化的配置(需要安装图形化系统)。4、编译内核
make
zImage
make
bzImage
区别:在X86平台上,zImage只能用于小雨512k内核。如果需要获取详细编译信息,则在后面加上V=1.
编译好的内核位于arch/<cpu>/boot/目录下。
5、编译内核模块
make
modues
6、安装内核模块
make
modues_install
将编译好的内核模块从内核源代码目录copy到/lib/modues下。7、制作
init
ramdisk
mkinitrd
$initrd-$version
-$version内核安装(X86)1、cp
arch/X86/boot/bzImage
/boot/vmliuz
-$version2、cp
$initrd
/boot/3、修改etc/grub.conf
或
/etc/lilo.conf$version为所编译的内核版本号。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)