

\section{Build instructions}

The full build sequence is as follows.

\subsection{Get ready build tools}

Make available \Prog{any} commands by default path.
Execute inside the working directory, created in previous section:
\begin{verbatim}
PATH=$(pwd)/rrr/bin:$PATH
\end{verbatim}

You should execute that command in each terminal session you use for building.
If session was closed (machine reboot), you will need to execute again the command above.

\subsection{Try the build process}

Do the checking build with command:

\begin{verbatim}
any do lfs,pass1 grep-3.4
\end{verbatim}

\begin{itemize}
    \item[-] \Prog{any do} is the command to build packages.
    \item[-] \Arg{lfs,pass1} is the name of the configuration, used for the build.
        The main part is \Arg{lfs}, and word \Arg{pass1} is added to set up the unique settings
        for first temporary pass.
    \item[-] \Arg{grep-3.4} is the name of the package you wish to build.
        Program \Arg{grep} is chosen as simple sample, it has no special purpose.
\end{itemize}

The shell code, which describes specific actions to build \Arg{grep-3.4}, is located in:

\File{ports/packages/grep-3.4/grep.build}

If build was successfull, it will be automatically installed into \File{./tools} directory inside your working directory.
Also, the log of everything was saved into:

\File{./build/log/pass1-lfs/grep-3.4.log}

Consult the page \href{../tutorial.html}{Tutorial for LFS with any} in case of questions about build mechanics and possible errors.


\subsection{Clean up before procceeding}

Clean up everything before moving from test actions to real build:

\begin{verbatim}
rm -rf ./usr ./build
\end{verbatim}

\subsection{Build first temporary environment}

Build all packages from \Arg{tools} set, which form first temporary build environment.

The entire set is built with single command:

\begin{verbatim}
any do lfs,pass1 tools-%<LFS_VERSION>.src
\end{verbatim}

\begin{itemize}
    \item[-] \Arg{tools-%<LFS_VERSION>.src} is the text file, where all package names from \Arg{tools} set are written.
        It is located in \File{ports/list/} directory.
    \item[-] \Arg{pass1} is the configuration word which says: our packages belong to temporary set.
\end{itemize}

You will see name and status of each built package, one at a line.
Each built package is automatically installed into your working directory under default prefix \File{/usr}.

If no \texttt{FAIL} or \texttt{SKIP} statuses are occured, you are ready to continue.
If something had failed or been skipped, path to log file was printed. Examine it to understand the reason of failure,
correct it and build the failed packages again with command:

\begin{verbatim}
any do lfs,pass1 package-to-rebuild-1.2.3
\end{verbatim}

You may rebuild several packages at once as well:

\begin{verbatim}
any do lfs,pass1 package-1.2.3,foo-2.0,bar-3.4
\end{verbatim}

Here the \Arg{package-1.2.3}, \Arg{foo-2.0}, \Arg{bar-3.4} are the names of sample packages to rebuild.

Do not continue to the next section until all packages are properly built.
Each of them is needed, and the absence of any package will lead to further fatal errors.

\subsection{Preparation of temporary chroot}

We need to set up the freshly built environment, so it would be correct chroot container.

Enter the temporary environment:
\begin{verbatim}
sudo chroot `pwd` /bin/bash -l
\end{verbatim}

As previous building was done from generic user, all created files have that generic user as owner.
Some programs are needed to be executed with root privileges and to have \texttt{root} owner.
\begin{verbatim}
chown root:root \
    /bin/mount \
    /usr/bin/sudo \
    /bin/umount \
    /usr/lib/sudo/sudoers.so \
    /etc/sudo.conf \
    /etc/sudoers \
    /etc/sudoers.d
chmod 4555 /bin/mount /usr/bin/sudo /bin/umount
\end{verbatim}
\Prog{chmod} command sets up the \texttt{SUID} bit on binaries, so they will be able to reach the root privileges.

Now execute special script, which prepares isolated filesystem for package building.
\begin{verbatim}
/any/bin/enter
\end{verbatim}
This script creates device files and mounts virtual filesystems, provided by kernel of an OS.
Later examine that script to understand the effect.

Exit the temporary container:
\begin{verbatim}
exit
\end{verbatim}

Now turn off the execution of preparational script. It will not be needed on regular basis.
\begin{verbatim}
chmod -x ./any/bin/enter
\end{verbatim}

After preparations above our self-built isolated environment will be ready
for regular chrooting.

\subsection{Build the second part of temporary system}

Below the build command \Prog{anch} is used, which does chrooting automatically,
so user stays in comfortable generic environment, while actual building is going isolated.

\begin{verbatim}
anch do lfs,passinside tools-inside-%<LFS_VERSION>.src
\end{verbatim}

\begin{itemize}
    \item[-] \Arg{tools-inside-%<LFS_VERSION>.src} is the text file with second part of temporary system.
        It is located in \File{ports/list/} directory.
    \item[-] \Arg{passinside} is the configuration word which says: our packages belong to temporary set,
        but they are built inside container.
\end{itemize}

\subsection{Build the essential system}

Now we are ready to build packages for the main system.

Build the first part:
\begin{verbatim}
anch do lfs lfs-part1-%<LFS_VERSION>.src
\end{verbatim}
\begin{itemize}
    \item[-] \Prog{anch do} is command to do the build. It does all the same as \Prog{any do}, but goes into chroot before the build launch.
    \item[-] \Arg{lfs} is the name of configuration.
    \item[-] \Arg{lfs-part1-%<LFS_VERSION>.src} is the list with first part of packages. It ends with \Arg{bash} package.
\end{itemize}

After the finish of the given command the package \Arg{bash} will be installed into environment.
When the next build command will be launched, freshly built \Prog{bash} at \File{./bin/bash} will be used instead of the one from temporary set.


Build the remaining part:
\begin{verbatim}
anch do lfs lfs-part2-%<LFS_VERSION>.src
\end{verbatim}
\begin{itemize}
    \item[-] \Arg{lfs-part2-%<LFS_VERSION>.src} contains the remaining packages, included in LFS.
\end{itemize}

Again in case of any errors you should investigate the problem and rebuild the failed packages.

During \Arg{lfs-part2-%<LFS_VERSION>.src} the packages \Arg{util-linux} and \Arg{sudo} should be built and installed, which need setting up to work properly.
Enter chroot for that:
\begin{verbatim}
sudo chroot `pwd` /bin/bash -l
\end{verbatim}

After default chroot we are root user.
Set up the \Arg{sudo} files, which need to be root-owned and executable must have SUID-bit (that's value 4 in argument for \Prog{chmod} command):

\begin{verbatim}
chown root:root \
    /usr/bin/sudo \
    /usr/lib/sudo/sudoers.so \
    /etc/sudo.conf \
    /etc/sudoers \
    /etc/sudoers.d
chmod 4555 /usr/bin/sudo
\end{verbatim}

Set up in the same way \Prog{mount} utils:

\begin{verbatim}
chown -R root:root \
    /bin/mount \
    /bin/umount
chmod 4555 /bin/mount /bin/umount
\end{verbatim}

Leave the chroot:
\begin{verbatim}
exit
\end{verbatim}

At this moment we have all packages built and environment is ready for further actions.


\subsection{Leave the materials for installation}

We need to do some preparations for further installation.

\begin{verbatim}
lspkg -F lfs lfs-part1-%<LFS_VERSION>.src,lfs-part2-%<LFS_VERSION>.src > package-list.txt
\end{verbatim}
\begin{itemize}
    \item[-] \Prog{lspkg} is the tool to manipulate the lists of packages.
        With \Opt{-f} flag \Prog{lspkg} will list names of binary archives, which are expected for given package set.
    \item[-] \Arg{lfs} is the name of configuration.
    \item[-] \Arg{lfs-part1-%<LFS_VERSION>.src,lfs-part2-%<LFS_VERSION>.src} contain all packages from main set.
    \item[-] So file \File{package-list.txt} will keep names of all binary packages we have just built.
        That will be used for installation of packages to target machine and device.
\end{itemize}

\subsection{Cleaning up the environment}

As the main build environment is ready, remove unused heavy-weight materials.

Remove temporary build toolchain in \File{./tools/}.
\begin{verbatim}
rm -rf tools
\end{verbatim}

Remove the directory with object files after compilation of the sources, linking and other build processes.
It is useful for debug, but takes quite a lot of disk space.
\begin{verbatim}
rm -rf ./build/work
\end{verbatim}


\section{Further building}

Now our host environment is completely ready for building additional packages.
\begin{verbatim}
anch do lfs some-package-1.2.3
anch do lfs something-large.src
\end{verbatim}

The packages to build can be taken from \URL{http://www.linuxfromscratch.org/blfs/}{Beyond Linux From Scratch}.


\section{Build review}

While build process is going, you may want to review the actual build code, written for each package.
The content of anybuilds can be seen here:

\href{listing.html}{Build listing}.

Listing is not needed to complete the LFS, but is needed for understanding the content of the system.
