温馨提示×

如何调试ubuntu fromscratch

小樊
35
2026-09-01 13:11:50
栏目: 智能运维

“Ubuntu From Scratch”(UFS)通常指从零开始构建自己的 Ubuntu 风格系统(类似 LFS,但面向 Ubuntu/Debian 生态),常见做法包括:

  • 用 debootstrap 构建基础系统
  • chroot / 容器中进行配置
  • 自己打包内核、initrd、镜像

调试这类系统,核心思路是:分层隔离问题 + 尽早获得可观察性

下面按常见阶段给你一套实用调试方法。


一、构建阶段调试(debootstrap / 根文件系统)

1. 开启详细日志

debootstrap --verbose focal ./rootfs http://archive.ubuntu.com/ubuntu

如果失败,加:

--debug

2. 检查架构与源是否匹配

常见问题:

  • amd64 系统误用 arm64 源
  • 源地址拼错

验证:

uname -m
cat ./rootfs/etc/apt/sources.list

3. 手动进入 chroot 调试

mount --bind /dev ./rootfs/dev
mount --bind /proc ./rootfs/proc
mount --bind /sys ./rootfs/sys
chroot ./rootfs

在 chroot 中:

apt update
apt install -y bash coreutils

二、内核与启动调试

1. 内核不启动?先确认这几项

  • 是否编译了 正确的架构
  • 是否包含:
    • ext4 / xfs
    • SATA / NVMe 驱动
    • initrd 支持

2. 使用 QEMU 快速验证(强烈推荐)

qemu-system-x86_64 \
  -kernel bzImage \
  -initrd initrd.img \
  -append "console=ttyS0 root=/dev/sda1" \
  -hda rootfs.img \
  -nographic

3. 内核启动参数调试

init=/bin/bash

可跳过 init 系统,直接进 shell。

查看早期错误:

loglevel=7

三、init / systemd 调试

1. systemd 卡住

加内核参数:

systemd.log_level=debug
systemd.log_target=console

2. 进入 emergency 模式

systemd.unit=emergency.target

3. 手动启动服务调试

systemctl status <service>
journalctl -xe

四、根文件系统挂载问题

常见问题

  • fstab 写错
  • initrd 没包含存储驱动
  • UUID 不匹配

调试方法

blkid
cat /etc/fstab

在 initramfs 中:

ls /dev
mount -t ext4 /dev/sda1 /root

五、镜像 / ISO 调试

1. 用 loop 挂载检查内容

mount -o loop ubuntu-from-scratch.iso /mnt

2. GRUB 配置检查

cat /mnt/boot/grub/grub.cfg

3. 用虚拟机验证

  • QEMU
  • VirtualBox
  • VMware

六、推荐调试工具链

场景 工具
启动验证 QEMU
日志 journalctl / dmesg
文件系统 busybox
网络 dhclient / ip
调试 shell bash-static

七、一个最小可启动系统检查清单

✅ debootstrap 成功
✅ chroot 可运行 apt
✅ 内核包含根设备驱动
✅ initrd 可挂载根
✅ fstab 正确
✅ GRUB 配置正确


如果你愿意,可以告诉我:

  • 你用的是 debootstrap / LFS风格 / 自写脚本
  • 卡在 哪一步
  • 是否有 报错信息

我可以直接帮你定位问题。

0