Skip to main content

linuxutils_system/
pivot_root.rs

1use linuxutils_common::man::ManContent;
2
3pub const MAN: ManContent = ManContent::empty();
4
5use clap::Parser;
6use std::process::ExitCode;
7
8/// Change the root filesystem via pivot_root(2).
9///
10/// Moves the current root to `put_old` and makes `new_root` the new root.
11/// Both paths must be directories; `new_root` must be a mount point and
12/// `put_old` must be at or under `new_root`.
13#[derive(Parser)]
14#[command(name = "pivot_root", about = "Change the root filesystem")]
15pub struct Args {
16    /// New root filesystem path
17    new_root: String,
18
19    /// Where to move the old root
20    put_old: String,
21}
22
23pub fn run(args: Args) -> ExitCode {
24    if let Err(e) = rustix::process::pivot_root(&args.new_root, &args.put_old) {
25        eprintln!(
26            "pivot_root: failed to pivot from '{}' to '{}': {}",
27            args.new_root,
28            args.put_old,
29            std::io::Error::from(e)
30        );
31        return ExitCode::FAILURE;
32    }
33    ExitCode::SUCCESS
34}