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又没指定,唉!

相关推荐
还是奇怪24 分钟前
Linux - 安全排查 2
linux·运维·安全
tan77º2 小时前
【Linux网络编程】Socket - UDP
linux·服务器·网络·c++·udp
czhc11400756632 小时前
Linux 76 rsync
linux·运维·python
蓝易云3 小时前
Qt框架中connect()方法的ConnectionType参数使用说明 点击改变文章字体大小
linux·前端·后端
花落已飘4 小时前
多线程 vs 异步
linux·网络·系统架构
PanZonghui4 小时前
Centos项目部署之Nginx部署项目
linux·nginx
码出钞能力5 小时前
linux内核模块的查看
linux·运维·服务器
星辰云-5 小时前
# Linux Centos系统硬盘分区扩容
linux·运维·centos·磁盘扩容
聽雨2376 小时前
02每日简报20250704
linux·科技·金融·生活·社交电子·娱乐·媒体
Maki Winster6 小时前
Peek-Ubuntu上Gif录制工具-24.04LTS可装
linux·ubuntu·peek