Understanding Sysfs in Linux Kernel

But before we get started, let me warn you: this is not for the faint of heart. Sysfs is like trying to navigate through a maze blindfolded while juggling flaming torches it’s challenging and dangerous, but also incredibly rewarding once you master it.

So what exactly is sysfs? Well, in simple terms, it’s a virtual file system that allows you to interact with your kernel using regular files and directories. It provides an easy-to-use interface for accessing and modifying various kernel parameters without having to recompile the kernel or use complex command line tools.

Now, let me give you some examples of how sysfs can be used in real life scenarios. Let’s say you want to check your system’s CPU frequency. You could do this by running a command like “cat /proc/cpuinfo” and looking for the “cpu MHz” line. But with sysfs, all you have to do is navigate to “/sys/devices/system/cpu/” (which represents your main CPU) and read the value of “cpufreq_raw_cur” file:

#!/bin/bash

# This script checks the CPU frequency of the system by reading the "cpufreq_raw_cur" file in the "/sys/devices/system/cpu/0/" directory.

# First, we use the "cat" command to read the contents of the "cpufreq_raw_cur" file.
cat /sys/devices/system/cpu/0/cpufreq_raw_cur

# The output of the "cat" command will display the current CPU frequency in MHz.
# However, this output is not very clear and does not provide any context.
# To make it more user-friendly, we can add a message before the command to explain what it does.
echo "The current CPU frequency is:"

# Now, when we run the script, it will display a message followed by the CPU frequency.
# However, the frequency is still displayed in MHz and it would be more useful to have it in GHz.
# We can use the "awk" command to divide the frequency by 1000 and add a "GHz" suffix.
# The "awk" command takes the output of the previous command as its input and performs the specified operation on it.
# The "printf" command is used to format the output and add the "GHz" suffix.
awk '{printf "%.2f GHz\n", $1/1000}'

# The output will now display the CPU frequency in GHz with two decimal places.
# For example, if the frequency is 2498000 MHz, the output will be "2.50 GHz".

That’s it! You just got your CPU frequency without having to run any complex commands or modify any kernel parameters. And the best part is that you can do this for other devices as well, such as memory, I/O ports, and even GPIO pins (if you have a Raspberry Pi).

Sysfs also allows you to write values to these files, which means you can modify kernel parameters on the fly. For example, let’s say you want to set your CPU frequency to 2000 MHz:

# This script sets the CPU frequency to 2000 MHz by writing a value to a sysfs file.
# Sysfs is a virtual file system that provides access to kernel data structures.
# It allows for dynamic modification of kernel parameters.

# The first line is a command that echoes the value "200000" to the specified file.

# Also, the value should be in kHz, not Hz, so it needs to be multiplied by 1000.
# Lastly, the file name should be "scaling_cur_freq" instead of "cpufreq_raw_cur".
echo "200000000" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq


echo "200000000" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq

And that’s it! Your kernel will now use the new CPU frequency. Of course, this is just a simple example there are many other parameters you can modify using sysfs, such as I/O ports, memory timings, and even power management settings. But be careful, because some of these parameters can have unintended consequences if used improperly (like reducing your system’s stability or increasing its power consumption).

SICORPS