这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录 前一修订版 后一修订版 | 前一修订版 | ||
jaysnote:ramfs_rootfs_initramfs [2018/11/16 21:27] jaylee ↷ 页面名由jaysnote:ramfs_rootfs_initramf改为jaysnote:ramfs_rootfs_initramfs |
jaysnote:ramfs_rootfs_initramfs [2021/06/22 23:14] (当前版本) |
||
---|---|---|---|
行 1: | 行 1: | ||
< | < | ||
- | # ramfs, rootfs and initramf | + | # ramfs, rootfs and initramfs |
october 17, 2005 | october 17, 2005 | ||
行 9: | 行 9: | ||
## What is ramfs? | ## What is ramfs? | ||
+ | Ramfs is a very simple filesystem that exports Linux' | ||
- | Ramfs is a very simple filesystem that exports | + | Normally all files are cached in memory by Linux. Pages of data read from backing store (usually the block device the filesystem is mounted on) are kept around in case it' |
- | Normally all files are cached in memory by Linux. | + | With ramfs, there is no backing store. |
- | With ramfs, there is no backing store. | + | The amount of code required to implement ramfs is tiny, because all the work is done by the existing Linux caching infrastructure. |
- | This means the pages are never marked clean, so they can't be freed by th M when it's looking to recycle memory. | + | you're mounting the disk cache as a filesystem. |
- | + | ||
- | The amount of code required to implement ramfs is tiny, because all th ork is done by the existing Linux caching infrastructure. | + | |
- | you're mounting the disk cache as a filesystem. | + | |
## ramfs and ramdisk: | ## ramfs and ramdisk: | ||
- | |||
- | The older "ram disk" mechanism created a synthetic block device out o n area of RAM and used it as backing store for a filesystem. | ||
(such as ext2) to format and interpret this data. | (such as ext2) to format and interpret this data. | ||
- | Compared to ramfs, this wastes memory (and memory bus bandwidth), | + | Compared to ramfs, this wastes memory (and memory bus bandwidth), |
More to the point, all the work ramfs is doing has to happen _anyway_, | More to the point, all the work ramfs is doing has to happen _anyway_, | ||
- | since all file access goes through the page and dentry caches. | + | since all file access goes through the page and dentry caches. |
- | Another reason ramdisks are semi-obsolete is that the introduction | + | Another reason ramdisks are semi-obsolete is that the introduction |
See losetup (8) for details. | See losetup (8) for details. | ||
## ramfs and tmpfs: | ## ramfs and tmpfs: | ||
- | One downside of ramfs is you can keep writing data into it until you fil p all memory, and the VM can't free it because the VM thinks that file hould get written to backing store (rather than swap space), but ramfs hasn' | + | One downside of ramfs is you can keep writing data into it until you fill up all memory, and the VM can't free it because the VM thinks that files should |
- | A ramfs derivative called tmpfs was created to add size limits, and the abilit o write the data to swap space. | + | A ramfs derivative called tmpfs was created to add size limits, and the ability to write the data to swap space. |
## What is rootfs? | ## What is rootfs? | ||
+ | Rootfs is a special instance of ramfs (or tmpfs, if that's enabled), which is always present in 2.6 systems. | ||
- | Rootfs is a special instance of ramfs (or tmpfs, if that's enabled), which i lways present in 2.6 systems. | + | Most systems just mount another filesystem over rootfs and ignore it. |
- | + | ||
- | Most systems just mount another filesystem over rootfs and ignore it. | + | |
## What is initramfs? | ## What is initramfs? | ||
- | + | All 2.6 Linux kernels contain a gzipped " | |
- | All 2.6 Linux kernels contain a gzipped " | + | |
All this differs from the old initrd in several ways: | All this differs from the old initrd in several ways: | ||
行 55: | 行 49: | ||
- The old initrd was always a separate file, while the initramfs archive is linked into the linux kernel image. | - The old initrd was always a separate file, while the initramfs archive is linked into the linux kernel image. | ||
- | - The old initrd file was a gzipped filesystem image (in some file format, | + | - The old initrd file was a gzipped filesystem image (in some file format,such as ext2, that needed a driver built into the kernel), while the new initramfs archive is a gzipped cpio archive (like tar only simpler,see cpio(1) and `Documentation/ |
- | | + | |
- | | + | |
- The program run by the old initrd (which was called /initrd, not /init) did some setup and then returned to the kernel, while the init program from initramfs is not expected to return to the kernel. | - The program run by the old initrd (which was called /initrd, not /init) did some setup and then returned to the kernel, while the init program from initramfs is not expected to return to the kernel. | ||
行 67: | 行 59: | ||
## Populating initramfs: | ## Populating initramfs: | ||
+ | The 2.6 kernel build process always creates a gzipped cpio format initramfs archive and links it into the resulting kernel binary. | ||
- | The 2.6 kernel build process always creates a gzipped cpio format initramf rchive | + | The config option **CONFIG_INITRAMFS_SOURCE** (in General Setup in menuconfig,and living in usr/ |
- | The config option CONFIG_INITRAMFS_SOURCE (in General Setup in menuconfig, | + | ```bash |
- | and living in usr/ | + | |
- | + | ||
- | ```bash | + | |
dir /dev 755 0 0 | dir /dev 755 0 0 | ||
nod / | nod / | ||
行 84: | 行 74: | ||
dir /mnt 755 0 0 | dir /mnt 755 0 0 | ||
file /init initramfs/ | file /init initramfs/ | ||
- | ``` | + | ``` |
- | Run " | + | Run `" |
- | One advantage of the configuration file is that root access is not required | + | One advantage of the configuration file is that root access is not required |
- | The kernel does not depend on external cpio tools. | + | The kernel does not depend on external cpio tools. |
- | (obviously) self-contained. | + | |
- | The one thing you might need external cpio utilities installed for is creatin r extracting your own preprepared cpio files to feed to the kernel build | + | The one thing you might need external cpio utilities installed for is creating or extracting your own preprepared cpio files to feed to the kernel build (instead of a config file or directory). |
- | (instead of a config file or directory). | + | |
- | The following command line can extract a cpio image (either by the above scrip r by the kernel build) back into its component files: | + | The following command line can extract a cpio image (either by the above script or by the kernel build) back into its component files: |
```bash | ```bash | ||
- | | + | cpio -i -d -H newc -F initramfs_data.cpio --no-absolute-filenames |
``` | ``` | ||
- | The following shell script can create a prebuilt cpio archive you ca se in place of the above config file: | + | The following shell script can create a prebuilt cpio archive you can use in place of the above config file: |
```bash | ```bash | ||
- | !/bin/sh | + | #!/bin/sh |
- | Copyright 2006 Rob Landley rob@landley.net and TimeSys Corporation. | + | #Copyright 2006 Rob Landley rob@landley.net and TimeSys Corporation.Licensed under GPL version 2 |
- | Licensed under GPL version 2 | + | |
- | | + | if [ $# -ne 2 ] |
- | then | + | then |
- | echo " | + | echo " |
- | exit 1 | + | exit 1 |
- | fi | + | fi |
- | if [ -d " | + | |
- | then | + | if [ -d " |
- | echo " | + | then |
- | (cd " | + | echo " |
- | else | + | (cd " |
- | echo "First argument must be a directory" | + | else |
- | exit 1 | + | echo "First argument must be a directory" |
- | fi | + | exit 1 |
+ | fi | ||
``` | ``` | ||
- | Note: The cpio man page contains some bad advice that will break your initramf rchive | + | > **Note:** The cpio man page contains some bad advice that will break your initramfs archive |
- | ## External initramfs images: | + | External initramfs images: |
+ | -------------------------- | ||
+ | If the kernel has initrd support enabled, an external cpio.gz archive can also be passed into a 2.6 kernel in place of an initrd. | ||
- | If the kernel has initrd support enabled, an external cpio.gz archive can als e passed into a 2.6 kernel in place of an initrd. In this case, the kerne ill autodetect the type (initramfs, | + | This has the memory efficiency advantages of initramfs (no ramdisk block device) but the separate packaging |
- | This has the memory efficiency advantages of initramfs (no ramdisk bloc evice) but the separate packaging of initrd (which is nice if you hav on-GPL code you'd like to run from initramfs, without conflating it wit he GPL licensed Linux kernel binary). | + | It can also be used to supplement the kernel' |
- | + | ||
- | It can also be used to supplement the kernel' | + | |
## Contents of initramfs: | ## Contents of initramfs: | ||
- | An initramfs archive is a complete self-contained root filesystem for Linux. | + | An initramfs archive is a complete self-contained root filesystem for Linux.If you don't already understand what shared libraries, devices, and path you need to get a minimal root filesystem up and running, here are som references: |
- | If you don't already understand what shared libraries, devices, and path ou need to get a minimal root filesystem up and running, here are som eferences: | + | |
- | http:// | + | * http:// |
- | http:// | + | |
+ | * http:// | ||
- | http:// | + | * http:// |
- | The " | + | The " |
- | I use uClibc (http:// | + | I use uClibc (http:// |
- | myself. | + | |
- | In theory you could use glibc, but that's not well suited for small embedde | + | In theory you could use glibc, but that's not well suited for small embedde |
- | A good first step is to get initramfs to run a statically linked "hello world" | + | A good first step is to get initramfs to run a statically linked "hello world" program as init, and test it under an emulator like qemu (www.qemu.org) |
- | program as init, and test it under an emulator like qemu (www.qemu.org) | + | |
```c | ```c | ||
行 158: | 行 145: | ||
include < | include < | ||
- | |||
include < | include < | ||
行 173: | 行 159: | ||
``` | ``` | ||
- | When debugging a normal root filesystem, it's nice to be able to boot with | + | When debugging a normal root filesystem, it's nice to be able to boot with " |
- | " | + | |
## Why cpio rather than tar? | ## Why cpio rather than tar? | ||
- | |||
This decision was made back in December, 2001. The discussion started here: | This decision was made back in December, 2001. The discussion started here: | ||
行 189: | 行 173: | ||
The quick and dirty summary version (which is no substitute for readin he above threads) is: | The quick and dirty summary version (which is no substitute for readin he above threads) is: | ||
- | 1. cpio is a standard. | + | 1. cpio is a standard. |
- | such as: | + | |
+ | http:// | ||
+ | |||
+ | It's not as popular as tar because the traditional cpio command line tools require _truly_hideous_ command line arguments. | ||
+ | such as: | ||
2. The cpio archive format chosen by the kernel is simpler and cleaner (and thus easier to create and parse) than any of the (literally dozens of) various tar archive formats. | 2. The cpio archive format chosen by the kernel is simpler and cleaner (and thus easier to create and parse) than any of the (literally dozens of) various tar archive formats. | ||
行 201: | 行 189: | ||
| | ||
+ | |||
| | ||
+ | |||
| | ||
行 212: | 行 200: | ||
## Future directions: | ## Future directions: | ||
- | + | Today (2.6.16), initramfs is always compiled in, but not always used. | |
- | Today (2.6.16), initramfs is always compiled in, but not always used. | + | |
"early userspace" | "early userspace" | ||
- | The move to early userspace is necessary because finding and mounting the rea oot device is complex. | + | The move to early userspace is necessary because finding and mounting the real root device is complex. |
- | and so on. | + | |
- | This kind of complexity (which inevitably includes policy) is rightly | + | This kind of complexity (which inevitably includes policy) is rightly |
The klibc package has now been accepted into Andrew Morton' | The klibc package has now been accepted into Andrew Morton' | ||
- | The kernel' | + | The kernel' |
</ | </ |