It had never occurred to me that my Linux drives weren’t running at their optimal efficiencies—until I ran into the hdparm utility, which set the record straight. If you are searching for ways to eek out as much efficiency as you can from your Linux servers/desktops, then hdparm is exactly what you need.
In this Daily Feature, I will show you a couple of tips for how to use hdparm to gain performance from your old or new hard drives running Linux. Using these tips, you can expand your use of hdparm to cover a number of optimizations.
What does hdparm do?
The hdparm utility provides a command-line interface for various hard disk input/output controls, which are supported by the stock Linux ATA/IDE device driver subsystem. From this utility, you can control DMA support, read-ahead timing, bus state, advanced power management, 32-bit support, and so on.
Be forewarned that this is a very powerful tool. Using it incorrectly can easily trash your disk. Because of this, make sure you read this article carefully, and also check the man page that accompanies hdparm.
Get and install hdparm
The source for hdparm can be found on Ibiblio. The installation is very easy. First, download the tar file into /usr/local/src. As the root user, cd into the /usr/local/src directory and unpack the file with the command:
tar xvzf hdparm-RELEASE.tgz
where RELEASE is the version downloaded. After you’ve run this command, a new directory, hdparm-RELEASE, will be created, where RELEASE is again the downloaded version. Change into this newly created directory and run the following commands:
The above commands will compile and then install the hdparm utility on the system (installing the binary into /sbin).
The first step in learning any Linux command is to learn the proper syntax. With hdparm the syntax is simple:
hdparm [flags] [device]
The above syntax is standard for almost all Linux commands that interact with hardware. The first section, hdparm, is the basic command executable. The next section, [flags], is where any of the hdparm options are added (for a complete listing of flags and options check out the hdparm man page). The final section is where you define the location of your hardware. Remember that Linux hardware is defined in the /dev directory and will look something like /dev/hda, which would be the first partition of a standard IDE hard drive.
First step: Gather your drive information
Fortunately, with the help of hdparm, you no longer have to scramble through your PC’s documentation to get the information you need about your hard drive. The command hdparm -Idc /dev/hda (where hda is the actual location of your drive) will offer results similar to that in Listing A.
As you can see, there's a ton of information to be gleaned from hdparm. At this point, it is possible to start making choices that will bring about the best possible optimization for nearly any hard drive.
Optimize the drive
By examining the output of Listing A, some obvious optimizations can be made. Below is a description of a couple of the best examples for optimizing the test drive.
This option allows the enabling of direct memory access (DMA). DMA allows data on the drive to be transferred from main memory (the drive) to a device (storage, output, printer, etc.) without passing through the CPU. This, obviously, makes data travel quite a bit faster.
Unfortunately, the test drive is showing that it is not currently using DMA. It’s obvious from its size that the drive in question is a newer drive and can therefore utilize DMA. Before I enable DMA on the drive, I am going to use hdparm to test the device read timings. Running the command hdparm -t /dev/hda (with DMA unset) returns this output:
Timing buffered disk reads: 64 MB in 20.96 seconds = 3.03 MB/sec
This is not nearly optimal. In fact, this is quite bad. By enabling DMA with the command hdparm -d 1 /dev/hda, I am sure the output will improve. And it does—considerably. After enabling DMA, the output of hdparm -t /dev/hda looks like this:
Timing buffered disk reads: 64 MB in 6.39 seconds = 10.02 MB/sec
This is a substantial improvement, featuring disk reads that are now over three times faster than they were.
Enable 32-bit support
For some odd reason, the test drive has defaulted to a 16-bit setting. This setting is shown in Listing A as I/O support = 0 (default 16-bit). This will also be a primary factor in slow disk reads. Fortunately, with the help of hdparm, this can be changed.
By using the -c switch, the drive can be set to the more appropriate 32-bit mode. The command to set the first IDE drive to 32 bit would be hdparm -c 1 /dev/hda. Now running the command hdparm -t /dev/hda returns this much more impressive reading:
Timing buffered disk reads: 64 MB in 1.77 seconds = 36.16 MB/sec
An improvement of disk reads that are over 10 times faster than the original reading is certainly worth investing the short amount of time it takes to learn hdparm!
Make the settings permanent
Of course, it is impractical to have to rerun these commands each and every time the machine restarts. In order to avoid this, it is possible to insert a single command such as hdparm -d 1 -c 1 /dev/hda at the end of the /etc/rc.d/rc.local file so that the command will be set at boot.
RTFMP: Read That Fine Man Page
There are a number of other optimizations and configurations to be had with hdparm. Although few will give you the performance increase as the two described above, there are some that will certainly make a difference.
In order to learn all the gory details of hdparm, run the command man hdparm, and a listing of all the switches (and what they do) will appear.
Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website jackwallen.com.