1_5.4.4_Root file system_Building the root file system_Building the root file system_P

Publisher:温柔浪漫Latest update time:2021-08-21 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Create a root file system based on the following five items:


/dev/console,/dev/null;

init ⇒ busybox;

/etc/inittab;

The program specified in the configuration file;

C library.

/dev/console,/dev/null

First, create two devices: /dev/console and /dev/null. Let's take a look at the descriptions of these two devices in the PC. (All devices exist in the form of files in the /dev directory, called device files)


The C in crw indicates a character device, 5 is the major device number, and 1 is the minor device number. (In an embedded system, a character device or block device has a major device number and a minor device number, which are collectively referred to as the device number. The major device number is used to represent a specific driver, and the minor device number is used to represent each device of the driver. For example, an embedded system has two LED lights that need to be turned on or off independently, so you can write a character device driver for the LED lights, register its major device number as 10, and the minor device numbers are 1 and 2, representing two LED lights)

insert image description here

So, we first create a dev directory, and then create console and null in the dev directory.


Use the mknod command to create a device file. c means that a character device is created (b means a block device), then the major device number is 5, the minor device number is 1, and null is similar.

insert image description here

After creation, use ll to view it.

insert image description here

init ⇒ busybox

The second item is the link from init to busybox. Our init = /linuxrc, so it is the link from linuxrc to busybox. This item has been done.

insert image description here/etc/inittab

The third item is to construct an inittab. If we do not construct our own inittab file, the default configuration will be used. The following figure is the default configuration summarized earlier.

insert image description here

We don't need so many, just one ::askfirst:-/bin/sh is enough.


Create an etc directory and create an inittab file in this directory. Note that the terminal needs to be set to console, and the standard input, standard output, and standard error should all be located in the console.


The final result is as follows. Use the cat command to view the file contents.

insert image description here

In this way, the inittab file is configured. This is the simplest inittab file. It only executes one program with the execution timing of ASKFIRST - /bin/sh. The standard input, standard output, and standard error of this program are all controlled by /dev/console.


Programs specified in the configuration file

Fourth, the program specified in the configuration file. Since we only want to make a minimal root file system and not execute any application programs, we will not configure it here and will improve it later.


C Library

Fifth, C library.


Check the application manual, which tells how to install the C library.

insert image description here

Just copy all the .so files. -d means that if the file is a link file, then it will also be a link file when copied, and the dynamically linked files will not be copied, so it will not be very large. (.a means static library, we don't need static library)

insert image description here

Create a lib directory under 046, and then copy all the .so files to it. Remember not to forget -d.

insert image description here
insert image description here

In this way, we have made a minimal root file system. It should be noted that this root file system has no applications.


Burn the root file system

So, how do we burn this root file system to our development board?


Answer: You need to make an image file. Check the Embedded Linux Application Development Manual. 17.4.4 and 17.4.5 respectively describe how to make the yaffs and yaffs2 root file systems. Yaffs is used for small page (512 bytes per page) Flash. Our NAND flash has 2048 bytes per page, so we need to use the yaffs2 root file system that supports large pages.


The tool used to compile the image file is called mkyaffs2image, and the format is: mkyaffs2image directory name image file name.

insert image description here

Execute the following compilation instructions to compile an image file 046_first_fs.yaffs2, send this file to Windows, and then burn the development board.

insert image description here

Select y on the menu page in uboot and use the DNW burning tool to burn the image file.

insert image description here

After the flashing is complete, an error occurs when loading the root file system. The error message is as follows.

insert image description here

I checked the init_main function and found that the error occurred when matching the name. Action was lowercase, but it was uppercase in inittab, which caused the match to fail.

insert image description here
insert image description here

Change ASKFIRST to lowercase, recompile the image file and burn it, and it will start normally.

insert image description here
insert image description here

In this way, my first root file system was successfully burned and run.


Complete the root file system

The compiled root file system does not have any functions. For example, if we want to use the ps command, we will find that this command is not supported.

insert image description here

We need to improve it further, how do we improve it?


First, use mkdir proc to create a proc folder. Although a proc folder is created, the ps command is still invalid at this time, and a virtual root file system must be mounted.

insert image description here

So, what to do?


Answer: In the kernel, the information about which applications are currently running is stored in a virtual root file system called proc in the kernel. You can implement the ps command by mounting the proc directory you just created to this virtual root file system.


Use mount -t proc none /proc to mount, and then the ps command can be used normally.

insert image description here
insert image description here

At this time, enter the proc directory and you can see that there are many files in it. This is because proc has been mounted to the virtual root file system provided by the kernel.

insert image description here

If you do not want to mount manually, you can set it to mount automatically in the /etc/inittab configuration file.


Add a line to the inittab file, ::sysinit:etc/init.d/rcS, then create an init.d directory under the etc path and create an rcS file in this directory, which is a script file.

insert image description here

Enter the mount command in the rcS file and add an executable attribute to it. Then each time you run it, the proc will be automatically mounted during initialization.

insert image description here
insert image description here

In addition to the above method, another method is to use the mount -a command, which will mount all file systems defined in /etc/fstab.

insert image description here

To use this method, first modify the rcS script, comment out mount -t proc none /proc, and add mount -a.

insert image description here

Then, you need to add an fstab file in the etc directory, and add proc /proc and so on in this file in sequence, with each item separated by a space.


#Lines are format instructions:


device, the device to be mounted;

mount-point, mount point;

type, file system type;

options, hook parameters, separated by commas;

insert image description here

Both methods can achieve automatic mounting.

insert image description here

After loading the new root file system, you can directly use the ps command to view which files are mounted by viewing the /proc/mounts file.

insert image description here

Continue to improve.


There are only two things in the dev directory now. The files in the dev directory correspond to devices and drivers. If we have hundreds of drivers, it will be troublesome to create a dev file for each driver separately. There is a udev mechanism that can automatically create device nodes (/dev/device-node) in the dev directory.

insert image description here

In embedded Linux systems, there is a simplified version of udev called mdev. So how to use this mdev?


There is a mdev.txt in the docs directory, which explains how to use it.

insert image description here

It can be seen that the first and fourth steps are to mount /sys and /dev to sysfs and tmpfs respectively, which are also virtual root file systems.

insert image description here

Mount sys and dev in fstab.

insert image description here

Use rcS to automatically perform the remaining operations. The modified rcS file is as follows.

insert image description here

/proc/sys/kernel/hotplug, hot plug, when the dynamic loading device is inserted into a hot plug device such as a USB flash drive, the kernel calls this hotplug, hotplug points to mdev, so that the kernel will automatically create the corresponding device node.


mdev -s creates the device nodes that the kernel already has.


For detailed instructions, please refer to this article (https://blog.csdn.net/qq_33160790/article/details/79266306)


Recompile the yaffs2 root file system, burn and restart, you can see that there are many more devices in the dev directory.

insert image description here

Many file systems are mounted.

insert image description here

In this way, our minimal root file system is more complete.


If we want to use another root file system format, such as jffs2, this format is generally used on NOR Flash, of course it can also be used on Nand Flash. (I didn't find the zlib compression package, and this file system is not the focus at present. I will talk about the study of jffs2 later)


Mounting the root file system using NFS

Now we have to re-burn the root file system every time we change it, so is there any way to avoid burning it?


Answer: Use NFS to mount the root file system, place the root file system on the server, and when the kernel starts, directly use the root file system on the server as the root file system of the device.


If you want to mount the root file system using NFS, the development board cannot be directly connected to the computer. It needs to be connected to a router or switch, and then the computer is also connected to this router or switch.

insert image description here

Connect the development board to the router, and the computer to the router wirelessly. The development board still does not respond when the network cable is plugged in. After setting the IP address, it is displayed as follows. At this time, pinging the computer can be successful.

insert image description here
insert image description here

Mounting NFS requires two conditions:


The server allows that directory to be mounted by others;

The development board goes to mount that directory.

Set the server to allow mounting

Well, how does the server allow that directory to be mounted by others?


Answer: Through the NFS service, modify the /etc/exports file and add the directory to be mounted to the file.

insert image description here
insert image description here

Then restart the NFS service. The instructions and results are shown in the figure below.

insert image description here

Generally speaking, after doing this, the directory can be mounted by others, but we need to experiment on the server first, and try mounting it to the /mnt directory of the server.


Use the mount command to mount the server. If the mount is successful, it means the server can be mounted.

insert image description here
insert image description here

Set up the development board to mount the server

[1] [2]
Reference address:1_5.4.4_Root file system_Building the root file system_Building the root file system_P

Previous article:1_5.4.3_Root file system_Building the root file system busybox_P
Next article:1_5.5.1_Character Device Driver Concept Introduction_P

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号