initramfs开不了机_linux系统安装教程

initramfs开不了机_linux系统安装教程initramfs分类:嵌入式2014-02-0618:08:43…_config_initramfs_source源文件tar.gz

initramfs开不了机_linux系统安装教程

分类: 嵌入式

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 2from 2 f r o m 1”
  12. (cd “1″</span><span style=”color:#0000CC;”>;</span> find <span style=”color:#0000CC;”>.</span> <span style=”color:#0000CC;”>|</span> cpio <span style=”color:#0000CC;”>-</span>o <span style=”color:#0000CC;”>-</span>H newc <span style=”color:#0000CC;”>|</span> gzip<span style=”color:#0000CC;”>)</span> <span style=”color:#0000CC;”>&gt;</span> <span style=”color:#FF00FF;”>” 1″</span><span style=”color:#0000CC;”>;</span> find <span style=”color:#0000CC;”>.</span> <span style=”color:#0000CC;”>|</span> cpio <span style=”color:#0000CC;”>-</span>o <span style=”color:#0000CC;”>-</span>H newc <span style=”color:#0000CC;”>|</span> gzip<span style=”color:#0000CC;”>)</span> <span style=”color:#0000CC;”>&gt;</span> <span style=”color:#FF00FF;”>” 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根文件系统。如果你还不熟悉共享库、设备和路径,你需要获得一个小的根文件系统来启动和运行,这里有一些参考资料:

http://www.tldp.org/HOWTO/Bootdisk-HOWTO/

http://www.tldp.org/HOWTO/From-PowerUp-To-Bash-Prompt-HOWTO.html

http://www.linuxfromscratch.org/lfs/view/stable/

The “klibc” package (http://www.kernel.org/pub/linux/libs/klibc) 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”(http://www.kernel.org/pub/linux/libs/klibc)被设计为一个极小的C库来静态连接早期的用户空间代码,并附带着一些相关的工具。它是BSD授权。

I use uClibc (http://www.uclibc.org) and busybox (http://www.busybox.net) myself. These are LGPL and GPL, respectively. (A self-contained initramfs package is planned for the busybox 1.3 release.)

我使用uClibc (http://www.uclibc.org)和busybox (http://www.busybox.net)。这些分别是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  (www.qemu.org) or User Mode Linux, like so:

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

先执行命令:

cat > hello.c << EOF

然后输入以下内容:

#include

今天的文章initramfs开不了机_linux系统安装教程分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/71638.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注