博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux initial RAM disk (initrd) overview
阅读量:4136 次
发布时间:2019-05-25

本文共 6111 字,大约阅读时间需要 20 分钟。

 

 

The initial RAM disk (initrd) is an initial root file system that is mounted prior to when the real root file system is available. The initrd is bound to the kernel and loaded as part of the kernel boot procedure. The kernel then mounts this initrd as part of the two-stage boot process to load the modules to make the real file systems available and get at the real root file system.

The initrd contains a minimal set of directories and executables to achieve this, such as the insmod tool to install kernel modules into the kernel.

In the case of desktop or server Linux systems, the initrd is a transient file system. Its lifetime is short, only serving as a bridge to the real root file system. In embedded systems with no mutable storage, the initrd is the permanent root file system. This article explores both of these contexts.

The initrd image contains the necessary executables and system files to support the second-stage boot of a Linux system.

Depending on which version of Linux you're running, the method for creating the initial RAM disk can vary. Prior to Fedora Core 3, the initrd is constructed using the loop device. The loop device is a device driver that allows you to mount a file as a block device and then interpret the file system it represents. The loop device may not be present in your kernel, but you can enable it through the kernel's configuration tool (make menuconfig) by selecting Device Drivers > Block Devices > Loopback Device Support. You can inspect the loop device as follows (your initrd file name will vary):

# mkdir temp ; cd temp# cp /boot/initrd.img.gz .# gunzip initrd.img.gz# mount -t ext -o loop initrd.img /mnt/initrd# ls -la /mnt/initrd#

You can now inspect the /mnt/initrd subdirectory for the contents of the initrd. Note that even if your initrd image file does not end with the .gz suffix, it's a compressed file, and you can add the .gz suffix to gunzip it.

Beginning with Fedora Core 3, the default initrd image is a compressed cpio archive file. Instead of mounting the file as a compressed image using the loop device, you can use a cpio archive. To inspect the contents of a cpio archive, use the following commands:

# mkdir temp ; cd temp# cp /boot/initrd-2.6.14.2.img initrd-2.6.14.2.img.gz# gunzip initrd-2.6.14.2.img.gz# cpio -i --make-directories < initrd-2.6.14.2.img#

The result is a small root file system, as shown in Listing 3. The small, but necessary, set of applications are present in the ./bin directory, including nash (not a shell, a script interpreter), insmod for loading kernel modules, and lvm (logical volume manager tools).

# ls -la#drwxr-xr-x  10 root root    4096 May 7 02:48 .drwxr-x---  15 root root    4096 May 7 00:54 ..drwxr-xr-x  2  root root    4096 May 7 02:48 bindrwxr-xr-x  2  root root    4096 May 7 02:48 devdrwxr-xr-x  4  root root    4096 May 7 02:48 etc-rwxr-xr-x  1  root root     812 May 7 02:48 init-rw-r--r--  1  root root 1723392 May 7 02:45 initrd-2.6.14.2.imgdrwxr-xr-x  2  root root    4096 May 7 02:48 libdrwxr-xr-x  2  root root    4096 May 7 02:48 loopfsdrwxr-xr-x  2  root root    4096 May 7 02:48 proclrwxrwxrwx  1  root root       3 May 7 02:48 sbin -> bindrwxr-xr-x  2  root root    4096 May 7 02:48 sysdrwxr-xr-x  2  root root    4096 May 7 02:48 sysroot#

Of interest in Listing 3 is the init file at the root. This file, like the traditional Linux boot process, is invoked when the initrd image is decompressed into the RAM disk. We'll explore this later in the article.

The cpio command

Using the cpio command, you can manipulate cpio files. Cpio is also a file format that is simply a concatenation of files with headers. The cpio file format permits both ASCII and binary files. For portability, use ASCII. For a reduced file size, use the binary version.

Let's now go back to the beginning to formally understand how the initrd image is constructed in the first place. For a traditional Linux system, the initrd image is created during the Linux build process. Numerous tools, such as mkinitrd, can be used to automatically build an initrd with the necessary libraries and modules for bridging to the real root file system. The mkinitrd utility is actually a shell script, so you can see exactly how it achieves its result. There's also the YAIRD (Yet Another Mkinitrd) utility, which permits customization of every aspect of the initrd construction.

Because there is no hard drive in many embedded systems based on Linux, the initrd also serves as the permanent root file system. Listing 4 shows how to create an initrd image. I'm using a standard Linux desktop so you can follow along without an embedded target. Other than cross-compilation, the concepts (as they apply to initrd construction) are the same for an embedded target.

#!/bin/bash# Housekeeping...rm -f /tmp/ramdisk.imgrm -f /tmp/ramdisk.img.gz# Ramdisk ConstantsRDSIZE=4000BLKSIZE=1024# Create an empty ramdisk imagedd if=/dev/zero of=/tmp/ramdisk.img bs=$BLKSIZE count=$RDSIZE# Make it an ext2 mountable file system/sbin/mke2fs -F -m 0 -b $BLKSIZE /tmp/ramdisk.img $RDSIZE# Mount it so that we can populatemount /tmp/ramdisk.img /mnt/initrd -t ext2 -o loop=/dev/loop0# Populate the filesystem (subdirectories)mkdir /mnt/initrd/binmkdir /mnt/initrd/sysmkdir /mnt/initrd/devmkdir /mnt/initrd/proc# Grab busybox and create the symbolic linkspushd /mnt/initrd/bincp /usr/local/src/busybox-1.1.1/busybox .ln -s busybox ashln -s busybox mountln -s busybox echoln -s busybox lsln -s busybox catln -s busybox psln -s busybox dmesgln -s busybox sysctlpopd# Grab the necessary dev filescp -a /dev/console /mnt/initrd/devcp -a /dev/ramdisk /mnt/initrd/devcp -a /dev/ram0 /mnt/initrd/devcp -a /dev/null /mnt/initrd/devcp -a /dev/tty1 /mnt/initrd/devcp -a /dev/tty2 /mnt/initrd/dev# Equate sbin with binpushd /mnt/initrdln -s bin sbinpopd# Create the init filecat >> /mnt/initrd/linuxrc << EOF#!/bin/ashechoecho "Simple initrd is active"echomount -t proc /proc /procmount -t sysfs none /sys/bin/ash --loginEOFchmod +x /mnt/initrd/linuxrc# Finish up...umount /mnt/initrdgzip -9 /tmp/ramdisk.imgcp /tmp/ramdisk.img.gz /boot/ramdisk.img.gz

转载地址:http://gemvi.baihongyu.com/

你可能感兴趣的文章
rootkit related
查看>>
mysql-应用案例-视图的应用
查看>>
什么是虚拟机?
查看>>
Microsoft Visual C++ 和 Borland C++ Builder 之比较
查看>>
C++ Builder 中定时器的应用
查看>>
Notepad++ 是程序员的必备利器之一
查看>>
Source Insight : 程序员最得心应手的代码阅读和编辑工具(高效)
查看>>
如何用C语言获取系统的用户登录名?
查看>>
如何用C语言获取系统的sid信息?
查看>>
C/C++中如何写长串(字符数组的拼接)?
查看>>
strtok函数真是个蹩脚而又恶心的设计(千万不要嵌套使用strtok函数)
查看>>
Windows和Linux的netstat
查看>>
C++如何实现string的trim功能? (已经包含trimLeft和trimRight)
查看>>
如何确保一个函数的被调用次数不少于另外一个函数的被调用次数?
查看>>
从netstat看网络编程
查看>>
ssh, telnet在发起什么连接请求?
查看>>
利用STL中的map来写一个自己的命令行界面
查看>>
127.0.0.1和0.0.0.0
查看>>
什么是抓包?为什么要抓包?
查看>>
第一年的年终奖
查看>>