LinuxCommandLibrary

switch_root

Change root filesystem to another

TLDR

Move /proc, /dev, /sys and /run to the specified filesystem, use this filesystem as the new root and start the specified init process

$ switch_root [new_root] [/sbin/init]
copy

Display help
$ switch_root [[-h|--help]]
copy

SYNOPSIS

switch_root new_root init [arg...]

PARAMETERS

new_root
    The path to the directory that will become the new root filesystem. This directory must already be mounted before calling switch_root.

init
    The path to the initial program (e.g., /sbin/init) on the new_root filesystem that will be executed immediately after the root switch. This program takes over control of the system.

[arg...]
    Optional arguments that will be passed directly to the init program when it is executed.

DESCRIPTION

switch_root is a utility primarily used during the Linux boot process, specifically within an initramfs or initrd environment. Its main purpose is to change the current root filesystem from the temporary in-memory root (provided by the initramfs) to the actual, persistent root filesystem mounted from a disk. It achieves this by internally calling the pivot_root(2) system call, which shifts the current root to a new mount point and designates the new root as the system's actual root. After successfully pivoting, switch_root then executes the specified init program (typically /sbin/init or /bin/systemd) on the newly established root filesystem. This command is crucial for a seamless transition from the initial boot environment to the fully functional operating system, ensuring that the final root is correctly established before the main system services are launched.

CAVEATS

switch_root is designed for a very specific use case: transitioning from an initramfs/initrd environment to the real root filesystem. It should be the last command executed in the initramfs script, as it uses exec(2) to launch the new init, meaning it does not return. The new_root must be properly mounted before execution. It relies on the pivot_root(2) system call, which requires specific kernel capabilities and permissions. The old root filesystem, if not explicitly unmounted by the new init, will typically become accessible under new_root/old_root or similar.

BOOT SEQUENCE ROLE

switch_root plays a critical role in the Linux boot sequence. After the kernel loads and unpacks the initramfs, the initramfs's /init script identifies and mounts the real root filesystem. Once the real root is prepared, switch_root is invoked. Its execution ensures that the system's perspective of the root changes from the temporary in-memory filesystem to the actual disk-based root, finally executing the system's designated init process to continue the boot process with the correct filesystem hierarchy and services.

COMPARISON WITH <I>PIVOT_ROOT</I>

While switch_root uses the pivot_root(2) system call internally, it's more than just a direct wrapper. switch_root handles some setup and cleanup around the pivot operation specifically for the boot process, such as attempting to make the old root available at /old_root (though this often becomes new_root/old_root) and most importantly, it execs the new init process. In contrast, pivot_root(2) is merely the low-level system call that changes the mount points, requiring manual handling of unmounting the old root and executing the subsequent processes.

HISTORY

switch_root is a component of the util-linux project, a collection of essential utilities for Linux. Its development is closely tied to the evolution of the Linux boot process, particularly the widespread adoption of initramfs. It provides a more robust and standardized method for transitioning to the real root filesystem compared to earlier, more rudimentary scripts that might have used complex chroot and unmounting sequences. Its inclusion in util-linux ensures it's a standard tool available in almost all Linux distributions for managing the boot sequence from the temporary initramfs environment.

SEE ALSO

pivot_root(8), chroot(1), init(8), initramfs(7)

Copied to clipboard