February 2, 2005

Detecting hardware from outside the box

Author: Preston St. Pierre

Linux comes with several good utilities for getting detailed information on what's inside the box. Here are three recipes for getting information from lspci, dmesg, and /proc.

This article is excerpted from the recently published book "The Linux Cookbook."

Detecting hardware with lspci

You're looking at new systems, or installing Linux on a box that used to run a different OS, and you're wondering if all the components -- video, modem, Ethernet, sound -- will work on Linux. The vendors can't, or won't, tell you if their products will work on Linux. You need to know what the chipsets are, to find out if there are Linux drivers.

Alternatively, you want to know what components are installed inside a computer, and you don't feel like hauling the thing out, popping the case open, and tearing it apart.

Solution: Use lspci:

# /sbin/lscpi# /sbin/lspci -v# /sbin/lspci -vv

To show a summary of all devices connected to the PCI bus, use:

$ /sbin/lspci
00:00.0 Host bridge: VIA Technologies, Inc. VT8363/8365 [KT133/KM133] (rev 02)
00:01.0 PCI bridge: VIA Technologies, Inc. VT8363/8365 [KT133/KM133 AGP]
00:06.0 Ethernet controller: Linksys Network Everywhere Fast Ethernet 10/100 model NC100 (rev 11)

Use the -v or -vv flags to display more information:

# /sbin/lspci -v
0000:01:00.0 VGA compatible controller: 3Dfx Interactive, Inc. Voodoo 3 (rev 01) (prog-if 00 [VGA])
Subsystem: 3Dfx Interactive, Inc.: Unknown device 1252
Flags: 66MHz, fast devsel, IRQ 10
Memory at d4000000 (32-bit, non-prefetchable) [size=32M]
Memory at d8000000 (32-bit, prefetchable) [size=32M]
I/O ports at c000 [size=256]
Expansion ROM at <unassigned> [disabled] [size=64K]
Capabilities: [54] AGP version 1.0
Capabilities: [60] Power Management version 1

If you're looking for drivers, you can now take this output (e.g., VT8363/8365 or 3Dfx
Interactive, Inc. Voodoo 3 (rev 01)
) to run a Google search.

lspci reads some information from the PCI bus, then displays additional information from its own database of hardware IDs -- vendors, devices, classes and subclasses -- at /usr/share/misc/pci.ids. There is even a command to update this file:

# update-pciids

The lspci maintainers welcome submissions of new data; please read /usr/share/misc/pci.ids for how to make submissions.

If there is a device attached to the system that the lspci simply does not recognize, such as a very old, odd ISA device, you'll have to open the case to see what it is. Or try running dmesg (Recipe 5.3).

Using dmesg to collect hardware information

PCI is fine, but it's yesterday's news; you need an inventory of all the devices on the system, not just PCI devices. You're interested in USB devices, SCSI devices, memory configuration, even the CPU.

Solution: Use dmesg. dmesg is a record of everything detected by the kernel.

To view all dmesg output, use:

$ dmesg | less

You can also filter the output of dmesg to find specific devices. For example, to list all USB devices, use:

$ dmesg | grep -i usb

To list ISA devices, use:

$ dmesg | grep -i isa
isapnp: Scanning for PnP cards...
isapnp: SB audio device quirk - increasing port range
isapnp: Card 'SupraExpress 56i Voice'

To see how much physical memory is on the system, use:

$ dmesg | grep -i memory
Memory: 256492k/262080k available (1467k kernel code, 5204k reserved, 516k data, 96k init, 0k highmem)

This shows IDE devices using the SCSI emulation subsystem, which is used on 2.4 and older kernels:

$ dmesg | grep -i scsi
Kernel command line: root=/dev/hda6 ro hdb=scsi hdc=scsi
ide_setup: hdb=scsi
ide_setup: hdc=scsi
SCSI subsystem driver Revision: 1.00
hdb: attached ide-scsi driver.
hdc: attached ide-scsi driver.
scsi0 : SCSI host adapter emulation for IDE ATAPI devices

Here are what "real," not emulated, SCSI devices look like:

$ dmesg | grep -i scsi
SCSI subsystem driver Revision: 1.00
scsi0 : Adaptec AIC7XXX EISA/VLB/PCI SCSI HBA DRIVER, Rev 6.2.8
<Adaptec aic7892 Ultra160 SCSI adapter>
aic7892: Ultra160 Wide Channel A, SCSI Id=7, 32/253 SCBs
...Vendor: IBM-PSG Model: DPSS-336950M M Rev: S9HA
Attached scsi disk sda at scsi0, channel 0, id 0, lun 0
(scsi0:A:0): 160.000MB/s transfers (80.000MHz DT, offset 63, 16bit)
SCSI device sda: 71096640 512-byte hdwr sectors (36401 MB)
Partition check:
sda: sda1 sda2 sda3 sda4 < sda5 sda6 >

Shown here is information about a USB camera that is connected to the system, including its location in the filesystem. Typically, USB output runs to a dozen lines or more:

$ dmesg | grep -i usb
usb.c: registered new driver ibmcam
ibmcam.c: IBM PC Camera USB camera found (model 2, rev. 0x030a)
usbvideo.c: ibmcam on /dev/video0: canvas=352x240 videosize=352x240

To show serial ports, use:

$ dmesg | grep -i tty
ttyS00 at 0x03f8 (irq = 4) is a 16550A

To show CPU or CPUs, use:

$ dmesg | grep -i cpu
Initializing CPU#0
CPU: L1 I Cache: 64K (64 bytes/line), D cache 64K (64 bytes/line)
CPU: L2 Cache: 64K (64 bytes/line)
Intel machine check reporting enabled on CPU#0.
CPU: After generic, caps: 0183f9ff c1c7f9ff 00000000 00000000
CPU: Common caps: 0183f9ff c1c7f9ff 00000000 00000000
CPU: AMD Duron(tm) Processor stepping 01

Note that these searches only return lines containing your search string. There is often more information adjacent to these lines, which you'll find by eyeballing the whole file:

Initializing CPU#0
Detected 801.446 MHz processor.

dmesg always provides up-to-date information, even if you're changing hardware frequently (for example, plugging in and detaching hotplug USB devices).

Getting live hardware snapshots with /proc

You want to monitor a running system in real time, and view things like physical memory and CPU information, or identify drives.

Solution: Read the /proc virtual filesystem. Use only cat to read /proc, or utilities designed expressly for it, such as sysctl, lspci, ps, and top. The syntax is the same as for reading any file:

$ cat /proc/filename

You can explore /proc just like any filesystem and easily find the information you want. Look to the named folders for hardware information:

$ ls /proc
bus cmdline cpuinfo devices dma driver filesystems ide kcore kmsg ksyms loadavg
meminfo misc modules mounts mtrr partitions pci scsi swaps sys tty

For example, to show CPU information, use:

$ cat /proc/cpuinfo
processor : 0
vendor_id : AuthenticAMD
cpu family : 6
model : 3
model name : AMD Duron(tm) Processor
stepping : 1
cpu MHz : 801.442

To show physical memory and swap usage, use:

$ cat /proc/meminfo
total: used: free: shared: buffers: cached:
Mem: 262746112 237740032 25006080 0 11575296 150138880
Swap: 534601728 81661952 452939776
MemTotal: 256588 kB
MemFree: 24420 kB

To tell all about an IDE hard drive, use:

$ cat /proc/ide/via
-------VIA BusMastering IDE Configuration---------
Driver Version: 3.37
South Bridge: VIA vt82c686a
Revision: ISA 0x22 IDE 0x10
Highest DMA rate: UDMA66
BM-DMA base: 0xd400
PCI clock: 33.3MHz

To see disk geometry, both real and logical, use:

$ cat /proc/ide/ide0/hda/geometry
physical 39870/16/63
logical 2501/255/63

To identify a drive, use:

$ cat /proc/ide/ide0/hda/model

To show driver versions for all IDE drivers, use:

$ cat /proc/ide/drivers
de-scsi version 0.93
ide-cdrom version 4.59-ac1
ide-floppy version 0.99.newide
ide-disk version 1.17
ide-default version 0.9.newide

To show capabilities of CD drives, use:

$ cat /proc/sys/dev/cdrom/info
CD-ROM information, Id: cdrom.c 3.12 2000/10/18
drive name: sr1 sr0
drive speed: 40 32
Can read multisession: 1 1
Can read MCN: 1 1
Reports media changed: 1 1
Can play audio: 1 1
Can write CD-R: 1 0
Can write CD-RW: 1 0
Can read DVD: 0 1
Can write DVD-R: 0 0
Can write DVD-RAM: 0 0

To show SCSI devices, using the following command. Note that it does not differentiate between devices attached to the SCSI bus and IDE devices using the SCSI-emulation subsystem. These are IDE CD drives:

$ cat /proc/scsi/scsi
Attached devices:
Host: scsi0 Channel: 00 Id: 00 Lun: 00
Vendor: TOSHIBA Model: DVD-ROM SD-M1202 Rev: 1020
Type: CD-ROM ANSI SCSI revision: 02
Host: scsi0 Channel: 00 Id: 01 Lun: 00
Vendor: LITE-ON Model: LTR-24102B Rev: 5S54
Type: CD-ROM ANSI SCSI revision: 02

For AMD Users

Since AMD went to "performance ratings," instead of plain ole gigahertz, CPU ratings can be confusing. Your shiny new Athlon 3200 won't appear in /proc/cpuinfo as "cpu MHz 3200" -- instead, it will be something like 2800. You're not being ripped off; that's a result of how AMD chooses to rate the performance of their processors. In a nutshell, they claim that clock speed alone is not an accurate measure of performance, so they devised a different scale that more accurately reflects the CPU's true abilities. Visit http://www.amd.com for details.

On the other hand if your Pentium 3200 shows up in /proc/cpuinfo as a number other than 3200, there is a problem, because Intel uses the literal clock speeds.

This following command is just plain fun and has absolutely no practical value. It requires a functioning sound system. Warning: it's noisy -- this is the sound of your CPU in action. Ctrl-C stops it:

# cat /proc/kcore > /dev/dsp

Disk geometry, as expressed by /proc or any other utility, is largely a fiction. Modern drives are far more complex than the old "heads sectors cylinders" model.

As mentioned earlier, to read /proc use only cat or utilities designed expressly for it, such as sysctl, lspci, ps, and top. Pagers like less and more give a different picture, because they re-read /proc with each page. And you don't want to use a text editor, or any utility with write powers, because you can mess up your system in a heartbeat.

Click Here!