O'Reilly Book Excerpts: PC Hacks
PC Hacks for Linux
by Jim Aspinwall
Editor's note: PC Hacks author Jim Aspinwall handpicks two Linux-specific hacks to share from his new book. Whether Linux just won't boot or it isn't as zippy as you'd expect, help is merely a hack away. Jim will teach you how to recover your system's boot-ability as well as how to test and optimize Linux's settings for the best hard drive performance.
Hack #50: Fix GRUB or LILO Boot Problems
Use the SystemRescueCD to repair a blown GRUB or LILO boot.
Linux has advanced disk management and repair tools too. Of note is the SystemRescueCD
from http://www.sysresccd.org. SystemRescueCD is offered as an ISO file that
can be written to a CD-R to make a bootable rescue CD. The packages includes
several essential tools for fixing Linux boot-up problems, including:
- GNU Parted
- Used for editing disk partitions
under Linux.
- QtParted
- A Linux-based clone of PartitionMagic.
- Partimage
- A Linux-based
clone of Ghost/Drive Image to create images of disks and partitions.
- Sfdisk
- A tool that lets you back up and restore a partition table.
It also includes a variety of filesystem tools that allow you to format, resize,
and debug an existing partition of your hard disk supporting e2fs, reiserfs,
xfs, jfs, ntfs, and DOS partition types.
With these tools on hand, you may never have to reinstall a Linux operating
system again.
Hack #67: Linux’s Drive Performance Booster
HDPARM is to Linux what the Intel Bus Master and Via IDETool driverperformance
enhancements are to Microsoft Windows. Don’t forget to include this gem
in your next Linux system build.
To be on the safe side, your new Linux installation starts up with the least
common denominator of disk drive performance capabilities—typically DMA-33—robbing
you of 50–150% of your potential performance. Once Linux is installed, you
are free and encouraged to start tweaking the configuration of your disk drive
and its interface to squeeze the most of them.
WARNING: Setting HDPARM parameters too aggressively—that
is, in excess of the disk controller or drive capabilities—can lead to
data loss.
It is best to test HDPARM settings on a fresh installation of the operating system
before committing any applications or programs to the drive and prepare to back
down on the settings and reinstall the OSif the drive is unstable or the HDPARM
tests show erratic results or fail.
The tool needed, HDPARM, is included with
the operating system (or available from your package manager). It can be adjusted
manually and then put into a startup script to make your chosen settings effective
every time the system starts up.
HDPARM is a command-line utility that provides
powerful control over your hard drive parameters (HD PARaMeters). It can also
tell you a lot about your disk drive. Everything you do with HDPARM, until you
make a script for it, will be done at the command line.
TIP: You must be logged in as root to run HDPARM. You can also
use the sudo command to run the command as root if you have sufficient privileges.
Assume /dev/hda is the designation for your hard drive. (This is the default
for the first IDE drive; a SATA drive may appear as /dev/hde if your motherboard
also has IDE interfaces.) Run the following command:
hdparm -i /dev/hda
You should get some info like the following:
/dev/hda:
Model=QUANTUM FIREBALLlct, FwRev=APL.1234, SerialNo=1234567
Config={ HardSect NotMFM HdSw>15uSec Fixed DTR>10Mbs }
RawCHS=16383/16/63, TrkSize=32256, SectSize=21298, ECCbytes=4
BuffType=DualPortCache, BuffSize=418kB, MaxMultSect=8, MultSect=off
CurCHS=16383/16/63, CurSects=-66060037, LBA=yes, LBAsects=39876478
IORDY=on/off, tPIO={min:120,w/IORDY:120}, tDMA={min:120,rec:120}
PIO modes: pio0 pio1 pio2 pio3 pio4
DMA modes: mdma0 mdma1 mdma2 udma0 udma1 udma2 udma3 udma4 *udma5
AdvancedPM=no
Drive Supports : ATA/ATAPI-5 T13 1321D revision 1 : ATA-1 ATA-2
ATA-3 ATA-4 ATA-5
This tremendous amount of data provided tells you:
- MaxMultSect
- The maximum number of sectors your hard disk can read at a time.
- MultSect
- The current number of sectors being read at a time.
- PIO modes and DMA modes
- The modes supported by your hard drive. The one marked with an
asterisk (*) is the one currently set.
- AdvancedPM
- Indicates whether or not your hard drive supports Advanced Power
Management.
Another command:
hdparm /dev/hda
reveals the following information:
/dev/hda:
multcount = 0 (on)
I/O support = 0 (16-bit)
unmaskirq = 0 (off)
using_dma = 0 (off)
keepsettings = 0 (off)
nowerr = 0 (off)
readonly = 0 (off)
readahead = 8 (on)
geometry = 2482/255/63, sectors = 39876480, start = 0
The items of interest are:
- multcount
- The number of sectors being read at a time.
- I/O support
- The operating mode of your hard disk (16/32/32sync).
- using_dma
- Whether or not the drive is using the DMA feature. This may be on by
default if your version of Linux properly detects and supports your
chipset and drive’s DMA capabilities.
- keepsettings
- Whether the settings are kept after the drive resets (usually caused by
errors).
- readonly
- Whether the drive is read-only. Normally set to 1 only for CD-ROMs.
- readahead
- How many sectors ahead will be read when you access the hard drive.
The HDPARM program provides two performance-testing features that are
crucial to letting you know whether or not you’re making improvements
as you tweak along. The command:
hdparm -Tt /dev/hda1
will show results such as the following before enhancing the performance:
/dev/hda1:
Timing buffer-cache reads: 128 MB in 5.97 seconds = 21.43 MB/sec
Timing buffered disk reads: 64 MB in 17.97 seconds = 3.56 MB/sec
and then results like these after enhancing the performance:
Timing buffer-cache reads: 128 MB in 0.91 seconds =140.66 MB/sec
Timing buffered disk reads: 64 MB in 3.78 seconds = 16.93 MB/sec
The goal of this hack is to see the time in seconds decrease and the MB/sec
to increase. You can do that by using a variety of parameters, invoked one at
a time, then rerunning the performance tests to see if things are improving
or not.
Mistakes during the setup process may damage your filesystem and all of its
data, so it’s best to do this after a fresh install of Linux or right
after you’ve done a full backup.
Begin by setting the operating mode of the interface between the system and
the disk drive using one of the following parameters:
hdparm -c0 /dev/hda #sets operating mode to 16-bits
hdparm -c1 /dev/hda #sets operating mode to 32-bits
hdparm -c3 /dev/hda #sets operating mode to 32-bits synchronized
Mode 1 (-c1) is used most often for best performance. Mode 3 (-c3) only is
needed for some chipsets.
Next set the data transfer parameters, which you can determine from the
output of the “-I” command shown earlier (in that case 8 is the
maximum supported):
hdparm -m8 /dev/hda
Next try activating DMA mode for your system interface:
hdparm -d1
Then set the drive mode (a value of X32 is most common; UDMA-5 is X69):
hdparm -X32 /dev/hda
or:
hdparm -X69 /dev/hda
Finally, try setting the read-ahead value, which is typically set to the same
value as multcount from earlier, or 8:
hdparm -a8 /dev/hda
If any or all of these settings make incremental improvements in performance,
remember them and create a script that sets them all sequentially or
includes them all in one line. I prefer sequential lines to ensure the drive
accepts each command separately and I do not lose a setting if another fails
to take. From all of this, you might typically be using the following parameters:
hdparm -c1
hdparm -m8 /dev/hda
hdparm -d1
hdparm -X34 /dev/hda
hdparm -a8 /dev/hda
Another single-command example that may work best for your system is:
hdparm -X66 -d1 -u1 -m16 -c3 /dev/hda
Save either to a file and make the file a script to place in the directory for
the runlevel at which you normally use Linux. For example:
- Using a text editor, create then save the script as /etc/init.d/hdparm.local.
- Configure it to start in runlevel 5 with the following command:
ln -s /etc/init.d/hdparm.local /etc/rc5.d/S20hdparm.local
- The rc5.d part of the parameter string indicates runlevel 5, which is the
normal operating mode for most Linux systems. To find out your
default runlevel, examine /etc/inittab for the inittdefault entry, as in:
id:5:initdefault:
The next step is to keep an eye on dmesg and/or /var/log/syslog. In some
cases, an error will cause the settings to be reset. So that’s where the
-k (keep) flag comes in. If you’re 100% positive that these settings won’t
corrupt your data, you can add -k to the script.
Jim Aspinwall
has been the Windows Helpdesk columnist and feature editor for CNET.com and the author of three books on PC maintenance.
View catalog information for Web Database Applications with PHP & MySQL
Return to Linux DevCenter.