Chinaunix首页 | 论坛 | 博客
  • 博客访问: 845579
  • 博文数量: 116
  • 博客积分: 1472
  • 博客等级: 上尉
  • 技术积分: 1725
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-06 11:45
文章分类

全部博文(116)

文章存档

2015年(1)

2014年(42)

2013年(5)

2012年(19)

2011年(49)

我的朋友

分类: 嵌入式

2014-02-06 18:08:43

What is initramfs?

------------------

All 2.6 Linux kernels contain a gzipped "cpio" format archive, which is extracted into rootfs when the kernel boots up. After extracting, the kernel checks to see if rootfs contains a file "init", and if so it executes it as PID1. If found, this init process is responsible for bringing the system the rest of the way up, including locating and mounting the real root device (if any). If rootfs does not contain an init program after the embedded cpio archive is extracted into it, the kernel will fall through to the older code to locate and mount a root partition, then exec some variant of /sbin/init out of that.

所有的2.6 Linux内核包含了一个gzip压缩过的"cpio"格式的存档,当内核启动时它将被提取到rootfs(可以在内核引导的时候解压缩为rootfs)。在提取之后,内核检测rootfs是否包含了一个"init"文件,如果包含就执行它并设置其PID为1。如果找到init文件,这个init进程将负责引导系统的其余内容,包含了要寻找和挂载的真正根设备(root device,若有的话)。如果rootfs在提取cpio存档后并不包含一个init程序,内核执行旧的代码,定位并且安装root分区,执行/sbin/init程序。

All this differs from the old initrd in several ways:

与旧的initrd有以下几点不同:

- The old initrd was always a separate file, while the initramfs archive is linked into the linux kernel image. (The directory linux-*/usr is devoted to generating this archive during the build.)

- 存在的方式不同:旧的initrd通常是一个独立的文件,而initramfs存档

Linux内核链接在一起(/usr目录下的程序负责生成initramfs文档)


- 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/early-userspace/buffer-format.txt). The kernel's cpio extraction code is not only extremely small, it's also__init text and data that can be discarded during the boot process.

- 文件格式不同:旧的initrd文件是一个gzip压缩过的文件系统映像(可以是ext2等,需要内核的驱动),而initramfs存档是一个gzip压缩过的类似于tar的cpio存档(象tar一样简单,参见cpio(1) and Documentation/early-userspace/buffer-format.txt)。内核的cpio提取代码不仅极少,而且init文本和数据可以在启动过程中被丢弃。

- 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. (If /init needs to hand off control it can overmount / with a new root device and exec another init program. See the switch_root utility, below.)

- 是否返回内核:initrd运行的程序(叫做/initrd,而不是/init),执行一些设定,接着返回内核;而initramfs中的init程序执行后并不返回内核。(如果/init需要向内核传递控制权,可以再次在/目录下安装(挂载)一个新的root设备并且启动一个新的init程序)

- When switching another root device, initrd would pivot_root and then umount the ramdisk. But initramfs is rootfs: you can neither pivot_root

rootfs, nor unmount it. Instead delete everything out of rootfs to free up the space (find -xdev / -exec rm '{}' ';'), overmount rootfs with the new root (cd /newmount; mount --move . /; chroot .), attach stdin/stdout/stderr to the new /dev/console, and exec the new init.

-  切换root设备时处理方式不同:当切换到另一个根设备,initrd执行pivot_root后,卸载ramdisk;但是initramfs是rootfs:既不能pivot_root,也不能卸载。而是:

  •    首先,删除rootfs的所有内容来释放空间(find -xdev / -exec rm '{}' ';'),
  •    接着,再次挂载(安装)root到rootfs上(cd /newmount; mount --move . /; chroot .)(cd /newmount; mount --move . /; chroot .),
  •     最后,把stdin/stdout/stderr挂到新的/dev/console上,并执行新的init。


Since this is a remarkably persnickety process (and involves deleting commands before you can run them), the klibc package introduced a helper

program (utils/run_init.c) to do all this for you. Most other packages (such as busybox) have named this command "switch_root".

由于这是一个相当困难的实现过程(包括在你运行它们之前涉及到删除命令),所以klibc工具包引入一个帮助程序/utils/run_init.c来做这些事情。其他大部分工具包(包括busybox)把这个命令称为"switch_root"。 

 

Populating initramfs:

提供 initramfs

---------------------

The 2.6 kernel build process always creates a gzipped cpio format initramfs archive and links it into the resulting kernel binary. By default, this

archive is empty (consuming 134 bytes on x86).

2.6的内核缺省情况下总是生成一个gzipped的cpio文档,并且和内核链接在一起。这个文档缺省是空的(在x86环境下的大小是134字节)

The config option CONFIG_INITRAMFS_SOURCE (in General Setup in menuconfig,and living in usr/Kconfig) can be used to specify a source for the initramfs archive, which will automatically be incorporated into the resulting binary. This option can point to an existing gzipped cpio archive, a directory containing files to be archived, or a text file specification such as the following example:

配置选项CONFIG_INITRAMFS_SOURCE(存在于menuconfig的General Setup选项,并存放usr/Kconfig中)可用于为initramfs存档指定的一个源文件,它自动嵌入到生成的二进制文件中。这个选项可以指向一个现有的gzip压缩的cpio存档、或者一个包含着将被归档的文件的目录、或者是一个文本文件范例(这里指配置文件或描述文件),比如下面的示例:


  1. dir /dev 755 0 0
  2. nod /dev/console 644 0 0 c 5 1
  3. nod /dev/loop0 644 0 0 b 7 0
  4. dir /bin 755 1000 1000
  5. slink /bin/sh busybox 777 0 0
  6. file /bin/busybox initramfs/busybox 755 0 0
  7. dir /proc 755 0 0
  8. dir /sys 755 0 0
  9. dir /mnt 755 0 0
  10. file /init initramfs/init.sh 755 0 0


Run "usr/gen_init_cpio" (after the kernel build) to get a usage message documenting the above file format.

在内核编译完成后,可以执行/usr/gen_init_cpio命令获得一个关于cpio文档的用法信息,该信息记录了cpio文件格式的相关说明。

One advantage of the configuration file is that root access is not required to set permissions or create device nodes in the new archive. (Note that those two example "file" entries expect to find files named "init.sh" and "busybox" in a directory called "initramfs", under the linux-2.6.* directory. SeeDocumentation/early-userspace/README for more details.)


该配置文件的一个优点是:不需要root权限就可以执行权限设置、或者在新的存档文件中创建设备节点。(注意上面范例中的那两条"file"命令:期望在linux-2.6.*目录下,在"initramfs"目录中查找"init.sh"和"busybox"这两个文件。参见Documentation/early- userspace/README 以获得更多细节。)

The kernel does not depend on external cpio tools. If you specify a directory instead of a configuration file, the kernel's build infrastructure creates a configuration file from that directory (usr/Makefile calls scripts/gen_initramfs_list.sh), and proceeds to package up that directory using the config file (by feeding it to usr/gen_init_cpio, which is created from usr/gen_init_cpio.c).

内核并不需要外部的cpio工具来实现initramfs的cpio文档。如果在配置时指定的是一个目录而不是一个描述文件,内核编译时将从这个目录生成一个配置/描述文件(usr/Makefile 调用scripts/gen_initramfs_list.sh),并使用该配置文件来对该目录进行打包(该配置文件作为/usr/gen_init_cpio.c的输入,而usr/gen_init_cpio由usr/gen_init_cpio.c 生成)。如果不使用配置文件或者配置目录,而使用定制的cpio文档时,需要外部的cpio工具。

The kernel's build-time cpio creation code is entirely self-contained, and the kernel's boot-time extractor is also (obviously) self-contained.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).

在内核编译时,cpio的生成代码和内核浑然一体;在内核引导时,cpio的解压缩程序也和内核浑然一体。只有一种情况需要安装外部的cpio工具:创建或提取你自己的cpio文件以提供给内核构建使用(不是一个配置文件或目录)。


The following command line can extract a cpio image (either by the above script or by the kernel build) back into its component files:

例如下面的命令可以从cpio映像文件抽取包含的文件(不管是通过以上脚本或是通过内核编译)、压缩cpio映像文件


  1. cpio -i -d -H newc -F initramfs_data.cpio --no-absolute-filenames

The following shell script can create a prebuilt cpio archive you can use in place of the above config file:


下面的shell脚本可以生成一个定制的cpio.gz文档,可以用来代替配置文件生成的cpio文档:



  1. #!/bin/sh
  2. # Copyright 2006 Rob Landley <rob@landley.net> and TimeSys Corporation.
  3. # Licensed under GPL version 2
  4. if [ $# -ne 2 ]
  5. then
  6. echo "usage: mkinitramfs directory imagename.cpio.gz"
  7. exit 1
  8. fi
  9. if [ -d "$1" ]
  10. then
  11. echo "creating $2 from $1"
  12. (cd "$1"; find . | cpio -o -H newc | gzip) > "$2"
  13. else
  14. echo "First argument must be a directory"
  15. exit 1
  16. fi

Note: The cpio man page contains some bad advice that will break your initramfs archive if you follow it. It says "A typical way to generate the list of filenames is with the find command; you should give find the -depth option to minimize problems with permissions on directories that are unwritable or not searchable." Don't do this when creating initramfs.cpio.gz images, it won't work. The Linux kernel cpio extractor won't create files in a directory that doesn't exist, so the directory entries must go before the files that go in those directories. The above script gets them in the right order.


注意:cpio的man页包含了一些不良建议,如果按照里面的做法,会破坏你的initramfs存档。里面说到"生成文件名列表的一个典型方式是使用find命令,你应该使用-depth选项,以最小化搜索不可写或者不可搜索的目录时所产生的权限问题"。在创建initramfs.cpio.gz映像时不要这么做,因为它并不可行。Linux内核的cpio提取器不会在一个不存在的目录中创建文件,因此目录项必须在文件被提取到该目录之前被提取。以上脚本保证了它们的顺序正确性。

External initramfs images:

外部initramfs映像

--------------------------

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. In this case, the kernel will autodetect the type (initramfs, not initrd) and extract the external cpio archive into rootfs before trying to run /init.

如果内核启用initrd支持,一个外部的cpio.gz存档也可替换一个2.6 内核的initrd。在这种情况下,内核将自动检测该文件的类型(initramfs, 而不是initrd)并在尝试运行/init前,提取外部的cpio存档到rootfs。

This has the memory efficiency advantages of initramfs (no ramdisk block device) but the separate packaging of initrd (which is nice if you have non-GPL code you'd like to run from initramfs, without conflating it with the GPL licensed Linux kernel binary).

这样做(使用外部initramfs)让initramfs具有内存效率的优势(没有内存磁盘块设备),而单独封装的initrd没有这种优势(如果你有非GPL许可协议的代码从initramfs运行,这还是不错的。不要与GPL许可的二进制的Linux内核混为一谈)。

It can also be used to supplement the kernel's built-in initramfs image. The files in the external archive will overwrite any conflicting files in the built-in initramfs archive. Some distributors also prefer to customize a single kernel image with task-specific initramfs images, without recompiling.

外部initramfs映像也可以用来补充内核内建的initramfs映像。外部initramfsk存档中的文件将覆盖与内建initramfs存档中任意不一致的文件。一些发行商也偏爱于定制单一的内核映像并附带着特定任务的initramfs映像,而不用重编译。

 

Contents of initramfs:

----------------------

An initramfs archive is a complete self-contained root filesystem for Linux.If you don't already understand what shared libraries, devices, and paths you need to get a minimal root filesystem up and running, here are some references:

initramfs存档是一个完整独立的Linux根文件系统。如果你还不熟悉共享库、设备和路径,你需要获得一个小的根文件系统来启动和运行,这里有一些参考资料:

The "klibc" package () is designed to be a tiny C library to statically link early userspace code against, along with some related utilities. It is BSD licensed.

软件包"klibc"()被设计为一个极小的C库来静态连接早期的用户空间代码,并附带着一些相关的工具。它是BSD授权。

I use uClibc () and busybox () myself. These are LGPL and GPL, respectively. (A self-contained initramfs package is planned for the busybox 1.3 release.)

我使用uClibc ()和busybox ()。这些分别是LGPL和GPL授权。(一个独立的initramfs软件包被设计用在busybox 1.3 release。)

In theory you could use glibc, but that's not well suited for small embedded uses like this. (A "hello world" program statically linked against glibc is over 400k. With uClibc it's 7k. Also note that glibc dlopens libnss to do name lookups, even when otherwise statically linked.)

从理论上说你可以使用glibc,但是对于小型嵌入式系统象这样使用并适合。(一个"hello world"程序静态连接glibc将近400k。用uClibc的话它是7k。还要注意,glibc dlopens libnss作为名称查找,即使是使用静态连接。)

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  () or User Mode Linux, like so:

一个好的开头是:让initramfs以init的方式运行一个静态连接的"hello world"程序作,并在一个类似qemu()的仿真器或用户模式的Linux中测试它,象这样:

先执行命令:

cat > hello.c << EOF

然后输入以下内容:

#include

#include

int main(int argc, char *argv[])

{

printf("Hello world!\n");

sleep(999999999);

}

EOF

gcc -static hello.c -o init

echo init | cpio -o -H newc | gzip > test.cpio.gz

# Testing external initramfs using the initrd loading mechanism.

qemu -kernel /boot/vmlinuz -initrd test.cpio.gz /dev/zero

最后,按下Ctrl+D退出

When debugging a normal root filesystem, it's nice to be able to boot with "init=/bin/sh". The initramfs equivalent is "rdinit=/bin/sh", and it's

just as useful.

当调试一个普通根文件系统时,能够用"init=/bin/sh"来启动将是不错的。与initramfs等价的是"rdinit=/bin/sh",而且它很有用。

 

Why cpio rather than tar?

为什么要使用cpio格式,而不采用tar格式

-------------------------

This decision was made back in December, 2001. The discussion started here:

这个决定的发布是在2001年12月。讨论浏览在这里:

And spawned a second thread (specifically on tar vs cpio), starting here:

并且催生了第二件事情(tar与cpio的比较),浏览这里

The quick and dirty summary version (which is no substitute for reading the above threads) is:

临时应急的版本(该版本是无法替代阅读上述帖子内容的)是:

1) cpio is a standard. It's decades old (from the AT&T days), and already widely used on Linux (inside RPM, Red Hat's device driver disks). Here's

a Linux Journal article about it from 1996:

cpio是一个标准。它十来岁了(从美国AT&T的日子开始),并已被广泛应用在Linux上(在RPM中,Red Hat的设备驱动磁盘上)。这里有从1996年起有关它的Linux日志:

It's not as popular as tar because the traditional cpio command line tools require _truly_hideous_ command line arguments. But that says nothing either way about the archive format, and there are alternative tools,such as:

它并没有象tar一样流行,因为传统的cpio命令行工具要求使用极可怕的命令行参数。但是不管是哪种方式的存档格式,都没有说明清楚。对于cpio,有一些可选的替代工具。如:

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. The complete initramfs archive format is explained in buffer-format.txt, created in usr/gen_init_cpio.c, and extracted in init/initramfs.c. All three together come to less than 26k total of human-readable text.
cpio存档的格式是由内核选择的,它比(许多国家的)任意的tar存档格式都简单且干净(并极容易来创建和解析)。文本文件buffer-format.txt对initramfs存档格式作出了完整的说明,该文件文件由usr/gen_init_cpio.c创建产生,并提取在init/initramfs.c中。所有三个可阅读的文本一起不到26k。

3) The GNU project standardizing on tar is approximately as relevant as Windows standardizing on zip. Linux is not part of either, and is free

to make its own technical decisions.

GNU项目使用tar标准似乎是与Windows使用zip的标准相关。Linux并不是它们其中任一个的一部分,并自由的作出了自己的技术决策。

4) Since this is a kernel internal format, it could easily have been something brand new. The kernel provides its own tools to create and extract this format anyway. Using an existing standard was preferable,but not essential.

由于这是一个内核的内部格式,它可以很容易做一些更新。内核提供它自己的工具来创建和提取这个格式。使用现有的标准是可取的,但不是必要的。

5) Al Viro made the decision (quote: "tar is ugly as hell and not going to be supported on the kernel side"):

Al Viro 作出决定(引用: "tar is ugly as hell and not going to be supported on the kernel side"):

explained his reasoning:

解释它的理由:

and, most importantly, designed and implemented the initramfs code.

最重要的是,设计和实现initramfs的代码。

 

Future directions:

未来的方向

------------------

Today (2.6.16), initramfs is always compiled in, but not always used. The kernel falls back to legacy boot code that is reached only if initramfs does not contain an /init program. The fallback is legacy code, there to ensure a smooth transition and allowing early boot functionality to gradually move to "early userspace" (I.E. initramfs).

在当前内核版本(2.6.16)中, initramfs总被编译进去,但并没总被使用。要让内核做到回退到原来的启动代码,只需要initramfs中不包含/init程序。该回退操作是通过旧代码程序来实现的,以此确保一个平滑的过渡,并允许前期的引导功能逐步移至"前期的用户空间"(即initramfs)。

The move to early userspace is necessary because finding and mounting the real root device is complex. Root partitions can span multiple devices (raid or separate journal). They can be out on the network (requiring dhcp, setting a specific MAC address, logging into a server, etc). They can live on removable media, with dynamically allocated major/minor numbers and persistent naming issues requiring a full udev implementation to sort out. They can be compressed, encrypted, copy-on-write, loopback mounted, strangely partitioned,and so on.

移至前期用户空间是必要的,因为寻找和挂载真正的根设备是复杂的。根分区可以跨多个设备(raid or separate journal)。这些设备可在网络上(要求dhcp,设定一个特定MAC地址,登录到一台服务器等)。它们也可以位于可移动媒体,存着动态分配major/minor 编号和持续命名的问题,这要求有一个完整的udev实现来整理这些问题。它们可被压缩、加密、写时拷贝,回环挂载、异常分区等。

This kind of complexity (which inevitably includes policy) is rightly handled in userspace. Both klibc and busybox/uClibc are working on simple initramfs packages to drop into a kernel build.

这种复杂性(必将包含的策略)在用户空间被恰当地处理。klibc和busybox/uClibc两者运行于简单的initramfs软件包上,进行内核的构建。

The klibc package has now been accepted into Andrew Morton's 2.6.17-mm tree.The kernel's current early boot code (partition detection, etc) will probably be migrated into a default initramfs, automatically created and used by the kernel build.

软件包klibc现在已被接受到Andrew Morton's 2.6.17-mm 树中。内核当前的早期启动代码(分区检测等)可能会被迁移到一个默认的initramfs中,由内核构建自动创建并使用。

阅读(4009) | 评论(0) | 转发(0) |
0

上一篇:Rootfs

下一篇:initial RAM disk

给主人留下些什么吧!~~