Home > System Tutorial > LINUX > body text

Secrets of the Linux root file system

WBOY
Release: 2024-02-15 13:42:05
forward
1242 people have browsed it

Linux is an open source, portable, customizable operating system that is widely used in various fields, such as servers, desktops, embedded devices, etc. The core of Linux is the kernel, which is responsible for managing hardware resources and providing basic services. However, the kernel is not an independent entity and requires a file system to store and access various data and programs. A file system is a method of organizing and managing files. It defines the file's name, location, attributes, permissions and other information. In Linux, there are many different types of file systems, such as ext4, xfs, btrfs, etc., each of which has its own characteristics and advantages. However, among all file systems, there is a special file system, which is the foundation and core of the Linux system, which is the root file system (rootfs). So, what is the root file system? What does it do? How is it created and managed? This article will answer the questions from the following aspects:

Secrets of the Linux root file system

1. Root file system

A file system is a directory structure included in a disk (including optical disks, floppy disks, flash disks and other storage devices) or partitions; an applicable disk device can contain one or more file systems; if you want to enter a file system, the first thing you have to do is to mount the file system; in order to mount the file system, you must specify a mount point.

Note: For our application development, when purchasing a development board, the manufacturer will provide a ready-made root file system and BootLoader. If necessary, we can change the commands without making a new root file from scratch. system. However, the production process here can give us a deeper understanding of the Linux file system.

2. Main root file system

* In Linux, rootfs is essential. PC mainly implements ramdisk and directly mounts the root file system on HD (Harddisk, hard disk); embedded systems generally do not boot from HD, but from Flash. The simplest method is to load rootfs to the RAMDisk of RAM. The slightly more complicated one is to read Cramfs directly from Flash, and the more complicated one is to partition on Flash and build a file system such as JFFS2.

* RAMDisk compresses the prepared rootfs and writes it to Flash. During startup, the Bootloader loads it into RAM, decompresses it, and then mounts it to /. This method is simple to operate, but the file system in RAM is not compressed, so it requires RAM, a scarce resource in many embedded systems.

ramdisk uses memory space to simulate hard disk partitions. Ramdisk usually uses disk file system compression and is stored in flash. During system initialization, it is decompressed to SDRAM and mounted to the root file system. In Linux systems, ramdisk has There are two types. One is that it can be formatted and loaded. It is already supported in Linux kernel 2.0/2.2. Its disadvantage is that the size is fixed; the other is only supported by the 2.4 kernel and is implemented through ramfs. It cannot be formatted. However, it is easy to use and its size increases or decreases according to the required space. It is a commonly used ramdisk technology in Linux at present.

* initrd is the format of RAMDisk. Before kernel 2.4, it was image-initrd. Kernel 2.5 introduced cpio-initrd, which greatly simplified the Linux startup process and conformed to the basic philosophy of Linux: Keep it simple, stupid (KISS) . However, cpio-initrd, as a new format, has not been extensively tested. Image-initrd is mainly used in embedded Linux.

* Cramfs is a very simple file system written by Linus. It has good compression and can also be run directly from Flash without loading into RAM, thus saving RAM. However, Cramfs is read-only, which is inconvenient for directories that need to be modified at runtime (such as /etc, /var, /tmp). Therefore, these directories are generally made into writable fs such as ramfs.

* SquashFS is an enhancement to Cramfs. Breaking through some limitations of Cramfs, it also has advantages in terms of Flash and RAM usage. However, according to the developers, it may not be as good as Cramfs in terms of performance. This is also a new approach that will require more testing before adoption in embedded systems.

3.Ramdisk production

The method of making RAMDisk is as follows:
(1) In the Linux operating system environment of the PC, generate a file that can be virtualized into a block device. Assume that the file name is init.img.

$ dd if=/dev/zero of=init.img bs=4096 count=1024
Copy after login

bs*count is the block device size (unit: bytes). After generating the init.img file, the file must be formatted.

$ mke2fs –m0 –F init.img
Copy after login

(2) Create a new folder ram and attach init.img to the ram directory.

$ mkdir ram
$ mount init.img ram/ -o loop
Copy after login

这时,读写ram目录,等效于读写init.img文件。用户可以将根文系统所需的文件写入到ram目录中。往ram目录写完文件以后,还需要使用umount ram命令卸载init.img,才能将已写入的文件保存到init.img中。

(3)添加完毕,需要umount ram

4.Cramfs制作

CramFS(Compressed Rom File System)是Linux Torvalds在Transmeta任职时,所参与开发的文件系统。它是针对Linux内核2.4之后的版本所设计的一种新型只读文件系统,采用了zlib 压缩,压缩比一般可以达到1:2,但仍可以作到高效的随机读取,Linux系统中,通常把不需要经常修改的目录压缩存放,并在系统引导的时候再将压缩文件 解开。因为Cramfs不会影响系统的读取文件的速度,而且是一个高度压缩的文件系统。因此非常广泛应用于嵌入式系统中。

在嵌入式的 环境之下,内存和外存资源都需要节约使用。如果使用RAMDISK方式来使用文件系统,那么在系统运行之后,首先要把Flash上的映像文件解压缩到内存 中,构造起RAMDISK环境,才可以开始运行程序。但是它也有很致命的弱点。在正常情况下,同样的代码不仅在Flash中占据了空间(以压缩后的形式存 在),而且还在内存中占用了更大的空间(以解压缩之后的形式存在),这违背了嵌入式环境下尽量节省资源的要求。

使用CramFS文件 系统就是一种解决这个问题的方式。CramFS是一个压缩格式的文件系统,它并不需要一次性地将文件系统中的所有内容都解压缩到内存之中,而只是在系统需 要访问某个位置的数据的时候,马上计算出该数据在CramFS中的位置,将它实时地解压缩到内存之中,然后通过对内存的访问来获取文件系统中需要读取的数 据。CramFS中的解压缩以及解压缩之后的内存中数据存放位置都是由CramFS文件系统本身进行维护的,用户并不需要了解具体的实现过程,因此这种方 式增强了透明度,对开发人员来说,既方便,又节省了存储空间。

但是Cramfs也有其缺点:延迟、小于16MB、不支持写,只支持PAGE_CREATE_SIZE=4096的内核。

制作的命令:(root文件夹中存放着文件系统)

#mkcramfs root cramfs.img
Copy after login

5.其他根文件系统的制作

制作yaffs文件系统: mkyaffsimage myroots myroots.img
制作squashfs: mksquashfs $(FS_DIR) $(FS_NAME) -noappend -be -lzma -no-fragments –noI
Copy after login

6.BusyBox编译制作

建立目标板空根目录文件夹及根目录下的文件夹:

[root@190 friendly-arm]# mkdir myroots
[root@190 friendly-arm]#pwd
/friendly-arm/myroots
[root@190 friendly-arm]#cd myroots
[root@190 myroots]# mkdir bin sbin usr lib dev mnt opt root etc home proc tmp var
[root@190 myroots]# mkdir etc/init.d
Copy after login

进入etc/init.d目录下,建立一个脚本文件,并命名为rcS,用gedit打开,添加如下内容:

#! /bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:
runlevel=S
prevlevel=N
umask 022
export PATH runlevel prevlevel
#
# Trap CTRL-C &c only in this shell so we can interrupt subprocesses.
#
trap ":" INT QUIT TSTP
Copy after login

创建 dev 中的节点
如果使用“linux 2.6.xx”的内核,应该事先创建节点“console”、“null”。否则在系统启动时内容会提示以下错误:

Warning: unable to open an initial console.
Kernel panic - not syncing: Attempted to kill init!
Copy after login

创建节点的命令如下:

# mknod dev/console c 5 1
# mknod dev/null c 1 3 移植Busybox:
Copy after login

进入到压缩文件存放的目录下,并解压。然后进入解压后的busybox目录文件夹,随后配置busybox 。

[root@190 busybox-1.2.0]# make menuconfig
Copy after login

由于每个版本的配置选项组织有所不同。不管怎样,我们注意以下选项就行了:

1) Support for devfs
2) Build BusyBox as a static binary ( no shared libs ) //将busybox编译成静态链接
3) Do you want to build busybox with a Cross Compile?
(/usr/local/arm/3.3.2/bin/arm-linux-) Cross Compile prefix //指定交叉编译器
4) init
5) Support reading an inittab file //支持init读取/etc/inittab 配置文件
6) (X) ash选中ash //建立的rcS脚本才能执行
7)ash
8)cp cat ls mkdir mv //可执行命令工具的选择,自己看着办吧,需要用到的就选上
9) mount
10) umount
11) Support loopback mounts
12) Support for the old /etc/mtab file
13) insmod
14) Support version 2.2.x to 2.4.x Linux kernels
15) Support version 2.6.x Linux kernels
16) vi
17)don’t use user
Copy after login

以上内容必须选上,其他可按默认值;如果要支持其他功能,如网络支持等,可按需选择,英语不是很烂的话,这些都没有问题。
配置好之后,保存退出。然后对其编译和安装到刚才建立的根文件系统目录下:

[root@190 busybox-1.2.0] make TARGET_ARCH=arm CROSS=arm-linux- \ PREFIX=/friendly-arm/myroots/ all install
Copy after login

安装好之后,将相应的二进制文件拷贝到根文件系统相应的目录下:
拷贝一些管理员控制程序到/sbin目录下,最重要的就是要包含一个init命令;
拷贝应用程序运行时所需要的库到/lib,库文件可以从PC机上的交叉编译工具安装目录下拷贝,如libc-2.2.2.so、libcryt-2.2.2.so、libm-2.2.2.so、libutil-2.2.2.so等,为部分相应库建立快捷方式,提供一些应用程序按快 捷方式名称调用。值得注意的是C库要采用C库的版本glibc, glibc位于/lib/libc.so.6

Through the above analysis, we can see that the root file system is an important part of the Linux system. It contains key files and directories required for the startup and operation of the Linux system. The root file system is usually mounted under the / (root) directory and can contain mount points for other file systems. Root file systems can use different types of file system formats and can be created and managed through a number of tools. For example, we can use the mkfs command to format a partition or device as a root file system, we can use the mount command to mount or unmount a root file system, and we can use the df command to view the usage of a root file system, etc. In short, the root file system is the secret of the Linux system, which provides a stable and flexible foundation for the Linux system.

The above is the detailed content of Secrets of the Linux root file system. For more information, please follow other related articles on the PHP Chinese website!

source:lxlinux.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!