Ubuntu From Scratch如何自定义设置
导读:Ubuntu From Scratch 自定义设置实战指南 一 核心思路与目录结构 构建流程要点:在宿主机使用 debootstrap 生成最小化系统 → 进入 chroot 完成系统配置与软件预装 → 生成 squashfs 根文件系统...
Ubuntu From Scratch 自定义设置实战指南
一 核心思路与目录结构
- 构建流程要点:在宿主机使用 debootstrap 生成最小化系统 → 进入 chroot 完成系统配置与软件预装 → 生成 squashfs 根文件系统 → 用 GRUB 制作 BIOS+UEFI 双启动 → 通过 xorriso 生成可启动 ISO。
- 常用目录与文件作用:
- image/casper/:存放 Live 启动关键文件(如 vmlinuz、initrd、filesystem.squashfs、filesystem.manifest、filesystem.size)。
- image/isolinux/grub.cfg:GRUB 启动菜单与内核启动参数。
- chroot/:目标系统的根目录,所有软件安装与系统配置在此完成。
二 环境与构建准备
- 安装构建依赖(宿主机):
- 命令:
sudo apt-get install binutils debootstrap squashfs-tools xorriso grub-pc-bin grub-efi-amd64-bin mtools
- 命令:
- 初始化最小系统(示例为 amd64/focal):
- 命令:
sudo debootstrap --arch=amd64 --variant=minbase focal $HOME/live-ubuntu-from-scratch/chroot http://us.archive.ubuntu.com/ubuntu/
- 命令:
- 挂载必要文件系统并进入 chroot:
- 挂载:
sudo mount --bind /dev chroot/dev & & sudo mount --bind /run chroot/run - 进入:
sudo chroot chroot - 在 chroot 内:
mount -t proc proc /proc & & mount -t sysfs sys /sys & & mount -t devpts devpts /dev/pts - 环境:
export HOME=/root & & export LC_ALL=C
- 挂载:
- 基础配置(chroot 内):
- 主机名:
echo "ubuntu-fs-live" > /etc/hostname - 软件源(示例为 focal):
cat > /etc/apt/sources.list < < EOF ... EOF- 更新:
apt-get update
- 安装并启用 systemd:
apt-get install -y libterm-readline-gnu-perl systemd-sysv - 初始化 machine-id:
dbus-uuidgen > /etc/machine-id & & ln -fs /etc/machine-id /var/lib/dbus/machine-id - 处理 initctl 兼容性:
dpkg-divert --local --rename --add /sbin/initctl & & ln -s /bin/true /sbin/initctl
- 主机名:
三 常用自定义项与配置位置
- 预装软件
- 方式一(chroot 内):
apt-get install -y < pkg1> < pkg2> ... - 方式二(项目化):在 live-custom-ubuntu-from-scratch 框架中维护 packages.list,由 build.sh 自动安装,便于版本化管理与复用。
- 方式一(chroot 内):
- 启动菜单与内核参数(GRUB)
- 文件:image/isolinux/grub.cfg
- 关键示例:
- 试用:
linux /casper/vmlinuz boot=casper nopersistent toram quiet splash --- - 安装:
linux /casper/vmlinuz boot=casper only-ubiquity quiet splash --- - 内存测试(BIOS):
linux16 /install/memtest86+ - 内存测试(UEFI):加载 EFI 可执行文件进行链式引导
- 试用:
- Live 系统与安装目标清单
- 生成清单:
sudo chroot chroot dpkg-query -W --showformat='${ Package} ${ Version} \n' | sudo tee image/casper/filesystem.manifest - 桌面安装清单(安装器完成后保留的包):
cp image/casper/filesystem.manifest image/casper/filesystem.manifest-desktop - 常见移除项(按需):
ubiquity、casper、discover、laptop-detect、os-prober
- 生成清单:
- 桌面与安装器定制
- 使用 live-custom-ubuntu-from-scratch 时,可在 config/ 下分别定制:
- boot/:引导相关配置
- casper/:如 casper.conf(读写模式、挂载点等)
- debian-installer/:键盘、网络、分区等安装流程细节
- isolinux/:启动菜单
- packages.list:预装软件
- ubiquity-slideshow/:安装向导幻灯片
- 使用 live-custom-ubuntu-from-scratch 时,可在 config/ 下分别定制:
- 图形界面与主题(GNOME 示例)
- 工具:
sudo apt-get install gnome-tweak-tool - 主题与图标:解压到 /usr/share/themes、/usr/share/icons
- 锁屏背景:编辑 /etc/alternatives/gdm3.css 中的 #lockDialogGroup
- Shell:安装 zsh 与 oh-my-zsh,配置主题(如 Powerlevel10k)与插件(语法高亮、自动建议)
- 工具:
四 生成镜像与启动测试
- 打包根文件系统
- 压缩:
sudo mksquashfs chroot image/casper/filesystem.squashfs - 记录大小:
printf $(sudo du -sx --block-size=1 chroot | cut -f1) > image/casper/filesystem.size - 说明文件:
image/README.diskdefines(示例含 DISKNAME、ARCH=amd64 等)
- 压缩:
- 制作 GRUB 启动镜像
- UEFI:
grub-mkstandalone --format=x86_64-efi --output=isolinux/bootx64.efi "boot/grub/grub.cfg=isolinux/grub.cfg" - BIOS:
grub-mkstandalone --format=i386-pc --output=isolinux/core.img ... - 生成 efiboot.img(FAT16 UEFI 启动盘镜像)并拷贝 bootx64.efi
- 合并 BIOS 引导:
cat /usr/lib/grub/i386-pc/cdboot.img isolinux/core.img > isolinux/bios.img
- UEFI:
- 生成 ISO(BIOS+UEFI 双启动)
- 命令:
sudo xorriso -as mkisofs -iso-level 3 -full-iso9660-filenames -volid "Ubuntu from scratch" -output "../ubuntu-from-scratch.iso" -eltorito-boot boot/grub/bios.img -no-emul-boot -boot-load-size 4 -boot-info-table --eltorito-catalog boot/grub/boot.cat --grub2-boot-info --grub2-mbr /usr/lib/grub/i386-pc/boot_hybrid.img -eltorito-alt-boot -e EFI/efiboot.img -no-emul-boot -append_partition 2 0xef isolinux/efiboot.img -m "isolinux/efiboot.img" -m "isolinux/bios.img" -graft-points "/EFI/efiboot.img=isolinux/efiboot.img" "/boot/grub/bios.img=isolinux/bios.img" "."
- 命令:
- 校验与测试
- 校验:
find image -type f -print0 | xargs -0 md5sum | grep -v -e 'md5sum.txt' -e 'bios.img' -e 'efiboot.img' > image/md5sum.txt - 测试:用 QEMU、VirtualBox 或实体机以 UEFI/BIOS 模式启动 ISO,验证 Live 环境、安装器与自定义内容。
- 校验:
五 进阶与替代方案
- 图形化定制工具 Cubic
- 安装:
sudo apt-add-repository ppa:cubic-wizard/release & & sudo apt install --no-install-recommends cubic - 特点:提供可视化 chroot 工作流,便于更换软件源、预装软件、设置 Plymouth 开机动画、壁纸与主题,一键生成 ISO,适合不熟悉命令行的用户。
- 安装:
- 从源码编译软件(进阶)
- 准备:
sudo apt-get install build-essential - 流程:
./configure → make → sudo make install - 注意:源码安装不会自动更新,需自行维护安全与依赖,生产环境优先使用仓库或 snap/flatpak。
- 准备:
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: Ubuntu From Scratch如何自定义设置
本文地址: https://pptw.com/jishu/753402.html
