Changing the Root of Your LinuxLink Classic Filesystem

After you have manually mounted an NFS-exported RFS, you need to tell the kernel on the target to use the newly-mounted filesystem as its root filesystem. Ordinarily, the root filesystem is mounted automatically during the Linux boot process, and is the filesystem that is mounted at the directory /. To modify the system’s notion of what the root filesystem is, use either pivot_root or chroot.

The main difference between the two is that pivot_root is intended to switch the complete system over to a new root directory and remove dependencies on the old one, so that you can unmount the original root directory and proceed as if it had never been in use. The chroot utility only changes the root for the shell [or program] from which it is called – all existing processes and programs started from another shell use the original root.

The pivot_root utility is only used for initrd-based RAM disks; use a utility such as switch_root for initramfs-based RAM disks.

chroot

The basic syntax of the chroot command is:

# chroot <mount_point> <shell>

where <mount_point> is the name of the directory you want to use as the root filesystem – in this context, the new root filesystem is the NFS-exported RFS, and <shell> is the path to the shell on the new filesystem, typically /bin/sh or /bin/bash. Thus, when the NFS root filesystem is mounted on the /mnt directory, the following command makes this the new root filesystem:

# chroot /mnt /bin/bash

pivot_root

The pivot_root utility exchanges the current root filesystem with a new root filesystem, moving the old root directory <old_root> into a subdirectory of <new_root>. The basic syntax of pivot_root is:

# pivot_root <new_root> <old_root>

Depending on how pivot_root is implemented in your distribution, root and cwd might or might not change. If pivot_root and chroot are in your current PATH, and chroot is available under both the old and the new root, you can invoke pivot_root in either case with the following sequence:

# cd <new_root>
# pivot_root "." <old_root>
# exec chroot "<command>"

Here, exec chroot changes the running executable <command>, which is necessary if the old root directory is later unmounted. This procedure is also useful when using stdin, stdout, and stderr, because they might still point to a device on the old root filesystem.

switch_root

Root filesystems based on initramfs cannot use the pivot_root utility. The switch_root utility is a widely-used alternative, and is included in BusyBox. The basic syntax of switch_root is:

exec switch_root <new_root> <new_init>

where <new_root> is the name of the directory you want to use as the root filesystem, and <new_init> is the path, relative to <new_root>, to the initialization directory on the new filesystem.

You must invoke this command via exec so that the new root inherits PID 1. The switch_root utility frees the memory used by initramfs, moves into the new directory, and executes <new_init> as PID 1.