Part 2: Host System Preparation
Overview
Before building your Linux distribution, you must prepare your host system with proper partitions, filesystems, and environment variables.
Step 1: Create Partitions
Recommended Partition Layout
Minimum Setup:
- Root partition (
/): 10 GB minimum, 30 GB recommended - Swap partition: 2 GB minimum (or 2x RAM for hibernation)
Advanced Setup (Optional):
/boot: 200 MB (kernel storage)/: 20 GB (system files)/home: Variable (user data)/opt: 5-10 GB (optional software)/usr/src: 30-50 GB (source code storage)- Swap: 2 GB minimum
Using fdisk
# List all disks and partitions
fdisk -l
# Start fdisk on your target disk
fdisk /dev/sda
# Inside fdisk:
# n = new partition
# p = primary partition
# (enter) = accept defaults or specify size
# t = change partition type (82 for swap, 83 for Linux)
# w = write changes and exit
Using cfdisk (Easier Interface)
# Start cfdisk with curses interface
cfdisk /dev/sda
# Use arrow keys to navigate
# Select [ New ] to create partition
# Choose [ primary ] or [ logical ]
# Enter size: 30G for root, 2G for swap
# Select [ Type ] and choose:
# - Linux (83) for root partition
# - Linux swap (82) for swap partition
# Select [ Write ] then type "yes" to confirm
# Select [ Quit ] to exit
Example Partition Scheme
# For a 50GB disk:
/dev/sda1 200M /boot (ext4)
/dev/sda2 30G / (ext4)
/dev/sda3 4G swap (swap)
/dev/sda4 15.8G /home (ext4 - optional)
Record Your Partitions
Important: Note your partition names for later use:
# Example - replace with YOUR actual partitions:
LFS_PARTITION=/dev/sda2
SWAP_PARTITION=/dev/sda3
Step 2: Create Filesystems
Format Root Partition (ext4)
# Replace /dev/sda2 with YOUR LFS partition
mkfs -v -t ext4 /dev/sda2
Filesystem Options:
- ext2: No journaling, good for /boot, faster but less recovery
- ext3: With journaling, good recovery from crashes
- ext4: Recommended - modern, fast, supports large files (up to 16 TB)
Format Other Partitions (if created)
# Boot partition (ext2 recommended for speed)
mkfs -v -t ext2 /dev/sda1
# Home partition (ext4)
mkfs -v -t ext4 /dev/sda4
Initialize Swap Partition
# NEW swap partition - needs formatting
mkswap /dev/sda3
# EXISTING swap partition - skip this step
Step 3: Set the $LFS Variable
The $LFS variable points to your LFS build directory. This is critical for the entire build process.
# Set LFS variable (use YOUR partition)
export LFS=/mnt/lfs
# Make it permanent - add to your .bashrc
echo 'export LFS=/mnt/lfs' >> ~/.bashrc
# Verify it's set
echo $LFS
# Should output: /mnt/lfs
Important:
- Set this variable in EVERY terminal session you use for building
- Add to
~/.bashrcto make permanent - Never use
sudowith a command that needs$LFSunless you export it for root
Step 4: Set Umask
# Set umask to ensure proper permissions
umask 022
# Add to ~/.bashrc for persistence
echo 'umask 022' >> ~/.bashrc
Step 5: Create Mount Point
# Create mount point directory
sudo mkdir -pv $LFS
# Verify
ls -ld $LFS
Step 6: Mount the LFS Partition
# Mount your LFS partition
sudo mount -v -t ext4 /dev/sda2 $LFS
# Verify mount
mount | grep $LFS
# Should show: /dev/sda2 on /mnt/lfs type ext4 (rw,relatime)
Enable Swap (if created)
# Activate swap partition
sudo swapon -v /dev/sda3
# Verify swap is active
swapon --show
# or
free -h
Step 7: Create Directory Structure
# Create initial directories in LFS partition
sudo mkdir -pv $LFS/{etc,var,usr,tools}
sudo mkdir -pv $LFS/usr/{bin,lib,sbin}
# For 64-bit systems, create lib64
sudo mkdir -pv $LFS/lib64
# Create sources directory for downloads
sudo mkdir -pv $LFS/sources
# Make sources writable and sticky
sudo chmod -v a+wt $LFS/sources
Directory Purpose:
$LFS/sources- Downloaded packages and patches$LFS/tools- Cross-compilation toolchain$LFS/etc- System configuration$LFS/usr- User programs and libraries$LFS/var- Variable data
Step 8: Set Up Build User (Recommended)
For security, build as non-root user:
# Create lfs user
sudo groupadd lfs
sudo useradd -s /bin/bash -g lfs -m -k /dev/null lfs
# Set password
sudo passwd lfs
# Grant ownership of build directories
sudo chown -v lfs $LFS/{usr{,/*},lib,var,etc,bin,sbin,tools,sources}
sudo chown -v lfs $LFS/lib64 # Only for 64-bit
# Switch to lfs user
su - lfs
Configure lfs User Environment
# As lfs user, create ~/.bash_profile
cat > ~/.bash_profile << "EOF"
exec env -i HOME=$HOME TERM=$TERM PS1='\u:\w\$ ' /bin/bash
EOF
# Create ~/.bashrc
cat > ~/.bashrc << "EOF"
set +h
umask 022
LFS=/mnt/lfs
LC_ALL=POSIX
LFS_TGT=$(uname -m)-lfs-linux-gnu
PATH=/usr/bin
if [ ! -L /bin ]; then PATH=/bin:$PATH; fi
PATH=$LFS/tools/bin:$PATH
CONFIG_SITE=$LFS/usr/share/config.site
export LFS LC_ALL LFS_TGT PATH CONFIG_SITE
EOF
# Load the new environment
source ~/.bash_profile
# Verify environment
echo $LFS
echo $LFS_TGT
echo $PATH
Environment Explanation:
set +h- Disables bash hash function to ensure correct programs are foundumask 022- Ensures newly created files/dirs have correct permissionsLFS=/mnt/lfs- Points to LFS partitionLC_ALL=POSIX- Sets locale to POSIX for consistent behaviorLFS_TGT- Target triplet for cross-compilationPATH- Ensures tools from /tools are found first
Step 9: Verify Mount Persists Across Reboots
Option 1: Manual Mount (After Each Reboot)
# After reboot, remount manually
export LFS=/mnt/lfs
sudo mount -v -t ext4 /dev/sda2 $LFS
sudo swapon -v /dev/sda3
Option 2: Automatic Mount via /etc/fstab
Warning: Only add to fstab AFTER you finish building LFS!
# Edit /etc/fstab (on HOST system, not LFS)
sudo nano /etc/fstab
# Add these lines (adjust partition names):
/dev/sda2 /mnt/lfs ext4 defaults 1 1
/dev/sda3 none swap sw 0 0
Build Stages Overview
LFS builds in three stages:
Stage 1: Cross-Compilation Toolchain
- Build in:
$LFS/tools - Purpose: Create independent compiler
- User:
lfs
Stage 2: Temporary Tools
- Build in:
$LFS - Purpose: Minimal system for chroot
- User:
lfs
Stage 3: Final LFS System
- Build in: chroot environment
- Purpose: Complete operating system
- User:
root(inside chroot)
Verification Checklist
Before proceeding to Part 3, verify:
# Check LFS variable
[ -z "$LFS" ] && echo "ERROR: LFS not set" || echo "LFS=$LFS"
# Check partition mounted
mount | grep -q "$LFS" && echo "LFS mounted" || echo "ERROR: LFS not mounted"
# Check directory ownership (if using lfs user)
ls -ld $LFS/sources
# Should show: drwxrwxrwt ... lfs ...
# Check environment (as lfs user)
echo $LFS_TGT
# Should show: x86_64-lfs-linux-gnu (or i686-lfs-linux-gnu)
# Check PATH
echo $PATH
# Should start with: /usr/bin or $LFS/tools/bin:/usr/bin
Common Issues and Solutions
Issue: $LFS not set in new terminal
Solution: Always run source ~/.bashrc or set manually:
export LFS=/mnt/lfs
Issue: Permission denied errors
Solution: Check ownership and use lfs user:
sudo chown -R lfs:lfs $LFS/sources
su - lfs
Issue: Partition not mounted after reboot
Solution: Remount before continuing:
export LFS=/mnt/lfs
sudo mount -v -t ext4 /dev/sda2 $LFS
Issue: Swap not available
Solution: Activate swap:
sudo swapon -v /dev/sda3
swapon --show
Next Steps
Your host system is now prepared. Proceed to:
- Part 3: Download all required packages and patches
- Part 4: Build cross-compilation toolchain
- Part 5: Compile temporary tools
Important Reminders
- Always verify
$LFSis set before running commands - Always work as
lfsuser during Stages 1-2 - Never use
sudowith compilation commands - Always verify partition is mounted before starting
- Save your partition names - you'll need them for bootloader config