如何重新归档 initramfs cpio,而不丢失硬链接?

如何重新归档 initramfs cpio,而不丢失硬链接?

我有一个 Debian initramfs,它:

  • 我解压缩为:
~/my_initramfs $ sudo cpio -idmBv < ../initramfs.cpio
.
bin
conf
conf/arch.conf
...
cpio: usr/sbin/watchdog linked to usr/bin/[
cpio: usr/sbin/watchdog linked to usr/bin/[[
cpio: usr/sbin/watchdog linked to usr/bin/ar
...
cpio: usr/sbin/watchdog linked to usr/sbin/vconfig
usr/sbin/watchdog
44511 blocks
  • 确认其中包含 busybox 的 247 个硬链接:
~/my_initramfs $ ls -lA bin/* | head
-rwxr-xr-x 247 root root 764552 Jul 25  2021 bin/[
-rwxr-xr-x 247 root root 764552 Jul 25  2021 bin/[[
-rwxr-xr-x 247 root root 764552 Jul 25  2021 bin/ar
-rwxr-xr-x 247 root root 764552 Jul 25  2021 bin/arch
-rwxr-xr-x 247 root root 764552 Jul 25  2021 bin/ash
-rwxr-xr-x 247 root root 764552 Jul 25  2021 bin/awk
-rwxr-xr-x 247 root root 764552 Jul 25  2021 bin/basename
-rwxr-xr-x 247 root root 764552 Jul 25  2021 bin/bc
-rwxr-xr-x 247 root root 764552 Jul 25  2021 bin/blkdiscard
-rwxr-xr-x 247 root root 764552 Jul 25  2021 bin/bunzip2
  • 并重新存档
~/my_initramfs $ sudo find . -print | sudo cpio -ovB > ~/my_initramfs.cpio
.
./run
./usr
./usr/bin
...
./sbin
./init
81237 blocks

新档案的大小现在是原始档案的两倍。

~/my_initramfs $ ls -lA ../*initramfs.cpio
-rw-r--r-- 1 fadedbee fadedbee 227892224 Apr  3 15:27 ../initramfs.cpio
-rw-r--r-- 1 radedbee fadedbee 415933440 Apr  8 11:03 ../my_initramfs.cpio

查看两个 cpio 存档的内容,看起来新存档有许多(名称不同)busybox 副本:

~/my_initramfs $ cpio -itv < ../initramfs.cpio | grep "bin/a"
-rwxr-xr-x 247 root     root            0 Jul 25  2021 usr/sbin/arping
-rwxr-xr-x 247 root     root            0 Jul 25  2021 usr/sbin/arp
-rwxr-xr-x 247 root     root            0 Jul 25  2021 usr/sbin/adjtimex
-rwxr-xr-x 247 root     root            0 Jul 25  2021 usr/sbin/acpid
445102 blocks
-rwxr-xr-x 247 root     root            0 Jul 25  2021 usr/bin/awk
-rwxr-xr-x 247 root     root            0 Jul 25  2021 usr/bin/ash
-rwxr-xr-x 247 root     root            0 Jul 25  2021 usr/bin/arch
-rwxr-xr-x 247 root     root            0 Jul 25  2021 usr/bin/ar

~/my_initramfs $ cpio -itv < ../my_initramfs.cpio | grep "bin/a"
-rwxr-xr-x 247 root     root       764552 Jul 25  2021 usr/bin/arch
-rwxr-xr-x 247 root     root       764552 Jul 25  2021 usr/bin/ash
-rwxr-xr-x 247 root     root       764552 Jul 25  2021 usr/bin/ar
-rwxr-xr-x 247 root     root       764552 Jul 25  2021 usr/bin/awk
-rwxr-xr-x 247 root     root       764552 Jul 25  2021 usr/sbin/acpid
-rwxr-xr-x 247 root     root       764552 Jul 25  2021 usr/sbin/adjtimex
812365 blocks
-rwxr-xr-x 247 root     root       764552 Jul 25  2021 usr/sbin/arping
-rwxr-xr-x 247 root     root       764552 Jul 25  2021 usr/sbin/arp
  • 如何创建包含硬链接的 cpio 存档,而不是 busybox 的副本?

我已经尝试过--link,这给出了错误:

~/my_initramfs $ find . -print | cpio -ovB --link > ~/my_initramfs.cpio
cpio: --link is meaningless with --create
Try 'cpio --help' or 'cpio --usage' for more information.

答案1

我通过阅读源码找到了答案:https://salsa.debian.org/kernel-team/initramfs-tools/-/blob/master/mkinitramfs?ref_type=heads#L492

~/my_initramfs $ find . -depth | sudo cpio -o > ~/my_initramfs.cpio
812365 blocks
~/my_initramfs $ find . -depth | sudo cpio -o -H newc > ~/my_initramfs.cpio
445102 blocks

相关内容