news 2026/6/23 21:17:47

linux系统新增启动项,支持从数据盘启动

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
linux系统新增启动项,支持从数据盘启动

linux系统(PE系统)挂载数据盘并支持从数据盘启动的情况大多使用在云上机器排查问题等场景。

本文简绍如何在PE系统中新增启动项,并指向数据盘。

主要采用grub2中链式启动能力

什么是链式启动?

主要用于实现跨操作系统的链式加载引导。其核心功能是将控制权转移至其他操作系统的启动程序,通过加载目标分区的引导扇区或EFI文件完成多系统启动环境的构建

https://www.gnu.org/software/grub/manual/grub/grub.html#Chain_002dloading

选择从数据盘启动系统时,既要支持 bios 和 uefi

数据盘系统为bios 启动的时候,PE系统走bios 启动模式,实现方式: set root=(hd1) + chainloader +1

数据盘系统 为uefi 启动的时候,PE系统走uefi启动模式。 实现方式:首选要选择uefi shim path,记得要跳过PE 系统盘本身的 shim path, 选择该设备为root,并从该 shim path 启动。

x86 支持的从数据盘已有系统启动的 grub 配置
cat /etc/grub.d/40_custom #!/usr/bin/sh exec tail -n +3 $0 # This file provides an easy way to add custom menu entries. Simply type the # menu entries you want to add after this comment. Be careful not to change # the 'exec tail' line above. # # 设置了一个uefi启动项的列表,覆盖主流Linux版本的shimx64.efi路径 set os_shim_list="/EFI/ubuntu/shimx64.efi /EFI/centos/shimx64.efi /EFI/debian/shimx64.efi /EFI/redhat/shimx64.efi /EFI/almalinux/shimx64.efi /EFI/rocky/shimx64.efi /EFI/fedora/shimx64.efi /EFI/opensuse/shim.efi /EFI/sles/shim.efi /EFI/BOOT/fbaa64.efi" # 新增一个启动项"Booting From Existing OS" menuentry "Booting From Existing OS" { # 判断启动方式是 BIOS if test "${grub_platform}" = "pc"; then insmod chain # 设置从数据盘启动 hd1 set root=(hd1) chainloader +1 boot else insmod chain insmod fat set root="" #启动方式是uefi,遍历 uefi启动项 for shimpath in $os_shim_list;do # 在数据盘 hd1 上search uefi启动项 search --no-floppy --set=root --file ${shimpath} --hint hd1, # 没有找的 uefi启动项 ,continue if [ -z "$root" ];then echo "root not found, continue" continue else # 找的uefi启动项后 echo "finally find root: $root" # 判断这个uefi启动项是不是 pe系统的 hd0(一般不会) regexp --set 0:matched_harddisk "hd0" "$root" # 如果是pe系统的uefi启动项,continue if [ "$matched_harddisk" = "hd0" ];then echo "root is pe root, skip" #remember reset matched_harddisk set root="" set matched_harddisk="" continue else # 不是pe系统的uefi启动项,就是数据盘系统的,从这个启动项启动 if [ -n "$root" ];then chainloader ${shimpath} boot fi fi fi done fi }
arm 支持的从数据盘已有系统启动的 grub 配置
cat /etc/grub.d/40_custom #!/usr/bin/sh exec tail -n +3 $0 # This file provides an easy way to add custom menu entries. Simply type the # menu entries you want to add after this comment. Be careful not to change # the 'exec tail' line above. # # 设置了一个uefi启动项的列表,覆盖主流Linux版本的shimaa64.efi路径 set os_shim_list="/EFI/ubuntu/shimaa64.efi /EFI/centos/shimaa64.efi /EFI/debian/shimaa64.efi /EFI/redhat/shimaa64.efi /EFI/almalinux/shimaa64.efi /EFI/rocky/shimaa64.efi /EFI/fedora/shimaa64.efi /EFI/opensuse/shimaa64.efi /EFI/sles/shim.efi /EFI/BOOT/fbaa64.efi" # 新增一个启动项"Booting From Existing OS" menuentry "Booting From Existing OS" { insmod chain insmod fat set root="" #遍历 uefi启动项 for shimpath in $os_shim_list;do # 在数据盘 hd1 上search uefi启动项 search --no-floppy --set=root --file ${shimpath} --hint hd1, # 没有找的 uefi启动项 ,continue if [ -z "$root" ];then echo "root not found, continue" continue else # 找到uefi启动项后 echo "finally find root: $root" # 判断这个uefi启动项是不是 pe系统的 hd0(一般不会) regexp --set 0:matched_harddisk "hd0" "$root" # 如果是pe系统的uefi启动项,continue if [ "$matched_harddisk" = "hd0" ];then echo "root is pe root, skip" #remember reset matched_harddisk set root="" set matched_harddisk="" continue else # 不是pe系统的uefi启动项,就是数据盘系统的,从这个启动项启动 if [ -n "$root" ];then chainloader ${shimpath} boot fi fi fi done }

注:

#测试过程发现arm缺失grub mod,故安装 yum -y install grub2-efi-aa64-modules.noarch cp -r /usr/lib/grub/arm64-efi/ /boot/grub2/
设置grub等待时间并重新生成grub配置文件

/etc/default/grub 文件中 GRUB_TIMEOUT 配置为10秒,启动时给用户选择时间

配置完成需重新生成grub配置文件

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

例如

您采用一个 centos 7 系统的数据盘挂载在了PE系统上,修复了问题后,想进入centos 7系统,即 在PE系统下 reboot后在启动项中选择“Booting From Existing OS”即可从数据盘启动(即centos 7)

相关参考:

https://docs.opencloudos.org/OCS/Install_Guide/ocs-UEFI/#1-uefi

https://www.gnu.org/software/grub/manual/grub/grub.html#Chain_002dloading

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/23 19:13:14

金运环球:金价高位回落,非农与零售数据即将来袭

现货黄金周一(12月15日)冲高回落,一度逼近4350美元心理关口,但随后回吐大部分涨幅,收报4305.35美元/盎司,仅微涨0.1%。周二(12月16日)亚市早盘,金价震荡微涨,…

作者头像 李华
网站建设 2026/6/23 19:08:39

活动力度大的门头招牌企业

活动力度大的门头招牌企业在商业竞争日益激烈的今天,一个独特且吸引人的门头招牌对于企业的重要性不言而喻。而在众多门头招牌企业中,活动力度大的企业往往更受客户青睐。以贰师兄广告为例,它就是这样一家值得关注的企业。活动丰富&#xff0…

作者头像 李华
网站建设 2026/6/23 7:25:39

Java毕设选题推荐:基于JavaWeb的兽医站管理系统的设计与实现现代化兽医站管理系统【附源码、mysql、文档、调试+代码讲解+全bao等】

博主介绍:✌️码农一枚 ,专注于大学生项目实战开发、讲解和毕业🚢文撰写修改等。全栈领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围:&am…

作者头像 李华
网站建设 2026/6/23 4:44:17

Arduino配置8266开发板

arduino ide下载网址: https://www.arduino.cc/en/software/ 首选项中配置ESP8266开发板地址: https://dl.espressif.com/dl/package_esp32_index.json http://arduino.esp8266.com/stable/package_esp8266com_index.json 开发板管理中搜索ESP8266后安…

作者头像 李华
网站建设 2026/6/22 16:44:52

【课程设计/毕业设计】基于SpringBoot+Vue茶叶销售系统的设计与实现基于Java语言的茶叶销售系统的前端设计与实现【附源码、数据库、万字文档】

博主介绍:✌️码农一枚 ,专注于大学生项目实战开发、讲解和毕业🚢文撰写修改等。全栈领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围:&am…

作者头像 李华