内存文件系统ramdisk_initramfs
一、内核支持的文件系统
linux支持两种内存根文件系统:ramdisk和initramfs。
内核有3中挂在rootfs的方式:普通的ext/jiffs2/yaffs2, initrd(ramdisk)和initramfs。
Document/early-userspace/README 128 The kernel has currently 3 ways to mount the root filesystem: 129 130 a) all required device and filesystem drivers compiled into the kernel, no 131 initrd. init/main.c:init() will call prepare_namespace() to mount the 132 final root filesystem, based on the root= option and optional init= to run 133 some other init binary than listed at the end of init/main.c:init(). 134 135 b) some device and filesystem drivers built as modules and stored in an 136 initrd. The initrd must contain a binary '/linuxrc' which is supposed to 137 load these driver modules. It is also possible to mount the final root 138 filesystem via linuxrc and use the pivot_root syscall. The initrd is 139 mounted and executed via prepare_namespace(). 140 141 c) using initramfs. The call to prepare_namespace() must be skipped. 142 This means that a binary must do all the work. Said binary can be stored 143 into initramfs either via modifying usr/gen_init_cpio.c or via the new 144 initrd format, an cpio archive. It must be called "/init". This binary 145 is responsible to do all the things prepare_namespace() would do. 146 147 To maintain backwards compatibility, the /init binary will only run if it 148 comes via an initramfs cpio archive. If this is not the case, 149 init/main.c:init() will run prepare_namespace() to mount the final root 150 and exec one of the predefined init binaries.
initramfs可以直接嵌入linux内核中,同内核一起编译。它可以为压缩文件,也可为普通非压缩文件系统。
其位置通过CONFIG_INITRAMFS_SOURCE指定。
注:若指定了CONFIG_INITRAMFS_SOURCE,则系统不会再响应ramdisk,jffs2等根文件系统,
不响应CONFIG_CMDLINE,即使INITRAMFS_SOURCE指定的目录为空或非正确的文件系统。
initramfs文件系统启动时,会在根文件系统中执行第一个init程序,对uboot中传过来的init=/linuxrc不予理睬。
注:busybox中注:不管linuxrc,还是init,都是符号链接,指向/bin/busybox.
二、配置内核支持ramdisk
首先解压内核到工作目录,然后进入内核目录,执行如下语句。
#sudo su
#make menuconfig (如ubuntu下的用户不能执行该命令则先执行如下命令 sudo apt-get install libncurses5-dev)
General setup-->[*] Initial RAM filesystem and RAM disk support
--> [*]Optimize for size
Device Drivers-->Block devices--><*>RAM block device support
(4096)Default RAM disk size (kbytes) //如果你之前制作的ramdisk是8192kb的,则写成8192
File system --> <*> Second extended fs support //提供内核对ext2文件系统的支持
退出保存配置。
三、制作ramdisk
1. BusyBox编译工具,包含bin, sbin, usr, linuxrc.
2. 添加相关重要目录:dev, etc, mnt, proc, sys, lib, var, tmp.
proc, sys为虚拟文件系统,在内存中,创建文件系统时,只要创建空目录即可。
3. dev中添加相关设备文件。
mknod -m 666 ttyS0 c 4 64
mknod -m 666 null c 1 3
mknod -m 666 console c 5 1
mtd0 c 90 0
mtd1 c 90 2
mtd2 c 90 4
mtdblock0 b 31 0
mtdblock1 b 31 1
telnet相关的文件:
ptmx c 5 2
mkdir dev/pts
4. etc目录文件:inittab, init.d/rcS, fstab, profile, group, shadow, passwd.
制作,请参考rootfs下相关文档。
5. 把相关内容保存到rootfs下,通过以下脚本生成rootfs.img.gz(ramdisk).
#!/bin/sh umount ./mnt test -e ./mnt || mkdir ./mnt>/dev/null dd if=/dev/zero of=rootfs.img bs=1024 count=8192 losetup /dev/loop1 rootfs.img echo y | mke2fs -m0 rootfs.img sudo mount -o loop rootfs.img ./mnt echo "create the rootfs.img.gz" sudo cp rootfs/* ./mnt -a sync sudo umount ./mnt gzip rootfs.img -v9
6. 通过uboot下载到内存中,并配置bootargs。
以at91sam9260为例:
tftp 0x21100000 rootfs.img.gz
setenv bootargs mem=64M console=ttyS0,115200 initrd=0x21100000,0x800000 root=/dev/ram0 rw init=/linuxrc
7. boot即可测试ramdisk文件系统。
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步