About file systems

Source: Internet
Author: User
Tags tmp folder

Theoretically, an embedded device does not need a file system if the kernel can run without running user processes. A file system is simply a directory structure, because Linux operating system devices exist in the system as files, classification and management of these files and interfaces for interaction with the kernel form a certain directory structure, that is, the file system, A file system is a form of system reflection. It provides users with an interface for detecting and controlling systems.

In my opinion, the root file system is a special file system. What is the difference between the root file system and the common file system? Because the root file system is the first file system to be mounted during kernel startup, the root file system must include the directories and key files required for Linux Startup, for example, when Linux is started, related files under the init directory are required. When Linux is mounted to a partition, Linux will definitely find the mount file/etc/fstab, the root file system also contains many application bin directories. any file that is required to start these Linux systems can become the root file system.

Linux supports multiple file systems, including ext2, ext3, vfat, NTFS, iso9660, jffs, romfs, and NFS. to centrally manage various file systems, linux introduces Virtual File System (VFS), which provides a unified operation interface and Application Programming Interface for various file systems.

During Linux Startup, the first file system must be mounted. If the system cannot mount the root file system from a specified device, the system will exit the startup due to an error. You can then automatically or manually Mount other file systems. Therefore, different file systems can exist in a system at the same time.

Different file system types have different characteristics, so there are different application scenarios based on the hardware characteristics and system requirements of storage devices. In Embedded Linux applications, the main storage devices are Ram (DRAM, SDRAM) and Rom (flash memory is often used). Commonly Used file system types based on storage devices include: jffs2, yaffs, cramfs, romfs, ramdisk, ramfs/tmpfs, etc.

1. Flash-based File System

As the main storage medium of embedded systems, flash memory has its own characteristics. The Flash write operation can only change 1 of the corresponding position to 0, but not 0 to 1 (Flash is to restore the content of the corresponding storage block to 1). Therefore, in general, when writing content to flash, you must first erase the corresponding storage range. This erasure is based on a block as a single bit.

Flash Memory mainly includes the nor and NAND technologies. Flash Memory has a limited number of writes. nand flash memory also has special hardware interfaces and read/write time series. Therefore, you must design a file system that meets application requirements based on flash hardware features. Traditional file systems such as ext2 have many drawbacks when used as Flash file systems.

A flash chip can be divided into multiple partitions, and different file systems can be used for each partition. The two flash chips can also be combined into one partition for use and adopt one file system. That is, the file system is for memory partitions, rather than storage chips.

In Embedded Linux, MTD (memory technology device, storage technology device) provides a unified abstract interface between the underlying hardware (flash memory) and the upper layer (File System, that is, the Flash file system is based on the MTD driver layer (see the file system structure diagram in Linux above ). The main advantage of using the MTD driver is that it is specially designed for a variety of non-volatile memory (mainly flash memory, therefore, it provides better support, management, and sector-based erasure and read/write operations for flash.

(1) jffs2

Jffs is a file system first developed by Swedish axis communications based on the Linux kernel for embedded systems. Jffs2 is a flash file system developed by RedHat Based on jffs. It was originally an embedded file system developed for the RedHat embedded product ECOs. Therefore, jffs2 can also be used in Linux and uClinux.

Jffs2: log flash File System Version 2 (Journalling flash filesystem V2)

It is mainly used for nor-type flash memory and is based on the MTD driver layer. It features read/write, data compression, and hash-table-based log file systems, it also provides crash/power-down security protection and "Write balance" support. The main drawback is that when the file system is full or near full, jffs2 is greatly slowed down due to the spam relationship.

Jffs is not suitable for nand flash memory because the capacity of nand flash memory is generally large, which leads to the rapid increase of memory space occupied by jffs to maintain log nodes, when mounting a jffs file system, you need to scan the entire Flash content to find all log nodes and create a file structure. It takes a lot of time for large-capacity nand flash memory.

(2) yaffs: yet another flash File System

Yaffs/yaffs2 is a log file system designed for embedded systems to use nand flash memory. Compared with jffs2, jffs2 reduces some features (for example, it does not support data compression), so it is faster, has a short Mount time, and consumes less memory. In addition to Linux and ECOs, it also supports wince, PSOs, and threadx.

Yaffs/yaffs2 comes with a NAND chip driver and provides APIs for direct access to the file system for embedded systems. You can directly operate the file system without using MTD and VFS in Linux. Of course, yaffs can also be used with MTD drivers.

The main difference between yaffs and yaffs2 is that the former only supports small pages (512 bytes) NAND Flash, while the latter supports large pages (2 kb) NAND Flash. In addition, yaffs2 significantly improves the memory space usage, garbage collection speed, and read/write speed.

(3) cramfs: Compressed ROM file system

Cramfs is a read-only compressed file system developed by Linux founder Linus Torvalds. It is also based on the MTD driver.

In the cramfs file system, each page (4 kb) is compressed separately and can be accessed by random pages. The compression ratio is as high as, saving a lot of flash storage space for the embedded system, this allows the system to store the same file through a lower-capacity FLASH, thus reducing system costs.

The cramfs file system stores data in Compressed Mode and decompress the data during running. Therefore, applications cannot run in xip mode. All applications must be copied to ram for running, however, this does not mean that the ram space required by ramfs is larger, because cramfs uses paging compression to store files. When reading files, it will not consume too much memory space at once, only allocate memory for the part currently actually read. No memory space is allocated for the part that has not been read. When the file we read is not in the memory, the cramfs file system automatically calculates the location of the compressed data and decompress it to ram.

In addition, it is fast and efficient. Its read-only feature is conducive to protecting the file system from damages and improving the system reliability.

Thanks to the above features, cramfs is widely used in embedded systems.

However, its read-only attribute is also a major defect, making it impossible for users to expand their content pairs.

Cramfs images are usually stored in flash, but they can also be stored in other file systems. Using loopback devices, you can install them in other file systems.

(4) romfs

The traditional romfs file system is a simple, compact, and read-only file system. It does not support dynamic erasure and storage, and stores data in sequence, therefore, you can run applications in xip (execute in place, In-chip operation) mode to save Ram space during system operation. Generally, the uClinux system uses the romfs file system.

Other file systems: fat/FAT32 can also be used for extended memory (SD cards for actual embedded systems, such as PDA, smartphone, and digital cameras ), this is mainly for better compatibility with the most popular Windows desktop operating systems. Ext2 can also be used as an embedded Linux File System, but using it in flash memory has many drawbacks.

2. Ram-based File System

(1) ramdisk

Ramdisk uses some fixed-size memory as a partition. It is not an actual file system, but a mechanism to load the actual file system into the memory, and can be used as the root file system. By placing some frequently accessed files (such as the read-only root file system) in the memory through ramdisk, the system performance can be significantly improved.

In the Linux Startup phase, initrd provides a mechanism to load the kernel image and the root file system into the memory.

(2) ramfs/tmpfs

Ramfs is a memory-based file system developed by Linus Torvalds. It works on the VFS layer and cannot be formatted. Multiple file systems can be created, you can specify the maximum memory size that can be used during creation. (In fact, VFS is essentially a memory file system, which unifies the representation of files in the kernel and caches the disk file system .)

The ramfs/tmpfs file system stores all the files in Ram, so read/write operations occur in Ram. You can use ramfs/tmpfs to store temporary or frequently-modified data, for example, the/tmp and/var directories avoid the read/write loss of flash memory and increase the data read/write speed.

Ramfs/tmpfs differs from the traditional ramdisk in that it cannot be formatted and the file system size can change with the size of the contained file.

One disadvantage of tmpfs is that all data is lost when the system is rebooted.

3. Network File System (NFS)

NFS is a technology developed and developed by Sun to share files through networks between different machines and operating systems. In the development and debugging phase of an embedded Linux system, you can use this technology to establish an NFS-based root file system on the host and mount it to an embedded device, so that you can easily modify the content of the root file system.

All of the above discussions are based on the memory-based file system of storage devices, which can be used as the root file system of Linux. In fact, Linux also supports logical or pseudo file systems, such as procfs (proc file system), for obtaining system information, and devfs (Device File System) and sysfs, used to maintain device files.

The storage device of the development board we want to transplant is nandflash. We can use a wide range of cramfs file systems.

2. Preparations for transplantation

1. Target Board

We still use the Development Board parameters that have been used during the previous porting process.

2. Software preparation

(1) busybox

Busybox is known as the Swiss army knife in the embedded Linux system. From this title, we can see that busybox is a collection of multiple functions, it combines many commonly used Unix Commands and tools into a single executable program. Compared with the GNU tool, busybox provides slightly fewer functions and parameters, but it is sufficient in a relatively small system (such as a boot disk) or an embedded system.

Busybox fully considers the special work environment with limited hardware resources. It uses a clever way to reduce its size: All commands are centralized into an executable file through the "plug-in" method, in actual application, different symbolic links are used to determine which operation to perform. For example, the final executable file generated is busybox. When you create a symbolic link ls for it, you can execute this new command to implement the column directory function. A single execution file is used to share the program code to the maximum extent, and even other operating system resources such as file headers and program control blocks in the memory are shared. For systems with tight resources, it's the most appropriate.

In the compilation process of busybox, you can easily add or subtract its "plug-in". The final symbolic link can also be automatically generated by the compilation system.

Compile busybox

The compilation process of busybox is very similar to that of the kernel. Make menuconfig is used for configuration and then compile in make.

[1] download the busyboxtool from http://www.busybox.net/downloads. Choose busybox-1.13.4.tar.bz2.

Unzip 2】decompress busybox-1.13.4.tar.bz2 using the command

Tar jxvf busybox-1.13.4.tar.bz2

[3] Go To The busybox directory and modify makefile in line 10 cross_compile = arm-Linux-

[4] Make menuconfig for configuration. You can select static compilation. If it is dynamic compilation, copy the corresponding library file. By default, save the configuration.

[5] Make all install

This will generate the _ Install Folder under the busybox directory.

(2) file system packaging tools

[1] download the cramfstool from http://prdownloads.sourceforge.net/cramfs.

Run tar zxvf cramfs-1.1.tar.gz to decompress cramfs-1.1.tar.gz.

[3] Run make in the root directory of the cramfs tool.

[4] Make will generate a mkcramfs file in the root directory of the cramfs tool. This is the tool we need.

Iii. Production Process

1. Create a root file system directory

Is the _ install directory CD generated by busybox... /_ Install

2. Create various necessary system file directories.

Mkdir Dev lib TMP proc

3. Create a device file.

CD Dev

Mknod fb0 C 29 0 create a framebuffer Device File

Mknod ts0 c 13 128 create a touch screen Device File

Mknod console C 5 1

Mknod tty0 C 4 0

Mknod tty1 C 4 1

Mknod tty2 C 4 2

Mknod tty3 C 4 3

Mknod tty4 C 4 4

4. Add necessary library files. Because you have not selected static compilation of busybox before, copy the corresponding library files here.

CD lib

CP-Arn... /ARM-Linux/lib /*.

5. Add applications as needed

Here we write a simple application to print a greeting. The program code is as follows:

# Include <stdio. h>

Void main ()

{

Printf ("Hello world \ n ");

}

Note that you must use arm-Linux-GCC during compilation. Since we copied all the library files of the compiler before, we can directly compile them dynamically. The generated Executable File hello is placed in the TMP folder. Command used:

Rm-Linux-GCC hello. C-O hello

CP-Arn... /_ Install/tmp/

6. Package

Mkcramfs _ install rootfs

Iv. Burning the write process

We adopt the same kernel burning method as the kernel burning method. We use the TFTP method to connect the target board to the PC using a network cable and configure the network parameters of the target board, mainly serverip and ipaddr.

First, download rootfs to the memory: # TFTP 30008000 rootfs

Follow the nandflash partition of the previous kernel to burn and write the file system in the memory to flash:

# NAND erase 500000 3b00000

# NAND write 30008000 500000 3b00000

Restart uboot to load the file system.

We can see that the kernel is started, and no panic is displayed. Then we will prompt you to press enter and enter the command line. We can use some common Linux commands, such as LS, CD, and VI.

As shown in:

Note: We use the putty serial port display software. If you use the dnw to view the serial port information, the following error will occur:

The first directory [1; 34 mbin [0 m

We will find that there are other contents besides the bin directory. This is not a problem with the file system, but the dnw software does not support additional information indicating colors, if you change to putty, it will be displayed normally.

We can run our own application Hello:./Hello

As shown in figure Hello World:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.