But before we get started, let me warn you: this is not for the faint of heart (or those who prefer serious tutorials).
So what exactly is sysfs? Well, it’s a virtual file system that provides an interface to access kernel data structures via files. It allows you to read and write values for various device driver parameters without having to modify the source code or recompile the kernel. Pretty cool, right? Let’s take a look at some examples!
To set the stage: let’s say we have a simple LED driver that can turn on/off an LED based on user input. To access this driver via sysfs, you would navigate to /sys/class/leds and voila there it is! You should see something like this:
# This script shows the available LED drivers in the /sys/class/leds directory
# and their corresponding names.
# First, we use the "ls" command to list the contents of the /sys/class/leds directory.
# The output will show the names of the LED drivers available.
$ ls /sys/class/leds
# The output should look something like this:
beep_beep cool-device:::system led0 led1 led2 led3 power_led1 power_led2 power_led3 system
# The names listed above correspond to the different LED drivers available.
# For example, "beep_beep" could be the name of an LED driver that produces a beeping sound,
# while "power_led1" could be the name of an LED driver that controls the power LED on a device.
# To access a specific LED driver, you would navigate to the /sys/class/leds directory and
# use the corresponding name. For example, to access the "power_led1" driver, you would use:
/sys/class/leds/power_led1
# This would allow you to control the power LED on your device using sysfs.
Now, let’s say we want to turn on the LED with index 1 (which is called “led1” in this case). To do that, you would navigate into /sys/class/leds/led1 and create a new file named brightness. This will allow us to write values between 0-255 for the LED’s intensity:
# Change directory to the LED with index 1
$ cd /sys/class/leds/led1
# Create a new file named "brightness" to control the LED's intensity
$ touch brightness
Now, let’s say we want to turn on the LED with maximum brightness. To do that, you would write a value of 255 into the newly created file:
# Create a file named "brightness" and write a value of 255 into it
# This will turn on the LED with maximum brightness
# The ">" symbol redirects the output of the "echo" command into the file
# The "echo" command prints the specified value (255) into the file
echo "255" > brightness
The LED should now be lit up like a Christmas tree. But wait what if we want to turn off the LED instead? No problem, my friend! Simply write a value of 0 into the same file:
// This script turns off the LED by writing a value of 0 into the "brightness" file.
// First, we use the "echo" command to write a value of 0 into the "brightness" file.
// The ">" symbol redirects the output of the "echo" command into the "brightness" file.
// This overwrites any existing content in the file with the new value of 0.
// The "0" value indicates that the LED should be turned off.
echo "0" > brightness
And that’s it! You can now use sysfs to control your device drivers without having to modify any source code or recompile anything. Pretty sweet, huh?
But wait there’s more! Sysfs also allows you to read values from various kernel data structures via files. For example, let’s say we want to check the current temperature of our CPU. To do that, navigate into /sys/class/thermal and create a new file named temp:
// Change directory to /sys/class/thermal
$ cd /sys/class/thermal
// Create a new file named temp0_input
$ touch temp0_input
Now, let’s read the value from this newly created file:
# This script reads the value from a newly created file called "temp0_input" and prints it to the console.
# The "cat" command is used to display the contents of a file.
# The file name "temp0_input" is passed as an argument to the "cat" command.
cat temp0_input
# The value "45" is printed to the console.
And that’s it! You can now use sysfs to access various kernel data structures without having to modify any source code or recompile anything. Pretty sweet, huh?
We hope this helped!