concepts

sysfs — Kernel Object and Device Attributes

sysfs exposes kernel object hierarchies and attributes. Device and class views may use symlinks to point to the same objects. Text attributes often expose a single value or a small structured list; their permitted operations and semantics are documented per subsystem. A read can query a driver and is not simply reading a normal disk file. Some attributes are writable and can change kernel or device behavior; do not write them in a general tutorial. Use readlink and documented attributes for authorized inventory, while accounting for device removal, restricted mounts and kernel-version differences. The example parses the online CPU list (ranges and individual numbers), checks for valid non-overlapping entries, and prints a normalized result rather than exposing host inventory. It neither changes CPU online state nor claims the list equals the caller's permitted affinity. Example prerequisites: Linux, Bash and Python 3 standard library; run as a non-root user in a private temporary working directory. Example scope: native read-only observation. Expected output: sysfs online CPU list parsed. No system configuration changes.

Formal Syntax & Structure

linux syntaxlinux
/sys/class/; /sys/devices/; /sys/devices/system/cpu/online

Parameters & Attributes

NameTypeDescription
classdirectory viewLogical device categories.
devicesdirectory viewPhysical/logical device hierarchy.
onlineCPU-list attributeCurrently online CPU identifiers.

Return Value

Concept / observation

Attribute format is subsystem-specific; this example checks CPU-list parsing.

Working Example

Example Snippetlinux
python3 - <<'PY'from pathlib import Pathonline = Path('/sys/devices/system/cpu/online').read_text().strip()seen=set()for token in online.split(','):    parts=token.split('-')    assert len(parts) in (1,2) and all(p.isdecimal() for p in parts)    lo,hi=int(parts[0]),int(parts[-1])    assert 0<=lo<=hi and hi-lo<1000000    cpus=set(range(lo,hi+1))    assert not seen & cpus    seen |= cpusassert seenprint('sysfs online CPU list parsed')PY
Linux Reference