Red Hat Fedora 5 Unleashed
For UNIX and Linux, everything is a file. In this section, you learn about special types of files found in Linux that represent all the devices found on your system and handle all the input and output on your system. You also learn how to identify and create them. If you have installed the kernel documentation, it will contain a text file named /usr/src/linux-2.6/Documentation/devices.txt, an excerpt of which reads 3 char Pseudo-TTY slaves 0 = /dev/ttyp0 First PTY slave 1 = /dev/ttyp1 Second PTY slave ... 255 = /dev/ttyef 256th PTY slave These are the old-style (BSD) PTY devices; Unix98 devices are on major 136 and above. block First MFM, RLL and IDE hard disk/CD-ROM interface 0 = /dev/hda Master: whole disk (or CD-ROM) 64 = /dev/hdb Slave: whole disk (or CD-ROM) For partitions, add to the whole disk device number: 0 = /dev/hd? Whole disk 1 = /dev/hd?1 First partition 2 = /dev/hd?2 Second partition ... 63 = /dev/hd?63 63rd partition
For Linux/i386, partitions 1-4 are the primary partitions, and 5 and above are logical partitions. Other versions of Linux use partitioning schemes appropriate to their respective architectures.
The number 3 at the upper left of the preceding listing represents the major number that identifies a class of device. In this case, major 3 identifies both character devices (pseudo-TTY slaves) and block devices (IDE drives). The columns of numbers that follow under the device types are called the minor numbers; any device can be identified uniquely by its major and minor number. Linux uses these numbers internally; you usually see the name of the device as it is listed in the /dev directory. The major/minor numbers do show up in kernel error messages from time to time, and understanding the numbers helps you debug the problem. As you will see in the next section, the mknod command needs to be told those numbers in order to create a device. Take some time to browse the entire file because it contains some interesting information that provides answers to many frequently asked questions. The /dev directory contains all the special files known as device files. The files are placed in /dev during the original installation, and you can also create device files to go there if you need them. During the normal operation of Linux, you never need to bother with the files in /dev, but if you want to look at all the files in /dev, use this command: # ls -l --sort=none /dev | less
The --sort=none argument keeps the devices mostly grouped by major numbers for your viewing convenience; redirecting the output of the command (known as piping) through the less command allows you to use the PageUp and PageDown keys to navigate the long list rather than have it scroll off the screen. There are too many devices to list here, but they will all be either block or character devices. A character device is a file that handles data one character at a time and processes data sequentially. Examples include TTY (display console) devices, SCSI tape drives, the keyboard, audio devices, the Coda network file system, among others. Block devices are files that have a beginning, an end, and a fixed size; data can be written and read from anywhere inside them in any order. Because a block device can be much larger than the data it contains, utilities such as tar and cpio work with the files' data rather than the size, so they can store and retrieve the data directly off the block device rather than require a formatted file on a block device. (You can see how those commands are used in Chapter 17.) This works especially well with tape devices because they are character devices rather than block devices and are not formatted in the way that block devices are formatted. Special devices are block or character devices established for a special purpose. Two special devices of interest are
Naming Conventions Used for Block and Character Devices
The traditional naming system for block and character devices has been in use for a while. In /usr/src/linux-2.6/Documentation/devices.txt, the device name for the first partition of the first IDE disk would be /dev/hda1. If it were a SCSI disk, it would be /dev/sda1 (shown elsewhere in the devices.txt file). All device names are included in devices.txt, and all the devices that have been created on your system are listed in the /dev directory. That directory also can hold links; for example, /dev/cdrom can actually be a link back to the actual device that is your CD-ROM (perhaps /dev/hdc? or /dev/scd0?). /dev/mouse and /dev/modem are commonly used symbolic links in the /dev directory. Using mknod to Create Devices
If you need a device file in /dev that's not already there for some reason, you can create the special file with the mknod command. You might have noticed that /dev lists devices that you do not have. That's because the device's listing must appear in /dev before the system can use the actual device; if you install the hardware at some point in the future, the system will not create the device files on-the-fly. So, if you're preparing to install a device, and you check /dev but find that it does not contain the device's file, you can create the file with mknod. Creating files with mknod is a straightforward process as long as you know what type of device you are creating the file for and what the device's major and minor numbers are. The syntax of mknod is # mknod [OPTION].. NAME TYPE [MAJOR MINOR]
The useful option -m allows you to set the mode at file creation instead of doing it separately with the chmod command. You can obtain the values for NAME, TYPE, MAJOR, and MINOR from devices.txt, which even has a block of experimental numbers if you are inclined to experiment.
|