linux – 你什么时候使用pivot_root而不是switch_root?
作者:互联网
我想更好地理解Linux init进程,以便通过ceph而不是nfs来网络引导系统.
在这个过程中,我遇到了两种形式的切换根.一个叫做switch_root,另一个叫做pivot_root.这些脚本是从使用pxe引导过程通过tftp获得的内存文件系统(initramfs)运行的.
你什么时候用一个而不是另一个?我已经看到在一些初始脚本中使用了两个放在root中.
解决方法:
我找到了一个很好的解释here.但是,让我尝试在答案中加入我所理解的更短格式.
更短的版本
>系统启动时,需要早期用户空间.有可能
使用initramfs或initrd实现.
> initrd加载到ramdisk中,这是一个实际的文件系统.
> initramfs不是文件系统.
>对于initrd,使用pivot_root,对于initramfs,使用switch_root.
版本更长
现在,详细解释我上面提到的内容.
While both an initramfs and an initrd serve the same purpose, there
are 2 differences. The most obvious difference is that an initrd is
loaded into a ramdisk. It consists of an actual filesystem (typically
ext2) which is mounted in a ramdisk. An initramfs, on the other hand,
is not a filesystem. It is simply a (compressed) cpio archive (of type
newc) which is unpacked into a tmpfs. This has a side-effect of making
the initramfs a bit more optimized and capable of loading a little
earlier in the kernel boot process than an initrd. Also, the size of
the initramfs in memory is smaller, since the kernel can adapt the
size of the tmpfs to what is actually loaded, rather than relying on
predefined ramdisk sizes, and it can also clean up the ram that was
used whereas ramdisks tend to remain in use (due to details of the
pivot_root implementation).There is also another side-effect difference: how the root device (and
switching to it) is handled. Since an initrd is an actual filesystem
unpacked into ram, the root device must actually be the ramdisk. For
an initramfs, there is a kernel “rootfs” which becomes the tmpfs that
the initramfs is unpacked into (if the kernel loads an initramfs; if
not, then the rootfs is simply the filesystem specified via the root=
kernel boot parameter), but this interim rootfs should not be
specified as the root= boot parameter (and there wouldn’t be a way to
do so, since there’s no device attached to it). This means that you
can still pass your real root device to the kernel when using an
initramfs. With an initrd, you have to process what the real root
device is yourself. Also, since the “real” root device with an initrd
is the ramdisk, the kernel has to really swith root devices from one
real device (the ramdisk) to the other (your real root). In the case
of an initramfs, the initramfs space (the tmpfs) is not a real device,
so the kernel doesn’t switch real devices. Thus, while the command
pivot_root is used with an initrd, a different command has to be used
for an initramfs. Busybox provides switch_root to accomplish this,
while klibc offers new_root.
标签:linux,startup,init,initramfs 来源: https://codeday.me/bug/20190809/1629020.html