Linux 使用 filesystem interfaces 用于 both stored 数据 和 live 内核 state. The Virtual Filesystem Switch (VFS) makes those objects accessible 通过 common system calls, 同时 pseudo-filesystems such 作为 procfs 和 sysfs generate their contents from kernel objects. Similar path syntax does 不 give a pseudo-file the persistence or I/O semantics 的 a disk file.
VFS 作为内核抽象 #
VFS 是 the 内核 layer behind 操作 such 作为 open, read, write,
stat, 和 mount. It represents mounted filesystems 通过 common 对象 types:
- a superblock represents a filesystem instance, 同时 a mount attaches that instance 在 a location in one mount namespace;
- an inode represents an 对象 和 its metadata;
- a dentry associates a name,使用 an inode 和 participates in pathname lookup;
- a file represents an open instance 和 its current position;
- an address space connects cacheable 文件 contents 到 the page cache.
Filesystem implementations provide operation tables 用于 relevant objects. ext4 can map reads 到 persistent blocks, NFS 可以 issue network requests, 和 procfs 可以 format a 内核 structure. Callers still receive 文件 descriptors 和 使用 the same system- call boundary.
“Virtual filesystem” 是 therefore ambiguous. VFS names the abstraction layer; a pseudo-filesystem 是 one filesystem 实现 whose objects do not correspond 到 durable files 在 a storage volume. tmpfs 是 also memory-backed, but it provides ordinary mutable 文件 semantics 和 distinct 数据 per mount. procfs and sysfs expose purpose-built 内核 interfaces instead.
Procfs 进程与系统视图 #
Procfs 是 conventionally mounted 在 /proc. Numeric directories expose processes,
while nonnumeric entries expose system-wide state 和 control interfaces:
/proc/1234/status formatted state for process 1234
/proc/1234/fd/ links for its open file descriptors
/proc/self/ the reading process's own proc directory
/proc/meminfo memory-accounting snapshot
/proc/sys/ sysctl parametersFiles under /proc/<pid> 是 checked against 进程 credentials, ptrace access
rules, capabilities, mount options, 和 security modules. 一个 pathname being readable
by root in one context does 不 make it public 到 an unprivileged process.
实例、PID 命名空间与挂载命名空间 #
Procfs output 是 namespace-sensitive. 一个 procfs mount associated,使用 a PID namespace
shows tasks visible in namespace; /proc/self 和 /proc/thread-self also
resolve relative 到 the caller. 一个 进程 creating 或 entering another PID namespace
must 使用 an appropriate procfs mount 到 obtain a matching 进程 view.
Mount namespaces independently control where 和 whether procfs instance is
mounted. Containers 可以 consequently have different /proc mount points, read-only
settings, 和 visibility options 同时 sharing the same kernel. Options such as
hidepid restrict access 到 other users’ 进程 directories, 和 subset=pid can
hide top-level entries unrelated 到 tasks 在 supporting kernels.
Procfs 应当 不 be modeled 作为 one global 目录 tree cached 由 pathname. The same path 可以 resolve 到 different information 用于 different callers, namespaces, credentials, 或 moments in time. Open descriptors 到 a dead 进程 do 不 become handles 到 a later 进程 reuses the same PID.
Sysfs 与驱动对象模型 #
Sysfs, conventionally mounted 在 /sys, exports the kernel’s 对象 model. A
registered kobject receives a 目录 beneath its parent, so 目录 hierarchy
expresses 对象 relationships 和 controls lifetime. Linux 设备 model builds
devices, drivers, buses, 和 classes 在 infrastructure:
/sys/devicespresents devices in their physical 或 firmware-discovered topology./sys/bus/<bus>/deviceslinks 到 devices participating in a bus./sys/bus/<bus>/driverspresents registered drivers 和 their bindings./sys/class/<class>groups devices 由 function, such 作为 network 或 block devices.
Many entries outside /sys/devices 是 symbolic-link views 的 the same underlying
device object. Scripts 应当 resolve documented attributes rather than infer
physical topology 来自 a convenient class path.
A sysfs attribute connects a small 文件 到 subsystem-defined show 和 store
callbacks. core allocates a page-sized buffer 和 asks show 到 render a value
or passes one write buffer 到 store. Attributes 应当 contain one ASCII value, or
an array 的 values 的 the same type, because simple text forms 是 inspectable and
avoid embedding an ad hoc structured protocol. Binary attributes exist 用于 数据 that
cannot follow convention 和 have separate callbacks.
将文本文件视为 ABI 端点 #
Text does 不 mean informal. 一个 documented procfs 或 sysfs 文件 是 a 用户空间 ABI with file-specific syntax:
- Read the complete 值 和 parse 仅 forms promised 由 its ABI documentation.
- Include the expected newline 当 writing conventional text attributes.
- Do 不 depend 在 undocumented whitespace, column positions, 目录 ordering, 或 the presence 的 testing-only interfaces.
- Check write errors 和 short writes; a successful
opendoes 不 imply a proposed 值 是 accepted.
Mode bits 和 文件 permissions 是 仅 one access-control layer. Capability checks, ownership, namespaces, lockdown policy, 和 Linux Security Modules 可以 reject an operation. World-writable sysfs attributes 是 intentionally discouraged because a write often changes live 设备 或 driver state.
kernel’s Documentation/ABI tree classifies interfaces 作为 stable, testing,
obsolete, 或 removed. Software tied 到 a testing attribute accepts a different
compatibility commitment 来自 software using a stable one. Feature detection should
check 用于 a documented path 和 handle ENOENT; 内核 version comparisons cannot
account 用于 vendor backports 和 配置 choices.
读取是快照而非事务 #
Many pseudo-files 是 生成的 当 read. Kernel state 可以 change between opening,
reading, 和 parsing a file, 或 between reads 的 two related files. Even one large
procfs report may summarize fields observed 在 slightly different times. Rewinding a
sysfs attribute 到 offset zero 可以 invoke its show callback again 和 返回 a new
value.
Consequently, multiple reads do 不 provide an atomic 系统 snapshot unless the specific ABI promises one. Robust consumers should:
- 使用 one read operation 或 read loop 用于 a small attribute;
- tolerate processes 和 devices disappearing,使用
ENOENT,ENODEV, 或 related errors; - validate identifiers again 当 acting 在 previously read state;
- 使用 a dedicated transactional API 当 consistency across objects 是 required.
Some sysfs attributes support poll/select notification 当 their subsystem calls
the sysfs notification mechanism. Such notification 是 opt-in per attribute, 不 a
property 的 every 文件 under /sys. Consumers generally reopen 或 seek 到 offset
zero 和 reread 在……之后 an event. Procfs entries likewise have file-specific polling
semantics.
伪文件不是持久化存储 #
Several ordinary-file assumptions fail 用于 procfs 和 sysfs:
- reported size 可以 be zero even though
readreturns data; mmap, arbitrary seeks, append mode, 或 partial writes may be unsupported;- 目录 entries 可以 appear 和 disappear 作为 processes 和 devices change;
- a successful write 可以 trigger hardware activity rather than store retrievable bytes;
- 数据 vanishes 在 shutdown 和 是 reconstructed 来自 the next kernel’s state.
Configuration 必须 survive reboot belongs in a persistent 配置 system
whose startup logic deliberately writes the required 内核 interfaces. Copying
/proc 或 /sys cannot back up 内核 state, 和 restoring copied files cannot
recreate processes 或 devices.
评论
在 GitHub 上阅读或评论(发表评论需要登录)