LinuxCommandLibrary

systemctl-switch-root

Switch to a new root filesystem

TLDR

Switch to a new root filesystem and execute its default init system

$ systemctl switch-root [path/to/new_root]
copy

Switch to a new root filesystem and run a specific init binary
$ systemctl switch-root [path/to/new_root] [/sbin/init]
copy

Switch to a new root filesystem with verbose output
$ systemctl switch-root [path/to/new_root] --verbose
copy

SYNOPSIS

systemctl switch-root [ROOT [INIT]]

PARAMETERS

ROOT
    Specifies the path to the new root directory. This directory will become the new / (root) of the system. If omitted, the command assumes the current root directory (/) is mounted as /sysroot in the new environment and attempts to pivot into it.

INIT
    Specifies the path to the init system executable to be run inside the new root. This is typically /sbin/init or the systemd executable itself. If omitted, the current systemctl instance (the PID 1 process) is re-executed within the new root to continue the boot process.

DESCRIPTION

The systemctl switch-root subcommand facilitates the critical transition from an initramfs environment to the real root filesystem of a Linux system. This is a fundamental step during the boot process, typically executed by the initramfs after it has prepared necessary drivers and mounted the main root device.

The command performs a pivot_root(2) or chroot(2) system call to change the root directory, remounts essential filesystems (such as /dev, /proc, /sys) within the new root, and then optionally executes a new init system (usually systemd itself) from this new location. This process effectively discards the temporary initramfs environment and launches the full operating system, enabling a flexible boot process where the initramfs handles complex early setup tasks before handing control to the main system.

CAVEATS

This command is primarily intended for use by initramfs scripts during the automated boot process. Manually executing it can lead to an unbootable system or unexpected behavior unless you fully understand the implications for the running system.

It assumes that the current system is operating within an initramfs context and that the new root filesystem is correctly set up and ready to take over. Incorrect ROOT or INIT paths can cause the system to fail to boot or become unresponsive.

BOOT PROCESS INTEGRATION

systemctl switch-root plays a pivotal role in the Linux boot sequence. After the kernel is loaded and the initramfs takes over, its primary responsibility is to find, mount, and prepare the actual root filesystem. Once the root filesystem is ready, systemctl switch-root is invoked to perform the final transition, effectively discarding the temporary initramfs environment and handing control to the main operating system's init process (which is typically systemd).

PIVOT_ROOT VS. CHROOT

While chroot simply changes the apparent root directory for a process and its children, pivot_root is a more powerful operation that actually moves the root filesystem of the current process to a new location (the old root typically becomes accessible at a specified mount point, e.g., /oldroot or /sysroot/oldroot). systemctl switch-root primarily utilizes pivot_root as it is the more robust mechanism for a full system root change, but it may fall back to chroot in specific scenarios or if pivot_root encounters issues.

HISTORY

The switch-root functionality is an integral part of modern Linux boot processes, particularly for systems utilizing systemd. Historically, initramfs scripts would often directly invoke pivot_root system calls or dedicated utilities like switch_root (often provided by util-linux). The integration of this function as a subcommand of systemctl emerged with the widespread adoption of systemd as the default init system. This integration allows systemd itself (as PID 1) to manage the transition and subsequent re-execution, ensuring a more consistent, robust, and centralized startup sequence.

SEE ALSO

systemctl(1), pivot_root(2), chroot(1), initramfs(7)

Copied to clipboard