Saturday, October 21, 2006

Take care of your hard disks with hdparm

The main problem that we all face with our hard disk is bad sector and subsequent data loss for that. Everybody know what is the blow of such data loss, specially if you are using your PC as a web server or database server.

There are various reasons for which your hard disk can get bad sectors. The most prominent of them is sudden power failure. As the disk constantly keeps spinning at 5400-10000 rpm depending on hdd model, if the disk is being accessed (data being written and read) when the power failure occurs, the head may write down abnormal data and bad sector can occur from that.

Now a days, most OS by default takes advantage of a feature called write-caching incorporated in modern hard disk drives to boost write performance. While increasing write throughput, the feature indirectly increases the risk of data corruption and bad sectors as your drive keeps constantly writing to disk.

So its a very good idea to disable write-caching in your hard drive if your disk has sensitive data. To accomplish that we are going to use a very handy tool in UNIX, hdparm.

'hdparm' is a tool designed to manage every setting of your hard drive. With this tool, you can tune your hard drive to your style. With some settings activated your hard drive will give you highest performance (with the risk of data loss when power failure occurs) and with others you can ensure maximum safety of your data at the expense of performance.

Now if u want maximum safety for your data, just run the following in root shell (be warned that you won't get maximum performance from your drive):

#hdparm -W 0 /dev/hda
[Note: replace hda with your drive identifier]

This effectively turns write-cache off.

If you want to automatically deactivate write-cache off on your hard drive, then add these lines in /etc/hdparm.conf:

/dev/hda {
mult_sect_io = 16
write_cache = off
dma = on
defect_mana = on

On the other hand if you want to gain performance by pushing your hard drive to the edge (but this may make your hard drive more prone to data corruption in case of power failure):

#hdparm -d 1 -A 1 -m 16 -u 1 -a 64 -X udma5 /dev/hda

or add these lines in /etc/hdparm.conf:

/dev/hda {
lookahead = on
write_cache = on
read_ahead_sect = 64
dma = on
mult_sect_io = 16
interrupt_unmask = on
transfer_mode = 69

Your hard drive should be running in turbo mode by now (but you won't notice any difference if these settings were enabled by default).

Time heals every wound, but time itself is a wound that never heals.