123
In Linux, pivot_root, chroot, and switch_root are commands used to change the root filesystem of a process. Each has its specific use cases and functionalities.
pivot_root
The pivot_root command is used to change the root filesystem of the current process and its children. It swaps the current root filesystem with a new one, making the old root accessible at a specified location. This command is typically used during the boot process when the system transitions from an initial ramdisk (initrd) to the real root filesystem
1.
Example:
In this example, the root filesystem is changed to /new-root, and the old root is accessible at /old-root3.
chroot
The chroot command changes the root directory for the current process and its children to a specified directory. Unlike pivot_root, chroot does not swap the root filesystem but simply changes the reference point for the current process. This command is often used to create isolated environments, such as chroot jails.
2.
The switch_root command is used to switch from an initial ramdisk (initramfs) to the real root filesystem. It is similar to pivot_root but is specifically designed for use with initramfs. switch_root performs additional cleanup tasks, such as moving common mount points (/dev, /proc, /sys, etc.) into the new root and attempting to delete everything in the old root
1.
In this example, the root filesystem is switched to /new-root, and /sbin/init is executed as the new init process
1.
chroot: Changes the root directory for the current process without swapping the root filesystem. Used for creating isolated environments.
switch_root: Switches from initramfs to the real root filesystem, performing additional cleanup tasks. Used during the boot process with initramfs.
Use Cases
pivot_root: Used when you need to preserve the original root for some purpose, such as during the boot process with initrd1.
chroot: Used to create isolated environments, such as chroot jails, for security or testing purposes2.
switch_root: Used to switch from initramfs to the real root filesystem during the boot process, performing additional cleanup tasks1.
By understanding the differences and use cases of these commands, you can choose the appropriate one for your specific needs in managing the root filesystem of a process.
Comments
Post a Comment