{"id":252,"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:00","modified_gmt":"2020-08-22T20:26:00","slug":"this-is-the-page-title-toplevel-87","status":"publish","type":"page","link":"http:\/\/www.linux-tutorial.info\/?page_id=252","title":{"rendered":"Devices and Device Nodes"},"content":{"rendered":"\n<title>Devices and Device Nodes<\/title>\n<question id=\"\" type=\"mc\" text=\"What is the term for the special software the operating system uses to access hardware?\" \/>\n<concept id=\"\" description=\"'Device drivers' or 'drivers' are the special software the operating system uses to access hardware?\" \/>\n<p>\nIn <glossary>Linux<\/glossary>, nothing works without devices. I mean\n<i>nothing<\/i>. Getting input from a keyboard or displaying it on your screen\nboth require devices. Accessing data from the hard disk or printing a report\nalso require devices. In an <glossary>operating system<\/glossary>\nlike <glossary>DOS<\/glossary>, all of the input and output functions are almost entirely\nhidden from you by the operating system, which uses special software called &#8220;device\ndrivers&#8221; or simply &#8220;drivers&#8221;. Drivers for these devices\nmust exist for you to be able to use them, though they are hidden behind the\ncloak of the <glossary>operating system<\/glossary>.\n<\/p>\n<p>\nAlthough they access the same physical hardware, device drivers under <glossary>Linux<\/glossary>\nare more complex than their <glossary>DOS<\/glossary> cousins.\nAlthough adding new drivers is easier under <glossary>DOS<\/glossary>,\nLinux provides more flexibility in modifying those you already have. Linux provides a mechanism\nto simplify adding these input and output functions. Linux also provides a large set of tools\nand utilities to modify and configure how your system and these device drivers interact.\n<\/p>\n<question id=\"192\" text=\"Under Linux hardware is accessed through special files called what?\" \/>\n<concept id=\"148\" description=\"Under Linux hardware is accessed through special files called 'device nodes'.\" \/>\n<p>\nAs mentioned in a previous section, applications and other programs access everything, even hardware, through files. The term used for a special file to access hardware is a &#8220;<glossary>device node<\/glossary>&#8220;.\nMany new Linux users have trouble with device nodes for a number of different\nreasons. For the uninitiated, it is often difficult to figure out exactly what\ndevice node is needed for a particular task. Part of this is because the device\nnode names aren&#8217;t exactly intuitive, and part of it is because it&#8217;s not often\nobvious which device node is the one you actually need.<\/p>\n<p>\nOne of the first\nproblems encountered by new Linux users is with hard disks. Users almost always\ncome from a Windows background, and they are used to accessing hard disks,\nCD-ROMs, and floppies by using drive letters (like Window&#8217;s <i>C:\\<\/i> or\n<i>D:\\<\/i> drives, for example). For the most part, Windows users do not even\ncare where the various partitions are; they just know which drive letter to use\nto access a particular file or directory.<\/p>\n<p>\nIn most cases, that&#8217;s all they really\nneed to know. As often is the case with Windows, the is not obvious relation between the drive letter and the physical device. For example, the D: and G: partitions could be on the same physical hard disk and the E: and F: partition could be on two different disks. It is also possible that the hard disks containing the E: partition is actually found <em>before<\/em> the disk containing the D: partition.s\n<\/p>\n<p>\nWith Linux (or any UNIX variant), however, the situation is\nvery different. Although new installation procedures and administration tools\nhave made things a lot easier, there still comes a time when you need to know\nthat the device node <i>\/dev\/hda1<\/i> relates to your hard disk and\n<i>\/dev\/tty01<\/i> is a console terminal. For most day-to-day activity you can\nget by with simply knowing the names of the devices and what they are used for.\nBut even learning that can be a daunting task. There are just so many\nunintuitive names to deal with. Still, with a little time and practice, the\nfunction of these devices should become clear, and soon you&#8217;ll be using them\nlike an old pro.\n<\/p>\n<p CLASS =\"subtitle\"><b>What&#8217;s in a Name?<\/b><\/p>\n<p>\nSo what\nexactly is a device node? It&#8217;s basically a file. Like all other dialects of\nUnix, Linux accesses hardware devices just as if it were reading or writing any\nother file. This makes writing programs for Linux easier because the system can\nuse many of the same functions to access both hardware devices and &#8220;normal&#8221;\nfiles.<\/p>\n<p>\nDevice nodes (often referred to simply as &#8220;devices&#8221; in casual\nconversation) are the files that the kernel, applications, and even command-line\ntools use when they need to access the hardware. You can think of the device\nnode (or file) as providing an interface similar to a telephone jack. The phone\njack provides a convenient and standardized way of attaching things to the phone\nline, but the jack is not the phone line itself. It doesn&#8217;t matter if you&#8217;re\nplugging a telephone, a modem, or a fax machine into the jack, because all of\nthese use the same interface. Similarly, your printer doesn&#8217;t care if it&#8217;s being\naccessed by the kernel, by a word processor, or by a graphics program, because\nthey all do so through the same interface.<\/p>\n<p>\nThe down side to all of this\nis that device nodes and the concept of accessing hardware through them can be\nconfusing to users who are unfamiliar with these ideas. There are, however,\nparallels in the DOS and Windows world. Using names such as <i>A:, COM1:,<\/i>\nand <i>PRN:<\/i> to access hardware in DOS is not all that different than using\ndevice nodes to access hardware under Linux (at least from the user&#8217;s point of\nview).<\/p>\n<p>\nIn order to access the hardware in this fashion, the operating\nsystem has to refer to each piece of hardware by a unique name. In Linux, for\nexample, <i>\/dev\/fd0<\/i> is the name for the floppy drive, similar to the\n<i>A:<\/i> that DOS uses. In DOS, the name assigned to the printer is<i>\nPRN:,<\/i> while in Linux it&#8217;s <i>\/dev\/lpt0.<\/i> In order for you to access these\ndevices, you simply have to know their names.<\/p>\n<p>\nSince device nodes are just\nfiles on the hard disk, they are treated like files.On most systems, everyone\ncan at least look at them, and the system administrator (root) can access the\ndevice nodes directly, just like any other file.<\/p>\n<p>\nAs with other files on\nyour computer, device nodes are assigned specific permissions that allow some\npeople to read from and write to them, but limit other people&#8217;s access. These\npermissions are the safety mechanism that prevents unfortunate accidents such as\nrandom disk overwrites from happening. If you do have access to read from and\nwrite to the various device nodes, you <i>could<\/i> actually over-write the hard\ndisk. This, among other reasons, is why you really do have to be very careful\nabout what you do when you&#8217;re logged in to your system as root.<\/p>\n<p CLASS =\"subtitle\">Odds and Ends<\/p>\n<p>\nThere are\na couple of oddities about Linux <glossary term=\"device node\">device nodes<\/glossary> that need to be addressed. The\nfirst actually applies to all dialects of Unix and is related to the difference\nbetween a block device and a character device. The general misconception is that\ncharacter devices are only read one character at a time. This is <strong>not<\/strong>\nthe case. Character devices differ from block devices in that they are <em>typically<\/em> read sequentially\nrather than randomly. Hard drives are block devices because they can be accessed\nrandomly, and terminals are character devices because they are accessed\nsequentially.\n<\/p>\n<p>\nHowever, this is only a convention and not hard and fast rule. In many cases, you read block devices one characters at a time.\n<\/p>\n<p>\nUnder Linux (as well as other Unix dialects), access to\nblock devices goes through a system cache called the <command>buffer cache<\/command>. One key\nadvantage of the buffer cache is that the system can keep track of recently\naccessed blocks. If a process needs to read something that is still in the\nbuffer cache (and has not been changed), there is no need to re-read the device.\nInstead, the system simply passes the block from the buffer to the process.<\/p>\n<p>\nWhen writing back to a block device, the process is similar. The process\nthinks it is writing to the device, but is actually writing to the\n<glossary>buffer cache<\/glossary>.\nThis block is marked as &#8220;dirty&#8221; and will be written to the disk when the system\ngets around to it. If a process needs to read the block, then there is again no\nneed to access the device directly.<\/p>\n<p>\nNote that there is a delay in writing\nthe information to the disk. If something happens to the computer before the\ndata stored in the buffer is written (for example, a power outage), there is a\npossibility that the data could be lost. The delay is fairly short (default 30\nseconds for data buffers and 5 seconds for metadata buffers), however, so it&#8217;s\nunlikely that too much will be lost. In addition, it is possible to use the\n<i>O_SYNC<\/i> flag when opening the device, which forces the buffered data to be\nwritten.<\/p>\n<p>\nAnother oddity that you will find on Linux systems is that a\nlarge portion of the major numbers are repeated. That is, there are often two\ncompletely unrelated devices that have the same major number. For example, hard\ndisks and pseudo-ttys (when using telnet) both have a major number of 3. Some\nUnix dialects, such as SCO, use the same major number for the block and\ncharacter versions of the same device. Despite this, the device drivers are\nstill capable of determining which driver is needed because there are other\nmethods used to differentiate between them.<\/p>\n<p CLASS =\"subtitle\">A Rose By any Other Name<\/p>\n<p>\nIt is possible to have two device nodes that point at\nthe same device. These nodes can have different names, but if they are of the\nsame device type and they have the same major-minor number pair, they are\nactually pointing at the same device.<\/p>\n<p>\nSo, why would anyone want to have\ntwo device nodes pointing at the same device? The biggest reason for this is\nconvenience. It is extremely useful to name a device in such a way that we mere\nmortals can recognize it. There are several common devices on Linux systems that\nhave more than one name, one being the swap device.<\/p>\n<p>\nOn one of systems, the\n<glossary>swap<\/glossary> partition is the fourth primary partition on the first SCSI hard disk.\nUnder the device node naming scheme we discussed earlier, it is called\n<device>\/dev\/sda4<\/device>. Remembering that the swap partition is <i>\/dev\/ sda4,<\/i>\nhowever, isn&#8217;t all that easy. For this reason, the swap partition is also\nusually called <device>\/dev\/swap<\/device>. This is much more recognizable than the name\ngiven it under the standard naming scheme. While <i>\/dev\/sda4<\/i> tells me where\nthe swap partition is, <i>\/dev\/swap<\/i> tells me <i>what<\/i> it is.\n<\/p>\n<p>\nAnother common device that uses this trick is <i>\/dev\/tape.<\/i> In my case,\nit is the same as <i>\/dev\/st0,<\/i> which is my first SCSI tape drive. However,\nif I access <i>\/dev\/tape,<\/i> it really does not matter if my tape drive is SCSI\nor not, as the system does the work for me.<\/p>\n<p>\nOne thing to note is that you\ncannot simply copy device nodes using <command>cp<\/command>. In addition, you should not\njust create new device nodes for this purpose using the <command>mknod<\/command> command.\nAlthough this would get you two identical device nodes, when you change one, the\nother is unaffected. For this reason, you should create links between the device\nnodes rather than making duplicates of them.<\/p>\n<p>\nOne thing I use this linking\nmechanism for is my <glossary>FAT<\/glossary> partitions. Since I need filesystems that are available\nfrom Linux, Windows NT, and a couple of other operating systems, I have several\nFAT partitions. In order to make things simpler for me, I do one of two things.\nEither I create links using the DOS\/Windows drive letter or I create links with\nthe name by which the drive is shared.<\/p>\n<p>\nFor example, my data is stored on\nwhat appears as the <i>G:\\<\/i> drive under DOS\/Windows, and which resides on the\nLinux partition <device>\/dev\/sdb6<\/device>.I might have a device node\n<device>\/dev\/dos_g<\/device>,\nthat is linked to <device>\/dev\/sdb6<\/device>. The <device>\/dev\/dos_g<\/device> name tells me that\nthis partition appears under DOS as drive <i>G:\\.<\/i> Since the drive is also\nshared with Samba, I might create a link <device>\/dev\/Data<\/device>, which is the share\nname. These (along with other FAT partitions) are then mounted automatically\nthrough the <file type=\"\">\/etc\/fstab<\/file> file when the system boots. Remembering that\n<i>\/dev\/dos_g<\/i> is the same as the <i>G:\\<\/i> drive in DOS is much simpler\nthan trying to remember <device>\/dev\/sdb6<\/device>.\n<\/p>\n<p>\nWhether you create hard links\nor symbolic links is almost a matter of personal preference. Typically, however,\nsymbolic links are used. If you look in the <directory>\/dev<\/directory> directory, you will see\na number of device which are already symbolic links. Therefore, I think it is\nbetter to stick with what is already on your system.\n<\/p>\n<p CLASS =\"subtitle\">Finding Out More<\/p>\n<p>\nMany of the devices on your system\nhave associated man pages. Figuring out which man page you need, however, isn&#8217;t\nalways straightforward. If you are unsure what a particular device is used for,\nyou can usually figure out the meaning of the base name. For example, <i>hd<\/i>\nfor IDE hard disks, <i>sd<\/i> for <glossary>SCSI<\/glossary> hard disks, <i>fd<\/i> for floppy drives,\nand so forth. Often there is a general man page for that type of device, so\n<tt>man sd<\/tt> will call up the page for SCSI type hard drives. Alternatively,\nyou can use the <i>-k<\/i> option on <command>man<\/command> to search for a particular\nkeyword. For example, <tt>man -k disk<\/tt> will show you all of the man pages\nthat contain the word &#8220;disk.&#8221;<\/p>\n<p>\nMan pages are useful, when they exist.\nUnfortunately, not all devices have an associated man page. If this is the case,\nyou can usually find some information in the documentation subdirectory in the\nkernel source tree (typically <directory>\/usr\/src\/linux<\/directory>). There, you will find a\nfile called <i>devices.txt,<\/i> which is a reasonably comprehensive list of the\nmajor numbers. Often this file will also list the minor numbers for each device,\nor at least give an explanation of the related minor numbering scheme. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Devices and Device Nodes In Linux, nothing works without devices. I mean nothing. Getting input from a keyboard or displaying it on your screen both require devices. Accessing data from the hard disk or printing a report also require devices. &hellip; <a href=\"http:\/\/www.linux-tutorial.info\/?page_id=252\">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-252","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/252","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=252"}],"version-history":[{"count":1,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/252\/revisions"}],"predecessor-version":[{"id":540,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/252\/revisions\/540"}],"wp:attachment":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}