Titan Tang's Blog

Titan Tang's Blog

Initrd的man文档

NAME initrd - boot loader initialized RAM disk CONFIGURATION The /dev/initrd is a read-only block device assigned major number 1 and minor number 250. Typically /dev/initrd is owned by root.disk with mode 0400 (read access by root only). If the Linux system does not have /dev/initrd already created, it can be created with the following commands:

mknod -m 400 /dev/initrd b 1 250
chown root:disk /dev/initrd

Also, support for both “RAM disk” and “Initial RAM disk” (e.g. CONFIG_BLK_DEV_RAM=y and CONFIG_BLK_DEV_INITRD=y) must be compiled directly into the Linux kernel to use /dev/initrd. When using /dev/initrd, the RAM disk driver cannot be loaded as a module. DESCRIPTION The special file /dev/initrd is a read-only block device. This device is a RAM disk that is initialized (e.g., loaded) by the boot loader before the kernel is started. The kernel then can use /dev/initrd’s contents for a two-phase system boot-up. In the first boot-up phase, the kernel starts up and mounts an initial root file-system from the contents of /dev/initrd (e.g., RAM disk initialized by the boot loader). In the second phase, additional drivers or other modules are loaded from the initial root device’s contents. After loading the additional modules, a new root file system (i.e., the normal root file system) is mounted from a different device. Boot-up Operation When booting up with initrd, the system boots as follows: 1. The boot loader loads the kernel program and /dev/initrd’s contents into memory. 2. On kernel startup, the kernel uncompresses and copies the contents of the device /dev/initrd onto device /dev/ram0 and then frees the memory used by /dev/initrd. 3. The kernel then read-write mounts the device /dev/ram0 as the initial root file system. 4. If the indicated normal root file system is also the initial root file-system (e.g. /dev/ram0) then the kernel skips to the last step for the usual boot sequence. 5. If the executable file /linuxrc is present in the initial root file-system, /linuxrc is executed with UID 0. (The file /linuxrc must have executable permission. The file /linuxrc can be any valid executable, including a shell script.) 6. If /linuxrc is not executed or when /linuxrc terminates, the normal root file system is mounted. (If /linuxrc exits with any file-systems mounted on the initial root file-system, then the behavior of the kernel is UNSPECIFIED. See the NOTES section for the current kernel behavior.) 7. If the normal root file system has a directory /initrd, the device /dev/ram0 is moved from / to /initrd. Otherwise if the directory /initrd does not exist, the device /dev/ram0 is unmounted. (When moved from / to /initrd, /dev/ram0 is not unmounted and therefore processes can remain running from /dev/ram0. If directory /initrd does not exist on the normal root file system and any processes remain running from /dev/ram0 when /linuxrc exits, the behavior of the kernel is UNSPECIFIED. See the NOTES section for the current kernel behavior.) 8. The usual boot sequence (e.g., invocation of /sbin/init) is performed on the normal root file system. 前几天老许问我关于initrd的作用,说起来只知道是放驱动程序用的磁盘镜像,我的理解就是放了一些linux要用的基本目录,加上/lib/modules里面跟启动关系紧密的驱动程序,但是完全不懂里面到底是什么东西,所以今天想起来,在ubuntu上面做了一下实验,注意,这个initrd文件在不同的发行版上面使用不太一样的格式存储,所以要区别对待

mkdir /tmp/initrd
cd /tmp/initrd
gzip -d /boot/initrd.img-2.6.32-24-generic
file initrd.img #看到是cpio文件
cpio -i < initrd.img

到这里就解压出来了镜像里面的全部内容,ls一下可以看到目录结构包含了 bin conf etc init lib sbin scripts var, 其中init是个可执行文件,应该就是我们的1号进程。这里有点疑问,我比较了一下这个init和/sbin/init,发现并不是同一个文件,再结合上面initrd的man文档的第6步解释,猜想这个文件一般不会被执行,因为在这里会挂上我们的根文件系统以后执行/sbin/init了 然后我看了一下这个目录下面lib/modules下面果然有这个版本内核的驱动程序,总共有14M大小,而我/lib/modules下面同一个内核的驱动程序模块为89M,少了这么多,我大概看了一下内容,虽然不是很了解每个模块的作用,但是看名字,感觉是内核启动时可能会需要用到的驱动程序,比如磁盘,网络支持等功能。比如摄像头驱动啥的这里就都没有,所以小了不少,反正这些在启动阶段用不到的。

Comments