Библиотека сайта rus-linux.net
|Purchase||Copyright © 2002 Paul Sheer. Click here for copying permissions.||Home|
Next: 19. Partitions, File Systems, Up: rute Previous: 17. Overview of the   Contents
- 18.1 Device Files
- 18.2 Block and Character Devices
- 18.3 Major and Minor Device Numbers
- 18.4 Common Device Names
tar, and Tricks with Block Devices
- 18.6 Creating Devices with
UNIX was designed to allow transparent access to hardware devices across all CPU architectures. UNIX also supports the philosophy that all devices be accessible using the same set of command-line utilities.
UNIX has a beautifully consistent method of allowing programs to
access hardware. Under UNIX, every piece of hardware is a file. To
demonstrate this novelty, try viewing the file
/dev/hda (you will have to
root to run this command):
/dev/hda is not really a file at all. When you read
from it, you are actually reading directly from the first
physical hard disk of your machine.
/dev/hda is known
as a device file, and all of them are stored under the
Device files allow access to hardware. If you have a sound card installed and configured, you can try:
Say something into your microphone and then type:
The system will play out the sound through your speakers. (Note that this does not always work, since the recording volume or the recording speed may not be set correctly.)
If no programs are currently using your mouse, you can also try:
If you now move the mouse, the mouse protocol commands will be written directly to your screen (it will look like garbage). This is an easy way to see if your mouse is working, and is especially useful for testing a serial port. Occasionally this test doesn't work because some command has previously configured the serial port in some odd way. In that case, also try:
At a lower level, programs that access device files do so in two basic ways:
- They read and write to the device to send and retrieve
bulk data (much like
- They use the C
ioctl(IO Control) function to configure the device. (In the case of the sound card, this might set mono versus stereo, recording speed, or other parameters.)
Because every kind of device that one can think of (except for network cards) can be twisted to fit these two modes of operation, UNIX's scheme has endured since its inception and is the universal method of accessing hardware.
Hardware devices can generally be categorized into random access devices like disk and tape drives, and serial devices like mouse devices, sound cards, and terminals.
Random access devices are usually accessed in large contiguous
blocks of data that are stored persistently. They are read from
in discrete units (for most disks, 1024 bytes at a time). These
are known as block devices. Running an
ls -l /dev/hda shows
b on the far left of the listing, which means that your hard disk
is a block device:
Serial devices, on the other hand, are accessed one byte at a
time. Data can be read or written only once. For example, after
a byte has been read from your mouse, the same byte cannot be
read by some other program. Serial devices are called character
devices and are indicated by a
c on the far left of the
Signal Processor--that is, your sound card) device looks like:
Devices are divided into sets called major device numbers.
For instance, all SCSI disks are major number
8. Further, each individual device has a minor device number
/dev/sda, which is minor device 0.
Major and minor device numbers identify the device
to the kernel. The file name of the device is arbitrary
and is chosen for convenience and consistency. You can see the
major and minor device number (
8, 0) in the
ls listing for
A list of common devices and their descriptions follows.
The major numbers are shown in parentheses. The complete reference
for devices is the file
hdstands for hard disk, but refers here only to IDE devices--that is, common hard disks. The first letter after the
hddictates the physical disk drive:
- First drive, or primary master.
- Second drive, or primary slave.
- Third drive, or secondary master.
- Fourth drive, or secondary slave.
less /dev/hda), you would be reading raw from the actual physical disk starting at the first sector of the first track, sequentially, until the last sector of the last track.
Partitions [With all operating systems, disk drives are divided into sections called partitions. A typical disk might have 2 to 10 partitions. Each partition acts as a whole disk on its own, giving the effect of having more than one disk. For instance, you might have Windows installed on one partition and LINUX installed on another. More details come in Chapter 19.]are named
/dev/hda2, etc., indicating the first, second, etc., partition on physical drive
sdstands for SCSI disk, the high-end drives mostly used by servers.
sdais the first physical disk probed, and so on. Probing goes by SCSI ID and has a system completely different from that of IDE devices.
/dev/sda1is the first partition on the first drive, etc.
- These are serial devices numbered from
/dev/ttyS0is your first serial port (COM1 under MS-DOS or Windows). If you have a multiport card, these can go to
64, and up.
- PS/2 mouse.
- A symlink to
/dev/psaux. Other mouse devices are also supported.
- A symlink to
/dev/ttyS1or whatever port your modem is on.
- Identical to
ttyS? but now fallen out of use.
- Floppy disk.
fd0is equivalent to your
fd1devices autodetect the format of the floppy disk, but you can explicitly specify a higher density by using a device name like
/dev/fd0H1920, which gives you access to 1.88 MB, formatted, 3.5-inch floppies. Other floppy devices are shown in Table 18.1.
See Section 19.3.4 on how to format these devices.
- Parallel port.
/dev/par0is your first parallel port or LPT1 under DOS.
- Line printer. Identical to
- Random number generator. Reading from this device gives pseudo-random numbers.
- SCSI tape. SCSI backup tape drive.
- Produces zero bytes, and as many of them as you need. This is useful
if you need to generate a block of zeros for some reason. Use
dd(see Section 18.5.2) to read a specific number of zeros.
- Null device. Reads nothing. Anything you write to the device is discarded. This is very useful for discarding output.
- Parallel port IDE disk.
- Parallel port ATAPI CD-ROM.
- Parallel port ATAPI disk.
- SCSI CD-ROM.
- SCSI CD-ROM (Identical, alternate name).
- SCSI generic. This is a general-purpose SCSI command interface for devices like scanners.
- Frame buffer. This represents the kernel's attempt at a graphics driver.
- A symlink to
/dev/hdc. It can also be linked to your SCSI CD-ROM.
- ISDN modems.
- Virtual console. This is the terminal device for
the virtual console itself and is numbered
/dev/tty?? (3) and
TTY devices used for emulating a terminal. These are
called pseudo-TTYs and are identified by two lowercase
letters and numbers, such as
ttyq3. To nondevelopers, these are mostly of theoretical interest.
/usr/src/linux/Documentation/devices.txt also has this to
say (quoted verbatim):
It is recommended that these links exist on all systems:
Locally defined links
The following links may be established locally to conform to the configuration of the system. This is merely a tabulation of existing practice, and does not constitute a recommendation. However, if they exist, they should have the following uses:
/dev/modemshould not be used for a modem which supports dial-in as well as dialout, as it tends to cause lock file problems. If it exists,
/dev/modemshould point to the appropriate primary TTY device (the use of the alternate callout devices is deprecated).
For SCSI devices,
/dev/cdromshould point to the ``cooked'' devices (
/dev/sr*, respectively), whereas
/dev/scannershould point to the appropriate generic SCSI devices (
/dev/mousemay point to a primary serial TTY device, a hardware mouse device, or a socket for a mouse driver program (e.g.
Sockets and pipes
Non-transient sockets and named pipes may exist in
/dev. Common entries are:
dd probably originally stood for disk dump. It is actually just
cat except it can read and write in discrete
blocks. It essentially reads and writes between devices while
converting the data in some way. It is generally used in one of these ways:
dd, you must specify an input file and an output file with the
of= options. If the
of= option is omitted,
dd writes to stdout. If the
if= option is omitted,
dd reads from stdin. [If you are confused, remember that
dd thinks of in and out with respect to itself.]
dd is an unforgiving and destructive command
that should be used with caution.
To create a new RedHat boot floppy, find the
boot.img file on
and with a new floppy, run:
This command writes the raw disk image directly to the floppy disk. All distributions will have similar disk images for creating installation floppies (and sometimes rescue floppies).
If you have ever tried to repartition a LINUX disk back into a
DOS/Windows disk, you will
know that DOS/Windows
FDISK has bugs in it that prevent it from recreating the
partition table. A quick
will write zeros to the first 10 megabytes of your first IDE drive. This will wipe out the partition table as well as any file system information and give you a ``brand new'' disk.
To zero a floppy disk is just as easy:
Even writing zeros to a floppy may not be sufficient. Specialized equipment can probably still read magnetic media after it has been erased several times. If, however, you write random bits to the floppy, it becomes completely impossible to determine what was on it:
Here is a nice trick to find out something about a hard drive:
x86 boot sector.
To discover what a floppy disk is, try
x86 boot sector, system )k?/bIHC, FAT (12 bit)
for DOS floppies.
If you have two IDE drives that are of identical size, and provided that you are sure they contain no bad sectors and provided neither are mounted, you can run
to copy the entire disk and avoid having to install an operating system from scratch. It doesn't matter what is on the original (Windows, LINUX, or whatever) since each sector is identically duplicated; the new system will work perfectly.
(If they are not the same size, you will have to use
mirrordir to replicate the file system exactly.)
You can use
tar to back up to any device.
Consider periodic backups to an ordinary IDE drive instead of a
tape. Here we back up to the secondary slave:
tar can also back up across multiple floppy disks:
0 and archive the
/home directory onto it. You should not try to use
compression with tape drives because they are error prone, and a single
error could make the entire archive unrecoverable. The
mt command stands
tape and controls generic SCSI tape
devices. See also
If you don't want to see any program output,
> /dev/null to the command. For example,
we aren't often interested in the output of
make is discussed later.]Here we absorb everything save for error messages.
Then, of course, we can absorb all output including error messages with either
innumerable uses in shell scripting to suppress the output of a
command or to feed a command dummy (empty) input.
/dev/null is a safe file from a security point
of view. It is often used when a file is required for some
feature in a configuration script, and you would like the
particular feature disabled. For instance, specifying the users
/dev/null inside the password file will
certainly prevent insecure use of a shell, and is an explicit
way of saying that that account does not allow shell
You can also use
/dev/null to create a file containing nothing:
or alternatively, to create a file containing only zeros. Try
Although all devices are listed in the
you can create a device anywhere in the file system by using the
c are for creating a block
or character device, respectively.
To demonstrate, try
my-floppy can be used just like
Note carefully the mode (i.e., the permissions) of
/dev/fd0 should be readable and writable only to
to users belonging to the
floppy group, since we
obviously don't want an arbitrary user to be able to log in
(remotely) and overwrite a floppy disk.
In fact, this is the reason for having devices represented as files in the first place. UNIX files naturally support group access control, and therefore so do devices.
To create devices that are missing from your
directory (some esoteric devices will not be present by
default), simply look up the device's major and minor number in
/usr/src/linux/Documentation/devices.txt and use the
mknod command. This procedure is, however, somewhat tedious, and
/dev/MAKEDEV is usually available for convenience.
You must be in the
/dev directory before you run this
Typical usage of
to create a complete set of floppy disk devices.
man page for
MAKEDEV contains more
details. In particular, it states:
Note that programs giving the error ``ENOENT: No such file or directory'' normally means that the device file is missing, whereas ``ENODEV: No such device'' normally means the kernel does not have the driver configured or loaded.
Next: 19. Partitions, File Systems, Up: rute Previous: 17. Overview of the   Contents
Только зарегистрированные пользователи могут оценивать и комментировать статьи.