Diferença entre / dev e / sys / class?

29

Qual é a diferença entre a representação do dispositivo em /dev e a em /sys/class ?

É um preferido em detrimento do outro? Existe algo que se oferece e o outro não?

    
por TheMeaningfulEngineer 14.03.2014 / 12:27

1 resposta

24

Os arquivos em /dev são arquivos de dispositivos reais que o UDEV cria em tempo de execução. O diretório /sys/class é exportado pelo kernel no tempo de execução, expondo a hierarquia do hardware através de sysfs .

Do tutorial do libudev e do Sysfs

trecho

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.

outro trecho

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.

Uso?

Em geral, você usa regras em /etc/udev/rules.d para aumentar seu sistema. Regras podem ser construídas para executar scripts quando vários hardwares estiverem presentes.

Uma vez que o sistema está ativo, você pode escrever scripts para trabalhar com /dev ou /sys , e isso realmente se resume a preferências pessoais, mas eu normalmente tentaria trabalhar com /sys e fazer uso de ferramentas como como udevadm para consultar o UDEV para localizações de vários recursos do sistema.

$ 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"
...
    
por 14.03.2014 / 13:49