
\section{Installation instructions}

As the building is over, we can install our packages.
The installation is possible on another machine or another disk. It can be done separately from build process, described above.

For example, you may save the built packages \Arg{*.tgz} from \File{build/pack/lfs/} directory
and the list of all packages \File{package-list.txt} at some regular directory.
When you decide to do the installation, you will need just that materials, without build environment.

\subsection{Prepare the disk}

In the example below we use device file \File{/dev/sdd} as the target for our installation.
The directory \File{/mnt/} will contain filesystem, where we are installing into.
Commands of this section should be executed from \Arg{root} user.

Create partitions on \File{/dev/sdd} device for our installation.
\begin{verbatim}
gdisk /dev/sdd
# create BIOS Boot partition (/dev/sdd1)
# create Main partition (/dev/sdd2)
mkfs.ext4 /dev/sdd2
\end{verbatim}

Optionally, you may locate your system on more then one partition of the disk.
The widespread practice is locating directories like \File{var/} or \File{tmp/}
on separate partitions.
\begin{verbatim}
gdisk /dev/sdd
# create more partitions of your choice: /dev/sdd3, /dev/sdd4, etc
mkfs.ext4 /dev/sdd3
mkfs.ext4 /dev/sdd4
...
\end{verbatim}

Connect the hardware device and directory, available to us:
\begin{verbatim}
mount /dev/sdd2 /mnt
\end{verbatim}
Now \File{/mnt} reflects the content of the disk, where our system would be located.

Prepare directory layout on target disk:
\begin{verbatim}
tar xfp build/pack/lfs/rootdirs-0.0*.tgz -C /mnt
\end{verbatim}
Package \Arg{rootdirs} contains directories for root filesystem, needed by OS.

If you had created additional partitions, mount them at locations of your choice inside \File{/mnt/}:
\begin{verbatim}
# mount other partitions, if you had done some
# mount /dev/sdd3 /mnt/var
# mount /dev/sdd4 /mnt/tmp
...
\end{verbatim}


\subsection{Package installation}

Install packages!

If you are still inside the working directory, where the system was built, execute:
\begin{verbatim}
for i in $( cat package-list.txt ) ; do
    tar xfp $i -C /mnt
done
\end{verbatim}
\begin{itemize}
    \item[-] \File{package-list.txt} is the result of \Prog{lspkg} launch, listed in build instructions.
    \item[-] Argument \Arg{p} is used at \Prog{tar} invocation to preserve file permissions during the extraction.
\end{itemize}

If you are installing another machine, it make sense to copy the built packages and their list to separate directory.
Then the extracting the archives would be like:
\begin{verbatim}
for i in $( ls dir-with-packages/* ) ; do
    tar xfp $i -C /mnt
done
\end{verbatim}

Do the additional cleanup:
\begin{verbatim}
# FIXME
rm -rf /mnt/tools
rmdir /mnt/lib64
ln -s lib /mnt/lib64
\end{verbatim}


\subsection{Prepare boot configuration}

Edit configuration files to boot up.
\begin{verbatim}
# edit /mnt/etc/fstab
# edit /mnt/boot/grub/grub.cfg
\end{verbatim}

\subsection{Prepare installed filesystem}

We need to setup our installed environment: execute package setup scripts, so that programs would correctly work.
For that we need to chroot into installed filesystem.

Copy container setup script into installed filesystem. It will be available under the path \File{/sbin/enter}.
That script will perform essential basic settings, making possible all the rest of the setup.
\begin{verbatim}
cp any/bin/enter /mnt/sbin/
\end{verbatim}

Now chroot into installed filesystem.
\begin{verbatim}
chroot /mnt
\end{verbatim}

Manually prepare the \File{/bin/sh} alias. It is needed before the execution of shell scripts.
\begin{verbatim}
ln -s bash /bin/sh
\end{verbatim}

Now as shell scripts work, perform container setting up.
\begin{verbatim}
/sbin/enter
\end{verbatim}

Do the main setup of each package.
\begin{verbatim}
for i in /var/lib/pkg/scripts/* ; do
    $i
done
\end{verbatim}

Generate password for root user.
\begin{verbatim}
passwd
\end{verbatim}


\subsection{Prepare the bootloader}

We need to initialise GRUB bootloader in order to boot from the device.

To set up bootloader correctly, we need \File{./dev/} filesystem, populated with devices.
That filesystem is managed by \Prog{udev}. So start \Arg{udev} init service:
\begin{verbatim}
/etc/rc.d/rcS.d/S00mountvirtfs start
/etc/rc.d/rcS.d/S10udev start
\end{verbatim}
First init script \Arg{mountvirtfs} is needed, as \Prog{udev} depends on kernel virtual filesystems
to determine what disk devices are on board.

Now install the bootloader record into our disk:
\begin{verbatim}
grub-install --target=i386-pc /dev/sdd
\end{verbatim}


\subsection{Finish the installation}

Clean up everything.

This will stop the process of \Prog{udev} daemon, launched at \Prog{Grub} initialisation.
\begin{verbatim}
pkill -1 udevd
\end{verbatim}

Unmount the virtual filesystems, managed by kernel of the OS.
\begin{verbatim}
umount /run /sys /proc /dev
\end{verbatim}

Leave the target environment.
\begin{verbatim}
exit
\end{verbatim}

If we had done additional partitions earlier, unmount them first:
\begin{verbatim}
# umount /mnt/tmp
# umount /mnt/var
...
\end{verbatim}

Now unmount main installed filesystem.
\begin{verbatim}
umount /mnt
\end{verbatim}

And we are done!


\section{Result}

In the example above we install our self-built system to \File{/dev/sdd2} partition.
We can reboot, choose lfs menu entry in \texttt{Grub} and get to it.

Moreover, the entire system had been saved in directory \File{build/pack/lfs/} in form of binary tar packages.
It can be reinstalled again to another device or machine.

\end{Name}

\LatexManEnd
\end{document}
