Follow★ Star the official account to get information first
As we all know, embedded devices generally have limited storage media and tight resources, which is why a command system for embedded devices called BusyBox has emerged. The characteristic of BusyBox is its powerful functionality while occupying a small storage capacity. Today, I will introduce how to download and compile BusyBox for your embedded device.
Introduction to BusyBox
BusyBox is an open-source, lightweight Unix toolbox that integrates streamlined versions of many standard Unix tools. This tool is designed to run in resource-constrained environments, such as embedded systems or boot disks. BusyBox consolidates multiple standard Unix utilities into a single executable file, including file operations, process management, network configuration, text processing, and more.
The main features of BusyBox include:
- Small Size: Its goal is to keep functionality as compact as possible, saving system resources and allowing the system to run more efficiently.
- Multi-functional: Integrates over 300 common commands, such as file operations, text processing, system management, network tools, etc., covering most common system management functions.
- Configurable: You can choose which features to include based on your needs and remove unnecessary parts, thus meeting the requirements of different systems.
- Portable: It can run on different POSIX systems, whether Linux, BSD, or Solaris, without issues.
- Open Source: It is an open-source project released under the GPL license, allowing everyone to use and modify it freely.
BusyBox is widely used in embedded Linux systems. It helps build a compact and efficient root filesystem, significantly reducing disk and memory usage. It is also commonly used as a startup script or initialization script tool to configure the system and start services.
Principle of BusyBox
BusyBox utilizes the parameters passed to the <span>main</span>
function by the shell. At startup, the shell passes the user-input command and its parameters to the <span>main</span>
function. For example, when you enter <span>ls -l /home/user</span>
in the command line, the shell parses this command line into an array of parameters and passes it to BusyBox’s main function. This array contains the command name (<span>ls</span>
), options (<span>-l</span>
), and file path (<span>/home/user</span>
), etc.
BusyBox checks <span>argv[0]</span>
(the first parameter passed to the <span>main</span>
function) to determine which command the user requested. The command name entered by the user (e.g., <span>ls</span>
, <span>cp</span>
, <span>mv</span>
, etc.) is stored in <span>argv[0]</span>
. Then, BusyBox uses its internal command mapping mechanism to find and call the corresponding function for that command.
For example, when <span>argv[0]</span>
is <span>ls</span>
, the <span>main</span>
function will look up the implementation function corresponding to the command name <span>ls</span>
, which might be <span>ls_main()</span>
, and pass the remaining parameters (like <span>-l</span>
and the path) to that function. Thus, the <span>ls</span>
command will execute its file listing operation.
When executing each specific command, the command’s parameters are further parsed. For example, in the implementation of the <span>ls</span>
command, there will be a piece of code to parse options like <span>-l</span>
. This is usually done by processing the subsequent parameters in the <span>argv</span>
array. The <span>argc</span>
parameter indicates the total number of parameters, and <span>argv</span>
is an array containing the command name and options. Therefore, the code for the <span>ls</span>
command will iterate through the <span>argv</span>
array to parse all options and file paths.
After the command execution is complete, the command handling function returns an integer value based on the operation result. Typically, if the command executes successfully, the return value is <span>0</span>
, otherwise it is a non-zero value. This return value is passed back to the <span>main</span>
function and then returned by BusyBox to the shell to indicate the execution status of the command.
In this way, BusyBox implements the functionality of supporting multiple Unix tool commands within a single executable file. Each command is handled by an independent function and executes specific operations based on parameters. Through this method, BusyBox provides a compact yet efficient multi-command support while maintaining a small executable file size.
Downloading and Compiling BusyBox
The steps to compile BusyBox from source are not complicated. Let me guide you through the process step by step.
1. Prepare the Environment
Before starting, ensure that you have installed some necessary tools on your system:
- GCC Compiler: Used for compiling source code.
- Make Tool: Used for automating the build process.
- Other Necessary Tools: Such as binutils, gzip, tar, etc.
You can install the necessary packages using the following command (for Ubuntu):
sudo apt-get update
sudo apt-get install build-essential gcc make binutils libncurses5-dev
2. Download BusyBox Source Code
Visit the BusyBox official website or download the latest source code directly using the following command:
wget https://busybox.net/downloads/busybox-1.0.tar.bz2
Extract the source package:
tar -xjf busybox-1.0.tar.bz2
cd busybox-1.0
3. Configure BusyBox
Now that we are in the source directory, we need to configure it to select the features and components we want to compile.
Using Default Configuration:
make defconfig
This command will generate a default configuration file suitable for most users.
Custom Configuration: If you want to manually select the commands and options to compile, you can run:
make menuconfig
This command will start a configuration interface based on <span>ncurses</span>
, where you can select the required features, commands, libraries, etc., or enable or disable certain options. In this configuration interface, you can use the up and down arrow keys to select menu items, press Enter to confirm your selection, and press the <span>Esc</span>
key to exit the configuration.
4. Compile BusyBox
After configuration is complete, you can start compiling BusyBox. Run the following command:
make
Executing this command will start the compilation of BusyBox, which may take a few minutes depending on your computer’s performance.
By following these steps, you have successfully compiled BusyBox from source. You can configure and customize BusyBox according to your needs, selecting the required commands and features. BusyBox provides a compact yet feature-rich toolkit, especially suitable for embedded systems or environments that require extreme minimization.
Installing BusyBox on Development Board
As previously discussed, if you want to compile BusyBox on your computer and use the compiled files on an embedded device, you will need cross-compilation tools. We have already covered the introduction to cross-compilation tools, so I will not elaborate further here.
Use the following command to install the compiled results to the specified storage directory:
make install CONFIG_PREFIX=/home/JsmesBin/linux/nfs/rootfs
After compilation, all tools and files of BusyBox will be installed in the <span>rootfs</span>
directory, which contains the <span>bin</span>
, <span>sbin</span>
, and <span>usr</span>
directories, as well as the <span>linuxrc</span>
file. The Linux kernel’s <span>init</span>
process will eventually look for the user-space init program, and once found, it will run this user-space <span>init</span>
program, thus switching to user mode. If BusyBox is set with <span>init=/linuxrc</span>
, then <span>linuxrc</span>
can serve as the user-space init program.
After BusyBox is compiled, the root filesystem is still not usable; it requires some additional files. You need to add library files to <span>rootfs/lib</span>
: since the above BusyBox uses dynamically linked libraries, you also need to add dynamic libraries to the root filesystem.
First, create a folder named <span>lib</span>
in <span>rootfs</span>
. The library files are obtained from the cross-compiler, which I have stored in the <span>/usr/local/arm/</span>
directory. Enter the <span>libc/lib</span>
directory of the cross-compiler, where there are many <span>.so</span>
and <span>.a</span>
files, which are the library files. Copy all <span>.so</span>
and <span>.a</span>
files from this directory to the <span>rootfs/lib</span>
directory.
At this point, all library files for the root filesystem have been added, and you can proceed to create the image and filesystem as usual to include BusyBox in your image. Of course, you can also use the <span>NFS</span>
mounting method to test the created root filesystem <span>rootfs</span>
.
Recommended Reading
01 |Join the Embedded Group |
02 |Embedded Resource Acquisition |
03 |Detailed Explanation of STM32 Interrupt Priorities |
04 |New Approach to Downloading Programs on STM32 – Using Serial Port to Download Programs |