knowing a device special file major and minor numbers in linux
All files in /dev are special files. they represent devices of the computer. They were created with the mknod syscall. My question is: How can I know the minor and major numbers that were used to create this special file?
5 Answers 5
The list is called the LANANA Linux Device List, and it is administered by Alan Cox.
You can find the latest copy online (direct link), or in the Linux source. Its filename in the kernel tree is Documentation/devices.txt .
To see the major and minor numbers that created a node in /dev (or any device node for that matter), simply use ls with the -l option:
22:26 jsmith@undertow% ls -l /dev/xvd? brw-rw---- 1 root disk 202, 0 Nov 1 20:31 /dev/xvda brw-rw---- 1 root disk 202, 16 Nov 1 20:31 /dev/xvdb brw-rw---- 1 root disk 202, 32 Nov 1 20:31 /dev/xvdc
In this example, 202 is the three devices’ major number, and 0 , 16 , and 32 are minors. The b at left indicates that the node is a block device. The alternative is c , a character device:
crw-rw-rw- 1 root tty 5, 0 Nov 22 00:29 /dev/tty
Looks like the lanana.org domain has expired («lanana.org expired on 04/10/2012 and is pending renewal or deletion»). I hope that gets fixed — looks like it might be a bit of a recurring problem: wiki.linuxfoundation.org/en/Minutes_Apr_27_2011
$ ls -l /dev/fd0 /dev/null brw-rw---- 1 root floppy 2, 0 Nov 22 19:48 /dev/fd0 crw-rw-rw- 1 root root 1, 3 Nov 22 19:48 /dev/null $ stat -c '%n: %F, major %t minor %T' /dev/fd0 /dev/null /dev/fd0: block special file, major 2 minor 0 /dev/null: character special file, major 1 minor 3
Most device numbers are fixed (i.e. /dev/null will always be character device 1:3 ) but on Linux, some are dynamically allocated.
$ cat /proc/devices Character devices: . 10 misc . Block devices: . 253 mdp 254 device-mapper $ cat /proc/misc . 57 device-mapper .
For example, on this system, it just so happens that /dev/mapper/control will be c:10:57 while the rest of /dev/mapper/* will be b:254:* , and this could differ from one boot cycle to another — or even as modules are loaded/unloaded and devices are added/removed.
You can explore these device registrations further in /sys .
$ readlink /sys/dev/block/2:0 ../../devices/platform/floppy.0/block/fd0 $ cat /sys/devices/platform/floppy.0/block/fd0/dev 2:0 $ readlink /sys/dev/char/1:3 ../../devices/virtual/mem/null $ cat /sys/devices/virtual/mem/null/dev 1:3
Linux : Major and Minor device numbers
What types of devices fall under the major device number category and what types of devices fall under the minor device number category. What is the real difference between the two categories?
3 Answers 3
All devices have a major, minor number pair. The major number is a larger, more generic category (e.g. hard disks, input/output devices etc. ) while the minor number is more specific (i.e. tells what bus the device is connected to).
The list of device numbers is no longer at that location in the Linux kernel source tree. Do you know where it moved to?
A common major device number is 7 (near «Loopback devices»), e.g. /dev/loop0 , /dev/loop1 , . /dev/loop34 (e.g., as used in lsblk -e7 to reduce the output).
The major device number identifies the driver (e.g. IDE disk drive, floppy disk, parallel port, serial port, . ) or sometimes a peripheral card (first IDE card, second IDE card of the PC) and the minor number identifies the specific device (i.e., the first floppy would have minor 0, the second would be 1, . ).
If we had a hard disk, floppy disk, and a parallel port, would the hard disk be minor 0, floppy disk minor 1, and parallel port minor 2?
No. Major numbers are fixed. E.g. SCSI drives at major 8. The first SCSI drive is (8,0), the second drive (8,1), the third (8,2) etc etc. Even if you have no other major devices that will still stay fixed at 8. Same with every other device. The M coded into the drivers and will always be the same.
@Referential, no. The first IDE controller has major number 3, on it the master is block minor 0 and the slave 1; floppy major is block 2, parallel port is char 6, minor identifies the port. The number belongs to the driver, independent of a device being present (or even if the driver is compiled into the kernel at all). LANANA (Linux Assigned Names And Numbers Authority) is in charge of allocating them.
Which types of devices would specifically fall under the minor device number category? I thought that the minor device number category mirrors the major device number (eg: hard disk 1 has a major device number and the minor number identifies it as a hard drive). If someone can please give me a few more examples. Thanks
@Referential There are a few devices that can only exist in one copy, they don’t have minor device numbers. Each time a device can be present seveal times, they are numbered by a minor number. Major is device type (driver), minor is device number (instance).
The role of major and minor numbers in device driver
I think the story should starts from what happened when you type:
it will call ext2_mknod(«/dev», «c83», CHAR, DEV(8, 3)), most file systems implement mknod as a wrapper of init_special_inode:
void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev) < inode->i_mode = mode; if (S_ISCHR(mode)) < inode->i_fop = &def_chr_fops; inode->i_rdev = rdev; > else if (S_ISBLK(mode)) < inode->i_fop = &def_blk_fops; inode->i_rdev = rdev; > else if (S_ISFIFO(mode)) inode->i_fop = &def_fifo_fops; else if (S_ISSOCK(mode)) inode->i_fop = &bad_sock_fops; else printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o)\n", mode); >
when you finally call open(«/dev/c83»), it will goes into function def_chr_fops.chrdev_open, which will replace the fops for file «/dev/c83» with the fops your registered in cdev_init():
int chrdev_open(struct inode * inode, struct file * filp) < struct cdev *p; . p = inode->i_cdev; . filp->f_op = fops_get(p->ops); . if (filp->f_op->open) < lock_kernel(); ret = filp->f_op->open(inode,filp); unlock_kernel(); > . return ret; >
after that, every system call such as read/write/close goes directly to functions pointers registered in cdev_init()!
so, for your first question:
- yes, as you see in chrdev_open().
- yes, since the fops of devices is really same as the one registered in cdev_init
- plays a importance role in open(), since open() use pairs to find the correct device drivers. but after that, other file operations, such as read/write/close(), don’t take part in again, everything goes through function pointers in fops resolved in open().