#!/bin/bash # ============================================================================ # DarkForge Linux — Phase 0: Environment Setup # ============================================================================ # Purpose: Set up the build environment, directory structure, and variables # needed for the cross-toolchain bootstrap. # Inputs: LFS - path to target partition (e.g., /mnt/darkforge) # Outputs: Environment variables, directory tree, lfs user # Assumes: Target partition is mounted at $LFS, running as root # Ref: LFS 13.0 Chapters 2-4 # ============================================================================ set -euo pipefail # --- Configuration ----------------------------------------------------------- # These must be set before running this script. # Override via environment or edit here. LFS="${LFS:-/mnt/darkforge}" LFS_TGT="x86_64-darkforge-linux-gnu" # DarkForge hardware-specific flags (applied in native builds, NOT cross-compile) DF_CFLAGS="-march=znver5 -O2 -pipe -fomit-frame-pointer" DF_CXXFLAGS="${DF_CFLAGS}" DF_MAKEFLAGS="-j32" DF_LDFLAGS="-Wl,-O1,--as-needed" # --- Verify mount point ------------------------------------------------------ if ! mountpoint -q "${LFS}" 2>/dev/null; then echo "ERROR: ${LFS} is not a mount point." echo "Run 000-setup-disk.sh first to create and mount the LFS filesystem," echo "or mount a partition manually: mount /dev/ ${LFS}" exit 1 fi echo "=== DarkForge Phase 0: Environment Setup ===" echo "LFS=${LFS}" echo "LFS_TGT=${LFS_TGT}" # --- Create directory structure ----------------------------------------------- echo ">>> Creating directory structure..." mkdir -pv "${LFS}/sources" mkdir -pv "${LFS}/tools" mkdir -pv "${LFS}"/{etc,var,usr/{bin,lib,sbin}} # Create /lib64 symlink for x86_64 case $(uname -m) in x86_64) mkdir -pv "${LFS}/lib64" ;; esac # Symlinks for unified /usr layout (LFS 13.0 style) for dir in bin lib sbin; do ln -sfv "usr/${dir}" "${LFS}/${dir}" 2>/dev/null || true done # Set sticky bit on sources directory chmod -v a+wt "${LFS}/sources" # --- Create lfs user (if not exists) ------------------------------------------ echo ">>> Setting up 'lfs' build user..." if ! id lfs &>/dev/null; then groupadd lfs useradd -s /bin/bash -g lfs -m -k /dev/null lfs echo "Created 'lfs' user." else echo "'lfs' user already exists." fi # Grant lfs ownership of build directories chown -v lfs "${LFS}"/{usr{,/*},lib,var,etc,bin,lib64,sbin,tools,sources} 2>/dev/null || true # --- Write lfs user's shell profile ------------------------------------------- echo ">>> Writing lfs shell profile..." cat > /home/lfs/.bash_profile << 'PROFILE_EOF' exec env -i HOME=$HOME TERM=$TERM PS1='\u:\w\$ ' /bin/bash PROFILE_EOF cat > /home/lfs/.bashrc << BASHRC_EOF set +h umask 022 LFS=${LFS} LC_ALL=POSIX LFS_TGT=${LFS_TGT} PATH=${LFS}/tools/bin:/usr/bin if [ ! -L /bin ]; then PATH=/bin:\$PATH; fi export LFS LC_ALL LFS_TGT PATH export MAKEFLAGS="${DF_MAKEFLAGS}" BASHRC_EOF chown lfs:lfs /home/lfs/.bash_profile /home/lfs/.bashrc # --- Write persistent environment config for other scripts -------------------- cat > "${LFS}/sources/darkforge-env.sh" << ENV_EOF # DarkForge build environment — sourced by all toolchain scripts export LFS="${LFS}" export LFS_TGT="${LFS_TGT}" export LC_ALL=POSIX export PATH="${LFS}/tools/bin:/usr/bin" if [ ! -L /bin ]; then export PATH="/bin:\${PATH}"; fi export MAKEFLAGS="${DF_MAKEFLAGS}" # Hardware-specific flags (only for native compilation, NOT cross-compile) export DF_CFLAGS="${DF_CFLAGS}" export DF_CXXFLAGS="${DF_CXXFLAGS}" export DF_LDFLAGS="${DF_LDFLAGS}" ENV_EOF echo "=== Environment setup complete ===" echo "" echo "Next steps:" echo " 1. Download source tarballs to ${LFS}/sources/" echo " 2. Switch to lfs user: su - lfs" echo " 3. Run toolchain build scripts in order (001, 002, ...)"