Analyze Linux system design ideas together-04 build root file system (2)

In an environment where learning materials are flying all over the sky, knowledge becomes very fragmented, and there is not much systematic knowledge. This leads many people to study hard every day to move themselves, but in the end it has little effect or even give up learning. My mission is to filter out a large amount of spam, to systemize knowledge, to reach the essence of the problem in a short and quick way, and to free everyone from the pain of finding a needle in a haystack.

1 Why perfect

The establishment of many concepts and technological innovations are ultimately aimed at solving certain problems or types of problems. Therefore, the problem is often the root of many behaviors.

So why should we improve the minimum root file system established in the previous article?

Because ps, reboot, df, top and other commands are not easy to use, the following error will be reported.

# arm9嵌入式设备上执行下述指令
/ # ps
  PID  Uid        VSZ Stat Command
ps: can't open '/proc': No such file or directory
/ # reboot
reboot: can't open '/proc': No such file or directory

2 What is mounting

The concept of mount is quite abstract. Even if you use the mount command again, if you don't carefully consider this concept, you can only float on the surface. Mount is an action, like a matchmaking, and the matchmaker does the same thing. Who are the two ends of the bridge? One end is the root file system, and the other end is another file system.

At this point, we must explain what the root file system is (we skipped this concept in the previous article). Root file system is not fixed to a certain type of file system, but it shows that the use - as the root file . Speaking to relate to root tree , file system file is organized in a tree topology. System startup must first mount a file system as the root file system, the latter are then mounted file system leaves the file system , and then a long root long before the leaves is normal, right?

So what kind of file system can or be qualified as the root file system to be mounted first? If the file system has init programs, /etc/inittab, applications registered in /etc/inittab (such as -/bin/sh), c language dynamic libraries, /dev/console & /dev/null these necessary modules (See the analysis in the previous article for details), the file system can be used as the root file system to be mounted first by the kernel. For example, yaffs, yaffs2, jffs2, and nfs file systems can be used as root file systems as long as they include the programs listed above.

Let's go back to mounting and give an example that everyone understands well. For example, you have a lot of small appliances that need to be powered on, but there is only one socket on the wall at home, so you found several sockets (such as file systems), and the brands of sockets are different. What are you going to do next? Pick a high-power socket (like the root file system) and plug it into the wall jack (the action of plugging is mount). Then plug other plugs into this high-power plug (like mounting other file systems). Finally, you can easily use your small household appliances (you can easily access files~).

Do you have a new understanding of mount? Finally, remember: mount is mapped .


3 Improve the proc directory

3.1 Direct mount method

We found that the ps and reboot commands are caused by the lack of the /proc directory. The meaning of the /proc directory is to mount a proc file system. Proc is a virtual file system that stores a series of special files of the kernel's running state. These virtual files are actually generated by the kernel and stored in RAM, and the device will disappear when power is off.

Next, use the mount command to directly mount the proc file system.

Add the contents of the root file system on the basis of the previous article. Create the /proc directory, add the /etc/init.d/rcS file and modify the /etc/inittab file.

  1. Create /proc directory
# 在Ubuntu系统中操作
albert@ubuntu:/work/nfsroot/minifs $ ls
bin  dev  etc  lib  linuxrc  sbin  usr
albert@ubuntu:/work/nfsroot/minifs $ mkdir proc #创建proc目录
albert@ubuntu:/work/nfsroot/minifs $ ls
bin  dev  etc  lib  linuxrc  proc  sbin  usr
  1. Edit the /etc/inittab file
# 在Ubuntu系统中操作
# 在inittab中添加Linux内核启动配置脚本文件(/etc/init.d/rcS文件)
::sysinit:/etc/init.d/rcS

Tips: The /etc/init.d/rcS file is the kernel startup script file. Any shell commands you want to operate during the system startup phase can be added to this file. As for the full spelling of rcS, I personally understand that it is runtime configuration Script. If there are any errata, please leave a comment, thank you~

  1. Create and edit the /etc/init.d/rcS file
# 在Ubuntu系统中操作
# 创建(根文件系统安装目录)/etc/init.d/rcS文件
albert@ubuntu:/work/nfsroot/minifs/etc $ mkdir init.d
albert@ubuntu:/work/nfsroot/minifs/etc $ ls
init.d  inittab
albert@ubuntu:/work/nfsroot/minifs/etc $ cd init.d
albert@ubuntu:/work/nfsroot/minifs/etc/init.d $ touch rcS
albert@ubuntu:/work/nfsroot/minifs/etc/init.d $ ls -al
total 12
drwxrwxr-x 2 albert albert 4096 Oct 31 16:21 .
drwxrwxr-x 3 albert albert 4096 Oct 31 16:17 ..
-rw-rw-r-- 1 albert albert   25 Oct 31 16:21 rcS
albert@ubuntu:/work/nfsroot/minifs/etc/init.d $ chmod +x rcS #一定要注意添加可执行权限
albert@ubuntu:/work/nfsroot/minifs/etc/init.d $ ls -al
total 12
drwxrwxr-x 2 albert albert 4096 Oct 31 16:21 .
drwxrwxr-x 3 albert albert 4096 Oct 31 16:17 ..
-rwxrwxr-x 1 albert albert   25 Oct 31 16:21 rcS
albert@ubuntu:/work/nfsroot/minifs/etc/init.d $ 
  # 在Ubuntu系统中操作
  # 编辑(根文件系统安装目录)/etc/init.d/rcS文件内容
  # mount -t type dev dir
  mount -t proc none /proc #将proc文件系统映射到/proc目录

Tips: Pay attention to distinguish the path in Ubuntu and the path on the embedded device according to the context. Don't get confused. The creation of the /etc/init.d/rcS file mentioned here is the path on the embedded device. Don't damage the system of Ubuntu itself.

3.2 How to use fstab

3.2 and 3.1 are in a parallel relationship. Both methods can complete the mounting of the proc file system. Choose one. The fstab method is recommended. Because the mount command in rcS file if you are using more will look messy, too much code is repeated, this time you need to use a table-driven / data-driven ideas to solve this problem. fstab is the product of this idea. fstab is used to manage the mounting parameters of all file system, and only mount -a is needed in the rcS file.

Next, demonstrate this optimization process.

  1. Edit the contents of the /etc/init.d/rcS file
  # 在Ubuntu系统中操作
  # 编辑(根文件系统安装目录)/etc/init.d/rcS文件内容
  # mount -t type dev dir
  #mount -t proc none /proc #将proc文件系统映射到/proc目录
  mount -a	#将上一行代码修改为本行
  1. Create /etc/fstab file
# 在Ubuntu系统中操作
# 创建(根文件系统安装目录)/etc/fstab文件
albert@ubuntu:/work/nfsroot/minifs $ cd etc/
albert@ubuntu:/work/nfsroot/minifs/etc $ touch fstab
albert@ubuntu:/work/nfsroot/minifs/etc $ ls
fstab  init.d  inittab
  1. Edit the contents of the /etc/fstab file
  # 在Ubuntu系统中操作
  # 编辑(根文件系统安装目录)/etc/fstab文件
  # device    mount-point    fs-type    options  dump  fsck  order    
    none      /proc          proc       defaults 0     0     

Here is an explanation of the header:

entry meaning Description
device The device to be mounted Because all objects in Linux are files, all hardware devices are mapped into a virtual file. For example, the field for block devices may be /dev/mtdblock3; the format of this field for nfs is <host>:<dir>; but for file systems that are not related to the device, such as proc, this field can be filled in at will, generally it is filled in as file system Type or none.
mount-point Mount point Is the path path.
fs-type File system type There are roughly the following types of file systems: yaffs, yaffs2, jffs2, ext2, ext3, nfs, proc, tmpfs, sysfs, etc.

4 Improve the dev directory

Why should we improve the dev directory? One reason is that there are many devices, and manual creation is cumbersome; the other reason is to support hot swapping.

We next improve the dev directory using mdev (a simplified version of udev) mechanism. mdev is a program that scans all the class device directories in /sys/class and /sys/block when running. If there is a file named dev in the directory, and the file contains the device number, mdev will pass this information A device node is automatically created under /dev for this device.

The steps to use mdev are as follows:

  1. Mount the sysfs file system to the /sys directory. The mdev command cannot be run without this operation.
  2. Mount the tmpfs file system to the /dev directory.
  3. Create the /dev/pts directory and mount the devpts file system to the /dev/pts directory. Note that the creation of the /dev/pts directory must be after step 2.
  4. Add the mdev program call to the /proc/sys/kernel/hotplug file: echo /sbin/mdev> /proc/sys/kernel/hotplug. This step is done to ensure that the mdev command is automatically run when hot plugging, so as to automatically create and delete device node files.
  5. Scan once when the device starts: mdev -s. Ensure that all currently scanned device node files are created when the device starts.

Tips:

1. tmpfs is a virtual memory file system, which can use RAM or swap partitions. The advantage of mounting /dev to tmpfs is that the device node files can be saved from the flash, which improves the running speed.

2. pts is a remote virtual terminal. devpts is the remote virtual terminal file device. You can understand the basic situation of the current remote virtual terminal through /dev/pts. By operating the corresponding device files, you can achieve the purpose of operating the hardware. The so-called virtual kernel file device directly interacts with the kernel. The kernel is in the memory, so it is called virtual, because this device file does not represent the real hardware, but a virtual device that exists in the memory. By accessing such files, you can achieve the purpose of communicating with the kernel (read/write).


We do it once.

  1. Create /sys directory
# 在Ubuntu系统中操作
albert@ubuntu:/work/nfsroot/minifs $ mkdir sys
  1. Edit the /etc/fstab file
  # 在Ubuntu系统中操作
  # 编辑(根文件系统安装目录)/etc/fstab文件
  # device    mount-point    fs-type    options  dump  fsck  order    
    none      /proc          proc       defaults 0     0
    none      /sys           sysfs      defaults 0     0
    none      /dev           tmpfs      defaults 0     0
  1. Edit the /etc/init.d/rcS file
  # 在Ubuntu系统中操作
  # 编辑(根文件系统安装目录)/etc/init.d/rcS文件
  mount -a
  mkdir /dev/pts
  mount -t devpts none /dev/pts
  echo /sbin/mdev > /proc/sys/kernel/hotplug
  mdev -s

5 Make a yaffs2 file system image and burn it

See the previous part for the production process. Burning the new root file system image, the effect of running is as follows:
Insert picture description here


Congratulations, you have continued to finish reading a blog, and you have improved a little bit! If it feels good, just like it and let’s go. Your likes and attention will be the motivation for my continuous output~~

Guess you like

Origin blog.csdn.net/weixin_44873133/article/details/109409831