Building Linux from Scratch

Loading Packages and Patches

Chapter 3 of the LFS documentation defines LFS with its later feature set. In addition to the build tools, you need to download the desired sources for a choice of software packages that define the system. The makers of LFS have taken the trouble to make a preselection, resolve their version dependencies, and list them in a text file that you can download and conveniently pass into Wget (Listing 2).

Listing 2

Downloading a Software Set

$ wget http://lfs.linux-sysadmin.com/lfs/downloads/stable-systemd/wget-list
$ wget --input-file=wget-list --continue --directory-prefix=$LFS/sources

The LFS developers provide a number of patches that fix bugs or adapt the packages to the future target system. In addition, they explain all the options of the configure calls. An Md5sum checksum list, which is also provided, helps to check the checksums of the almost 90 packages that are downloaded.

Concluding Work

In LFS chapter 4, the root user creates the directory structures required for the build $LFS/{bin,etc,lib,lib64,sbin,tools,usr,var}, as well as the lfs user as owner of the build directories. Then it is a matter of becoming the lfs user and emptying and optimizing the shell environment for this account.

Since the new environment is a non-login shell, it needs a .bashrc file to extend and customize the shell environment – for example, to set environment variables such as $LFS, LC_ALL, or PATH. The $LFS/tools/ directory will contain the cross-compiler later on.

The next step is to restart the build host to make sure there are no problems and that it works. At this point at the latest, it is advisable to create a VM snapshot.

Compiling the Cross Toolchain

In chapter 5 of the LFS manual, you remain logged in as the lfs user, for whom the real work now begins: building the cross-compiler and its tools (Listing 3). The cross-compiler and its tools are temporarily stored in $LFS/tools/; you can already install the libraries where they will need to be in the future.

Listing 3

Testing the Cross-Compiled GCC

$ echo 'int main(){}' > dummy.c
$ $LFS_TGT-gcc dummy.c
### Shows the information from the segment headers
### of the file, if any
$ readelf -l a.out | grep '/ld-linux'
### Should return something like:
### [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]

The workflows for building the software packages almost always remain the same until the end. You change to the sources/ directory, where you unpack the source files using tar xf. Then change to the new directory created in the process, where you create a build/ directory, if necessary.

Now the package can be compiled and installed. Then delete the source code directory again. You can do this in a first run for the linker and assembler, the cross-compiler, the Linux API headers, glibc, and libstdc++.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Linux from Scratch 6.5 Available

    The Linux from Scratch (LFS) intensive training has been updated to version 6.5.

  • Linux From Scratch

    Linux From Scratch helps you create a custom Linux system with everything you need and nothing more. And as you build your system, you’ll get an insider’s look at how Linux really works.

  • Buildroot

    Whether you need a tiny OS for 1MB of flash memory or a complex Linux with a graphical stack, you can quickly set up a working operating system using Buildroot.

  • detLFS

    The detLFS project provides an ideal foundation for compiling Linux from source code, either to experience the fundamentals of how Linux works or to prepare an operating system for a project with very specific requirements.

  • Mozilla and Samsung Collaborate on New Browser Engine

    Mozilla is collaborating with Samsung on a new web browser engine called Servo.

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

Subscribe to our Linux newsletters

News