💻

Building a Minimalistic Linux Distribution

Jul 1, 2024

Building a Minimalistic Linux Distribution

Overview

  • Presenter: Victor Engelmann
  • Objective: Build an extremely minimalistic Linux distribution (~12 MB) that provides a shell for basic file navigation and program execution.
  • Tools: BusyBox and the Linux kernel.
  • Key Steps: Building a kernel, creating a root partition containing BusyBox.

Key Components

  1. Linux Kernel

    • Essential for any Linux system.
    • Provides instructions and drivers on startup.
    • Download from kernel.org (use version 5.x).
    • Steps to build:
      • Download and extract the kernel source code.
      • Configure with make defconfig and compile with make -j8.
  2. BusyBox

    • Explanation: A single binary providing common Unix utilities.
    • Functionality: Compact replacement for a full suite of Unix utilities.
    • Download from busybox.net (version 1.34.1 recommended).
    • Steps to build:
      • Download and extract BusyBox source code.
      • Configure with make defconfig.
      • Modify configuration for a static build (use CONFIG_STATIC=y).
      • Compile using make -j8.

Building Process

  1. Preparation

    • Create text files (busybox.sh and temp.sh) for step-by-step compilation.
    • Goal: Consolidate into a single script for automatic building.
  2. Downloading and Extracting Sources

    • Use variables for versioning to simplify changes.
    • Create a src directory to download and extract files.
  3. Kernel Compilation

    • Download and compile the kernel follows the process described above.
    • Use default configurations to simplify the setup.
  4. BusyBox Compilation

    • Ensure a static build to avoid dependency on dynamic link libraries.
    • Update configuration using scripting tools for efficiency (sed for modifying .config).
    • For Manjaro systems, installed additional packages musl and kernel-headers-musl.
    • Use musl-gcc to compile BusyBox.

Combining Kernel and BusyBox

  1. File Structure

    • Create directories: initrd, bin, dev, proc, sys.
    • Copy BusyBox binary to bin.
    • Create symbolic links for each BusyBox command in bin.
  2. Init Script

    • Shell script to mount virtual filesystems and start a shell (bin/sh).
    • Optional: Add poweroff -f to avoid kernel panic on exit.
  3. Packaging

    • Use cpio to create an initial RAM file system (initrd.img).
    • Combine with the kernel image for initialization.

Running the System

  1. QEMU Configuration

    • Use qemu-system-x86_64 to boot the system.
    • Commands: -kernel for the kernel image, -initrd for the initial ramdisk image.
    • Optional: -nographic to run without a graphical window.
  2. Debugging Tips

    • Catch kernel output using console redirection.
    • Handle kernel panics by adding poweroff command in the init script.

Conclusion

  • Result: A minimalistic but functional Linux system.
  • Code Summary: Approximately 63 lines of shell scripting.
  • Encouragement to share, like, and subscribe to the channel.