User space sysfs, proc, and udev
"Everything is a file in the kernel"
Virtual File System
Linux is designed with several unique features, one of which is the virtual file system (VFS). A virtual file system is an abstraction layer over the real filesystem that presents system information and kernel data as if they were ordinary files and directories. Unlike traditional files stored on disk, the files in a virtual file system are generated dynamically by the kernel and represent real-time data about processes, hardware, and kernel states. This allows users and programs to interact with the system in a standardized way through file operations like reading and writing, without needing complex kernel-level programming.
Two major virtual filesystems in Linux are /proc and sysfs, which expose process information and kernel objects, respectively. Combined with the udev device manager, these components provide a robust interface for managing and interacting with the system's hardware and processes. What is sysfs?
sysfs is a virtual filesystem in Linux that exposes kernel objects and hardware attributes to user space. It acts as an interface between the kernel and users, making it possible to retrieve detailed system information and configure kernel parameters by interacting with files. It’s primarily located under the /sys directory.
Key Purposes of sysfs:
Exposing Kernel and Hardware Information: Kernel objects, such as devices, drivers, and classes, are organized hierarchically in
/sys. This provides a clear, structured view of the hardware connected to the system.Dynamic Device Control:
sysfsallows users to modify device behavior by writing values to files. For example, changing a CPU’s frequency governor or adjusting the brightness of a screen can be done through this interface.Simplified Kernel Interaction: Before
sysfs, interacting with kernel components required special commands or system calls.sysfsprovides a simpler, file-based interface, where reading and writing to files maps to querying and configuring kernel parameters.
Example:
To check the current CPU frequency scaling governor, you can read from /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor:
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
In this way, sysfs brings a much more accessible, user-friendly means of kernel interaction.
What is /proc?
/proc (short for "process") is another virtual filesystem that reflects kernel and process-related information. Unlike sysfs, which is focused on devices and kernel objects, /proc is focused on system-wide information and process-level data.
Key Purposes of /proc:
Process and System Information:
/procholds information about system components such as memory, CPU, and networking, as well as data on every running process. This allows administrators and programs to retrieve details about the current system state without needing specialized tools.Dynamic Interaction with Kernel Parameters: Inside
/proc/sys/, there are tunable kernel parameters. These values can be changed by writing to files, allowing real-time kernel configuration.
Example:
To see detailed CPU information, you can read from /proc/cpuinfo:
cat /proc/cpuinfo
Similarly, you can monitor memory usage by reading /proc/meminfo:
cat /proc/meminfo
This read-only or writable view of processes and system states makes /proc a vital resource for debugging, performance monitoring, and system management.
What is udev?
udev is the Linux device manager responsible for dynamically managing device nodes in /dev. It listens for hardware events (such as the connection of a USB device) and creates or removes device nodes accordingly. udev automates the management of devices, particularly useful in a system where hardware is frequently connected or disconnected (hotplugging).
Key Purposes of udev:
Device Node Management: When a device is plugged in, such as a USB drive,
udevautomatically creates the corresponding device node in/dev(e.g.,/dev/sdb1). This device file represents the hardware and allows user-space programs to interact with it.Hotplug Support: In modern systems, devices are often connected or removed while the system is running.
udevmonitors these events and updates/devto ensure that the device nodes are available or removed when necessary.Custom Rules for Devices:
udevis configurable via rules, typically found in/etc/udev/rules.d/. These rules allow users to define how specific devices should be named, what permissions they should have, or what actions should be triggered when they are connected. For example, you can create custom names for devices, such as naming a specific USB drive/dev/myusb.
Example:
When you plug in a USB drive, udev automatically creates a node like /dev/sdb1. This node allows you to mount the drive and access its contents. If you unplug the USB drive, udev will automatically remove this node.
How Do They Work Together?
udevand Device Management: When a new device is connected (e.g., USB, network adapter), the kernel generates an event.udevlistens for this event and creates a corresponding device file in/dev, allowing you to interact with the device. It may also trigger scripts or actions defined in its rules.sysfsand Kernel Information: The device attributes managed byudevare accessible viasysfs. For example, afterudevcreates a network interface, information about the device can be found in/sys/class/net/. You can read this information to configure network settings./procfor System and Process Monitoring: Whilesysfsandudevdeal primarily with devices,/procprovides a window into the running processes and system state. It complementssysfsby offering dynamic data on the performance and behavior of the system, such as CPU usage, memory consumption, and process statistics.
Together, sysfs, /proc, and udev form a comprehensive system for managing and monitoring devices, processes, and kernel interactions in a Linux environment.
Embedded Linux Examples: How sysfs, /proc, and udev Come Together
GPIO Pin Control on an Embedded Board (e.g., Raspberry Pi, BeagleBone): Using
sysfs, you can manage GPIO pins directly. For example, you can set up a pin as output and toggle its state:echo "18" > /sys/class/gpio/export echo "out" > /sys/class/gpio/gpio18/direction echo "1" > /sys/class/gpio/gpio18/valueMonitoring Process Status on an Embedded Device: You can monitor the resource usage of critical processes (e.g., memory footprint of a sensor driver) using
/proc:cat /proc/1234/status # Where 1234 is the PID of the processCustom
udevRule for Device Behavior: You could create a customudevrule that triggers a script when a specific USB device is connected:ACTION=="add", ATTRS{idVendor}=="abcd", ATTRS{idProduct}=="1234", RUN+="/path/to/script.sh"
This ensures that your embedded system responds dynamically when the device is plugged in, perhaps to gather data from a connected sensor or to log device activity.
Summary
sysfsprovides a window into kernel objects and device attributes, offering an easy way to query and configure kernel parameters and device states./procgives detailed information about running processes and the system’s overall state, making it useful for performance monitoring and system diagnostics.udevdynamically manages device nodes in/dev, providing hotplug support and the ability to automatically respond to hardware changes.
These components enable developers and system administrators to efficiently interact with hardware and system internals, making Embedded Linux highly adaptable and powerful for managing hardware resources in constrained environments, from small IoT devices to complex embedded systems.