The files in /dev
are actual devices files which UDEV creates at run time. The directory /sys/class
is exported by the kernel at run time, exposing the hierarchy of the hardware through sysfs
.
From the libudev and Sysfs Tutorial
excerpt
On Unix and Unix-like systems, hardware devices are accessed through special files (also called device files or nodes) located in the /dev directory. These files are read from and written to just like normal files, but instead of writing and reading data on a disk, they communicate directly with a kernel driver which then communicates with the hardware. There are many online resources describing /dev files in more detail. Traditonally, these special files were created at install time by the distribution, using the mknod command. In recent years, Linux systems began using udev to manage these /dev files at runtime. For example, udev will create nodes when devices are detected and delete them when devices are removed (including hotplug devices at runtime). This way, the /dev directory contains (for the most part) only entries for devices which actually exist on the system at the current time, as opposed to devices which could exist.
another excerpt
The directories in Sysfs contain the heirarchy of devices, as they are attached to the computer. For example, on my computer, the hidraw0 device is located under:
/sys/devices/pci0000:00/0000:00:12.2/usb1/1-5/1-5.4/1-5.4:1.0/0003:04D8:003F.0001/hidraw/hidraw0
Based on the path, the device is attached to (roughly, starting from the end) configuration 1 (:1.0) of the device attached to port number 4 of device 1-5, connected to USB controller 1 (usb1), connected to the PCI bus. While interesting, this directory path doesn't do us very much good, since it's dependent on how the hardware is physically connected to the computer.
Fortunately, Sysfs also provides a large number of symlinks, for easy access to devices without having to know which PCI and USB ports they are connected to. In /sys/class there is a directory for each different class of device.
Usage?
In general you use rules in /etc/udev/rules.d
to augment your system. Rules can be constructed to run scripts when various hardware is present.
Once a system is up you can write scripts to work against either /dev
or /sys
, and it really comes down to personal preferences, but I would usually try and work against /sys
and make use of tools such as udevadm
to query UDEV for locations of various system resources.
$ udevadm info -a -p $(udevadm info -q path -n /dev/sda) | head -15
Udevadm info starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.
looking at device '/devices/pci0000:00/0000:00:1f.2/ata1/host0/target0:0:0/0:0:0:0/block/sda':
KERNEL=="sda"
SUBSYSTEM=="block"
DRIVER==""
ATTR{ro}=="0"
ATTR{size}=="976773168"
ATTR{stat}==" 6951659 2950164 183733008 41904530 16928577 18806302 597365181 580435555 0 138442293 622621324"
ATTR{range}=="16"
...
The /sys
directory is generally where the sysfs filestystem is mounted, which contains information about devices and other kernel information.
The files in /sys/block
contain information about block devices on your system. Your local system has a block device named sda
, so /sys/block/sda
exists. Your Amazon instance has a device named xvda
, so /sys/block/xvda
exists.
The /sys/block/<dev>/stat
file provides several statistics about the state of block device <dev>
. It consists of a single line of text containing 11 decimal values separated by whitespace:
Name units description
---- ----- -----------
read I/Os requests number of read I/Os processed
read merges requests number of read I/Os merged with in-queue I/O
read sectors sectors number of sectors read
read ticks milliseconds total wait time for read requests
write I/Os requests number of write I/Os processed
write merges requests number of write I/Os merged with in-queue I/O
write sectors sectors number of sectors written
write ticks milliseconds total wait time for write requests
in_flight requests number of I/Os currently in flight
io_ticks milliseconds total time this block device has been active
time_in_queue milliseconds total wait time for all requests
So, each block device will have its own stat
istics file, hence the different values.
See kernel docs for more details.
Best Answer
The
/sys
filesystem (sysfs) contains files that provide information about devices: whether it's powered on, the vendor name and model, what bus the device is plugged into, etc. It's of interest to applications that manage devices.The
/dev
filesystem contains files that allow programs to access the devices themselves: write data to a serial port, read a hard disk, etc. It's of interest to applications that access devices.A metaphor is that
/sys
provides access to the packaging, while/dev
provides access to the content of the box.The files in
/sys
are not device nodes, but symbolic links and regular files. Those regular files are special in that reading or writing to them invokes file-specific functions in the kernel, like device nodes. The difference is that files in/sys
work this way because of the filesystem they are on, whereas device nodes work this way due to their device node characteristics (the file type indicating a (block or character) device, and the device major and minor number indicating which device it is).The reason for
/dev
existing independently of/sys
is partly historical:/dev
dates back to the dawn of Unix, while/sys
is a much more recent invention. If Linux was designed today with no historical background,/dev/sda
might be/sys/block/sda/content
.