rootfs root file system is perfect

1. Improve the root file system

In the article Building a root file system with BusyBox, the creation of the root file system is introduced. When the rootfs is built and tested, it prompts that the file "/etc/init.d/rcS" cannot be run, indicating that the rootfs is still not perfect. The following will introduce how to Improve the root file system.

1.1 Create /etc/init.d/rcS file

rcS is a shell script. After the Linux kernel starts, some services need to be started. rcS is a script file that specifies which files to start. Create /etc/init.d/rcS file in rootfs, the command is as follows:

mkdir -p etc/init.d/
cd etc/init.d/
touch rcS

Then enter the following in the rcS file:

1 #!/bin/sh 
2 
3 PATH=/sbin:/bin:/usr/sbin:/usr/bin:$PATH 
4 LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/lib:/usr/lib 
5 export PATH LD_LIBRARY_PATH 
6 
7 mount -a 
8 mkdir /dev/pts 
9 mount -t devpts devpts /dev/pts 
10 
11 echo /sbin/mdev > /proc/sys/kernel/hotplug 
12 mdev -s
  • Line 1, indicating that this is a shell script
  • On line 3, the PATH environment variable holds the directory where the executable file may exist
  • On line 4, the LD_LIBRARY_PATH environment variable holds the directory where the library file is located
  • On line 5, use export to export the above environment variables, which is equivalent to declaring "global variables"
  • Line 7, use the mount command to mount all file systems, these file systems are specified by the file /etc/fstab, so the /etc/fstab file must be created later
  • Lines 8 and 9, create the directory /dev/pts, and then mount devpts to the /dev/pts directory
  • Lines 11 and 12, use mdev to manage hot-plug devices, through these two lines, the kernel can automatically create device nodes in the /dev directory

After creating the rcS file, give it executable permissions:

chmod 777 rcS

1.2 Create /etc/fstab file

The fstab file is to automatically configure the partitions that need to be automatically mounted after Linux is turned on. The format is as follows:

<file system> <mount point> <type> <options> <dump> <pass>
  • file system: the special device to be mounted, it can also be a block device, such as /dev/sda, etc.
  • mount point: The mount point must be an existing directory
  • type: file system type, such as ext2, ext3, proc, romfs, tmpfs, etc.
  • options: mount options, used to set mount parameters, generally use the defaults default options
    – defaults: rw, suid, dev, exec, auto, nouser, and async
    – auto: the system automatically mounts, fstab defaults to this option
    – noauto : Do not automatically mount when booting
    – nouser: only superusers can mount
    – ro: mount with read-only permissions
    – rw: mount with readable and writable permissions
    – user: any user can mount
  • dump: 1 means backup is allowed, 0 means no backup, generally set to 0
  • pass: Disk check setting, 0 means no check. The root directory '/' is set to 1, other partitions cannot be set to 1, and other partitions start from 2. Generally, the root directory is not mounted in fstab, so it is generally set to 0 here

Create the /etc/fstab file in the rootfs root file system:

cd etc/
touch fstab

The contents of the fstab file are as follows:

#<file system> <mount point> <type> <options> <dump> <pass>
proc 		 	/proc 	      proc    defaults  0 		0
sysfs 		 	/sys 	      sysfs   defaults  0 		0
tmpfs 		 	/tmp 	      tmpfs   defaults  0 		0

1.3 Create /etc/inittab file

For details of inittab, please refer to the file examples/inittab under busybox. The init program will read the file /etc/inittab, and inittab consists of several instructions. The structure of each instruction is the same, consisting of 4 segments separated by ":", the format is as follows:

<id>:<runlevels>:<action>:<process>
  • id: The identifier of each command, which cannot be repeated. For the init of busybox, it is used to specify the control tty of the startup process. Generally, the serial port or LCD screen is set as the control tty
  • runlevels: This item is completely useless for busybox, so leave it empty
  • action: action keyword, used to specify the actions that may be used by the process
  • process: specific actions, such as programs, scripts, or commands

Referring to the examples/inittab file of busybox, create the /etc/inittab file in the rootfs root file system:

cd etc
touch inittab

The contents of the inittab file are as follows:

1 #etc/inittab
2 ::sysinit:/etc/init.d/rcS
3 console::askfirst:-/bin/sh
4 ::restart:/sbin/init
5 ::ctrlaltdel:/sbin/reboot
6 ::shutdown:/bin/umount -a -r 
7 ::shutdown:/sbin/swapoff -a
  • Line 2, run the script file /etc/init.d/rcS after the system starts
  • Line 3, use console as a console terminal, which is ttymxc0
  • Line 4, if you restart, run /sbin/init
  • Line 5, press ctrl+alt+del to run /sbin/reboot to restart the system
  • Line 6, execute /bin/umount when shutting down, that is, unmount each file system
  • Line 7, execute /sbin/swapoff when shutting down, that is, turn off the swap partition

After the /etc/inittab file is created, just restart the development board, so far! The files to be created in the root file system have all been completed. Next, we need to conduct other tests on the root file system, such as whether the software we wrote is running normally, whether it supports automatic startup of the software, whether Chinese support is normal, and whether it can be linked, etc.

2. Other functional tests of the root file system

The root file system has been perfected, and then continue to test whether the root file system is easy to use

2.1 Software running test

Write a simple c language program and run it to verify whether the library file can be used

⏩ Create a hello.c in rootfs of ubuntu

#include <stdio.h>

int main(void){
    
    
  while(1){
    
    
    printf("hello world!\r\n"); //打印
    sleep(2); //休眠2秒
  }
}

⏩ Use the cross-compilation chain to compile the program

arm-linux-gnueabihf-gcc hello.c -o hello

⏩ After compilation, an executable file named hello will be generated. You can use filethe command to view the file type and encoding format, as shown in the figure below
insert image description here

It can be seen that hello is a 32-bit LSB executable file, ARM architecture, and dynamically linked

⏩ Restart the development board and run the hello file on the development board. The program prints every 2 seconds and runs normally, indicating that there is no problem with the dynamic library in the root file system. "Ctrl+c" can stop the program
insert image description here
. Interactive window, you can add "&" at runtime, that is ./hello&, let it run in the background
insert image description here

When the program is running in the background, the interactive serial port still prints, but at this time you can press the Enter key to input commands, which does not affect the output of the program. The only effect is that the output of the program will interrupt our input, but You can ignore the interruption and continue to enter commands that can still be executed

For programs running in the background, you can psview the process ID through the command, and then use kill -9 pidthe (process ID) command to close the program running in the background. As shown in the figure below, enter the ps command, you can see that the process ID of the hello program is 92, enter kill -9 92 (don’t worry about being interrupted) and press Enter, you can see that the hello process is closed
insert image description here

2.2 Chinese character test

Create a new "Chinese test" folder in the rootfs directory of Ubuntu, and create a new "test document.txt", enter any Chinese in it, after downloading to the development board, in the command line mode of the development board, use the command to check whether the Chinese catis display correctly
insert image description here

2.3 Boot self-starting test

Generally, after making a good product, it is necessary to automatically start the corresponding software after booting. The principle of realizing booting self-starting is also very simple. When Linux is started, there is a default boot script (the shell file /etc/init.d/rcS) , modify this script and add self-starting related content

########## /etc/init.d/rcS 文件代码 ##########
#!/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/lib:/usr/lib
runlevel=S
umask 022
export PATH LD_LIBRARY_PATH runlevel

mount -a
mkdir /dev/pts
mount -t devpts devpts /dev/pts

echo /sbin/mdev > /proc/sys/kernel/hotplug
mdev -s

#开机自启动,先进入hello所在文件夹再执行
cd /drivers 
./hello &
cd /

After the start-up code is added, restart the development board, and you can see that the hello software has been automatically run when it is turned on:
insert image description here

2.4 External network connection test

The external network here is relative to the internal and external LAN, that is, to test whether the board can access websites such as Baidu and QQ, first use the ping command to test:
insert image description here

When the network connection is normal, these URLs cannot be pinged because of the lack of domain name resolution service, and the IP address of the domain name resolution server needs to be configured. The general domain name can be set as the gateway address of the network where it is located. For example, the gateway of my LAN is 192.168.10.1, or it can be set as the operator’s domain name resolution server address: 114.114.114.114

Create a new file /etc/resolv.conf in rootfs, and enter the following content in it:

nameserver 114.114.114.114
nameserver 192.168.10.1

Modify, save and exit, and ping the Baidu website again, it can be seen that the ping is successful!
insert image description here

So far! The root file system is completely made. The three uboot, kernel, and rootfs together constitute a complete Linux system. This system is a system that can run normally, and Linux driver development can be performed on this system.

Follow the official account and send "busybox" to get busybox related information

Guess you like

Origin blog.csdn.net/Chuangke_Andy/article/details/126740441