Discover Sysfs: The Linux Kernel Virtual Filesystem
Meet the system in Linux responsible for recognizing hardware
sysfs is a Kernel feature for exporting useful information about the system to user processes (programs) through a memory-based filesystem.
sysfs organizes Kernel information into directories in a strict manner based on the internal organization of Kernel data structures.
The files created in this filesystem are mainly in ASCII (plain text) with one value per file.
These characteristics ensure that the information passed by sysfs to 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. sysfs is usually mounted on the /sys directory.
The main directories in sysfs are: block, bus, class, devices, firmware, fs, kernel, module, and power.
BLOCK
The block directory contains subdirectories for each block filesystem (mainly hard drives) existing on the system.
Within the device directories, there are several files that indicate, among other things, the device size, status, etc.
BUS
This directory contains subdirectories for each bus type supported by the Kernel. And each subdirectory contains the devices and drivers directories.
The first contains a list of all detected devices that are of the indicated bus type.
The second contains the device drivers for that bus type.
CLASS
The class directory contains representations of each device class registered in the Kernel.
Classes indicate the type of device they represent. For example: printer class, disk class, etc.
Each class subdirectory will have a directory for each device type found belonging to that class. For example: the "net" class contains the devices "eth0" and "eth1".
And, within each device directory, you find files and directories associated with the device type 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 devices by the type of electrical connection they are attached to. Only two devices are the exception to the rule: platform devices and system devices.
The first directory is related to peripheral devices inherent to the processing platform and hardware bus. For example: devices inherent to input and output ports, serial and parallel controllers, etc.
System represents non-peripheral devices that don't fit into any other classification. For example: CPUs, APICs, timers, etc.
FIRMWARE
The firmware directory contains interfaces for inspecting and assigning firmware-specific objects. Firmware are codes executed during the machine boot process, like BIOS software.
MODULE
The module directory contains subdirectories with all Kernel modules loaded into memory.
Modules are pieces of programs that may or may not be part of the Kernel, so they are read and executed as needed by the user.
For example: if you don't frequently use the driver for an external USB hard drive, you can choose to load the module that enables external disk support only when you need to use this type of media. This type of modular architecture allows the Kernel to stay lightweight, using less memory and processing, leaving the CPU more time free to execute user programs.
A lean Kernel allows the system to have better efficiency, but it doesn't prevent other functionalities from being added on demand in the form of Kernel modules.
It's important to know that all module directories contain a file called refcnt that counts the number of references or number of users using that specific module.
POWER
The power directory represents the power management subsystem. It has some files that represent the method the system will use to enter suspension or power saving mode.
It's important for you to know that the information contained in this filesystem is organized so that various programs and utilities use it to better interact with the Kernel.
HALD
hald is a program that runs in server mode (daemon) maintaining a database about devices connected to the system, in real time.
This program connects to the Kernel through D-BUS listening to messages regarding devices and provides a programming API library so that other programs can discover, monitor, and use devices.
HALD makes programmers' work easier by offering an easy and fast way to interact with devices without needing direct access to them.
UDEV
Dynamic device management (udev) is responsible for maintaining the directory structure of /dev (directory that contains links representing devices) in a way that only connected devices are listed in the /dev structure.
Usually this manager runs as a program called udevd in server mode (daemon) listening to Kernel events to find when a device is removed or added to the system.
When a device event is detected in the Kernel it compares with the rules listed in sysfs to identify the device and, after that, creates or removes the device's symbolic link in the /dev directory.
D-BUS
D-BUS is a free software project maintained by freedesktop.org. Its main goal is to create a message bus for communication between programs.
It allows programs to interact with each other so that the programmer doesn't need to know the other program's instance they want to connect to.
This way, D-BUS offers a programming library that allows message exchange between applications through standardized protocols, using XML to link distinct 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 7-day free trial. If you already have an account, you can access it here.
Liked it? Share :-)
That was just one piece. The course teaches the whole path — from your first command to exam day.