从 initramfs 到新根的 pivot_root - 错误“无效参数”

sou*_*edi 6 linux initramfs

您不能在 initramfs rootfs 上使用 pivot_root,您将获得无效参数。您只能旋转真实的文件系统。

的确:

  1. Fedora Linux 28 - 这使用dracutinitramfs。
  2. 通过rd.break在内核命令行上添加一个选项来引导到 initramfs shell 。
  3. cd /sysroot
  4. usr/bin/pivot_root . mnt

- >pivot_root失败,“无效参数”,对应于errno的值EINVAL

对此没有解释man 2 pivot_root

EINVAL put_old不在new_root之下。

为什么会失败?正如下一个评论者回答的那样,“那么 Linux 将如何退出早期用户空间?”

sou*_*edi 9

与 不同的是initrd,Linux 不允许卸载initramfs. 显然这有助于保持内核代码简单。

pivot_root您可以使用该switch_root命令代替。它执行以下过程。 请注意,switch_root删除旧根目录上的所有文件,以释放 initramfs 内存,因此您需要小心运行此命令的位置。

initramfs 是 rootfs:您既不能 pivot_root rootfs,也不能卸载它。而是删除 rootfs 中的所有内容以释放空间(find -xdev / -exec rm '{}' ';'),使用新根目录重载 rootfs(cd /newmount; mount --move . /; chroot .),将 stdin/stdout/stderr 附加到新的 /dev/console,并执行新的 init。

请注意,建议的 shell 命令只是 C 代码的粗略等效项。除非它们都内置到您的 shell 中,否则这些命令将不会真正起作用,因为第一个命令会从 initramfs 中删除所有程序和其他文件:-)。


Rootfs 是 ramfs(或 tmpfs,如果已启用)的一个特殊实例,它始终存在于 2.6 系统中。您无法卸载 rootfs 的原因与您无法终止 init 进程的原因大致相同;与使用特殊代码来检查和处理空列表相比,内核确保某些列表不会变空更小更简单。

https://github.com/torvalds/linux/blob/v4.17/Documentation/filesystems/ramfs-rootfs-initramfs.txt