mylfs/phase6_8_chroot.sh
2025-10-20 10:02:23 +01:00

81 lines
1.6 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# phase6_8_chroot.sh
# LFS 12.2 阶段6~8完整系统自动构建
set -euo pipefail
IFS=$'\n\t'
LFS_WORKDIR="$PWD/lfs_build"
CPU_CORES="$(nproc)"
LFS_SRC="$LFS_WORKDIR/sources"
ROOTFS="$LFS_WORKDIR/rootfs"
info(){ echo -e "\e[34m[INFO]\e[0m $*"; }
# 挂载虚拟文件系统
info "挂载 /proc /sys /dev ..."
mkdir -p "$ROOTFS"/{proc,sys,dev}
mount --bind /dev "$ROOTFS"/dev
mount --bind /dev/pts "$ROOTFS"/dev/pts
mount -t proc proc "$ROOTFS"/proc
mount -t sysfs sys "$ROOTFS"/sys
# 阶段6~8官方包顺序部分示例
PACKAGES=(
"bash"
"coreutils"
"diffutils"
"file"
"findutils"
"gawk"
"grep"
"gzip"
"make"
"patch"
"sed"
"tar"
"xz"
"perl"
"tcl"
"vim"
)
for pkg in "${PACKAGES[@]}"; do
info "构建阶段6~8包: $pkg"
src=$(ls "$LFS_SRC" | grep "$pkg")
[ -f "$src" ] || { echo "源码 $pkg 未找到"; exit 1; }
rm -rf "$pkg-build"
mkdir "$pkg-build"
tar xf "$LFS_SRC/$src" -C "$pkg-build" --strip-components=1
cd "$pkg-build"
# Patch
for patchf in "$LFS_SRC"/*.patch; do
patch -Np1 < "$patchf" 2>/dev/null || true
done
# chroot 编译
chroot "$ROOTFS" /bin/bash -c "
cd /tmp/$pkg-build
./configure --prefix=/usr
make -j$CPU_CORES
make install
"
cd "$LFS_BUILD"
rm -rf "$pkg-build"
done
# 安装内核 & grub
info "安装内核和 grub ..."
chroot "$ROOTFS" /bin/bash -c "
cd /tmp
tar xf /build/linux-6.6.10.tar.xz
cd linux-6.6.10
make defconfig
make -j$CPU_CORES
make modules_install
cp arch/x86/boot/bzImage /boot/vmlinuz
"
chroot "$ROOTFS" grub-install /dev/sda
info "阶段6~8完整系统构建完成"