内核编译

当特定环境需要,为了提升服务器性能或支持特定的硬件设备,我看可以选择裁剪内核和特定模块的编译。而编译内核则需要以下几个步骤

一、环境配置

1、最基本的gcc编译器
2、openssle-devel包
3、 ncurses包(处理图形界面配置)
4、kernel源码文件(www.kernel.org获得最新稳定版本,但不建议编译夸多个大版本的内核,因为有可能编译完成并使用之后,很有可能会出现问题。而先已知的redhat6使用的是2.6版本,redhat7使用的是3.10版本,最新的kernel版本是4.14)

我们直接yum install 这些工具
yum install -y openssl-devel ncurses gcc libelf-dev libelf-devel

二、编译安装的指令

.config:准备文本配置文件(当时用meke menuconfig之后会在kernel的源码文件目录生成这个文件,期内就是保存的你预编译的内核的选项信息)
make menuconfig:配置内核选项
make [-j #]    有几个cpu j后就跟几,指用几个cpu进行编译,可以提升编译速度
make modules_install:安装模块
make install :安装bzImage为/boot/vmlinuz-VERSION-RELEASE
make gconfig:基于GTK (GNOME)环境窗口界面
make xconfig:基于QT(KDE)环境的窗口界面支持“全新配置”模式进行配置
make defconfig:基于内核为目标平台提供的“默认”配置进行配置
make allyesconfig: 所有选项均回答为“yes“
make allnoconfig: 所有选项均回答为”no“
编译内核的一部分功能:

只编译某子目录中的相关代码
cd /usr/src/linux
make dir/
只编译一个特定的模块
make dir/file.ko
例如:只为e1000编译驱动:
make drivers/net/ethernet/intel/e1000/e1000.ko

如何交叉编译内核:
编译的目标平台与当前平台不相同
make ARCH=arch_name
要获取特定目标平台的使用帮助
make ARCH=arch_name help
make ARCH=arm help
make clean:清理大多数编译生成的文件,但会保留config文件等
make mrproper: 清理所有编译生成的文件、config及某些备份文件
make distclean:mrproper、patches以及编辑器备份文件

三、编译安装内核步骤

  1. tar xf linux-4.14.10.tar.xz -C /usr/src #解压新的内核到/usr/src下
  2. cd /usr/src #cd 到内核的目录
  3. ln -sv linux-4.14.10 linux #创建内核目录的软连接
  4. cd /usr/src/linux #进入内核目录
  5. cp /boot/config-$(uname -r) ./.config #拷贝boot下的config-version 文件到当前目录,并重命名为.config (我们使用的centos系统在安装好之后,在boot目录下都有一份内核预编译的配置文件,我们可以基于这个文件来进行增减)
  6. #make help #make命令的帮助
  7. make menuconfig #使用图形界面来预配置内核选项
  8. make -j 2 #使用 2个cpu来编译(这个根据实际的cpu数量来定,影响编译时间)
  9. make modules_install #安装内核的外部模块到/lib/modules的指定路径
  10. make install #安装编译好的内核文件到/boot目录下

下面我们详细说一下make menuconfig环节,内核必选的几个功能,否则机器无法启动。更详细的内核参数需要自行去查阅资料了。

四、配置编译内核的环境

我们将kernel源码文件下载并解压到root的家目录

  1. [root@newhostname ~]# tar -xf linux-4.14.10.tar.xz
  2. [root@newhostname ~]# cd linux-4.14.10/

现在配置预编译环境,我们使用make menuconfig

  1. [root@newhostname ~]# make menuconfig

输入命令行会出现一个图形化的字符配置。

我们要实现内核的裁剪,来缩减内核第体积,剔除无用的功能,达到优化系统的目的, 但是需要考虑到核心的功能,如果没有核心的功能,系统将无法启动,那么目的也就实现不了了。以下的这些内核功能必须存在,否则内核将无法启动:

确保引导系统的每一个至关重要的驱动(比如SCSI控制器,等等)是编译进内核而不是作为一个模块,否则系统将无法完全引导。

  1. #KERNEL 启用devtmpfs支持
  2. Device Drivers --->
  3. Generic Driver Options --->
  4. [*] Maintain a devtmpfs filesystem to mount at /dev
  5. [ ] Automount devtmpfs at /dev, after the kernel mounted the rootfs
  1. #KERNEL Enabling SCSI disk support
  2. Device Drivers --->
  3. SCSI device support --->
  4. <*> SCSI disk support
  1. KERNEL 选择必须的文件系统
  2. File systems --->
  3. <*> Second extended fs support
  4. <*> The Extended 3 (ext3) filesystem
  5. <*> The Extended 4 (ext4) filesystem
  6. <*> Reiserfs support
  7. <*> JFS filesystem support
  8. <*> XFS filesystem support
  9. <*> Btrfs filesystem support
  10. DOS/FAT/NT Filesystems --->
  11. <*> MSDOS fs support
  12. <*> VFAT (Windows-95) fs support
  13. Pseudo Filesystems --->
  14. [*] /proc file system support
  15. [*] Tmpfs virtual memory file system support (former shm fs)

如果使用PPPoE连接到互联网,或者是拨号调制解调器,则启用下面的选项

  1. KERNEL 选择PPPoE所需要的驱动
  2. Device Drivers --->
  3. Network device support --->
  4. <*> PPP (point-to-point protocol) support
  5. <*> PPP support for async serial ports
  6. <*> PPP support for sync tty ports

这两个压缩选项将是无害的,但是它们并不是核心的选项,包括基于以太网的PPP选项也是一样,只有在配置内核模式PPPoE时才会需要不要忘记选择内核中的网卡。

大多数系统会有多核心处理,所以激活“Symmetric multi-processing support”是重要的

  1. KERNEL 激活SMP支持
  2. Processor type and features --->
  3. [*] Symmetric multi-processing support

在多核心系统中,每一个核心计作一个处理器。

如果使用USB输入设备(比如键盘和鼠标)或其他USB设备,不要忘记启用那些

  1. HID support --->
  2. -*- HID bus support
  3. <*> Generic HID driver
  4. [*] Battery level reporting for HID devices
  5. USB HID support --->
  6. <*> USB HID transport layer
  7. [*] USB support --->
  8. <*> xHCI HCD (USB 3.0) support
  9. <*> EHCI HCD (USB 2.0) support
  10. <*> OHCI HCD (USB 1.1) support

如果应该需要支持32位程序(CONFIG_IA32_EMULATION),请确保选择IA32仿真该选项,在现在来看,这个选项在大多数系统内是必要的。

  1. KERNEL Selecting processor types and features
  2. Processor type and features --->
  3. [ ] Machine Check / overheating reporting
  4. [ ] Intel MCE Features
  5. [ ] AMD MCE Features
  6. Processor family (AMD-Opteron/Athlon64) --->
  7. ( ) Opteron/Athlon64/Hammer/K8
  8. ( ) Intel P4 / older Netburst based Xeon
  9. ( ) Core 2/newer Xeon
  10. ( ) Intel Atom
  11. ( ) Generic-x86-64
  12. Executable file formats / Emulations --->
  13. [*] IA32 Emulation

如果以前在分区磁盘时使用GPT分区标签支持,请启用GPT分区标签支持

  1. -*- Enable the block layer --->
  2. Partition Types --->
  3. [*] Advanced partition selection
  4. [*] EFI GUID Partition support

如果使用UEFI引导系统,请在Linux内核中启用 Enable EFI stub support

  1. KERNEL Enable support for UEFI
  2. Processor type and features --->
  3. [*] EFI runtime service support
  4. [*] EFI stub support
  5. [*] EFI mixed-mode support
  6. Firmware Drivers --->
  7. EFI (Extensible Firmware Interface) Support --->
  8. <*> EFI Variable Support via sysfs

以上的参考文献:

https://wiki.gentoo.org/wiki/Kernel/Configuration
https://wiki.gentoo.org/wiki/Handbook:X86/Installation/Kernel

五、卸载内核

除/lib/modules/目录下不需要的内核库文件
删除/usr/src/linux/目录下不需要的内核源码
删除/boot目录下启动的内核和内核映像文件
更改grub的配置文件,删除不需要的内核启动列表

posted @ 2018-02-28 21:19  莫孟林  阅读(545)  评论(0编辑  收藏  举报