网站搜索

如何确定并修复 Linux 中的引导问题


Linux 系统启动速度如此之快,以至于大多数输出滚动得太快,以至于无法读取发送到控制台的文本(显示正在启动的服务)。因此,观察启动问题/错误对我们来说是一个挑战。

在本文中,我们将简要解释 Linux 系统引导过程中的不同阶段,然后学习如何确定并深入了解引导问题:要查看的文件或查看系统引导消息的命令。

Linux启动过程总结

总而言之,一旦我们按下开机按钮,主板上集成的BIOS基本输入输出系统)程序就会执行 >POST开机自检)——扫描磁盘、RAM(随机存取存储器)、键盘等硬件。如果出现错误(硬件丢失/故障),则会在屏幕上报告。

POST期间,BIOS还会查找启动设备,即要启动的磁盘(通常是第一个硬盘,但我们可以将其配置为 DVD、USB 、网卡等代替)。

然后系统将连接到磁盘并搜索主引导记录(大小为512字节),其中存储引导加载程序(大小为446字节),其余空间存储有关磁盘分区的信息(最多四个)和 MBR 本身。

建议阅读: 4 个最佳 Linux 引导加载程序

引导加载程序将识别并指向并加载内核和initrd文件(初始化RAM磁盘 - 提供对已安装的根文件系统和存储在/中的模块/驱动程序的内核访问) lib目录),一般存放在文件系统的/boot目录下。

加载内核后,它会执行 init(或较新的 Linux 发行版上的 systemd),这是第一个具有 PID 1 的进程,它依次启动系统上的所有其他进程。它也是系统关闭时执行的最后一个进程。

如何找出 Linux 启动问题或错误消息

正如我们之前提到的,Linux 启动过程发生得非常快,我们甚至无法清楚地读取发送到控制台的大部分输出。

因此,记录启动问题/错误需要系统管理员结合特定命令来查看某些重要文件。这些包括:

/var/log/boot.log – 记录系统启动消息

这可能是您想要查看的第一个文件,以查看系统启动期间展开的所有内容。

我们可以在启动过程完成后查看此文件,以帮助我们确定和解决启动问题/错误,而不是在启动过程中努力跟踪屏幕上的输出。

我们使用 cat 命令来实现此目的,如下所示(下面是该文件的示例):

cat /var/log/boot.log
[  OK  ] Started Load/Save RF Kill Switch Status.
[ TIME ] Timed out waiting for device dev-disk-by\x2duuid-53e41ce9\x2ddc18\x2d458c\x2dbc08\x2d584c208ed615.device.
[DEPEND] Dependency failed for /dev/disk/by-uuid/53e41ce9-dc18-458c-bc08-584c208ed615.
[DEPEND] Dependency failed for Swap.
[  OK  ] Reached target System Initialization.
[  OK  ] Listening on UUID daemon activation socket.
[  OK  ] Started Daily Cleanup of Temporary Directories.
[  OK  ] Listening on CUPS Scheduler.
[  OK  ] Started Daily apt activities.
[  OK  ] Reached target Timers.
[  OK  ] Listening on Avahi mDNS/DNS-SD Stack Activation Socket.
[  OK  ] Started ACPI Events Check.
[  OK  ] Started Trigger resolvconf update for networkd DNS.
[  OK  ] Started CUPS Scheduler.
[  OK  ] Reached target Paths.
[  OK  ] Listening on D-Bus System Message Bus Socket.
[  OK  ] Listening on ACPID Listen Socket.
         Starting Console System Startup Logging...
[  OK  ] Listening on Cockpit Web Service Socket.
[  OK  ] Reached target Sockets.
[  OK  ] Reached target Basic System.
         Starting LSB: Set the CPU Frequency Scaling governor to "ondemand"...
[  OK  ] Started ACPI event daemon.
[  OK  ] Started mintsystem.service.
         Starting Detect the available GPUs and deal with any system changes...
         Starting LSB: daemon to balance interrupts for SMP systems...
         Starting Bluetooth service...
[  OK  ] Started ClamAV virus database updater.
         Starting LSB: Starts syslogd...
[  OK  ] Started Regular background program processing daemon.
         Starting Modem Manager...
         Starting Accounts Service...
......

从上面的输出中,我们可以看到以下几行表明存在启动问题。

[DEPEND] Dependency failed for /dev/disk/by-uuid/53e41ce9-dc18-458c-bc08-584c208ed615.
[DEPEND] Dependency failed for Swap.

问题:交换分区有问题;系统无法读取交换文件/设备/分区,或者它不存在。

让我们通过 free 命令检查系统是否正在使用交换空间。

free

total        used        free      shared  buff/cache   available
Mem:        3742792     2421060      433696      287376      888036      967000
Swap:             0           0           0

或者,我们可以运行 swapon 命令来查看系统交换空间使用情况摘要(我们不会得到任何输出)。

swapon -s

我们可以通过在 Linux 中创建交换空间来解决这个问题。

注意:该文件的内容在系统关闭时被清除:新数据在新启动时存储在其中。

/var/log/messages – 常规系统日志

该文件存储一般系统消息,包括系统引导期间记录的消息。

要查看它,请输入:

cat /var/log/messages
Jun  4 13:04:44 tecmint syslogd (GNU inetutils 1.9.4): restart
Jun  4 13:19:55 tecmint -- MARK --
Jun  4 13:39:55 tecmint -- MARK --
Jun  4 13:59:55 tecmint -- MARK --
Jun  4 14:19:55 tecmint -- MARK --
Jun  4 14:20:17 tecmint vmunix: [ 4945.388740] i915 0000:00:02.0: BAR 6: [??? 0x00000000 flags 0x2] has bogus alignment
Jun  4 14:20:17 tecmint vmunix: [ 4945.388837] i915 0000:00:02.0: BAR 6: [??? 0x00000000 flags 0x2] has bogus alignment
Jun  4 14:20:17 tecmint vmunix: [ 4945.388903] i915 0000:00:02.0: BAR 6: [??? 0x00000000 flags 0x2] has bogus alignment
Jun  4 14:20:17 tecmint vmunix: [ 4945.388930] i915 0000:00:02.0: BAR 6: [??? 0x00000000 flags 0x2] has bogus alignment
Jun  4 14:20:17 tecmint vmunix: [ 4945.389334] i915 0000:00:02.0: BAR 6: [??? 0x00000000 flags 0x2] has bogus alignment
Jun  4 14:20:17 tecmint vmunix: [ 4945.389402] pcieport 0000:00:1c.0: BAR 15: assigned [mem 0xdfa00000-0xdfbfffff 64bit pref]
.....

由于这个文件可能比较长,我们可以使用more命令(甚至显示百分比)在页面中查看它,如下所示。

more /var/log/messages

与之前的文件不同,/var/log/messages 的内容在关机时不会被清除,因为它不仅包含启动消息,还包含有关其他系统活动的消息。因此,较旧的文件会被压缩并保留在系统上以供以后检查,如下所示。

ls -l message*

-rw-r--r-- 1 root root 1206127 Jun 10 14:20 messages
-rw-r--r-- 1 root root 1419494 Jun  4 13:00 messages.1
-rw-r--r-- 1 root root  153011 May 28 09:30 messages.2.gz

dmesg – 显示内核消息

dmesg 命令可以在引导过程完成后显示操作,例如传递给内核的命令行选项;检测到的硬件组件、添加新 USB 设备时发生的事件或 NIC(网络接口卡)故障等错误以及驱动程序报告在网络上未检测到链接活动等等。

dmesg
[    0.000000] Initializing cgroup subsys cpuset
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Initializing cgroup subsys cpuacct
[    0.000000] Linux version 4.4.0-21-generic (buildd@lgw01-21) (gcc version 5.3.1 20160413 (Ubuntu 5.3.1-14ubuntu2) ) #37-Ubuntu SMP Mon Apr 18 18:33:37 UTC 2016 (Ubuntu 4.4.0-21.37-generic 4.4.6)
[    0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-4.4.0-21-generic root=UUID=57b36d48-1938-43c2-bf85-e97bc9f423ea ro quiet splash
[    0.000000] KERNEL supported cpus:
[    0.000000]   Intel GenuineIntel
[    0.000000]   AMD AuthenticAMD
[    0.000000]   Centaur CentaurHauls
[    0.000000] x86/fpu: xstate_offset[2]:  576, xstate_sizes[2]:  256
[    0.000000] x86/fpu: Supporting XSAVE feature 0x01: 'x87 floating point registers'
[    0.000000] x86/fpu: Supporting XSAVE feature 0x02: 'SSE registers'
[    0.000000] x86/fpu: Supporting XSAVE feature 0x04: 'AVX registers'
[    0.000000] x86/fpu: Enabled xstate features 0x7, context size is 832 bytes, using 'standard' format.
[    0.000000] x86/fpu: Using 'eager' FPU context switches.
[    0.000000] e820: BIOS-provided physical RAM map:
[    0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000008ffff] usable
[    0.000000] BIOS-e820: [mem 0x0000000000090000-0x00000000000bffff] reserved
[    0.000000] BIOS-e820: [mem 0x0000000000100000-0x000000001fffffff] usable
[    0.000000] BIOS-e820: [mem 0x0000000020000000-0x00000000201fffff] reserved
[    0.000000] BIOS-e820: [mem 0x0000000020200000-0x0000000040003fff] usable
[    0.000000] BIOS-e820: [mem 0x0000000040004000-0x0000000040004fff] reserved
[    0.000000] BIOS-e820: [mem 0x0000000040005000-0x0000000080b2ffff] usable
[    0.000000] BIOS-e820: [mem 0x0000000080b30000-0x0000000080d31fff] reserved
[    0.000000] BIOS-e820: [mem 0x0000000080d32000-0x00000000ce3eefff] usable
[    0.000000] BIOS-e820: [mem 0x00000000ce3ef000-0x00000000ce5eefff] type 20
[    0.000000] BIOS-e820: [mem 0x00000000ce5ef000-0x00000000daeeefff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000daeef000-0x00000000daf9efff] ACPI NVS
....

Journalctl – 查询Systemd Journal的内容

这在大多数当前使用 systemd 的 Linux 发行版中非常有用:最新的系统和服务管理器。在它之下,系统日志记录是通过日志组件完成的 - 不同组件记录的所有消息的集中位置。

这些消息包括内核和启动消息;来自系统日志或各种服务的消息。我们可以使用它来查看启动消息,并通过读取输出并识别感兴趣的行(根据终端文本颜色设置,用红线指出错误)来确定启动问题。

journalctl
Jun 13 16:35:32 tecmint mtp-probe[963]: checking bus 2, device 5: "/sys/devices/pci0000:00/0000:00:1d.0/u
Jun 13 16:35:32 tecmint mtp-probe[963]: bus: 2, device: 5 was not an MTP device
Jun 13 16:35:54 tecmint systemd[1]: dev-disk-by\x2duuid-53e41ce9\x2ddc18\x2d458c\x2dbc08\x2d584c208ed615.
Jun 13 16:35:54 tecmint systemd[1]: Timed out waiting for device dev-disk-by\x2duuid-53e41ce9\x2ddc18\x2d
Jun 13 16:35:54 tecmint systemd[1]: Dependency failed for /dev/disk/by-uuid/53e41ce9-dc18-458c-bc08-584c2
Jun 13 16:35:54 tecmint systemd[1]: Dependency failed for Swap.
Jun 13 16:35:54 tecmint systemd[1]: swap.target: Job swap.target/start failed with result 'dependency'.
Jun 13 16:35:54 tecmint systemd[1]: dev-disk-by\x2duuid-53e41ce9\x2ddc18\x2d458c\x2dbc08\x2d584c208ed615.
Jun 13 16:35:54 tecmint systemd[1]: dev-disk-by\x2duuid-53e41ce9\x2ddc18\x2d458c\x2dbc08\x2d584c208ed615.
Jun 13 16:35:54 tecmint systemd[1]: Reached target System Initialization.
Jun 13 16:35:54 tecmint systemd[1]: Started ACPI Events Check.
Jun 13 16:35:54 tecmint systemd[1]: Listening on CUPS Scheduler.
Jun 13 16:35:54 tecmint systemd[1]: Starting Console System Startup Logging...
Jun 13 16:35:54 tecmint systemd[1]: Started Daily Cleanup of Temporary Directories.

上面是命令输出的示例,显示了我们通过查看 /var/log/boot.log 已经识别出的错误:交换分区错误。要查看更多输出行,只需按[Enter]按钮即可。

另请查看这些与常见系统和服务管理器相关的有关 Linux 系统启动和服务管理的有用指南:

  1. RHEL 7 中的进程管理:启动、关闭以及其间的一切
  2. 管理系统启动流程和服务(SysVinit、Systemd 和 Upstart)
  3. “init”和“systemd”背后的故事:为什么需要用“systemd”替换“init”

在本文中,我们简要解释了 Linux 系统启动过程的不同阶段,并了解了如何确定和解决启动问题。要向本指南添加任何想法,请使用下面的评论表。