Creating partitions

Create following partitions using fdisk, format with mkfs.ext4 and label it with e2label.

/dev/sda2	# Primary partition for BIOS	+2M
/dev/sda3	# Primary partition for swap	+6G
/dev/sda4 	# Extended partition
/dev/sda5 	# Logical partition for boot	+200M
/dev/sda6 	# Logical partition for usr	+10G
/dev/sda7 	# Logical partition for opt	+20G
/dev/sda8 	# Logical partition for tmp	+10G
/dev/sda9 	# Logical partition for usr/src	+60G
/dev/sda10 	# Logical partition for root	+20G
/dev/sda11 	# Logical partition for home	+101G

Add LFS variable to ~/.bashrc file

export LFS=/mnt/lfs
source ~/.bashrc

Mount partitions in $LFS directory

mkdir -pv $LFS
sudo mount -v -t ext4 /dev/sda10 $LFS		# For root (/) directory

mkdir -pv $LFS/boot
sudo mount -v -t ext4 /dev/sda5 $LFS/boot

mkdir -pv $LFS/usr
sudo mount -v -t ext4 /dev/sda6 $LFS/usr

mkdir -pv $LFS/opt
sudo mount -v -t ext4 /dev/sda7 $LFS/opt

mkdir -pv $LFS/tmp
sudo mount -v -t ext4 /dev/sda8 $LFS/tmp

mkdir -pv $LFS/usr/src
sudo mount -v -t ext4 /dev/sda9 $LFS/usr/src

mkdir -pv $LFS/home
sudo mount -v -t ext4 /dev/sda11 $LFS/home

Or add partition entries to /etc/fstab file so that next time when system boots, all partitions will be auto mounted.

UUID for particular partition can be found using blkid command. ex: sudo blkid /dev/sda10.

UUID=87545a47-1866-44d6-8b93-12c61c8725a0 /mnt/lfs       ext4    defaults        0       0

UUID=607e54d8-5c2f-4631-b353-6696ee821d9d /mnt/lfs/usr   ext4    defaults        0       0

UUID=e556fd19-2fbc-48a5-afd8-277076eb3252 /mnt/lfs/opt   ext4    defaults        0       0

UUID=f7ba6fac-2889-4317-a35a-483ce1519155 /mnt/lfs/tmp   ext4    defaults        0       0

UUID=f2d42e83-5c5d-425f-a290-0a52b6e7f0fd /mnt/lfs/usr/src       ext4    defaults        0       0

UUID=4c140ade-dcb4-4bb9-955e-b8caa0aaf0a7 /mnt/lfs/home  ext4    defaults        0       0

UUID=7d2b4784-5afd-4aa3-a927-c3225d1b3b97 /mnt/lfs/boot  ext4    defaults        0       0

Create a separate directory to store source files

mkdir $LFS/sources
mkdir $LFS/tools
sudo ln -sv $LFS/tools /

This directory is used to store tools, which required to create actual LFS. But these tools are not part of LFS.

Create user lfs and add that user to group lfs

sudo groupadd lfs
sudo useradd -s /bin/bash -m -k /dev/null lfs
sudo passwd lfs

Change ownership of LFS directories

sudo chown -Rv lfs:lfs /tools
sudo chown -Rv lfs:lfs $LFS

Login as lfs user

su - lfs

Remove unnecessary env variables

For clean build environment remove all env variables except HOME, TERM and PS1

cat > ~/.bash_profile << "EOF"
exec env -i HOME=$HOME TERM=$TERM PS1='\u:\w\$ ' /bin/bash
EOF

When su - lfs is entered, the login shell reads .bash_profile file and sets the above env variables. This .bash_profile initiates a new non-login shell session which reads only.bashrc but not .bash_profile.

Create new .bashrc file

cat > ~/.bashrc << "EOF"
set +h			# Forcess shell to always look in $PATH but not bash hash fn.
umask 022
LFS=/mnt/lfs
LC_ALL=POSIX		# Can be POSIX or C
LFS_TGT=$(uname -m)-lfs-linux-gnu
PATH=/tools/bin:/bin:/usr/bin
export LFS LC_ALL LFS_TGT PATH
EOF

And then

source ~/.bash_profile

Compile packages for temperory tools.

These tools are required to produce actual LFS.

If compiling for 64 bit system:

case $(uname -m) in
  x86_64) mkdir -v /tools/lib && ln -sv lib /tools/lib64 ;;
esac

Binutils

Download link

The Binutils package contains a linker, an assembler, and other tools for handling object files.

cd binutils*
mkdir build && cd build
../configure --prefix=/tools            \
             --with-sysroot=$LFS        \
             --with-lib-path=/tools/lib \
             --target=$LFS_TGT          \
             --disable-nls              \
             --disable-werror && make

For Binutils contents:

http://www.linuxfromscratch.org/lfs/view/stable-systemd/chapter06/binutils.html#contents-binutils.

GCC

Download Link

GCC requires GMP, MPFR and MPC packages.

cd $LFS/sources
tar xvf gcc-7.2.0.tar.xz
cd gcc-*
tar xvf ../mpfr-3.1.5.tar.xz
mv mpfr-3.1.5 mpfr
tar xvf ../gmp-6.1.2.tar.xz
mv gmp-6.1.2/ gmp
tar xvf ../mpc-1.0.3.tar.gz
mv mpc-1.0.3/ mpc

To change default dynamic linker path to /tools directory, and to remove /usr/include path from header file search paths:

for file in gcc/config/{linux,i386/linux{,64}}.h
do
  cp -uv $file{,.orig}
  sed -e 's@/lib\(64\)\?\(32\)\?/ld@/tools&@g' \
      -e 's@/usr@/tools@g' $file.orig > $file
  echo '
#undef STANDARD_STARTFILE_PREFIX_1
#undef STANDARD_STARTFILE_PREFIX_2
#define STANDARD_STARTFILE_PREFIX_1 "/tools/lib/"
#define STANDARD_STARTFILE_PREFIX_2 ""' >> $file
  touch $file.orig
done
case $(uname -m) in
  x86_64)
    sed -e '/m64=/s/lib64/lib/' \
        -i.orig gcc/config/i386/t-linux64
 ;;
esac
mkdir -v build
cd       build

Prepare for GCC build:

../configure                                       \
    --target=$LFS_TGT                              \
    --prefix=/tools                                \
    --with-glibc-version=2.11                      \
    --with-sysroot=$LFS                            \
    --with-newlib                                  \
    --without-headers                              \
    --with-local-prefix=/tools                     \
    --with-native-system-header-dir=/tools/include \
    --disable-nls                                  \
    --disable-shared                               \
    --disable-multilib                             \
    --disable-decimal-float                        \
    --disable-threads                              \
    --disable-libatomic                            \
    --disable-libgomp                              \
    --disable-libmpx                               \
    --disable-libquadmath                          \
    --disable-libssp                               \
    --disable-libvtv                               \
    --disable-libstdcxx                            \
    --enable-languages=c,c++

Compile and Install gcc

make
make install

Linux Header Files

These header files will be used by Glibc

cd $LFS/sources
tar xvf linux-4.12.7.tar.xz
cd linux-4.12.7
make mrproper
make INSTALL_HDR_PATH=dest headers_install
cp -rv dest/include/* /tools/include

Glibc

cd $LFS/sources
tar xvf glibc-2.26.tar.xz
cd glibc-2.26
mkdir -v build
cd build
../configure                             \
      --prefix=/tools                    \
      --host=$LFS_TGT                    \
      --build=$(../scripts/config.guess) \
      --enable-kernel=3.2             \
      --with-headers=/tools/include      \
      libc_cv_forced_unwind=yes          \
      libc_cv_c_cleanup=yes
make
make install

Check Compiling and linking

Check whether compiling and linking working with the tools generated until now.

echo 'int main(){}' > dummy.c
$LFS_TGT-gcc dummy.c
readelf -l a.out | grep ': /tools'

This should produce the result like below

[Requesting program interpreter: /tools/lib/ld-linux.so.2]

Resuming installing packages

Libstdc++

cd $LFS/sources/
rm -r gcc-7.2.0
tar xvf gcc-7.2.0.tar.xz
cd gcc-7.2.0
mkdir -v build
cd build
../libstdc++-v3/configure           \
    --host=$LFS_TGT                 \
    --prefix=/tools                 \
    --disable-multilib              \
    --disable-nls                   \
    --disable-libstdcxx-threads     \
    --disable-libstdcxx-pch         \
    --with-gxx-include-dir=/tools/$LFS_TGT/include/c++/7.2.0
make
make install

Bin Utils - pass 2

cd $LFS/sources/
rm -r binutils-2.29
tar xvf binutils-2.29.tar.bz2
cd binutils-2.29
mkdir -v build
cd build/
CC=$LFS_TGT-gcc                \
AR=$LFS_TGT-ar                 \
RANLIB=$LFS_TGT-ranlib         \
../configure                   \
    --prefix=/tools            \
    --disable-nls              \
    --disable-werror           \
    --with-lib-path=/tools/lib \
    --with-sysroot
make
make install

Now prepare the linker

make -C ld clean
make -C ld LIB_PATH=/usr/lib:/lib
cp -v ld/ld-new /tools/bin

GCC - pass 2

cd $LFS/sources/
tar xvf gcc-7.2.0.tar.xz
cd gcc-7.2.0
cat gcc/limitx.h gcc/glimits.h gcc/limity.h > \
  `dirname $($LFS_TGT-gcc -print-libgcc-file-name)`/include-fixed/limits.h
for file in gcc/config/{linux,i386/linux{,64}}.h
do
  cp -uv $file{,.orig}
  sed -e 's@/lib\(64\)\?\(32\)\?/ld@/tools&@g' \
      -e 's@/usr@/tools@g' $file.orig > $file
  echo '
#undef STANDARD_STARTFILE_PREFIX_1
#undef STANDARD_STARTFILE_PREFIX_2
#define STANDARD_STARTFILE_PREFIX_1 "/tools/lib/"
#define STANDARD_STARTFILE_PREFIX_2 ""' >> $file
  touch $file.orig
done
case $(uname -m) in
  x86_64)
    sed -e '/m64=/s/lib64/lib/' \
        -i.orig gcc/config/i386/t-linux64
  ;;
esac
tar -xf ../mpfr-3.1.5.tar.xz
mv -v mpfr-3.1.5 mpfr
tar -xf ../gmp-6.1.2.tar.xz
mv -v gmp-6.1.2 gmp
tar -xf ../mpc-1.0.3.tar.gz
mv -v mpc-1.0.3 mpc
mkdir -v build
cd build
CC=$LFS_TGT-gcc                                    \
CXX=$LFS_TGT-g++                                   \
AR=$LFS_TGT-ar                                     \
RANLIB=$LFS_TGT-ranlib                             \
../configure                                       \
    --prefix=/tools                                \
    --with-local-prefix=/tools                     \
    --with-native-system-header-dir=/tools/include \
    --enable-languages=c,c++                       \
    --disable-libstdcxx-pch                        \
    --disable-multilib                             \
    --disable-bootstrap                            \
    --disable-libgomp
make
make install
ln -sv gcc /tools/bin/cc

Check if compiler works

echo 'int main(){}' > dummy.c
cc dummy.c
readelf -l a.out | grep ': /tools'

The output should be something like

      [Requesting program interpreter: /tools/lib64/ld-linux-x86-64.so.2]

Packages to support running the test suites

Tcl-core

cd $LFS/sources/
tar xvf tcl-core8.6.7-src.tar.gz
cd tcl8.6.7/
cd unix/
./configure --prefix=/tools
make
make install
chmod -v u+w /tools/lib/libtcl8.6.so
make install-private-headers
ln -sv tclsh8.6 /tools/bin/tclsh

Expect

cd $LFS/sources/
tar xvf expect5.45.tar.gz
cd expect5.45
cp -v configure{,.orig}
sed 's:/usr/local/bin:/bin:' configure.orig > configure
./configure --prefix=/tools       \
            --with-tcl=/tools/lib \
            --with-tclinclude=/tools/include
make
make SCRIPTS="" install

DejaGNU

cd $LFS/sources/
tar xvf dejagnu-1.6.tar.gz
cd dejagnu-1.6
./configure --prefix=/tools
make install

Check

cd $LFS/sources/
tar xvf check-0.11.0.tar.gz
cd check-0.11.0
PKG_CONFIG= ./configure --prefix=/tools
make
make install

Ncurses

cd $LFS/sources/
tar xvf ncurses-6.0.tar.gz
cd ncurses-6.0
sed -i s/mawk// configure
./configure --prefix=/tools \
            --with-shared   \
            --without-debug \
            --without-ada   \
            --enable-widec  \
            --enable-overwrite
make
make install

Bash

cd $LFS/sources/
tar xvf bash-4.4.tar.gz
cd bash-4.4
./configure --prefix=/tools --without-bash-malloc
make
make install
ln -sv bash /tools/bin/sh

Bison

cd $LFS/sources/
tar xvf bison-3.0.4.tar.xz
cd bison-3.0.4
./configure --prefix=/tools
make
make install

Bzip2

cd $LFS/sources/
tar xvf bzip2-1.0.6.tar.gz
cd bzip2-1.0.6
make
make PREFIX=/tools install

Coreutils

cd $LFS/sources/
tar xvf coreutils-8.27.tar.xz
cd coreutils-8.27
./configure --prefix=/tools --enable-install-program=hostname
make
make install

Diffutils

cd $LFS/sources/
tar xvf diffutils-3.6.tar.xz
cd diffutils-3.6
./configure --prefix=/tools
make
make install

File

cd $LFS/sources/
tar xvf file-5.31.tar.gz
cd file-5.31
./configure --prefix=/tools
make
make install

Findutils

cd $LFS/sources/
tar xvf findutils-4.6.0.tar.gz
cd findutils-4.6.0
./configure --prefix=/tools
make
make install

Gawk

cd $LFS/sources/
tar xvf gawk-4.1.4.tar.xz
cd gawk-4.1.4
./configure --prefix=/tools
make
make install

Gettext

cd $LFS/sources/
tar xvf gettext-0.19.8.1.tar.xz
cd gettext-0.19.8.1
cd gettext-tools
EMACS="no" ./configure --prefix=/tools --disable-shared
make -C gnulib-lib
make -C intl pluralx.c
make -C src msgfmt
make -C src msgmerge
make -C src xgettext
cp -v src/{msgfmt,msgmerge,xgettext} /tools/bin

Grep

cd $LFS/sources/
tar xvf grep-3.1.tar.xz 
cd grep-3.1
./configure --prefix=/tools
make
make install

Gzip

cd $LFS/sources/
tar xvf gzip-1.8.tar.xz
cd gzip-1.8
./configure --prefix=/tools
make
make install

m4

cd $LFS/sources/
tar xvf m4-1.4.18.tar.xz
cd m4-1.4.18
./configure --prefix=/tools
make
make install

Make

cd $LFS/sources/
tar xvf make-4.2.1.tar.bz2
cd make-4.2.1
./configure --prefix=/tools --without-guile
make
make install

Patch

cd $LFS/sources/
tar xvf patch-2.7.5.tar.xz
cd patch-2.7.5
./configure --prefix=/tools
make
make install

Perl

cd $LFS/sources/
tar xvf perl-5.26.0.tar.xz
cd perl-5.26.0
sed -e '9751 a#ifndef PERL_IN_XSUB_RE' \
    -e '9808 a#endif'                  \
    -i regexec.c
sh Configure -des -Dprefix=/tools -Dlibs=-lm
make
cp -v perl cpan/podlators/scripts/pod2man /tools/bin
mkdir -pv /tools/lib/perl5/5.26.0
cp -Rv lib/* /tools/lib/perl5/5.26.0

Sed

cd $LFS/sources/
tar xvf sed-4.4.tar.xz
cd sed-4.4
./configure --prefix=/tools
make
make install

Tar

cd $LFS/sources/
tar xvf tar-1.29.tar.xz
cd tar-1.29
./configure --prefix=/tools
make
make install

Texinfo

cd $LFS/sources/
tar xvf texinfo-6.4.tar.xz
cd texinfo-6.4
./configure --prefix=/tools
make
make install

Util-Linux

cd $LFS/sources/
tar xvf util-linux-2.30.1.tar.xz
cd util-linux-2.30.1
./configure --prefix=/tools                \
            --without-python               \
            --disable-makeinstall-chown    \
            --without-systemdsystemunitdir \
            --without-ncurses              \
            PKG_CONFIG=""
make
make install

xz

cd $LFS/sources/
tar xvf xz-5.2.3.tar.xz
cd xz-5.2.3
./configure --prefix=/tools
make
make install

Change the ownership of $LFS/tools to root

chown -R root:root $LFS/tools

From here onwards all commands must be run as root.

sudo su -

Create virtual file systems

mkdir -pv $LFS/{dev,proc,sys,run}

Create device nodes

The following device nodes are needed when kernel boots the system.

mknod -m 600 $LFS/dev/console c 5 1
mknod -m 666 $LFS/dev/null c 1 3

Preparing for chroot environment

The following three sections needs to be run again before continuing installation of packages if we leave this environment for any reason.

Populate /dev directory

mount -v --bind /dev $LFS/dev

Mount virtual file systems

mount -vt devpts devpts $LFS/dev/pts -o gid=5,mode=620
mount -vt proc proc $LFS/proc
mount -vt sysfs sysfs $LFS/sys
mount -vt tmpfs tmpfs $LFS/run
if [ -h $LFS/dev/shm ]; then
  mkdir -pv $LFS/$(readlink $LFS/dev/shm)
fi

Enter into chroot environment

chroot "$LFS" /tools/bin/env -i \
    HOME=/root                  \
    TERM="$TERM"                \
    PS1='\u:\w\$ '              \
    PATH=/bin:/usr/bin:/sbin:/usr/sbin:/tools/bin \
    /tools/bin/bash --login +h

Create FHS compliant directory tree

mkdir -pv /{bin,boot,etc/{opt,sysconfig},home,lib/firmware,mnt,opt}
mkdir -pv /{media/{floppy,cdrom},sbin,srv,var}
install -dv -m 0750 /root
install -dv -m 1777 /tmp /var/tmp
mkdir -pv /usr/{,local/}{bin,include,lib,sbin,src}
mkdir -pv /usr/{,local/}share/{color,dict,doc,info,locale,man}
mkdir -v  /usr/{,local/}share/{misc,terminfo,zoneinfo}
mkdir -v  /usr/libexec
mkdir -pv /usr/{,local/}share/man/man{1..8}

case $(uname -m) in
 x86_64) mkdir -v /lib64 ;;
esac

mkdir -v /var/{log,mail,spool}
ln -sv /run /var/run
ln -sv /run/lock /var/lock
mkdir -pv /var/{opt,cache,lib/{color,misc,locate},local}
ln -sv /tools/bin/{bash,cat,dd,echo,ln,pwd,rm,stty} /bin
ln -sv /tools/bin/{install,perl} /usr/bin
ln -sv /tools/lib/libgcc_s.so{,.1} /usr/lib
ln -sv /tools/lib/libstdc++.{a,so{,.6}} /usr/lib
sed 's/tools/usr/' /tools/lib/libstdc++.la > /usr/lib/libstdc++.la
for lib in blkid lzma mount uuid
do
    ln -sv /tools/lib/lib$lib.{a,so*} /usr/lib
    sed 's/tools/usr/' /tools/lib/lib${lib}.la > /usr/lib/lib${lib}.la
done
ln -sv bash /bin/sh
ln -sv /proc/self/mounts /etc/mtab

Create /etc/passwd file

cat > /etc/passwd << "EOF"
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/dev/null:/bin/false
daemon:x:6:6:Daemon User:/dev/null:/bin/false
messagebus:x:18:18:D-Bus Message Daemon User:/var/run/dbus:/bin/false
systemd-bus-proxy:x:72:72:systemd Bus Proxy:/:/bin/false
systemd-journal-gateway:x:73:73:systemd Journal Gateway:/:/bin/false
systemd-journal-remote:x:74:74:systemd Journal Remote:/:/bin/false
systemd-journal-upload:x:75:75:systemd Journal Upload:/:/bin/false
systemd-network:x:76:76:systemd Network Management:/:/bin/false
systemd-resolve:x:77:77:systemd Resolver:/:/bin/false
systemd-timesync:x:78:78:systemd Time Synchronization:/:/bin/false
systemd-coredump:x:79:79:systemd Core Dumper:/:/bin/false
nobody:x:99:99:Unprivileged User:/dev/null:/bin/false
EOF

Create /etc/group file

cat > /etc/group << "EOF"
root:x:0:
bin:x:1:daemon
sys:x:2:
kmem:x:3:
tape:x:4:
tty:x:5:
daemon:x:6:
floppy:x:7:
disk:x:8:
lp:x:9:
dialout:x:10:
audio:x:11:
video:x:12:
utmp:x:13:
usb:x:14:
cdrom:x:15:
adm:x:16:
messagebus:x:18:
systemd-journal:x:23:
input:x:24:
mail:x:34:
systemd-bus-proxy:x:72:
systemd-journal-gateway:x:73:
systemd-journal-remote:x:74:
systemd-journal-upload:x:75:
systemd-network:x:76:
systemd-resolve:x:77:
systemd-timesync:x:78:
systemd-coredump:x:79:
nogroup:x:99:
users:x:999:
EOF

Remove ‘I have no name’ shell prompt

exec /tools/bin/bash --login +h

Create log files

touch /var/log/{btmp,lastlog,faillog,wtmp}
chgrp -v utmp /var/log/lastlog
chmod -v 664  /var/log/lastlog
chmod -v 600  /var/log/btmp

Install necessary permanent packages

Linux API Headers

cd $LFS/sources
cd linux-4.12.7
make mrproper
make INSTALL_HDR_PATH=dest headers_install
find dest/include \( -name .install -o -name ..install.cmd \) -delete
cp -rv dest/include/* /usr/include

Man pages

cd $LFS/sources
tar xvf man-pages-4.12.tar.xz
cd man-pages-4.12
make install

Glibc

cd $LFS/sources 
rm -r glibc-2.26
tar xvf glibc-2.26.tar.xz
cd glibc-2.26
patch -Np1 -i ../glibc-2.26-fhs-1.patch
ln -sfv /tools/lib/gcc /usr/lib
case $(uname -m) in
    i?86)    GCC_INCDIR=/usr/lib/gcc/$(uname -m)-pc-linux-gnu/7.2.0/include
            ln -sfv ld-linux.so.2 /lib/ld-lsb.so.3
    ;;
    x86_64) GCC_INCDIR=/usr/lib/gcc/x86_64-pc-linux-gnu/7.2.0/include
            ln -sfv ../lib/ld-linux-x86-64.so.2 /lib64
            ln -sfv ../lib/ld-linux-x86-64.so.2 /lib64/ld-lsb-x86-64.so.3
    ;;
esac
rm -f /usr/include/limits.h
mkdir -v build
cd build

Prepare for compilation

CC="gcc -isystem $GCC_INCDIR -isystem /usr/include" \
../configure --prefix=/usr                          \
             --disable-werror                       \
             --enable-kernel=3.2                    \
             --enable-stack-protector=strong        \
             libc_cv_slibdir=/lib
unset GCC_INCDIR
make
make check

Following test cases can be ignored.

1. misc/tst-clone3
2. posix/tst-getaddrinfo4 (known error case)
3. posix/tst-getaddrinfo5 (known error case)
4. rt/tst-cputimer1 and rt/tst-cpuclock2
5. nptl/tst-thread-affinity-{pthread,pthread2,sched}
6. malloc/tst-malloc-usable and nptl/tst-cleanupx4
touch /etc/ld.so.conf
sed '/test-installation/s@$(PERL)@echo not running@' -i ../Makefile
make install
cp -v ../nscd/nscd.conf /etc/nscd.conf
mkdir -pv /var/cache/nscd
install -v -Dm644 ../nscd/nscd.tmpfiles /usr/lib/tmpfiles.d/nscd.conf
install -v -Dm644 ../nscd/nscd.service /lib/systemd/system/nscd.service

Install locales

mkdir -pv /usr/lib/locale
localedef -i cs_CZ -f UTF-8 cs_CZ.UTF-8
localedef -i de_DE -f ISO-8859-1 de_DE
localedef -i de_DE@euro -f ISO-8859-15 de_DE@euro
localedef -i de_DE -f UTF-8 de_DE.UTF-8
localedef -i en_GB -f UTF-8 en_GB.UTF-8
localedef -i en_HK -f ISO-8859-1 en_HK
localedef -i en_PH -f ISO-8859-1 en_PH
localedef -i en_US -f ISO-8859-1 en_US
localedef -i en_US -f UTF-8 en_US.UTF-8
localedef -i es_MX -f ISO-8859-1 es_MX
localedef -i fa_IR -f UTF-8 fa_IR
localedef -i fr_FR -f ISO-8859-1 fr_FR
localedef -i fr_FR@euro -f ISO-8859-15 fr_FR@euro
localedef -i fr_FR -f UTF-8 fr_FR.UTF-8
localedef -i it_IT -f ISO-8859-1 it_IT
localedef -i it_IT -f UTF-8 it_IT.UTF-8
localedef -i ja_JP -f EUC-JP ja_JP
localedef -i ru_RU -f KOI8-R ru_RU.KOI8-R
localedef -i ru_RU -f UTF-8 ru_RU.UTF-8
localedef -i tr_TR -f UTF-8 tr_TR.UTF-8
localedef -i zh_CN -f GB18030 zh_CN.GB18030

Configuring glic

cat > /etc/nsswitch.conf << "EOF"
# Begin /etc/nsswitch.conf

passwd: files
group: files
shadow: files

hosts: files dns
networks: files

protocols: files
services: files
ethers: files
rpc: files

# End /etc/nsswitch.conf
EOF

Adding timezone data

tar -xf ../../tzdata2017b.tar.gz

ZONEINFO=/usr/share/zoneinfo
mkdir -pv $ZONEINFO/{posix,right}

for tz in etcetera southamerica northamerica europe africa antarctica  \
          asia australasia backward pacificnew systemv; do
    zic -L /dev/null   -d $ZONEINFO       -y "sh yearistype.sh" ${tz}
    zic -L /dev/null   -d $ZONEINFO/posix -y "sh yearistype.sh" ${tz}
    zic -L leapseconds -d $ZONEINFO/right -y "sh yearistype.sh" ${tz}
done

cp -v zone.tab zone1970.tab iso3166.tab $ZONEINFO
zic -d $ZONEINFO -p America/New_York
unset ZONEINFO
tzselect
ln -sfv /usr/share/zoneinfo/Asia/Kolkata /etc/localtime

Configuring dynamic loader

cat > /etc/ld.so.conf << "EOF"
# Begin /etc/ld.so.conf
/usr/local/lib
/opt/lib

EOF
cat >> /etc/ld.so.conf << "EOF"
# Add an include directory
include /etc/ld.so.conf.d/*.conf

EOF
mkdir -pv /etc/ld.so.conf.d

Adjusting the toolchain

mv -v /tools/bin/{ld,ld-old}
mv -v /tools/$(uname -m)-pc-linux-gnu/bin/{ld,ld-old}
mv -v /tools/bin/{ld-new,ld}
ln -sv /tools/bin/ld /tools/$(uname -m)-pc-linux-gnu/bin/ld
gcc -dumpspecs | sed -e 's@/tools@@g'                   \
    -e '/\*startfile_prefix_spec:/{n;s@.*@/usr/lib/ @}' \
    -e '/\*cpp:/{n;s@$@ -isystem /usr/include@}' >      \
    `dirname $(gcc --print-libgcc-file-name)`/specs

Check whether everything works fine…

echo 'int main(){}' > dummy.c
cc dummy.c -v -Wl,--verbose &> dummy.log
readelf -l a.out | grep ': /lib'

The output should be

[Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]

Now make sure that we’re setup to use the correct start files:

grep -o '/usr/lib.*/crt[1in].*succeeded' dummy.log

The output of the last command should be:

/usr/lib/../lib/crt1.o succeeded
/usr/lib/../lib/crti.o succeeded
/usr/lib/../lib/crtn.o succeeded

Verify that the compiler is searching for the correct header files:

grep -B1 '^ /usr/include' dummy.log

This command should return the following output:

#include <...> search starts here:
 /usr/include

Next, verify that the new linker is being used with the correct search paths:

grep 'SEARCH.*/usr/lib' dummy.log |sed 's|; |\n|g'

References to paths that have components with ‘-linux-gnu’ should be ignored, but otherwise the output of the last command should be:

SEARCH_DIR("/usr/lib")
SEARCH_DIR("/lib")

Next make sure that we’re using the correct libc:

grep "/lib.*/libc.so.6 " dummy.log

The output of the last command should be:

attempt to open /lib/libc.so.6 succeeded

Lastly, make sure GCC is using the correct dynamic linker:

grep found dummy.log

The output of the last command should be (allowing for platform-specific differences in dynamic linker name):

found ld-linux-x86-64.so.2 at /lib/ld-linux-x86-64.so.2

Once everything is working correctly, clean up the test files:

rm -v dummy.c a.out dummy.log

Zlib

cd $LFS/sources
tar xvf zlib-1.2.11.tar.xz
cd zlib-1.2.11
./configure --prefix=/usr
make
make check
make install
mv -v /usr/lib/libz.so.* /lib
ln -sfv ../../lib/$(readlink /usr/lib/libz.so) /usr/lib/libz.so

File

cd $LFS/sources
tar xvf file-5.31.tar.gz
cd file-5.31
./configure --prefix=/usr
make
make check
make install

Readline

cd $LFS/sources
tar xvf readline-7.0.tar.gz
cd readline-7.0
sed -i '/MV.*old/d' Makefile.in
sed -i '/{OLDSUFF}/c:' support/shlib-install
./configure --prefix=/usr    \
            --disable-static \
            --docdir=/usr/share/doc/readline-7.0
make SHLIB_LIBS="-L/tools/lib -lncursesw"
make SHLIB_LIBS="-L/tools/lib -lncurses" install
mv -v /usr/lib/lib{readline,history}.so.* /lib
ln -sfv ../../lib/$(readlink /usr/lib/libreadline.so) /usr/lib/libreadline.so
ln -sfv ../../lib/$(readlink /usr/lib/libhistory.so ) /usr/lib/libhistory.so
install -v -m644 doc/*.{ps,pdf,html,dvi} /usr/share/doc/readline-7.0

M4

cd $LFS/sources
rm -r m4-1.4.18
tar xvf m4-1.4.18.tar.xz
cd m4-1.4.18
./configure --prefix=/usr
make
make check
make install

Bc

cd $LFS/sources
cd bc-1.07.1
tar xvf bc-1.07.1.tar.gz
cat > bc/fix-libmath_h << "EOF"
#! /bin/bash
sed -e '1   s/^/{"/' \
    -e     's/$/",/' \
    -e '2,$ s/^/"/'  \
    -e   '$ d'       \
    -i libmath.h

sed -e '$ s/$/0}/' \
    -i libmath.h
EOF
ln -sv /tools/lib/libncursesw.so.6 /usr/lib/libncursesw.so.6
ln -sfv libncurses.so.6 /usr/lib/libncurses.so
sed -i -e '/flex/s/as_fn_error/: ;; # &/' configure
./configure --prefix=/usr           \
            --with-readline         \
            --mandir=/usr/share/man \
            --infodir=/usr/share/info
make
echo "quit" | ./bc/bc -l Test/checklib.b
make install

Binutils

cd $LFS/sources
rm -r binutils-2.29
tar xvf binutils-2.29.tar.bz2
cd binutils-2.29
expect -c "spawn ls"
mkdir -v build
cd build/
../configure --prefix=/usr       \
             --enable-gold       \
             --enable-ld=default \
             --enable-plugins    \
             --enable-shared     \
             --disable-werror    \
             --with-system-zlib
make tooldir=/usr
make -k check
make tooldir=/usr install

Gmp

cd $LFS/sources
tar xvf gmp-6.1.2.tar.xz
cd gmp-6.1.2
cp -v configfsf.guess config.guess
cp -v configfsf.sub config.sub
./configure --prefix=/usr    \
            --enable-cxx     \
            --disable-static \
            --docdir=/usr/share/doc/gmp-6.1.2
make
make html

Test the build

make check 2>&1 | tee gmp-check-log

Check if all 190 test cases pass

awk '/# PASS:/{total+=$3} ; END{print total}' gmp-check-log
make install
make install-html

Mpfr

cd $LFS/sources
tar xvf mpfr-3.1.5.tar.xz
cd mpfr-3.1.5
./configure --prefix=/usr        \
            --disable-static     \
            --enable-thread-safe \
            --docdir=/usr/share/doc/mpfr-3.1.5
make
make html
make check
make install
make install-html

Mpc

cd $LFS/sources
tar xvf mpc-1.0.3.tar.gz
cd mpc-1.0.3
./configure --prefix=/usr    \
            --disable-static \
            --docdir=/usr/share/doc/mpc-1.0.3
make
make html
make check
make install
make install-html

Gcc

cd $LFS/sources
rm -r gcc-7.2.0
tar xvf gcc-7.2.0.tar.xz
case $(uname -m) in
  x86_64)
    sed -e '/m64=/s/lib64/lib/' \
        -i.orig gcc/config/i386/t-linux64
  ;;
esac
rm -f /usr/lib/gcc
mkdir -v build
cd build
SED=sed                               \
../configure --prefix=/usr            \
             --enable-languages=c,c++ \
             --disable-multilib       \
             --disable-bootstrap      \
             --with-system-zlib
make
ulimit -s 32768
make -k check

To view test summy

../contrib/test_summary

Install

make install
ln -sv ../usr/bin/cpp /lib
ln -sv gcc /usr/bin/cc
install -v -dm755 /usr/lib/bfd-plugins
ln -sfv ../../libexec/gcc/$(gcc -dumpmachine)/7.2.0/liblto_plugin.so \
        /usr/lib/bfd-plugins/

Check by compiling program

echo 'int main(){}' > dummy.c
cc dummy.c -v -Wl,--verbose &> dummy.log
readelf -l a.out | grep ': /lib'

The output should be like

[Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
grep -o '/usr/lib.*/crt[1in].*succeeded' dummy.log

The output should be like

/usr/lib/gcc/x86_64-pc-linux-gnu/7.2.0/../../../../lib/crt1.o succeeded
/usr/lib/gcc/x86_64-pc-linux-gnu/7.2.0/../../../../lib/crti.o succeeded
/usr/lib/gcc/x86_64-pc-linux-gnu/7.2.0/../../../../lib/crtn.o succeeded

Check header files

grep -B4 '^ /usr/include' dummy.log

The output should be like

#include <...> search starts here:
 /usr/lib/gcc/x86_64-pc-linux-gnu/7.2.0/include
 /usr/local/include
 /usr/lib/gcc/x86_64-pc-linux-gnu/7.2.0/include-fixed
 /usr/include

Verify linker search paths

grep 'SEARCH.*/usr/lib' dummy.log |sed 's|; |\n|g'

The output should be like

SEARCH_DIR("/usr/x86_64-pc-linux-gnu/lib64")
SEARCH_DIR("/usr/local/lib64")
SEARCH_DIR("/lib64")
SEARCH_DIR("/usr/lib64")
SEARCH_DIR("/usr/x86_64-pc-linux-gnu/lib")
SEARCH_DIR("/usr/local/lib")
SEARCH_DIR("/lib")
SEARCH_DIR("/usr/lib");

Verify libc

grep "/lib.*/libc.so.6 " dummy.log

The outtput should be like

attempt to open /lib/libc.so.6 succeeded

Verify correct dynamic lineker

grep found dummy.log

The output should be like

found ld-linux-x86-64.so.2 at /lib/ld-linux-x86-64.so.2
rm -v dummy.c a.out dummy.log
mkdir -pv /usr/share/gdb/auto-load/usr/lib
mv -v /usr/lib/*gdb.py /usr/share/gdb/auto-load/usr/lib

Bzip2

cd $LFS/sources
rm -r bzip2-1.0.6
tar xvf bzip2-1.0.6.tar.gz
cd bzip2-1.0.6
patch -Np1 -i ../bzip2-1.0.6-install_docs-1.patch
sed -i 's@\(ln -s -f \)$(PREFIX)/bin/@\1@' Makefile
sed -i "s@(PREFIX)/man@(PREFIX)/share/man@g" Makefile
make -f Makefile-libbz2_so
make clean
make
make PREFIX=/usr install
cp -v bzip2-shared /bin/bzip2
cp -av libbz2.so* /lib
ln -sv ../../lib/libbz2.so.1.0 /usr/lib/libbz2.so
rm -v /usr/bin/{bunzip2,bzcat,bzip2}
ln -sv bzip2 /bin/bunzip2
ln -sv bzip2 /bin/bzcat

Pkg-config

cd $LFS/sources
tar xvf pkg-config-0.29.2.tar.gz
cd pkg-config-0.29.2
./configure --prefix=/usr --with-internal-glib --disable-host-tool --docdir=/usr/share/doc/pkg-config-0.29.2
make
make check
make install

Ncurses

cd $LFS/sources
rm -r ncurses-6.0
tar xvf ncurses-6.0.tar.gz
cd ncurses-6.0
sed -i '/LIBTOOL_INSTALL/d' c++/Makefile.in
./configure --prefix=/usr           \
            --mandir=/usr/share/man \
            --with-shared           \
            --without-debug         \
            --without-normal        \
            --enable-pc-files       \
            --enable-widec
make
make install
mv -v /usr/lib/libncursesw.so.6* /lib
ln -sfv ../../lib/$(readlink /usr/lib/libncursesw.so) /usr/lib/libncursesw.so
for lib in ncurses form panel menu ; do
    rm -vf                    /usr/lib/lib${lib}.so
    echo "INPUT(-l${lib}w)" > /usr/lib/lib${lib}.so
    ln -sfv ${lib}w.pc        /usr/lib/pkgconfig/${lib}.pc
done
rm -vf /usr/lib/libcursesw.so
echo "INPUT(-lncursesw)" > /usr/lib/libcursesw.so
ln -sfv libncurses.so /usr/lib/libcurses.so
mkdir -v /usr/share/doc/ncurses-6.0
cp -v -R doc/* /usr/share/doc/ncurses-6.0

Recompile package with following commands.

make distclean
./configure --prefix=/usr    \
            --with-shared    \
            --without-normal \
            --without-debug  \
            --without-cxx-binding \
            --with-abi-version=5 
make sources libs
cp -av lib/lib*.so.5* /usr/lib

Attr

cd $LFS/sources
history | tail -n 30 | awk '{$1=""; print $0}'
tar xvf attr-2.4.47.src.tar.gz
cd attr-2.4.47
sed -i -e 's|/@pkg_name@|&-@pkg_version@|' include/builddefs.in
sed -i -e "/SUBDIRS/s|man[25]||g" man/Makefile
sed -i 's:{(:\\{(:' test/run
./configure --prefix=/usr --disable-static
make
make -j1 tests root-tests
make install install-dev install-lib
chmod -v 755 /usr/lib/libattr.so
mv -v /usr/lib/libattr.so.* /lib
ln -sfv ../../lib/$(readlink /usr/lib/libattr.so) /usr/lib/libattr.so

Acl

cd $LFS/sources
tar xvf acl-2.2.52.src.tar.gz
cd acl-2.2.52
sed -i -e 's|/@pkg_name@|&-@pkg_version@|' include/builddefs.in
sed -i "s:| sed.*::g" test/{sbits-restore,cp,misc}.test
sed -i 's/{(/\\{(/' test/run
sed -i -e "/TABS-1;/a if (x > (TABS-1)) x = (TABS-1);" libacl/__acl_to_any_text.c
./configure --prefix=/usr --disable-static --libexecdir=/usr/lib
make
make install install-dev install-lib
chmod -v 755 /usr/lib/libacl.so
mv -v /usr/lib/libacl.so.* /lib
ln -sfv ../../lib/$(readlink /usr/lib/libacl.so) /usr/lib/libacl.so

Libcap

cd $LFS/sources
cd ..
tar xvf libcap-2.25.tar.xz
cd libcap-2.25
sed -i '/install.*STALIBNAME/d' libcap/Makefile
make
make RAISE_SETFCAP=no lib=lib prefix=/usr install
chmod -v 755 /usr/lib/libcap.so
mv -v /usr/lib/libcap.so.* /lib
ln -sfv ../../lib/$(readlink /usr/lib/libcap.so) /usr/lib/libcap.so

Sed

cd $LFS/sources
rm -r sed-4.4
tar xvf sed-4.4.tar.xz
cd sed-4.4
sed -i 's/usr/tools/' build-aux/help2man
sed -i 's/testsuite.panic-tests.sh//' Makefile.in
./configure --prefix=/usr --bindir=/bin
make
make html
make check
make install
install -d -m755 /usr/share/doc/sed-4.4
nstall -m644 doc/sed.html /usr/share/doc/sed-4.4

Shadow

cd $LFS/sources
tar xvf shadow-4.5.tar.xz
cd shadow-4.5
sed -i 's/groups$(EXEEXT) //' src/Makefile.in
find man -name Makefile.in -exec sed -i 's/groups\.1 / /' {} \;
find man -name Makefile.in -exec sed -i 's/getspnam\.3 / /' {} \;
find man -name Makefile.in -exec sed -i 's/passwd\.5 / /' {} \;
sed -i -e 's@#ENCRYPT_METHOD DES@ENCRYPT_METHOD SHA512@' \
       -e 's@/var/spool/mail@/var/mail@' etc/login.defs
sed -i 's/1000/999/' etc/useradd
./configure --sysconfdir=/etc --with-group-name-max-length=32
make
make install
mv -v /usr/bin/passwd /bin
pwconv
grpconv
cat /etc/default/useradd | top
cat /etc/default/useradd | head
passwd root

Psmisc

cd $LFS/sources
tar xvf psmisc-23.1.tar.xz
cd psmisc-23.1
./configure --prefix=/usr
make
make install
mv -v /usr/bin/fuser /bin
mv -v /usr/bin/killall /bin

Iana-etc

cd $LFS/sources
tar xvf iana-etc-2.30.tar.bz2
cd iana-etc-2.30
make
make install

Bison

cd $LFS/sources
rm -r bison-3.0.4
tar xvf bison-3.0.4.tar.xz
cd bison-3.0.4
./configure --prefix=/usr --docdir=/usr/share/doc/bison-3.0.4
make
make install

Flex

tar xvf flex-2.6.4.tar.gz
cd flex-2.6.4
sed -i "/math.h/a #include <malloc.h>" src/flexdef.h
HELP2MAN=/tools/bin/true ./configure --prefix=/usr --docdir=/usr/share/doc/flex-2.6.4
make
make check
make install
ln -sv flex /usr/bin/lex

Grep

cd $LFS/sources
rm -r grep-3.1
tar xvf grep-3.1.tar.xz
cd grep-3.1
./configure --prefix=/usr --bindir=/bin
make
make check
make install

Bash

cd $LFS/sources
rm -r bash-4.4
tar xvf bash-4.4.tar.gz
cd bash-4.4
patch -Np1 -i ../bash-4.4-upstream_fixes-1.patch
./configure --prefix=/usr --docdir=/usr/share/doc/bash-4.4 --without-bash-malloc --with-installed-readline
make
make install
mv -vf /usr/bin/bash /bin
exec /bin/bash --login +h

Libtool

cd $LFS/sources
tar xvf libtool-2.4.6.tar.xz
cd libtool-2.4.6
./configure --prefix=/usr
make
make check TESTSUITEFLAGS=-j2
make install

Five unexpected test cases fails. If we recheck after installing automake, everything passes.

Gdbm

cd $LFS/sources
tar xvf gdbm-1.13.tar.gz
cd gdbm-1.13
./configure --prefix=/usr --disable-static --enable-libgdbm-compat
make
make check
make install

Gperf

cd $LFS/sources
tar xvf gperf-3.1.tar.gz
cd gperf-3.1
./configure --prefix=/usr --docdir=/usr/share/doc/gperf-3.1
make
make -j1 check
make install

Expat

cd $LFS/sources
tar xvf expat-2.2.3.tar.bz2
cd expat-2.2.3
sed -i 's|usr/bin/env |bin/|' run.sh.in
./configure --prefix=/usr --disable-static
make
make check
make install
install -v -dm755 /usr/share/doc/expat-2.2.3
install -v -m644 doc/*.{html,png,css} /usr/share/doc/expat-2.2.3

Inetutils

cd $LFS/sources
tar xvf inetutils-1.9.4.tar.xz
cd inetutils-1.9.4
./configure --prefix=/usr --localstatedir=/var --disable-logger --disable-whois --disable-rcp --disable-rexec --disable-rlogin --disable-rsh --disable-servers
make
make check
make install
mv -v /usr/bin/{hostname,ping,ping6,traceroute} /bin
mv -v /usr/bin/ifconfig /sbin

Perl

cd $LFS/sources
rm -r perl-5.26.0
tar xvf perl-5.26.0.tar.xz
cd perl-5.26.0
echo "127.0.0.1 localhost $(hostname)" > /etc/hosts
export BUILD_ZLIB=False
export BUILD_BZIP2=0
sh Configure -des -Dprefix=/usr                 \
                  -Dvendorprefix=/usr           \
                  -Dman1dir=/usr/share/man/man1 \
                  -Dman3dir=/usr/share/man/man3 \
                  -Dpager="/usr/bin/less -isR"  \
                  -Duseshrplib                  \
                  -Dusethreads
make
make -k test
make install
make install
unset BUILD_ZLIB BUILD_BZIP2

XML::PARSER

cd $LFS/sources
tar xvf XML-Parser-2.44.tar.gz
cd XML-Parser-2.44
perl Makefile.PL
make
make test
make install

Intltool

cd $LFS/sources
tar xvf intltool-0.51.0.tar.gz
cd intltool-0.51.0
sed -i 's:\\\${:\\\$\\{:' intltool-update.in
./configure --prefix=/usr
make
make check
make install
install -v -Dm644 doc/I18N-HOWTO /usr/share/doc/intltool-0.51.0/I18N-HOWTO

Autoconf

cd $LFS/sources
tar xvf autoconf-2.69.tar.xz
cd autoconf-2.69
./configure --prefix=/usr
make
make check
make install

Automake

cd $LFS/sources
tar xvf automake-1.15.1.tar.xz
cd automake-1.15.1
./configure --prefix=/usr --docdir=/usr/share/doc/automake-1.15.1
make
sed -i "s:./configure:LEXLIB=/usr/lib/libfl.a &:" t/lex-{clean,depend}-cxx.sh
make -j4 check
make install

xz

cd $LFS/sources
rm -r xz-5.2.3
tar xvf xz-5.2.3.tar.xz
cd xz-5.2.3
./configure --prefix=/usr --disable-static --docdir=/usr/share/doc/xz-5.2.3
make
make install
mv -v /usr/bin/{lzma,unlzma,lzcat,xz,unxz,xzcat} /bin
mv -v /usr/lib/liblzma.so.* /lib
ln -svf ../../lib/$(readlink /usr/lib/liblzma.so) /usr/lib/liblzma.so

Kmod

cd $LFS/sources
tar xvf kmod-24.tar.xz
cd kmod-24
./configure --prefix=/usr --bindir=/bin --sysconfdir=/etc --with-rootlibdir=/lib --with-xz --with-zlib
make
make install
for target in depmod insmod lsmod modinfo modprobe rmmod; do ln -sfv ../bin/kmod /sbin/$target; done
ln -sfv kmod /bin/lsmod

Gettext

cd $LFS/sources
rm -r gettext-0.19.8.1
tar xvf gettext-0.19.8.1.tar.xz
cd gettext-0.19.8.1
sed -i '/^TESTS =/d' gettext-runtime/tests/Makefile.in && sed -i 's/test-lock..EXEEXT.//' gettext-tools/gnulib-tests/Makefile.in
./configure --prefix=/usr --disable-static --docdir=/usr/share/doc/gettext-0.19.8.1
make
make install
chmod -v 0755 /usr/lib/preloadable_libintl.so

Systemd

cat > config.cache << "EOF"
KILL=/bin/kill
MOUNT_PATH=/bin/mount
UMOUNT_PATH=/bin/umount
HAVE_BLKID=1
BLKID_LIBS="-lblkid"
BLKID_CFLAGS="-I/tools/include/blkid"
HAVE_LIBMOUNT=1
MOUNT_LIBS="-lmount"
MOUNT_CFLAGS="-I/tools/include/libmount"
cc_cv_CFLAGS__flto=no
SULOGIN="/sbin/sulogin"
GPERF_LEN_TYPE=size_t
XSLTPROC="/usr/bin/xsltproc"
EOF
./configure --prefix=/usr            \
            --sysconfdir=/etc        \
            --localstatedir=/var     \
            --config-cache           \
            --with-rootprefix=       \
            --with-rootlibdir=/lib   \
            --enable-split-usr       \
            --disable-firstboot      \
            --disable-ldconfig       \
            --disable-sysusers       \
            --without-python         \
            --with-default-dnssec=no \
            --docdir=/usr/share/doc/systemd-234
make
make install
rm -rfv /usr/lib/rpm
for tool in runlevel reboot shutdown poweroff halt telinit; do
     ln -sfv ../bin/systemctl /sbin/${tool}
done
ln -sfv ../lib/systemd/systemd /sbin/init
systemd-machine-id-setup

Procps

cd $LFS/sources
tar xvf procps-ng-3.3.12.tar.xz
cd procps-ng-3.3.12
./configure --prefix=/usr --exec-prefix= --libdir=/usr/lib --docdir=/usr/share/doc/procps-ng-3.3.12 --disable-static --disable-kill --with-systemd
make
make install
mv -v /usr/lib/libprocps.so.* /lib
ln -sfv ../../lib/$(readlink /usr/lib/libprocps.so) /usr/lib/libprocps.so

E2fsprogs

cd $LFS/sources
tar xvf e2fsprogs-1.43.5.tar.gz
cd e2fsprogs-1.43.5
mkdir -v build
cd build
LIBS=-L/tools/lib CFLAGS=-I/tools/include PKG_CONFIG_PATH=/tools/lib/pkgconfig ../configure --prefix=/usr --bindir=/bin --with-root-prefix="" --enable-elf-shlibs --disable-libblkid --disable-libuuid --disable-uuidd --disable-fsck
make
make install
make install-libs
chmod -v u+w /usr/lib/{libcom_err,libe2p,libext2fs,libss}.a
gunzip -v /usr/share/info/libext2fs.info.gz
install-info --dir-file=/usr/share/info/dir /usr/share/info/libext2fs.info
makeinfo -o doc/com_err.info ../lib/et/com_err.texinfo
install -v -m644 doc/com_err.info /usr/share/info
install-info --dir-file=/usr/share/info/dir /usr/share/info/com_err.info

Coreutils

cd $LFS/sources
rm -r coreutils-8.27
tar xvf coreutils-8.27.tar.xz
cd coreutils-8.27
patch -Np1 -i ../coreutils-8.27-i18n-1.patch
sed -i '/test.lock/s/^/#/' gnulib-tests/gnulib.mk
FORCE_UNSAFE_CONFIGURE=1 ./configure --prefix=/usr --enable-no-install-program=kill,uptime
FORCE_UNSAFE_CONFIGURE=1 make
make install
mv -v /usr/bin/{cat,chgrp,chmod,chown,cp,date,dd,df,echo} /bin
mv -v /usr/bin/{false,ln,ls,mkdir,mknod,mv,pwd,rm} /bin
mv -v /usr/bin/{rmdir,stty,sync,true,uname} /bin
mv -v /usr/bin/chroot /usr/sbin
mv -v /usr/share/man/man1/chroot.1 /usr/share/man/man8/chroot.8
sed -i s/\"1\"/\"8\"/1 /usr/share/man/man8/chroot.8
mv -v /usr/bin/{head,sleep,nice,test,[} /bin

Diffutils

cd $LFS/sources
rm -r diffutils-3.6
tar xvf diffutils-3.6.tar.xz
cd diffutils-3.6
./configure --prefix=/usr
make
make install

Gawk

cd $LFS/sources
rm -r gawk-4.1.4
tar xvf gawk-4.1.4.tar.xz
cd gawk-4.1.4
./configure --prefix=/usr
make
make install
mkdir -v /usr/share/doc/gawk-4.1.4
cp -v doc/{awkforai.txt,*.{eps,pdf,jpg}} /usr/share/doc/gawk-4.1.4

Findutils

cd $LFS/sources
rm -r findutils-4.6.0
tar xvf findutils-4.6.0.tar.gz
cd findutils-4.6.0
sed -i 's/test-lock..EXEEXT.//' tests/Makefile.in
./configure --prefix=/usr --localstatedir=/var/lib/locate
make
make install
mv -v /usr/bin/find /bin
sed -i 's|find:=${BINDIR}|find:=/bin|' /usr/bin/updatedb

Groff

cd $LFS/sources
tar xvf groff-1.22.3.tar.gz
cd groff-1.22.3
PAGE=A4 ./configure --prefix=/usr
make -j1
make install

Grub

cd $LFS/sources
tar xvf grub-2.02.tar.xz
cd grub-2.02
./configure --prefix=/usr --sbindir=/sbin --sysconfdir=/etc --disable-efiemu --disable-werror
make
make install

Less

cd $LFS/sources
tar xvf less-487.tar.gz
cd less-487
./configure --prefix=/usr --sysconfdir=/etc
make
make install

Gzip

cd $LFS/sources
rm -r gzip-1.8
tar xvf gzip-1.8.tar.xz
cd gzip-1.8
./configure --prefix=/usr
make
make install
mv -v /usr/bin/gzip /bin

Iproute

We are going to skip arpd installation

cd $LFS/sources
tar xvf iproute2-4.12.0.tar.xz
cd iproute2-4.12.0
sed -i /ARPD/d Makefile
sed -i 's/arpd.8//' man/man8/Makefile
rm -v doc/arpd.sgml
sed -i 's/m_ipt.o//' tc/Makefile
make
make DOCDIR=/usr/share/doc/iproute2-4.12.0 install

Kbd

cd $LFS/sources
rm -r kbd-2.0.4
tar xvf kbd-2.0.4.tar.xz
cd kbd-2.0.4
patch -Np1 -i ../kbd-2.0.4-backspace-1.patch
sed -i 's/\(RESIZECONS_PROGS=\)yes/\1no/g' configure
sed -i 's/resizecons.8 //' docs/man/man8/Makefile.in
PKG_CONFIG_PATH=/tools/lib/pkgconfig ./configure --prefix=/usr --disable-vlock
make
make install
mkdir -v /usr/share/doc/kbd-2.0.4
cp -R -v docs/doc/* /usr/share/doc/kbd-2.0.4

Libpipeline

cd $LFS/sources
rm -r libpipeline-1.4.2
tar xvf libpipeline-1.4.2.tar.gz
cd libpipeline-1.4.2
PKG_CONFIG_PATH=/tools/lib/pkgconfig ./configure --prefix=/usr
make
make install

Make

cd $LFS/sources
rm -r make-4.2.1
tar xvf make-4.2.1.tar.bz2
cd make-4.2.1
./configure --prefix=/usr
make
make install

Patch

cd $LFS/sources
rm -r patch-2.7.5
tar xvf patch-2.7.5.tar.xz
cd patch-2.7.5
./configure --prefix=/usr
make
make install

Dbus

cd $LFS/sources
tar xvf dbus-1.10.22.tar.gz
cd dbus-1.10.22
./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var --disable-static --disable-doxygen-docs --disable-xml-docs --docdir=/usr/share/doc/dbus-1.10.22 --with-console-auth-dir=/run/console
make
make install
mv -v /usr/lib/libdbus-1.so.* /lib
ln -sfv ../../lib/$(readlink /usr/lib/libdbus-1.so) /usr/lib/libdbus-1.so
ln -sfv /etc/machine-id /var/lib/dbus

Util-linux

cd $LFS/sources
rm -r util-linux-2.30.1
tar xvf util-linux-2.30.1.tar.xz
cd util-linux-2.30.1
mkdir -pv /var/lib/hwclock
./configure ADJTIME_PATH=/var/lib/hwclock/adjtime --docdir=/usr/share/doc/util-linux-2.30.1 --disable-chfn-chsh --disable-login --disable-nologin --disable-su --disable-setpriv --disable-runuser --disable-pylibmount --disable-static --without-python
make
make install

Man-db

cd $LFS/sources
tar xvf man-db-2.7.6.1.tar.xz
cd man-db-2.7.6.1
./configure --prefix=/usr --docdir=/usr/share/doc/man-db-2.7.6.1 --sysconfdir=/etc --disable-setuid --enable-cache-owner=bin --with-browser=/usr/bin/lynx --with-vgrind=/usr/bin/vgrind --with-grap=/usr/bin/grap
make
make install
sed -i "s:man man:root root:g" /usr/lib/tmpfiles.d/man-db.conf

Tar

cd $LFS/sources
rm -r tar-1.29
tar xvf tar-1.29.tar.xz
cd tar-1.29
FORCE_UNSAFE_CONFIGURE=1 ./configure --prefix=/usr --bindir=/bin
make
make install
make -C doc install-html docdir=/usr/share/doc/tar-1.29

Texinfo

cd $LFS/sources
rm -r texinfo-6.4
tar xvf texinfo-6.4.tar.xz
cd texinfo-6.4
./configure --prefix=/usr --disable-static
make
make install
make TEXMF=/usr/share/texmf install-tex

Vim

echo '#define SYS_VIMRC_FILE "/etc/vimrc"' >> src/feature.h
./configure --prefix=/usr
make
make install
ln -sv vim /usr/bin/vi
for L in  /usr/share/man/{,*/}man1/vim.1; do
    ln -sv vim.1 $(dirname $L)/vi.1
done
ln -sv ../vim/vim80/doc /usr/share/doc/vim-8.0.586

Configuring vim

cat > /etc/vimrc << "EOF"
" Begin /etc/vimrc

set nocompatible
set backspace=2
set mouse=r
syntax on
if (&term == "xterm") || (&term == "putty")
  set background=dark
endif
set spelllang=en
set spell


" End /etc/vimrc
EOF

touch ~/.vimrc

Cleaning up

rm -rf /tmp/*
rm -f /usr/lib/lib{bfd,opcodes}.a
rm -f /usr/lib/libbz2.a
rm -f /usr/lib/lib{com_err,e2p,ext2fs,ss}.a
rm -f /usr/lib/libltdl.a
rm -f /usr/lib/libfl.a
rm -f /usr/lib/libfl_pic.a
rm -f /usr/lib/libz.a

New chroot environment

chroot "$LFS" /usr/bin/env -i              \
    HOME=/root TERM="$TERM" PS1='\u:\w\$ ' \
    PATH=/bin:/usr/bin:/sbin:/usr/sbin     \
    /bin/bash --login

Don’t forget to mount virtual file systems and populate /dev before entering to chroot with the above command.

Configuring systemd

cat > /etc/systemd/system/getty@tty1.service.d/noclear.conf << EOF
[Service]
TTYVTDisallocate=no
EOF

Create /etc/fstab file with following contents

# Begin /etc/fstab

# file system  mount-point  type     options             dump  fsck
#                                                              order
UUID=87545a47-1866-44d6-8b93-12c61c8725a0 /      ext4    defaults        0       0

UUID=607e54d8-5c2f-4631-b353-6696ee821d9d /usr  ext4    defaults        0       0

UUID=e556fd19-2fbc-48a5-afd8-277076eb3252 /opt  ext4    defaults        0       0

UUID=f7ba6fac-2889-4317-a35a-483ce1519155 /tmp  ext4    defaults        0       0

UUID=f2d42e83-5c5d-425f-a290-0a52b6e7f0fd /usr/src      ext4    defaults        0       0

UUID=4c140ade-dcb4-4bb9-955e-b8caa0aaf0a7 /home ext4    defaults        0       0

UUID=7d2b4784-5afd-4aa3-a927-c3225d1b3b97 /boot ext4    defaults        0       0

UUID=4ed5b2db-1972-4d96-9543-c6ba260a526b none            swap    sw              0       0

# End /etc/fstab

Installation of kernel

make mrproper
make menuconfig

Compare following modules and make changes

General setup -->
   [ ] Enable deprecated sysfs features to support old userspace tools [CONFIG_SYSFS_DEPRECATED]
   [ ] Enable deprecated sysfs features by default [CONFIG_SYSFS_DEPRECATED_V2]
   [*] open by fhandle syscalls [CONFIG_FHANDLE]
   [ ] Auditing support [CONFIG_AUDIT]
   [*] Control Group support [CONFIG_CGROUPS]
Processor type and features  --->
   [*] Enable seccomp to safely compute untrusted bytecode [CONFIG_SECCOMP]
Networking support  --->
  Networking options  --->
   <*> The IPv6 protocol [CONFIG_IPV6]
Device Drivers  --->
  Generic Driver Options  --->
   [ ] Support for uevent helper [CONFIG_UEVENT_HELPER]
   [*] Maintain a devtmpfs filesystem to mount at /dev [CONFIG_DEVTMPFS]
   [ ] Fallback user-helper invocation for firmware loading [CONFIG_FW_LOADER_USER_HELPER]
Firmware Drivers  --->
   [*] Export DMI identification via sysfs to userspace [CONFIG_DMIID]
File systems  --->
   [*] Inotify support for userspace [CONFIG_INOTIFY_USER]
   <*> Kernel automounter version 4 support (also supports v3) [CONFIG_AUTOFS4_FS]
  Pseudo filesystems  --->
   [*] Tmpfs POSIX Access Control Lists [CONFIG_TMPFS_POSIX_ACL]
   [*] Tmpfs extended attributes [CONFIG_TMPFS_XATTR]
Processor type and features  --->
   [*]   EFI stub support  [CONFIG_EFI_STUB]
make
make modules_install
cp -v arch/x86/boot/bzImage /boot/vmlinuz-4.12.7-lfs-8.1-systemd
cp -v System.map /boot/System.map-4.12.7
cp -v .config /boot/config-4.12.7
install -d /usr/share/doc/linux-4.12.7
cp -r Documentation/* /usr/share/doc/linux-4.12.7
chown -R 0:0 *

Create a new file /etc/modprobe.d/usb.conf

install -v -m755 -d /etc/modprobe.d
cat > /etc/modprobe.d/usb.conf << "EOF"
# Begin /etc/modprobe.d/usb.conf

install ohci_hcd /sbin/modprobe ehci_hcd ; /sbin/modprobe -i ohci_hcd ; true
install uhci_hcd /sbin/modprobe ehci_hcd ; /sbin/modprobe -i uhci_hcd ; true

# End /etc/modprobe.d/usb.conf
EOF

Configuring grub

grub-install /dev/sda

Create /boot/grub/grub.cfg file with following content

# Begin /boot/grub/grub.cfg
set default=0
set timeout=5

insmod ext2
set root=(hd0,5)

menuentry "GNU/Linux, Linux 4.12.7-lfs-8.1-systemd" {
        linux   /vmlinuz-4.12.7-lfs-8.1-systemd root=/dev/sda10 ro
}

/dev/sda5 is the separate /boot partition.

For multiple operating systems:

menuentry "GNU/Linux, Linux 4.12.7-lfs-8.1-systemd" {
        insmod ext2
        set root=(hd0,5)
        linux   /vmlinuz-4.12.7-lfs-8.1-systemd root=/dev/sda10 ro
}

menuentry "Elementary OS, Linux 4.12.7-lfs-8.1-systemd" {
        insmod ext2
        set root=(hd0,1)
        linux   /boot/vmlinuz-4.10.0-32-generic root=/dev/sda1 ro
        initrd  /boot/initrd.img-4.10.0-32-generic
}

For most of the commercial distributions, initrd is required.

Finishing up

cat > /etc/os-release << "EOF"
NAME="Linux From Scratch"
VERSION="8.1-systemd"
ID=lfs
PRETTY_NAME="Linux From Scratch 8.1-systemd"
VERSION_CODENAME="nayab"
EOF
echo 8.1-systemd > /etc/lfs-release
cat > /etc/lsb-release << "EOF"
DISTRIB_ID="Linux From Scratch"
DISTRIB_RELEASE="8.1-systemd"
DISTRIB_CODENAME="naybas"
DISTRIB_DESCRIPTION="Linux From Scratch"
EOF

Reboot

logout
umount -v $LFS/dev/pts
umount -v $LFS/dev
umount -v $LFS/run
umount -v $LFS/proc
umount -v $LFS/sys
umount -v $LFS/usr/src
umount -v $LFS/usr
umount -v $LFS/tmp
umount -v $LFS/home
umount -v $LFS/boot
umount -v $LFS