Entering the Chroot Environment

Now that all the packages which are required to build the rest of the needed tools are on the system, it is time to enter the chroot environment and finish installing the temporary tools. This environment will also be used to install the final system. As user root, Create the Following Helper Script:

cat > lfs-chroot.sh << "EOF"
#!/bin/bash
zprint() { echo -e "\033[1;32m *** $1 *** \033[0m"; }
stars() { printf '%.0s*' {1..100}; printf '\n'; }
LFS=/mnt/lfs

chroot_pre() {
    stars
    zprint " === Mounting Virtual Kernel Filesystems === "
    mkdir -pv $LFS/{dev,proc,sys,run}
    mount --types proc /proc $LFS/proc
    mount --rbind /sys $LFS/sys
    mount --make-rslave $LFS/sys
    mount --rbind /dev $LFS/dev
    mount --make-rslave $LFS/dev
    mount --rbind /run $LFS/run
    mount --make-slave $LFS/run

    if [ -h $LFS/dev/shm ]; then
        install -v -d -m 1777 $LFS$(realpath /dev/shm)
    else
        mount -vt tmpfs -o nosuid,nodev tmpfs $LFS/dev/shm
    fi
    if [ ! -f $LFS/etc/resolv.conf ]; then
        printf "nameserver 1.1.1.1\nnameserver 8.8.8.8\n" > $LFS/etc/resolv.conf
    fi
    stars
}

chroot_exec() {
    stars
    zprint " === Entering Chroot $LFS === "
    /usr/sbin/chroot "$LFS" \
    /usr/bin/env -i HOME=/root TERM="$TERM" \
    PS1='[$?](lfs chroot) \u:\w\$ ' \
    PATH=/usr/bin:/usr/sbin \
    MAKEFLAGS="-j$(nproc)" \
    TESTSUITEFLAGS="-j$(nproc)" \
    /bin/bash --login
    zprint " === Welcome Back === "
    stars
}
check_unmount() { mountpoint -q "$1" && umount -v -l "$1"; }

chroot_post() {
    stars
    zprint " === Un-Mounting Virtual Kernel Filesystems === "
    check_unmount $LFS/sys/firmware/efi/efivars
    check_unmount $LFS/dev
    check_unmount $LFS/run
    check_unmount $LFS/proc
    check_unmount $LFS/sys
    stars
}
stars
# checks if directory exists
[ ! -d $LFS ] && { zprint "Error $LFS is not a mountpoint"; exit 1; }

# mounts virtual kernel filesystems
chroot_pre

# enters the new root environment
chroot_exec

# cleans up the virtual kernel filesystems
chroot_post

stars
EOF

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 5 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 chroot environment. After that, only the HOME, TERM, PS1, and PATH variables are set again. The TERM=$TERM construct sets the TERM variable inside chroot to the same value as outside chroot. 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.

From this point on, there is no need to use the LFS variable any more because all work will be restricted to the LFS file system; the chroot command runs the Bash shell with the root (/) directory set to $LFS.

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.

[Note]

Note

It is important that all the commands throughout the remainder of this chapter and the following chapters are run from within the chroot environment. If you leave this environment for any reason (rebooting for example), ensure that the virtual kernel filesystems are mounted as explained in the section called “Mounting and Populating /dev” and the section called “Mounting Virtual Kernel File Systems” and enter chroot again before continuing with the installation.