Quando você usaria pivot_root por switch_root?

11

Eu estou querendo entender melhor o processo de inicialização do Linux para fazer o netboot em um sistema através do ceph em vez do nfs.

No processo, encontrei duas formas de alternar raiz. Um chamado switch_root e o outro chamado pivot_root. Esses scripts são executados a partir de um sistema de arquivos na memória (initramfs) obtido via tftp usando o processo de inicialização do pxe.

Quando você usaria um sobre o outro? Eu vi ambos usados em algum script de inicialização colocado na raiz.

    
por Matt H 24.04.2014 / 01:28

1 resposta

7

Encontrei uma explicação maravilhosa aqui . No entanto, deixe-me tentar colocar em um formato mais curto do que eu entendi na resposta.

Versão mais curta

  1. Enquanto o sistema inicializa, ele precisa de um espaço de usuário inicial. Pode ser alcançado usando initramfs ou initrd.
  2. O initrd é carregado no ramdisk, que é um FILE SYSTEM real.
  3. O initramfs é não um sistema de arquivos .
  4. Para initrd pivot_root é usado e para initramfs switch_root é usado.

Versão mais longa

Agora, para a explicação detalhada do que eu coloquei acima.

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.

    
por 24.04.2014 / 01:56