Skip to content

lscpu: add --sysroot and test fixtures - #718

Merged
sylvestre merged 2 commits into
uutils:mainfrom
tuxnode:lscpu/sysroot-fixtures
Nov 27, 2026
Merged

sylvestre merged 2 commits into
uutils:mainfrom
tuxnode:lscpu/sysroot-fixtures

Conversation

@tuxnode

@tuxnode tuxnode commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

#717

Add -s/--sysroot to lscpu and thread the system root through every sysfs/procfs read. This makes it possible to run lscpu against a fake filesystem tree in tests and matches the util-linux --sysroot option.

The default behavior without --sysroot is unchanged: the root defaults to /.

Relation to #528

#528 also adds --sysroot, but additionally introduces a shared uulinux crate and changes lsmem. This PR contains only the lscpu portion so it can be reviewed and tested independently.

If #528 is preferred as-is, this PR can be closed.

Changes

  • Add -s, --sysroot <directory> to the lscpu CLI.
  • Add a small system_path(root, path) helper in sysfs.rs:
    • avoids Path::join discarding the root when given an absolute path
    • keeps the default / behavior unchanged
  • Pass the root through all relevant reads:
    • /proc/cpuinfo
    • /sys/devices/system/cpu/online
    • /sys/devices/system/cpu/cpuN/topology/*
    • /sys/devices/system/cpu/cpuN/cache/index*/*
    • /sys/devices/system/cpu/vulnerabilities/*
    • /sys/devices/system/cpu/cpufreq/boost
    • /sys/kernel/cpu_byteorder
  • Add TestSysCpu, a fake sysfs/procfs fixture built with tempfile::TempDir.
  • Add test_sysroot_output and test_sysroot_json.

The fixture deliberately uses values that are unlikely to match the host (TestVendor, Big Endian, boost=0, fake_vuln) so the tests fail if any read accidentally bypasses --sysroot.

Behavior

Before:

lscpu
# reads /sys and /proc directly

After:

lscpu --sysroot /path/to/root
# reads /path/to/root/sys and /path/to/root/proc

lscpu without --sysroot still reads from /.

Follow-ups

Thread a system root through all sysfs and procfs reads so lscpu can run against a fake tree. Add integration tests that exercise proc/cpuinfo, topology, caches, frequency boost, vulnerabilities and JSON output through --sysroot.
Comment thread tests/by-util/test_lscpu.rs Resolved
Comment thread tests/by-util/test_lscpu.rs Resolved
Comment thread src/uu/lscpu/src/sysfs.rs Resolved
Comment thread src/uu/lscpu/src/lscpu.rs Resolved
Comment thread src/uu/lscpu/src/lscpu.rs Resolved
Comment thread src/uu/lscpu/src/sysfs.rs Resolved
Address review comments: assert fields on the matching output line, parse JSON entries before checking values, return an error instead of panicking for an invalid sysroot, and simplify the sysroot argument definition.
@tuxnode

tuxnode commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review! All six comments are addressed in f43d930.

@tuxnode
tuxnode requested a review from sylvestre November 27, 2026 11:04
@sylvestre
sylvestre merged commit f155bed into uutils:main Nov 27, 2026
14 checks passed
@sylvestre

Copy link
Copy Markdown
Contributor

Thanks for your PR

@tuxnode
tuxnode deleted the lscpu/sysroot-fixtures branch November 27, 2026 17:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants