7.3. Setting up the Environment

The current shell is also the init process, so exiting from it will cause kernel panic. Prevent exiting from the shell accidentally:

enable -n exit exec
readonly IGNOREEOF=1000

The standard I/O streams of the initial shell process is connected with /dev/console. However, the testsuite of some packages may expect the standard I/O streams to be connected with a real TTY device node. Spawn a new shell process on the TTY device with agetty:

agetty -n -l /bin/bash tty0

If you are working via a serial console, replace tty0 with the name of the serial console device node, for example ttyS0.

The command above spawns a new shell process on the TTY device specified in the command, and the initial shell process will run in background as an init process with very limited functions. The new shell process will output:

bash: cannot set terminal process group (-1): Inappropriate ioctl for device
bash: no job control in this shell

This is normal because the shell is not assigned with a controlling terminal yet. Now set up the controlling terminal and some environment variables:

exec setsid -c /usr/bin/env -i  \
    HOME=/root                  \
    TERM="$TERM"                \
    PS1='(lfs) \u:\w\$ '        \
    PATH=/usr/bin:/usr/sbin     \
    MAKEFLAGS="-j$(nproc)"      \
    TESTSUITEFLAGS="-j$(nproc)" \
    /bin/bash --login

The command replaces the current shell process with a new shell process, with controlling terminal set up.

If you don't want to use all available logical cores, replace $(nproc) with the number of logical cores you want to use for building packages in this chapter and the following chapters. The test suites of some packages (notably Autoconf, Libtool, and Tar) in Chapter 8 are not affected by MAKEFLAGS, they use a TESTSUITEFLAGS environment variable instead. We set that here as well for running these test suites with multiple cores.

The -i option given to the env command will clear all the variables in the environment. After that, only the HOME, TERM, PS1, and PATH variables are set again. The TERM=$TERM construct will set the TERM variable to the default value specified by agetty. This variable is needed so programs like vim and less can operate properly. If other variables are desired, such as CFLAGS or CXXFLAGS, this is a good place to set them.

Notice that /tools/bin is not in the PATH. This means that the cross toolchain will no longer be used.

Also note that the bash prompt will say I have no name! This is normal because the /etc/passwd file has not been created yet.

Now set up a temporary hostname, which is required by test suite of some packages:

hostname lfs
[Note]

Note

It is important that all the commands throughout the remainder of this chapter and the following chapters are run from within the environment we've set. If you leave this environment for any reason (rebooting for example), ensure that the virtual kernel filesystems are mounted as explained in Section 7.2.1, “Mounting Virtual Kernel File Systems” and Section 7.2.2, “Adjusting devtmpfs” and set up the environment again before continuing with the installation.