Totor, I added the bounty because another user posted the same question because he felt that this one had not received enough attention. I asked him to delete his question and offered a bounty on this one to get more answers. Please remember to accept one of the answers below if they answer your question.
–
terdon♦Apr 24 '14 at 11:07

@terdon thanks for the bounty, it produced nice answers. I have not carefully tested everything yet, but will accept Graeme's answer in the meanwhile.
–
TotorApr 30 '14 at 13:25

4 Answers
4

To get this information from sysfs for a device file, first determine the major/minor number by looking at the output of ls -l, eg

$ ls -l /dev/sda
brw-rw---- 1 root disk 8, 0 Apr 17 12:26 /dev/sda

The 8, 0 tells us that major number is 8 and the minor is 0. The b at the start of the listing also tells us that it is a block device. Other devices may have a c for character device at the start.

If you then look under /sys/dev, you will see there are two directories. One called block and one called char. The no-brainer here is that these are for block and character devices respectively. Each device is then accessible by its major/minor number is this directory. If there is a driver available for the device, it can be found by reading the target of the driver link in this or the device sub-directory. Eg, for my /dev/sda I can simply do:

Note that the existence of various directories in /sys may change depending on the kernel configuration. Also not all devices have a device subfolder. For example, this is the case for partition device files like /dev/sda1. Here you have to access the device for the whole disk (unfortunately there are no sys links for this).

A final thing which can be useful to do is to list the drivers for all devices for which they are available. For this you can use globs to select all the directories in which the driver links are present. Eg:

Finally, to diverge from the question a bit, I will add another /sys glob trick to get a much broader perspective on which drivers are being used by which devices (though not necessarily those with a device file):

find /sys/bus/*/drivers/* -maxdepth 1 -lname '*devices*' -ls

Update

Looking more closely at the output of udevadm, it appears to work by finding the canonical /sys directory (as you would get if you dereferenced the major/minor directories above), then working its way up the directory tree, printing out any information that it finds. This way you get information about parent devices and any drivers they use as well.

To experiment with this I wrote the script below to walk up the directory tree and display information at each relevant level. udev seems to look for readable files at each level, with their names and contents being incorporated in ATTRS. Instead of doing this I display the contents of the uevent files at each level (seemingly the presence of this defines a distinct level rather than just a subdirectory). I also show the basename of any subsystem links I find and this showing how the device fits in this hierarchy. udevadm does not display the same information, so this is a nice complementary tool. The parent device information (eg PCI information) is also useful if you want to match the output of other tools like lshw to higher level devices.

Is there any way to determine all the drivers being used? Like for example the udevadm answer will give you sd and ahci. Is there a way to determine ahci is being used as well?
–
PatrickApr 17 '14 at 21:29