What is ramfs?
Ramfs is a RAM File System with dynamic spatial changes. It is very simple and used to implement the Linux cache mechanism (Cache page cache and dentry cache) file system.
Generally, all Linux Files are cached in the memory. The data page to be read is cached in memory after being read from the block device. Perform the marked as clean operation on the data page of the supported storage device. When the Virtual File System needs to support the data page memory of the storage device, it can be released. Based on the same mechanism, you can release the occupied data page memory after writing data to the storage device (writing a file back to the storage device, marked as clean. The same mechanism exists for the cache occupied by the file directory (dentry: directory entry.
However, ramfs does not need to support storage devices (there is no support for caching, but there is a cache ). That is to say, the file written to ramfs can be normally allocated with page cache and dentry cache, but cannot be written to the supporting storage device. These page cache and dentry cache cannot be released or recycled by VM.
Because ramfs can be based on the existing Linux file system structure, the code used to implement ramfs is very small. Generally, the cache supporting storage devices is installed as a file system. Therefore, ramfs cannot be selected through menuconfig, so it must enter the kernel.
You can write data under ramfs until the memory is full. Because the VM (vitual memory) thinks that files should be written back to support storage devices, rather than swap space, the VM cannot release the memory allocated by ramfs. Therefore, only the root user (or trusted user) can perform ramfs write operations.
-------------------------------------------------------------------------------
What is RAM disk?
"RAM disk" is an out-of-date mechanism that supports storage by the File System (2.6 No). It is the Integrated Block device (synthetic block device) opened on the ram ). The size of the RAM disk is fixed, and the size of the installed file system (not ramfs) is also fixed. To use RAM disk, You need to copy the memory from the fake block device to the page cache to generate and destroy dentry, and the driver of the file system needs to format and interpret the above data, therefore, the RAM disk mechanism is no longer used.
Compared with ramfs, RAM disk wastes both memory and bus bandwidth. At the same time, RAM disk also increases the unnecessary burden on the CPU and pollutes the CPU cache (although there are ways to avoid pollution, it is very resource-consuming ). The ramfs mechanism is very natural, because file access can be performed through page cache and dentry cache. Another reason why RAM disk is deprecated is the introduction of loopback. The loopback device provides a more flexible and convenient way to create integrated Block devices from files rather than from memory blocks.
-------------------------------------------------------------------------------
What is tmpfs?
Tmpfs is a derivative of ramfs, used to limit the cache size and write data to the swap space. It is a file system used to save All VM files. All cached content in tmpfs is temporary. Once uninstalled, all content will be lost. It places all the caches in the kernel, and its size changes synchronously with the size of files. However, its size is limited and can be modified. It can write pages that are no longer needed to the swap space.
-------------------------------------------------------------------------------
What is rootfs?
Rootfs is a special ramfs instance, which must exist in the kernel of 2.6. Rootfs cannot be detached (instead of adding special code to maintain an empty linked list, it is better to always add rootfs nodes, so it is easy to maintain kernel: simple and refined. Rootfs is an empty ramfs instance, which occupies a very small space ). Most other file systems are installed on rootfs.
-------------------------------------------------------------------------------
What is initramfs?
2.6 of linux kernels contain documents in cpio format compressed by gzip, which can be decompressed to rootfs during kernel boot. After decompression, the kernel checks whether the rootfs contains the init file. If the init file exists, the kernel executes the file and assigns the process number pid = 1. This INIT program directs the entire system, including locating and installing the real root device. If no INIT program (init file) exists in the rootfs extracted from the cpio file, the kernel executes the old Code, locates and installs the root partition, and runs the/sbin/INIT program.
-------------------------------------------------------------------------------
Differences between initramfs and initrd
1. initrd is a separate file; initramfs is linked together with the Linux kernel (the program under the/usr directory is responsible for generating the initramfs document ).
2. initrd is a compressed file system image (such as ext2, which requires a kernel driver). initramfs is a cpio compression file similar to tar. The cpio decompression code in the kernel is very small, and the init data can be discarded after boot.
3. the initrd Program (initd, not init) runs partial setup and returns the kernel. The INIT program executed by initramfs does not return the kernel (if/init needs to pass control to the kernel, you can install a new root device in the/directory and start a new INIT program ).
4. When switching to another root device, initrd will unmount the ramdisk After Execute initt_root. initramfs is a rootfs and cannot be either initt_root or unmounted. Initramfs deletes all rootfs content (find-xdev/-exec RM '{} '';') and installs root to rootfs (CD/newmount; mount -- move. /; chroot .), mount stdin/sdout/stderr to the new/dev/console and re-Execute init. Because this is a very difficult implementation process (including deleting a command before using it), klibc toolkit introduces a Helper Program/utils/run_init.c to execute the above process. Most other tool kits (including busybox) call this command "switch_root ".
-------------------------------------------------------------------------------
Populating initramfs
By default, the 2.6 kernel always generates a gzipped cpio document, which is linked with the kernel. This document is blank by default. The size of this document in x86 environment is 134 bytes.
The config_initramfs_source configuration parameter specifies the source of the initramfs document and is automatically embedded into the binary file. This parameter can point to a gzipped cpio file, a directory containing the file, or a text file containing the file description. For example, text files:
DIR/dev 755 0 0
NOD/dev/console 644 0 0 C 5 1
NOD/dev/loops 0 644 0 0 B 7 0
DIR/bin755 1000 1000
Slink/bin/sh busybox 777 0 0
File/bin/busybox initramfs/busybox 755 0 0
DIR/proc 755 0 0
DIR/sys 755 0 0
DIR/mnt 755 0 0
File/init initramfs/init. Sh 755 0 0
After the kernel is compiled, run the/usr/gen_init_cpio command to obtain the cpio document. One advantage of the configuration file is that you do not need the root permission or create a device node in the new document. The two file commands in the preceding document are used to find the init. Sh file and busybox file under the initramfs directory. The kernel does not need an external cpio tool to implement the cpio documentation of initramfs. If a directory instead of a description file is specified during configuration, a description file (as input of/usr/gen_init_cpio.c) will be generated from this directory during kernel compilation ). During kernel compilation, the cpio Generation Code is integrated with the kernel, and the decompression program is integrated with the kernel during boot.
An external cpio tool is required if you use a custom cpio document instead of a configuration file or directory. For example, the following command can extract included files from the cpio image file and compress the cpio image file:
Cpio-I-d-H NEWC-F initramfs_data.cpio -- no-absolute-filenames
The shellscript below can generate a specific cpio.gz document, which can be used to replace the cpio document generated by the configuration file:
#! /Bin/sh
If [$ #-ne 2]
Then
Echo "Usage: mkinitramfs directory imagename.cpio.gz"
Exit 1
Fi
If [-d "$1"]
Then
Echo "creating $2 from $1"
(Cd "$1"; find. | cpio-o-h NEWC | gzip)> "$2"
Else
Echo "first argument must be a directory"
Exit 1
Fi
-------------------------------------------------------------------------------