NVME盘未格式化导致Ubuntu20.04启动慢

背景

最近公司一款产品转产,工厂组装好后,用我提供的系统镜像烧录,系统起来后发现Ubuntu20.04转圈了90秒才进入图形界面,这是不可接受的,公司老总要求当天必须解决。

定位

分析syslog

看不到系统启动时的日志,而且大量日志是集中在1秒内写入的,说明启动阶段的日志有丢失

查看串口打印

考虑到串口不会丢日志,接上串口,再次复现故障,发现启动10秒后打印突然就没有了,然后过了90秒才开始有打印,说明系统确实在这90秒里什么都没做,就干等。

分析systemd

与同事一起分析systemd,特别是跟研发这边的旧机器(没有启动慢问题)的systemd做比对,发现新机器少了个target,这个target负责执行/etc/fstab里指定的挂载,而systemd.mount挂载NVME超时,systemd的配置又决定了图形界面的初始化必须在NVME挂载之后进行,于是系统转圈了90秒。

systemd.mount手册截取

复制代码
Options¶

TimeoutSec=
Configures the time to wait for the mount command to finish. If a command does not exit within the configured time, the mount will be considered failed and be shut down again. All commands still running will be terminated forcibly via SIGTERM, and after another delay of this time with SIGKILL. (See KillMode= in systemd.kill(5).) Takes a unit-less value in seconds, or a time span value such as "5min 20s". Pass 0 to disable the timeout logic. The default value is set from DefaultTimeoutStartSec= option in systemd-system.conf(5).

systemd-system.conf手册截取

复制代码
Options¶

DefaultTimeoutStartSec=, DefaultTimeoutStopSec=, DefaultTimeoutAbortSec=, DefaultRestartSec=
Configures the default timeouts for starting, stopping and aborting of units, as well as the default time to sleep between automatic restarts of units, as configured per-unit in TimeoutStartSec=, TimeoutStopSec=, TimeoutAbortSec= and RestartSec= (for services, see systemd.service(5) for details on the per-unit settings). For non-service units, DefaultTimeoutStartSec= sets the default TimeoutSec= value.

DefaultTimeoutStartSec= and DefaultTimeoutStopSec= default to 90 s in the system manager and 90 s in the user manager. DefaultTimeoutAbortSec= is not set by default so that all units fall back to TimeoutStopSec=. DefaultRestartSec= defaults to 100 ms.

Added in version 209.

可以看到,systemd默认挂载超时时间恰好是90秒!

解决NVME挂载问题

经定位,是NVME出厂时并未格式化,导致/etc/fstab里配置的分区不存在,进而导致systemd根据/etc/fstab动态创建的mount单元执行超时

复制代码
# UNCONFIGURED FSTAB FOR BASE SYSTEM
/dev/nvme0n1p1 /home/root               ext4    defaults        0 2

验证问题

用fdisk命令创建Linux分区,再用mkfs.ext4命令给Linux分区创建ext4文件系统后,重启,启动慢的问题解决。

解决方案

研发解决

  1. 创建NVME检查脚本,检查是否有ext4分区,没有就创建分区并格式化
  2. 添加适当的systemd单元,使其在/etc/fstab被systemd解析前调用NVME检查脚本

工厂解决

要求工厂装配前必须将NVME格式化,且格式化成ext4分区。

  1. 买一个USB转NVME的硬盘盒
  2. 将NVME盘接到PC机上
  3. 使用傲梅分区助手这个第三方软件将NVME盘格式化成ext4分区。

后记

为什么systemd要给mount设置超时参数?本地挂载设置超时没有意义,网络挂载/etc/fstab又没指定,唉!

相关推荐
qeen8742 分钟前
【Linux】Linux简单介绍与基本指令(上)
linux·运维·服务器·学习
tianyuanwo1 小时前
深入解析 RISC-V 虚拟化中的 UEFI 固件配置:从 XML 到 NVRAM 的生命周期管理
xml·linux·risc-v
道川贤林2 小时前
OrangePi 系统启动优先级修改
linux·linux驱动·orangepi·u-boot
xsc-xyc2 小时前
用 Tailscale + Syncthing 实现手机、电脑与 NAS 的跨网络文件同步
linux·网络·网络安全·智能手机·电脑
IsJunJianXin2 小时前
pdd小程序 cdp 保存响应体
linux·服务器·小程序·pdd小程序·拼多多响应体解密·小程序cdp·拼多多rpc取响应体
爱就是恒久忍耐3 小时前
现代CMake的build方式
linux·运维·服务器
古城小栈4 小时前
Python 的主流Ai框架为什么优先适配 Linux 系统?
linux·人工智能·python
盼小辉丶5 小时前
Ubuntu极速部署OpenClaw完全指南(本地模型+DeepSeek)
linux·ubuntu·openclaw
字节高级特工5 小时前
【Linux】C语言进程地址空间分布
linux·c++·后端·算法
黑白园5 小时前
【环境搭建】Ubuntu安装(一)
linux·ubuntu