Skip to content

lscpu: add -s/--sysroot support - #528

Open
jmr wants to merge 1 commit into
uutils:mainfrom
jmr:sysroot
Open

jmr wants to merge 1 commit into
uutils:mainfrom
jmr:sysroot

Conversation

@jmr

@jmr jmr commented Apr 16, 2026

Copy link
Copy Markdown
Contributor

Add the -s/--sysroot option present in upstream util-linux but previously missing. All hardcoded /sys/ and /proc/ paths in sysfs.rs are threaded through a root: &Path parameter; the default root is / so behaviour without the flag is unchanged.

To support this cleanly, introduce a new local shared crate uulinux with a join_under_root(root, path) -> PathBuf helper that prepends a sysroot to an absolute system path without discarding the root (unlike Path::join). Also use it in lsmem to replace the existing hand-rolled path construction there.

Add unit tests for join_under_root and Linux integration tests for lscpu --sysroot using a minimal fake sysfs/procfs tree.

@sylvestre

Copy link
Copy Markdown
Contributor

sorry but it needs to be rebased

@jmr

jmr commented Sep 25, 2026

Copy link
Copy Markdown
Contributor Author

it needs to be rebased

Done.

@jmr

jmr commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

Ping.

Add the `-s`/`--sysroot` option present in upstream util-linux but
previously missing. All hardcoded `/sys/` and `/proc/` paths in
`sysfs.rs` are threaded through a `root: &Path` parameter; the default
root is `/` so behaviour without the flag is unchanged.

To support this cleanly, introduce a new local shared crate `uulinux`
with a `join_under_root(root, path) -> PathBuf` helper that prepends a
sysroot to an absolute system path without discarding the root (unlike
`Path::join`). Also use it in `lsmem` to replace the existing
hand-rolled path construction there.

Add unit tests for `join_under_root` and Linux integration tests for
`lscpu --sysroot` using a minimal fake sysfs/procfs tree.
Comment thread src/uu/lscpu/src/lscpu.rs
json: matches.get_flag(options::JSON),
};

let sysroot = matches

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be value_parser!(PathBuf) with default_value("/"), no?

Comment thread src/uu/lscpu/src/sysfs.rs
pub fn read_online_cpus(root: &Path) -> String {
let path = join_under_root(root, Path::new("/sys/devices/system/cpu/online"));
fs::read_to_string(path)
.expect("Could not read sysfs")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with a wrong --sysroot this now panics
please return a proper error instead (same for the unwrap in CpuTopology::new and read_cpu_caches)
and add a test with a missing dir

Comment thread src/uulinux/src/lib.rs
///
/// Unlike [`Path::join`], this never discards `root` when `path` is absolute.
/// Useful for prepending a sysroot to a system path like `/sys/devices/...`.
pub fn join_under_root(root: &Path, path: &Path) -> PathBuf {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we really need a new crate for a 2 lines function? :)
also uulinux seems like a too generic name

Comment thread src/uu/lsmem/src/lsmem.rs
MAIN_SEPARATOR,
opts.sysmem.trim_start_matches(MAIN_SEPARATOR)
);
opts.sysmem = join_under_root(Path::new(sysroot), Path::new(&opts.sysmem))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated to lscpu, please move it to a separate pr
also it changes the behavior with --sysroot "" (now a relative path)

.succeeds()
.no_stderr()
.stdout_contains("CPU(s):")
.stdout_contains("2")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"2" will match almost anything
could you please check the full lines, e.g. "CPU(s): 2"?


#[test]
#[cfg(target_os = "linux")]
fn test_sysroot_long_flag() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need this test? clap already guarantees it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants