Linux From Scratch

Learn via video courses
Topics Covered

Overview

Linux from Scratch is a book and a Linux distribution written by Gerard Beekmans, and currently maintained by Bruce Dubbs. The book provides step-by-step instructions to build Linux from Scratch.

Unlike other major Linux distributions, Linux From Scratch does not provide an installation medium. The users are supposed to build Linux From Scratch by following the instructions from the book. As there is no actual installation medium like an ISO, the user needs to have a Linux distribution already installed on their system to build the project.

What is Linux From Scratch?

As discussed earlier, Linux From Scratch is a tutorial book and a learning project rather than an actual distribution. It does not come with any software packages pre-installed or even provide an installation disk. The project merely exists as a book, which the user is supposed to read, understand and follow along to build a minimal Linux distribution.

Since Linux From Scratch does not have an installation medium, we need a system with an installed Linux distribution to begin building the project. The host distribution requires the GNU coreutils installed, commonly called the GNU shell utilities. It includes programs and software like the bash shell, grep, awk, GCC, bison, and binutils among many others. These programs are necessary to build the download and the build packages required to build the Linux From Scratch Project.

The aim of the book is not to make a useable Linux distribution but to provide the end user with a learning experience and a greater insight into the internal workings of a Linux distribution.

We do not have any pre-compiled packages for the system. When we start with the project, we download the source code of the required system utilities and then build them manually to get them working together. The Linux From Scratch project does not even come with its package manager so it is quite difficult to keep the system and its installed packages up-to-date since each update requires a near complete rebuild of system packages and libraries.

The Linux From Scratch book is divided into various sections, which correspond to different stages of the project while we're building it. It first starts with setting up a different base in the already installed system, followed by downloading the package source code in source tarballs (.tar compressed files), building them, linking them, building the kernel and the bootloader and finally, booting into the new system.

The Linux From Scratch project's first book is vast but it does not represent the final vision of the project. Its next iteration, which is called the BLFS book (Beyond Linux From Scratch) exists as well. This book aims to provide the user with the ability to build the X window system, a desktop environment and other system utility apps to allow the users to use and build their Linux From Scratch systems with a usable Graphical User Interface system.

How to Make Your Own Linux OS?

Linux From Scratch is the perfect project if we want to start building our own Linux distribution, either for public use or for learning purposes. As we discussed earlier, to build the Linux From Scratch project, we need an existing host Linux distribution. Any distribution with recent enough GNU system utilities is enough to get started. Popular distributions like Ubuntu, Debian, Fedora, OpenSuse or Arch Linux are good choices to get started and follow along with the Linux From Scratch tutorial book.

One important thing to note about Linux From Scratch book is that it's not a project aimed towards beginners. The project expects the end user to be proficient and comfortable with Linux and the command-line interface.

The Linux From Scratch tutorial book is divided into multiple chapters, each representing a state and milestone towards the completion of the project. For example, chapter 2 of the book describes the creation of partitions and adding filesystems to these partitions. Some chapters are solely dedicated to building and installing packages on the newly-created filesystems. These are some of the most important chapters since a major part of building Linux From Scratch involves the correct building, installation and linking of these packages with each other. The user needs to follow the book precisely as it says. If a step is missed during the installation process, the user might end up with an unbootable system. It is important that all the instructions mentioned in the book are followed correctly and no step is missed during the execution.

Once we have an installed distribution, we start building the Linux From Scratch project. We need to follow along with the instructions from the Linux From Scratch tutorial book. This build of the book focuses on building Linux From Scratch using the SysV init program. The Linux From Scratch project also provides a book that focuses on building a system with the Systemd init system. It can be found in the Systemd Book on the Linux From Scratch website.

The first step to getting started with the project is the creation of some environment variables. These variables are important as these are used in various commands in the different steps of Linux From Scratch. If these variables are not exported to the host system, some of the commands might cause permanent damage to the host system. The variables are exported in the user's shell configuration file, usually .bashrc.

Once the environmental variables are exported, we need to create a few separate partitions using the host file system's utilities. The Linux From Scratch's file system will reside in these newly-created partitions. When these partitions are created, filesystems like ext4 and FAT are created on these partitions.

When these partitions are created, we move on to downloading the source code for all the packages. These packages will be installed on the system. The source code for these packages is downloaded from their source git repositories. The source code is downloaded as .tar archive files which are also called tarballs. These tarballs are optionally compressed using the Gzip or XZ algorithms and are represented by the file extensions .tar.gz or .tar.xz.

After the package tarballs are downloaded, the first step of building Linux From Scratch is initiated. We started with compiling a toolchain for the Linux From Scratch system. This includes building the essential system and compilation tools like The GNU C compiler, bison and the Linux kernel API headers. These are some of the programs and files which are necessary to set up an independent Linux distribution.

When building a program from its source code, we first need to extract the source code from the downloaded tarball. It is done using the command:

The extension of the tarball may change depending on the compression algorithm used to compress the source tarball.

We start by extracting the binutils package. Binutils is a package that contains a linker, loader and a few other tools for handling object files. A linker and loader are necessary programs required by the GNU C compiler to build other programs. At this stage, we are still using the compiler tools of the host system. This is because, at this stage, the Linux From Scratch system is not complete enough to build programs using its compiler.

Once the binutils package build is finished, we start building the GNU C compiler. This forms the basis of the complete system. We also compile the Linux API headers and the Glibc program. The Glibc package is a crucial program since it contains the entire GNU C library. All programs running on Linux depend on this library as their first dependency, hence it is quite important to make sure the Glibc package builds without any errors.

All Linux From Scratch packages are built following the same command structure:

The ./configure command runs a script that most packages have. This configure script is used to modify how the packages are built with the help of the flags that the user passes to the script.

Once the package is configured, we build the package. Most packages use the GNU Make program to build and install the packages.

To build a package with the make command, we usually run

We can optionally pass in the number of jobs that are used for the compilation in parallel. It is passed as the number of CPU cores that we want to compile the program with. This is written as

This command passes in 8 CPU cores to the make command.

After the package is built, it is installed by running the command:

This installs the package in its target directory that was set with the ./configure command.

After some of the basic packages are built, the Linux From Scratch tutorial book explains the installation of some other common system utilities required for further compilation steps.

When a base system is ready, the book moves to building an isolated environment for the system. It is because, at this stage, the system has been built using the compiler of the host system. This has the possibility of causing errors in the new Linux From Scratch system. Hence, once a base system has been set up, we chroot into this base system to further compile the project using its compiler. Chroot stands for change root. It is called change root because we switch from the root user of the host system to the root user of the Linux From Scratch system.

At this point, the book is finished halfway. Now, using this newly chrooted base system, the core system libraries like the binutils and the bison program, the GNU C compiler, the Glibc program, and some other important programs and tools are re-built. It is done to ensure the final system is not built with any dependency of the host system.

After the core tools and programs have been re-compiled, the Linux From Scratch tutorial book moves on to building the common system utilities and networking tools required to run the system.

After these are built, the only thing left to do is to configure the kernel, install the bootloader and configure the network settings for the system. The chapters after Chapter 8 deal with this part of system configuration.

When the Linux kernel source code is decompressed, the kernel is configured by running commands like make menuconfig or make defconfig.

This opens a Terminal User Interface. The user needs to select the options and features they require in their kernel. It is a very crucial step since enabling the wrong features can render the system unbootable.

Once the kernel is configured, it is built as a bootable image and moved to the boot partition of the system. After installation of the kernel, it is necessary to install and configure a bootloader to allow booting into the newly built system. The bootloader used in both books is GRUB. GRUB is installed on the boot partition and configured to boot the kernel built by the user in the previous step.

Before the system is rebooted into this newly-built system, it is important to enable and configure networking in the Linux From Scratch system to make sure we can connect to the internet. The Linux From Scratch tutorial book mentions various ways of configuring networking inside the system. The user is expected to configure the networking of the system as per their needs and requirements. Once satisfactory changes are made, it is time to boot into this new system.

Building Linux From Scratch is a lengthy and time-consuming process but the end user learns a lot about the internal working of a Linux distribution by following along this book.

List of Packages in LFS

In Linux From Scratch, we download the source tarballs of packages and the download weights at around 400 MB.

Some of the most important packages downloaded and used to build Linux From Scratch are:

  • Bash
  • Binutils
  • Bison
  • Coreutils
  • Dbus
  • Findutils
  • Gawk
  • Flex
  • Glibc
  • Grep
  • GRUB
  • Kmod
  • The Linux kernel
  • M4
  • Make
  • OpenSSL
  • Systemd
  • Tar
  • Zlib

These are some of the most important packages used to build Linux From Scratch. While this list is not complete and we need to download more packages to get a basic Linux From Scratch system to boot, this list represents the most important packages which are required by any Linux distribution to provide a working environment for the user.

Benefits of Linux From Scratch

  • While Linux From Scratch is not a distribution that can be used as a daily driver, it gives the end user who follows the Linux From Scratch tutorial book a deep insight into the working of a Linux distribution.
  • Building the Linux From Scratch project provides experienced users with the ability to customize their Linux systems as per their very specific needs. Users are even free to change the libraries used by a program that they see fit, as long as these shared libraries work with all the software on the system that use that library.
  • The project also gives the users the ability to learn about complex topics like building a toolchain and cross-compiling programs.

Conclusion

  • Linux From Scratch is a Linux distribution and a book that provides instructions on how to build a custom Linux distribution. It is aimed towards experienced Linux users who wish to gain a deeper insight into the internal working of a Linux distribution.
  • The Linux From Scratch project does not provide a downloadable medium but rather is supplied as a book. The end user is expected to build the system by downloading the source code of packages, building them locally and installing them.
  • We need an already installed Linux system with the GNU coreutils to build Linux From Scratch. We utilize the tools of the existing Linux installation to build the initial base setup for the project and then isolate the project from the host system after developing and building the initial base system.
  • While the project is not supposed to be used as a daily driver due to the lack of a package manager and a manageable way to keep the system up-to-date, it provides a valuable learning experience to the end users.
  • Linux From Scratch can be used as a base for building our own Linux distribution from scratch which can be made available for mass use.