Which /dev/ device is my piece of hardware associated with?

Hi,

I plugged in my USB camera the other day and on a hunch figured it would be associated with /dev/video0 (or something close to it). I got lucky. Is there a place I can go to to see a list of devices and associated so I don't have to be so lucky?

Thanks for the help.

devices, files, and autodetect

Well, it would be more accurate to say that most hardware devices are made available to user programs as "files" in the /dev directory. But, /etc/fstab has nothing to do with creating a "device designation".

So, how does it all fit together? Let's start at the end, and work backwards.

/etc/fstab is a file used by the mount(8) program to establish the relationships between certain "block special" devices and directories in the filesystem. It is, in effect, a configuration file for the "mount" command. To qualify for inclusion in fstab, the device must already exist, as must the directory. Not all devices are named in fstab, and neither are all directories.

So, what do fstab(5) and mount(8) do? In the MSDOS/MSWindows world, each "disk" is the root of it's own "tree" of directories, and each "disk" is separate from every other "disk". OTHO, in the Linux world, there is only one "tree" of directories, and each "disk" is given part of that tree to manage. /etc/fstab tells the "mount" system tool where to place "disks" within the filesystem tree. "disks" that are not named may still exist, but are not attached to the filesystem tree, and files and directories on those disks are not accessable through the tree. Only the directories that provide the attachment points ("mount points") are named in fstab; there may be more directories, but they don't officially host mount points.

So, /etc/fstab is the configuration file that tells mount which devices host what parts of the filesystem directory tree, nothing more.

Up the chain a bit, we find the /dev directory. What's in this directory, you ask? Well, in here you find "special" files that represent devices. These special files can be used by programs just the same way normal files can be used, but instead of manipulating data in a "file", they manipulate data on a "device".

To give you an example, /dev/hda3 is the name of the device file associated to a specific partition on my hard disk. I can echo Bla bla bla >/dev/hda3, and I'll write 12 bytes of data to the device in exactly the same way that echo Bla bla bla >/tmp/a_file would write to a file. That's all that the /dev devices are; files that lead directly to the hardware instead of an abstraction in the "file system".

There are two types of device files: "character special" and "block special". "Character special" device files are devices that take data character at a time, and don't offer random access to the data. These are things like serial ports, parallel ports, modems, mice, etc. "Block special" devices take data in fixed-size blocks (typically, 512 bytes at a time), and offer random access to the data. These are things like hard disks and ram disks. You can tell which of these two types a device is by looking at the "long" information from a ls command.

bash-3.00$ ls -l /dev/ttyS0 /dev/hda3
brw-rw---- 1 root disk 3, 3 2002-06-09 15:27 /dev/hda3
crw-rw---- 1 root uucp 4, 64 1994-07-17 19:48 /dev/ttyS0

In the above example output, the "b" in "brw-rw----" (on the /dev/hda3 line) indicates that /dev/hda3 is a "block special" device. Similarly, the "c" in "crw-rw----" (on the /dev/ttyS0 line) indicates that /dev/ttyS0 is a "character special" device.

So, what else can we see there? Well, we can see that /dev/hda3 has a "major device number" of "3" and a "minor device number of "3" while /dev/ttyS0 has a "major device number" of "4" and a "minor device number" of "64".

The major device number is a number that associates each device file with it's driver program. So, all devices with a major device number of "3" use the same driver, and it is a different driver that the one used by those devices with a major device number of "4".

The minor device number is a number that uniquely identifies each specific device that a device driver can manage. Major device number "3" can handle 128 minor devices, of which /dev/hda3 is minor device 3.

Now, there are a couple of ways that these "device" files can get into the /dev directory. You can actually "make" these files (if you are the root user) by using the mknod(1) command. This builds a device file statically; the name, major and minor device numbers are all 'fixed' when you run the command, and do not change or disappear, even if there is no actual hardware to connect to the device driver. The names given to these device files is up to the person who runs the "mknod" command; there is a standard, but it is up to the person to determine if and when to follow the standard.

The other way that these device files get built is through magic in the Linux kernel (and a lot of help from some userland tools). As the kernel boots up, it can take inventory of some of the hardware on your system. Some of the userland programs that are run at startup take this inventory, and (in effect) "mknod" the equivalent device files in the /dev directory. As this is an automated dynamic process, there is less latitude in the names given to each device. Again, there is a standard, but this time the name is fixed (in a config file) and the automated tool isn't given the latitude to vary from that name. If the kernel device autodetect detects a specific type of device, the tools give it a specific name.

I'm being deliberatly vague in the naming of the autodetected devices, as there are a couple of ways the names can be established, and there is some latitude given to the distributions as to how they implement all this. This "dynamic" device stuff isn't even required, so some distros don't even use it at all.

There is, however, a "master list" of device numbers and their associated device descriptions. If you installed the Linux kernel source, you will find the list in the "Documentation/" directory, as the file devices.txt. The file starts off with

LINUX ALLOCATED DEVICES
Maintained by H. Peter Anvin

Last revised: 3 June 2001

This list is the Linux Device List, the official registry of allocated
device numbers and /dev directory nodes for the Linux operating
system.

The latest version of this list is available from
http://www.lanana.org/docs/device-list/ or
ftp://ftp.kernel.org/pub/linux/docs/device-list/. This version may be
newer than the one distributed with the Linux kernel.

The LaTeX version of this document is no longer maintained.

This document is included by reference into the Filesystem Hierarchy
Standard (FHS). The FHS is available from http://www.pathname.com/fhs/.

HTH
--
Lew Pitcher

Master Codewright & JOAT-in-training
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.

Post new comment