In Linux, each directory has its own purpose for its existence. The same applies to the sysfs directory. The sysfs entries are used by the kernel to export the information to processes in the user domain and to get input from the user. These entries traverse the file system to find the display and storage functions registered for them.
Entries in sysfs can be sorted by bus type, object type, device type, parent/child relationships, and so on. Symlinks help reduce redundancies.
What will we cover?
In this article, we will examine the sysfs file system in Linux. Let’s start with an overview of the sysfs.
Overview of the sysfs file system
Sysfs was first introduced in Linux in kernel version 2.6.0.
The sysfs is a virtual file system in Linux. This means that the files on sysfs are not on a disk or 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 userspace using virtual files. This data includes data about various kernel subsystems, hardware devices, and associated device drivers.
Normally sysfs is mounted on the /sys partition and automatically mounted by the system. It can also be mounted manually at boot time using the fstab file:
From the previous command, we note that sysfs is mounted on /sys. This is the default location where the sysfs is mounted.
The sysfs hierarchy
Sysfs is a cluster of files, folders, and symlinks. Most files in /sys are read-only. Some files are also writable, which helps in modifying kernel variables. Symlinks are commonly used to link entries throughout the file system tree.
We recommend using the tree command to see a full view of this directory. On our system, the “Tree” command shows a total of 9480 directories and 38635 files, which is a large collection of files and directories.
At the top level, 11 large subdirectories are created at system startup. These subdirectories are representations of the major subsystems registered with sysfs. At boot, these subsystems are registered with the kobject core. Once initialization is complete, they start finding objects registered in their respective directories.
Files and subdirectories of the /sys file system
Now let’s look at some files and subdirectories of the /sys file system and discuss their basic purpose. The main virtual subdirectories at the top level of /sys are block, bus, hypervisor, class, devices, kernel, firmware, module and power.
1. /sys/block: It has a symbolic link and directories for each block device detected on the system. These symlinks point to their respective directories under the /sys/devices directory. There are many subdirectories here:
These block devices, shown in cyan, have attributes that contain 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 one or more devices. Examples of bus types are PCI, PCMCIA, SCSI or USB. There is a subdirectory for each bus type in the kernel, located in the /sys/bus directory. Each of these subdirectories has two other subdirectories: Devices and Drivers.
i) Devices: It contains symlinks to the entries in /sys/devices (the global device tree) for each device detected on the bus.
ii) Drivers: It contains a subdirectory for each 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 also contains another level of subdirectories for each device class registered in the system. These classes can be terminals, network devices, audio devices, and so on. These subdirectories contain symlinks for each device in a class. These symbolic links point to entries in the /sys/devices directory.
4. /sys/class/net: Each symbolic link in this directory represents either one of the real or virtual network devices visible in the network namespace of the process accessing the directory. Each of these symbolic links points to 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 contains every physical device found by the kernel-registered bus types.
6. /sys/firmware: Contains interfaces for managing objects and attributes specific to firmware. Here firmware is code that runs at system startup. To the example, the platform can be x86 BIOS, OpenFirmware and ia64. There are many files in the EFI folder as example.
config_table esrt fw_vendor runtime systab
efivars fw_platform_size mok-variables runtime-map vars
Each of these files contains a value for a parameter.
7. /sys/kernel: There are many files and folders in this subdirectory that provide data about the active kernel.
8. /sys/module: For each module loaded into the kernel, there is a subdirectory located within this directory. The naming of these subdirectories depends on the name of the corresponding kernel. In each module directory there is a subdirectory called section that contains attributes related to the module sections. Many files are like coresize, initsize, initstate, srcversion etc. Likewise 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 enters the sleep/suspend state is controlled by Hard Disk. “Condition” allows a power to attain a state of low power.
In this guide, we learned about the sysfs filesystems in Linux and also saw different subdirectories and their purposes. The sysfs contains a huge amount of data. For more information see the man pages and also on this pagewhich is hosted on kernel.org