ILD

initramfs
作者:Yuan Jianpeng 邮箱:yuanjp89@163.com
发布时间:2020-1-13 站点:Inside Linux Development

initramfs是内存文件系统。但是和initrd。initrd(init ramdisk)是用内存模拟一块磁盘。在模拟的磁盘上建立一种真实的文件系统(如ext3),initrd需要文件系统的驱动模块。但是initramfs不需要文件系统驱动,是一种tmpfs。


initramfs的启动路径存储在ramdisk_execute_command,使用rdinit=/sbin/init,命令行参数来设置这个变了。没有rdinit设置时,默认为/init。内核会检查ramdisk_execute_command这个路径的文件是否存在,如果不存在,则尝试重新寻找rootfs。


initramfs可以嵌入到内核中,也可以外挂。外挂的时候,只能通过bootloader修改device tree的方式来设置initramfs的内存位置。


使用fit格式的固件,可以将内核镜像、dtb、initramfs存储到一个固件中。


initramfs的相关内核选项:

CONFIG_BLK_DEV_INITRD,开启Initial RAM filesystem and RAM disk的支持。

CONFIG_INITRAMFS_SOURCE,内嵌initramfs的路径,可以是目录,也可以是打包好的cpio文档。

以及一些压缩选项,如:CONFIG_RD_XZ


注意cpio里面的文档,必须严格按照顺序存放,比如必须先放置目录项,再放置里面的文件,否则解压的时候没有事先创建目录,导致解压失败。


编译fit的its的典型配置:

/dts-v1/;

/ {
        description = "X-router Fit Image";
        #address-cells = <1>;

        images {
                kernel {
                        description = "OpenWrt Linux kernel";
                        data = /incbin/("linux-4.14.120/arch/arm/boot/zImage");
                        type = "kernel";
                        arch = "arm";
                        os = "linux";
                        compression = "none";
                        load = <0x80208000>;
                        entry = <0x80208000>;
                        hash-1 {
                                algo = "sha1";
                        };
                };
                fdt {
                        description = "Flattened Device Tree blob";
                        data = /incbin/("linux-4.14.120/arch/arm/boot/dts/qcom-ipq4019-ap.dk01.1-c1.dtb");
                        type = "flat_dt";
                        arch = "arm";
                        compression = "none";
                        hash-1 {
                                algo = "sha1";
                        };
                };
                initramfs {
                        description = "Compressed Initramfs";
                        data = /incbin/("initramfs.cpio.xz");
                        type = "ramdisk";
                        arch = "arm";
                        os = "linux";
                        compression = "none";
                        load = <0x80208000>;
                        entry = <0x80208000>;
                        hash-1 {
                                algo = "sha1";
                        };
                };
        };

        configurations {
                default = "config@1";
                config@1 {
                        description = "IPQ4018 Linux kernel with FDT blob and initramfs";
                        kernel = "kernel";
                        fdt = "fdt";
                        ramdisk = "initramfs";
                };
        };
};


参考:

https://developer.toradex.com/knowledge-base/initramfs-and-tmpfs

https://blog.csdn.net/androidstar_cn/article/details/53165941

https://www.kernel.org/doc/html/latest/driver-api/early-userspace/early_userspace_support.html

https://github.com/siemens/u-boot/tree/master/doc/uImage.FIT



Copyright © insidelinuxdev.net 2017-2021. Some Rights Reserved.