sys目录介绍
文章目录
- 1. 前言
- 2. 目录层次
- 3. 目录介绍
- 3.1 devices 目录
- 3.2 block 目录
- 3.3 bus 目录
- 3.4 class 目录
- 3.5 dev 目录
- 3.6 firmware目录
- 3.7 fs 目录
- 3.8 kernel目录
- 3.9 module 目录
- 3.10 power 目录
1. 前言
linux 下一切皆文件,文件的类型也很多,如普通文件,目录文件等,详细见下面表格。我们的硬件设备在
/dev
目录下都可以找到对应的节点,一般都是字符设备文件©和块设备文件(b)。对于控制这些硬件设备,在用户态,我们也是使用文件IO的方式实现的,常见的控制方式有以下这几种:
- 操作
/dev
目录下相关的设备节点,来控制硬件属性,如使用open
’、read
等系统调用完成,或者直接使用echo
(需要内核驱动实现对应的file_operations
函数支持输入解析控制)和dd
指令进行控制;- 操作
proc
文件系统下的相关文件,前提需要内核驱动实现proc_ops
相关接口,然后通过echo
指令进行控制;- 操作
sysfs
文件系统下的相关文件,前提需要内核驱动实现sysfs_ops
相关接口,然后通过cat
和echo
指令进行控制;其中
- proc:轻量级、快速,适合监控和简单调试,但功能受限。
- sys:功能强大,支持设备模型和动态配置,适合复杂驱动开发。
文件类型 | 标识符 | 描述 | 示例 |
---|---|---|---|
普通文件 | - | 存储文本、二进制数据或程序代码,最常见的文件类型 | /home/user/file.txt |
目录文件 | d | 用于组织其他文件和子目录,包含文件名和inode的映射表 | /etc 、/home |
符号链接(软链接) | l | 指向另一个文件或目录的快捷方式,删除源文件后链接失效 | /usr/bin/python -> python3 |
块设备文件 | b | 以数据块(如512B)为单位读写存储设备(硬盘、U盘等) | /dev/sda |
字符设备文件 | c | 以字符流形式读写设备(键盘、终端等) | /dev/tty |
命名管道(FIFO) | p | 用于进程间通信,数据遵循先进先出(FIFO)原则 | mkfifo mypipe 创建的管道 |
套接字文件 | s | 用于网络或本地进程间通信(如TCP/IP或Unix Domain Socket) | /var/run/mysqld/mysqld.sock |
而本文主要讲解的是sys
目录的架构。
2. 目录层次
sysfs
主要是对设备进行管理,可以展现出包含所有硬件设备的视图,挂载在/sys
目录下:
cat@lubancat:/dev$ mount | grep sysfs
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
进入到sys
目录下,可以看到系统包含如下文件:
cat@lubancat:/sys$ ll
总计 4
dr-xr-xr-x 14 root root 0 1月 1 2021 ./
drwxr-xr-x 19 root root 4096 9月 20 2024 ../
drwxr-xr-x 2 root root 0 1月 1 2021 block/
drwxr-xr-x 37 root root 0 1月 1 2021 bus/
drwxr-xr-x 85 root root 0 1月 1 2021 class/
drwxr-xr-x 4 root root 0 1月 1 2021 dev/
drwxr-xr-x 16 root root 0 1月 1 2021 devices/
drwxr-xr-x 3 root root 0 1月 1 2021 firmware/
drwxr-xr-x 10 root root 0 1月 1 2021 fs/
drwxr-xr-x 15 root root 0 1月 1 2021 kernel/
drwxr-xr-x 206 root root 0 1月 1 2021 module/
drwxr-xr-x 3 root root 0 5月 2 00:17 power/
其中包含 block
、bus
、class
、dev
、devices
、fimware
、fs
、kernel
、module
、power
。
3. 目录介绍
3.1 devices 目录
- 此目录存放了所有的硬件设备,所有的设备的属性都可以在目录进行查看,是
sys
目录下最重要的目录。
3.2 block 目录
-
此目录是块设备的存放目录,照 sysfs 的设计理念,系统所有的设备都存放在/sys/devices 目录下,所以/sys/block 目录下的文件通常是链接到/sys/devices 目录下的文件。
# 可以看到确实是连接到 devices 目录下 cat@lubancat:/sys/block$ ll 总计 0 drwxr-xr-x 2 root root 0 1月 1 2021 ./ dr-xr-xr-x 14 root root 0 1月 1 2021 ../ lrwxrwxrwx 1 root root 0 1月 1 2021 loop0 -> ../devices/virtual/block/loop0/ lrwxrwxrwx 1 root root 0 1月 1 2021 loop1 -> ../devices/virtual/block/loop1/ lrwxrwxrwx 1 root root 0 1月 1 2021 loop2 -> ../devices/virtual/block/loop2/ lrwxrwxrwx 1 root root 0 1月 1 2021 loop3 -> ../devices/virtual/block/loop3/ lrwxrwxrwx 1 root root 0 1月 1 2021 loop4 -> ../devices/virtual/block/loop4/ lrwxrwxrwx 1 root root 0 1月 1 2021 loop5 -> ../devices/virtual/block/loop5/ lrwxrwxrwx 1 root root 0 1月 1 2021 loop6 -> ../devices/virtual/block/loop6/ lrwxrwxrwx 1 root root 0 1月 1 2021 loop7 -> ../devices/virtual/block/loop7/ lrwxrwxrwx 1 root root 0 1月 1 2021 mmcblk0 -> ../devices/platform/fe2e0000.mmc/mmc_host/mmc0/mmc0:0001/block/mmcblk0/ lrwxrwxrwx 1 root root 0 1月 1 2021 mmcblk0boot0 -> ../devices/platform/fe2e0000.mmc/mmc_host/mmc0/mmc0:0001/block/mmcblk0/mmcblk0boot0/ lrwxrwxrwx 1 root root 0 1月 1 2021 mmcblk0boot1 -> ../devices/platform/fe2e0000.mmc/mmc_host/mmc0/mmc0:0001/block/mmcblk0/mmcblk0boot1/ lrwxrwxrwx 1 root root 0 1月 1 2021 mmcblk1 -> ../devices/platform/fe2c0000.mmc/mmc_host/mmc1/mmc1:aaaa/block/mmcblk1/ lrwxrwxrwx 1 root root 0 1月 1 2021 ram0 -> ../devices/virtual/block/ram0/ lrwxrwxrwx 1 root root 0 5月 2 12:22 sda -> ../devices/platform/fc880000.usb/usb6/6-1/6-1.3/6-1.3:1.0/host0/target0:0:0/0:0:0:0/block/sda/
3.3 bus 目录
-
此目录所有设备按照总线类型分类放置的目录结构,
devices
目录下每一种设备都是挂在某种总线下的,例如设备挂在USB
设备 挂载在USB
总线下。bus
目录下的文件通常也是链接到了devices
目录。cat@lubancat:/sys/bus/usb$ tree . ├── devices │ ├── 1-0:1.0 -> ../../../devices/platform/usbhost3_0/fcd00000.usb/xhci-hcd.3.auto/usb1/1-0:1.0 │ ├── 2-0:1.0 -> ../../../devices/platform/usbhost3_0/fcd00000.usb/xhci-hcd.3.auto/usb2/2-0:1.0 │ ├── 3-0:1.0 -> ../../../devices/platform/usbdrd3_1/fc400000.usb/xhci-hcd.4.auto/usb3/3-0:1.0 │ ├── 4-0:1.0 -> ../../../devices/platform/fc800000.usb/usb4/4-0:1.0 │ ├── 5-0:1.0 -> ../../../devices/platform/fc840000.usb/usb5/5-0:1.0 │ ├── 5-1 -> ../../../devices/platform/fc840000.usb/usb5/5-1 │ ├── 5-1:1.0 -> ../../../devices/platform/fc840000.usb/usb5/5-1/5-1:1.0 │ ├── 5-1:1.1 -> ../../../devices/platform/fc840000.usb/usb5/5-1/5-1:1.1 │ ├── 6-0:1.0 -> ../../../devices/platform/fc880000.usb/usb6/6-0:1.0 │ ├── 6-1 -> ../../../devices/platform/fc880000.usb/usb6/6-1 │ ├── 6-1.1 -> ../../../devices/platform/fc880000.usb/usb6/6-1/6-1.1 │ ├── 6-1:1.0 -> ../../../devices/platform/fc880000.usb/usb6/6-1/6-1:1.0 │ ├── 6-1.1:1.0 -> ../../../devices/platform/fc880000.usb/usb6/6-1/6-1.1/6-1.1:1.0 │ ├── 6-1.1:1.1 -> ../../../devices/platform/fc880000.usb/usb6/6-1/6-1.1/6-1.1:1.1 │ ├── 6-1.3 -> ../../../devices/platform/fc880000.usb/usb6/6-1/6-1.3 │ ├── 6-1.3:1.0 -> ../../../devices/platform/fc880000.usb/usb6/6-1/6-1.3/6-1.3:1.0 │ ├── 7-0:1.0 -> ../../../devices/platform/fc8c0000.usb/usb7/7-0:1.0 │ ├── 8-0:1.0 -> ../../../devices/platform/usbdrd3_1/fc400000.usb/xhci-hcd.4.auto/usb8/8-0:1.0 │ ├── usb1 -> ../../../devices/platform/usbhost3_0/fcd00000.usb/xhci-hcd.3.auto/usb1 │ ├── usb2 -> ../../../devices/platform/usbhost3_0/fcd00000.usb/xhci-hcd.3.auto/usb2 │ ├── usb3 -> ../../../devices/platform/usbdrd3_1/fc400000.usb/xhci-hcd.4.auto/usb3 │ ├── usb4 -> ../../../devices/platform/fc800000.usb/usb4 │ ├── usb5 -> ../../../devices/platform/fc840000.usb/usb5 │ ├── usb6 -> ../../../devices/platform/fc880000.usb/usb6 │ ├── usb7 -> ../../../devices/platform/fc8c0000.usb/usb7 │ └── usb8 -> ../../../devices/platform/usbdrd3_1/fc400000.usb/xhci-hcd.4.auto/usb8
3.4 class 目录
-
此目录的所有设备是按照其功能分类放置的,例如
/sys/calss/tty
目录存放的都是串口设备,/sys/calss/input
目录存放的都是输入类设备,无我们的鼠标mouse
。同样该目录下的文件也是链接到了/sys/devices
目录。├── input │ ├── event0 -> ../../devices/platform/fd8b0030.pwm/input/input0/event0 │ ├── event1 -> ../../devices/platform/feb20000.spi/spi_master/spi2/spi2.0/rk805-pwrkey.1.auto/input/input1/event1 │ ├── event10 -> ../../devices/platform/fc840000.usb/usb5/5-1/5-1:1.1/0003:093A:2533.0002/input/input11/event10 │ ├── event2 -> ../../devices/platform/hdmiin-sound/sound/card0/input2/event2 │ ├── event3 -> ../../devices/platform/es8388-sound/input/input3/event3 │ ├── event4 -> ../../devices/platform/es8388-sound/sound/card1/input4/event4 │ ├── event5 -> ../../devices/platform/hdmi1-sound/sound/card2/input5/event5 │ ├── event6 -> ../../devices/platform/hdmi0-sound/sound/card3/input6/event6 │ ├── event7 -> ../../devices/platform/dp0-sound/sound/card4/input7/event7 │ ├── event8 -> ../../devices/platform/adc-keys/input/input8/event8 │ ├── event9 -> ../../devices/platform/fc840000.usb/usb5/5-1/5-1:1.0/0003:093A:2533.0001/input/input9/event9 │ ├── input0 -> ../../devices/platform/fd8b0030.pwm/input/input0 │ ├── input1 -> ../../devices/platform/feb20000.spi/spi_master/spi2/spi2.0/rk805-pwrkey.1.auto/input/input1 │ ├── input11 -> ../../devices/platform/fc840000.usb/usb5/5-1/5-1:1.1/0003:093A:2533.0002/input/input11 │ ├── input2 -> ../../devices/platform/hdmiin-sound/sound/card0/input2 │ ├── input3 -> ../../devices/platform/es8388-sound/input/input3 │ ├── input4 -> ../../devices/platform/es8388-sound/sound/card1/input4 │ ├── input5 -> ../../devices/platform/hdmi1-sound/sound/card2/input5 │ ├── input6 -> ../../devices/platform/hdmi0-sound/sound/card3/input6 │ ├── input7 -> ../../devices/platform/dp0-sound/sound/card4/input7 │ ├── input8 -> ../../devices/platform/adc-keys/input/input8 │ ├── input9 -> ../../devices/platform/fc840000.usb/usb5/5-1/5-1:1.0/0003:093A:2533.0001/input/input9 │ ├── mice -> ../../devices/virtual/input/mice │ ├── mouse0 -> ../../devices/platform/fd8b0030.pwm/input/input0/mouse0 │ └── mouse1 -> ../../devices/platform/fc840000.usb/usb5/5-1/5-1:1.0/0003:093A:2533.0001/input/input9/mouse1
3.5 dev 目录
-
此目录照设备号的方式放置设备,该目录下有很多以主设备号:次设备号(major:minor)命名的文件,这些文件都是链接文件,链接到
/sys/devices
目录下对应的设备。├── 81:0 -> ../../devices/platform/fdee0000.hdmirx-controller/video4linux/video0├── 89:0 -> ../../devices/platform/fd880000.i2c/i2c-0/i2c-dev/i2c-0├── 89:1 -> ../../devices/platform/fea90000.i2c/i2c-1/i2c-dev/i2c-1├── 89:10 -> ../../devices/platform/fdea0000.hdmi/i2c-10/i2c-dev/i2c-10├── 89:11 -> ../../devices/platform/fde50000.dp/i2c-11/i2c-dev/i2c-11├── 89:5 -> ../../devices/platform/fead0000.i2c/i2c-5/i2c-dev/i2c-5├── 89:6 -> ../../devices/platform/fec80000.i2c/i2c-6/i2c-dev/i2c-6├── 89:7 -> ../../devices/platform/fec90000.i2c/i2c-7/i2c-dev/i2c-7└── 89:9 -> ../../devices/platform/fde80000.hdmi/i2c-9/i2c-dev/i2c-9
3.6 firmware目录
-
此目录描述了内核中的固件。
├── rkvenc-core@fdbe0000 │ │ ├── assigned-clock-rates │ │ ├── assigned-clocks │ │ ├── clock-names │ │ ├── clocks │ │ ├── compatible │ │ ├── interrupt-names │ │ ├── interrupts │ │ ├── iommus │ │ ├── mem-supply │ │ ├── name │ │ ├── operating-points-v2 │ │ ├── phandle │ │ ├── power-domains │ │ ├── reg │ │ ├── reset-names │ │ ├── resets │ │ ├── rockchip,ccu │ │ ├── rockchip,normal-rates │ │ ├── rockchip,skip-pmu-idle-request │ │ ├── rockchip,srv │ │ ├── rockchip,task-capacity │ │ ├── rockchip,taskqueue-node │ │ ├── status │ │ └── venc-supply
3.7 fs 目录
-
此目录用于描述系统中所有文件系统,包括文件系统本身和按文件系统分类存放的已挂载点。
cat@lubancat:/sys/fs$ ls bpf btrfs cgroup ext4 f2fs fuse nfs pstore
3.8 kernel目录
-
这里是内核中所有可调参数的位置。
cat@lubancat:/sys/kernel$ ll 总计 0 drwxr-xr-x 15 root root 0 1月 1 2021 ./ dr-xr-xr-x 14 root root 0 1月 1 2021 ../ -r--r--r-- 1 root root 4096 5月 2 15:19 boot_mode drwxr-xr-x 2 root root 0 5月 2 15:19 cgroup/ drwxr-xr-x 3 root root 0 1月 1 1970 config/ drwx------ 55 root root 0 1月 1 1970 debug/ drwxr-xr-x 3 root root 0 5月 2 15:19 dmabuf/ -r--r--r-- 1 root root 4096 5月 2 15:19 fscaps drwxr-xr-x 18 root root 0 5月 2 14:25 iommu_groups/ drwxr-xr-x 151 root root 0 5月 2 15:19 irq/ -r--r--r-- 1 root root 4015280 5月 2 15:19 kheaders.tar.xz drwxr-xr-x 6 root root 0 11月 22 2023 mm/ -r--r--r-- 1 root root 84 5月 2 15:19 notes -r--r--r-- 1 root root 4096 5月 2 15:19 oops_count -rw-r--r-- 1 root root 4096 5月 2 15:19 profiling -rw-r--r-- 1 root root 4096 5月 2 15:19 rcu_expedited -rw-r--r-- 1 root root 4096 5月 2 15:19 rcu_normal drwxr-xr-x 2 root root 0 5月 2 15:19 reboot/ drwxr-xr-x 4 root root 0 1月 1 1970 security/ drwxr-xr-x 165 root root 0 5月 2 15:19 slab/ drwxr-xr-x 4 root root 0 5月 2 14:25 software_nodes/ drwxr-xr-x 4 root root 0 5月 2 15:19 sunrpc/ drwx------ 6 root root 0 1月 1 1970 tracing/ -r--r--r-- 1 root root 4096 5月 2 15:19 uevent_seqnum -r--r--r-- 1 root root 4096 5月 2 15:19 warn_count
3.9 module 目录
- 这里有系统中所有模块的信息。
3.10 power 目录
-
这里是系统中电源选项,有一些属性可以用于控制整个系统的电源状态。
cat@lubancat:/sys/power$ ls mem_sleep pm_debug_messages pm_print_times pm_wakeup_irq suspend_stats wakeup_count pm_async pm_freeze_timeout pm_test state sync_on_suspend