Managing Files for Character Devices, Block Devices, and Special Devices

 < Day Day Up > 

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'll 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 16.) 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

  • /dev/null This is the null device, also called the "bit bucket." Any output written to it is discarded. It is useful to redirect messages to it when you do not want them displayed on the standard output or written to a file. For example,

     $ ls 2> /dev/null 

    will display the file listing to the screen, but will not display any error messages even though the ls command might generate one; we redirected the standard error messages (2>) to /dev/null. Almost all commands generate messages to the standard output and standard error when run, and sometimes, as when running a script, it is useful to not see the messages.

  • /dev/zero This device has an inexhaustible supply of zeros; use all you want it makes more. It is very useful for writing strings of zeros to a device or file, such as when preparing an image of a file system (perhaps to be copied onto compact flash or other media).

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 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.

The Device File System devfs

An alternative device file system known as the Device File System (devfs) will soon come into common use in Linux. It only has entries for devices that you have attached to your system, and the naming convention is different from the traditional system. It can be used in tandem with the traditional system. Although Fedora Core supports devfs in the kernel sources, it is not enabled by default because Red Hat does not yet believe it is stable for Enterprise use.

Naming conventions are different with devfs. For example, /dev/hda1 is named /dev/ide/hd/c0b0t0u0p1 and is a symbolic link to /dev/ide/host0/bus0/target0/lun0/part1.

The Device File System is similar to the /proc file system in that it is a virtual file system; the devices exist in memory and not as actual files on the drive.

Why use devfs? There are some limitations to the use of major and minor numbers (128 maximum are available), which is why H. Peter Anvin maintains devices.txt, and access to new device numbers is limited. The address space could be enlarged, but this creates some performance issues. It also gets around the requirement that the /dev directory be on the root file system, making read-only root file systems difficult to use. You cannot share a root file system using NFS, and you cannot embed it in a ROM file system. The workaround requires creating a ramdisk and copying the contents of /dev there.

Non-Unix file systems cannot be mounted as the root file system because they do not support the special characteristics of Unix file systems. devfs can solve these types of problems and provide even more flexibility to Linux. FreeBSD, BeOS, Plan9, and QNX use devfs; so it is not a new concept, just new to Linux.

For additional details, read the Linux DEVFS FAQ at http://www.atnf.csiro.au/~rgooch/linux/docs/devfs.html.


Relevant Fedora Core and Linux Commands

You use these commands when managing file systems in Fedora Core Linux:

df Shows free disk space

du Displays disk usage

dump An ext2 file system backup utility

dumpe2fs Shows information about an ext2 file system

e2fsadm Administers an LVM/ext2 file system

e2image Creates an image file of ext2 file system data

fdisk The standard Linux partition table editor

fsck Checks or repairs a file system

lsraid Displays information about Linux RAID devices

mformat Formats a DOS floppy disk; part of the Mtools suite of tools

mkfs Creates various file systems and acts as a wrapper for the actual programs that do the work

mkisofs Creates a CD-ROM file system in iso960 format

mkreiserfs Creates a Linux reiserfs file system

mkswap Prepares a Linux swap device

mount Mounts a supported file system

parted The GNU partition editor and resizing utility

reiserfsck Checks a Linux reiserfs file system

resize_reiserfs Resizes a Linux reiserfs file system

smbmount Mounts an smbfs file system

stat Shows file or file system status

swapon Displays swap usage or start using system swap device

swapoff Turns off swap usage

sync Flushes file system buffers

tune2fs Changes file system parameters on ext2 file systems

umount Unmounts a file systems

usermount The Fedora Core graphical file system mounting and formatting tool


     < Day Day Up > 


    Red Hat Fedora 4 Unleashed
    Red Hat Fedora 4 Unleashed
    ISBN: 0672327929
    EAN: 2147483647
    Year: 2006
    Pages: 361

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net