SourceForge VA Linux Systems
Copyright © 2000 Paul Sheer - Click here for copying permissions       Source by FTP

next up previous contents index
Next: Partitioning, formatting and mounting Up: Rute Users Tutorial and Previous: UNIX Directory Layout   Contents   Index

Subsections

UNIX devices

Add comments here 

Device files

UNIX has a beautifully consistent method of allowing programs to access hardware. Under UNIX, every piece of hardware is a file. To demonstrate this, try view the file /dev/hda

 
less -f /dev/hda

/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 /dev directory.

Device files allow access to hardware. If you have a sound card install and configured, you can try:

 
cat /dev/dsp > my_recording

Say something into your microphone and then type:

 
cat my_recording > /dev/dsp

Which will play out the sound through your speakers (note that this will not always work, since the recording volume may not be set correctly, nor the recording speed.)

If no programs are currently using your mouse, you can also try:

 
cat /dev/mouse

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.

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 less and cat above).
  • 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 etc.)
Because every kind of device that one can think of can be twisted to fit these two modes of operation (except for network cards), UNIX's scheme has endured since its inception and is considered the ubiquitous method of accessing hardware.

Block and character devices

Hardware devices can generally be categorised into random access devices like disk and tape drives, and serial devices like mouses, 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. Doing an ls -l /dev/hda shows that your hard disk is a block device by the b on the far left of the listing:

 
brw-r-----   1 root     disk       3,  64 Apr 27  1995 /dev/hdb

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. These are called character devices and are indicated by a c on the far left of the listing. Your /dev/dsp (Digital Signal Processor -- i.e. sound card) device looks like:

 
crw-r--r--   1 root     sys       14,   3 Jul 18  1994 /dev/dsp

Major and Minor device numbers

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 like /dev/sda which is minor device 0). The major and minor device number is what identifies the device to the kernel. The file-name of the device is really arbitrary and is chosen for convenience and consistency. You can see the major and minor device number (8,   0) in the ls listing for /dev/sda:

 
brw-rw----   1 root     disk       8,   0 May  5  1998 /dev/sda

Miscellaneous devices

A list of common devices and their descriptions follows. The major numbers are shown in braces. The complete reference for Devices is the file /usr/src/linux/Documentation/devices.txt.
/dev/hd??
hd stands for Hard Disk, but refers here only to IDE devices -- i.e. common hard disks. The first letter after the hd dictates the physical disk drive:
/dev/hda (3)
First drive, or primary master.
/dev/hdb (3)
Second drive, or primary slave.
/dev/hdc (22)
Third drive, or secondary master.
/dev/hdd (22)
Fourth drive, or secondary slave.
When accessing any of these devices, 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.
Partitions21.1are named /dev/hda1, /dev/hda2 etc.indicating the first, second etc.partition on physical drive a.
/dev/sd?? (8)
sd stands for SCSI Disk, the high end drives mostly used by servers. sda is the first physical disk probed and so on. Probing goes by Scsi ID and has a completely different system to IDE devices. /dev/sda1 is the first partition on the first drive etc.
/dev/ttyS? (4)
These are serial devices devices numbered from 0 up. /dev/ttyS0 is your first serial port (COM1 under DOS). If you have a multi-port card, these can go up to 32, 64 etc.
/dev/psaux (10)
PS/2 mouse.
/dev/mouse
Is just a symlink to /dev/ttyS0 or /dev/psaux. There are other mouse devices supported also.
/dev/modem
Is just a symlink to /dev/ttyS1 or whatever port your modem is on.
/dev/cua? (4)
Identical to ttyS? but now fallen out of use.
/dev/fd? (2)
Floppy disk. fd0 is equivalent to your A: drive and fd1 your B: drive. The fd0 and fd1 devices auto-detect 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.88MB formatted 3.5 inch floppies.
See Section 22.3 on how to format these devices.
Floppy devices are named /dev/fdlmnnnn
l 0 A: drive
  1 B: drive
m d ``double density'', ``360kB'' 5.25 inch
  h ``high density'', ``1.2MB'' 5.25 inch
  q ``quad density'' 5.25 inch
  D ``double density'', ``720kB'' 3.5 inch
  H ``high density'', ``1.44MB'' 3.5 inch
  E Extra density 3.5 inch.
  u Any 3.5 inch floppy. Note that u is now replacing D, H and E, thus leaving it up to the user to decide if the floppy has enough density for the format.
nnnn

360 410 420 720 800 820 830 880 1040 1120 1200 1440 1476 1494 1600 1680 1722 1743 1760 1840 1920 2880 3200 3520 3840

The size of the format. With D, H and E, 3.5 inch floppies only have devices for the sizes that are likely to work. For instance there is no /dev/fd0D1440 because double density disks won't manage 1440kB. /dev/fd0H1440 and /dev/fd0H1920 are probably the ones you are most interested in.

/dev/par? (6)
Parallel port. /dev/par0 is your first parallel port or LPT1 under DOS.
/dev/lp? (6)
Line printer. Identical to /dev/par?.
/dev/random
Random number generator. Reading from this device give pseudo random numbers.
/dev/st? (9)
SCSI tape. SCSI backup tape drive.
/dev/zero (1)
Produces zero bytes, and as many of them us you need. This is useful if you need to generate a block of zeros for some reason. Use dd (see below) to read a specific number of zeros.
/dev/null (1)
Null device. Reads nothing. Anything you write to the device is discarded. This is very useful for discarding output.
/dev/pd?
parallel port IDE disk.
/dev/pcd?
parallel port ATAPI CDROM.
/dev/pf?
parallel port ATAPI disk.
/dev/sr?
SCSI CDROM.
/dev/scd?
SCSI CDROM (Identical, alternate name).
/dev/fb? (29)
Frame buffer. This represents the kernels attempt at a graphics driver.
/dev/cdrom
Is just a symlink to /dev/hda, /dev/hdb or /dev/hdc. It also my be linked to your SCSI CDROM.
/dev/ttyI?
ISDN Modems.
/dev/tty? (4)
Virtual console. This is the terminal device for the virtual console itself and is numbered /dev/tty1 through /dev/tty63.
/dev/tty?? (3) and /dev/pty?? (2)
Other TTY devices used for emulating a terminal. These are called pseudo-TTY's and are identified by two lower case letters and numbers, such as ttyq3. To non-developers, these are mostly of theoretical interest.
The file /usr/src/linux/Documentation/devices.txt also has this to say:
                Recommended links
It is recommended that these links exist on all systems:
/dev/core /proc/kcore symbolic Backward compatibility
/dev/ramdisk ram0 symbolic Backward compatibility
/dev/ftape qft0 symbolic Backward compatibility
/dev/bttv0 video0 symbolic Backward compatibility
/dev/radio radio0 symbolic Backward compatibility
/dev/i2o* /dev/i2o/* symbolic Backward compatibility
/dev/scd? sr? hard Alternate SCSI CD-ROM name
                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/mouse mouse port symbolic Current mouse device
/dev/tape tape device symbolic Current tape device
/dev/cdrom CD-ROM device symbolic Current CD-ROM device
/dev/cdwriter CD-writer symbolic Current CD-writer device
/dev/scanner scanner symbolic Current scanner device
/dev/modem modem port symbolic Current dialout device
/dev/root root device symbolic Current root filesystem
/dev/swap swap device symbolic Current swap device
/dev/modem should not be used for a modem which supports dialin as well as dialout, as it tends to cause lock file problems. If it exists, /dev/modem should point to the appropriate primary TTY device (the use of the alternate callout devices is deprecated).
For SCSI devices, /dev/tape and /dev/cdrom should point to the ``cooked'' devices (/dev/st* and /dev/sr*, respectively), whereas /dev/cdwriter and /dev/scanner should point to the appropriate generic SCSI devices (/dev/sg*).
/dev/mouse may point to a primary serial TTY device, a hardware mouse device, or a socket for a mouse driver program (e.g. /dev/gpmdata).
                Sockets and pipes
Non-transient sockets and named pipes may exist in /dev. Common entries are:
/dev/printer socket lpd local socket
/dev/log socket syslog local socket
/dev/gpmdata socket gpm mouse multiplexer

dd, tar and tricks with block devices

dd probably originally stood for disk dump. It is actually just like 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:

 
 
 
 
5 
 
 
 
 
dd if=<in-file> of=<out-file> [bs=<block-size>] \
       [count=<number-of-blocks>] [seek=<output-offset>] \
       [skip=<input-offset>]

dd if=<in-file> [bs=<block-size>] [count=<number-of-blocks>] \
       [skip=<input-offset>] > <outfile>

dd of=<out-file> [bs=<block-size>] [count=<number-of-blocks>] \
       [seek=<output-offset>] < <infile>

dd works by specifying an input file and an output file with the if= and of= options. If the of= option is omitted, then dd writes to stdout. If the if= option is omitted, then dd reads from stdin.

Create boot disks from boot images

To create a new RedHat boot floppy, find the boot.img file on ftp.redhat.com, and with a new floppy, do:

 
dd if=boot.img of=/dev/fd0

This will write the raw disk image directly to the floppy disk.

Erasing disks

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:

 
dd if=/dev/zero of=/dev/hda bs=1024 count=10240

will write zeros to the first ten megabytes of your first IDE drive. This will wipe out the partition table as well as any file-system and give you a ``barnd new'' disk.

To zero a floppy disk is just as easy:

 
dd if=/dev/zero of=/dev/fd0 bs=1024 count=1440

Identifying data on raw disks

Here is a nice trick to find out something about a hard drive:

 
dd if=/dev/hda1 count=1 bs=512 | file -

gives x86 boot sector.

To discover what a floppy disk is, try

 
dd if=/dev/fd0 count=1 bs=512 | file -

gives x86 boot sector, system )k?/bIHC, FAT (12 bit) for DOS floppies.

Duplicating a disk

If you have two IDE drives that are of identical size, provided that you are sure that they contain no bad sectors, you can do

 
dd if=/dev/hdc of=/dev/hdd

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.

Floppy backups

tar can be used to backup to any device. Consider periodic backups to an ordinary IDE drive instead of a tape. Here we backup to the secondary slave:

 
tar -cvzf /dev/hdd /bin /boot /dev /etc /home /lib /sbin /usr /var

tar can also backup accross multiple floppy disks:

 
tar -cvMf /dev/fd0 /home/simon

Tape backups

tar traditionally backs up onto tape drives. The command

 
 
mt -f /dev/st0 rewind
tar -cvf /dev/st0 /home

rewinds scsi tape 0 and archives 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 archive irrecoverable. The mt command stands for magnetic tape, and is used to control generic SCSI tape devices. See also mt1.

Hiding program output, creating blocks of zeros

If you don't want to see any program output, just append > /dev/null to the command. For example, we aren't often interested in the output of make21.2, only the error messages:

 
make > /dev/null

And,

 
make >& /dev/null

also absorbs all error messages. /dev/null finds enumerable uses in shell scripting to suppress the output of a command or feed a command dummy (empty) input. /dev/null is a safe file from a security point of view, and is often used where a file is required for some feature in some configuration script, where you would like the particular feature disabled. For instance, specifying the users shell to /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 logins.

/dev/null can also be used to create a file containing nothing:

 
cat /dev/null > myfile

or alternatively, to create a file containing only zeros, try

 
dd if=/dev/zero bs=1024 count=<number-of-kilobytes> > myfile

Creating devices with mknod and /dev/MAKEDEV

Although all devices are listed in the /dev directory, you can create a device anywhere in the file system using the mknod command:

 
mknod [-m <mode>] <file-name> [b|c] <major-number> <minor-number>

The letters b and c are for creating a block or character device respectively.
To demonstrate, try

 
 
mknod -m 0600 ~/my-floppy b 2 0
ls -al /dev/fd0 ~/my-floppy

my-floppy can be used just like /dev/fd0

Note carefully the mode (i.e. the permissions) of /dev/fd0. /dev/fd0 should be readable and writable only to root and to users belonging to the floppy group, since we obviously don't want an arbitrary user to be able to login (remotely) and write over a floppy disk.
In fact, this is the reason for having devices represented as files in the first place. UNIXfiles naturally support group access control, and therefore so also do devices.
To create devices that are missing from your /dev 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 is however somewhat tedious, and the script /dev/MAKEDEV is usually present for convenience. You must be in the /dev directory before you run this script.
Typically example usage of MAKEDEV is,

 
 
 
cd /dev
./MAKEDEV -v fd0
./MAKEDEV -v fd1

to create a complete set of floppy disk devices.

The man page for MAKEDEV contains more details, and explains the following:
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 up previous contents index
Next: Partitioning, formatting and mounting Up: Rute Users Tutorial and Previous: UNIX Directory Layout   Contents   Index
Paul Sheer 2000-10-07