
ft_linux
Build Your Own Linux Distribution from Scratch
ft_linux represents the ultimate test of low-level systems understanding: building an operating system from source without using package managers or pre-built binaries. Starting from a raw virtual disk, the project involves disk partitioning (/boot, /, swap), compiling a cross-compilation toolchain, building over 70+ essential GNU packages from source, configuring SysVinit and Eudev device management, and compiling a tailored Linux kernel with custom version branding (how_to_train_your_kernel), resulting in a standalone, bootable OS with working network connectivity.
Key Features
Custom Compiled Kernel
Configured and compiled Linux Kernel from source with modular driver support and a customized version string tagged with login credentials.
70+ Source-Compiled Packages
Compiled Glibc, Binutils, Coreutils, Bash, Make, Tar, and network utilities directly from upstream source tarballs.
FHS-Compliant Layout
Architected a strict Filesystem Hierarchy Standard tree with partitioned /boot, swap, and ext4 root partitions.
Bootable OS with Networking
Standalone system booted via GRUB with operational DHCP internet access, user management, and dynamic device detection via Eudev.
Development Journey
Host Preparation & Disk Layout
Partitioned dedicated target storage (/dev/sdb) into separate /boot (200MB ext2), swap (2GB), and root (ext4) partitions. Configured the isolated LFS build environment and user.
Cross-Toolchain Compilation
Built the temporary cross-compiler toolchain: Binutils, GCC pass 1, Linux API headers, Glibc, and Libstdc++, breaking dependencies on the host OS.
Chroot & Base System Construction
Entered the isolated chroot jail, mounted virtual kernel filesystems (/dev, /proc, /sys), and compiled 70+ essential system packages, utilities, and libraries.
Kernel Configuration & Bootloader
Executed make menuconfig to tailor kernel drivers, compiled the bzImage, set up /etc/fstab, installed GRUB to the MBR, and verified successful bare-metal booting.
Challenges & Solutions
Circular Toolchain Dependencies
Compiling a C compiler (GCC) requires an existing C compiler and standard library, which can leak host system dynamic links into the target OS.
Employed a rigorous multi-pass cross-compilation approach: building a static temporary toolchain first, adjusting the specs file, and verifying every binary against the LFS linker.
Kernel Hardware Driver Selection
Building a lean monolithic or modular kernel without bloated generic distributions while ensuring storage controllers (SATA/SCSI) and network devices initialize properly at boot.
Carefully audited hardware controller IDs using lshw and lspci, enabling essential virtio and ahci storage drivers directly built-in (y) rather than loadable modules (m).
Init System & Device Node Management
Handling dynamic device node creation on boot without modern systemd abstraction.
Configured Eudev (standalone udev fork) and crafted SysVinit inittab runlevel scripts (/etc/init.d) to mount devtmpfs and populate device nodes dynamically.
#!/bin/bash
set -euo pipefail
LFS_DISK="${1:-/dev/sdb}"
LFS="/mnt/lfs"
# Create partitions: 1: /boot (+200M), 2: swap (+2G), 3: root (rest)
parted -s "$LFS_DISK" mklabel msdos
parted -s "$LFS_DISK" mkpart primary ext2 1MiB 201MiB
parted -s "$LFS_DISK" set 1 boot on
parted -s "$LFS_DISK" mkpart primary linux-swap 201MiB 2249MiB
parted -s "$LFS_DISK" mkpart primary ext4 2249MiB 100%
# Format filesystems
mkfs.vfat -F 32 "${LFS_DISK}1" || mkfs.ext2 "${LFS_DISK}1"
mkswap "${LFS_DISK}2"
mkfs.ext4 -F "${LFS_DISK}3"
# Mount root and boot
mkdir -pv "$LFS"
mount -v -t ext4 "${LFS_DISK}3" "$LFS"
mkdir -pv "$LFS/boot"
mount -v -t ext2 "${LFS_DISK}1" "$LFS/boot"
swapon -v "${LFS_DISK}2"