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.
This commit is contained in:
Will Fancher 2024-10-21 16:14:51 -04:00
parent 76612b17c0
commit ec02a76ff5
4 changed files with 61 additions and 0 deletions

View File

@ -0,0 +1,21 @@
{
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 ];
};
}

View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "chroot-realpath"
version = "0.1.0"

View File

@ -0,0 +1,9 @@
[package]
name = "chroot-realpath"
version = "0.1.0"
edition = "2021"
[dependencies]
[profile.release]
opt-level = "z"

View File

@ -0,0 +1,24 @@
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(())
}