Sysfs is a virtual filesystem in Linux that exposes:
- Kernel objects
- Devices
- Drivers
- Bus information
- Runtime attributes
to user space through files and directories.
Sysfs is mounted at:
/sysIt is one of the most important interfaces used in Linux device driver development.
Unlike procfs, sysfs is:
- Structured
- Device-oriented
- Stable
- Recommended for modern Linux drivers
A Sysfs Driver is a Linux kernel module that creates entries inside:
/sysThese entries allow:
- Reading kernel/device data
- Writing configuration values
- Runtime control of drivers
User space interacts using:
cat
echocommands.
Sysfs provides a clean interface between:
Kernel Space ↔ User Space
It is mainly used for:
| Usage | Purpose |
|---|---|
| Device configuration | Runtime settings |
| Driver control | Enable/disable features |
| Monitoring | View device status |
| Hardware abstraction | Standard Linux interface |
| Debugging | Device diagnostics |
| Sysfs Path | Purpose |
|---|---|
| /sys/class/gpio | GPIO control |
| /sys/class/leds | LED control |
| /sys/class/pwm | PWM management |
| /sys/block | Block devices |
| /sys/bus | Bus information |
Driver examples:
- Sensor value reading
- LED brightness control
- GPIO direction setup
- Device enable/disable
- Runtime tuning
+-------------------------------+
| User Space Application |
|-------------------------------|
| cat |
| echo |
+---------------+---------------+
|
v
+-------------------------------+
| Sysfs Virtual Filesystem |
+---------------+---------------+
|
v
+-------------------------------+
| Kernel Object (kobject) |
+---------------+---------------+
|
v
+-------------------------------+
| Sysfs Driver |
|-------------------------------|
| show() |
| store() |
+---------------+---------------+
|
v
+-------------------------------+
| Device / Kernel Data |
+-------------------------------+
| Feature | Sysfs | Procfs |
|---|---|---|
| Purpose | Device model | Process/kernel info |
| Structure | Structured | Free-form |
| Recommended | Yes | Limited |
| Device Integration | Excellent | Weak |
Represents kernel object.
Sysfs is built around:
struct kobjectRepresents sysfs file.
Each attribute has:
- show() → Read
- store() → Write
Collection of kobjects.
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/kobject.h>
#include <linux/sysfs.h>
#include <linux/string.h>Driver creates kobject.
Attribute file added.
Kernel calls:
show()Kernel calls:
store()Sysfs files removed.
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/kobject.h>
#include <linux/sysfs.h>
static struct kobject *my_kobject;
static int value = 0;
static ssize_t value_show(struct kobject *kobj,
struct kobj_attribute *attr,
char *buf)
{
return sprintf(buf, "%d\n", value);
}
static ssize_t value_store(struct kobject *kobj,
struct kobj_attribute *attr,
const char *buf,
size_t count)
{
sscanf(buf, "%d", &value);
printk(KERN_INFO "Sysfs Value = %d\n", value);
return count;
}
static struct kobj_attribute value_attribute =
__ATTR(value, 0664, value_show, value_store);
static int __init sysfs_driver_init(void)
{
int ret;
my_kobject = kobject_create_and_add("my_sysfs", kernel_kobj);
if (!my_kobject)
return -ENOMEM;
ret = sysfs_create_file(my_kobject,
&value_attribute.attr);
if (ret) {
printk(KERN_ERR "Failed to create sysfs file\n");
kobject_put(my_kobject);
return ret;
}
printk(KERN_INFO "Sysfs Driver Loaded\n");
return 0;
}
static void __exit sysfs_driver_exit(void)
{
kobject_put(my_kobject);
printk(KERN_INFO "Sysfs Driver Removed\n");
}
module_init(sysfs_driver_init);
module_exit(sysfs_driver_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("Simple Sysfs Driver");obj-m += sysfs_driver.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
all:
make -C $(KDIR) M=$(PWD) modules
clean:
make -C $(KDIR) M=$(PWD) cleanmakeOutput:
sysfs_driver.kosudo insmod sysfs_driver.kols /sys/kernel/my_sysfs/Expected:
value
cat /sys/kernel/my_sysfs/valueExpected Output:
0
echo 100 > /sys/kernel/my_sysfs/valuedmesg | tailExpected:
Sysfs Value = 100
Creates sysfs directory.
Example:
kobject_create_and_add(name,
parent);Creates sysfs file.
Example:
sysfs_create_file(kobj,
&attr);Releases kobject.
Example:
kobject_put(kobj);Creates sysfs attribute.
Syntax:
__ATTR(name,
permission,
show,
store)Example:
0664
Meaning:
| Value | Permission |
|---|---|
| 6 | Read + Write |
| 4 | Read Only |
Used when user reads sysfs file.
Example:
cat /sys/kernel/my_sysfs/valuecalls:
value_show()Used when user writes sysfs file.
Example:
echo 10 > valuecalls:
value_store()| Advantage | Description |
|---|---|
| Structured Interface | Clean device representation |
| Device-Oriented | Integrated with driver model |
| Standard Linux Method | Widely used |
| Easy User Access | cat/echo support |
| Runtime Configuration | Dynamic control |
| Disadvantage | Description |
|---|---|
| Text Only | No binary support |
| Limited Complexity | Simple interfaces only |
| Permission Management | Security considerations |
| Not for Large Data | Small attributes preferred |
| Feature | Sysfs | Procfs | Debugfs |
|---|---|---|---|
| Purpose | Device model | Process/kernel info | Debugging |
| Stable ABI | Yes | Partial | No |
| Recommended for Drivers | Yes | Limited | Debug only |
| Location | /sys | /proc | /sys/kernel/debug |
A virtual filesystem exposing kernel objects and device attributes to user space.
For:
- Runtime configuration
- Device management
- Monitoring
| Function | Purpose |
|---|---|
| show() | Read operation |
| store() | Write operation |
Core Linux kernel object used in sysfs hierarchy.
Sysfs provides structured device representation.
Cause:
- sysfs_create_file() failure
Fix:
- Verify return values
Cause:
- Wrong attribute permissions
Fix:
- Verify mode bits
Cause:
- Invalid buffer handling
Fix:
- Validate inputs
ls /sys/kernel/cat /sys/kernel/my_sysfs/valuedmesg | tailAfter learning basic sysfs drivers, move to:
- Device attributes
- Attribute groups
- Binary sysfs attributes
- Driver model integration
- Bus subsystem
- Power management
- Runtime PM
Used to create multiple attributes together.
Example:
sysfs_create_group()One value per file.
Always validate:
store()input.
Preferred over procfs for modern drivers.
Sysfs is designed for small text attributes.
Sysfs is heavily used on:
- Raspberry Pi 5
- BeagleBone Black
- NVIDIA Jetson Nano
- STM32MP157
| Path | Purpose |
|---|---|
| /sys/class/gpio | GPIO control |
| /sys/class/leds | LED management |
| /sys/block | Storage devices |
| /sys/bus | Bus subsystem |
| /sys/devices | Device hierarchy |