实例:编译Linux内核3.18.25概要
前提:
将CentOS 7上3.10.0-229.el7.x86_64重新编译一个3.18.25内核
自编译安装,按照自己的硬件平台架构编译,能最适合硬件主机的性能
且按照需要编译模块,可以选择自己需要的包编译
[root@localhost tmp]# uname -a
一、编译前准备工作
1.宿主机系统
本次编译宿主机:VMware Workstation Pro12 上的CentOS 7.0操作系统
2.开发环境(开发工具,开发库),头文件
[root@localhost yum.repos.d]# yum -y groupinstall "开发工具" "服务器平台开发"
[root@localhost yum.repos.d]# yum -y groupinstall "Development Tools" "Server PlatformDevelopment"
[root@localhostyum.repos.d]# yum install -y ncurses-devel
包组:Development Tools、Server Platform Development
CentOS 7上还要特备注意要安装包:ncurses-devel包
3.获取目标主机上硬件设备的相关信息
(1)CPU:
~]#cat /proc/cpuinfo
~]#lscpu
~]# x86info -a:默认未安装,显示很详细的CPU信息
(2)PCI设备总线:显卡、网卡… ...
~]# lspci -v:详细信息 -vv:更加详细的信息
~]# lsusb:查看usb设备 -v -vv
~]# lsblk:查看硬盘设备
(3)hal-device:了解全部硬件设备信息(CentOS 6)
4.获取到目标主机系统功能的相关信息,例如要启用的文件系统
5.获取内核源代码包kernel源码
www.kernel.org下载内核源代码3.18.25.el7.x86_64.tar,需要转换成二进制格式才能使用
二、编译安装内核源码
1.解压源码:linux-3.18.25.tar
[root@localhost/]# tar xf /tmp/linux-3.18.25.tar.xz -C /usr/src
[root@localhost/]# cd /usr/src
[root@localhostsrc]# ln -sv linux-3.18.25 linux
注意:
1) 源码应该解压放置在/usr/src或者/usr/local/src下,系统在实现内核编译时特别建议放置在/usr/src目录
2) 编译驱动时候,系统会找/usr/src/linux目录,自己解压会带版本号
建议用连接方式创建,方便更换linux kernel时候的能够回滚,容易管理
2.配置内核选项
(1)make参数:[root@localhost linux]#make help
Cleaning targets: | 清理目标 |
clean - Remove most generated files but keep theconfig and
enough build support to build external modules
mrproper - Remove all generated files + config +various backup files
distclean - mrproper + remove editor backup and patchfiles
Configuration targets: | 目标配置 |
config - Update current config utilising aline-oriented program
nconfig - Update current config utilising ancurses menu based program
menuconfig - Update current config utilising a menubased program
xconfig - Update current config utilising a QT basedfront-end
gconfig - Update current config utilising a GTK basedfront-end
oldconfig - Update current config utilising a provided.config as base
localmodconfig - Update current config disabling modules notloaded
localyesconfig - Update current config converting local modsto core
silentoldconfig - Same as oldconfig, butquietly, additionally update deps
defconfig - New config with default from ARCH supplieddefconfig
savedefconfig - Save current config as ./defconfig(minimal config)
allnoconfig - New config where all options are answeredwith no
allyesconfig - New config where all options are acceptedwith yes
allmodconfig - New config selecting modules when possible
alldefconfig - New config with all symbols set todefault
randconfig - New config with random answer to alloptions
listnewconfig - List new options
olddefconfig - Same as silentoldconfig but sets newsymbols to their default value
kvmconfig - Enable additional options for guest kernelsupport
tinyconfig - Configure the tiniest possible kernel
Other generic targets: | 其他的通用目标 |
all - Build all targets marked with [*]
*vmlinux - Build the bare kernel
*modules - Build all modules
modules_install - Install all modules toINSTALL_MOD_PATH (default: /)
firmware_install- Install all firmware toINSTALL_FW_PATH
(default:$(INSTALL_MOD_PATH)/lib/firmware)
dir/ - Build all files in dir and below
dir/file.[oisS] - Build specified target only
dir/file.lst - Build specified mixed source/assemblytarget only
(requires a recent binutilsand recent build (System.map))
dir/file.ko - Build module including final link
modules_prepare - Set up for buildingexternal modules
tags/TAGS - Generate tags file for editors
cscope - Generate cscope index
gtags - Generate GNU GLOBAL index
kernelrelease - Output the release version string (use withmake -s)
kernelversion - Output the version stored in Makefile (usewith make -s)
image_name - Output the image name (use with make -s)
headers_install - Install sanitised kernelheaders to INSTALL_HDR_PATH
(default: ./usr)
Static analysers | 静态分析器 |
checkstack - Generate a list of stack hogs
namespacecheck - Name space analysis on compiled kernel
versioncheck - Sanity check on version.h usage
includecheck - Check for duplicate included header files
export_report - List the usages of all exported symbols
headers_check - Sanity check on exported headers
headerdep - Detect inclusion cycles in headers
coccicheck - Check with Coccinelle.
Kernel selftest | 内核自我测试 |
kselftest - Build and run kernel selftest (run asroot)
Build, install, and bootkernel before
running kselftest on it
Kernel packaging: | 内核打包工具 |
rpm-pkg - Build both source and binary RPMkernel packages
binrpm-pkg - Build only the binary kernelpackage
deb-pkg - Build the kernel as a debpackage
tar-pkg - Build the kernel as anuncompressed tarball
targz-pkg - Build the kernel as a gzipcompressed tarball
tarbz2-pkg - Build the kernel as a bzip2compressed tarball
tarxz-pkg - Build the kernel as a xzcompressed tarball
perf-tar-src-pkg - Build perf-3.18.25.tar source tarball
perf-targz-src-pkg - Build perf-3.18.25.tar.gz source tarball
perf-tarbz2-src-pkg - Buildperf-3.18.25.tar.bz2 source tarball
perf-tarxz-src-pkg - Build perf-3.18.25.tar.xz source tarball
Documentation targets: | 目标文档 |
Linux kernel internal documentation indifferent formats:
htmldocs - HTML
pdfdocs - PDF
psdocs - Postscript
xmldocs - XML DocBook
mandocs - man pages
installmandocs - install man pages generated by mandocs
cleandocs - clean all generated DocBook files
Architecture specific targets (x86): | 平台特有的编译目标 |
*bzImage - Compressed kernel image(arch/x86/boot/bzImage)
install - Install kernel using
(your) ~/bin/installkernel or
(distribution)/sbin/installkernel or
install to $(INSTALL_PATH)and run lilo
fdimage - Create 1.4MB boot floppy image (arch/x86/boot/fdimage)
fdimage144 - Create 1.4MB boot floppy image (arch/x86/boot/fdimage)
fdimage288 - Create 2.8MB boot floppy image (arch/x86/boot/fdimage)
isoimage - Create a boot CD-ROM image (arch/x86/boot/image.iso)
bzdisk/fdimage*/isoimage alsoaccept:
FDARGS="..." arguments for the booted kernel
FDINITRD=file initrd for thebooted kernel
x86_64_defconfig - Build for x86_64
i386_defconfig - Build for i386
make V=0|1 [targets] 0 => quiet build(default), 1 => verbose build
make V=2 [targets] 2 => give reason for rebuild of target
make O=dir [targets] Locate all output filesin "dir", including .config
make C=1 [targets] Check all c source with $CHECK (sparse by default)
make C=2 [targets] Force check of all c source with $CHECK
make RECORDMCOUNT_WARN=1 [targets] Warn aboutignored mcount sections
make W=n [targets] Enable extra gcc checks, n=1,2,3 where
1:warnings which may be relevant and do not occur too often
2:warnings which occur quite often but may still be relevant
3:more obscure warnings, can most likely be ignored
Multiplelevels can be combined with W=12 or W=123
Execute"make" or "make all" to build all targets marked with [*]
Forfurther info see the ./README file
(2)编译配置:Configuration targets
1)支持“更新”模式进行配置:在已有的.config文件的基础之上进行“修改”配置;
make config:基于命令行以遍历的方式去配置内核中可配置的每个选项;
make menuconfig:基于cureses的文本配置窗口
make gconfig:基于GTK开发环境的窗口界面; 包组“桌面平台开发”
make xonfig:基于QT开发环境的窗口界面;
2)支持“全新配置”模式进行配置:
make defconfig:基于内核为目标平台提供的“默认”配置为模板进行配置;
make allnoconfig:所有选项均为“no”;
3.编译 (# cd /usr/src/linux目录下)
(1)多线程编译:make [-j #]
-j # :以#个线程编译
(2)编译内核中的一部分代码:编译之后要手动移动到指定的目录/lib/modules/*
1)只编译某子目录中的相关代码:# make path/to/dir/
2)只编译一个特定的模块:# make path/to/dir/file.ko
(3)实现交叉编译:目标平台与当前编译操作所在的平台不同;
# make ARCH=arch_name
要获取特定目标平台的使用帮助:# make ARCH=arch_name help
4.安装内核的模块:]# make modules_install
安装会在/lib/modules/目录下生成一个与内核版号一致的目录
5.安装内核核心:~]# make install
(1)实际过程:
实际上是安装/usr/src/linux/arch/x86/boot/bzImage文件为内核核心文件
/usr/src/linux/arch/x86_64/boot/bzImage-> ../../x86/boot/bzImage是其链接文件
64位兼容32位,所以统一安装在/usr/src/linux/arch/x86目录下
(2)make install 命令自动安装执行操作
1) 自动安装在/usr/src/linux/arch/x86/boot/bzImage内核核心文件:vmlinuz-3.18.25
2) 在boot/grub.conf下创建目录下title条目
3) 创建ramdisk文件:initramfs-3.18.25.img
注意:Centos 7上的grub2,相对于CentOS5/6的grub来说是完全重新设计的理念
三、重启系统选择内核
1.reboot之后会在grub选项中发现多了新编译的内核文件
2.版本号查看验证
[root@localhost ~]# uname -r
3.18.25
四、注意事项和技巧
1.screen:一个终端上打开多个屏幕(桌面)
-screen managerwith VT100/ANSI terminal emulation在终端模拟器上打开屏幕
远程连接时编译,终端万一断开,则会停止编译出错,所以make过程运行在screen命令下运行
此时,不会因为远程连接ssh断开而终止,出现错误
screen命令:
打开screen:~]# screen
拆除screen:Ctrl+a, d
列出screen:~]# screen -ls
连接至screen:~]# screen -r SCREEN_ID
关闭screen: ~]# exit
2.事先清理操作:在执行过编译操作的内核源码树上做重新编译:
#make clean:清理编译生成的绝大多数文件,但会保留config,及编译外部模块所需要的文件;
#make mrproper:清理编译生成的所有文件,包括配置生成的config文件及某些备份文件;
#make distclean:相当于mrproper,额外清理各种patches以及编辑器备份文件;
3.仿照当前系统上的内核配置作为,更改部分自己所需的配置
(1)RHEl系列会在/boot目录下放置当前系统的config文件以便学习
/boot/config-3.10.0-229.el7.x86_64文件
可以将当前文件cp到/usr/src/linux下,以.confiig命名成可执行的配置文件
(2)非RHEL系列:一般内核自动输出的文件
/proc/config.gz解压即可当做模板使用
五、内核编译过程思路总结:
~]# tar xf ########## -C /usr/src | 解压源码包 |
~]# cd /usr/src | 进入src系统编译内核时建议安装目录 |
~]# ln -s ########### linux | 创建连接,方便管理多个内核时支持回滚 |
~]# cd linux | 切换到连接文件路径,/usr/src/linux |
~]# cp /boot/config-####### /usr/src/linux/.config | 复制当前系统内核编译模块作为模板,方便操作 |
~]# make menuconfig | 配置内核选项,选择便是进内核、不编译或者编译成模块 |
~]# make [-j #] | 编译内核,可使用-j指定编译线程数量 虚拟机上不要超过物理机上的线程数量 |
~]# make modules_install | 安装内核的模块 |
~]# make install | 安装内核核心 |
重启系统,选择使用新内核 |
只编译内核部分内容 | |
~]# cd /usr/src | 进入src系统编译内核时建议安装目录 |
~]# make path/to/dir/ # make path/to/dir/file.ko | 编译目录上的某一模块或者某一目录下的所有模块 |
# make ARCH=arch_name # make ARCH=arch_name help | 交叉编译 获取关于交叉编译平台的帮助信息 |
编译内核时对CPU负载会很大: | [此时为4线程编译] |
本文出自 “许鼎的博客” 博客,请务必保留此出处http://xuding.blog.51cto.com/4890434/1730636