Linux From Scratch Compilation Progress(System Preparation)

Keith Lv2

Linux From Scratch (System Preparation)

This article provides a brief introduction to partition-related knowledge in LFS . Personally, I believe that the content covered in this section is quite valuable and worth understanding. Many subsequent tasks mainly involve compiling various components, and so on. If you are interested in learning about the specific compilation process, application, and principles of each package, you can refer to the relevant content in the LFS/BLFS manual. The manual is regularly updated at each stage, and there may be some minor differences in the compilation methods. Paying attention to these distinctions will help avoid significant issues.

  1. First, we need to have a host operating system. Personally, I am using the AlmaLinux system . To avoid partitioning our main system and risking damage, it is more cost-effective to use VirtualBox as a virtualization software to set up the host system. Alternatively, we can use VMware on Windows for system setup. In this case, we will use the Gentoo live image file for the host system. This allows for easier packaging of the image and partitioning. VirtualBox provides executable installation files for both Windows and Linux. For Debian and RedHat-based operating systems, software installation can be done directly through the dpkg or rpm package managers.
  2. Next, it is important to develop a good habit of taking timely snapshots after completing each phase of operations in the virtual machine. Additionally, during the process of preparing the host system with Gentoo, it is recommended to perform all operations with root privileges. Now let’s move on to the more specific disk partitioning operations.
1
2
3
4
5
sudo su -
# change your previlige to root in order to the next operation
fdisk -l
# mainly focus on the "fdisk" command and it has to been runing in the
# root environment which is based on the Gentoo liveCD

1.Host System Prepartion

After performing the “fdisk -l” operation, typically the disk partitions listed in the Gentoo live CD are labeled as /dev/sda. However, this situation does not include cases where users have already performed disk partitioning using other systems such as Ubuntu as the host system.

If the /dev/sda partition appears after executing the “fdisk -l” command, proceed to select this partition for further disk partitioning. The main partitions consist of three major divisions: /boot partition, /swap partition, and /root partition. The /boot partition is used for system booting, the /swap partition is used for memory swapping, and the /root partition is used for user or administrator data storage. They are sequentially designated as /dev/sda1, /dev/sda2, and /dev/sda3. The overall data storage partition should have a minimum space of 30GB. Next, change the swap partition type to “Linux swap/solaris” in Linux. After completing all the steps, you need to format each newly created partition. Finally, before formatting, make sure to write the partition changes to the disk. Otherwise, when you restore the system using snapshots, the partitioning changes will not have any substantial effect. The following is a list of commands and the order of operations:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
fdisk /dev/sda 
p
# type "p" and then you will discover /dev/sda partition have ful space and
# there is no storage information in it
n
# type "n" is used for setting up a new partition for lfs system
3
# type "3" is used for assigning memory for each partition, commonly,
# /dev/sda1-->/boot==256MB
# /dev/sda2-->/swap==1GB/2GB
# /dev/sda3-->/root==30GB or more
w
# write the partition change into the disk

# Next, we need to format the partition that has been created. However, since the three partitions have different types, there will be slight differences in the formatting commands.

mkfs -v -t ext4 /dev/sda3
mkfs -v -t ext4 /dev/sda1
mkswap /dev/sda2

Once all the previous steps have been completed, we have successfully partitioned the disk and performed the basic formatting operations. Now, we will proceed to create the /boot partition for LFS and mount the previously created /dev/sda1 partition (with a size of 256MB) to the specified location. To ensure the safety of the host system and avoid any interference, all the partition mounting operations will be performed under the /mnt directory. For convenience in subsequent operations, we have set the environment variable export LFS=/mnt/lfs in the host system beforehand. You can verify if this command is effective by using the echo $LFS command. Next, to mount the /boot partition and create the /boot directory, follow the steps below:

1
2
3
4
mkdir -pv $LFS && mkdir -pv $LFS/boot
mount -v -t ext4 /dev/sda1 $LFS/boot
mount -v -t ext4 /dev/sda3 $LFS
# in order to mount /dev/sda3 partition as root in /mnt/lfs directory

Next, since we previously created the /swap partition for memory swapping, we still need to activate the /swap partition in LFS. However, it’s worth noting that with the continuous development of devices and underlying hardware, the significance of /swap has become less necessary compared to ancient times. In reality, it is optional. Nevertheless, for the sake of completeness in the LFS build process, we will still create and activate the /swap partition. You can use the following command to activate it:

1
2
3
/sbin/swapon -v /dev/sda2
# here we finally finished the host prepartion and don't forget saving snapshot in
# virtual machine

2.Package Download

After preparing the host system, it is necessary to download the source code for all the software following the instructions in the LFS manual. After downloading, refrain from immediately extracting the packages. First, perform an MD5 checksum verification for these installation packages using the command “md5sum” (e.g., man md5sum). Start by creating a “sources” folder in the $LFS directory using the command mkdir -pv $LFS/sources. Once all the source code files have been downloaded and verified, ensure that all the files in the “sources” folder have root permissions using the command chown root:root $LFS/sources/\*.

3.Group Useradd and file structure demonstration

After completing the above steps, you can use the ls or ls -l command to view the structure of the $LFS directory as follows:

Partition Specific application
/lost+found Default guide partition will be installed here
/sources Downloaded package and patches
/boot Boot filesystem

The file structure mentioned above does not conform to the typical Linux root directory file structure. Next, we will create symbolic links according to the instructions in the LFS manual to obtain the following file structure. Please refer to the LFS manual mentioned at the beginning for the specific steps.

1
2
3
4
5
6
7
8
9
10
11
12
$LFS/
|-- bin
|-- boot
|-- etc
|-- home
|-- lib
|-- lib64 -> lib
|-- lost+found
|-- sbin
|-- usr
|-- var
|-- sources

Next, we need to create a user for the LFS system. You can choose any username you prefer. In this case, we will use “lfs” to be consistent with the manual. The command to create the “lfs” user is as follows. Please note that the following operations are applicable to almost all Linux distributions. The primary purpose of creating the “lfs” user is to avoid contaminating the host system:

1
2
3
4
5
6
7
groupadd lfs && useradd -s /bin/bash -g lfs -m -k /dev/null lfs
passwd lfs (setup your own passwd)
# as follows in order to give root directory operation right to lfs user
chown -v lfs $LFS/{usr{,/*},lib,var,etc,bin,sbin,tools}
case $(uname -m) in
x86_64) chown -v lfs $LFS/lib64 ;;
esac

At this point, we can use the command su - lfs to switch to the LFS environment. By doing so, we can create the .bashrc file according to the manual, which will establish environment variables in the Bash terminal environment.

  • Title: Linux From Scratch Compilation Progress(System Preparation)
  • Author: Keith
  • Created at : 2024-03-19 23:02:57
  • Updated at : 2024-03-19 23:09:14
  • Link: https://redefine.ohevan.com/2024/03/19/Linux-From-Scratch-Compilation-Progress-System-Preparation/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments
On this page
Linux From Scratch Compilation Progress(System Preparation)