The sys folder: the kernel's view of devices
Video 128 of 140 in Virtualization Foundations, in the unit “Processes, signals, /proc and /sys”. 3:11, with English and Hindi audio.
Linux shows its devices as plain files in a folder called sys. Each small file holds one value, such as the speed of a network link or the size of a disk. You do not need a special tool to read them, only cat and ls. This article explains what the sys folder holds, how to read a device's speed and size, and why a label on the hardware can differ from what the kernel reports. It also covers the one serious risk: some of these files can be written, and writing changes the device at once.
What you will learn
- The sys folder is a live view of devices and drivers, and it is filled in by the kernel as you read it.
- Each small file in sys holds one value, such as a speed, a size or a network address.
- A label on a port says what the card can do, while the speed file says what the link is doing now.
- Device names such as enp1s0 or sda differ between machines, so check the list first.
- Writing to a sys file changes the device at once, so read first and write only when you are sure.
The label is not the same as the live speed
A virtualization host has a network port that is labelled as 25 gigabit. The sticker says what the card can do, but the link may have negotiated a lower speed right now. The kernel knows the truth, and it reports the port's state as a value in a file in the sys folder.
The sys folder is another window into the kernel, like the proc folder. Nothing is stored on a disk there. It is arranged by device, and each small file holds one value.
Listing network ports and reading one speed
ls /sys/class/netThe list includes lo, the loopback interface, and the names of the physical ports. Those names vary by machine, so use a name from your own list.
cat /sys/class/net/enp1s0/speedThe speed file gives the link speed in megabits per second. For a 25 gigabit link, the file shows about 25000. If the cable is unplugged, the file may show nothing useful, or an error. That is a clue about the link, not a broken file.
Listing disks and reading a size
ls /sys/blockBlock devices are listed by their kernel names, such as sda or vda. Each disk has a folder there, with a size file inside it.
cat /sys/block/sda/sizeThe number is the size in sectors of 512 bytes, so multiply it by 512 to get the size in bytes.
What the sys folder holds
- class/net: the network interfaces, with their speed, their MTU, their state and their MAC address.
- block: the disks and their sizes, with one folder for each disk.
- devices and bus: the whole tree of hardware, and the drivers that work with each piece.
- Writable files: some files can be written, and writing one changes the device at once.
Read first, and write with care
The bigger danger in sys is writing. Echoing a wrong value into a device file changes the device at once, with no undo. A change made this way is often lost after a reboot, so do not count on it to last.
Note: Interface and disk names change from one machine to another. Check the list before you read or change any entry.
Try it yourself
You need a Linux machine. Every step only reads a value and changes nothing. Use the names from your own lists in place of enp1s0 and sda.
List the network interfaces the kernel knows. The loopback interface lo is always there.
ls /sys/class/netWhat you should see: The list includes lo, and the names of the physical ports on this machine.
Read the link speed of one interface. Replace enp1s0 with a name from the previous list.
cat /sys/class/net/enp1s0/speedWhat you should see: The speed in megabits per second. A 25 gigabit link shows about 25000.
List the block devices, which are the disks, by their kernel names.
ls /sys/blockWhat you should see: The disks appear by their kernel names, such as sda or vda.
Read the size of one disk. Replace sda with a name from the previous list.
cat /sys/block/sda/sizeWhat you should see: A number of 512-byte sectors. Multiply it by 512 to get the size in bytes.
Terms
- sys folder
- A folder that the kernel fills in as you read it, showing devices, drivers and kernel settings, with one value in each small file.
- Block device
- A disk or similar storage device, which Linux lists by a kernel name such as sda in the sys folder.
Check yourself
Why can a port labelled 25 gigabit show a lower speed in sys?
The label says what the card can do. The speed file shows what the link is doing now, which may be lower if it negotiated a slower speed.
How do you get a disk's size in bytes from the size file?
The file counts sectors of 512 bytes, so multiply the number in it by 512.
What is the main risk when you work in the sys folder?
Writing a wrong value into a device file changes the device at once, with no undo.
Previous: What is inside the proc folder for a process. Next: lsof: the files a process holds open.