2

From man 2 pivot_root

pivot_root() moves the root file system of the current process to the directory _put_old_ and makes _new_root_ the new root file system of the current process.

int pivot_root(const char *new_root, const char *put_old);

While reading this I got following doubts

  1. What is the use of _put_old_? Is it is used only by kernal for its internal purpose or do we have any use with this?
  2. Why current filesystem should move to _put_old_ directory?
  3. Why _put_old_ directory should be under the _new_root_ directory?

Official doc clearly explains what we wanted to do if we wanted to utilize pivot_root syscall but I feel doc not clearly explained why it is enforcing these many rules and what it is doing behind the scene.

Someone who knows this kindly help me to solve this.

2
  • pivot_root(2) is a special purpose, one use only interface; it's not something generally useful. Think of it as of a chroot(2) which let you discard the old root (and all the resources it kept hold of). If the old root weren't put somewhere inside the new root tree, you would not be able to ever unmount it and get rid of it for good. Commented Dec 12, 2019 at 10:51
  • Or something like that. Unless you, I'm taking at heart the warning from the BUGS section of the pivot_root(2) manpage: "Some of the more obscure uses of pivot_root() may quickly lead to insanity". Commented Dec 12, 2019 at 10:54

1 Answer 1

2

pivot_root is used at system start-up. It was introduced specifically to facilitate the transition between the initial linux system on RAM, and the final root filesystem.

When linux boots, the kernel and the initial root file system are loaded into memory by the bootloader. The bootloader (which is stored in ROM) is a small program which loads the linux system into RAM and then executes the kernel.

At the end of the boot sequence, the RAM file system is mounted as the initial root file system. There are two schemes to do this, initrd and initramfs. Both methods start the kernel from the initial root file system.

The kernel must perform several jobs to create the final root file system. There are different techniques used to this (depends on the linux distribution) , but essentially the kernel loads the hardware drivers required to access the final root filesystem.

For example, if the root filesystem is on NFS, the kernel must setup networking first, acquire the information to access the NFS share, and then mount the NFS share.

Once the new root is accessible, the new root is mounted at a temporary mount point and rotated into place using pivot_root. The original root may still required for a short time for scripts to complete any final cleanup tasks. Once any cleanup is complete the initial root system is unmounted to free memory.

For more details see Initial linux ramdisk.

1
  • The bootloader that lives in the ROM's job is only to look for the MBR/GPT partition and load the "boot code" that it finds in there, usually grub's stage1 file. So I think we're talking about two bootloaders here: The one that lives in ROM and grub stage 1 which lives in the bootable disk's first sector with a size of exactly 512 bytes. At least for systems that use grub. Commented Feb 18 at 13:25

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.