“Embedded” Make your own rootfs root file system

Use busybox to create a rootfs root file system that can run on your own board

In the embedded development process, every time we get a new board, the first three things we need to do are to burn the uboot kernel boot program, burn the linux kernel, and burn the rootfs root file system.
Let them run on the board to facilitate subsequent development.
I will take you through making a rootfs root file system from scratch.

1. Obtain busybox source files

We can download the latest busybox source files from the official website of busybox. Download it to our ubuntu host computer. It is best to find our tftp server directory to facilitate subsequent burning.
Unzip the busybox source files in this directory

tar -xvf busybox-1.36.1.tar.bz2

Don’t worry after getting the busybox source files. First check whether your tools are ready.
That is a cross compiler. The cross-compiler directly determines the compatibility of your generated rootfs root file system with your Linux kernel.
Prepare the compiler you used to compile the linux kernel at that time.
What I use here is arm-cortex_a9-linux-guneabi-gcc

2. Make preparations

There are three main things to do in preparation. First, make clean to clear the previous configuration (you don’t need to clean it up for the first time)
Then modify the Makefile, specify the architecture, and change the content of ARCH?= to arm, as shown in the figure below
(Depending on your development board architecture) I am using an arm architecture development board.
Add your compiler name after CROSS_COMPILE = and specify the compiler to use, as shown in the figure below.
(I have added the compiler path to the environment variable here, so it is the compiler name used. If you have not added the compiler path to the environment variable, please add it first, or you can use the absolute path of the compiler here. )


Save and exit after setting.

Next, use make defconfig to initialize our busybox. Here is an explanation of this command.

Next we need to use the make menuconfig command to configure the contents.
Note: If this command cannot be executed, please install the relevant dependencies first.
Execute the following commands to install the necessary components. Run make menuconfig again

sudo apt-get install libncurses5-dev


After opening, you will enter an interface like this.
Some functions can be configured. We scroll down to find the linux module utilities option and enter.

After entering, the following screen will appear. We will press the n key to deselect the simplified modutils option.
Press the y key to select all the command options below.
Save and exit.


This concludes our preparations.

3. Compilation

After the preparation work is completed, you can use the make command to compile the busybox source code.
NOTE: Errors may be encountered.
If an error is found, look for the wrong file name.
Go to the menu generated by make menuconfig and look for this option, find it and press n to remove it.
This way the file will not be compiled during compilation.

4. Installation

If the compilation is successful, execute make install to install the generated binary. That is, all the generated binary files are copied to a directory.
The default installation directory of busybox is _install, which is in the root directory of busybox.
We enter the _install directory and we can see that busybox has helped us generate three necessary directories.
They are bin sbin usr directory.
These three directories are related to user commands. For example, we commonly use the ls command.
In addition to these three necessary directories, there are also dev, proc, sys, lib and etc directories.
The dev, proc, and sys directories are created by the kernel at startup. Only the lib and etc directories need to be configured by ourselves.
(Other non-essential directories can be created by yourself)

5. Build lib directory

Building the lib directory means copying the necessary dynamic library files and dynamic library linker. During the development stage, there is no need to consider the huge space occupied by dynamic libraries, because rootfs is generally mounted through the nfs service and is placed in the Ubuntu system of the host computer.
Use the compiler’s readelf -d
For example, mine is arm-cortex_a9-linux-gnueabi-readelf -d to view the necessary library files for running the corresponding application. (Execute this command on the host computer) For example, as shown in the figure below:

After getting the necessary library file name, go to the root directory of our cross-compiler. Use the find command to find them

Copy all dynamic libraries to the lib directory of our newly generated root file system.
Note: Copy all files with so, including dynamic libraries and linkers. Otherwise, the software still cannot run.
After the copy is completed, the effect will be as shown below:

The size of the dynamic library is still very large.
To save space. Use arm-cortex_a9-linux-gnueabi-strip to reduce the file size.
Note: This method will delete the debug information of the program. Do not strip the software during the development phase, but must strip it during the release phase.
At this point, the lib directory construction is completed.

6. Build etc directory

The etc directory mainly contains the following three files and one directory for configuring the startup script.



First use the vi editor to create the inittab file and add the following content.

This is to tell the Linux kernel to first go to the etc/init.d directory to execute the rcS script file during startup.
After executing the script file, go to the bin directory to execute the sh executable file. to start our shell terminal
Then we create the init.d directory and create the rcS script file in it.
Add the following command

These commands are related to the driver and telnet service. Just write directly.
After saving and exiting, execute chmod 777 rcS to give it execution permission.
Go back to the etc directory and create the fstab file.
The fstab file is related to the driver.
Add the following commands:

Save and exit.
So at this point, congratulations, you have made your own rootfs root file system.

Next, connect the development board and use nfs mounting method to see if it can be mounted.

Or make an image file and burn it to the lower computer. (You can read my other article)

It is not easy to make. Please support the author. Your support for the author will help create a good learning and communication environment.