Skip to content

Get to know Sysfs on Linux

Discover the Linux system responsible for recognizing hardware

Sysfs is a Kernel resource for exporting useful information about the system for user processes (programs) through an in-memory file system.

Sysfs organizes Kernel information into directories in a way rigorous and based on the internal organization of the Kernel’s data structures.

The files that are created in this file system are mainly in ASCII (plain text) with one value per file.

These features ensure that the information passed by sysfs to the programs is accurate and easily used.

Since the sysfs structure is composed of directories, files and symbolic links, navigation within it is easy and intuitive. The sysfs It is usually mounted in the /sys directory.

The main sysfs directories are: block, bus, class, devices, firmware, fs, kernel, module and power.

BLOCK

The block directory contains subdirectories for each block file system (mainly hard drives) in the system.

Within the device directories, there are several files that indicate, among other things, the size of the device, status, etc.

BUS

This directory contains subdirectories for each type of bus supported by the Kernel. And each subdirectory contains the devices and drivers directories.

The first contains a list of all devices found that are of the indicated bus type.

The second contains the device drivers for that type of bus.

CLASS

The class directory contains representations of each device class that is registered with the Kernel.

The classes indicate the type of device they represent. For example: printer class, disc class, etc.

Each class subdirectory will have a directory for each type of device found belonging to that class. For example: the “net” class contains the devices “eth0” and “eth1”.

And within each device directory, there are files and directories associated with the type of device it belongs to.

DEVICES

The devices directory contains the global hierarchy of devices found and supported by the Kernel.

Its directory hierarchy follows the organization of the devices to which type of electrical connection they are connected to. Only two devices are the exception to the rule: platform and system devices.

The first directory relates to the peripheral devices inherent to the hardware processing platform and bus. For example: devices inherent to input and output ports, serial and parallel controllers, etc.

The system, on the other hand, represents non-peripheral devices that do not fit any other classification. For example: CPUs, APICs, timers, etc.

FIRMWARE

The firmware directory contains interfaces to verify and assign specific firmware objects. Firmware are codes executed during the machine loading process, as BIOS software.

MODULE

The module directory contains subdirectories containing all Kernel modules loaded into memory.

Modules are pieces of programs that may or may not be part of the Kernel, so that they are read and executed according to the user’s needs.

For example: if you don’t frequently use the drive for an external USB hard drive, you can choose to Load the module that enables support for external disks only when using this type of media. This type of modular architecture allows the Kernel to be lighter, taking up less memory and also less processing, leaving the CPU more free time to execute user programs.

A lean Kernel allows the system to have better efficiency, but does not prevent other functionalities from being added on demand in the form of Kernel modules.

It is important to know that all module directories contain a file called refcnt that counts the number of references or number of users that make use of the specific module.

POWER

The power directory represents the energy management subsystem. It has some files that represent the method that the system will use to enter the mode of suspension or energy saving.

It is important to know that the information contained in this file system is organized in such a way that various programs and utilities make use of it to better interact with the Kernel.

HALD

Hald is a program that runs in server mode (daemon) that maintains a database of the devices connected to the system in real time.

This program is connected to the Kernel through the D-BUS listening to the messages related to the devices and provides an API programming library so that other programs can discover, monitor, and make use of the devices.

HALD makes it easier for programmers to work by offering an easy and quick way to interact with devices without having to access them directly.

UDEV

Dynamic device management (udev) is responsible for maintaining the /dev directory structure (directory that contains the links that represent the devices) in order to allow only connected devices are listed in the /dev framework.

Generally, this manager runs in the form of a program called udevd in server mode (daemon) that listens to Kernel events to find when a device is removed or added to the system.

When a device event is noticed in the Kernel, it compares it with the rules listed in the sysfs to identify the device and then creates or removes the symbolic link of the device in the /dev directory.

D-BUS

D-BUS is an open source project maintained by freedesktop.org. Its main purpose is to create a message exchange bus between programs.

It allows programs to interact with each other in such a way that the programmer does not need to know the instance of the other program to which he wishes to connect.

In this way, D-BUS offers a programming library that allows the exchange of messages between applications using standard protocols, using XML to connect between different programs.

D-BUS supports several high-level programming languages such as Qt, GLib, Java, C#, Python, etc.

Learn much more about Linux in our online course. You can enroll here with a free 7-day trial. If you already have an account, you can log in here.

Did you like it? Share :-)