#Bash 的 pivot_root 命令
pivot_root [OPTION]... NEW_ROOT PUT_OLD
功能
更改根文件系统。
pivot_root将当前进程的根文件系统移动到PUT_OLD目录,并将NEW_ROOT设置为新的根文件系统。
类型
可执行文件(/usr/sbin/pivot_root),属于 util-linux。
参数
OPTIONS选项:-h,--help- 显示帮助-V,--version- 显示版本
NEW_ROOT- 新的根文件系统PUT_OLD- 将旧的根文件系统挂载到此处
#示例
$ mkdir ./mnt # 创建挂载点
$ sudo mount /dev/sda ./mnt/ # 挂载文件系统
$ wget https://busybox.net/downloads/binaries/1.35.0-x86_64-linux-musl/busybox -O ./mnt/busybox
$ sudo mkdir ./mnt/old_root # 创建目录挂载旧的根文件系统
$ sudo unshare -m /bin/bash # 创建新的 mount namespace,在其中运行一个新的 bash
$ sudo pivot_root ./mnt/ ./mnt/old_root # 将根文件系统切换到 ./mnt/
# cd / # 切换到根目录(原先的工作目录已经失效)
# ./busybox ls # 通过 busybox 执行命令
busybox old_root
# ./busybox mkdir /proc # 挂载基础文件系统
# ./busybox mount -t proc proc /proc
# ./busybox mkdir /sys
# ./busybox mount -t sysfs sysfs /sys
可以通过
Ctrl + D退出新建的 bash ,从而恢复原先的状态。
切换根文件系统后,没有
/bin目录下的各种命令,因此下载busybox来执行基础命令。
#相关命令
| 命令 | 说明 |
|---|---|
| chroot | 执行命令时指定根路径 |
| switch_root | 切换根文件系统(不可恢复) |
#推荐阅读
#手册
PIVOT_ROOT(8) System Administration PIVOT_ROOT(8)
NAME
pivot_root - change the root filesystem
SYNOPSIS
pivot_root new_root put_old
DESCRIPTION
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. Since
pivot_root(8) simply calls pivot_root(2), we refer to the man page of
the latter for further details.
Note that, depending on the implementation of pivot_root, root and
current working directory of the caller may or may not change. The
following is a sequence for invoking pivot_root that works in either
case, assuming that pivot_root and chroot are in the current PATH:
cd new_root
pivot_root . put_old
exec chroot . command
Note that chroot must be available under the old root and under the new
root, because pivot_root may or may not have implicitly changed the
root directory of the shell.
Note that exec chroot changes the running executable, which is
necessary if the old root directory should be unmounted afterwards.
Also note that standard input, output, and error may still point to a
device on the old root file system, keeping it busy. They can easily be
changed when invoking chroot (see below; note the absence of leading
slashes to make it work whether pivot_root has changed the shell’s root
or not).
OPTIONS
-h, --help
Display help text and exit.
-V, --version
Print version and exit.
EXAMPLE
Change the root file system to /dev/hda1 from an interactive shell:
mount /dev/hda1 /new-root
cd /new-root
pivot_root . old-root
exec chroot . sh <dev/console >dev/console 2>&1
umount /old-root
Mount the new root file system over NFS from 10.0.0.1:/my_root and run
init:
ifconfig lo 127.0.0.1 up # for portmap
# configure Ethernet or such
portmap # for lockd (implicitly started by mount)
mount -o ro 10.0.0.1:/my_root /mnt
killall portmap # portmap keeps old root busy
cd /mnt
pivot_root . old_root
exec chroot . sh -c 'umount /old_root; exec /sbin/init' \
<dev/console >dev/console 2>&1
SEE ALSO
chroot(1), pivot_root(2), mount(8), switch_root(8), umount(8)
REPORTING BUGS
For bug reports, use the issue tracker at
https://github.com/util-linux/util-linux/issues.
AVAILABILITY
The pivot_root command is part of the util-linux package which can be
downloaded from Linux Kernel Archive
<https://www.kernel.org/pub/linux/utils/util-linux/>.
util-linux 2.39.3 2023-10-23 PIVOT_ROOT(8)