查wiki的initrd有很好的說明
這裡提供最簡易的認知
Linux系統開機時,除了載入kernel,還需掛載根檔案系統(rootfs),
由於根檔案系統並不一定能很容易的被掛載,(例如rootfs不位在本機硬碟,需要使用其它工具才能從遠端掛載它;又例如你的rootfs在特殊的儲存裝置,需要先載入它的driver才能正常使用)。
為了提供足夠掛載真正 rootfs 能力,所以設計成兩階段掛載rootfs,先掛載容易取得的 initrd檔為rootfs,有了足夠的能力,再掛載真正的rootfs,這樣的設計就很有彈性。
當然initrd檔要放在系統很容易就能存取到的地方,例如kernel有支援的硬碟中的一個檔案。
initrd檔案有兩種:
1. (old) Ram disk image : (optionally gzip-compressed) file system image (例如一個 ext2 file system 的壓縮檔)
2. (new) Initramfs image: gzip-compressed cpio archive (直接將開機所需檔丟給 cpio,再經 gzip 壓縮)
kernel能自行判斷是哪一種 initrd 檔,成功解壓縮形成在 ram 中的 rootfs。如果是 Ram disk image,就會把原本的 file system (如ext2) 還原成 ram disk;如果是 initramfs image,則會使用它cpio中包含的檔案形成 tmpfs file system(或是較舊的 ramfs file system)
雖然也是在 ram 中運作,但 tmpfs 並不是 ram disk,ram disk 指的是把一般的 disk file system 放到 ram 中運作,而 tmpfs就是設計在ram中運作,並不是 disk file system,也比它有效率。
My note on creating initramfs:
To extract initramfs:
gunzip < /boot/initrd.img-2.6.28-11-generic | cpio -i -d -H newc --no-absolute-filenames
To create initramfs:
cd myinitrd/
find . | cpio -o -H newc | gzip > ../myinitrd.gz
Note 1: Makesure all files in myinitrd/ are belong to root (chown -R 0:0 *). Otherwise, init script may fail to mount proc and show you "permission denied (are you root )" errors.
Note 2: use gzip -9 ... may lead to kernel panic "invalid compressed format" when booting !! Guess it's because some vmlinuz doesn't support that compression level.
Reference:
深入理解 Linux 2.6 的 initramfs 機制 (上)
Introducing initramfs, a new model for initial RAM disks
how to create an initramfs image
https://www.kernel.org/doc/Documentation/initrd.txt
https://www.kernel.org/doc/Documentation/initrd.txt
沒有留言:
張貼留言