Entries in sysfs can be sorted based on the bus type, object type, device type, parent/child relationships, etc. Symlinks aid in reducing redundancies.
What Will We Cover?
In this article, we will explore the sysfs filesystem in Linux. Let’s start with an overview of the sysfs.
Overview of the sysfs File System
Sysfs was introduced in Linux for the first time in kernel version 2.6.0.
The sysfs is a virtual file system in Linux. It means that the files on sysfs do not reside on a disk or any physical media. However, the contents of the file systems are stored in memory. Originally, sysfs was based on ramfs and was called ddfs (Device Driver Filesystem).
Sysfs sends data to user space using virtual files. These data comprise data about various kernel subsystems, hardware devices, and associated device drivers.
Usually, sysfs is mounted on the /sys partition and is automatically mounted by the system. Also, it can be mounted manually on boot using the fstab file:
From the previous command, we note that the sysfs is mounted on /sys. This is the standard location where the sysfs is mounted on.
The Sysfs Hierarchy
Sysfs is a cluster of files, folders, and symlinks. Most of the files inside /sys are read-only. Some files are also writable, which helps in modifying kernel variables. Symlinks are widely used for linking entries throughout the filesystem tree.
We recommend using the “tree” command to see a complete view of this directory. On our system, the “tree” command shows a total of 9480 directories and 38635 files which is an extensive collection of files and directories.
On the top level, 11 major subdirectories are created at system boot. These subdirectories are representations of the major subsystems registered with the sysfs. At boot-up, these subsystems are registered with the kobject core. Once initialization is done, they start to find objects registered within their corresponding directories.
Files and Subdirectories of the /sys File System
Let’s now see a few files and subdirectories of the /sys file system and discuss their basic purpose. The most important virtual subdirectories on the top level of /sys are block, bus, hypervisor, class, devices, kernel, firmware, module, and power.
1. /sys/block: It has one symlink and directories for every block device discovered on the system. These symlinks point to their respective directories under the directory /sys/devices. There are many subdirectories inside here:
These block devices, shown in cyan color, have attributes containing information about the partitions. Examples of these subdirectories are sda, sdb, etc.
2. /sys/bus: A bus is a medium/channel between a processor and a device or devices. Examples of bus types include PCI, PCMCIA, SCSI, or USB. For every bus type in the kernel, there is a subdirectory that resides in the /sys/bus directory. Each such subdirectory has further two more subdirectories: devices and drivers.
i) devices: It contains symlinks to the entries in /sys/devices (the global device tree) for every device discovered on the bus.
ii) driver: It contains a subdirectory for every device driver loaded on the bus. These subdirectories contain attributes for managing driver parameters and symlinks.
3. /sys/class: A class is a high-level view of a device. This directory further contains one more level of subdirectories for every device class registered on the system. These classes may be terminals, network devices, sound devices, etc. These subdirectories contain symlinks for every device in a class. These symlinks point to the entries in the /sys/devices directory.
4. /sys/class/net: Every symlink in this directory represents either one of the real or virtual networking devices, which are visible in the network namespace of the process accessing the directory. Every such symlink points to the entries in the /sys/devices directory.
5. /sys/devices: The /sys/device directory contains the entire kernel device tree in the file system view. It includes each physical device found by the bus types registered with the kernel.
6. /sys/firmware: It contains interfaces for managing objects and attributes that are firmware specific. Here, firmware is a code that is executed on system boot-up. For example, the platform may be x86 bios, OpenFirmware, and ia64. There are many files in the EFI folder as an example.
Sample output:
config_table esrt fw_vendor runtime systab
efivars fw_platform_size mok-variables runtime-map vars
Each of these files contains some value for a parameter.
7. /sys/kernel: There are many files and folders in this subdirectory that give data about the active kernel.
8. /sys/module: For every module loaded into the kernel, there is a subdirectory that resides in this directory. The naming of these subdirectories is based on the name of the corresponding kernel. Inside each module directory, there is a subdirectory called sections which contains attributes about the module sections. Many files are like coresize, initsize, initstate, srcversion, etc. Similarly, there are many subdirectories like drivers, holders, notes, parameters, etc.
9. /sys/power: It represents the power subsystem and has only two attributes: disk and state. The method by which the system will go to sleep/suspend state is controlled by “disk”. “State” permits a power to gain a low-power state.
Conclusion
In this guide, we learned about the sysfs file systems in Linux and also seen various sub-directories and their purposes. The sysfs contains a vast amount of data. More information can be found on the man pages and also on this page, which is hosted on kernel.org