Device Files

 < Day Day Up > 



The name of a device file is designed to reflect the task of the device. Printer device files begin with lp for "line print." Because you could have more than one printer connected to your system, the particular printer device files are distinguished by two or more numbers or letters following the prefix lp, such as lp0, lp1, lp2. The same is true for terminal device files. They begin with the prefix tty, for "teletype," and are further distinguished by numbers or letters such as tty0, tty1, ttyS0, and so on. You can obtain a complete listing of the current device filenames and the devices for which they are used from the kernel.org Web site at www.kernel.org/pub/linux/docs/device-list/devices.txt.

Device Symbolic Links

All of these filenames will be implemented as device files in your /dev directory. Here you can find printer, CD-ROM, hard drive, SCSI, and sound device files, along with many others. Certain files are really symbolic links that bear common device names that are often linked to the actual device file used. A symbolic link is another name for a file that is used like a shortcut, referencing that file. For example, a /dev/cdrom symbolic link links to the actual device file used for your CD-ROM. If your CD-ROM is an IDE device, it may use the device file hdc. In this case, /dev/cdrom would be a link to /dev/hdc. In effect, /dev/cdrom is another name for /dev/hdc. A /dev/modem link file also exists for your modem. If your modem is connected to the second serial port, its device file would be /dev/ttyS1. In this case, /dev/modem would be a link to that device file. Applications can then use /dev/modem to access your modem, instead of having to know the actual device file used. A listing of commonly used device links is shown in Table 32-1.

Tip 

You will notice that there are no entries for the Ethernet devices in the /dev file, such as eth0 or eth1. That is because these are really aliases for kernel modules defined in the /etc/modules.conf file, or devices handled by the kernel directly. They are not device files.

Hardware Device Installation: Kudzu

Hardware devices are automatically detected by the Red Hat hardware probing tool known as Kudzu. Kudzu is a tool that detects and configures new or changed hardware on a system. Kudzu is run when you boot to configure new hardware devices and detect removed ones. You can also run Kudzu manually. When you start Kudzu, it detects the current hardware and checks it against a database stored in /etc/sysconfig/hwconf. It then determines if any hardware has been added to or removed from the system. When it detects new or added hardware, Kudzu will, if needed, invoke the appropriate configuration tool for the device, such as redhat-config-xfree86 for video cards, or redhat-config-keyboard for keyboards. For simple hardware configurations like CD-ROMs, Kudzu just links the cdrom device symbolic link to the new device, /dev/cdrom.

Table 32-1: Device Links

Link

Description

/dev/mouse

Current mouse device

/dev/tape

Current tape device

/dev/cdrom

Current CD-ROM device

/dev/cdwriter

Current CD-writer device

/dev/scanner

Current scanner device

/dev/modem

Current dial-out device, modem port

/dev/root

Current root file system

/dev/swap

Current swap device

Kudzu then updates its database in /etc/sysconfig/hwconf. If a removed device is detected, the hwconf file is updated accordingly. If the device was installed simply with a device link such as /dev/cdrom or /dev/mouse, that link is removed. In the case of network cards, alias entries like that for eth0 will be removed.

Creating Device Files Manually

Linux implements two types of devices: block and character. A block device, such as a hard disk, transmits data a block at a time. A character device, such as a printer or modem, transmits data one character at a time, or rather as a continuous stream of data, not as separate blocks. Device driver files for character devices have a c as the first character in the permissions segment displayed by the ls command. Device driver files for block devices have a b. In the next example, lp0 (the printer) is a character device and hda1 (the hard disk) is a block device:

# ls -l hda1 lp0 brw-rw---- 1 root disk 3, 1 Jan 30 02:04 hda1 crw-rw---- 1 root lp   6, 0 Jan 30 02:04 lp0

Device Types

The device type can be either b, c, p, or u. As already mentioned, the b indicates a block device, and c is for a character device. The u is for an unbuffered character device, and the p is for a FIFO (first in, first out) device. Devices of the same type often have the same name; for example, serial interfaces all have the name: ttyS. Devices of the same type are then uniquely identified by a number attached to the name. This number has two components: the major number and the minor number. Devices may have the same major number, but if so, the minor number is always different. This major and minor structure is designed to deal with situations in which several devices may be dependent on one larger device, such as several modems connected to the same I/O card. All the modems would have the same major number that references the card, but each modem would have a unique minor number. Both the minor and major numbers are required for block and character devices (b, c, and u). They are not used for FIFO devices, however.

Valid device names along with their major and minor numbers are listed in the devices.txt file located in the /Documentation directory for the kernel source code, /usr/src/linux-ver/Documentation. When you create a device, you use the major and minor numbers as well as the device name prefix for the particular kind of device you are creating. Most of these devices are already created for you and are listed in the /etc/dev directory.

mknod

Although most distributions include an extensive set of device files already set up for you, you can create your own. You use the mknod command to create a device file, either a character or block type. The mknod command has the following syntax:

mknod options device device-type major-num minor-num 

For example, Linux systems usually provide device files for three parallel ports (lp02). If you need more, you can use the mknod command to create a new one. Printer devices are character devices and must be owned by the root and daemon. The permissions for printer devices are read and write for the owner and the group, 660 (see Chapter 28 for a discussion of file permissions). The major device number is set to 6, while the minor device number is set to the port number of the printer, such as 0 for LPT1 and 1 for LPT2. Once the device is created, you use chown to change its ownership to the root user, since only the administrator should control it. Change the group to lp with the chgrp command.

Most devices belong to their own groups, such as disks for hard disk partitions, lp for printers, floppy for floppy disks, and tty for terminals. In the next example, a parallel printer device is made on a fourth parallel port, /dev/lp3. The -m option specifies the permissions—in this case, 660. The device is a character device, as indicated by the c argument following the device name. The major number is 6, and the minor number is 3. If you were making a device at /dev/lp4, the major number would still be 6, but the minor number would be 4. Once the device is made, the chown command then changes the ownership of the parallel printer device to root. For printers, be sure that a spool directory has been created for your device. If not, you need to make one. Spool directories contain files for data that varies according to the device output or input, like printers or scanners.

# mknod -m 660 /dev/lp3 c 6 3 # chown root /dev/lp3 # chgrp lp /dev/lp3



 < Day Day Up > 



Red Hat(c) The Complete Reference
Red Hat Enterprise Linux & Fedora Edition (DVD): The Complete Reference
ISBN: 0072230754
EAN: 2147483647
Year: 2004
Pages: 328

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net