“This is the 40th day of my participation in the First Challenge 2022. For details: First Challenge 2022”

1. Introduction

If you have done Linux system porting, 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 and kernel to make the root file system.

The root file system is the first file system on which the kernel is mounted at startup. The system boot launcher will load some basic initialization scripts and services into memory to run after the root file system is mounted.

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 startup 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. As the root of the system, the root file system performs some initial configuration operations after the system is started. For example, the system login password and account information are stored in the root file system.

The process of mounting the root file system: first run the uboot boot kernel, after the kernel is started, according to the configured environment variables, find the location of the root file system, mount /dev/xxx, and then execute /linuxrc program, and so on. Switch the root directory and mount the specific root file system. After the root file system is complete, the init process, the first user process, performs various initialization operations on the system.

/dev/xxx is the device node where the root file system is stored. The root file system can be mounted from the SD card, CD-ROM, 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, and /dev

The /bin directory is used to store basic system commands, such as ls.

/etc stores the configuration file of the system. After mounting the root file system, run the configuration file in /etc to initialize the system.

The /lib directory stores the shared library files required for system operation, that is, dynamic library files. The format is xxx.so

The /dev directory is where the device nodes generated by the kernel are stored. The files in this directory are automatically generated by the kernel.

2. Create a root file system

To create a root file system, use the Busybox toolkit.

BusyBox is an integration of more than 300 of the most commonly used Linux commands and tools. BusyBox includes simple tools such as ls, cat, and echo, as well as larger, more complex tools such as grep, find, mount, and Telnet. Some call BusyBox the Swiss Army knife of Linux tools. In short, BusyBox is like a large toolkit, packed with Linux tools and commands, including the Linux shell.

Busybox download: busybox.net/

Busybox is then configured, cross-compiled, and installed to generate the base directory files required by the root file system.

After downloading busybox-1.23.2.tar.bz2, unzip busybox-1.23.2.tar.bz2 to a Linux directory.

Here’s how to do it:

[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 installCopy the code

Once the installation is complete, you can see the generated files in the configured directory.

3. Improve the root file system

The files generated by BusyBox above are just the basic files of the root file system. You will need to make some improvements yourself, 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 / *. / - rdCopy the code

(2). Create a fstab file and complete the /etc directory

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

(3) create initTab

Cp busybox Decompress directory /examples/inittab /tiny4412/rootfs/etc/ [wbyq@wbyq rootfs]$cp.. Busybox/busybox - 1.23.2 / examples/inittab/etc /Copy the code

(4). Modify the inittab file copied above

: : sysinit: / etc/init. D/rcS # set initialized to perform file console: : askfirst: - / bin/sh # need to add the console askfirst said need press enter to enter the system respawn said boot directly into the system : : ctrlaltdel: / sbin/reboot # specify the restart command: : shutdown: / bin/umount - a - r # shutdown command execution when specifiedCopy the code

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 mount -a mkdir /dev/pts mount -t devpts devpts /dev/pts echo /sbin/mdev > /proc/sys/kernel/hotplug mdev -s /bin/hostname  wbyqCopy the code

(6). Create a /etc/profile file

[wbyq@wbyq rootfs]$touch /etc/profile [wbyq@wbyq rootfs]$gedit /etc/profile USER="id-un" LOGNAME=$USER PS1='[\u@\h \W]$ ' PATH=$PATH HOSTNAME='/bin/hostname' export USER LOGNAME PS1 PATH HOSTNAMECopy the code

Parameter Description: PS1 is an environment variable set in command line style.