首页主机资讯ubuntu fromscratch配置方法

ubuntu fromscratch配置方法

时间2025-12-05 03:35:03发布访客分类主机资讯浏览1104
导读:Ubuntu From Scratch 配置方法 一 目标与路径选择 常见有两条路径: 构建最小可启动的 Ubuntu 根文件系统(适合容器、嵌入式或虚拟化镜像)。 制作可启动的 Ubuntu Live 镜像(ISO),支持“试用/安装...

Ubuntu From Scratch 配置方法

一 目标与路径选择

  • 常见有两条路径:
    • 构建最小可启动的 Ubuntu 根文件系统(适合容器、嵌入式或虚拟化镜像)。
    • 制作可启动的 Ubuntu Live 镜像(ISO),支持“试用/安装”与 UEFI/BIOS 双启动。
  • 术语澄清:严格意义的 LFS(Linux From Scratch) 是从源码构建整个系统、通常不依赖发行版仓库;而“Ubuntu From Scratch”多指基于 Ubuntu 内核与工具链,定制根文件系统或 Live 镜像。前者复杂度更高,后者更贴近日常使用与交付。

二 方案一 最小 Ubuntu 根文件系统并 QEMU 测试

  • 准备构建环境
    • 在 Ubuntu 主机安装工具链与依赖:
      • sudo apt update
      • sudo apt install -y build-essential libncurses-dev bison flex libssl-dev libelf-dev
  • 获取并编译内核
    • 获取 Ubuntu 内核源码(示例为 Ubuntu 20.04 分支 focal;请按你的目标版本调整):
      • git clone https://git.launchpad.net/ubuntu-kernel/ubuntu/ -b ubuntu-focal ubuntu-kernel
    • 配置与编译(示例为 x86_64;ARM 需切换 arch 配置与交叉编译链):
      • cd ubuntu-kernel
      • make defconfig
      • make -j$(nproc)
      • sudo make modules_install install
  • 创建根文件系统与 initramfs
    • 创建临时根并挂载为 tmpfs(便于快速迭代):
      • sudo mkdir -p rootfs
      • sudo mount -t tmpfs -o size=4G tmpfs rootfs
    • 复制内核与 initramfs(版本号以实际为准,如 5.15.0-xx-generic):
      • sudo cp arch/x86/boot/bzImage rootfs/boot/vmlinuz-
      • sudo update-initramfs -c -k -d rootfs/boot
      • sudo cp /boot/initrd.img- rootfs/boot/initrd.img-
  • 在 chroot 中初始化基础用户态
    • 建议先 debootstrap 一个最小化 Ubuntu 基础系统,再 chroot 配置(示例为 focal;amd64):
      • sudo debootstrap --arch=amd64 focal rootfs http://archive.ubuntu.com/ubuntu/
      • sudo mount --bind /dev rootfs/dev
      • sudo mount --bind /proc rootfs/proc
      • sudo mount --bind /sys rootfs/sys
      • sudo chroot rootfs
        • 配置时区与基础网络:ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime;echo “nameserver 223.5.5.5” > /etc/resolv.conf
        • 设置主机名与 root 密码:echo “my-ufs” > /etc/hostname;passwd
        • 安装基础包:apt update & & apt install -y systemd-sysv locales sudo
        • 退出 chroot:exit
    • 卸载绑定挂载:sudo umount -R rootfs/dev rootfs/proc rootfs/sys
  • QEMU 测试启动
    • qemu-system-x86_64
      -kernel rootfs/boot/vmlinuz-
      -initrd rootfs/boot/initrd.img-
      -append “root=/dev/sda1 rw console=ttyS0”
      -drive file=rootfs.img,format=raw,if=virtio
      -m 2048 -nographic
    • 提示:若使用 tmpfs 根,可改为 -append “root=/dev/ram0 ramdisk_size=4096000” 直接基于内存启动;持久化请预先用 dd 创建 rootfs.img 并格式化为 ext4,然后在 append 中使用 root=/dev/sda1。

三 方案二 制作可启动 Ubuntu Live ISO(UEFI/BIOS)

  • 准备 chroot 环境
    • 参考“方案一”用 debootstrap 构建最小化 Ubuntu(如 focal),在 chroot 中安装桌面/工具与 Ubiquity(安装器):
      • apt update & & apt install -y ubuntu-desktop ubiquity casper lupin-casper
      • 清理无关包与缓存:apt clean & & rm -rf /var/lib/apt/lists/*
  • 生成 Live 镜像所需清单
    • 在构建目录生成 manifest(记录 Live 系统包清单)与 desktop 清单(安装后保留的包清单):
      • sudo chroot chroot dpkg-query -W --showformat=‘${ Package} ${ Version} \n’
        | sudo tee image/casper/filesystem.manifest
      • sudo cp image/casper/filesystem.manifest image/casper/filesystem.manifest-desktop
      • 对 desktop 清单移除安装器与 Live 相关包(示例):
        • sed -i ‘/ubiquity/d; /casper/d; /discover/d; /laptop-detect/d; /os-prober/d’
          image/casper/filesystem.manifest-desktop
  • 生成 SquashFS 只读根
    • 将 chroot 目录压缩为 Live 根文件系统:
      • sudo mksquashfs chroot image/casper/filesystem.squashfs -comp xz
  • 准备内核与引导文件
    • 复制内核与 initramfs 到 casper 目录(版本号以实际为准):
      • cp chroot/boot/vmlinuz- image/casper/vmlinuz
      • cp chroot/boot/initrd.img- image/casper/initrd
    • 可选:加入 MemTest86+(BIOS)与 MemTest86(UEFI)
      • cp chroot/boot/memtest86+.bin image/install/memtest86+
      • wget -O image/install/memtest86-usb.zip https://www.memtest86.com/downloads/memtest86-usb.zip
      • unzip -p image/install/memtest86-usb.zip memtest86-usb.img > image/install/memtest86
      • rm -f image/install/memtest86-usb.zip
  • 配置 GRUB(ISO 中 isolinux/grub.cfg 与 EFI 引导)
    • 在 image/ 下创建标记文件:touch image/ubuntu
    • 创建 image/isolinux/grub.cfg(示例菜单项):
      • search --set=root --file /ubuntu
      • insmod all_video
      • set default=“0”
      • set timeout=30
      • menuentry “Try Ubuntu FS without installing” { linux /casper/vmlinuz boot=casper nopersistent toram quiet splash — initrd /casper/initrd }
      • menuentry “Install Ubuntu FS” { linux /casper/vmlinuz boot=casper only-ubiquity quiet splash — initrd /casper/initrd }
      • menuentry “Check disc for defects” { linux /casper/vmlinuz boot=casper integrity-check quiet splash — initrd /casper/initrd }
      • menuentry “Test memory Memtest86+ (BIOS)” { linux16 /install/memtest86+ }
      • menuentry “Test memory Memtest86 (UEFI, long load time)” { insmod part_gpt insmod search_fs_uuid insmod chain loopback loop /install/memtest86 chainloader (loop,gpt1)/efi/boot/BOOTX64.EFI }
  • 生成 ISO
    • 安装必要工具:sudo apt install -y xorriso grub-pc-bin grub-efi-amd64-bin mtools
    • 目录结构示例:image/{ casper,isolinux,install,EFI/BOOT/}
    • 生成 ISO(BIOS + UEFI 双引导):
      • grub-mkrescue -o ubuntu-fs.iso
        -iso-level 3
        -J -joliet-long
        –modules=“linux16 linux normal iso9660 search all_video gfxterm png jpeg”
        image/
        –efi-directory=image/EFI/BOOT
        –boot-directory=image/isolinux

四 常见问题与优化

  • 架构与交叉编译
    • x86_64 可直接在主机编译;ARM 需准备交叉工具链与相应内核配置(如 arm64 使用 aarch64-linux-gnu-* 工具链)。
  • 内核与 initramfs
    • 若使用自定义内核,确保启用必要驱动(如 CONFIG_BLK_DEV_INITRDCONFIG_VIRTIO、文件系统如 ext4、以及你的存储/网卡驱动)。
  • 持久化与分区
    • ISO 默认不持久化;如需持久化,可在启动参数加入 persistent 并准备持久化分区/文件。
  • 镜像体积优化
    • 使用 xz 压缩的 SquashFS;在 chroot 中清理缓存与临时文件;移除不必要的语言包与文档。
  • 启动失败排查
    • 检查内核日志(dmesg)、initramfs 是否包含必要模块、GRUB 根设备搜索是否正确(search --set=root --file /ubuntu)。
  • 安全与维护
    • 在 chroot 中设置 root 密码、创建普通用户、配置 /etc/fstab、时区与网络;定期 apt update/upgrade 与内核更新。

以上流程覆盖“最小根文件系统 + QEMU 验证”和“可启动 Live ISO(UEFI/BIOS)”两条主线,可按需裁剪与扩展。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: ubuntu fromscratch配置方法
本文地址: https://pptw.com/jishu/764327.html
Ubuntu Rust 依赖如何管理 如何更新 Ubuntu Rust 版本

游客 回复需填写必要信息