linux 4.1内核源码编译

编译环境

Arch Linux on VirtualBox

下载内核

https://www.kernel.org/

下载的内核压缩包,此时的最新内核版本为4.1:

解压包

# tar -xvJf linux-4.1.tar.xz

在这里为了方便直接切换到su权限了,如果为普通权限,须在每条命令前加上sudo,如果sudo可用的话。

(命令语句中划线的部分为文件名或目录名,是要根据实际情况更改的部分,后面也会遇到很多)

cd到源码根目录

# cd ~/Documents/linux-4.1

进行设置

这里使用了当前运行内核的默认设置。

拷贝正在运行的内核的配置文件(.config文件)到编译根目录

# zcat /proc/config.gz > .config

 

当然也可以手动设置

扫描二维码关注公众号,回复: 5302130 查看本文章
# sudo make menuconfig

(末尾参考资料一中有详细的说明)

编译内核

# sudo make [-j 2]

后面的参数-j2表示,cpu要开启两个线程来编译内核,可以加快编译速度。

编译完成后的内核文件:

[root@localhost linux-4.1.18]# ll arch/x86/boot/

安装模块

指定:驱动模块的安装目录
    [root@localhost linux-4.1.18]# make modules_install INSTALL_MOD_PATH=/home/test/tmp/
    [root@localhost tmp]# ls lib/
    firmware  modules

# sudo make modules_install

 

将编译好的内核拷贝到/boot目录

# cp -v arch/x86/boot/bzImage /boot/vmlinuz-YourKernelName

# cp -v arch/x86/boot/bzImage /boot/vmlinuz-YourKernelName

在这里YourKernelName为4.1:

# cp -v arch/x86/boot/bzImage /boot/vmlinuz-4.1

# cp -v arch/x86/boot/bzImage /boot/vmlinuz-4.1

制作初始化内存盘

# mkinitcpio -k FullKernelName /etc/mkinitcpio.conf -g /boot/initramfs-YourKernelName.img 

可以通过 ls /lib/modules 命令查询到可用的FullKernelName:

在这里FullKernelName为4.1.0-ARCH(如上图):

# mkinitcpio -k 4.1.0-ARCH -c /etc/mkinitcpio.conf -g /boot/initramfs-4.1.img 

[拷贝System.map]

# cp System.map /boot/System.map-YourKernelName

在这里YourKernelName为4.1:

# cp System.map /boot/System.map-4.1
# ln -sf /boot/System.map-YourKernelName /boot/System.map

完成了这部操作后,/boot目录如下图所示:

在这里vmlinuz-linux为当前内核,vmlinuz-4.1为新编译的内核。initramfs-4.1.img为新的初始化内存盘,还有刚拷贝的System.map-4.1文件以及System.map链接。

下面我们需要对新的内核文件和内存盘进行引导,以启动新内核。

设置启动项

 # grub-mkconfig -o /boot/grub/grub.cfg

我们可以看到grub自动添加了新的内核,新的内核启动选项在"Advanced options for Arch Linux"中。

重启

# reboot 0

编译前后内核对比

编译前:

猜你喜欢

转载自blog.csdn.net/yangguangmeng/article/details/83341255