hdparm - the complete guide

Share your own howto's etc. Not for support questions!

hdparm - the complete guide

Postby KOTAPAKA » 2008-11-12 01:29

Edited: The purpose is to edit the parameters of you hard drive so that you can control the load cycles of you hard drive. What this does is stops the parking of the head of the HD which is not needed in desktops and laptops (unless you are running with your laptop computer) but even then the options here tell the laptop to park it when running on battery.


OK so I've never used the method with the scripts that some people write and you just copy them. Instead about a year ago I found this method which is the way you are supposed to do it. I've always wanted to share but was too lazy. Now I am overwhelmed with work and I am punishing myself for not sharing it earlier. I am not sure whether you can find this way online (apart from this howto) but you could search.

And just a note which I remembered in the writing process. 255 will not work on some machines. If it doesn't for yours use 254 as a value - it's the same basically.

1. You will need the packages hdparm, sysvconfig and smartmontools all of which can be obtained from the repos. Install them both.

2. You need to make sure that hdparm is started as a process when you log in. Start sysvconfig and press enter to enter Enable/Disable mode. Find hdparm and press space to place an asterisk in the brackets on the left of it. Now confirm your choice by pressing enter and make sure you save before exiting sysvconfig.

3. Now here comes the bit that I do differently. There are some files that you need to edit. These are hdparm's config file and a few scripts that are already there and are loaded when you log in. So you just need to give them the right information. To be honest I think they've changed them now so but as I haven't reinstalled my system after I found this method I can't tell you exactly. When I say change I mean that they've introduced new scripts so the old ones might not be used. However I will tell you how to edit both in case you have an old install.

(i) First comes /etc/hdparm.conf. This file is not changed so you should be able to find it. Edit it in the following way:

Code: Select all
## This is the default configuration for hdparm for Debian.  It is a
## rather simple script, so please follow the following guidelines :)
## Any line that begins with a comment is ignored - add as many as you
## like.  Note that an in-line comment is not supported.  If a line
## consists of whitespace only (tabs, spaces, carriage return), it will be
## ignored, so you can space control fields as you like.  ANYTHING ELSE
## IS PARSED!!  This means that lines with stray characters or lines that
## use non # comment characters will be interpreted by the initscript. 
## This has probably minor, but potentially serious, side effects for your
## hard drives, so please follow the guidelines.  Patches to improve
## flexibilty welcome.  Please read /usr/share/doc/hdparm/README.Debian for
## notes about known issues, especially if you have an MD array.
##
## Note that if the init script causes boot problems, you can pass 'nohdparm'
## on the kernel command line, and the script will not be run.
##
## Uncommenting the options below will cause them to be added to the DEFAULT
## string which is prepended to options listed in the blocks below.
##
## If an option is listed twice, the second instance replaces the first.
##
## /sbin/hdparm is not run unless a block of the form:
##      DEV {
##         option
##         option
##         ...
##      }
## exists.  This blocks will cause /sbin/hdparm OPTIONS DEV to be run.
## Where OPTIONS is the concatenation of all options previously defined
## outside of a block and all options defined with in the block.

# -q be quiet
quiet
# -a sector count for filesystem read-ahead
#read_ahead_sect = 12
# -A disable/enable the IDE drive's read-lookahead feature
#lookahead = on
# -b bus state
#bus = on
# -B apm setting
#apm = 255
# -c enable (E)IDE 32-bit I/O support - can be any of 0,1,3
#io32_support = 1
# -d disable/enable the "using_dma" flag for this drive
#dma = off
# -D enable/disable the on-drive defect management
#defect_mana = off
# -E cdrom speed
#cd_speed = 16
# -k disable/enable the "keep_settings_over_reset" flag for this drive
#keep_settings_over_reset = off
# -K disable/enable the drive's "keep_features_over_reset" flag
#keep_features_over_reset = on
# -m sector count for multiple sector I/O
#mult_sect_io = 32
# -P maximum sector count for the drive's internal prefetch mechanism
#prefetch_sect = 12
# -r read-only flag for device
#read_only = off
# -s Turn on/off power on in standby mode
#poweron_standby = off
# -S standby (spindown) timeout for the drive
#spindown_time = 0
# -u interrupt-unmask flag for the drive
#interrupt_unmask = on
# -W Disable/enable the IDE drive's write-caching feature
#write_cache = off
# -X IDE transfer mode for newer (E)IDE/ATA2 drives
#transfer_mode = 34
# -y force to immediately enter the standby mode
#standby
# -Y force to immediately enter the sleep mode
#sleep
# -Z Disable the power-saving function of certain Seagate drives
#disable_seagate
# -M Set the acoustic management properties of a drive
#acoustic_management
# -p Set the chipset PIO mode
# chipset_pio_mode
# --security-freeze Freeze the drive's security status
# security_freeze
# --security-unlock Unlock the drive's security
# security_unlock = PWD
# --security-set-pass Set security password
# security_pass = password
# --security-disable Disable drive locking
# security_disable
# --user-master Select password to use
# user-master = u
# --security-mode Set the security mode
# security_mode = h

# Root file systems.  Please see README.Debian for details
 ROOTFS = /dev/sda

## New note - you can use straight hdparm commands in this config file
## as well - the set up is ugly, but it keeps backwards compatibility
## Additionally, it should be noted that any blocks that begin with
## the keyword 'command_line' are not run until after the root filesystem
## is mounted.  This is done to avoid running blocks twice.  If you need
## to run hdparm to set parameters for your root disk, please use the
## standard format.

#Samples follow:
#First three are good for devfs systems, fourth one for systems that do
#not use devfs.  The fifth example uses straight hdparm command line
#syntax.  Any of the blocks that use command line syntax must begin with
#the keyword 'command_line', and no attempt is made to validate syntax. 
#It is provided for those more comfortable with hdparm syntax.

#/dev/discs/disc0/disc {
#   mult_sect_io = 16
#   write_cache = off
#   spindown_time = 240
#}

#/dev/discs/disc1/disc {
#   mult_sect_io = 32
#   spindown_time = 36
#   write_cache = off
#}

#/dev/cdroms/cdrom0 {
#   dma = on         
#   interrupt_unmask = on
#   io32_support = 0
#}

#/dev/hda {
#   mult_sect_io = 16
#   write_cache = off
#   dma = on
#}

#command_line {
#       hdparm -q -m16 -q -W0 -q -d1 /dev/hda
#}

/dev/sda
{
   apm = 255
   spindown_time = 0
       dma = on
}


Just a few highlights here. Notice the last 5 lines. Basically this is the thing. You can add to it or just uncomment the options. If you uncomment please uncomment and add you device to the ROOTFS line. PLEASE WHATEVER YOU DO REPLACE THE /DEV/SDA WITH YOUR DEVICE. I am using a laptop but for people with PC's this will probably be /dev/hda. To be on the safe side add the last 5 lines and uncomment the options as well. Say for this uncomment the -B and -S options. Adding dma is also a good idea.



(ii) Here come some new files. Basically I don't know why you need those after editing the hdparm.conf but they are there, they are loaded so we might just load the right things, right? This file is the old one. This is the APM script. APM is a power management which as I understand is no longer supported. When I compiled a kernel 5 months ago it said that they are dropping it. In case you use an old kernel here is what you have to do (if you do not have the following files don't worry a bit - you don't need them).
Code: Select all
cd /etc/apm/event.d
ls
nano 20hdparm       ## in the unlikely event that after ls you get something else containing hdparm use that one


This here is very simple. You need the following lines:
Code: Select all
APMD_DRIVES=sda        ##Again put YOUR device


And then edit what the script does:
Code: Select all
power_performance ()
{
    # Disable IDE hard disk spindown.
    for DRIVE in $APMD_DRIVES; do
      "${HDPARM}" -q -S 0 -B 255 "${DRIVE}" || true
    done
}


So basically when you are on AC power you get no loading/unloading cycles (the -B 255 option)

(iii) Ok here is the last bit. It is for ACPI power management. Unless you are running some ald and forgotten kernel this is what you should have.
Code: Select all
cd /etc/acpi/ac.d
ls
nano 90-hdparm.sh        ## or whatever ls shows you


Skip what it tells you (actually it is quite useful to read if you have time). We are interested in this section:

Code: Select all
 for dev in /dev/sd? /dev/hd? ; do
    if [ -b $dev ] ; then
      # Check for APM support; discard errors since not all drives
      # support HDIO_GET_IDENTITY (-i).
      if hdparm -i $dev 2> /dev/null | grep -q 'AdvancedPM=yes' ; then
        if [ $STATE = "BATTERY" ] ; then
          hdparm -B 128 $dev
        else
          hdparm -B 255 $dev
        fi
      fi
    fi
  done
fi

This is the end of the file so you will find it easily (and the file is small so...). It is straight forward - hdparm -B 128 is when running on battery, otherwise you get hdparm -B 255.

4. Reboot

5. Finally confirmed that it worked. Do
Code: Select all
smartctl -a /dev/sda | grep Load_Cycle_Count

where /dev/sda is your drive so substitute again. If this doesn't work do this:
Code: Select all
smartctl -a -d ata -i /dev/sda | grep Load_Cycle_Count

This should do it. This command will give you a value of 5 digits say 94116. Do the same command after a few minutes (I'd give it 10 minutes just to be sure) and do it again. The number should be the same. Well that's it. Hope it saves a few hard drives.

NB There is one thing which I noticed today and I'll probably solve in a few days. What I am talking about is that (at least on my machine) hdparm doesn't load if I resume from suspend. In such case I do (manually):
Code: Select all
hdparm -B 255 -S 0 /dev/sda
Last edited by KOTAPAKA on 2008-11-26 13:43, edited 2 times in total.
Debian Lenny "stable"
User avatar
KOTAPAKA
 
Posts: 507
Joined: 2008-02-22 00:26
Location: UK

Postby oompa » 2008-11-22 19:21

Seems nice, but what is the objective of this how to ?

I mean the title is certainly misleading and it doesn't state its purpose at the beginning so I'm not sure it's doing what I'm looking for.
oompa
 
Posts: 25
Joined: 2008-11-14 15:39

Postby KOTAPAKA » 2008-11-23 21:51

oops sorry. What this does as I have now added is simply to stop your hard drive from wearing down due to unnecessary parking of the head of the HD. As a big issue they might actually include a fix that comes along with the install of a distro in the future, if they haven't already. When I last installed my Lenny (July 2008) they hadn't so not sure whether this is by default now.
Debian Lenny "stable"
User avatar
KOTAPAKA
 
Posts: 507
Joined: 2008-02-22 00:26
Location: UK

Postby oOarthurOo » 2008-11-23 22:18

Back when my laptop had a working battery, I would use laptop-tools to control hardrive. It had the ability to do much more than just spin down hard-drive. It also resumes correctly from suspend.

With a desktop, or a laptop that doesn't have a working battery) just add hdparm -B 254 in rc.local.
oOarthurOo
 
Posts: 545
Joined: 2008-10-25 12:00
Location: Canada

Postby oompa » 2008-11-24 05:15

Good news, it's exactly what I was looking for. In the meantime, I think found a way to do the same using laptop-mode conf files under sidux. (located at /etc/laptop-mode/laptop-mode.conf)

I suppose my laptop was affected by this as load_cycle_count shows 24490 and the laptop is 2 weeks old.

Maybe you can help me with something, I'm currently wondering if there's any difference between "standby timeout", "spindown", "idle timeout" and "head parking". I'm starting to suspect there's not.

Am I mistaken in thinking that hdparm -S sets how long before head are parked due to inactivity ? And is 20 seconds (laptop mode default value) a decent value for a laptop running on battery ?
oompa
 
Posts: 25
Joined: 2008-11-14 15:39

Postby KOTAPAKA » 2008-11-26 18:55

I currently have very little time. I'll try to help you but I am fully booked till the 6th of December and I am flying on the 7th. I'll do my best but can't promise anything. Check in google - I'm sure it'll give you all the answers.

For spindown time best is 18. Check the file /etc/apm/event.d/20hdparm there is a line such as APMD_SPINDOWN=18. This however is disabled when running on AC power.

oOarthurOo wrote:Back when my laptop had a working battery, I would use laptop-tools to control hardrive. It had the ability to do much more than just spin down hard-drive. It also resumes correctly from suspend.

With a desktop, or a laptop that doesn't have a working battery) just add hdparm -B 254 in rc.local.


Yes but with scripts you can specify different options for different situations and other goodies.
Debian Lenny "stable"
User avatar
KOTAPAKA
 
Posts: 507
Joined: 2008-02-22 00:26
Location: UK

Postby oompa » 2008-12-03 23:49

Sure, no worries, I can wait.

In the mean time, I continue to try to help myself and in the process, I found How ot make use of harddisk power management features on thinkwiki.

I believe this an interesting addition to this very howto, and it's also where I found out that my hardware (newer hitachi drive coming with a thinkpad) has the -s option (spindown timeout) of hdparm is overrided with value of -B (power management) causing too frequent spindowns and greatly reducing the harddrive life by up to several years.

Is there a way to know if the high load_cycle_count value of my drive is related to this ? And if so can you help me find of to work aound it ?
oompa
 
Posts: 25
Joined: 2008-11-14 15:39

Re: hdparm - the complete guide

Postby redowk » 2013-01-12 12:02

this was very helpfull to me, thx
redowk
 
Posts: 1
Joined: 2013-01-12 12:01

Re: hdparm - the complete guide

Postby igagis » 2015-11-12 23:04

It was very helpful. But, there are some changes on Debian jessie:

1) use sysv-rc-conf instead of sysvconfig to enable hdparam for runlevel 2
2) only editing /etc/hdparam.conf is needed

That's it!
igagis
 
Posts: 1
Joined: 2015-11-12 22:56


Return to Docs, Howtos, Tips & Tricks

Who is online

Users browsing this forum: No registered users and 3 guests

fashionable
cron