Red Hat Linux 7 Unleashed

Red Hat Linux 7 Unleashed

By William Ball

Basic Concepts of Devices

Devices are either block devices or character devices. A character device is one from which you can read a sequence of characters—for example, the sequence of keys typed at a keyboard or the sequence of bytes sent over a serial line. Character devices are sometimes referred to as sequentially accessed devices. A block device is one that stores data and offers access to all parts of it equally; diskettes and hard disks are block devices. Block devices are sometimes called random access devices.

When you perform some operation on a file, the kernel can tell that the file involved is a device by looking at its file mode (not its location). Different major and minor device numbers distinguishes the device nodes. The major device number indicates to the kernel which of its drivers the device node represents. For example in Linux, a block device with major number 3 is an IDE disk drive, and one with the major device number 8 is a SCSI disk. Each driver is responsible for several instances of the hardware it drives, and these are indicated by the value of the minor device number. For example, the SCSI disk with the minor number 0 represents the whole "first" SCSI disk, and the minor numbers 1 to 15 represent 15 possible partitions on it. The ls command prints the major and minor device numbers for you:

ls -l --sort=none /dev/sda{ ,[0-9],[0-9][0-9]}  /dev/sdb
brw-rw----    1 root     disk       8,   0 Jul  4 08:22 /dev/sda
brw-rw----    1 root     disk       8,   1 Jul  4 08:22 /dev/sda1
brw-rw----    1 root     disk       8,   2 Jul  4 08:22 /dev/sda2
brw-rw----    1 root     disk       8,   3 Jul  4 08:22 /dev/sda3
brw-rw----    1 root     disk       8,   4 Jul  4 08:22 /dev/sda4
brw-rw----    1 root     disk       8,   5 Jul  4 08:22 /dev/sda5
brw-rw----    1 root     disk       8,   6 Jul  4 08:22 /dev/sda6
brw-rw----    1 root     disk       8,   7 Jul  4 08:22 /dev/sda7
brw-rw----    1 root     disk       8,   8 Jul  4 08:22 /dev/sda8
brw-rw----    1 root     disk       8,   9 Jul  4 08:22 /dev/sda9
brw-rw----    1 root     disk       8,  10 Jul  4 08:22 /dev/sda10
brw-rw----    1 root     disk       8,  11 Jul  4 08:22 /dev/sda11
brw-rw----    1 root     disk       8,  12 Jul  4 08:22 /dev/sda12
brw-rw----    1 root     disk       8,  13 Jul  4 08:22 /dev/sda13
brw-rw----    1 root     disk       8,  14 Jul  4 08:22 /dev/sda14
brw-rw----    1 root     disk       8,  15 Jul  4 08:22 /dev/sda15
brw-rw----    1 root     disk       8,  16 Jul  4 08:22 /dev/sdb

The somewhat obscure option (--sort=none) with this ls -l command ensures that the devices are presented in correct order. If you use only ls -l, the entries are sorted alphabetically, and /dev/sda10 comes before /dev/sda2.

The b at the far left of the output of this command indicates that each of these entries is a block device. (Character devices are indicated by a c.) The major and minor device numbers appear just before the time field, separated by commas. (This is the position normally occupied in ls -l output by the file's size.)are associated with drivers that provide a file interfac

Character Devices

There are many character devices on a Linux system. On my system there are over 1600 devices marked as character devices in /dev. To see how many character devices there are on your system, use the following:

$ ls -l /dev/|grep ^c|wc -l

Character devices all deal with data one character at a time and process them sequentially. For example, the keyboard device will interpret each key as it is typed and if you want to "move" within this character stream it would only be possible to move forward (since each keystroke is lost as you process it). Also, you could only skip forward by actually reading and discarding keystrokes.

One good example of a character device is /dev/audio. This enables you to output data to your sound card. Each byte that is sent to it will be handled by the device drive and then sent to the actual hardware of the sound card. Because this is a pure character device, there are no random access capabilities of this device. To "rewind" a sound, you need to resend the file to the device again.

Block Devices

If you had just one file of data to store, you could put it directly on a block device and read it back. Block devices have a fixed capacity, however, and you would need some method of marking the end of your data. Block devices behave in most respects just like ordinary files, except that although an ordinary file has a length determined by how much data is in it, the "length" of a block device is its total capacity. If you write a megabyte to a 100MB block device and read back its contents, you get the 1MB of data followed by 99MB of its previous contents. Bearing in mind this restriction, several UNIX utilities encode the amount of data available in the file's data rather than the file's total length. Hence, they are suitable for storing data directly on block devices—for example, tar and cpio, which are suitable for everybody, and dump, which is suitable only for the system administrator (because it requires read access to the block device underlying the data to be backed up). To back up the entire contents of your home directory to diskettes, you type the following:


   $ find $HOME -print0 | cpio --create -0 --format=crc >/dev/fd0

The -print0 and -0 options for find and cpio ensure that the names of the files to be backed up are separated by ASCII NULLs, rather than newlines. This ensures that any filenames containing a newline are correctly backed up.

Most of the backup utilities for Linux are written specifically to write their backups to any kind of file; in fact, they were designed for sequentially accessed character devices, such as tape drives. See Chapter 22, "Backup and Restore," for more information about the various backup programs that are available for Linux.

Share ThisShare This

Informit Network