制作 Ubuntu From Scratch(UFS) 镜像,本质上是从零开始构建一个最小化的 Ubuntu 系统(类似 LFS,但基于 Ubuntu 的软件包体系)。下面给你一套可落地的完整思路与步骤,适用于制作 可用于容器 / 虚拟机 / 物理机 的 Ubuntu 基础镜像。
先确定你要的“From Scratch”到什么程度:
| 类型 | 说明 |
|---|---|
| 真·From Scratch | 只用 debootstrap + 手动配置 |
| 半 From Scratch | 基于 ubuntu-minimal 再裁剪 |
| 容器镜像 | 使用 debootstrap + tar |
| 虚拟机镜像 | debootstrap + qemu / cloud-init |
下面以 Ubuntu 22.04 / 24.04 + debootstrap + 容器镜像 为例。
sudo apt update
sudo apt install -y debootstrap squashfs-tools git qemu-user-static
建议使用 Ubuntu / Debian 宿主机。
sudo debootstrap \
--arch=amd64 \
--variant=minbase \
jammy /ubuntu-root \
http://archive.ubuntu.com/ubuntu
说明:
minbase:最小化系统jammy:Ubuntu 22.04/ubuntu-root:目标根目录sudo chroot /ubuntu-root /bin/bash
在 chroot 内执行:
apt update
apt install -y \
systemd \
systemd-sysv \
netplan.io \
nano \
ca-certificates \
sudo
清理:
apt clean
rm -rf /var/lib/apt/lists/*
echo "ubuntu-fromscratch" > /etc/hostname
network:
version: 2
ethernets:
eth0:
dhcp4: true
useradd -m -s /bin/bash ubuntu
echo "ubuntu:ubuntu" | chpasswd
usermod -aG sudo ubuntu
cd /ubuntu-root
sudo tar -czf ../ubuntu-fromscratch.tar.gz .
导入为 Docker 镜像:
docker import ubuntu-fromscratch.tar.gz my-ubuntu:fromscratch
dd if=/dev/zero of=ubuntu.img bs=1M count=2048
mkfs.ext4 ubuntu.img
mkdir /mnt/ufs
mount ubuntu.img /mnt/ufs
cp -a /ubuntu-root/. /mnt/ufs/
umount /mnt/ufs
转换为 qcow2:
qemu-img convert -f raw -O qcow2 ubuntu.img ubuntu-fromscratch.qcow2
如果你想 完全不用 debootstrap:
.deb 包dpkg-deb -x xxx.deb root/
/etc/passwd/etc/fstab/lib/systemdmultistrap 或 mmdebstrap示例(mmdebstrap):
mmdebstrap jammy ./root \
--variant=apt \
--include=systemd,sudo
docker run -it my-ubuntu:fromscratch bash
或启动虚拟机:
qemu-system-x86_64 -hda ubuntu-fromscratch.qcow2
systemd → 无法 initca-certificates → apt 失败/procmount -t proc proc /proc
如果你愿意,我可以:
你打算用在 容器 / 虚拟机 / 嵌入式 哪个场景?