-->
Page 195
Page 196
One of the simplest and most elegant aspects of UNIX (and Linux) design is the way that everything is represented as a file. Even the devices on which files are stored are represented as files.
Hardware devices are associated with drivers that provide a file interface; the special files representing hardware devices (or just devices) are kept in the directory /dev. Devices are either block devices or character devices.
A character device is one from which you can read a sequence of charactersfor example, the sequence of keys typed at a keyboard or the sequence of bytes sent over a serial line. A block device is one that stores data and offers access to all parts of it equally; floppy and hard disks are block devices. Block devices are sometimes called random access devices, just as character devices are sometimes called sequentially accessed devices. With the latter, you can get data from any random part of a hard disk, but you have to retrieve the data from a serial line in the order it was sent.
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). The device nodes are distinguished by having different major and minor device numbers. The major device number indicates to the kernel which of its drivers the device node represents. (For example, 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 fifteen possible partitions on it. The ls command prints the major and minor device numbers for you:
$ ls -l --sort=none /dev/sda{,?,??} /dev/sdb brw-rw---- 1 root disk 8, 0 Sep 12 1994 /dev/sda brw-rw---- 1 root disk 8, 1 Sep 12 1994 /dev/sda1 brw-rw---- 1 root disk 8, 2 Sep 12 1994 /dev/sda2 brw-rw---- 1 root disk 8, 3 Sep 12 1994 /dev/sda3 brw-rw---- 1 root disk 8, 4 Sep 12 1994 /dev/sda4 brw-rw---- 1 root disk 8, 5 Sep 12 1994 /dev/sda5 brw-rw---- 1 root disk 8, 6 Sep 12 1994 /dev/sda6 brw-rw---- 1 root disk 8, 7 Sep 12 1994 /dev/sda7 brw-rw---- 1 root disk 8, 8 Sep 12 1994 /dev/sda8 brw-rw---- 1 root disk 8, 9 Sep 12 1994 /dev/sda9 brw-rw---- 1 root disk 8, 10 Sep 12 1994 /dev/sda10 brw-rw---- 1 root disk 8, 11 Sep 12 1994 /dev/sda11 brw-rw---- 1 root disk 8, 12 Sep 12 1994 /dev/sda12 brw-rw---- 1 root disk 8, 13 Sep 12 1994 /dev/sda13 brw-rw---- 1 root disk 8, 14 Sep 12 1994 /dev/sda14 brw-rw---- 1 root disk 8, 15 Sep 12 1994 /dev/sda15 brw-rw---- 1 root disk 8, 16 Sep 12 1994 /dev/sdb
The obscure options to this ls command ensure that the devices are presented in this order. If just ls -l had been used, the entries would have been sorted alphabetically, and /dev/sda10 would have come before /dev/sda2.
Page 197
The b at the far left indicates that all of these are block devices. (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.)
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 some fixed capacity, though, 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 just its total capacity. If you wrote a megabyte to a 100MB block device and read back its contents, you would get the 1MB of data followed by 99MB of its previous contents. Bearing in mind this restriction, there are still several UNIX utilities that encode the amount of data available in the file's data rather than the file's total length, and hence are suitable for storing data directly on block devicesfor 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 floppy disk, you would type the following:
$ tar cf /dev/fd0 $HOME
or
$ 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 that find sends to cpio are separated by ASCII NULs, rather than newlines. This ensures that any filenames containing a newline are correctly backed up.
NOTE |
The only characters that are illegal in UNIX filenames are the slash and the ASCII NUL. |
These backup utilities are written specifically to write their backups to any kind of file; in fact, they were designed for sequentially accessed character devicesfor example, tape drives.
When you have more than one item of data, it is necessary to have some method of organizing files on the device. These methods are called filesystems. Linux enables you to choose any organizational method to marshal your files on their storage device. For example, you can use the MS-DOS filesystem on a floppy, or the faster ext2 filesystem on your hard disk.