[wip] Linux from Scratch


What is Linux From Scratch?
Linux From Scratch (LFS) is a project that provides you with
step-by-step instructions for building your own customized Linux system
entirely from source.

About this Guide
LFS on ARM Cloud? I’ve done it before, why not again?

I’m following the instructions of the current development LFS, showing the Arm specific differences.

We do what we must because we can.

Target Audience



  • Finish the System
  • understand the boot process
  • Build a webserver
  • write a script that does all this
  • write this tutorial

Setup the System

I’m starting on a fresh Trusty Image, additional 20GB LSSD which will keep our final system.

Preparing the system

apt-get update
apt-get dist-upgrade
apt-get install build-essential gawk m4

mkfs.ext4 /dev/nbd1
mkdir /mnt/lfs
mount /dev/nbd1 /mnt/lfs
mkdir -pv /mnt/lfs/{tools,download,sources}
ln -sv mnt/lfs/tools /

Creating the user and setting up the environment

groupadd lfs
useradd -s /bin/bash -g lfs -m -k /dev/null lfs
chown -Rv lfs:lfs /mnt/lfs/
chown -Rv lfs:lfs /tools
cat > /home/lfs/.bash_profile << "EOF"
exec env -i HOME=$HOME TERM=$TERM PS1='\u:\w\$ ' /bin/bash

You need to set ARMV7 specific CFLAGS

cat > /home/lfs/.bashrc << "EOF"
set +h
umask 022
LFS_TGT=$(uname -m)-lfs-linux-gnueabihf
CFLAGS="-O2 -pipe -march=native -mfloat-abi=hard -Wa,-mimplicit-it=thumb"

chown lfs:lfs /home/lfs/{.bashrc,.bash_profile}

If you later strange errors like “instruction not allowed in it block” you forgot -mimplicit-it=thumb
LFS_TGT has to be -gnueabihf

switch, check if everything looks okay

su - lfs
echo $LFS_TGT
>> armv7l-lfs-linux-gnueabihf
echo $LFS
>> /mnt/lfs

Get the packages

We use the default LFS packages and add a small patch based from the PILFS project

cd $LFS/sources
wget http://www.linuxfromscratch.org/lfs/view/development/wget-list
wget --input-file=wget-list --continue --directory-prefix=$LFS/sources

took about 5 minutes to download everything

Constructing a Temporary System
Follow the instructions, sometimes you have to add ARM specific

5.4.1. Binutils pass 1
SBU is about 7.5 minutes

5.4.2 GCC-4.9.2 - Pass 1
GCC needs a patch to take advantage of the floating point hardware by default. We grab it from PILFS.

lfs@c1-10-1-0-12:/mnt/lfs/sources$ wget http://www.intestinate.com/pilfs/patches/gcc-4.9.0-pi-cpu-default.patch
patch -Np1 -i ../gcc-4.9.0-pi-cpu-default.patch

GCC also needs the additional files linux-eabi.h and linux-elf.h to have their dynamic linker locations changed in both passes of chapter 5:

for file in \
$(find gcc/config -name linux64.h -o -name linux.h -o -name sysv4.h -o -name linux-eabi.h -o -name linux-elf.h);
  cp -uv $file{,.orig}
  sed -e 's@/lib\(64\)\?\(32\)\?/ld@/tools&@g' \
      -e 's@/usr@/tools@g' $file.orig > $file
  echo '
#define STANDARD_STARTFILE_PREFIX_1 "/tools/lib/"
#define STANDARD_STARTFILE_PREFIX_2 ""' >> $file
  touch $file.orig

There’s also an issue with GMP while building GCC in chapter 5 that can be worked around with the following sed:

sed -i 's/none-/armv7l-/' Makefile

gcc sometimes seems to ignore my CFLAGS, if you encounter errors Like “instruction not allowed in it block” rerun configure with CFLAGS="-Wa,-mimplicit-it=thumb" CXXFLAGS="-Wa,-mimplicit-it=thumb" addet at the end of the instruction

make should take about 25 minutes.

5.7. Glibc-2.20
If everything is working as expected, the output of the toolchain test should be:

readelf -l a.out | grep ': /tools'
[Requesting program interpreter: /tools/lib/ld-linux-armhf.so.3]

5.10 GCC-4.9.2 - Pass 2
If you didn’t delete the source tree you can skip most of the adjustments, else it’s the same as in pass 1.
Don’t forget the sed after the configure
build time should be around 75 Minutes

to be continued



Can’t you just debootstrap an OS flavor you want?


Yeah, I could do that.

LFS is about the hard way. At least it compiles faster than on my Raspi cluster.


I just finished the Script for Chapter 5


Why don’t put the link in your guide? :slight_smile:


I’ll add it. There are still heavy changes, I think I’ll find time to reformat it over the weekend.


have you gotten this working for yourself yet? very excited to see what happens.

would you be better off with this sort of environment?


I’m about halfway done finishing the basic system, might be done by tomorrow.
Networking and bootscripts might take some time.