What do device type numbers mean

block-devicecharacter-special-filesdevices

file /dev/* prints the description of a bunch of files as "block special (M/N)" and "character special (M/N)", where M and N are numbers. For example:

$ file /dev/null
/dev/null: character special (1/3)

man file doesn't seem to document these, but refers vaguely to man stat, which seems to refer to these as major and minor device types. apropos 'device type' finds nothing. So what do these numbers mean?

Best Answer

Devices on Unix have a type (e.g. character or block), a major number (which typically refers to a driver), and a minor number (which typically refers to an instance).

So, for example:

% ls -l /dev/vda
brw-rw---- 1 root disk 253, 0 Feb  3 09:09 /dev/vda

This is a block device, major 253, minor 0.

If we look at /proc/devices we see it ends with something similar to

Block devices:
  2 fd
259 blkext
  9 md
253 virtblk
254 mdp

So we can see that 253 is "virtblk". Which makes sense, since this is a virtual machine with virtual disks!

The minor number, for this driver, refers to the block device and partition in the device

% ls -l /dev/vd*
brw-rw---- 1 root disk 253,  0 Feb  3 09:09 /dev/vda
brw-rw---- 1 root disk 253,  1 Feb  3 09:09 /dev/vda1
brw-rw---- 1 root disk 253,  2 Feb  3 09:09 /dev/vda2
brw-rw---- 1 root disk 253,  3 Feb  3 09:09 /dev/vda3
brw-rw---- 1 root disk 253, 16 Feb  3 09:09 /dev/vdb
brw-rw---- 1 root disk 253, 32 Feb  3 09:09 /dev/vdc
brw-rw---- 1 root disk 253, 33 Feb  3 09:09 /dev/vdc1

There are some special drivers which don't refer to "real" hardware. eg

% ls -l /dev/null
crw-rw-rw- 1 root root 1, 3 Feb  3 09:09 /dev/null

This is a character device, major 1, minor 3. /proc/devices tells us driver 1

  1 mem

We can see this "mem" driver handles a few other devices as well

% ls -l /dev | grep ' 1, '
crw-rw-rw- 1 root root      1,   7 Feb  3 09:09 full
crw-r--r-- 1 root root      1,  11 Feb  3 09:09 kmsg
crw-r----- 1 root kmem      1,   1 Feb  3 09:09 mem
crw-rw-rw- 1 root root      1,   3 Feb  3 09:09 null
crw------- 1 root root      1,  12 Feb  3 09:09 oldmem
crw-r----- 1 root kmem      1,   4 Feb  3 09:09 port
crw-rw-rw- 1 root root      1,   8 Feb  3 09:09 random
crw-rw-rw- 1 root root      1,   9 Feb  3 09:09 urandom
crw-rw-rw- 1 root root      1,   5 Feb  3 09:09 zero
Related Question