Linux From Scratch 7.7 Systemd

  1. Downloading the book (HTML version or PDF)

    $ wget http://www.linuxfromscratch.org/lfs/downloads/stable-systemd/LFS-BOOK-7.7-systemd.pdf
    
  2. Host System\ Thinkpad X61, 2GB of RAM, Core2 Duo T8100 2.10GHz\ Hardened-Gentoo x86_64\ GPT Partition\ 172.16.0.66\ Separate partition /dev/sda4, for LFS

Host Partition:\ Device          Start          End              Sectors        Size    Type\ /dev/sda1      2048          6143            4096            2M      BIOS boot\ /dev/sda2      6144          268287        262144        128M  Linux filesystem\ /dev/sda3      268288      8656895      8388608      4G       Linux filesystem\ /dev/sda4      8656896    29628415    20971520    10G     Linux filesystem\ /dev/sda5      29628416  234439599  204811184  97.7G  Linux filesystem

  1. Creating $LFS variable

    $ ssh fahmi@172.16.0.66
    $ su -
    root# mkdir /mnt/lfs
    root# export LFS=/mnt/lfs
    root# vim /etc/profile
    export LFS=/mnt/lfs
    
  2. Creating Tmux session, Downloading needed files and Checking the MD5 hash of the downloaded files

    $ ssh fahmi@172.16.0.66
    fahmi@gentoo $ tmux new -s LFS
    fahmi@gentoo $ su -
    root# mount /dev/sda4 /mnt/lfs
    root# cd /mnt/lfs
    root# mkdir sources
    root# wget http://www.linuxfromscratch.org/lfs/downloads/stable-systemd/md5sums
    root# wget http://www.linuxfromscratch.org/lfs/downloads/stable-systemd/wget-list
    root# wget --input-file=wget-list --continue --directory-prefix=$LFS/sources
    root# cp mv md5sums wget-list sources/
    root# pushd $LFS/sources
    root# md5sums -c md5sums
    root# popd
    

    Creating $LFS/tools directory and Creating the /tools symlink on the host system

    root# mkdir -v $LFS/tools
    root# ln -sv $LFS/tools /
    root# groupadd lfs
    root# useradd -s /bin/bash -g lfs -m -k /dev/null lfs
    root# passwd lfs
    root# chown -v lfs $LFS/tools
    root# chown -v lfs $LFS/sources
    root# su - lfs
    

    Setting Up the environment

    lfs@gentoo$ cat > ~/.bash_profile << "EOF"
       > exec env -i HOME=$HOME TERM=$TERM PS1='\u:\w$ ' /bin/bash
       > EOF
       > lfs@gentoo$ cat > \~/.bashrc << "EOF"
       > set +h
       > umask 022
       > LFS=/mnt/lfs
       > LC_ALL=POSIX
       > LFS_TGT=$(uname -m)-lfs-linux-gnu
       > PATH=/tools/bin:/bin:/usr/bin
       > export LFS LC_ALL LFS_TGT PATH
       > EOF
       > lfs@gentoo$ source \~/.bash_profile
       > lfs@gentoo$ export MAKEFLAGS='-j 3'
    
**Important Notice:**

Constructing a Temporary System
To re-emphasize the build process:
1. Place all the sources and patches in a directory that will be accessible from the chroot environment such as /mnt/lfs/sources/. Do not put sources in /mnt/lfs/tools/.
2. Change to the sources directory.
3. For each package:
   a. Using the tar program, extract the package to be built. In Chapter 5, ensure you are the lfs user when extracting the package.
   b. Change to the directory created when the package was extracted.
   c. Follow the book's instructions for building the package.
   d. Change back to the sources directory.
   e. Delete the extracted source directory and any `<package>-build` directories that were created in the build process unless instructed otherwise.
-> If you mount your partition on /mnt/lfs and set up the environment variable the same as the book told you, copy-paste is your friend.\
-> Always remember, during constructing a temporary system, you're compiling program inside $LFS/sources.
  1. Several Example of compiling programs:

BINUTILS

lfs@gentoo$ echo $LFS
/mnt/lfs
lfs@gentoo$ cd $LFS
lfs@gentoo$ cd sources
lfs@gentoo$ tar xjvf binutils-2.25.tar.bz2
lfs@gentoo$ cd binutils-2.25
lfs@gentoo$ mkdir -v ../binutils-build
lfs@gentoo$ cd ../binutils-build
lfs@gentoo$ ../binutils-2.25/configure     \
> \--prefix=/tools            \
>     --with-sysroot=$LFS        \
>     --with-lib-path=/tools/lib \
>     --target=$LFS_TGT          \
>     --disable-nls              \
>     --disable-werror
> lfs@gentoo$ make
> lfs@gentoo$ case $(uname -m) in
>   x86_64) mkdir -v /tools/lib && ln -sv lib /tools/lib64 ;;
> esac
> lfs@gentoo$ make install

GCC

lfs@gentoo$ cd ../
lfs@gentoo$ tar xjvf gcc-4.9.2.tar.bz2
lfs@gentoo$ rm -rf binutils-build/
lfs@gentoo$ cd gcc-4.9.2
lfs@gentoo$ tar -xf ../mpfr-3.1.2.tar.xz
lfs@gentoo$ mv -v mpfr-3.1.2 mpfr
lfs@gentoo$ tar -xf ../gmp-6.0.0a.tar.xz
lfs@gentoo$ mv -v gmp-6.0.0 gmp
lfs@gentoo$ tar -xf ../mpc-1.0.2.tar.gz
lfs@gentoo$ mv -v mpc-1.0.2 mpc
lfs@gentoo$ for file in \
$(find gcc/config -name linux64.h -o -name linux.h -o -name sysv4.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
lfs@gentoo$ sed -i '/k prot/agcc_cv_libc_provides_ssp=yes' gcc/configure
lfs@gentoo$ mkdir -v ../gcc-build
lfs@gentoo$ cd ../gcc-build
lfs@gentoo$ ../gcc-4.9.2/configure                             \
    --target=$LFS_TGT                              \
    --prefix=/tools                                \
    --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-libitm                               \
    --disable-libquadmath                          \
    --disable-libsanitizer                         \
    --disable-libssp                               \
    --disable-libvtv                               \
    --disable-libcilkrts                           \
    --disable-libstdc++-v3                         \
    --enable-languages=c,c++
lfs@gentoo$ make
lfs@gentoo$ make install

LINUX-3.19 API HEADERS

lfs@gentoo$ cd ../
lfs@gentoo$ tar xvf linux-3.19.tar.xz
lfs@gentoo$ rm -rf gcc-build/
lfs@gentoo$ cd linux-3.19
lfs@gentoo$ make mrproper
lfs@gentoo$ make INSTALL_HDR_PATH=dest headers_install
lfs@gentoo$ cp -rv dest/include/* /tools/include

-> I'm not gonna elaborate the whole process, so read the book, guys :).\ -> I suggest you perform the "sanity check" if you're told to do so.

  1. Stripping

    6. lfs@gentoo$ strip --strip-debug /tools/lib/*
       lfs@gentoo$ /usr/bin/strip --strip-unneeded /tools/{,s}bin/*
    
  2. Save more

    lfs@gentoo$ rm -rf /tools/{,share}/{info,man,doc}
    
  3. Changing the Ownership of $LFS/tools

    root# chown -R root:root $LFS/tools
    
  4. Preparing Virtual Kernel File Systems

    root# mkdir -pv $LFS/{dev,proc,sys,run}
    
  5. Creating Initial Device Nodes

    root# mknod -m 600 $LFS/dev/console c 5 1
    root# mknod -m 666 $LFS/dev/null c 1 3
    
  6. Mounting and Populating /dev

    root# mount -v --bind /dev $LFS/dev
    
  7. Mounting Virtual Kernel File Systems

    root# mount -vt devpts devpts $LFS/dev/pts -o gid=5,mode=620
    root# mount -vt proc proc $LFS/proc
    root# mount -vt sysfs sysfs $LFS/sys
    root# mount -vt tmpfs tmpfs $LFS/run
    
  8. For some host systems, which its /dev/shm is a symbolic link to /run/shm:

    root# if [ -h $LFS/dev/shm ]; then
       mkdir -pv $LFS/$(readlink $LFS/dev/shm)
       fi
    

    -> If you reboot the server, before chrooting into LFS environment, do Mounting and Populating /dev and Mounting Virtual Kernel File Systems

-> If you need package management, Install it now. Especially if you want LFS to be your primary Distro Linux. Just keep in mind, you have to maintain the software yourself, it's too much work.

  1. Entering the Chroot Environment

    root# 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
    

This is how it looks like

root

  1. Creating Directories

    root# mkdir -pv /{bin,boot,etc/{opt,sysconfig},home,lib/firmware,mnt,opt}
    root# mkdir -pv /{media/{floppy,cdrom},sbin,srv,var}
    root# install -dv -m 0750 /root
    root# install -dv -m 1777 /tmp /var/tmp
    root# mkdir -pv /usr/{,local/}{bin,include,lib,sbin,src}
    root# mkdir -pv /usr/{,local/}share/{color,dict,doc,info,locale,man}
    root# mkdir -v /usr/{,local/}share/{misc,terminfo,zoneinfo}
    root# mkdir -v /usr/libexec
    root# mkdir -pv /usr/{,local/}share/man/man{1..8}
    root# case $(uname -m) in
    > x86_64) ln -sv lib /lib64
    > ln -sv lib /usr/lib64
    > ln -sv lib /usr/local/lib64 ;;
    >esac
    
    root# mkdir -v /var/{log,mail,spool}\
    root# ln -sv /run /var/run\
    root# ln -sv /run/lock /var/lock\
    root# mkdir -pv /var/{opt,cache,lib/{color,misc,locate},local}
    
  2. Creating Essential Files and Symlinks

    root# ln -sv /tools/bin/{bash,cat,echo,pwd,stty} /bin
    root# ln -sv /tools/bin/perl /usr/bin
    root# ln -sv /tools/lib/libgcc_s.so{,.1} /usr/lib
    root# ln -sv /tools/lib/libstdc++.so{,.6} /usr/lib
    root# sed 's/tools/usr/' /tools/lib/libstdc++.la > /usr/lib/libstdc++.la
    root# ln -sv bash /bin/sh
    root# ln -sv /proc/self/mounts /etc/mtab
    root# cat > /etc/passwd <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
    >nobody:x:99:99:Unprivileged User:/dev/null:/bin/false
    >EOF
    root# cat > /etc/group <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:
    >nogroup:x:99:
    >users:x:999:
    >EOF
    root# exec /tools/bin/bash --login +h
    root# touch /var/log/{btmp,lastlog,wtmp}
    root# chgrp -v utmp /var/log/lastlog
    root# chmod -v 664 /var/log/lastlog
    root# chmod -v 600 /var/log/btmp
    

A few suggestion during Installing Basic System Software:\ -> Perform every test suite. Most of them are mandatory\ -> Glibc, GCC, linux kernel will take very long time to compile. Be patient :)\ -> Perform every "sanity check" if you're told to.\ -> Don't forget, "cd /sources"

  1. Stripping Again\ root# logout\ Reenter Chroot Environment with:

    root# chroot $LFS /tools/bin/env -i \
    HOME=/root TERM=$TERM PS1='\u:\w\$ ' \
    PATH=/bin:/usr/bin:/sbin:/usr/sbin \
    /tools/bin/bash --login
    root# /tools/bin/find /{,usr/}{bin,lib,sbin} -type f \
    -exec /tools/bin/strip --strip-debug '{}' ';'
    root# rm -rf /tmp/*
    

    Modified Chroot Command, do it every time you're entering the chroot environment.

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

    -> You can remove /tools now, but you will also remove the temporary copies of Tcl, Expect, and DejaGNU which were used for running the toolchain tests.\ -> I will skip the Basic System Configuration

  2. Making the LFS Bootable