Commit ec02a76f authored by Will Fancher's avatar Will Fancher
Browse files

chroot-realpath: init

Now it's placed between initrd-switch-root.target and
initrd-switch-root.service, meaning it is truly the last thing to
happen before switch-root, as it should be.
parent 76612b17
Loading
Loading
Loading
Loading
+21 −0
Original line number Diff line number Diff line
{
  lib,
  rustPlatform,
}:

let
  cargo = lib.importTOML ./src/Cargo.toml;
in
rustPlatform.buildRustPackage {
  pname = cargo.package.name;
  version = cargo.package.version;

  src = ./src;

  cargoLock.lockFile = ./src/Cargo.lock;

  meta = {
    description = "Output a path's realpath within a chroot.";
    maintainers = [ lib.maintainers.elvishjerricco ];
  };
}
+7 −0
Original line number Diff line number Diff line
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3

[[package]]
name = "chroot-realpath"
version = "0.1.0"
+9 −0
Original line number Diff line number Diff line
[package]
name = "chroot-realpath"
version = "0.1.0"
edition = "2021"

[dependencies]

[profile.release]
opt-level = "z"
+24 −0
Original line number Diff line number Diff line
use std::env;
use std::io::{stdout, Error, ErrorKind, Write};
use std::os::unix::ffi::OsStrExt;
use std::os::unix::fs;

fn main() -> std::io::Result<()> {
    let args: Vec<String> = env::args().collect();

    if args.len() != 3 {
        return Err(Error::new(
            ErrorKind::InvalidInput,
            format!("Usage: {} <chroot> <path>", args[0]),
        ));
    }

    fs::chroot(&args[1])?;
    std::env::set_current_dir("/")?;

    let path = std::fs::canonicalize(&args[2])?;

    stdout().write_all(path.into_os_string().as_bytes())?;

    Ok(())
}