Chapter 23
Dealing with Linux Devices

  • images Objective 2.7: Explain the use and operation of Linux devices.

images The typical Linux system has lots of different hardware devices connected to it. The list can include hard drives, monitors, keyboards, printers, audio cards, and network cards. Part of your job as a Linux administrator is to make sure all of those devices are working, and working properly. This chapter walks through how to install and troubleshoot the different types of hardware devices that can be connected to your Linux system. First, the chapter discusses the different types of device interfaces you may need to work with on your Linux system and how they communicate with the operating system. Following that, the chapter discusses the Linux utilities available for you to monitor and troubleshoot how those devices on your system are working. Finally, the chapter dives into the topic of hot pluggable devices, a topic that has become extremely important with the popularity of USB devices.

Communicating with Linux Devices

For any device to work on your Linux system, the Linux kernel must recognize it and know how to talk to it. The kernel uses installed modules (see Chapter 14) to know how to communicate with each type of hardware device on the system. If the module for a particular hardware device isn’t loaded, then the kernel won’t be able to communicate with the device.

After the kernel module is installed, the kernel must know how to communicate with the device. Linux supports several different types of hardware interfaces and methods for communicating with devices.

Device Interfaces

Each device you connect to your Linux system uses some type of standard protocol to communicate with the system hardware. The kernel module software must know how to send data to and receive data from the hardware device using those protocols. There are currently three popular standards used to connect devices.

PCI Boards

The Peripheral Component Interconnect (PCI) standard was developed in 1993 as a method for connecting hardware boards to PC motherboards. The standard has been updated a few times to accommodate faster interface speeds as well as increase data bus sizes on motherboards. The PCI Express (PCIe) standard is currently used on most server and desktop workstations to provide a common interface for external hardware cards.

Lots of different client devices use PCI boards to connect to a server or desktop workstation:

  • Internal Hard Drives: Hard drives using the Serial Advanced Technology Attachment (SATA) and the Small Computer System Interface (SCSI) connectors often use PCI boards to connect with workstations or servers. The Linux kernel automatically recognizes both SATA and SCSI hard drives connected to PCI boards.
  • External Hard Drives: Network hard drives using the Fiber Channel standard provide a high-speed shared drive environment for server environments. To communicate on a fiber channel network, the server usually uses PCI boards that support the Host Bus Adapter (HBA) standard.
  • Network Interface Cards: Hard-wired network cards allow you to connect the workstation or server to a local area network using the common RJ-45 cable standard. These types of connections are mostly found in high-speed network environments that require high throughput to the network.
  • Wireless Cards: There are PCI boards available that support the IEEE 802.11standard for wireless connections to local area networks. While these are not commonly used in server environments, they are very popular in workstation environments.
  • Bluetooth Devices: The Bluetooth technology allows for short distance wireless communication with other Bluetooth devices in a peer-to-peer network setup. These are most commonly found in workstation environments.
  • Video Accelerators: Applications that require advanced graphics often use video accelerator cards, which offload the video processing requirements from the CPU to provide faster graphics. While these are popular in gaming environments, you’ll also find video accelerator cards used in video processing applications for editing and processing movies.
  • Audio Cards: Similarly, applications that require high-quality sound often use specialty audio cards to provide advanced audio processing and play, such as handling Dolby surround sound to enhance the audio quality of movies.

The USB Interface

The Universal Serial Bus (USB) interface has become increasingly popular due to its ease of use and its increasing support for high-speed data communication. Since the USB interface uses serial communications, it requires fewer connectors with the motherboard, allowing for smaller interface plugs.

The USB standard has evolved over the years. The original version 1.0 only supported data transfer speeds up to 12Mbps. The 2.0 standard increased the data transfer speed to 480Mbps. The current USB standard, 3.2, allows for data transfer speeds up to 2.5Gbps, making it useful for high-speed connections to external storage devices.

There are a myriad of devices that can connect to systems using the USB interface. You can find hard drives, printers, digital cameras and camcorders, keyboards, mice, and network cards that have versions that connect using the USB interface.

images There are two steps to get Linux to interact with USB devices. The first step is that the Linux kernel must have the proper module installed to recognize the USB controller installed on your server, workstation, or laptops. The controller provides communication between the Linux kernel and the USB bus on the system. When the Linux kernel can communicate with the USB bus, any device you plug into a USB port on the system will be recognized by the kernel but is not necessarily usable. The second step is that the Linux system must have a kernel module installed for the individual device type plugged into the USB bus. Linux distributions have a wide assortment of modules installed by default. Should you run into a USB device that doesn’t work on your Linux system, refer to Chapter 14 for information on installing kernel modules.

The GPIO Interface

The General Purpose Input/Output (GPIO) interface has become popular with small utility Linux systems designed for controlling external devices for automation projects. This includes popular hobbyist Linux systems such as the Raspberry Pi and BeagleBone kits.

The GPIO interface provides multiple digital input and output lines that you can control individually, down to the single-bit level. The GPIO function is normally handled by a specialty integrated circuit (IC) chip, which is mapped into memory on the Linux system.

The GPIO interface is ideal for supporting communications to external devices such as relays, lights, sensors, and motors. Applications can read individual GPIO lines to determine the status of switches, turn relays on or off, or read digital values returned from any type of analog-to-digital sensors such as temperature or pressure sensors.

With the GPIO interface, you have a wealth of possibilities for using Linux to control objects and environments. You can write programs that control the temperature in a room, sense when doors or windows are opened or closed, sense motion in a room, or even control the operation of a robot.

The /dev Directory

After the Linux kernel can communicate with a device on an interface, it must be able to transfer data to and from the device. For many devices, this is done using device files. Device files are files that the Linux kernel creates in the special /dev directory to interface with hardware devices.

To retrieve data from a specific device, a program just needs to read the Linux device file associated with that device. The Linux operating system handles all the unsightliness of interfacing with the actual hardware. Likewise, to send data to the device, the program just needs to write to the Linux device file.

As you add hardware devices such as USB drives, network cards, or hard drives to your system, Linux creates a file in the /dev directory representing that hardware device. Application programs can then interact directly with that file to store and retrieve data on the device. This is a lot easier than requiring each application to know how to directly interact with a device.

There are two types of device files in Linux, based on how Linux transfers data to the device:

  • Character device files: Transfer data one character at a time. This method is often used for serial devices such as terminals and USB devices.
  • Block device files: Transfer large blocks of data. This method is often used for high-speed data transfer devices such as hard drives and network cards.

The type of device file is denoted by the first letter in the permissions list, as shown in Listing 23.1.

Listing 23.1: Partial output from the /dev directory

$ ls -al sd* tty*
brw-rw---- 1 root disk    8,  0 Feb 16 17:49 sda
brw-rw---- 1 root disk    8,  1 Feb 16 17:49 sda1
crw-rw-rw- 1 root tty     5,  0 Feb 16 17:49 tty
crw--w---- 1 root tty     4,  0 Feb 16 17:49 tty0
crw--w---- 1 gdm  tty     4,  1 Feb 16 17:49 tty1

The hard drive devices, sda and sda1, show the letter b, indicating that they are block device files. The tty terminal files show the letter c, indicating that they are character device files.

Besides device files, Linux also provides a system called the device mapper. The device mapper function is performed by the Linux kernel. It maps physical block devices to virtual block devices. These virtual block devices allow the system to intercept the data written to or read from the physical device and perform some type of operation on them. Mapped devices are used by the Logical Volume Manager (LVM) for creating logical drives and by the Linux Unified Key Setup (LUKS) for encrypting data on hard drives.

images The device mapper creates virtual devices in the /dev/mapper directory. These files are links to the physical block device files in the /dev directory.

The /proc Directory

The /proc directory is one of the most important tools you can use when troubleshooting hardware issues on a Linux system. It’s not a physical directory on the filesystem but instead a virtual directory that the kernel dynamically populates to provide access to information about the system hardware settings and status.

The Linux kernel changes the files and data in the /proc directory as it monitors the status of hardware on the system. To view the status of the hardware devices and settings, you just need to read the contents of the virtual files using standard Linux text commands.

There are different /proc files available for different system features, including the IRQs, I/O ports, and DMA channels in use on the system by hardware devices. The following sections discuss the files used to monitor these features and how you can access them.

Interrupt Requests

Interrupt requests (called IRQs) allow hardware devices to indicate when they have data to send to the CPU. The PnP system must assign each hardware device installed on the system a unique IRQ address. You can view the current IRQs in use on your Linux system by looking at the /proc/interrupts file using the Linux cat command, as shown in Listing 23.2.

Listing 23.2: Listing system interrupts from the /proc directory

$ cat /proc/interrupts
           CPU0      
  0:         36   IO-APIC   2-edge      timer
  1:        297   IO-APIC   1-edge      i8042
  8:          0   IO-APIC   8-edge      rtc0
  9:          0   IO-APIC   9-fasteoi   acpi
 12:        396   IO-APIC  12-edge      i8042
 14:          0   IO-APIC  14-edge      ata_piix
 15:        914   IO-APIC  15-edge      ata_piix
 18:          2   IO-APIC  18-fasteoi   vboxvideo
 19:       4337   IO-APIC  19-fasteoi   enp0s3
...
$

In Listing 23.2, the first column indicates the IRQ assigned to the device. Some IRQs are reserved by the system for specific hardware devices, such as 0 for the system timer and 1 for the system keyboard. Other IRQs are assigned by the system as devices are detected at boot time.

I/O Ports

The system I/O ports are locations in memory where the CPU can send data to and receive data from the hardware device. As with IRQs, the system must assign each device a unique I/O port. This is yet another feature handled by the PnP system.

You can monitor the I/O ports assigned to the hardware devices on your system by looking at the /proc/ioports file, as shown in Listing 23.3.

Listing 23.3: Displaying the I/O ports on a system

$ cat /proc/ioports
0000-0cf7 : PCI Bus 0000:00
  0000-001f : dma1
  0020-0021 : pic1
  0040-0043 : timer0
  0050-0053 : timer1
  0060-0060 : keyboard
  0064-0064 : keyboard
  0070-0071 : rtc_cmos
    0070-0071 : rtc0
  0080-008f : dma page reg
  00a0-00a1 : pic2
  00c0-00df : dma2
  00f0-00ff : fpu
  0170-0177 : 0000:00:01.1
    0170-0177 : ata_piix
  01f0-01f7 : 0000:00:01.1
    01f0-01f7 : ata_piix
  0376-0376 : 0000:00:01.1
    0376-0376 : ata_piix
  03c0-03df : vga+
  03f6-03f6 : 0000:00:01.1
    03f6-03f6 : ata_piix
0cf8-0cff : PCI conf1
0d00-ffff : PCI Bus 0000:00
...
$

There are lots of different I/O ports in use on the Linux system at any time, so your output will most likely differ from this example. With PnP, I/O port conflicts aren’t very common, but it is possible that two devices are assigned the same I/O port. In that case, you can manually override the settings automatically assigned by using the setpci command.

Direct Memory Access

Using I/O ports to send data to the CPU can be somewhat slow. To speed things up, many devices use Direct Memory Access (DMA) channels. DMA channels do what the name implies; they send data from a hardware device directly to memory on the system, without having to wait for the CPU. The CPU can then read those memory locations to access the data when it’s ready.

As with I/O ports, each hardware device that uses DMA must be assigned a unique channel number. To view the DMA channels currently in use on the system, just display the /proc/dma file:

$ cat /proc/dma
 4: cascade
$

This output indicates that only DMA channel 4 is in use on the Linux system.

The /sys Directory

Yet another tool available for working with devices is the /sys directory. The /sys directory is another virtual directory, similar to the /proc directory. It provides additional information about hardware devices that any user on the system can access.

There are lots of information files available within the /sys directory. They are broken down into subdirectories based on the device and function in the system. You can take a look at the subdirectories and files available within the /sys directory on your system using the ls command-line command, as shown in Listing 23.4.

Listing 23.4: The contents of the /sys directory

$ ls -al /sys
total 4
dr-xr-xr-x  13 root root    0 Feb 16 18:06 .
drwxr-xr-x  25 root root 4096 Feb  4 06:54 ..
drwxr-xr-x   2 root root    0 Feb 16 17:48 block
drwxr-xr-x  41 root root    0 Feb 16 17:48 bus
drwxr-xr-x  62 root root    0 Feb 16 17:48 class
drwxr-xr-x   4 root root    0 Feb 16 17:48 dev
drwxr-xr-x  14 root root    0 Feb 16 17:48 devices
drwxr-xr-x   5 root root    0 Feb 16 17:49 firmware
drwxr-xr-x   8 root root    0 Feb 16 17:48 fs
drwxr-xr-x   2 root root    0 Feb 16 18:06 hypervisor
drwxr-xr-x  13 root root    0 Feb 16 17:48 kernel
drwxr-xr-x 143 root root    0 Feb 16 17:48 module
drwxr-xr-x   2 root root    0 Feb 16 17:48 power
$

Notice the different categories of information available. You can obtain information about the system bus, devices, kernel, and even kernel modules installed.

Working with Devices

Linux provides a wealth of command-line tools for using the devices connected to your system as well as monitoring and troubleshooting the devices if there are problems. The following sections walk through some of the more popular tools you’ll want to know about when working with Linux devices.

Finding Devices

One of the first tasks for a new Linux administrator is to find the different devices installed on the Linux system. Fortunately there are a few command-line tools to help out with that.

The lsdev command

The lsdev command-line command displays information about the hardware devices installed on the Linux system. It retrieves information from the /proc/interrupts, /proc/ioports, and /proc/dma virtual files and combines them in one output, as shown in Listing 23.5.

Listing 23.5: Output from the lsdev command

$ lsdev
Device             DMA   IRQ  I/O Ports
...
acpi                      9
ACPI                             4000-4003 4004-4005 4008-400b 4020-4021
ahci                             d240-d247 d248-d24b d250-d257 d258-d25b
ata_piix              14 15      0170-0177 01f0-01f7 0376-0376 03f6-03f6
cascade             4
dma                            0080-008f
dma1                           0000-001f
dma2                           00c0-00df
e1000                            d010-d017
enp0s3                   19
fpu                            00f0-00ff
i8042                  1 12
Intel                            d100-d1ff     d200-d23f
keyboard                       0060-0060   0064-0064
ohci_hcd:usb1            22
PCI                          0000-0cf7 0cf8-0cff 0d00-ffff
pic1                           0020-0021
pic2                           00a0-00a1
piix4_smbus                      4100-4108
rtc0                      8      0070-0071
rtc_cmos                       0070-0071
snd_intel8x0             21
timer                     0
timer0                         0040-0043
timer1                         0050-0053
vboxguest                20
vboxvideo                18
vga+                           03c0-03df
$

This provides you with one place to view all of the important information about the devices running on the system, making it easier to pick out any conflicts that can be causing problems.

images The lsdev tool is part of the procinfo package. You may need to manually install that package in some Linux distributions.

The lsblk command

The lsblk command-line command displays information about the block devices installed on the Linux system. By default, the lsblk command displays all of the block devices, as shown in Listing 23.6.

Listing 23.6: The output from the lsblk command

If you notice at the end of Listing 23.6, the lsblk command also indicates blocks that are related, as with the device-mapped LVM volumes and the associated physical hard drive. You can modify the lsblk output to see additional information about the blocks by adding command-line options. The -S option displays only information about SCSI block devices on the system:

$ lsblk -S
NAME HCTL       TYPE VENDOR   MODEL             REV TRAN
sda  2:0:0:0    disk ATA      VBOX HARDDISK    1.0  sata
sr0  1:0:0:0    rom  VBOX     CD-ROM           1.0  ata
$

This is a quick way to view the different SCSI drives installed on the system.

The dmesg command

The kernel ring buffer records kernel-level events as they occur. Since it’s a ring buffer, the event messages overwrite after the buffer area fills up. You can view the current messages in the kernel ring buffer by using the dmesg command. It helps to monitor it whenever you install a new device, as shown in Listing 23.7.

Listing 23.7: Partial output from the dmesg command

[ 2525.499216] usb 1-2: new full-speed USB device number 3 using ohci-pci
[ 2525.791093] usb 1-2: config 1 interface 0 altsetting 0 endpoint 0x1 has
 invalid maxpacket 512, setting to 64
[ 2525.791107] usb 1-2: config 1 interface 0 altsetting 0 endpoint 0x81 has
 invalid maxpacket 512, setting to 64
[ 2525.821079] usb 1-2: New USB device found, idVendor=abcd, idProduct=1234
[ 2525.821088] usb 1-2: New USB device strings: Mfr=1, Product=2,
 SerialNumber=3
[ 2525.821094] usb 1-2: Product: UDisk          
[ 2525.821099] usb 1-2: Manufacturer: General
[ 2525.821104] usb 1-2: SerialNumber:
[ 2525.927096] usb-storage 1-2:1.0: USB Mass Storage device detected
[ 2525.927950] scsi host3: usb-storage 1-2:1.0
[ 2525.928033] usbcore: registered new interface driver usb-storage
[ 2525.940376] usbcore: registered new interface driver uas
[ 2526.961754] scsi 3:0:0:0: Direct-Access     General  UDisk          
 5.00 PQ: 0 ANSI: 2
[ 2526.966646] sd 3:0:0:0: Attached scsi generic sg2 type 0
[ 2526.992707] sd 3:0:0:0: [sdb] 31336448 512-byte logical blocks: (16.0
 GB/14.9 GiB)
[ 2527.009197] sd 3:0:0:0: [sdb] Write Protect is off
[ 2527.009200] sd 3:0:0:0: [sdb] Mode Sense: 0b 00 00 08
[ 2527.026764] sd 3:0:0:0: [sdb] No Caching mode page found
[ 2527.026770] sd 3:0:0:0: [sdb] Assuming drive cache: write through
[ 2527.127613]  sdb: sdb1
[ 2527.229943] sd 3:0:0:0: [sdb] Attached SCSI removable disk

The output from the dmesg command shows the steps the kernel took to recognize the new USB device that was plugged into the system.

Since the kernel is responsible for detecting devices and installing the correct modules, the dmesg command is a great troubleshooting tool to use when a device isn’t working correctly. It can help you determine if a hardware device module didn’t load correctly.

Working with PCI Cards

The lspci command allows you to view the currently installed and recognized PCI and PCIe cards on the Linux system. There are lots of command-line options you can include with the lspci command to display information about the PCI and PCIe cards installed on the system. Table 23.1 shows the more common ones that come in handy.

Table 23.1 The lspci command-line options

Option Description
-A Defines the method to access the PCI information
-b Displays connection information from the card point of view
-k Displays the kernel driver modules for each installed PCI card
-m Displays information in machine-readable format
-n Displays vendor and device information as numbers instead of text
-q Queries the centralized PCI database for information about the installed PCI cards
-t Displays a tree diagram that shows the connections between cards and buses
-v Displays additional information (verbose) about the cards
-x Displays a hexadecimal output dump of the card information

The output from the lspci command without any options shows all devices connected to the system, as shown in Listing 23.8.

Listing 23.8: Using the lspci command

$ lspci
00:00.0 Host bridge: Intel Corporation 440FX - 82441FX PMC [Natoma] (rev 02)
00:01.0 ISA bridge: Intel Corporation 82371SB PIIX3 ISA [Natoma/Triton II]
00:01.1 IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)
00:02.0 VGA compatible controller: InnoTek Systemberatung GmbH VirtualBox
 Graphics Adapter
00:03.0 Ethernet controller: Intel Corporation 82540EM Gigabit Ethernet
 Controller (rev 02)
00:04.0 System peripheral: InnoTek Systemberatung GmbH VirtualBox Guest Service
00:05.0 Multimedia audio controller: Intel Corporation 82801AA AC'97 Audio
 Controller (rev 01)
00:06.0 USB controller: Apple Inc. KeyLargo/Intrepid USB
00:07.0 Bridge: Intel Corporation 82371AB/EB/MB PIIX4 ACPI (rev 08)
00:0d.0 SATA controller: Intel Corporation 82801HM/HEM (ICH8M/ICH8M-E) SATA Controller [AHCI mode] (rev 02)
$

You can use the output from the lspci command to troubleshoot PCI card issues, such as if a card isn’t recognized by the Linux system.

Working with USB Devices

You can view the basic information about USB devices connected to your Linux system by using the lsusb command. Table 23.2 shows the options that are available with that command.

Table 23.2 The lsusb command options

Option Description
-d Displays only devices from the specified vendor ID
-D Displays information only from devices with the specified device file
-s Displays information only from devices using the specified bus
-t Displays information in a tree format, showing related devices
-v Displays additional information about the devices (verbose mode)
-V Displays the version of the lsusb program

The basic lsusb program output is shown in Listing 23.9.

Listing 23.9: The lsusb output

$ lsusb
Bus 001 Device 003: ID abcd:1234 Unknown
Bus 001 Device 002: ID 80ee:0021 VirtualBox USB Tablet
Bus 001 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
$

Most systems incorporate a standard USB hub for connecting multiple USB devices to the USB controller. Fortunately there are only a handful of USB hubs on the market, so all Linux distributions include the device drivers necessary to communicate with each of these USB hubs. That guarantees that your Linux system will at least detect when a USB device is connected.

Supporting Monitors

There are two basic elements that control the video environment on your Linux system: the video card and the monitor. To display any type of text or graphics, your Linux system must know how to interact with both of them. This is where the X Window System software comes in.

The X Window System was developed at the Massachusetts Institute of Technology (MIT) to provide a standard protocol for interacting with displays. The X Window System is most commonly referred to as just X, or X11, since the last version defined is version 11.

The X11 system operates beneath the graphical desktop environment on your Linux system, as shown in Figure 23.1.

The diagram shows the standard Linux graphics environment. The diagram shows three different layers, where the very first layer (from top) of Graphical Application is “Graphical Desktop Manager.” The second layer depicts “X Window System.” The third layer depicts four different parts: “Video Card,” “Monitor,” “Keyboard” and “Mouse.”

Figure 23.1 The standard Linux graphics environment

The job of X11 is to interact with the hardware level of your system’s video environment—the video card, monitor, keyboard, and mouse—and provide a standard interface that any desktop management software (such as KDE or GNOME) can use. Because of this, the X11 software must be able to interact with all of those hardware devices.

There are two common X11 implementations used in Linux:

  • XFree86: The original X11 software for Linux; it is extremely hard to configure and get working with different types of video hardware. For information on using and configuring XFree86, check out the http://xfree86.org website.
  • X11.org: A more user-friendly X11 software package for Linux; uses simple text-based configuration files.

Both the XFree86 and X.org packages store their configuration files in the /etc/X11 directory. The X.org system attempts to automatically detect the video card, monitor, keyboard, and mouse installed on the system at each boot time and dynamically changes the configuration files accordingly. If you make any changes to the video card or monitor, X11 will automatically detect the new equipment and alter the configuration accordingly, making it a breeze to swap out new video equipment.

images Both the XFree86 and X11 packages include lots of different drivers that support common video cards and monitors. For both packages, however, if they don’t recognize the specific video card or monitor on your system, they default to using generic drivers that may not produce the best quality video experience. If your Linux system uses a specialty graphics card or monitor, it’s best to obtain the Linux drivers for them and follow the XFree86 or X11 documentation to manually install the updated drivers.

Using Printers

Just as with the video environment in Linux, printing in Linux can be somewhat complex. With different types of printers available, trying to install the correct printer drivers as well as using the correct printer protocol to communicate with them can be a nightmare.

Fortunately, the Common Unix Printing System (CUPS) solves many of those problems for us. CUPS provides a common interface for working with any type of printer on your Linux system. It accepts print jobs using the PostScript document format and sends them to printers using a print queue system.

The print queue is a holding area for files sent to be printed. The print queue is normally configured to support not only a specific printer but also a specific printing format, such as landscape or portrait mode, single-sided or double-sided printing, or even color or black-and-white printing. There can be multiple print queues assigned to a single printer or multiple printers that can accept jobs assigned to a single print queue.

The CUPS software uses the Ghostscript program to convert the PostScript document into a format understood by the different printers. The Ghostscript program requires different drivers for the different printer types to know how to convert the document to make it printable on a certain type of printer. This is done using configuration files and drivers. Fortunately, CUPS installs many different drivers for common printers on the market and automatically sets the configuration requirements to use them. The configuration files are stored in the /etc/cups directory.

To define a new printer on your Linux system you can use the CUPS web interface. Open your browser and navigate to the URL http://localhost:631/. Figure 23.2 shows the web interface used by CUPS.

The figure shows a snapshot of the web interface used by CUPS.

Figure 23.2 The CUPS main web page

The CUPS web interface allows you to define new printers, modify existing printers, and check on the status of print jobs sent to each printer. Not only does CUPS recognize directly connected printers, but you can also configure network printers using several standard network printing protocols, such as the Internet Printing Protocol (IPP) or the Microsoft Server Message Block (SMB) protocol.

Aside from the CUPS web interface, there are a few command-line tools you can use for interacting with the print queues:

  • lpc: Start, stop, or pause the print queue.
  • lpq: Display the print queue status, along with any print jobs waiting in the queue.
  • lpr: Submit a new print job to a print queue.
  • lprm: Remove a specific print job from the print queue.

If you’re working from the command line, you can check the status of any print queue as well as submit print jobs. For each of the commands, to specify the printer use the -P command-line option along with the printer name, as shown in Listing 23.10.

Listing 23.10: Printing from the command-line in Linux

$ lpq -P EPSON_ET_3750_Series
EPSON_ET_3750_Series is ready
no entries
$ lpr -P EPSON_ET_3750_Series test.txt
$ lpq -P EPSON_ET_3750_Series
EPSON_ET_3750_Series is ready and printing
Rank    Owner   Job     File(s)                         Total Size
active  rich    1       test.txt                        1024 bytes
$

The first line in Listing 23.10 uses the lpq command to check the status of the print queue, which shows the printer is ready to accept new jobs and doesn’t currently have any jobs in the print queue. The lpr command submits a new print job to print a file. After the new print job is submitted, the lpq command shows the printer is currently printing and shows the print job that’s being printed.

images Red Hat–based systems use the Automatic Bug Reporting Tool (abrt) to create a report if any kernel-level hardware errors are detected. Red Hat Linux customers can send the report into tech support to help troubleshoot the hardware issue.

Using Hot Pluggable Devices

Computer hardware is generally categorized into two types:

  • Cold pluggable devices
  • Hot pluggable devices

Cold pluggable devices are hardware that can be connected to the system only when the system is completely powered down. These usually include things commonly found inside the computer case, such as memory, PCI cards, and hard drives. You can’t remove any of these things while the system is running.

Conversely, you can usually add and remove hot pluggable devices at any time. These are often external components, such as network connections, monitors, and USB devices. The trick with hot pluggable devices is that somehow the Linux kernel needs to know when the device is connected and automatically load the correct device driver module to support the device.

Linux provides an entire subsystem that interacts with hot pluggable devices, making them accessible to users. This subsystem is described in the following sections.

Detecting Dynamic Devices

The udev device manager is a program that is automatically started at boot time by the init process (usually at run level 5 via the /etc/rc5.d/udev script) or the Systemd systems and runs in the background at all times. It listens to kernel notifications about hardware devices. As new hardware devices are plugged into the running system, or existing hardware devices removed, the kernel sends out notification event messages.

The udev program listens to these notification messages and compares the messages against rules defined in a set of configuration files, normally stored under the /etc/udev/rules.d directory. If a device matches a defined rule, udev acts on the event notification as defined by the rule.

Each Linux distribution defines a standard set of rules for udev to follow. Rules define actions such as mounting USB memory sticks under the /media folder when they’re installed or disabling network access when a USB network card is removed. You can modify the rules defined, but it’s usually not necessary.

Working with Dynamic Devices

While the udev program runs in the background on your Linux system, you can still interact with it using the udevadm command-line tool. The udevadm command allows you to send commands to the udev program. The format of the udevadm command is as follows:

udevadm command [options]

Table 23.3 shows the commands available to send to the udevadm program.

Table 23.3 The udevadm commands

Command Description
control Modifies the internal state of udev
info Queries the udev database for device information
monitor Listens for kernel events and display them
settle Watches the udev event queue
test Simulates a udev event
test-builtin Runs a built-in device command for debugging
trigger Requests device events from the kernel

The control command allows you to change the currently running udev program. For example, by adding the -R option, you can force udev to reload the rules defined in the /etc/udev/rules.d directory.

Exercise 23.1 Adding a USB storage device to the Linux system

This exercise walks through how to view the kernel messages and device entries that occur when you connect a USB storage device to the Linux system.

  1. Log into your Linux graphical desktop and open a command prompt window.
  2. At the command prompt, enter the lsusb command to view any USB controllers and devices connected to your system.
  3. Plug a USB storage device, such as a memory stick, into a USB port; then wait a minute or so for the kernel to detect it.
  4. Type the command dmesg, and observe the kernel ring buffer entries entered when the kernel detected the new USB device. Note the device name assigned to the new device (such as sdb1).
  5. Type the lsusb command again and see if the new device appears in the output.
  6. Type the command lsblk to view the device and partition in the block table as well as to note where the partition is mounted in the virtual directory.
  7. Type the command ls /dev/sd* to view the SCSI devices on the system. You should see the USB device name that appeared in the dmesg output appear as a device file in the /dev folder.
  8. Use the graphical desktop interface to safely eject the USB storage device.
  9. Type the command dmesg to view the kernel entries when the device was removed.
  10. Type the command ls /dev/sd* to see if the device file has been removed.

Summary

There are lots of ways to connect hardware devices to Linux systems. Both PCI and USB interfaces provide a standard way for connecting devices to the main motherboard so they can communicate. The newer GPIO interface provides a way to interact with smaller devices that use a single line for inputs and outputs that control sensors, switches, relays, and motors.

Besides the physical interfaces, Linux also uses files to communicate with devices. When you connect a device to the system, Linux automatically creates a file in the /dev directory that’s used for applications to send data to and receive data from the devices. The kernel uses the /proc directory to create virtual files that contain information about the devices and system status. The /sys directory is also used by the kernel to create files useful for troubleshooting device issues.

Linux provides a handful of command-line tools that are useful when trying to troubleshoot device problems. The lsdev command allows you to view the status and settings for all devices on the system. The lsblk command provides information about block devices, such as hard drives and network cards, that are connected. The dmesg command lets you peek at the kernel ring buffer to view kernel event messages as it detects and works with devices. The lspci and lsusb commands allow you to view the PCI and USB devices that are connected to the Linux system.

Linux also provides software to help with monitors and printers. The X11 protocol, used by the XFree86 and X.org software packages, detects and interfaces with the video card, monitor, keyboard, and mouse connected to the system, providing a standard interface for applications to use. The CUPS software provides a standard method for applications to send documents to both local and network printers.

Finally, the chapter discussed how Linux handles hot pluggable devices. The udev application monitors the kernel events for information regarding new hardware detected on USB ports. If a new device is detected, udev handles the device as defined in the rules set. The udevadm application allows you to control how udev works on your system.

Exam Essentials

Describe how Linux systems communicate with devices. Linux systems create files in the /dev folder that applications use to send data to devices and retrieve data from devices. Device files can be either character files, which send and receive data one character at a time, or block files, which send and receive data in blocks.

Explain how you would find the hardware settings for a PCI board plugged into the Linux system. The lspci command displays the PCI devices currently connected to the system. You can use that information with the lsdev command, which displays the interrupts, I/O ports, and DMA channels used by each device. You can also find that information in the /proc/interrupts, /proc/ioports, and /proc/dma files.

Explain how Linux can detect hot pluggable devices. The udev application runs in the background, monitoring the kernel ring buffer for new devices. When a new device is added, the udev application detects it from the kernel ring buffer messages and follows instructions defined in rules contained in the /etc/udev/rules.d directory.

Review Questions

  1. What type of hardware interface uses interrupts, I/O ports, and DMA channels to communicate with the PC motherboard?

    1. USB
    2. GPIO
    3. PCI
    4. Monitors
    5. Printers
  2. What file does the Linux system use to track ports used to communicate with PCI boards?

    1. /proc/ioports
    2. /proc/interrupts
    3. /sys
    4. /dev
    5. /proc/dma
  3. Where does Linux create files to send data to and receive data from directly with devices?

    1. /sys
    2. /proc
    3. /etc
    4. /dev
    5. /dev/mapper
  4. Katie Jane created a new LVM volume on her Linux system. Where in the virtual directory should she look to find the virtual file related to the new volume?

    1. /dev
    2. /dev/mapper
    3. /proc
    4. /sys
    5. /etc
  5. Joel installed a new PCI card in his Linux system but is now getting a conflict with another device on the system. What command can he use to display the interrupts, I/O ports, and DMA channels in use by all of the existing devices?

    1. lsdev
    2. lsblk
    3. lspci
    4. lsusb
    5. dmesg
  6. Which Linux command displays the contents of the kernel ring buffer?

    1. lsdev
    2. lsblk
    3. lspci
    4. lsusb
    5. dmesg
  7. Which software packages implement the X Windows graphical system in Linux? (Choose two.)

    1. X.org
    2. CUPS
    3. XFree86
    4. X11
    5. udev
  8. Sophia needs to connect her Linux system to a new network printer on the office network. What software package does she need to ensure is installed so she can configure the new network printer?

    1. X.org
    2. CUPS
    3. XFree86
    4. X11
    5. udev
  9. Which program runs in the background monitoring the kernel ring buffer messages for new devices?

    1. X.org
    2. CUPS
    3. XFree86
    4. X11
    5. udev
  10. Which program allows you to reload the defined rules for detecting and installing new devices?

    1. udevadm
    2. udev
    3. lsusb
    4. lspci
    5. lsdev
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset