文章
2023/2/16
CentOS7更新内核是否需要手动更新grub.cfg的证明
2023-02-16
无标签

安装#

先看一下当前的启动项:

[root@localhost ~]# cat /boot/grub2/grub.cfg /boot/efi/EFI/centos/grub.cfg | grep "menuentry '"
menuentry 'CentOS Linux (3.10.0-693.el7.x86_64) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-693.el7.x86_64-advanced-ff03f518-c736-4ae6-9246-8184ca323e79' {
menuentry 'CentOS Linux (0-rescue-98762484ab64473dadd99e91d05811b6) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-0-rescue-98762484ab64473dadd99e91d05811b6-advanced-ff03f518-c736-4ae6-9246-8184ca323e79' {

有两个

安装新内核

[root@localhost ~]# yum install ./kernel-ml-6.1.12-1.el7.elrepo.x86_64.rpm
已加载插件:fastestmirror, langpacks
正在检查 ./kernel-ml-6.1.12-1.el7.elrepo.x86_64.rpm: kernel-ml-6.1.12-1.el7.elrepo.x86_64
./kernel-ml-6.1.12-1.el7.elrepo.x86_64.rpm 将被安装
正在解决依赖关系
--> 正在检查事务
---> 软件包 kernel-ml.x86_64.0.6.1.12-1.el7.elrepo 将被 安装
--> 解决依赖关系完成
依赖关系解决
==============================================================================================================================================================================================
Package 架构 版本 源 大小
==============================================================================================================================================================================================
正在安装:
kernel-ml x86_64 6.1.12-1.el7.elrepo /kernel-ml-6.1.12-1.el7.elrepo.x86_64 284 M
事务概要
==============================================================================================================================================================================================
安装 1 软件包
总计:284 M
安装大小:284 M
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : kernel-ml-6.1.12-1.el7.elrepo.x86_64 1/1
grubby fatal error: unable to find a suitable template
验证中 : kernel-ml-6.1.12-1.el7.elrepo.x86_64 1/1
已安装:
kernel-ml.x86_64 0:6.1.12-1.el7.elrepo
完毕!
[root@localhost ~]#

这里出现了一个错误,无法更新grub

grubby fatal error: unable to find a suitable template

所以需要 手动更新grub.cfg ,先要确定启动方式是传统引导还是uefi

ls /boot/efi/EFI

如果目录存在,那么就是uefi,运行

grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfg

如果不存在那就是传统引导

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

看一下当前的启动项,已经多了一个了

[root@localhost ~]# cat /boot/grub2/grub.cfg /boot/efi/EFI/centos/grub.cfg | grep "menuentry '"
cat: /boot/grub2/grub.cfg: 没有那个文件或目录
menuentry 'CentOS Linux (6.1.12-1.el7.elrepo.x86_64) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-6.1.12-1.el7.elrepo.x86_64-advanced-ff03f518-c736-4ae6-9246-8184ca323e79' {
menuentry 'CentOS Linux (3.10.0-693.el7.x86_64) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-693.el7.x86_64-advanced-ff03f518-c736-4ae6-9246-8184ca323e79' {
menuentry 'CentOS Linux (0-rescue-98762484ab64473dadd99e91d05811b6) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-0-rescue-98762484ab64473dadd99e91d05811b6-advanced-ff03f518-c736-4ae6-9246-8184ca323e79' {

将grub2启动项设置成第一个(新内核6.1.12-1.el7.elrepo.x86_64)然后重启

grub2-set-default 0
reboot

升级挂了怎么办? 卸载!#

给服务器接个键盘,开机的时候选第二个启动项启动(旧内核)

卸载新内核

[root@localhost ~]# yum list installed | grep kernel-
kernel-ml.x86_64 6.1.12-1.el7.elrepo installed
kernel-tools.x86_64 3.10.0-693.el7 @anaconda
kernel-tools-libs.x86_64 3.10.0-693.el7 @anaconda

刚刚安装的就是第一个,删掉

yum remove kernel-ml.x86_64

按照上面 手动更新grub.cfg 后重启即可修复

如果咩咩的文章对您有帮助,您可以 请我喝牛奶