Introduction
Sysfs (/sys) and the more traditional Devtmpfs (/dev) file systems serve very different purposes. Both of these are different presentations of all the devices attached to the system and both are constructs of the kernel, nevertheless, they are still quite different in the way they present information about the hardware.
In /dev each new device is represented by one and only one file, called a device file or a device node. These files are created to make interacting with these devices easier by making the I/O interface of these devices behave more like the traditional file I/O model (the so-called blocking file I/O model). So as a programmer it would be much easier for you to pretend that these devices are simply files and as per usual can be programmed using the same file I/O operations (Basically any function that takes a file descriptor as an argument). For this specific purpose /dev does quite well and that's the reason why it is still around.
However, in case you want to know about the device behind the device file then /dev proves to be very unhelpful. By design device files in /dev do not present much information about the hardware that they represent, so in case you want to gather some information about some attributes of the hardware attached to a certain device file, the files in /dev are not going to be very useful. Such information, on the other hand, can be easily found in the /sys directory (Sysfs).
The only issue with the /sys directory is that it's not as easy to find the "location" of the device as it is to find the equivalent information in /dev. In /dev you can simply find the device file associated with a device with a simple search and that's basically all the information that there is to know about that device (ls -al also reveals the so-called major and minor device numbers for the device, but these number have more significance to the kernel than to any userspace processes). The path to the device files are also very straightforward like this:
/dev/vda1
That's a device file that represents a device.
Now the question is how to find the device associated with the above device file in /sys file system?
Procedure
To find that information you can simply use the udevadm command as follows:
udevadm info --query=all --name=/dev/vda1
This basically tells you everything that you need to know about how that device is being represented in /sys. Here is the output of the above command:
udevadm info --query=all --name=/dev/vda1
P: /devices/pci0000:00/0000:00:04.0/virtio1/block/vda/vda1
N: vda1
S: disk/by-path/pci-0000:00:04.0-part1
S: disk/by-path/virtio-pci-0000:00:04.0-part1
S: disk/by-uuid/bc2e9cd0-c208-4cd5-a498-acaa76285cf5
E: DEVLINKS=/dev/disk/by-path/pci-0000:00:04.0-part1 /dev/disk/by-path/virtio-pci-0000:00:04.0-part1 /dev/disk/by-uuid/bc2e9cd0-c208-4cd5-a498-acaa76285cf5
E: DEVNAME=/dev/vda1
E: DEVPATH=/devices/pci0000:00/0000:00:04.0/virtio1/block/vda/vda1
E: DEVTYPE=partition
E: ID_FS_TYPE=xfs
E: ID_FS_USAGE=filesystem
E: ID_FS_UUID=bc2e9cd0-c208-4cd5-a498-acaa76285cf5
E: ID_FS_UUID_ENC=bc2e9cd0-c208-4cd5-a498-acaa76285cf5
E: ID_PART_ENTRY_DISK=253:0
E: ID_PART_ENTRY_FLAGS=0x80
E: ID_PART_ENTRY_NUMBER=1
E: ID_PART_ENTRY_OFFSET=2048
E: ID_PART_ENTRY_SCHEME=dos
E: ID_PART_ENTRY_SIZE=83883999
E: ID_PART_ENTRY_TYPE=0x83
E: ID_PART_TABLE_TYPE=dos
E: ID_PATH=pci-0000:00:04.0
E: ID_PATH_TAG=pci-0000_00_04_0
E: MAJOR=253
E: MINOR=1
E: SUBSYSTEM=block
E: TAGS=:systemd:
E: USEC_INITIALIZED=96379
You can of course use all the information presented above, but the most important section is the highlighted section at the top section of the output:
/devices/pci0000:00/0000:00:04.0/virtio1/block/vda/vda1
This is the path to the directory that is representing the vda1 device in Sysfs. You need to first modify the above path by replacing the starting forward slash "/" in the path with the string "/sys" and then you will get this:
/sys/devices/pci0000:00/0000:00:04.0/virtio1/block/vda/vda1
Now you have the absolute path of the directory associated with the vda1 device in Sysfs and if you see the content of this directory (below) you can see all sorts of information about the device and its attributes available inside the directory:
tree -a -L 2 -CA /sys/devices/pci0000:00/0000:00:04.0/virtio1/block/vda/vda1
/sys/devices/pci0000:00/0000:00:04.0/virtio1/block/vda/vda1
├── alignment_offset
├── dev
├── discard_alignment
├── holders
├── inflight
├── partition
├── power
│ ├── async
│ ├── autosuspend_delay_ms
│ ├── control
│ ├── runtime_active_kids
│ ├── runtime_active_time
│ ├── runtime_enabled
│ ├── runtime_status
│ ├── runtime_suspended_time
│ └── runtime_usage
├── ro
├── size
├── start
├── stat
├── subsystem -> ../../../../../../../class/block
├── trace
│ ├── act_mask
│ ├── enable
│ ├── end_lba
│ ├── pid
│ └── start_lba
└── uevent
Comments
0 comments
Article is closed for comments.