1.准备相关的开发环境 1)Centos6.9
[root@grub6 ~]# yum groupinstall "Development Tools" 安装开发工具包组 [root@grub6 ~]# yum groupinstall "Server Platform Development " 安装服务开发平台包组 [root@grub6 ~]# yum install ncurses-devel 安装ncurses-devel包ps:若未安装ncurses-devel包,后面无法使用make menuconfig 2)Centos7.4
[root@yuki ~]# yum groupinstall "Development Tools" 安装开发工具包组ps:centos7中发现服务开发平台包组已经没有了,并且安装Development Tools发生报错,显示没有没有安装组文件,使用如下方法解决
[root@yuki ~]# yum groups mark install "Development Tools" [root@yuki ~]# yum groups mark convert "Development Tools" [root@yuki ~]# yum groupinstall "Development Tools"2.获取目标主机上硬件设备的相关信息: 1)获取CPU信息:
1.cat /proc/cpuinfo 2. lscpu 3. x86info -a2)查看PCI设备:
1.lspci,支持-v,-vv选项 2.lsusb,支持-v,-vv选项 3.lsblk 4.hal-device 了解全部硬件设备的信息,centos6上默认安装1.获取linux内核源码文件:www.kernel.org
[root@yuki ~]# uname -r 3.10.0-693.el7.x86_64 [root@yuki ~]# ll linux-3.16.68.tar.xz -rw-r--r--. 1 root root 73304476 5月 26 01:16 linux-3.10.67.tar.xz2.解压缩源代码文件到指定目录:/usr/src
[root@yuki ~]# tar -xf linux-3.16.68.tar.xz -C /usr/src/3.软链接解压后的文件
[root@yuki src]# cd /usr/src/ [root@yuki src]# ls debug kernels linux linux-3.16.68 [root@yuki src]# ln -sv linux-3.16.68 linux "linux" -> "linux-3.16.68" [root@yuki src]# ls debug kernels linux linux-3.16.684.复制当前系统内核的配位模板文件到新内核目录下面
[root@yuki src]# cd linux [root@yuki linux]# cp /boot/config-3.10.0-693.el7.x86_64 ./.config cp:是否覆盖"./.config"? y [root@yuki linux]# ls ./.config ./.config5.配置内核选项 1)支持“更新”模式进行配置,在已有的.config文件的基础上进行“修改”配置
1.make config:基于命令行遍历的方式去配置内核中可配置的每个选项 2."make menuconfig:基于cureses的文本配置窗口" 一般使用此选项即可 3.make gconfig:基于GTK开发环境的窗口使用,对应包组为“桌面平台开发” 4.make xonfig:基于QT开发环境的窗口界面ps:使用make menuconfig进入如下界面,进行相应配置,此次测试设定的local version为3.16.683.10.68.1-el7
[root@yuki linux]# make menuconfig2)支持“全新配置”模式进行配置
1.make defconfig:基于内核为目标平台提供的“默认配置为模板进行配置” 2.make allnoconfig:所有选项都为No,可用于个性化定制6.编译内核:make [-j #],可使用-j指定编译线程数量
进入screen后让其自动编译 [root@yuki linux]# screen [root@yuki linux]# make -j 4 [detached from 9273.pts-0.yuki]ps:为防止出现终端机网络波动出现掉线,导致编译内核被终止,一般使用screen命令
screen命令 1.打开screen:screen 2.拆除screen:ctrl+a,之后按d 3.列出当前screen:screen ls 4.连接至screen:screen -r SCREEN_ID 5.关闭screen:exit7.安装内核模块,此时已经能够看到编译好的模块
[root@yuki ~]# make modules_install [root@yuki ~]# cat /proc/sys/kernel/hostname yuki [root@yuki ~]# ls /lib/modules 3.10.0-693.el7.x86_64 3.16.683.10.68.1-el7 [root@yuki ~]# ls /lib/modules/3.16.683.10.68.1-el7/ build modules.alias.bin modules.dep modules.order modules.symbols.bin kernel modules.builtin modules.dep.bin modules.softdep source modules.alias modules.builtin.bin modules.devname modules.symbols8.安装内核,已经能够看到对应的vmlinuz,initramfs..
[root@yuki ~]# make install [root@yuki ~]# ls /boot config-3.10.0-693.el7.x86_64 efi grub grub2 initramfs-0-rescue-8484df7a12254b9b8911a14c29af325a.img initramfs-3.10.0-693.el7.x86_64.img initramfs-3.16.683.10.68.1-el7.img initrd-plymouth.img symvers-3.10.0-693.el7.x86_64.gz System.map System.map-3.10.0-693.el7.x86_64 System.map-3.16.683.10.68.1-el7 vmlinuz vmlinuz-0-rescue-8484df7a12254b9b8911a14c29af325a vmlinuz-3.10.0-693.el7.x86_64 vmlinuz-3.16.683.10.68.1-el79.重启系统,此时已经能看到刚编译过的内核,选择使用新内核
[root@yuki ~]# vim /etc/grub2.cfg grub2的配置文件 [root@yuki ~]# vim /etc/default/grub 设置默认选项 [root@yuki ~]# uname -r 3.16.683.10.68.1-el71.多线程编译:make [-j #]
2.编译内核中的一部分代码 1)只编译某子目录中的相关代码
1.cd /usr/src/linux 2.make path/to/dir/2)只编译一个特定的模块
1.cd /usr/src/linux 2.make path/to/dir/file.lo3)实现交叉编译
目标平台与当前编译操作所在不同平台 make ARCH=arch_name 要获取特定目标平台的使用帮助 make ARCH=arch_name help1.在执行过编译操作的内核源码树上做重新编译
2.事先清理操作 1)make clean:清理编译生成的绝大多数文件,但会保留config,及编译外部模块所需要的文件
2)make mrproper:清理编译生成的所有文件,包括配置生成的config文件及某些备份文件
3)make distclean:相当于mrproper,额外清理各种patches以及编辑器备份文件