Managing Devices

Managing Devices

Everything you use to work with your computer is a device. The keyboard you use to type and the mouse you click, the hard disk where you store everything, and the printer where you get your hard copies-these are all devices. You often have to load device drivers to use the device in Linux.

To manage devices, you need to understand device drivers a bit and learn to use the tools Red Hat Linux includes to help you manage devices. The next few sections provide a brief overview of device drivers and explain how to manage driver modules.

Understanding Device Drivers

The Linux kernel treats all devices as files and uses a device just as it would use a file-opens it, writes data to it, reads data from it, and closes it when done. This ability to treat every device as a file comes through the use of device drivers. A device driver is a special program that controls a particular type of hardware. When the kernel writes data to the device, the device driver does whatever is appropriate for that device. For example, when the kernel writes data to the floppy drive, the floppy device driver puts that data onto the physical medium of the floppy disk. On the other hand, if the kernel writes data to the parallel port device, the parallel port driver sends the data to the printer connected to the parallel port.

Thus, the device driver isolates the device-specific code from the rest of the kernel and makes a device look like a file. Any application can access a device by opening the file specific to that device. Figure 20-2 illustrates this concept of Linux device drivers.

Click To expand
Figure 20-2: Application Accessing Devices through Device Drivers.

Device Files

As Figure 20-2 shows, applications can access a device as if it were a file. These files are special files called device files, and they appear in the /dev directory in the Red Hat Linux file system.

If you use the ls command to look at the list of files in the /dev directory, you'll see several thousand files. This does not mean that your system has several thousand devices. The /dev directory has files for all possible types of devices-that's why the number of device files is so large.

So how does the kernel know which device driver to use when an application opens a specific device file? The answer is in two numbers called the major and minor device numbers. Each device file is mapped to a specific device driver through these numbers.

To see an example of the major and minor device numbers, type the following command in a terminal window:

ls -l /dev/hda

You should see a line of output similar to the following:

brw-rw----    1 root     disk       3,   0 Feb 16 14:50 /dev/hda

In this line, the major and minor device numbers appear just before the date. In this case, the major device number is 3 and the minor device number is 0. The kernel selects the device driver for this device file by using the major device number.

You don't really need to learn much about the device files and the device numbers, except to be aware of their existence.

Block Devices

The first letter in the listing of a device file also provides an important clue. For the /dev/hda device, the first letter is a b, which indicates that /dev/hda is a block device-one that can accept or provide data in chunks (typically 512 bytes or 1KB). By the way, /dev/hda refers to the first IDE hard disk on your system (the C drive in Windows). Hard drives, floppy drives, and CD-ROM drives are all examples of block devices.

Character Devices

If the first letter in the listing of a device file is a c, then the device is a character device-one that can receive and send data one character (1 byte) at a time. For example, the serial port and parallel ports are character devices. To see the listing of a character device, type the following command in a terminal window:

ls -l /dev/ttyS0

The listing of this device should be similar to the following:

crw-rw----    1 root     uucp       4,  64 Feb 16 14:50 /dev/ttyS0

Notice that the very first letter is a c because /dev/ttyS0-the first serial port-is a character device.

Network Devices

The network devices, such as Ethernet and dial-up point-to-point protocol (PPP) connections, are somewhat special in that they do not have a file corresponding to the device; instead the kernel uses a special name for the device. For example, the Ethernet devices are named eth0 for the first Ethernet card, eth1 for the second one, and so on. The PPP connections are named ppp0, ppp1, and so on.

Because the network devices are not mapped to device files, there are no files corresponding to these devices in the /dev directory.

Managing Loadable Driver Modules

To use any device, the Linux kernel must contain the driver. If the driver code is linked into the kernel as a monolithic program-a program that's in the form of a single large file, then adding a new driver means rebuilding the kernel with the new driver code. Rebuilding the kernel means you have to reboot the PC with the new kernel before you can use the new device driver. Luckily, the Linux kernel uses a modular design that does away with all the rebooting hassles. Linux device drivers can be created in the form of modules that the kernel can load and unload without having to restart the PC.


Driver modules are one type of a broader category of software modules called loadable kernel modules (LKM). Other types of kernel modules include code that can support new types of file systems, modules for network protocols, and modules that interpret different formats of executable files.

Loading and Unloading Modules

You can manage the loadable device driver modules by using a set of commands. Table 20-8 summarizes these commands. You have to log in as root to use some of these commands. I explain a few of the commonly used module commands.

Table 20-8: Commands to Manage Kernel Modules




Inserts a module into the kernel


Removes a module from the kernel


Determines interdependencies between modules


Displays a list of symbols along with the name of the module that defined the symbol


Lists all currently loaded modules


Displays information about a kernel module


Inserts or removes a module or a set of modules intelligently. For example, if module A requires B, then modprobe will automatically load B when asked to load B.

If you need to use any of these commands, log in as root or type su - in a terminal window to become root.

To see what modules are currently loaded, type:


You should see a list of modules. For example, here is a list on one of my PCs:

Module                  Size  Used by    Not tainted
ide-cd                 35772   0  (autoclean)
cdrom                  33696   0  (autoclean) [ide-cd]
parport_pc             19076   1  (autoclean)
lp                      8996   0  (autoclean)
parport                37056   1  (autoclean) [parport_pc lp]
autofs                 13276   0  (autoclean) (unused)
ds                      8680   1
yenta_socket           13440   1
pcmcia_core            57216   0  [ds yenta_socket]
3c59x                  30832   1
keybdev                 2976   0  (unused)
mousedev                5492   1
hid                    22148   0  (unused)
input                   5888   0  [keybdev mousedev hid]
usb-uhci               26444   0  (unused)
usbcore                78880   1  [hid usb-uhci]
ext3                   88168   2
jbd                    51828   2  [ext3]

As you may expect, the list of modules depends on the types of devices installed on your system.

The first column lists the names of the modules in the last-to-first order. Thus, the first module in the list is the last one to be loaded. The Size column shows the number of bytes that the module occupies in your PC's memory. The remaining columns show other information about each module such as how many applications are currently using the module and the names of the modules that require the module.

The list displayed by lsmod includes all types of Linux kernel modules, not just device drivers. For example, the last two modules-jbd and ext3-are both part of the EXT3 file system (the latest file system for Linux).

Understanding the /etc/modules.conf File

How does the modprobe command know that it should load the opl3sa2 driver module when I use a module name sound-slot-0? The answer is in the /etc/modules.conf configuration file. That file contains a line that tells modprobe what it should load when it sees the module name sound-slot-0.

To view the contents of /etc/modules.conf, type

cat /etc/modules.conf

On my Red Hat Linux PC, the file contains the following lines:

alias parport_lowlevel parport_pc
alias eth0 eepro100
alias usb-controller usb-uhci
alias sound-slot-0 opl3sa2

Each line that begins with the keyword alias defines a standard name for an actual driver module. For example, the second line defines eepro100 as the actual driver name for the alias eth0, which stands for the first Ethernet card. Similarly, the fourth line defines opl3sa2 as the module to load when I use the name sound-slot-0.