• initramfs

    千次阅读 2018-09-03 16:53:45
    initramfs 分类: 嵌入式2014-02-06 18:08:43 ...

    分类: 嵌入式

    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:


    - 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存档


    - 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”.



    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).


    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.


    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).


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


    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:


    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 2from1”
    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;">"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.


    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. 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).


    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.



    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:





    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.


    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



  • initramfs介绍

    2013-09-28 23:14:36
    initramfs与initrd类似,也是初始化好了...但是目前initramfs只支持cpio包格式,它会被populate_rootfs->unpack_to_rootfs(&__initramfs_start, &__initramfs_end - &__initramfs_start, 0)函数(解压缩、)解析、安装。
  • os-initramfs-tools:用于initramfs-tools的包装
  • 解压initramfs

    2020-08-27 09:55:16
    root@localhost:/home/liuyd/Workdir/initramfs# /usr/lib/dracut/skipcpio initramfs-5.7.16-200.fc32.x86_64.img | zcat |cpio -divm root@localhost:/home/liuyd/Workdir/initramfs# ls bin dev etc init ...

    root@localhost:/home/liuyd/Workdir/initramfs# /usr/lib/dracut/skipcpio
    initramfs-5.7.16-200.fc32.x86_64.img | zcat |cpio -divm
    root@localhost:/home/liuyd/Workdir/initramfs# ls bin dev etc init
    initramfs-5.7.16-200.fc32.x86_64.img lib lib64 proc root run
    sbin shutdown sys sysroot tmp usr var

  • 用于initramfs initramfs是Linux内核的可选根文件系统。 有关更多信息,请参见 。 依存关系 带有crc32完整性检查选项的工作中的xz压缩机。 建造 $ cd xbox-linux-initramfs/ $ ./build-initramfs.sh 执行 使用...
  • 用于Linux的简单initramfs脚本 该项目尚未完成,可能无法正常运行。
  • update-initramfs initramfs使用bash更新您的initramfs 先决条件 /usr/src/linux/内核源文件,其中linux是到当前使用的内核目录的符号链接 启用内核设置以与initramfs一起使用。 请查阅以获取更多信息。 已安装以下...
  • initramfs概述

    2017-03-29 11:05:48
    initramfs概述 initramfs与initrd类似,也是初始化好了且存在于ram中的,可以压缩也可以不压缩。但是目前initramfs只支持cpio包格式,它 会被populate_rootfs->unpack_to_rootfs(&__initramfs_start, &__...
    initramfs与initrd类似,也是初始化好了且存在于ram中的,可以压缩也可以不压缩。但是目前initramfs只支持cpio包格式,它 会被populate_rootfs->unpack_to_rootfs(&__initramfs_start, &__initramfs_end - &__initramfs_start, 0)函数(解压缩、)解析、安装。

    (1) Linux内核只认cpio格式的initramfs文件包(因为unpack_to_rootfs只能解析cpio格式文件),非cpio格式的 initramfs文件包将被系统抛弃,而initrd可以是cpio包也可以是传统的镜像(image)文件,实际使用中initrd都是传统镜像文 件。

    (2) initramfs在编译内核的同时被编译并与内核连接成一个文件,它被链接到地址__initramfs_start处,与内核同时被 bootloader加载到ram中,而initrd是另外单独编译生成的,是一个独立的文件,它由bootloader单独加载到ram中内核空间外的 地址,比如加载的地址为addr(是物理地址而非虚拟地址),大小为8MB,那么只要在命令行加入"initrd=addr,8M"命令,系统就可以找到 initrd(当然通过适当修改Linux的目录结构,makefile文件和相关代码,以上两种情况都是可以相通的)。

    (3) initramfs被解析处理后原始的cpio包(压缩或非压缩)所占的空间 (&__initramfs_start - &__initramfs_end)是作为系统的一部分直接保留在系统中,不会被释放掉, 而对于initrd镜像文件,如果没有在命令行中设置"keepinitd"命令,那么initrd镜像文件被处理后其原始文件所占的空间 (initrd_end - initrd_start)将被释放掉。

    (4) initramfs可以独立ram disk单独存在,而要支持initrd必须要先支持ram disk,即要配置CONFIG_BLK_DEV_INITRD选项 -- 支持initrd,必须先要配置CONFIG_BLK_DEV_RAM -- 支持ram disk ,因为initrd image实际就是初始化好了的ramdisk镜像文件,最后都要解析、写入到ram disk设备/dev/ram或/dev/ram0中。
    注: 使用initramfs,命令行参数将不需要"initrd="和"root="命令? YES

    由于initramfs使用cpio包格式,所以很容易将一个单一的文件、目录、node编译链接到系统中去,这样很简单的系统中使用起来很方便,不需要 另外挂接文件系统。
    但是因为cpio包实际是文件、目录、节点的描述语言包,为了描述一个文件、目录、节点,要增加很多额外的描述文字开销,特别是对于目录和节点,本身很小 额外添加的描述文字却很多,这样使得cpio包比相应的image文件大很多。

    General setup  --->
    [*] Initial RAM filesystem and RAM disk (initramfs/initrd) support
    (/rootfs_dir) Initramfs source file(s)   //输入根文件系统的所在目录

    不需要"initrd="和"root="参数,但是必须在initramfs中创 建/init文件或者修改内核启动最后代码(init 文件是软连接,指向什么? init -> bin/busybox,否则内核启动将会失败)


    使用initrd的内核配置(使用网口将根文件系统下载到RAM --
    tftp addr ramdisk.gz):
    1. 配置initrd
    General setup  --->
    [*] Initial RAM filesystem and RAM disk (initramfs/initrd) support
    () Initramfs source file(s)   //清空根文件系统的目录配置

    2. 配置ramdisk
    Device Drivers  --->  
    Block devices  --->
    <*> RAM disk support
    (16)  Default number of RAM disks   // 内核在/dev/目录下生成16个ram设备节点
    (4096) Default RAM disk size (kbytes)
    (1024) Default RAM disk block size (bytes)

    使用 initrd的内 核启动参数:
    initrd=addr,0x400000 root=/dev/ram rw
    (1) addr是根文件系统的下载地址;
    (2) 0x400000是根文件系统的大小,该大小需要和内核配置的ramdisk size 4096 kbytes相一致;
    (3) /dev/ram是ramdisk的设备节点,rw表示根文件系统可读、可写;

    1. 内核启动参数不需要"initrd="(也可以写成"noinitrd");
    root=/dev/mtdblock2 (/dev/mtdblock2 -- 根文件系统所烧写的FLASH分区)

    2. 内核配置不需要ram disk;也不需要配置
    initramfs 或者initrd
    [ ] Initial RAM filesystem and RAM disk (initramfs/initrd) support

    注: boot的FLASH分区要和kernel的FLASH分区匹配(而非一致),需要进一步解释。

    TCP reno registered 停在这里

    root@zengxiaolong:/home/zengxiaolong/soft/s3c2410/rootfs# du -h
    4.0K    ./dev
    4.0K    ./tmp
    8.0K    ./etc/init.d
    40K    ./etc
    4.0K    ./usr
    4.0K    ./var
    4.0K    ./sbin
    2.1M    ./bin
    4.0K    ./sys
    116K    ./lib/modules
    2.8M    ./lib
    4.0K    ./proc
    4.9M    .

    initrd and initramfs
    # General setup
    # UBI - Unsorted block images

    Installation initramfs

    No special installation like with initrd is necessary. The initramfs is already in the kernel. If you start the kernel, the initramfs is already there. Therefore, there is no root=/dev/ram0 rw initrd=0x87000000,8M bootargs option necessary. Remove this if you still have it!
    <script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
    阅读(551) | 评论(0) | 转发(0) |
  • initramfs图像制作器 使用busybox制作initramfs图像的简单脚本。 建立Linux映像 获取资源 git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 编译Linux 进行默认配置 复制当前配置 cp /...
  • 适用于android initramfs的简单LVGL工具 这是一堆狗屎代码。
  • Linux initramfs

    2016-04-02 10:10:39
    linux开机进不去图形界面,停留在initramfs界面:~ initramfs: exit # 网上问度娘后,执行支招exit提示:... /dev/sda1 unexpected inconsistency, run fsck manually于是乎~~~# 按照提示执行以下命令:fsck -y...


    ~ initramfs: exit
    # 网上问度娘后,执行支招exit提示:
    /dev/sda1 unexpected inconsistency, run fsck manually


    # 按照提示执行以下命令:
    fsck -y -t ext4 /dev/sda1

    等待 ing…, It’s OK.

    Tip: fsck命令


  • initramfs解包打包

    2021-02-24 19:56:32
    mv centos8-arm64.initramfs centos8-arm64.initramfs.gz gzip -d centos8-arm64.initramfs.gz cpio -idm < centos8-arm64.initramfs 打包 find . | cpio -o -H newc | gzip > ../centos8-arm64....
  • dropbear_initramfs 使用debops-contrib.dropbear_initramfs角色,可以在使用作为SSH服务器挂载根文件系统之前,设置对initramfs的SSH访问。 这可用于通过SSH远程解锁全盘加密主机。 安装 该角色至少需要Ansible v...
  • 下面来介绍一下initramfs initramfs是编到内核中的一种根文件系统,使用的是cpio格式,处理流程和我们之前介绍的initrd机制的cpio格式的流程几乎一样, 只不过这个是编译到内核里的,需要用到时内核会自行解压使用。...
  • Linux-initramfs

    2019-07-02 14:16:00
    解决方案2.1 ramdisk(比如initrd)2.2 tmpfs(比如initramfs)2.3 ramdisk vs ramfs2.4 临时文件系统2.4.1 观察tmpfs2.4.2 Initramfs2.4.3 查看initramfs2.4.4 创建initramfs2.5 Linux系统中的initramfs2.6 cpio归档...
  • Easy LFS initramfs-开源

    2021-05-13 01:00:35
    initramfs是针对从头开始基于linux的人使用的最新内核。 您的linux将非常了解大多数硬件,并且只需下载然后解压缩并安装即可平稳运行。
  • Initramfs arrives

    2014-11-06 21:58:57
    Initramfs arrives The "initramfs" concept has been in the 2.5 plans since back before there was a 2.5 kernel. Things have been very quiet on the initramfs front, however, until the ...
  • Ubuntu initramfs问题

    千次阅读 2016-09-05 15:03:54
    Ubuntu initramfs异常修复
  • initramfs_custom 这些是为gentoo创建自定义initramfs时所需的ebuild文件。 以后会添加更多
  • linux initramfs启动原理

    千次阅读 2019-11-23 21:32:55
    当linux选择支持initramfs方式启动,并且在initramfs source file中选择了要打包的rootfs路径以后,则会尝试以initramfs方式启动。initramfs方式会对rootfs进行压缩,和linux kernel打包在同一个镜像文件中。然后...
  • 我的最小Linux 仅具有vmlinuz和initramfs的可运行linux(rootfs嵌入initramfs中) 如何克隆此存储库 git clone --recurse-submodules https://github.com/NobodyXu/my-minimal-linux



1 2 3 4 5 ... 20
收藏数 19,960
精华内容 7,984