[Techtalk] Major and minor device numbers

Akkana akkana at shallowsky.com
Sun Aug 18 15:06:33 EST 2002

hobbit at aloss.ukuu.org.uk writes:
> On Sun, Aug 18, 2002 at 10:13:32AM -0700 or thereabouts, Conni wrote:
> > Second problem:  I am not very technically inclined.  How do I find out
> > what minor device my system will use?  
> This is something I also would like an answer to. I don't understand
> these major and minor things. Every so often an ls -l produces a
> result with what I think are device numbers in the output, and
> they baffle me. :)

(Val, Hanna or anyone else, please correct me if I get any of this wrong!)

Okay, first let's look at ls -l for a file and two devices:

-rw-r--r--    1 root     root           94 Aug 16 15:43 /tmp/foo
crw-rw-rw-    1 root     root      10,  61 Aug  9 23:31 /dev/sonypi
brw-rw----    1 root     disk       3,   0 Apr 11 07:25 /dev/hda
brw-rw----    1 root     disk       3,  65 Apr 11 07:25 /dev/hdb1

There are two differences here worth noting:

1. The very first character in the line is '-' for a file, but
for devices it is 'c' or 'b'.  That means the device is a
"character special file" or "block special file", where "special
file" really means "device" (you can just say "char device" and
everyone will understand you).

Block devices are devices which tend to be accessed in fairly
big chunks; disks, CD drives and things like that where you want
to pump large amounts of data into or out of the device.
Character devices are anything else; often devices that do
something useful which doesn't involve transferring a lot of
data.  For instance, in the case of sonypi, you just want to read
a few bytes every now and then that say "the user clicked the wheel"
or "the user rolled the wheel down one stop".

2. Where the file has a size in bytes (94 bytes in this case),
the devices have two numbers separated by a comma.  These numbers
have nothing to do with size or bytes; they are the device's major
and minor device IDs.

What are these numbers?

Basically, the kernel keeps an internal list of all the device drivers
it knows about.  You want the kernel to be fast at going and getting the
right driver when you read or write a device; so you don't want it to be
looking at the driver name (like "sonypi") and then going through its
whole list of drivers doing string-compares on the name.  So to speed up
and simplify access to this driver table, each type of device has a
unique number, and when you access a "special file", its major number
tells the kernel which driver to use (sometimes it also needs the minor
number); and if the kernel doesn't already have a driver corresponding
to that device number, then it tries to figure out whether it has a
module it can load which will give it that driver.  The file
/etc/modules.conf contains some hints for the kernel about which
modules might implement driver for which devices.

The minor number is usually used to pass more information to the driver.
For example, in the listing above, /dev/hda and /dev/hdb1 are both
controlled by the same block driver (major number 3) but have different
minor numbers (0 vs. 65).  For that particular driver (an IDE disk
driver), the minor number contains information about which controller
and channel the disk is on, and which partition on that disk it should

Some major numbers are dumping-grounds which lots of drivers use.
In the case of sonypi, major number 10, it turns out that lots of
devices share that same major number: if you want to see the complete
list of major-10 char devices, this command will get it for you:
ls -l /dev | grep " 10," | egrep "^c"
So in the case of major 10, before it can choose a driver the kernel
has to look at the minor number as well, since if the minor is 1 then
it needs to load the PS/2 mouse driver, whereas (on my system, anyway)
if the minor number is 135 then it should load the driver for the
realtime clock instead.  If I put a line in /etc/modules.conf saying:
alias char-major-10-61 sonypi
then that tells the kernel that if a program (such as sonypid) asks
for the char device with major # 10 and minor # 61, it should search
in its module list (/lib/modules/[kernelversion]) for a driver named
sonypi, load that driver, and use it for any further read/write requests
that program might have on that file.

Did any of that make any sense?


More information about the Techtalk mailing list