{"id":253,"date":"2020-08-18T19:23:47","date_gmt":"2020-08-18T20:23:47","guid":{"rendered":"http:\/\/www.linux-tutorial.info\/?page_id=77"},"modified":"2020-08-22T19:26:16","modified_gmt":"2020-08-22T20:26:16","slug":"this-is-the-page-title-toplevel-88","status":"publish","type":"page","link":"http:\/\/www.linux-tutorial.info\/?page_id=253","title":{"rendered":"Major and Minor Numbers"},"content":{"rendered":"\n<concept id=\"68\" description=\"Two device nodes with completely different names could have the same major and minor numbers and are therefore the exact same device.\" \/>\n<question id=\"\" type=\"mc\" text=\"What command do you use to create device nodes?\" \/>\n<title>Major and Minor Numbers<\/title>\n<p>\nOne of the basic features of the Linux kernel is that it abstracts the handling of devices.\nAll hardware devices look like regular files; they can be opened, closed, read\nand\nwritten using the same, standard, system calls that are used to manipulate\nfiles.\nEvery device in the system is represented by a file.\nFor block (disk) and character devices, these device files are created\nby the <command>mknod<\/command> command\nand they describe the device using major and minor device numbers.\nNetwork devices are also represented by device special files but they are\ncreated by Linux as it finds and\ninitializes the network controllers in the system.\n<\/p>\n<p>\nTo <glossary>UNIX<\/glossary>,\neverything is a file. To write to the hard disk, you write to a file. To read\nfrom the keyboard is to read from a file. To store backups on a tape device is\nto write to a file. Even to read from memory is to read from a file. If the file\nfrom which you are trying to read or to which you are trying to write is a\n&#8220;normal&#8221; file, the process is fairly easy to understand: the file is opened and\nyou read or write data. If, however, the device you want to access is a special\ndevice file (also referred to as a device node), a fair bit of work needs to be\ndone before the read or write operation can begin.\n<\/p>\n<p>\nOne key aspect of\nunderstanding device files lies in the fact that different devices behave and\nreact differently. There are no keys on a hard disk and no sectors on a\nkeyboard, though you can read from both. The system, therefore, needs a\nmechanism whereby it can distinguish between the various types of devices and\nbehave accordingly.\n<\/p>\n<p>\n<p>\nTo access a device accordingly, the\n<glossary>operating system<\/glossary> must be told what to do. Obviously, the\nmanner in which the kernel accesses a hard disk will be different from the way\nit accesses a <glossary>terminal<\/glossary>.  Both can be read from and written\nto, but that&#8217;s about where the similarities end. To access each of these totally\ndifferent kinds of devices, the <glossary>kernel<\/glossary> needs to know that\nthey are, in fact, different.\n<\/p>\n<question id=\"193\" text=\"All the software functions for a specific device are jointly referred to as what?\" \/>\n<concept id=\"149\" description=\"All the software functions for a specific device are jointly referred to as a device driver?\" \/>\n<p>\nInside the <glossary>kernel<\/glossary> are\nfunctions for each of the devices the kernel is going to access. All the\nroutines for a specific device are jointly referred to as the\n<glossary>device driver<\/glossary>. Each device on the system has its own\n<glossary>device driver<\/glossary>. Within each device driver are the functions\nthat are used to access the device. For devices such as a hard disk or\n<glossary>terminal<\/glossary>, the system needs to be able to (among other\nthings) open the device, write to the device, read from the device, and close\nthe device. Therefore, the respective drivers will contain the routines needed\nto open, write to, read from, and close (among other things) those devices.\n<\/p>\n<p>\nThe <glossary>kernel<\/glossary> needs to be told how to access the device.\nNot only does the kernel need to be told what kind of\ndevice is being accessed but also any special information, such as the\n<glossary>partition<\/glossary> number if it&#8217;s a hard disk or density if it&#8217;s a\nfloppy, for example. This is accomplished by the\n<glossary>major number<\/glossary> and\n<glossary>minor number<\/glossary> of that device.\n<\/p>\n<question id=\"194\" text=\"What information does the major number of a device tell you?\" \/>\n<question id=\"195\" text=\"What information does the minor number of a device tell you?\" \/>\n<concept id=\"150\" description=\"A device node's 'major number' tells you what type of device it is.\" \/>\n<p>\n<!-- START copyrightDavid Rusling:1996-1999:GNU General Public License -->\nAll devices controlled by the same device driver have a common major device\nnumber.\nThe minor device numbers are used to distinguish between different devices and\ntheir controllers.\nLinux maps the device special file passed in system calls (say to mount a file\nsystem on a block device) to the device&#8217;s device driver using the major device\nnumber and a number of system tables, for example the character device table, chrdevs.\n<!-- END copyrightDavid Rusling:1996-1999:GNU General Public License -->\nThe major number is actually the\noffset into the kernel&#8217;s <glossary>device driver<\/glossary> table, which tells\nthe <glossary>kernel<\/glossary> what kind of device it is (whether it is a hard\ndisk or a serial terminal). The <glossary>minor number<\/glossary> tells the\nkernel special characteristics of the device to be accessed. For example, the\nsecond hard disk has a different minor number than the\nfirst. The COM1 port has a different minor number than the COM2 port,\neach partition on the primary IDE disk has a different\nminor device number, and so forth. So, for example, \/dev\/hda2, the second partition\nof the primary IDE disk has a major number of 3 and a minor number of 2.\n<\/p>\n<p>\nIt is through this table that the routines are accessed that, in turn, access the\nphysical hardware. Once the <glossary>kernel<\/glossary> has determined what kind\nof device to which it is talking, it determines the specific device, the\nspecific location, or other characteristics of the device by means of the\n<glossary>minor number<\/glossary>.\n<\/p>\n<question id=\"197\" text=\"The device node for IDE hard disks is made up of what parts?\" \/>\n<p>\nThe <glossary>major number<\/glossary> for the hd (IDE) driver is hard-coded at 3.\nThe minor numbers have the format\n<\/p>\n<p>(&lt;unit&gt;*64)+&lt;part&gt; <\/p>\n<p>\nwhere &lt;unit&gt;\nis the IDE drive number on the <i>first <\/i>controller, either 0 or 1, which is\nthen multiplied by 64. That means that all hd devices on the first\n<glossary>IDE<\/glossary> drive have a\n<glossary>minor number<\/glossary> less\nthan 64. &lt;part&gt; is the <glossary>partition<\/glossary> number, which can be\nanything from 1 to 20. Which minor numbers you will be able to access will\ndepend on how many partitions you have and what kind they are (extended,\nlogical, etc.).  The <glossary>minor number<\/glossary> of the device node that\nrepresents the whole disk is 0. This has the node name hda, whereas the other\n<glossary>device nodes<\/glossary> have a name equal to their\n<glossary>minor number<\/glossary> (i.e., \/dev\/hda6 has a minor number 6).\n<\/p>\n<p>\nIf you were to\nhave a second <glossary>IDE<\/glossary> disk on the first controller, the unit number would be 1. Therefore, all of the minor numbers would be 64 or greater. The\n<glossary>minor number<\/glossary> of the device node representing the whole disk\nis 1. This has the node name hdb, whereas the other\n<glossary>device nodes<\/glossary> have a name equal to their\n<glossary>minor number<\/glossary>\nplus 64 (i.e., \/dev\/hdb6 has a minor number 70).\n<\/p>\n<p>\nIf you have more than one <glossary>IDE<\/glossary> controller, the principle is the same. The only\ndifference is that the <i>major<\/i> number is 22.\n<\/p>\n<question id=\"196\" text=\"Device files begining with 'sd' are typically for what find of devices?\" \/>\n<question id=\"197\" text=\"The device node for SCSI hard disks is made up of what parts?\" \/>\n<p>\nFor\n<glossary>SCSI<\/glossary> devices, the scheme is a little different. When you\nhave a <glossary>SCSI<\/glossary>\n<glossary>host adapter<\/glossary>, you can\nhave up to seven hard disks. Therefore, we need a different way to refer to the\npartitions. In general, the format of the device nodes is\n<\/p>\n<p>\nsd&lt;drive&gt;&lt;partition&gt;\n<\/p>\n<p>\nwhere &#8220;sd&#8221; refers to the\n<glossary>SCSI<\/glossary> disk driver, &lt;drive&gt; is a letter for the\nphysical drive, and &lt;partition&gt; is the <glossary>partition<\/glossary>\nnumber. Like the hd devices, when a device refers to the entire disk, for\nexample the device &#8220;sda&#8221; refers to the first disk.\n<\/p>\n<p>\nThe <glossary>major number<\/glossary> for all\n<glossary>SCSI<\/glossary> drives is 8. The\n<glossary>minor number<\/glossary> is based on the drive number, which is\nmultiplied by 16 instead of 64, like the <glossary>IDE<\/glossary> drives. The\n<glossary>partition<\/glossary> number is then added to this number to give the\nminor.\n<\/p>\n<p>\nThe <glossary>partition<\/glossary> numbers are not as simple to\nfigure out. Partition 0 is for the whole disk (i.e., sda). The four\n<glossary>DOS<\/glossary> primary partitions are numbered 14. Then the extended\npartitions are numbered 58.  We then add 16 for each drive. For example:\n<\/p>\n<p>\n<screenoutput>brw-rw&#8212;-   1 root     disk       8,  22 Sep 12  1994\n\/dev\/sdb6<\/screenoutput>\n<\/p>\n<p>\nBecause b is after the sd, we know that this is on the\nsecond drive. Subtracting 16 from the minor, we get 6, which matches the\n<glossary>partition<\/glossary> number. Because it is between 4 and 8, we know\nthat this is on an extended <glossary>partition<\/glossary>.  This is the second\npartition on the first extended partition.\n<\/p>\n<p>\nThe floppy devices have an\neven more peculiar way of assigning minor numbers.\nThe <glossary>major number<\/glossary> is fairly easy its 2.\nBecause the names are a little easier to\nfigure out, lets start with them. As you might guess, the device names all begin\nwith fd. The general format is\n<\/p>\n<question id=\"198\" text=\"The device node for floppy drives is made up of what parts?\" \/>\n<p>\nfd&lt;drive&gt;&lt;density&gt;&lt;capacity&gt;\n<\/p>\n<p>\nwhere &lt;drive&gt; is\nthe drive number (0 for A:, 1 for B:), &lt;density&gt; is the density (d-double,\nh-high), and &lt;capacity&gt; is the capacity of the drive (360KiB, 1440KiB). You\ncan also tell the size of the drive by the density letter, lowercase letter\nindicates that it is a 5.25&#8243; drive and an uppercase letter indicates that it is\na 3.5&#8243; driver. For example, a low-density 5.25&#8243; drive with a capacity of 360KiB\nwould look like\n<\/p>\n<p>\nfd0d360\n<\/p>\n<p>\nIf your second drive was a high-density\n3.5&#8243; drive with a capacity of 1440KiB, the device would look like\n<\/p>\n<p>\nfd1H1440\n<\/p>\n<p>\nWhat the minor numbers represents is a fairly complicated process. In\nthe fd(4) <glossary>man-page<\/glossary> there is an explanatory table, but it is\nnot obvious from the table why specific minor numbers go with each device. The\nproblem is that there is no logical progression as with the hard disks. For\nexample, there was never a 3.5&#8243; with a capacity of 1200KiB nor has there been a\n5.25&#8243; with a capacity of 1.44MiB. So you will never find a device with H1200 or\nh1440. So to figure out the device names, the best thing is to look at the\n<glossary>man-page<\/glossary>.\n<\/p>\n<p>\nThe <glossary>terminal<\/glossary>\ndevices come in a few forms. The first is the system console, which are the\ndevices tty0-tty?. You can have up to 64 virtual terminals on you system\nconsole, although most systems that I have seen are limited five or six. All\nconsole terminals have a <glossary>major number<\/glossary> of 4. As we discussed\nearlier, you can reach the low numbered ones with ALT-Fn, where n is the number\nof the function key. So ALT-F4 gets you to the fourth virtual console. Both the\n<glossary>minor number<\/glossary> and the tty number are based on the function\nkey, so \/dev\/tty4 has a <glossary>minor number<\/glossary> 4 and you get to it\nwith ALT-F4. (Check the console(4) <glossary>man-page<\/glossary> to see how to\nuse and get to the other virtual terminals.)\n<\/p>\n<p>\nSerial devices can also\nhave terminals hooked up to them. These terminals normally use the devices\n<directory>\/dev\/ttyS<\/directory>n, where n is the number of the serial port (0, 1, 2, etc.). These also\nhave a minor number of 4, but the minor numbers all start at 64. (Thats why you\ncan only have 63 virtual consoles.) The minor numbers are this base of 64, plus\nthe serial port number (04). Therefore, the minor number of the third serial\nport would be 64+3=67.\n<\/p>\n<p>\nRelated to these devices are the modem control\ndevices, which are used to access modems. These have the same minor numbers but\nhave a major number of 5. The names are also based on the device number.\nTherefore, the <glossary>modem<\/glossary> device attached to the third serial\nport has a <glossary>minor number<\/glossary> of 64+3=67 and its name is cua3.\n<\/p>\n<p>\nAnother device with a <glossary>major number<\/glossary> of 5 is\n<directory>\/dev\/tty<\/directory>, which has a <glossary>minor number<\/glossary> of 0. This is a special device and is referred to as the &#8220;controlling <glossary>terminal<\/glossary>.\nThis is the terminal device for the currently running process. Therefore, no\nmatter where you are, no matter what you are running, sending something to\n\/dev\/tty will always appear on your screen.\n<\/p>\n<p>\nThe pseudo-terminals (those\nthat you use with <glossary>network<\/glossary> connections or X) actually come\nin pairs. The &#8220;slave&#8221; is the device at which you type and has a name like ttyp?,\nwhere ? is the tty number. The device that the process sees is the &#8220;master&#8221; and\nhas a name like ptyn, where n is the device number. These also have a\n<glossary>major number<\/glossary> 4. However, the master devices all have minor\nnumbers based on 128.  Therefore, pty0 has a <glossary>minor number<\/glossary>\nof 128 and pty9 has a minor number of 137 (128+9). The slave device has minor\nnumbers based on 192, so the slave device ttyp0 has a <glossary>minor number<\/glossary>\nof 192. Note that the tty numbers do not increase numerically\nafter 9 but use the letter a-f.\n<\/p>\n<p>\nOther oddities with the device numbering\nand naming scheme are the memory devices. These have a major number of 1. For\nexample, the device to access <glossary>physical memory<\/glossary> is <directory>\/dev\/mem<\/directory>\nwith a <glossary>minor number<\/glossary> of 1.\nThe <glossary>kernel<\/glossary>\nmemory device is <directory>\/dev\/kmem<\/directory>, and it has a <glossary>minor number<\/glossary> of 2.\nThe device used to access IO ports is <directory>\/dev\/port<\/directory> and it has a\n<glossary>minor number<\/glossary> of 4.\n<\/p>\n<p>\nWhat about minor number 3? This is for device\n<directory>\/dev\/null<\/directory>, which is nothing. If you direct output to this device, it goes into\nnothing, or just disappears. Often the error output of a command is directed\nhere, as the errors generated are uninteresting. If you redirect from <directory>\/dev\/null<\/directory>,\nyou get nothing as well. Often I do something like this:\n<\/p>\n<commandexample command=\"cat\">\ncat \/dev\/null &gt; file_name\n<\/commandexample>\n<p>\nThis device is also used a lot in\n<glossary>shell<\/glossary> scripts where you do not want to see any output or\nerror messages. You can then redirect standard out or standard error to\n<directory>\/dev\/null<\/directory> and the messages disappear. For details on standard out and error, see\nthe <tutorial id=21>section on redirection<\/tutorial>.\n<p>\nIf file_name doesn&#8217;t\nexist yet, it is created with a length of zero. If it does exist, the file is\ntruncated to 0 bytes.\n<\/p>\n<p>\nThe device <device>\/dev\/zero<device> has a\n<glossary>major number<\/glossary> of 5 and its\n<glossary>minor number<\/glossary> is 5. This\nbehaves similarly to \/dev\/null in that redirecting output to this device is the\nsame as making it disappear. However, if you direct input <i>from<\/i> this\ndevice, you get an unending stream of zeroes. Not the number &#8216;0&#8217;, which has an\nASCII value of 48, this is an <glossary>ASCII<\/glossary> 0.\n<\/p>\n<p>\nAre those all\nthe devices? Unfortunately not. However, I hope that this has given you a start\non how device names and minor numbers are configured. The file\n&lt;linux\/major.h&gt; contains a list of the currently used (at least well\nknown) major numbers. Some nonstandard package might add a\n<glossary>major number<\/glossary> of its own. Up to this point, they have been fairly good about not stomping on the existing major numbers.\n<\/p>\n<p>\nAs far as the minor numbers\ngo, check out the various man-pages. If there is a <glossary>man-page<\/glossary>\nfor a specific device, the <glossary>minor number<\/glossary> will probably be\nunder the name of the driver. This is in major.h or often the first letter of\nthe device name. For example, the parallel (printer) devices are lp?, so check\nout <command name=\"man\">man lp<\/command>.\n<\/p>\n<p>\nThe best overview of all the major and minor numbers is in\nthe <directory>\/usr\/src\/linux\/Documentation<\/directory> directory. The devices.txt is considered the &#8220;authoritative&#8221; source for this information.\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Major and Minor Numbers 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, read and written using the same, standard, &hellip; <a href=\"http:\/\/www.linux-tutorial.info\/?page_id=253\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-253","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/253","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=253"}],"version-history":[{"count":1,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/253\/revisions"}],"predecessor-version":[{"id":615,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/253\/revisions\/615"}],"wp:attachment":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}