打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
linux initrd 制作及使用

一,       Ramdisk

Ram Disk就是将内存中的一块区域作为物理磁盘来使用的一种技术。

使用时内核配置:两个选项:第一个设定Ramdisk个数,默认16个;第二个是设定Ramdisk的大小,设定16M

DeviceDrivers --->

[*] Blockdevices --->

<*>  RAM block device support

(16)   Default number of RAM disks

(16384) Default RAM disk size(kbytes)

另外:设置RAM disk支持

Generalsetup  --->

[*] Initial RAM filesystem and RAM disk(initramfs/initrd) support

 

二,       initrd

initrd全称是 initial RAMdisk,它提供一种让核心可以简单使用Ramdisk的能力,简单的说,这些能力包括:

格式化一个Ramdisk;

加载文件系统内容到Ramdisk;

将Ramdisk作为根文件系统;

 

而Linux启动阶段的Ramdisk相当于一个未格式化的硬盘分区,核心可以直接将initrd的内容释放到一个未初始化的Ramdisk里,这个过程与Ghost恢复一个分区的过程十分相似。于是,相应的内容被加载到相应的Ramdisk中,同时,这个Ramdisk也被格式化成某种由initrd格式所表达的分区格式。

 

initrd支持的格式

· Romfs文件系统;

· Ext2文件系统;

· cramfs文件系统;

· minix文件系统;

 

三,       Gzip支持

如果核心选择了Gzip支持(通常这是默认的,在init/do_mounts_rd.c中定义的BUILD_CRAMDISK宏)还可以使用Gzip压缩的initrd。相关的代码可以在核心源码drivers/block/rd.c:identify_ramdisk_image中找到。

 

四,       制作initrd

initrd有两种格式,initrd-image和initrd-cpio。

办法一

通过ramdisk来制作的方法比较简单(以ext2文件系统为例):
redice # mkfs.ext2 /dev/ram0
redice # mount /dev/ram0 /mnt/rd
redice # cp _what_you_like_ /mnt/rd #把需要的文件复制过去
redice # dd if=/dev/ram0 of=/tmp/initrd
redice # gzip -9/tmp/initrd
这个过程也最能够解释initrd的本质,对于Linux来说,Ramdisk的一个块设备,而initrd是这个块设备上所有内容的“克隆”(由
命令dd来完成)而生成的文件。核心中加载initrd相关的代码则用于完成将相反的过程,即将这一个文件恢复到Ramdisk中去。

办法二

通过loop设备来制作initrd的过程:
redice # dd if=/dev/zero of=/tmp/initrd bs=1024 count=4096 #制作一个4M的空白文件
redice # losetup /dev/loop0 /tmp/initrd #映射到loop设备上;
redice # mkfs.ext2 /dev/loop0 #创建文件系统;
redice # mount /dev/loop0 /mnt/rd
redice # cp _what_you_like_ /mnt/rd #复制需要的文件;
redice # umount /mnt/rd
redice # losetup -d /dev/loop0
redice # gzip -9 /tmp/initrd

办法三

bash# dd if=/dev/zeroof=../initrd.img bs=512k count=5
bash# mkfs.ext2 -F -m0 ../initrd.img
bash# mount -t ext2 -o loop../initrd.img   /mnt
bash# cp -r   * /mnt
bash# umount /mnt
bash# gzip -9 ../initrd.img

 

办法四(新式INITRD:cpio-initrd的制作)

# find . | cpio -c -o > ../initrd.img
# gzip ../initrd.img
解压initrd
gunzip initrd.img.gz
即使您的 initrd 映像文件不是以 .gz 结尾,它也可能是一个压缩文件,
可以给这个文件添加上 .gz后缀,然后再使用 gunzip 对其进行解压。

cpio 归档文件
# 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 -ivmd < initrd-2.6.14.2.img

五,       启动:

Redboot启动。

下载initrd

0x01000000-0x01258831

下载zImage

0x00100000-0x002f82f7

启动命令

exec -r 0x1000000 -s 0x258831 -c"root=/dev/ram rw console=ttymxc0,115200"

六,       结果:

办法三制作,也就是initrd-image可以成功启动:

Kernel command line: root=/dev/ram rwconsole=ttymxc0,115200

checking if image is initramfs...itisn't (no cpio magic); looks like an initrd

Freeing initrd memory: 2402K

RAMDISK: Compressed image found at block0

RAMDISK: ran out of compresseddata

out of input data

VFS: Mounted root (ext2filesystem).

Freeing init memory: 140K

Mounting /proc and /sys

Starting the hotplug events dispatcherudevd

Synthesizing initial hotplugevents

Setting the hostname toaigo_R&D

Mounting filesystems

initrd-cpio无法启动:

Kernel command line: root=/dev/ram rwconsole=ttymxc0,115200

checking if image is initramfs...itisn't (out of input data); looks like an initrd

Freeing initrd memory: 2376K

RAMDISK: Compressed image found at block0

RAMDISK: ran out of compresseddata

out of input data

List of all partitions:

1f00           2048 mtdblock0 (driver?)

1f01           4096 mtdblock1 (driver?)

1f02           2048 mtdblock2 (driver?)

1f03          16384 mtdblock3 (driver?)

1f04           8192 mtdblock4 (driver?)

1f05           4096 mtdblock5 (driver?)

1f06         786432 mtdblock6 (driver?)

1f07        3371008 mtdblock7 (driver?)

No filesystem could mount root,tried:  ext2 cramfs vfatmsdos

Kernel panic - not syncing: VFS: Unableto mount root fs on unknown-block(1,0)

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
initrd镜像文件的作用与制作
Linux内核Ramdisk(initrd)机制
Linux2.4内核和2.6内核对Initrd处理流程
详解linux内存磁盘初始化技术
解析linux根文件系统的挂载过程
initrd.img
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服