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.

| improve this question | | | | |
  • 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. – mosvy Dec 12 '19 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". – mosvy Dec 12 '19 at 10:54

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.

| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.