Major and Minor Numbers

来源:互联网 发布:matlab 矩阵转cell 编辑:程序博客网 时间:2024/06/05 10:39

 One of the basic features of the Linux kernel is that it abstracts the handling of devices.All hardware devices look like regular files; they can be opened, closed, readandwritten using the same, standard, system calls that are used to manipulatefiles.Every device in the system is represented by a file.For block (disk) and character devices, these device files are createdby themknod commandand they describe the device using major and minor device numbers.Network devices are also represented by device special files but they arecreated by Linux as it finds andinitializes the network controllers in the system.

To UNIX,everything is a file. To write to the hard disk, you write to a file. To readfrom the keyboard is to read from a file. To store backups on a tape device isto write to a file. Even to read from memory is to read from a file. If the filefrom which you are trying to read or to which you are trying to write is a"normal" file, the process is fairly easy to understand: the file is opened andyou read or write data. If, however, the device you want to access is a specialdevice file (also referred to as a device node), a fair bit of work needs to bedone before the read or write operation can begin.

One key aspect ofunderstanding device files lies in the fact that different devices behave andreact differently. There are no keys on a hard disk and no sectors on akeyboard, though you can read from both. The system, therefore, needs amechanism whereby it can distinguish between the various types of devices andbehave accordingly.

To access a device accordingly, theoperating system must be told what to do. Obviously, themanner in which the kernel accesses a hard disk will be different from the wayit accesses a terminal. Both can be read from and writtento, but that's about where the similarities end. To access each of these totallydifferent kinds of devices, thekernel needs to know thatthey are, in fact, different.

Inside the kernel arefunctions for each of the devices the kernel is going to access. All theroutines for a specific device are jointly referred to as thedevice driver. Each device on the system has its owndevice driver. Within each device driver are the functionsthat are used to access the device. For devices such as a hard disk orterminal, the system needs to be able to (among otherthings) open the device, write to the device, read from the device, and closethe device. Therefore, the respective drivers will contain the routines neededto open, write to, read from, and close (among other things) those devices.

The kernel needs to be told how to access the device.Not only does the kernel need to be told what kind ofdevice is being accessed but also any special information, such as thepartition number if it's a hard disk or density if it's afloppy, for example. This is accomplished by themajor number andminor number of that device.

All devices controlled by the same device driver have a common major devicenumber.The minor device numbers are used to distinguish between different devices andtheir controllers.Linux maps the device special file passed in system calls (say to mount a filesystem on a block device) to the device's device driver using the major devicenumber and a number of system tables, for example the character device table, chrdevs.The major number is actually theoffset into the kernel'sdevice driver table, which tellsthe kernel what kind of device it is (whether it is a harddisk or a serial terminal). Theminor number tells thekernel special characteristics of the device to be accessed. For example, thesecond hard disk has a different minor number than thefirst. The COM1 port has a different minor number than the COM2 port,each partition on the primary IDE disk has a differentminor device number, and so forth. So, for example, /dev/hda2, the second partition of the primary IDE disk has a major number of 3 and a minor number of 2.

Itis through this table that the routines are accessed that, in turn, access thephysical hardware. Once thekernel has determined what kindof device to which it is talking, it determines the specific device, thespecific location, or other characteristics of the device by means of theminor number.

The major number for the hd (IDE) driver is hard-coded at 3.The minor numbers have the format

(<unit>*64)+<part>

where <unit>is the IDE drive number on the first controller, either 0 or 1, which isthen multiplied by 64. That means that all hd devices on the firstIDE drive have aminor number lessthan 64. <part> is thepartition number, which can beanything from 1 to 20. Which minor numbers you will be able to access willdepend on how many partitions you have and what kind they are (extended,logical, etc.). Theminor number of the device node thatrepresents the whole disk is 0. This has the node name hda, whereas the otherdevice nodes have a name equal to their minor number (i.e., /dev/hda6 has a minor number 6).

If you were tohave a second IDE on the first controller, the unit numberwould be 1. Therefore, all of the minor numbers would be 64 or greater. Theminor number of the device node representing the whole diskis 1. This has the node name hdb, whereas the otherdevice nodes have a name equal to their minor numberplus 64 (i.e., /dev/hdb6 has a minor number 70).

If you have more thanone IDE controller, the principle is the same. The onlydifference is that the major number is 22.

ForSCSI devices, the scheme is a little different. When youhave aSCSIhost adapter, you canhave up to seven hard disks. Therefore, we need a different way to refer to thepartitions. In general, the format of the device nodes is

sd<drive><partition>

where sd refers to theSCSI disk driver, <drive> is a letter for thephysical drive, and <partition> is thepartitionnumber. Like the hd devices, when a device refers to the entire disk, forexample the device sda refers to the first disk.

The major number for allSCSI drives is 8. Theminor number is based on the drive number, which ismultiplied by 16 instead of 64, like theIDE drives. Thepartition number is then added to this number to give theminor.

The partition numbers are not as simple tofigure out. Partition 0 is for the whole disk (i.e., sda). The fourDOS primary partitions are numbered 14. Then the extendedpartitions are numbered 58. We then add 16 for each drive. For example:

brw-rw---- 1 root disk 8, 22 Sep 12 1994/dev/sdb6

Because b is after the sd, we know that this is on thesecond drive. Subtracting 16 from the minor, we get 6, which matches thepartition number. Because it is between 4 and 8, we knowthat this is on an extended partition. This is the secondpartition on the first extended partition.

The floppy devices have aneven more peculiar way of assigning minor numbers. Themajor number is fairly easy its 2. Because the names are a little easier tofigure out, lets start with them. As you might guess, the device names all beginwith fd. The general format is

fd<drive><density><capacity>

where <drive> isthe drive number (0 for A:, 1 for B:), <density> is the density (d-double,h-high), and <capacity> is the capacity of the drive (360Kb, 1440Kb). Youcan also tell the size of the drive by the density letter, lowercase letterindicates that it is a i5.25" drive and an uppercase letter indicates that it isa 3.5" driver. For example, a low-density 5.25" drive with a capacity of 360Kbwould look like

fd0d360

If your second drive was a high-density3.5" drive with a capacity of 1440Kb, the device would look like

fd1H1440

What the minor numbers represents is a fairly complicated process. Inthe fd(4)man-page there is an explanatory table, but it isnot obvious from the table why specific minor numbers go with each device. Theproblem is that there is no logical progression as with the hard disks. Forexample, there was never a 3.5" with a capacity of 1200Kb nor has there been a5.25" with a capacity of 1.44Mb. So you will never find a device with H1200 orh1440. So to figure out the device names, the best thing is to look at theman-page.

The terminaldevices come in a few forms. The first is the system console, which are thedevices tty0-tty?. You can have up to 64 virtual terminals on you systemconsole, although most systems that I have seen are limited five or six. Allconsole terminals have a major number of 4. As we discussedearlier, you can reach the low numbered ones with ALT-Fn, where n is the numberof the function key. So ALT-F4 gets you to the fourth virtual console. Both theminor number and the tty number are based on the functionkey, so /dev/tty4 has a minor number 4 and you get to itwith ALT-F4. (Check the console(4) man-page to see how touse and get to the other virtual terminals.)

Serial devices can alsohave terminals hooked up to them. These terminals normally use the devices/dev/ttySn, where n is the number of the serial port (0, 1, 2, etc.). These alsohave a minor number of 4, but the minor numbers all start at 64. (Thats why youcan only have 63 virtual consoles.) The minor numbers are this base of 64, plusthe serial port number (04). Therefore, the minor number of the third serialport would be 64+3=67.

Related to these devices are the modem controldevices, which are used to access modems. These have the same minor numbers buthave a major number of 5. The names are also based on the device number.Therefore, themodem device attached to the third serialport has a minor number of 64+3=67 and its name is cua3.

Another device with a major number of 5 is/dev/tty, which has a minor number of 0. This is a specialdevice and is referred to as the "controllingterminal. "This is the terminal device for the currently running process. Therefore, nomatter where you are, no matter what you are running, sending something to/dev/tty will always appear on your screen.

The pseudo-terminals (thosethat you use with network connections or X) actually comein pairs. The "slave" is the device at which you type and has a name like ttyp?,where ? is the tty number. The device that the process sees is the "master" andhas a name like ptyn, where n is the device number. These also have amajor number 4. However, the master devices all have minornumbers based on 128. Therefore, pty0 has aminor numberof 128 and pty9 has a minor number of 137 (128+9). The slave device has minornumbers based on 192, so the slave device ttyp0 has aminor numberof 192. Note that the tty numbers do not increase numericallyafter 9 but use the letter af.

Other oddities with the device numberingand naming scheme are the memory devices. These have a major number of 1. Forexample, the device to accessphysical memory is /dev/memwith a minor number of 1.The kernelmemory device is /dev/kmem, and it has a minor number of 2.The device used to access IO ports is /dev/port and it has aminor number of 4.

What about minor number 3? This is for device/dev/null, which is nothing. If you direct output to this device, it goes intonothing, or just disappears. Often the error output of a command is directedhere, as the errors generated are uninteresting. If you redirect from /dev/null,you get nothing as well. Often I do something like this:

cat /dev/null> file_name

This device is also used a lot inshell scripts where you do not want to see any output orerror messages. You can then redirect standard out or standard error to /dev/null and the messages disappear. For details on standard out and error, seethesection on redirection.

If file_name doesn'texist yet, it is created with a length of zero. If it does exist, the file istruncated to 0 bytes.

The device /dev/zero has amajor number of 5 and itsminor number is 5. Thisbehaves similarly to /dev/null in that redirecting output to this device is thesame as making it disappear. However, if you direct inputfrom thisdevice, you get an unending stream of zeroes. Not the number 0, which has anASCII value of 48this is anASCII 0.

Are those allthe devices? Unfortunately not. However, I hope that this has given you a starton how device names and minor numbers are configured. The file<linux/major.h> contains a list of the currently used (at least wellknown) major numbers. Some nonstandard package might add amajor number of its own. Up to this point, they have been fairly good aboutnot stomping on the existing major numbers.

As far as the minor numbersgo, check out the various man-pages. If there is a man-pagefor a specific device, the minor number will probably beunder the name of the driver. This is in major.h or often the first letter ofthe device name. For example, the parallel (printer) devices are lp?, so checkout man lp.

The best overview of all the major and minor numbers is inthe /usr/src/linux/Documentation directory. The devices.txt is considered the"authoritative" source for this information. 

0 0
原创粉丝点击