Ubuntu创建系统镜像的常用方法
方法一 Systemback 快速生成可启动镜像(适合桌面版)
- 适用场景:将当前系统连同用户数据打包为可启动的 .sblive,并在镜像小于 4GB 时直接转换为 ISO;大于 4GB 时可用 UDF/工具链转换。
- 安装 Systemback(Ubuntu 18.04/20.04 常见做法):
- 添加旧版 PPA 并导入密钥(该软件已不再维护,源可能不可用,若失败请考虑其它方法):
- sudo add-apt-repository --remove ppa:nemh/systemback
- sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 382003C2C8B7B4AB813E915B14E4942973C62A1B
- sudo add-apt-repository “deb http://ppa.launchpad.net/nemh/systemback/ubuntu xenial main”
- sudo apt update && sudo apt install systemback unionfs-fuse
- 制作镜像与启动盘:
- 运行:sudo systemback
- 选择 Live system create,勾选 Include the user data files(包含主目录数据),设置 Working directory(需有足够空间),点击 Create New 生成 .sblive。
- 写入 U 盘:插入 U 盘,选择 .sblive → Write to target,制作可启动盘。
- 转 ISO:若 .sblive ≤ 4GB,可选 Convert to ISO 直接生成 ISO;若大于 4GB,可参考下述 UDF/转换流程或改用其它工具(见文末提示)。
方法二 手动定制官方 ISO(Cubic 或命令行,适合深度定制)
- Cubic 图形化流程(推荐):
- 安装:sudo add-apt-repository ppa:cubic-wizard/releases && sudo apt update && sudo apt install cubic
- 启动 Cubic,选择原版 ISO → 进入工作区 → 挂载并解压 casper/filesystem.squashfs → 进入 chroot 安装/删除软件、修改配置 → 生成新的 filesystem.squashfs → 生成自定义 ISO。
- 命令行流程要点:
- 准备:sudo apt install squashfs-tools genisoimage xorriso
- 挂载原版 ISO:sudo mount -o loop ubuntu-22.04.iso /mnt
- 复制内容:rsync -a /mnt/ ./(排除 squashfs)
- 解压根文件系统:sudo unsquashfs ./casper/filesystem.squashfs && sudo mv squashfs-root edit
- Chroot 定制:
- sudo mount --bind /dev edit/dev
- sudo mount -t proc none edit/proc
- sudo chroot edit(执行 apt 安装/配置,完成后 exit)
- sudo umount edit/dev && sudo umount edit/proc
- 重新打包:sudo mksquashfs edit filesystem.squashfs -comp xz
- 更新元数据:
- du -sx --block-size=1 edit > casper/filesystem.size
- find . -type f -print0 | xargs -0 md5sum > md5sum.txt
- 生成 ISO(BIOS+UEFI 双引导示例):
- sudo mkisofs -o …/custom-linux.iso
-V “My Linux”
-b isolinux/isolinux.bin -c isolinux/boot.cat
-no-emul-boot -boot-load-size 4 -boot-info-table
-eltorito-alt-boot -e efi/boot/bootx64.efi -no-emul-boot .
方法三 使用 mkosi 从零构建可启动磁盘镜像或 ISO(现代化方案)
- 适用场景:面向自动化/可重复构建,直接生成 disk 镜像或再封装为 ISO,可集成内核、引导与软件包清单。
- 安装与最小配置:
- 安装:sudo apt update && sudo apt install mkosi systemd-container squashfs-tools grub-pc-bin
- 示例 mkosi.conf(生成可启动磁盘镜像):
- [Distribution]
- Distribution=ubuntu
- Release=jammy
- [Output]
- Format=disk
- Output=custom-linux.img
- Bootable=yes
- [Content]
- Packages=linux-image-generic systemd-boot bash curl nano
- 构建与转换:
- 构建:sudo mkosi build(生成 custom-linux.img)
- 简易封装 ISO(示例思路,按实际引导结构调整):
- mkdir iso-root && sudo mount custom-linux.img iso-root
- sudo mkisofs -o custom-linux.iso -b boot/grub/x86_64-efi/grub.img iso-root
- sudo umount iso-root
方法选择建议与注意事项
- 选择建议:
- 需要“快速把当前系统做成可启动镜像/启动盘”优先用 Systemback(简单、图形化;注意 .sblive > 4GB 的 ISO 限制与转换方案)。
- 需要“深度定制安装介质(预装软件/配置)”优先用 Cubic 或“手动 ISO 流程”(可控性强,适合制作模板 ISO)。
- 需要“自动化、可重复构建、集成到 CI”优先用 mkosi(现代化、可脚本化,易生成磁盘镜像与 ISO)。
- 注意事项:
- 空间与介质:确保 Working directory/U 盘/目标磁盘 空间充足;大于 4GB 的镜像在 ISO9660 下受限,Systemback 可用 UDF 或转换工具链处理,或改用支持大文件的方案(如 UDF 生成或 mkosi/Cubic 流程)。
- 引导与兼容性:同时兼顾 BIOS/UEFI 启动;UEFI 推荐使用 systemd-boot/GRUB EFI,ISO 构建时注意引导文件路径与参数。
- 还原/部署:镜像用于还原到同型号或兼容硬件更稳妥;跨硬件部署请关注 显卡/网卡/存储驱动 与 initramfs 配置。
- 数据安全:制作镜像前做好重要数据完整备份;chroot/定制操作请谨慎,避免误删关键包或破坏引导。