暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

how to boot into rescue mode or emergency mode

原创 ByteHouse 2024-12-30
710

摘要:

本文是关于CentOS 系统故障后,如何进入救援模式和紧急恢复模式的方法,与 OEL 系统的操作方法相同,因为他们都是来自一些相同的操作系统底座。

  1. CentOS 系统启动失败,进入 “Entering emergency mode. Exit the shell to continue” 提示
  2. 如何通过Systemd将Oracle Linux 7.x引导到救援模式或紧急模式 (Doc ID 2153996.1)

1.故障描述

进入了救援模式:

Generating “/run/initramfs/rdsosreport.txt”

“Entering emergency mode. Exit the shell to continue”
“Type ‘journalctl’ to view system log.”
You might want to save “/run/initramfs/rdsosreport.txt” to a USB stick or /boot after mounting them and attach it to a bug report.

从报错信息中可以看到输入journalctl命令就可以查看本次启动的日志。
日志最后如下图

从日志中看到错误原因是 /dev/mapper/centos-root 无法挂载到系统。即可 Failed to mount /sysroot

2.解决

xfs_repair -v -L /dev/dm-0 


出现上图结果后,重启虚拟机,解决问题

3.emergency mode

CentOS紧急模式(Emergency Mode)是一种系统启动时的特殊状态,当系统遇到严重错误或配置问题导致无法正常启动时,会进入此模式。在紧急模式下,系统只提供一个非常基础的外壳环境,没有启动任何网络服务或挂载额外的文件系统,以便用户进行故障排除和修复。

进入CentOS紧急模式的方法主要有两种:

  1. 在启动时通过编辑界面添加 systemd.unit 参数。启动CentOS时,当出现特定界面时,按e进入编辑界面。在编辑界面里,加入参数systemd.unit=emergency.target,然后按Ctrl-X启动,即可进入紧急模式。
  2. 使用系统的安装介质启动并进入救援模式。将CentOS安装介质插入计算机并重启,在启动菜单中选择“Troubleshooting”,然后选择“Rescue a CentOS system”。按照屏幕上的指示完成语言、键盘布局等设置后,系统可能会自动进入紧急模式,或者用户可以通过特定命令切换到紧急模式。

在紧急模式下,用户可以进行一些基本的故障排除和修复操作,如:

  1. 使用fsck工具检查和修复文件系统错误
  2. 重置root密码
  3. 修复GRUB引导加载器问题

此外,如果系统频繁进入紧急模式,可能需要检查硬件问题、查看系统日志、检查磁盘空间等,以确定问题的根本原因,并采取相应的解决措施。

请注意,在进行任何修复操作之前,确保已备份重要数据,并谨慎操作以避免进一步损坏系统。如果无法解决问题,可以寻求有经验的系统管理员或CentOS社区的帮助。


Linux系统启动时进入紧急模式,提示:Welcome to emergency mode,并提示输入root密码进入维护。

原因分析

紧急模式提供尽可能最小的环境,即使在系统无法进入救援模式的情况下,您也可以修复系统。在紧急模式下,系统仅安装根文件系统进行读取,不尝试安装任何其他本地文件系统,不激活网络接口,只启动一些基本服务。

进入紧急模式的原因通常是:

  • /etc/fstab文件存在错误导致挂载文件系统时失败。
  • 文件系统存在错误导致。

注意:
本节操作适用于Linux操作系统emergency mode(紧急模式)问题处理。操作步骤涉及修复文件系统操作,修复文件系统存在丢失数据风险,请先备份数据后进行修复操作。

处理方法

step 1.输入密码后回车,进入修复模式
step 2.在紧急模式下根分区是以只读方式挂载,要修改根目录下的文件需要执行以下命令,以读写方式重新挂载根分区

mount -o rw,remount /

step 3.请执行以下命令首先检查fstab文件是否存在错误,尝试挂载所有未挂载的文件系统。

mount -a
  • 如果出现mount point does not exist为挂载点不存在,请创建对应的挂载点。
  • 如果出现no such device为不存在该文件系统设备,请注释或者删除该挂载行。
  • 如果出现an incorrect mount option was specified为挂载参数错误,请修改为正确的参数。
  • 如果没有出现任何错误且提示UNEXPECTED INCONSISTENCY;RUN fsck MANUALLY,通常为文件系统错误导致。参考步骤7

step 4.执行以下命令,打开/etc/fstab修改相应的错误。

vi /etc/fstab

/etc/fstab文件包含了如下字段,通过空格分隔:

step 5.修改完成后,确认修改是否正确,再次执行以下命令首先检查fstab文件。

mount -a

step 6、执行重启命令

reboot

step 7、如果步骤3中没有任何错误,则可能为文件系统错误导致,执行:

dmesg |egrep "ext[2..4]|xfs" |grep -i error

说明:

  1. 输出结果中如果有I/O error … inode的错误信息则根因为文件系统错误导致。
  2. 如果上述命令没有发现日志记录文件系统文件错误则通常为超级块损坏。超级块是文件系统的“头部”。它包含文件系统的状态、尺寸和空闲磁盘块等信息。
  3. 如果损坏了一个文件系统的超级块(例如不小心直接将数据写到了文件系统的超级块分区中),那么系统可能会完全不识别该文件系统,系统启动时没有识别到文件系统导致进入紧急模式。ext2fs类型的文件系统将超级块的内容进行了备份,并存放于驱动程序的块组(blockgroup)边界。

step 8.请执行以下命令,卸载文件系统出错的目录

umount 挂载点

step 9.检查并修复已损坏的文件系统。

须知:
修复文件系统可能会导致数据丢失请先进行数据备份。

ext文件系统,执行以下命令,检查文件系统是否存在错误。

fsck -n /dev/vdb1

如果出现 “The super block Cloud no be read or does not describe a correct ext2 filesystem” 的提示请跳转至步骤10

如果需要修复,执行以下命令,修复文件系统。

fsck /dev/vdb1

xfs文件系统,执行以下命令,检查文件系统是否存在错误。

xfs_repair -n /dev/vdb1

如果需要修复,执行以下命令,修复文件系统。

xfs_repair /dev/vdb1

step 10.可选)出现 “The super block Cloud no be read or does not describe a correct ext2 filesystem” 通常为超级块损坏,请按照提示使用备份的超级块更新超级块。

执行以下命令,使用备份的超级块信息更新超级块。

e2fsck -b 8193 /dev/sda

说明

  1. -b 8193选项用于显示使用存放在文件系统中的8193块的超级块的备份数据。通常在主超级块已损坏时使用。备份超级块的位置是依赖的在文件系统的blocksize上。
  2. 对于具有1k块大小的文件系统,可以在块处找到备份超级块8193。
  3. 对于具有2k块大小的文件系统,在块16384;对于4k块,在块32768。 <设备名>为磁盘名称而非分区。

step 11.修复完成后执行以下命令,重启服务器。

reboot

4.救援模式(Rescue Mode) + 紧急模式(Emergency Mode)

启动模式

  1. 救援模式:
    救援模式能让使用者由光碟(或其它开机方法)启动基本的 Red Hat Enterprise Linux 环境;而不是藉由系统硬盘来启动。在正常操作中,Red Hat Enterprise Linux 系统会使用硬盘里的档案来进行所有工作 — 执行程式、储存档案等等。然而,在某些情况下,Red Hat Enterprise Linux 会无法完整存取系统的硬盘。透过使用救援模式,即使您无法由某个硬盘执行 Red Hat Enterprise Linux,您依然能够存取储存在该硬盘上的档案。总之,救援模式就是从其他介质启动(能够自己选择挂载的分区),从而获得一个runlevel(如果按照流程,会进入level1的单用户模式)。
  2. 单用户模式:
    在单用户模式,电脑为开机进入runlevel 1,本机档案系统已经被挂载,不过您的网路尚未启动,您可以使用一个系统维护的 shell。单机使用者模式与救援模式不同,它会试着自动挂载档案系统,如果您的档案系统无法被成功挂载,请勿使用单机使用者模式。而且如果系统上的执行层级 1 的设定已经毁损,您也无法使用单机使用者模式。
  3. 紧急模式:
    在紧急模式中,您是开机进入尽可能最小的环境中。根目录档案系统将会被挂载为仅能读取,而且将不会做任何的设定。比起单机使用者模式,紧急模式最主要的优点是并不载入 init 档案。因此当 init 档案毁损或无法使用时,您仍然可以挂载档案系统以回复重新安装阶段中遗失的资料。

注:现在的centos7 取消了使用多年的 SystemV 的服务管理方式,也就是说,从这一版开始,已经没有所谓的“执行等级 (run level) ”的概念了。新的管理方法使用的是 systemd 的模式(systemctl而非init),这个模式将很多的服务进行相依性管理。

如果用rescue mode获取环境,需要使用mount和chroot挂载根目录并更换当前根目录后然后使用passwd命令更改密码。如果能直接用单用户模式的环境,则可以直接更改密码。至于紧急模式,不推荐使用,因为很多基础服务未加载,强制重启后可能会有问题。

至于更改密码的具体步骤,请参考下面的几篇文章:
https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/4/html/Step_by_Step_Guide/s1-q-and-a-root-passwd.html
https://wiki.archlinux.org/index.php/Reset_root_password_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)
http://linux.vbird.org/linux_basic/0510osloader.php#solution

参考:
Runlevel https://en.wikipedia.org/wiki/Runlevel
Linux Standard Base https://en.wikipedia.org/wiki/Linux_Standard_Base
Basic System Recovery https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Installation_Guide/ap-rescuemode.html#s1-rescuemode-booting-single

模式切换

当CentOS操作系统无法正常启动时,可以使用救援模式或紧急模式进行系统登录。通过在启动时编辑界面添加systemd.unit 参数,分别设置为 rescue.target 和 emergency.target ,然后用Ctrl-X启动进入相应模式。救援模式和紧急模式之间可通过运行特定root命令相互切换。

当CentOS操作系统崩溃,无法正常启动时,可以通过救援模式或者紧急模式进行系统登录。

启动CentOS, 当出现下面界面时,按e进入编辑界面。

  1. 在编辑界面里,加入参数:systemd.unit=rescue.target ,然后Ctrl-X启动进入救援模式。

  2. 在编辑界面里,加入参数:systemd.unit=emergency.target, 然后Ctrl-X进入紧急模式。

救援模式与紧急模式可以相互切换:

  1. Switch to Emergency mode(target)
    To switch to Emergency target, simply run following command as root:
# systemctl emergency

Broadcast message from root@geeklab on pts/1 (Mon 2016-08-17 00:44:58 EDT):
The system is going down to emergency mode NOW!
  1. Switch to Rescue mode(target)
    To switch to rescue target, simply run following command as root:
# systemctl rescue

Broadcast message from root@geeklab on pts/0 (Mon 2016-08-17 00:22:44 EDT):
The system is going down to rescue mode NOW!

How to Bootup Oracle Linux 7.x into Rescue Mode or Emergency Mode Through Systemd (Doc ID 2153996.1)

APPLIES TO:

Linux OS - Version Oracle Linux 7.0 and later
Linux x86-64

GOAL

This article explains how to bootup OL7 system into rescue mode or emergency mode.
In OL7, both rescue mode and emergency mode are systemd targets which replaced the concept of runlevels in previous OL versions.

Rescue mode is equivalent to single user mode and requires the root password.

救援模式等效于单用户模式,并且需要root密码

Rescue mode allows you to repair your system in situations when it is unable to complete a regular booting process. Rescue mode will try to mount all local file systems and start some important system services, but it does not activate network interfaces neither allow multiple users to be logged in.

救援模式使您可以在无法完成常规引导过程的情况下修复系统。救援模式将尝试挂载所有本地文件系统并启动一些重要的系统服务,但是它不会激活网络接口,也不允许多个用户登录

Emergency mode provides the most minimal environment possible and allows you to repair your system even in situations when the system is unable to enter rescue mode. In emergency mode, the system mounts the root file system as read-only, does not attempt to mount any other local file systems, does not activate network interfaces.

紧急模式提供了尽可能最小的环境,即使在系统无法进入救援模式的情况下,您也可以修复系统。在紧急模式下,系统将根文件系统安装为只读,不尝试安装任何其他本地文件系统,也不激活网络接口

In case the Linux OS could not bootup itself, please refer to How to Bootup Oracle Linux 7.x into Rescue Mode From ISO Image.

如果Linux操作系统无法自行启动,请参阅 How to Bootup Oracle Linux 7.x into Rescue Mode From ISO Image

SOLUTION

Bootup into Emergency mode(target)

  1. During bootup, when the GRUB2 menu shows up, press the e key for edit.
  2. Add the following parameter at the end of the linux16 line on x86-64 systems, or the linuxefi line on UEFI systems:
systemd.unit=emergency.target

Press Ctrl+a (or Home) and Ctrl+e (or End) to jump to the start and end of the line.

  1. Press Ctrl+x to boot the system with the parameter.

Bootup into Rescue mode(target)

  1. During bootup, when the GRUB2 menu shows up, press the e key for edit.
  2. Add the following parameter at the end of the linux16 line on x86-64 systems, or the linuxefi line on UEFI systems:
systemd.unit=rescue.target

Press Ctrl+a (or Home) and Ctrl+e (or End) to jump to the start and end of the line.

  1. Press Ctrl+x to boot the system with the parameter.

Bootup into Debug Shell

The systemd debug shell provides a shell in the startup process that can be used to diagnose systemd related boot-up problems. Once in the debug shell, systemctl commands such as systemctl list-jobs, and systemctl list-units can be used to diagnose boot problems.

systemd debug shell程序在启动过程中提供了一个shell程序,可用于诊断与systemd相关的启动问题。进入debug shell后,可以使用systemctl命令(例如systemctl list-jobs和systemctl list-units)来诊断引导问题。

During bootup, when the GRUB2 menu shows up, press the e key for edit.
Add the following parameter at the end of the the linux16 line on x86-64 BIOS-based systems, or the linuxefi line on UEFI systems:

systemd.debug-shell

The systemd will configure the debug shell on TTY9. Press Ctrl+Alt+F9 to connect to the debug shell (no credential needed).

systemd将在TTY9上配置debug shell。按Ctrl+Alt+F9连接到debug shell(无需证书)

Switch to Emergency mode(target)

To switch to Emergency target, simply run following command as root:

# systemctl emergency

Broadcast message from root@dhcp-cn-10-182-71-190.cn.oracle.com on pts/1 (Mon 2016-06-27 00:44:58 EDT):

The system is going down to emergency mode NOW! 

To prevent systemd from sending informative message:

# systemctl --no-wall emergency
# systemctl isolate emergency.target 

Switch to Rescue mode(target)

To switch to rescue target, simply run following command as root:

# systemctl rescue
 
Broadcast message from root@dhcp-cn-10-182-71-190.cn.oracle.com on pts/0 (Mon 2016-06-27 00:22:44 EDT):
 
The system is going down to rescue mode NOW! 

To prevent systemd from sending informative message:

# systemctl --no-wall rescue
# systemctl isolate rescue.target 

REFERENCES

NOTE:2302734.1 - How to Bootup Oracle Linux 7.x into Rescue Mode From ISO Image

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论