Article directory
Linux system porting: root file system building
There are three key files for the startup of a Linux system
- uboot: Boot the Linux kernel to boot
- Linux Kernel: Implementing Basic Kernel Functions
- root filesystem: kernel-running support
This section learns about the construction of the root file system
1. What is the root file system
The root file (rootFS) system is not like a file system such as FATFS, FAT, EXT4, YAFFS and NTFS. It is more like a folder or directory . There will be many files in the root directory and subdirectories of the root file system. These files It is necessary for Linux to run, such as libraries, commonly used software and commands, device files, configuration files, etc. The first file system mounted after the Linux kernel starts is the root file system, and then the Linux kernel will start from the root file system. Read initialization scripts and other executable files to maintain the normal operation of the Linux kernel
A separate Linux kernel cannot work properly, it must have a root file system
Second, the root file system directory
The root file system mentioned above is a directory. Let's take ubuntu as an example to see what is under the root file system, use cd / to go back to the root directory, and then print the directory
2.1 Subdirectory bin
This directory stores the executable files required by the system, generally some commands, such as ls, mv and other commands
2.2 Subdirectory dev
This directory stores device files. In Linux, everything is a file. For example, serial port 0 is /dev/ttymxc0 in this directory. We operate external devices by operating files.
2.3 Subdirectories etc
Various configuration files are stored in this directory
2.4 Subdirectory lib
The library files necessary for Linux are stored in this directory. Commands and applications use these library files, such as the C language standard library and so on.
2.5 Subdirectory mnt
File mount directory, if you want to mount U disk, SD card, etc., you can temporarily mount it below
2.6 Subdirectory proc
When the Linux system starts, this directory will be used as the mount point of the proc file system
proc is a virtual file system. There is no actual storage device. The files exist temporarily and are generally used to store system operation information files.
2.7 Subdirectory usr
usr is the abbreviation of Unix Software Resource , which is the software resource directory of the Unix operating system, so there are many software stored in the usr directory.
2.8 Subdirectory var
This directory holds some data that can be changed
2.9 Subdirectory sbin
It is used for users to store some executable files, but the files and commands in this directory can only be used by administrators , mainly for user system management
2.10 Subdirectory sys
After the system starts, this directory is used as the mount point of the sysfs file system
sysfs is a special file system similar to the proc file system, sysfs is also a ram-based file system, there is no storage device, and the data exists temporarily
2.11 Subdirectory opt
Optional file and software storage area, the user chooses which files or software to put in this directory
3. BusyBox creates a root file system
BusyBox is a software that integrates a large number of Linux commands and tools, such as ls, mv, ifconfig and other commands BusyBox will provide, through which you can quickly build a file system, the official website address is: https://busybox.net/
I created a busybox folder under ubuntu to store busybox, here version 1.29.0
Unzip the package
tar -vxjf busybox-1.29.0.tar.bz2
3.1 Modify compile options
Modify the top-level makefile and add compile options
3.2 Configure busybox
The configuration of busybox is similar to that of linux and uboot. The configuration has the following steps:
- First configure the default configuration:
make defconfig
- Further open the graphical configuration:
make menuconfig
There are several important configurations that need to be modified:
Select the compiled library for dynamic compilation:
Static compilation does not require library files, but the compiled library will be very large. Dynamic compilation requires library files in the root file system, but the compiled busybox will be much smaller
The configuration interface compilation path is as follows:
-> Settings
-> Build static binary (no shared libs)
Uncheck static compilation:
Add vi editor:
Configuration path:
-> Settings
-> vi-style line editing commands
checked
Undo modutils library:
Configuration path:
-> Linux Module Utilities
-> Simplified modutils
Enable mdev:
The mdev tool is used to automatically create device nodes when the system boots and hotplugs or dynamically loads drivers
Configuration path:
-> Linux System Utilities
-> mdev (16 kb)
start all
Set up save the settings
3.3 Compile busybox
make
After compiling, install busybox to the nfs directory for easy debugging
make install CONFIG_PREFIX=/home/jeck/linux/nfs/rootfs
After the installation is complete, the following content appears in the installation directory:
There are three directories, bin, sbin and usr, and the linuxrc file in the rootfs directory. linuxrc is the init program of the user space.
4. Complete other directories and files
busybox generates three directories and init files, but there are other directories
4.1 lib Add library
Create a lib folder in rootfs. The lib library files are obtained from the cross compiler. Generally, copy the past selectively and tailor it according to the actual situation. For beginners like me, just copy all the past files directly.
cp /usr/local/arm/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc/lib/*so* /home/jeck/linux/nfs/rootfs/lib/ -d
cp /usr/local/arm/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc/lib/*.a /home/jeck/linux/nfs/rootfs/lib/ -d
cp /usr/local/arm/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/lib/*so* /home/jeck/linux/nfs/rootfs/lib/ -d
cp /usr/local/arm/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/lib/*.a /home/jeck/linux/nfs/rootfs/lib/ -d
-d means to copy symbolic links, because some files in the library directory are symbolic links
Copy as follows
4.2 usr/lib Add library
Create a directory named lib under the usr directory of rootfs and copy the following files into it
cp /usr/local/arm/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc/usr/lib/*so* /home/jeck/linux/nfs/rootfs/usr/lib/ -d
cp /usr/local/arm/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc/usr/lib/*.a /home/jeck/linux/nfs/rootfs/usr/lib/ -d
The copy is done as follows:
Look at the size of the copy
Within the emmc size range, no more than
4.3 Create other directories
Create additional run-required folders
mkdir dev proc mnt sys tmp root
4.4 Create the /etc/init.d/rcS file
After the Linux kernel is started, some services need to be started. The rcS script specifies the script files of which files to start. The script content is as follows:
#!/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin:$PATH
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/lib:/usr/lib
export PATH LD_LIBRARY_PATH
mount -a
mkdir /dev/pts
mount -t devpts devpts /dev/pts
echo /sbin/mdev > /proc/sys/kernel/hotplug
mdev -s
- The PATH environment variable holds directories where executable files may exist
- The LD_LIBRARY_PATH environment variable holds the directory where the library files are located
- export to export environment variables and declare
- mount command to mount all filesystems specified by the file /etc/fstab
- Create a directory /dev/pts and mount devpts in the /dev/pts directory
- echo print information
- mdev to manage hot-plug devices, so that the Linux kernel can automatically create device nodes in the /dev directory
After creating the file, give it executable permission
chmod 777 rcS
4.5 Create /etc/fstab file
fstab automatically configures which partitions need to be automatically mounted after Linux boots up. The file format is as follows:
<file system> <mount point> <type> <options> <dump> <pass>
Types of | Function |
---|---|
file system | device to mount |
mount point | mount point |
type | file system type |
options | mount options |
dump | If it is 1, it means backup is allowed, and if it is 0, it is not backed up. |
pass | Disk check setting, 0 means no check, generally set to 0 |
Enter the following in the file
#<file system> <mount point><type><options> <dump> <pass>
proc /procproc defaults 0 0
tmpfs /tmptmpfs defaults 0 0
sysfs /syssysfs defaults 0 0
4.6 Create /etc/inittab file
The init program will read the file /etc/inittab. The inittab consists of several instructions, each of which has the same structure.
<id>:<runlevels>:<action>:<process>
parameter | Function |
---|---|
id | Identifier of each instruction, cannot be repeated |
runlevels | This item is completely useless for busybox, empty |
action | Used to specify actions that the process may use |
process | A specific action, such as a program, script, or command, etc. |
action The action parameters are as follows:
Enter the command:
#etc/inittab
::sysinit:/etc/init.d/rcS
console::askfirst:-/bin/sh
::restart:/sbin/init
::ctrlaltdel:/sbin/reboot
::shutdown:/bin/umount -a -r
::shutdown:/sbin/swapoff -a
Command function:
- Line 2, run the script file /etc/init.d/rcS after the system is started
- Line 3, use the console as the console terminal, which is the ttymxc0 serial port
- Line 4, run /sbin/init on reboot
- Line 5, press the ctrl+alt+del key combination to run /sbin/reboot, it seems that the ctrl+alt+del key combination is used 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, close the swap partition
4.7 Create the /etc/resolv.conf file
Create a new file /etc/resolv.conf in rootfs and enter the following content
nameserver 114.114.114.114
nameserver 210.45.244.1
Configure the IP address of the DNS server to test the network connection
Five, nfs test rootfs
The bootargs environment variable in uboot will set the value of root to guide the system to find rootfs
The format of the nfs mount is as follows:
root=/dev/nfs nfsroot=[<server-ip>:]<root-dir>[,<nfs-options>] ip=<client-ip>:<server-ip>:<gw-ip>:<netmask>:<hostname>:<device>:<autoconf>:<dns0-ip>:<dns1-ip>
- server-ip: server IP address, that is, the IP address of the host where the root file system is stored
- root-dir: The storage path of the root file system, for example, mine is /home/jeck/linux/nfs/rootfs
- nfs-options: NFS optional options, generally not set
- client-ip: client IP address, which is the IP address of our development board. After the Linux kernel starts, this IP address will be used to configure the development board
- gw-ip: gateway address
- netmask: subnet mask, usually 255.255.255.0
- hostname: the name of the client, generally not set, this value can be empty
- device: The name of the device, that is, the name of the network card, generally eth0, eth1, ENET2 of the I.MX6U-ALPHA
development board of Punctual Atomic is eth0, and ENET1 is eth1. If the computer has only one network card, then basically it can only be eth0 - autoconf: automatic configuration, generally not used, so set to off
- dns0-ip: DNS0 server IP address, not used
- dns1-ip: DNS1 server IP address, not used
For my configuration, the root value goes in:
root=/dev/nfs nfsroot=210.45.244.70:/home/jeck/linux/nfs/rootfs,proto=tcp rw
ip=210.45.244.17:210.45.244.70:210.45.244.1:255.255.255.0::eth0:off
After entering uboot to set the startup parameters, mount the root file system from nfs, and test the directory after the startup is successful