Embedded Linux development busybox root file system production

Posted by baseballkid420 on Tue, 01 Mar 2022 17:41:58 +0100

1. Preface

If you have done Linux system transplantation or Linux related development, you should be familiar with the term root file system. In the process of building an embedded development environment, you must do three things: porting bootloader, porting kernel and making root file system.

The root file system is the first file system mounted on the mount when the kernel starts. After the root file system is mounted, the system boot boot program will load some basic initialization scripts and services into memory to run.

When Linux starts, the first thing that must be mounted is the root file system; If the system cannot mount the root file system from the specified device, the system will exit the boot due to an error. After success, other file systems can be mounted automatically or manually. Therefore, different file systems can exist in a system at the same time. The root file system is the root of the system. After the system is started, operations are required to complete some initialization configurations. For example, the boot password and boot account entered by the system login are stored in the root file system.

The process of mounting the root file system: first run uboot to boot the kernel. After the kernel is started, find the location of the root file system according to the configured environment variables, mount / dev/xxx, and then execute the / linuxrc program. Switch the root directory and mount the specific root file system. After the execution of the root file system, execute the init process, which is the first user process, to initialize the system.

The above-mentioned / dev/xxx is the device node where the root file system is stored. The root file system supports mounting from SD card, optical disc, EMMC and NFS network locations/ linuxrc is a file generated in the top-level directory after the root file system is created.

The root file system must have several important directories: / bin, / etc, / lib, / dev

The / bin directory stores the basic commands of the system, such as ls. These commands are placed here.

/Etc stores the configuration file of the system. After the root file system is mounted, the configuration file under / etc will be executed to complete the initialization of the system.

/The lib directory stores the shared library files required for the operation of the system, that is, the dynamic library Format: XXX so

/The dev directory stores the device nodes generated by the kernel, and the files in this directory are automatically generated by the kernel.

2. Root file system creation

Busybox toolkit is needed to make the root file system.

BusyBox is a software that integrates more than 300 of the most commonly used linux commands and tools. BusyBox includes some simple tools, such as ls, cat and echo, as well as some larger and more complex tools, such as grep, find, mount and telnet. Some people call BusyBox the Swiss Army knife in Linux tools. In short, BusyBox is like a big toolbox. It integrates and compresses many tools and commands of Linux, as well as the built-in shell of Linux system.

**Busybox download address:** https://busybox.net/

Next, configure, cross compile and install Busybox to generate the basic directory files required by the root file system.

The version I use here is 1.23.2. After downloading, unzip busybox-1.23.2 tar. Bz2 to the specified directory of Linux system.

Here are the steps:

[wbyq@wbyq ~]$ mkdir ~/work/busybox -p
[wbyq@wbyq ~]$ tar xvf busybox-1.23.2.tar.bz2 -C ~/work/busybox/
[wbyq@wbyq ~]$ cd work/busybox/
[wbyq@wbyq busybox]$ cd busybox-1.23.2/
[wbyq@wbyq busybox-1.23.2]$ make menuconfig

 Busybox Settings  --->   
    Build Options  --->             
         (arm-linux-) Cross Compiler prefix  
    Installation Options ("make install" behavior)  ---> 
         (/home/wbyq/work/rootfs) BusyBox installation prefix   

[wbyq@wbyq busybox-1.23.2]$ make && make install

After installation, you can see the generated files in the configured directory.

3. Improve the root file system

The files generated by busybox above are only the basic files of the root file system. Next, you need to make some improvements, such as modifying the etc configuration, copying the dynamic library, and so on.

(1). Copy dynamic library

[wbyq@wbyq lib]$ cp ~/work/arm-linux-gcc/opt/FriendlyARM/toolschain/4.5.1/arm-none-linux-gnueabi/sys-root/lib/* ./ -rd
[wbyq@wbyq lib]$ cp ~/work/arm-linux-gcc/opt/FriendlyARM/toolschain/4.5.1/arm-none-linux-gnueabi/sys-root/usr/lib/* ./ -rd
[wbyq@wbyq lib]$ sudo cp ~/work/arm-linux-gcc/opt/FriendlyARM/toolschain/4.5.1/arm-none-linux-gnueabi/lib/* ./ -rd

(2). Create fstab file and improve / etc directory

[wbyq@wbyq rootfs]$ cp /etc/fstab etc/
[wbyq@wbyq rootfs]$ cp /etc/passwd etc/
[wbyq@wbyq rootfs]$ cp /etc/group etc/

(3). Create inittab

cp busybox Unzip directory/examples/inittab /tiny4412/rootfs/etc/
[wbyq@wbyq rootfs]$ cp ../busybox/busybox-1.23.2/examples/inittab etc/

(4). Modify the inittab file copied above

::sysinit:/etc/init.d/rcS     #set initialization execution file
console::askfirst:-/bin/sh    #Console askfirst means you need to press enter to enter the system. respawn means you can directly enter the system after starting up
::ctrlaltdel:/sbin/reboot     #Specify restart command
::shutdown:/bin/umount -a -r  #Specifies the command to execute when shutting down

(5). Create etc / init D / RCS file

[wbyq@wbyq rootfs]$ touch etc/init.d/rcS
[wbyq@wbyq rootfs]$ chmod 777 etc/init.d/rcS
[wbyq@wbyq rootfs]$ gedit etc/init.d/rcS
 Write the following code:
mount -a
mkdir /dev/pts                                  
mount -t devpts devpts /dev/pts
echo /sbin/mdev > /proc/sys/kernel/hotplug
mdev -s
/bin/hostname wbyq

(6). Create etc/profile file

[wbyq@wbyq rootfs]$ touch etc/profile
[wbyq@wbyq rootfs]$ gedit etc/profile
 Write the following code:
PS1='[\u@\h \W]\$ '

Parameter interpretation:
PS1 is the environment variable of the command line style setting.

Topics: Linux Operation & Maintenance server