“/sys” 目录有什么用?

“/sys” 目录有什么用?

不久前,我注意到了这个我以前从未见过的目录。/sys我研究了一下,发现“现代 Linux 系统”通常有这个目录,并且它管理设备。我想这就是/dev。除了我提到的和这个引用自页:

/sys 是一个虚拟文件系统,可以访问它来设置或获取有关内核对系统的视图的信息。

我已经运行 Trusty 一段时间了,之前从未注意到它,这就是为什么我觉得它有点奇怪。有人能告诉我吗?/sys和之间有什么区别/dev?Ubuntu 什么时候开始使用这个目录的,为什么?谢谢。

答案1

/sys老的。它是在 Linux 内核达到 2.6 之前引入的(当时有 2.4/2.5 分裂)。因为第一个 Ubuntu 版本使用 2.6 内核每一个Ubuntu 的版本已经有了/sys

/dev包含实际的设备文件。它不提供对内核所知的所有设备的访问(例如以太网设备 -为什么网络接口不像其他设备一样位于 /dev 中?为什么以太网设备没有出现在“/dev”中?)。它是设备本身的接口 - 您可以向设备写入数据、从设备读取数据等等。

/sys是内核的一个接口。具体来说,它提供了内核提供的信息和配置设置的文件系统视图,与 非常相似/proc。写入这些文件可能会也可能不会写入实际设备,具体取决于您要更改的设置。它不仅用于管理设备,尽管这是一种常见的用例。

更多信息可以在这里找到内核文档

Top Level Directory Layout
~~~~~~~~~~~~~~~~~~~~~~~~~~

The sysfs directory arrangement exposes the relationship of kernel
data structures. 

The top level sysfs directory looks like:

block/
bus/
class/
dev/
devices/
firmware/
net/
fs/

devices/ contains a filesystem representation of the device tree. It maps
directly to the internal kernel device tree, which is a hierarchy of
struct device. 

bus/ contains flat directory layout of the various bus types in the
kernel. Each bus's directory contains two subdirectories:

    devices/
    drivers/

devices/ contains symlinks for each device discovered in the system
that point to the device's directory under root/.

drivers/ contains a directory for each device driver that is loaded
for devices on that particular bus (this assumes that drivers do not
span multiple bus types).

fs/ contains a directory for some filesystems.  Currently each
filesystem wanting to export attributes must create its own hierarchy
below fs/ (see ./fuse.txt for an example).

dev/ contains two directories char/ and block/. Inside these two
directories there are symlinks named <major>:<minor>.  These symlinks
point to the sysfs directory for the given device.  /sys/dev provides a
quick way to lookup the sysfs interface for a device from the result of
a stat(2) operation.

例如:

  • 设置笔记本电脑显示器亮度的一种方法是:

    echo N > /sys/class/backlight/acpi_video0/brightness
    
  • 获取网卡的MAC地址:

    cat /sys/class/net/enp1s0/address
    
  • 要获取当前的 CPU 扩展调节器:

    cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
    

等等...

相关内容