编译并升级Linux内核

测试环境:虚拟机ubuntu-18.04

系统内核版本:4.15.0-20-generic

源码内核版本:linux-4.19.8

 

说明:

第一次是在centos7的虚拟机中做该实验,编译并安装完内核以后,系统无法启动

之后在ubuntu虚拟机中做测试,升级成功

 

  • .config文件说明

/boot目录下的config文件,用于控制内核编译选项,编译内核时必须要有该配置文件

 

make config: 这个要求你手动选择所有配置项,配置完成后生成.config文件

 

make menuconfig: 以curses的图形节目显示配置菜单,当已有.config文件存在时,它会根据.config文件设定默认项.若不存在.config文件,则根据各级Kconfig文件来设定菜单项.完成配置后,生成.config文件.(需要curses库,用“yum install -y ncurses-devel”命令即可安装)

 

make oldconfig:  与make menuconfig相同,区别在于这个没有图形界面,当已有.config文件存在时,它根据.config文件设定默认项,若kconfig有新配置项时,会提示你进行选择;若不存在.config文件,则根据各级Kconfig文件来设定菜单项.完成配置后,生成.config文件.

 

若已存在.config文件, make menuconfig及make oldconfig都会把原.config另存为.config.old.

参考文章:https://blog.csdn.net/jerryoung/article/details/53783538

 

 

  • 编译内核

下载内核源码并解压以后,必须在源码目录里生成.config配置文件,否则make时会出错,无法编译

源码目录里默认是没有.config文件的,可以按以下方式生成该文件:

1、执行“make menuconfig”命令,进入配置界面,再退出,会按源码的默认配置生成一个.config文件

2、执行“make oldconfig”命令,作用与1相同

上面两种方法,编译好内核以后,在虚拟机中无法正常启动(不推荐使用)

3、在已有Linux操作系统中,/boot目录下会有config开头的配置文件,直接拷贝过来,以.config文件命名即可

编译内核make时,需要确认一些命令才可以正常编译(默认选项,直接回车即刻)(推荐使用)

4、执行“make config”命令,手动选择每一项配置后,生成.config文件(专业人员使用,新手还是别用了)

 

生成.config文件以后,在源码目录里直接执行“make”,就开始编译内核了

 

  • 安装模块

make modules_install

安装完成后,会在/lib/modules/目录下生成新版本的内核模块目录

 

  • 安装内核

make install

安装完成后,会在/boot目录下生产新的内核文件

 

我看网上有些文章说的,在这一步以后,还要更新 grub

我自己的测试结果是“make install”以后,grub.conf已经更新了,所以直接重启即可

 

  • 重启

reboot重新启动系统,在启动项中会有新版本内核的选择项

 

注意:虚拟机内存设置过小的话,系统会启动失败,报错:end kernel panic not syncing: system is deadlocked on memory;把内存调大以后,系统启动正常

 

用“uname -r”查看内核版本,显示“4.19.8”,升级内核成功!

 

参考文章:

https://linux.cn/article-9665-1.html

https://blog.csdn.net/baidu_24256693/article/details/80115354

 

  • 出错及解决方案
  • 错误1

root@ubuntu:/usr/src/linux-4.19.8# make

Makefile:595: include/config/auto.conf: No such file or directory

HOSTCC scripts/basic/fixdep

HOSTCC scripts/kconfig/conf.o

YACC scripts/kconfig/zconf.tab.c

/bin/sh: 1: bison: not found

scripts/Makefile.lib:196: recipe for target 'scripts/kconfig/zconf.tab.c' failed

make[2]: *** [scripts/kconfig/zconf.tab.c] Error 127

Makefile:539: recipe for target 'syncconfig' failed

make[1]: *** [syncconfig] Error 2

Makefile:635: recipe for target 'include/config/auto.conf' failed

make: *** [include/config/auto.conf] Error 2

 

apt install -y bison

 

刚开始出错的时候,只关注第一行的出错了(Makefile:595: include/config/auto.conf: No such file or directory),在网上搜了大半天,找不到解决方案;偶然发现一篇文章和我遇到的错误相同,原来直接解决第二条错误就可以了(/bin/sh: 1: bison: not found)

 

  • 错误2

root@ubuntu:/usr/src/linux-4.19.8# make

Makefile:595: include/config/auto.conf: No such file or directory

YACC scripts/kconfig/zconf.tab.c

LEX scripts/kconfig/zconf.lex.c

/bin/sh: 1: flex: not found

scripts/Makefile.lib:188: recipe for target 'scripts/kconfig/zconf.lex.c' failed

make[2]: *** [scripts/kconfig/zconf.lex.c] Error 127

Makefile:539: recipe for target 'syncconfig' failed

make[1]: *** [syncconfig] Error 2

Makefile:635: recipe for target 'include/config/auto.conf' failed

make: *** [include/config/auto.conf] Error 2

 

apt install -y flex

就是缺什么装什么就可以了,参考https://blog.csdn.net/zxhio/article/details/80312316

 

  • 错误3

fatal error: openssl/opensslv.h: No such file or directory

 

apt-get install libssl-dev -y

 

  • 错误4

安装完内核,重启无法启动系统,报错:end kernel panic not syncing: system is deadlocked on memory

 

虚拟机内存设置过小所致,将内存调大以后即可解决

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值