Linux 檢視initrd內容

2021-10-02 20:58:17 字數 3727 閱讀 3161

1、initrd格式

2.6及以後核心中的initrd.img採用cpio壓縮,不再是2.4核心使用的ext2格式,無法使用mount -o loop 掛載。需要使用gunzip解壓縮,然後再使用cpio解包。

[root@vm_0_11_centos tmp]# cp /boot/initramfs-3.10.0-1062.9.1.el7.x86_64.img ./initrd.img.gz

[root@vm_0_11_centos tmp]# file initrd.img.gz

initrd.img.gz: gzip compressed data, from unix, last modified: sun dec 8 08:35:21 2019, max compression

[root@vm_0_11_centos tmp]# gunzip initrd.img.gz

[root@vm_0_11_centos tmp]# file initrd.img

initrd.img: ascii cpio archive (svr4 with no crc)

[root@vm_0_11_centos tmp]#

2、解包

[root@vm_0_11_centos tmp]# mkdir initrd

[root@vm_0_11_centos tmp]# cd initrd/

[root@vm_0_11_centos initrd]#

[root@vm_0_11_centos initrd]# cpio -ivmd < ../initrd.img

initrd作為臨時根檔案系統作用於作業系統啟動階段,與真正的根檔案系統的結構相似,如下所示。

[root@vm_0_11_centos initrd]# ll

total 44

lrwxrwxrwx 1 root root 7 feb 15 06:30 bin -> usr/bin

drwxr-xr-x 2 root root 4096 feb 15 06:30 dev

drwxr-xr-x 11 root root 4096 feb 15 06:30 etc

lrwxrwxrwx 1 root root 23 feb 15 06:30 init -> usr/lib/systemd/systemd

lrwxrwxrwx 1 root root 7 feb 15 06:30 lib -> usr/lib

lrwxrwxrwx 1 root root 9 feb 15 06:30 lib64 -> usr/lib64

drwxr-xr-x 2 root root 4096 dec 8 08:35 proc

drwxr-xr-x 2 root root 4096 dec 8 08:35 root

drwxr-xr-x 2 root root 4096 dec 8 08:35 run

lrwxrwxrwx 1 root root 8 feb 15 06:30 sbin -> usr/sbin

-rwxr-xr-x 1 root root 3117 aug 8 2019 shutdown

drwxr-xr-x 2 root root 4096 dec 8 08:35 sys

drwxr-xr-x 2 root root 4096 dec 8 08:35 sysroot

drwxr-xr-x 2 root root 4096 dec 8 08:35 tmp

drwxr-xr-x 7 root root 4096 feb 15 06:30 usr

drwxr-xr-x 3 root root 4096 feb 15 06:30 var

[root@vm_0_11_centos initrd]#

3、壓包重新生成initrd

[root@vm_0_11_centos initrd]# find . | cpio -o -h newc > ../initrd.new.img

78919 blocks

[root@vm_0_11_centos initrd]# gzip ../initrd.new.img

4、製作initrd

常用的製作initrd的命令有兩個mkinitrd與dracut,實際上dracut也是在呼叫mkinitrd shell指令碼。

mkinitrd命令依據/lib/modules下的版本生成不同版本的initrd

[root@vm_0_11_centos initrd]# cd /lib/modules

[root@vm_0_11_centos modules]# ll

total 20

drwxr-xr-x 7 root root 4096 dec 8 08:35 3.10.0-1062.9.1.el7.x86_64

drwxr-xr-x 3 root root 4096 dec 8 08:31 3.10.0-1062.el7.x86_64

drwxr-xr-x 7 root root 4096 dec 8 08:35 3.10.0-957.21.3.el7.x86_64

drwxr-xr-x. 3 root root 4096 dec 8 08:32 3.10.0-957.el7.x86_64

drwxr-xr-x 7 root root 4096 dec 9 09:32 5.4.2-1.el7.elrepo.x86_64

[root@vm_0_11_centos modules]# rm -rf /tmp/init*

[root@vm_0_11_centos modules]# mkinitrd /tmp/initrd.img 3.10.0-1062.9.1.el7.x86_64

[root@vm_0_11_centos modules]# ll /tmp/initrd

ls: cannot access /tmp/initrd: no such file or directory

[root@vm_0_11_centos modules]# ll /tmp/initrd*

-rw------- 1 root root 19315059 feb 15 06:45 /tmp/initrd.img

[root@vm_0_11_centos modules]# file /tmp/ini*

/tmp/initrd.img: gzip compressed data, from unix, last modified: sat feb 15 06:45:37 2020, max compression

[root@vm_0_11_centos modules]#

dracut設計上就考慮到了映像尺寸的問題,盡量避免硬編碼,以提高生成的 cpio 映像載入記憶體的速度。 

[root@vm_0_11_centos modules]# dracut /tmp/initrd.img 3.10.0-957.21.3.el7.x86_64

[root@vm_0_11_centos modules]# file /tmp/initrd.img

/tmp/initrd.img: gzip compressed data, from unix, last modified: sat feb 15 06:57:33 2020, max compression

[root@vm_0_11_centos modules]#

Linux 檢視檔案內容

檢視檔案內容的命令 cat 由第一行開始顯示內容,並將所有內容輸出 tac 從最後一行倒序顯示內容,並將所有內容輸出 more 根據視窗大小,一頁一頁的現實檔案內容 less 和more類似,但其優點可以往前翻頁,而且進行可以搜尋字元 head 只顯示頭幾行 tail 只顯示最後幾行 nl 類似於c...

Linux檔案內容檢視

1 直接檢視文章的所有內容 cat tac nl 1 cat 從第一行開始到最後一行將檔案輸出 2 tac 寫法是cat的反寫,輸出內容也是從最後一行到第一行 3 nl 和cat命令類似,但會加上行號 1 more的使用 最後一行顯示文章的內容的百分比 空格鍵 下翻一頁 enter 下翻一行 字元 ...

Linux 檢視檔案內容

1 cat命令 檢視檔案內容 1 cat 檔案掛載點 一次性將全部內容輸出至控制台 命令 cat abentv 選項與引數 a 相當於 vet 的整合選項,可列出一些特殊字元而不是空白而已 b 列出行號,僅針對非空白行做行號顯示,空白行不標行號!e 將結尾的斷行字元 顯示出來 n 列印出行號,連同空...