如何重新打包 initramfs cpio 存档?

如何重新打包 initramfs cpio 存档?

我已经解压了 initramfs cpio 存档,并使用 重新打包它sudo find -depth | sudo cpio -o -H newc > ~/repacked_initramfs.cpio,如下所示:https://salsa.debian.org/kernel-team/initramfs-tools/-/blob/master/mkinitramfs?ref_type=heads#L492

repacked_initramfs.cpio 与原始大小完全相同:

~ $ ls -lA initramfs.cpio repacked_initramfs.cpio
-rw-r--r-- 1 fadedbee fadedbee 227892224 Apr  3 15:27 initramfs.cpio
-rw-r--r-- 1 fadedbee fadedbee 227892224 Apr  8 15:32 repacked_initramfs.cpio

gzip 压缩后的大小略有不同:

root@smarcimx8mq4g:~# ls -lA initramfs.cpio repacked_initramfs.cpio
-rw-r--r-- 1 root root 227892224 Apr  3 14:27 initramfs.cpio
-rw-r--r-- 1 user user 227892224 Apr  8 14:32 repacked_initramfs.cpio

root@smarcimx8mq4g:~# gzip initramfs.cpio

root@smarcimx8mq4g:~# gzip repacked_initramfs.cpio 

root@smarcimx8mq4g:~# ls -lA initramfs.cpio.gz repacked_initramfs.cpio.gz 
-rw-r--r-- 1 root root 68217581 Apr  3 14:27 initramfs.cpio.gz
-rw-r--r-- 1 user user 68212934 Apr  8 14:32 repacked_initramfs.cpio.gz

.img 文件的大小也是如此:

root@smarcimx8mq4g:~# mkimage -A arm64 -O linux -T ramdisk -n "Initial Ram Disk" -d initramfs.cpio.gz initramfs.img
Image Name:   Initial Ram Disk
Created:      Mon Apr  8 15:29:54 2024
Image Type:   AArch64 Linux RAMDisk Image (gzip compressed)
Data Size:    68217581 Bytes = 66618.73 KiB = 65.06 MiB
Load Address: 00000000
Entry Point:  00000000

root@smarcimx8mq4g:~# mkimage -A arm64 -O linux -T ramdisk -n "Initial Ram Disk" -d repacked_initramfs.cpio.gz repacked_initramfs.img
Image Name:   Initial Ram Disk
Created:      Mon Apr  8 15:30:21 2024
Image Type:   AArch64 Linux RAMDisk Image (gzip compressed)
Data Size:    68212934 Bytes = 66614.19 KiB = 65.05 MiB
Load Address: 00000000
Entry Point:  00000000

root@smarcimx8mq4g:~# ls -lA initramfs.img repacked_initramfs.img 
-rw-r--r-- 1 root root 68217645 Apr  8 15:29 initramfs.img
-rw-r--r-- 1 root root 68212998 Apr  8 15:30 repacked_initramfs.img

当我从原始 initramfs 启动时,它会从 initramfs 挂载真正的 rootfs,并且我收到预期的登录提示:

u-boot$ setenv loadinitrd ext4load mmc ${mmcdev}:2 ${initrd_addr} /root/initramfs.img
u-boot$ setenv mmcboot "echo Booting from mmc ...; run mmcargs; if test ${boot_fdt} = yes || test ${boot_fdt} = try; then if run loadfdt; then run loadinitrd ; booti ${loadaddr} ${initrd_addr} ${fdt_addr}; else echo WARN: Cannot load the DT; fi; else echo wait for boot; fi;"
u-boot$ run bootcmd
Setting bus to 0
1050 bytes read in 13 ms (78.1 KiB/s)
Importing environment from mmc (uEnv.txt)...
30075392 bytes read in 1280 ms (22.4 MiB/s)
49035 bytes read in 20 ms (2.3 MiB/s)
Booting from mmc ...
49035 bytes read in 19 ms (2.5 MiB/s)
68217645 bytes read in 2849 ms (22.8 MiB/s)
## Loading init Ramdisk from Legacy Image at 43800000 ...
   Image Name:   Initial Ram Disk
   Image Type:   AArch64 Linux RAMDisk Image (gzip compressed)
   Data Size:    68217581 Bytes = 65.1 MiB
   Load Address: 00000000
   Entry Point:  00000000
   Verifying Checksum ... OK
## Flattened Device Tree blob at 43000000
   Booting using the fdt blob at 0x43000000
   Using Device Tree in place at 0000000043000000, end 000000004300ef8a

Starting kernel ...

[    0.000000] Booting Linux on physical CPU 0x0000000000 [0x410fd034]
...
[    7.655738] Freeing unused kernel memory: 3008K
[    7.669121] Run /init as init process
Loading, please wait...
[    7.715755] udevd[246]: starting version 3.2.9
...
smarcimx8mq4g login:

但通过重新包装的 initramfs 我看到:

u-boot$ setenv loadinitrd ext4load mmc ${mmcdev}:2 ${initrd_addr} /root/repacked_initramfs.img
u-boot$ setenv mmcboot "echo Booting from mmc ...; run mmcargs; if test ${boot_fdt} = yes || test ${boot_fdt} = try; then if run loadfdt; then run loadinitrd ; booti ${loadaddr} ${initrd_addr} ${fdt_addr}; else echo WARN: Cannot load the DT; fi; else echo wait for boot; fi;"
u-boot$ run bootcmd
Setting bus to 0
1050 bytes read in 13 ms (78.1 KiB/s)
Importing environment from mmc (uEnv.txt)...
30075392 bytes read in 1281 ms (22.4 MiB/s)
49035 bytes read in 19 ms (2.5 MiB/s)
Booting from mmc ...
49035 bytes read in 19 ms (2.5 MiB/s)
68212998 bytes read in 2853 ms (22.8 MiB/s)
## Loading init Ramdisk from Legacy Image at 43800000 ...
   Image Name:   Initial Ram Disk
   Image Type:   AArch64 Linux RAMDisk Image (gzip compressed)
   Data Size:    68212934 Bytes = 65.1 MiB
   Load Address: 00000000
   Entry Point:  00000000
   Verifying Checksum ... OK
## Flattened Device Tree blob at 43000000
   Booting using the fdt blob at 0x43000000
   Using Device Tree in place at 0000000043000000, end 000000004300ef8a

Starting kernel ...

[    0.000000] Booting Linux on physical CPU 0x0000000000 [0x410fd034]
...
[    6.868638] Freeing unused kernel memory: 3008K
[    6.880635] Run /init as init process
[    6.884549] Failed to execute /init (error -2)
[    6.889004] Run /sbin/init as init process
[    6.893169] Run /etc/init as init process
[    6.897247] Run /bin/init as init process
[    6.901328] Run /bin/sh as init process
[    6.905221] Kernel panic - not syncing: No working init found.  Try passing init= option to kernel. See Linux Documentation/admin-guide/init.rst for guidance.
[    6.919390] CPU: 3 PID: 1 Comm: swapper/0 Not tainted 5.10.9 #43
[    6.925394] Hardware name: Embedian SMARC-iMX8M Computer on Module HDMI (DT)
[    6.932440] Call trace:
[    6.932617] usb 3-1.2: new high-speed USB device number 3 using xhci-hcd
[    6.934895]  dump_backtrace+0x0/0x1b0
[    6.945239]  show_stack+0x18/0x68
[    6.948556]  dump_stack+0xd8/0x134
[    6.951957]  panic+0x174/0x33c
[    6.955012]  kernel_init+0xfc/0x118
[    6.958500]  ret_from_fork+0x10/0x34
[    6.962078] SMP: stopping secondary CPUs
[    6.966509] Kernel Offset: disabled
[    6.969997] CPU features: 0x0240002,2000200c
[    6.974264] Memory Limit: none
[    6.977324] ---[ end Kernel panic - not syncing: No working init found.  Try passing init= option to kernel. See Linux Documentation/admin-guide/init.rst for guidance. ]---

我重新提取了 repacked-initramfs.cpio 并证明 /init 存在并且具有与 initramfs.cpio 中相同的权限、所有权和 sha1sum。

我可能会犯什么重新打包错误,这会导致内核找不到 /init,尽管它正确存在于 repacked_initramfs.cpio 中?

答案1

我添加LC_ALL=C sort到我的 cpio 管道中,现在内核可以/initrepacked_initramfs.img.

sudo find . | LC_ALL=C sort | sudo cpio -o -H newc > ~/repacked_initramfs.cpio

内核必须期望 initramfs 文件按照其完整路径按字母数字顺序排序。如果“/init”不在适当的位置,内核将无法找到它。

相关内容