Saturday, August 14, 2010

Linux shell commands for hardware information gathering

Linux provides a wealth of tools for doing various tasks ranging from text editing to process management to information gathering tools. Here's a few tools that's useful for information gathering tasks. Some are not strictly information gathering tools but are usable for tweaking or managing said hardware.

fdisk - Anyone familiar with DOS will be almost at home with this tool. I say "almost" because there's just no comparison. fdisk on Linux is simply far and above the DOS equivalent. It provides tools to tweak every aspect of your hard disk partition table to your heart's content.

It's too much for this post though. Here we're interested in it's information gathering properties. fdisk comes in handy when you want to find out the partition layout currently on your hard disk. You'll need to run it with root privileges.

~$ sudo fdisk -l

On my computer I get the following:

Disk /dev/sdb: 160.0 GB, 160000000000 bytes
255 heads, 63 sectors/track, 19452 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000081

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1       19452   156248158+  83  Linux

Disk /dev/sda: 160.0 GB, 160000000000 bytes
255 heads, 63 sectors/track, 19452 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x41ab2316

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1       18657   149862321   83  Linux
/dev/sda2           18658       19452     6385837+   5  Extended
/dev/sda5           18658       19452     6385806   82  Linux swap / Solaris

As you can see, I have 2 160GB hard disks. My first hard disk (/dev/sda) has 2 partitions: 1 for Ubuntu, the other is a swap partition contained in an extended partition. Partitions in extended partitions start counting from 5 hence sda5.

hdparm - Using hdparm you can basically tweak your hard disk performance characteristics. I wouldn't recommend trying unless you know what you're doing, or are willing to take a risk with your data. The defaults are usually sensible enough for most users.

Still, it's good to know a little about this tool. You can benchmark your hard disk's raw throughput capabilities, and even view supported features, model numbers, serial numbers. Of particular interest would be the cache/buffer size. Knowing the amount of cache your hard disk has comes in handy when you want to make use of ddrescue for hard disk cloning purposes. Read more.

You can get info on your hard disk with,

~$ sudo hdparm -I /dev/sda

Replace /dev/sda with the appropriate device (/dev/sdb, /dev/sdc, etc...). Here's a snippet from my hard disk:

Configuration:
    Logical        max    current
    cylinders    16383    16383
    heads        16    16
    sectors/track    63    63
    --
    CHS current addressable sectors:   16514064
    LBA    user addressable sectors:  268435455
    LBA48  user addressable sectors:  312500000
    Logical/Physical Sector size:           512 bytes
    device size with M = 1024*1024:      152587 MBytes
    device size with M = 1000*1000:      160000 MBytes (160 GB)
    cache/buffer size  = 8192 KBytes (type=DualPortCache)

My hard disk has a 8MB cache. Not terribly much considering some hard disks now come with 64MB but sufficient for a 160GB hard disk.

You can obtain raw performance number with,

~$ sudo hdparm -tT /dev/sda

Here's my output:

/dev/sda:
 Timing cached reads:   3108 MB in  2.00 seconds = 1554.01 MB/sec
 Timing buffered disk reads:  154 MB in  3.02 seconds =  51.02 MB/sec

You might want to run that a few times and average the results to get a more accurate reading.

lshw - This basically lets you view just about every aspect of your computer hardware. This tool needs to run with root privileges or it'll report partial info only. You can use it to extract information for specific classes (cpu, memory, storage, disk, etc...).

For example, running the following,

~$ sudo lshw -C cpu

You'll get something similar to this:

*-cpu                  
       description: CPU
       product: AMD Athlon(tm) II X4 620 Processor
       vendor: Advanced Micro Devices [AMD]
       physical id: 4
       bus info: cpu@0
       version: AMD Athlon(tm) II X4 620 Processor
       serial: To Be Filled By O.E.M.
       slot: CPU 1
       size: 2600MHz
       capacity: 2600MHz
       width: 64 bits
       clock: 200MHz
       capabilities: fpu fpu_exception wp vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp x86-64 3dnowext 3dnow constant_tsc rep_good nonstop_tsc extd_apicid pni monitor cx16 popcnt lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt

CPU model, vendor, clock speed, supported instruction sets can all be viewed from here.

With these command line tools you'll be able to extract information on your computer without resorting to opening up the casing. Of course, there's nothing stopping from doing so but it's handy to know.

Related posts:

Command line hard disk cloning with gddrescue
Speeding up your disk cloning process