Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Are the major, minor number Unique?

Do we have any citations and reference to it?

NAME   MAJ:MIN RM   SIZE RO MOUNTPOINT
sda      8:0    0 465.8G  0 
├─sda1   8:1    0 298.2M  0 
├─sda2   8:2    0     3G  0 
├─sda3   8:3    0 458.7G  0 /
├─sda4   8:4    0     1K  0 
└─sda5   8:5    0   3.8G  0 
sr0     11:0    1  1024M  0 
share|improve this question
    
maj is hardware, minor is sub division of the major hardware –  Kiwy Apr 11 at 8:35

3 Answers 3

up vote 12 down vote accepted

From The Linux Programming Interface, §14.1

Each device file has a major ID number and a minor ID number. The major ID identifies the general class of device, and is used by the kernel to look up the appropriate driver for this type of device. The minor ID uniquely identifies a particular device within a general class. The major and minor IDs of a device file are displayed by the ls ­-l command.

[...]

Each device driver registers its association with a specific major device ID, and this association provides the connection between the device special file and the device. The name of the device file has no relevance when the kernel looks for the device driver.

See also this old (2001) Linux Device Drivers (2e) chapter.

i.e. the intention is to provide a unique mapping of major:minor to device:instance for each type of device. Strictly, you can have two distinct devices with the same major: minor, as long as one is char and one is block:

# ls -l /dev/ram1 /dev/mem
crw-r----- 1 root kmem 1, 1 Jan  1  1970 /dev/mem
brw-rw---- 1 root disk 1, 1 Jan  1  1970 /dev/ram1

On Linux, at any point in time on one system the major:minor numbers for each type of device are unique. The numbers may however change over time, and need not be the same on across different Linux systems (even the same distribution, kernel and hardware). Note that character and block devices have distinct numbering spaces, e.g. block major 1 is assigned to RAM disks, char major 1 is assigned to a set of kernel devices including null and zero.

Historically device majors were (mostly) statically allocated through a registry (also still present, though unmaintained, in the kernel source Documentation/devices.txt). These days many devices are allocated dynamically, this is managed by udev, and the mappings viewable in /proc/devices. The fixed devices still exists in incude/uapi/linux/major.h (recently moved from include/major.h)

Now although the major:minor combination uniquely identifies specific device instances, there's nothing to stop you creating multiple devices nodes (files) that refer to the same device. They don't even have to be created in /dev (but they do have to be on a filesystem that supports creating device nodes, and isn't mounted with the nodev option).

A common use is creating duplicate zero, null and random devices in a chroot:

# find /dev /var/chroot -regextype posix-extended -regex ".*/(zero|null|random)" -type c | 
xargs ls -l
crwxrwxrwx 1 root root 1, 3 2012-11-21 03:22 /dev/null
crw-rw-r-- 1 root root 1, 8 2012-05-07 10:35 /dev/random
crw-rw-rw- 1 root root 1, 5 2012-11-21 03:22 /dev/zero
crwxrwxrwx 1 root root 1, 3 2012-11-21 03:22 /var/chroot/sendmail/dev/null
crw-rw-r-- 1 root root 1, 8 2012-05-07 10:35 /var/chroot/sendmail/dev/random
crw-rw-rw- 1 root root 1, 5 2012-11-21 03:22 /var/chroot/sendmail/dev/zero

The names are just aliases, the kernel doesn't care much about most names or locations, it cares about the major number so that it can select the correct driver, and the driver (usually) cares about the minor number so it can select the correct instance.

Most names are simply convention (though some are defined by POSIX). Note also that one device may register for multiple major numbers, check the sd driver in /proc/devices; a driver module name (.ko) need not be the same as the device name, and need not be the same as the device node in /dev, and a single driver module may manage multiple logical/physical devices or device names.


To recap: you may have two or more device nodes (in /dev/ or elsewhere) which have the same major:minor numbers, but if they are the same type they refer to the same device. You can have one driver which can handle multiple major instances, but within the kernel and within the driver, for each type (char or block) the major:minor number is taken to refer to a specific device (major) and a specific instance (minor) of the device.

You cannot have two device nodes with the same type and major:minor and expect them to access two different logical or physical devices. When a device is being accessed the kernel select one driver based on the type and the major number (and not based on the the device node name), and by convention the minor number deterministically selects a specific instance or sub-function.


Update Some interesting history and some *BSD perspective can be found in Poul-Henning Kamp's 2002 BSDCon presentation: https://www.usenix.org/legacy/events/bsdcon/full_papers/kamp/kamp_html/

If you leap back in time to 1978 (courtesy of Alcatel-Lucent, the Bell System Technical Journal Jul-Aug 1978) the 'Unix Time Sharing System' sets it out clearly (p1937):

Devices are characterized by a major device number, a minor device number, and a class (block or character). For each class, there is an array of entry points into the device drivers. The major device number is used to index the array when calling the code for a particular device driver. The minor device number is passed to the device driver as an argument. The minor number has no significance other than that attributed to it by the driver. Usually, the driver uses the minor number to access one of several identical physical devices.

share|improve this answer
    
So can we have two devices with same pair or MAJ:Min number –  Pilot Apr 11 at 9:27
1  
Yes, with constraints. Updated. –  mr.spuratic Apr 11 at 10:26
    
great answer..Thanks a ton.. –  Pilot Apr 11 at 10:30

When a device file is created by mknode, the major and minor numbers are supplied. These are how Linux identifies the underlying hardware device associated with a device file. In most cases they major number identifies the driver while the minor distinguishes the different devices the driver controls.

As such the numbers must be unique to each device or it would not be possible to create correct device files for all of them.

share|improve this answer

Major Number for SCSI disks is 8.

There will be a separate major number for every disk type.

As @Kiwy said in his comment, major number is used to identify disk type, and the minor number represents sub division of major hardware disk.

enter image description here

Source

In the above screenshot, you can see that the major number for encrypted lvm partitions are differ from the ordinary partitions.

share|improve this answer
    
Hey avi I just want to make sure if MAJ:MIN are unique for any devices (either LVM). I am aware of the fact the major number identifies the driver associated with the device –  Pilot Apr 11 at 8:54
    
min number should be unique.But there will be a separate major number for every disk type. –  Avinash Raj Apr 11 at 8:59
    
for lvm partitions, the major number should differ from other SCSI partitions. –  Avinash Raj Apr 11 at 9:00
    
now why do you need to put a screenshot instead of copy-past the content of your temrinal –  Kiwy Apr 11 at 9:09
    
@Kiwy i can't able to copy. The above screenshot is taken from Ubuntu server which is actually running in VBox. –  Avinash Raj Apr 11 at 9:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.