Difference between revisions of "Hdparm"

From ArchWiki
Jump to: navigation, search
(hdparm moved from /sbin/hdparm to /usr/bin/hdparm)
m (Benchmarking: format improvement)
 
(89 intermediate revisions by 24 users not shown)
Line 1: Line 1:
 +
{{Lowercase title}}
 
[[Category:File systems]]
 
[[Category:File systems]]
 
[[Category:Storage]]
 
[[Category:Storage]]
 
[[fr:hdparm]]
 
[[fr:hdparm]]
{{lowercase title}}
+
[[ja:hdparm]]
hdparm is a performance and [[benchmarking]] tool for your hard disk (SATA/IDE).
+
{{Related articles start}}
 +
{{Related|Securely wipe disk#hdparm}}
 +
{{Related articles end}}
 +
hdparm is a command line utility to set and view hardware parameters of [[wikipedia:Hard disk drive|hard disk drives]]. It can also be used as a simple [[benchmarking]] tool.
  
{{Warning|Be careful, it is easy to destroy your hard drive with hdparm!}}
+
{{Warning|Be careful, changing default parameters can damage the drive or freeze the system.}}
  
 
== Installation ==
 
== Installation ==
  
{{Pkg|hdparm}} can be installed from the [[Official Repositories|official repositories]]. For use with SCSI devices, install {{Pkg|sdparm}}.
+
[[Install]] the {{Pkg|hdparm}} package. For use with SCSI devices, install the {{Pkg|sdparm}} package.
  
 
== Usage ==
 
== Usage ==
  
 
=== Disk info ===
 
=== Disk info ===
To get information about your hard disk, run the following:
+
 
 +
To get information about hard disks, run the following:
 +
 
 
  # hdparm -I /dev/sda
 
  # hdparm -I /dev/sda
  
=== Reading speed MB/s ===
+
=== Benchmarking ===
 +
 
 +
hdparm can be used for [[Benchmarking#hdparm]].
 +
 
 +
=== Power management configuration ===
 +
 
 +
Modern hard drives support numerous power management features, the most common ones are summarized in the following table. See {{ic|hdparm(8)}} for the complete list.
 +
 
 +
{{Warning|Overly aggressive power management can reduce the lifespan of hard drives due to frequent parking and spindowns.}}
 +
 
 +
{| class="wikitable"
 +
! Parameter !! Description
 +
|-
 +
| {{ic|-B}} || Set the [[wikipedia:Advanced Power Management|Advanced Power Management]] feature. Possible values are between 1 and 255, low values mean more aggressive power management and higher values mean better performance. Values from 1 to 127 permit spin-down, whereas values from 128 to 254 do not. A value of 255 completely disables the feature.
 +
|-
 +
| {{ic|-S}} || Set the standby (spindown) timeout for the drive. The timeout specifies how long to wait in idle (with no disk activity) before turning off the motor to save power. The value of 0 disables spindown, the values from 1 to 240 specify multiples of 5 seconds and values from 241 to 251 specify multiples of 30 minutes.
 +
|-
 +
| {{ic|-M}} || Set the [[wikipedia:Automatic Acoustic Management|Automatic Acoustic Management]] feature. Most modern hard disk drives have the ability to speed down the head movements to reduce their noise output. The possible value depends on the disk, some disks may not support this feature.
 +
|}
 +
 
 +
To query current value, pass the parameter without a value. For example:
 +
 
 +
# hdparm -B /dev/sda
 +
 
 +
To apply different value, for example set APM to 127:
 +
 
 +
# hdparm -B 127 /dev/sda
 +
 
 +
== Tips and tricks ==
 +
 
 +
=== Querying the status of the disk without waking it up ===
 +
 
 +
Invoking hdparm with the query option is known to wake-up some drives.  Instead, consider {{ic|smartctl}} provided by {{pkg|smartmontools}} to query the device which will not wake up a sleeping disk.
 +
 
 +
Example:
 +
 
 +
{{hc|# smartctl -i -n standby /dev/sda|
 +
smartctl 6.5 2016-05-07 r4318 [x86_64-linux-4.10.13-1-ARCH] (local build)
 +
Copyright (C) 2002-16, Bruce Allen, Christian Franke, www.smartmontools.org
 +
 
 +
Device is in STANDBY mode, exit(2)
 +
}}
 +
 
 +
=== Persistent configuration using udev rule ===
 +
 
 +
To make the setting persistent, adapt the following [[udev]] rule:
 +
 
 +
{{hc|/etc/udev/rules.d/50-hdparm.rules|2=
 +
ACTION=="add", SUBSYSTEM=="block", KERNEL=="sda", RUN+="/usr/bin/hdparm -B 254 -S 0 /dev/sda"
 +
}}
 +
 
 +
Systems with multiple hard drives, can make the rule more flexible. For example, to apply power-saving settings for all external drives (assuming there is only one internal drive, {{ic|/dev/sda}}):
 +
 
 +
{{hc|/etc/udev/rules.d/50-hdparm.rules|<nowiki>
 +
ACTION=="add|change", KERNEL=="sd[b-z]", ATTR{queue/rotational}=="1", RUN+="/usr/bin/hdparm -B 127 -S 12 /dev/%k"</nowiki>
 +
}}
 +
 
 +
=== Putting a drive to sleep directly after boot ===
 +
 
 +
A device which is rarely needed can be put to sleep directly at the end of the boot process.
 +
This does not work with the above udev rule because it happens too early.
 +
In order to issue the command when the boot is completed, just create a [[systemd]] service.
 +
 
 +
{{hc|/etc/systemd/system/hdparm.service|2=
 +
[Unit]
 +
Description=hdparm sleep
 +
 
 +
[Service]
 +
Type=oneshot
 +
ExecStart=/usr/bin/hdparm -q -S 120 -y /dev/sdb
 +
 
 +
[Install]
 +
WantedBy=multi-user.target
 +
}}
 +
 
 +
Then [[enable]] it.
 +
 
 +
=== Working with unsupported hardware ===
 +
 
 +
Some drives, do not support spin down via hdparm.  A diagnostic error message similar to the following is a good indication this is the case:
 +
 
 +
{{hc|# hdparm -S 240 /dev/sda|
 +
/dev/sda:
 +
setting standby to 240 (20 minutes)
 +
HDIO_DRIVE_CMD(setidle) failed: Invalid argument
 +
}}
  
To measure how many MB/s your hard disk (SATA/IDE) can read, run the following:
+
Such drives can be spun down using {{pkg|hd-idle}} which ships with a [[systemd]] service. One need to edit {{ic|/etc/conf.d/hd-idle}} and the {{ic|HD_IDLE_OPTS}} value, then [[start]] and [[enable]] {{ic|hd-idle.service}}.
# hdparm -t --direct /dev/sda
 
  
=== Writing speed MB/s ===
+
Example using a 10 min idle time for {{ic|/dev/sda}} and a 1 min idle time for {{ic|/dev/disk/by-uuid/01CF0AC9AA5EAF70}}:
To measure how many MB/s your hard disk (SATA/IDE) can write, run the following:
 
$ sync;time bash -c "(dd if=/dev/zero of=bf bs=8k count=500000; sync)"
 
  
Do not forget to {{Keypress|Ctrl+c}} and {{ic|rm bf}} after that.
+
HD_IDLE_OPTS="-i 0 -a /dev/sda -i 600 -a /dev/disk/by-uuid/01CF0AC9AA5EAF70 -i 60"
  
{{Note|{{ic|bf}} is just the name of the output file that {{ic|dd}} writes to.}}
+
the leading {{ic|-i 0}} parameter indicates that hd-idle is disabled on other drives.
  
=== Parking your hard drive ===
+
=== Power management for Western Digital Green drives ===
  
If your hard drive is clicking many times, the kernel is parking the hard drive's actuator arm (what moves the read/write head). This happens often on laptops (2.5" IDE hard drives). If it happens too often, it could damage your hard drive.
+
''Western Digital Green'' hard drives have a special ''idle3'' timer which controls how long the drive waits before positioning its heads in their park position and entering a low power consumption state.  The factory default is aggressively set to 8 seconds, which can result in thousands of head load/unload cycles in a short period of time and eventually premature failure, not to mention the performance impact of the drive often having to wake-up before doing routine I/O. Western Digital issued a [http://wdc.custhelp.com/app/answers/detail/a_id/5357 statement], claiming that Linux is not optimized for low power storage devices and advising to reduce logging frequency. There are different ways to amend the ''idle3'' state:
  
This will just park the reading head when you shut down the computer:
+
* Western Digital supplies a DOS utility {{ic|wdidle3.exe}} for [https://support.wdc.com/downloads.aspx?p=113 download] for tweaking this setting. This utility is designed to upgrade only the firmware of the following hard drives: WD1000FYPS, WD7500AYPS, WD7501AYPS but is known to be able to change the ''idle3'' timer of other Green models as well.
  # hdparm -B254 /dev/sda
+
* hdparm features a reverse-engineered implementation behind the {{ic|-J}} flag, which is not as complete as the original official program, even though it seems to work on at least a few drives.
 +
* Another unofficial utility is provided by the {{Pkg|idle3-tools}} package. A raw ''idle3'' value is passed as a command parameter and the correspondence between this value and the timeout in seconds is provided in the bottom table of the [http://idle3-tools.sourceforge.net/ idle3-tool documentation page]. The following command sets the timer to 5 min:
 +
  # idle3ctl -s 138 /dev/sdc
 +
:this one completely disables the timer:
 +
# idle3ctl -d /dev/sdc
  
Default value is {{ic|-B128}}. An average value could be {{ic|-B199}} if it is parking too often.
+
{{Note|
 +
* A full power cycle is required for any change to take effect regardless of which program above is used. It means the drive needs to be powered OFF and then ON, a simple reboot does not suffice.
 +
* Some Western Digital Green drives are also known to have a different interpretation of hparm's standby timeout parameter, {{ic|-S 1}} resulting in a 10 min timer rather than 5 sec.
 +
* The power consumption of a Green drive is typically around 5.3W during read/write, 4.7W in idle mode and 0.7W in standby mode}}
  
To make this persistent, add a [[udev]] rule by creating e.g. <code>/etc/udev/rules.d/11-sda-apm-fix.rules</code>:
+
== Troubleshooting ==
  
ACTION=="add", SUBSYSTEM=="block", KERNEL=="sda", RUN+="/usr/bin/hdparm -B 254 /dev/sda"
+
=== APM level reset after suspend ===
  
Note that the APM level may get reset after a suspend, so you will probably also have to re-execute the command after each resume. That could be automated via a [[systemd#Suspend.2Fresume_service_files|systemd resume@ service file]].
+
The APM level may get reset after a suspend requiring it to be re-executed after each resume. This can be automated with the following [[systemd]] unit (adapted from a [https://bbs.archlinux.org/viewtopic.php?id=151640 forum thread]):
  
Or you could create <code>/usr/lib/systemd/system-sleep/hdparm_set</code>: [https://bbs.archlinux.org/viewtopic.php?id=159233 found here]
+
{{hc|/etc/systemd/system/apm.service|2=
 +
[Unit]
 +
Description=Local system resume actions
 +
After=suspend.target hybrid-sleep.target hibernate.target
  
#!/bin/sh
+
[Service]
hdparm -B254 /dev/sda
+
Type=simple
 +
ExecStart=/usr/bin/hdparm -B 254 /dev/sda
  
And make it executable:
+
[Install]
chmod +x /usr/lib/systemd/system-sleep/hdparm_set
+
WantedBy=sleep.target
 +
}}
  
==Tips and tricks==
+
{{Note|1=The {{ic|sleep.target}} is pulled by all {{ic|suspend}}, {{ic|hybrid-sleep}} and {{ic|hibernate}} targets, but it finishes starting up ''before'' the system is suspended, so the three targets have to be specified explicitly. See [https://wiki.archlinux.org/index.php?title=Talk:Hdparm&oldid=440457#Troubleshooting_APM_settings_after_suspend.2C_hibernate_or_hybrid-sleep].}}
=== KDE => 4.4.4 and hdparm ===
 
  
To stop [[KDE]] version 4.4.4 or greater from messing around with your (manually) configured hdparm values, enter the following and you should be done:
+
Alternatively, create a [[Power management#Hooks in /usr/lib/systemd/system-sleep|hook in /usr/lib/systemd/system-sleep]].
# touch /etc/pm/power.d/harddrive
 

Latest revision as of 08:14, 3 November 2017

hdparm is a command line utility to set and view hardware parameters of hard disk drives. It can also be used as a simple benchmarking tool.

Warning: Be careful, changing default parameters can damage the drive or freeze the system.

Installation

Install the hdparm package. For use with SCSI devices, install the sdparm package.

Usage

Disk info

To get information about hard disks, run the following:

# hdparm -I /dev/sda

Benchmarking

hdparm can be used for Benchmarking#hdparm.

Power management configuration

Modern hard drives support numerous power management features, the most common ones are summarized in the following table. See hdparm(8) for the complete list.

Warning: Overly aggressive power management can reduce the lifespan of hard drives due to frequent parking and spindowns.
Parameter Description
-B Set the Advanced Power Management feature. Possible values are between 1 and 255, low values mean more aggressive power management and higher values mean better performance. Values from 1 to 127 permit spin-down, whereas values from 128 to 254 do not. A value of 255 completely disables the feature.
-S Set the standby (spindown) timeout for the drive. The timeout specifies how long to wait in idle (with no disk activity) before turning off the motor to save power. The value of 0 disables spindown, the values from 1 to 240 specify multiples of 5 seconds and values from 241 to 251 specify multiples of 30 minutes.
-M Set the Automatic Acoustic Management feature. Most modern hard disk drives have the ability to speed down the head movements to reduce their noise output. The possible value depends on the disk, some disks may not support this feature.

To query current value, pass the parameter without a value. For example:

# hdparm -B /dev/sda

To apply different value, for example set APM to 127:

# hdparm -B 127 /dev/sda

Tips and tricks

Querying the status of the disk without waking it up

Invoking hdparm with the query option is known to wake-up some drives. Instead, consider smartctl provided by smartmontools to query the device which will not wake up a sleeping disk.

Example:

# smartctl -i -n standby /dev/sda
smartctl 6.5 2016-05-07 r4318 [x86_64-linux-4.10.13-1-ARCH] (local build)
Copyright (C) 2002-16, Bruce Allen, Christian Franke, www.smartmontools.org

Device is in STANDBY mode, exit(2)

Persistent configuration using udev rule

To make the setting persistent, adapt the following udev rule:

/etc/udev/rules.d/50-hdparm.rules
ACTION=="add", SUBSYSTEM=="block", KERNEL=="sda", RUN+="/usr/bin/hdparm -B 254 -S 0 /dev/sda"

Systems with multiple hard drives, can make the rule more flexible. For example, to apply power-saving settings for all external drives (assuming there is only one internal drive, /dev/sda):

/etc/udev/rules.d/50-hdparm.rules
ACTION=="add|change", KERNEL=="sd[b-z]", ATTR{queue/rotational}=="1", RUN+="/usr/bin/hdparm -B 127 -S 12 /dev/%k"

Putting a drive to sleep directly after boot

A device which is rarely needed can be put to sleep directly at the end of the boot process. This does not work with the above udev rule because it happens too early. In order to issue the command when the boot is completed, just create a systemd service.

/etc/systemd/system/hdparm.service
[Unit]
Description=hdparm sleep

[Service]
Type=oneshot
ExecStart=/usr/bin/hdparm -q -S 120 -y /dev/sdb

[Install]
WantedBy=multi-user.target

Then enable it.

Working with unsupported hardware

Some drives, do not support spin down via hdparm. A diagnostic error message similar to the following is a good indication this is the case:

# hdparm -S 240 /dev/sda
/dev/sda:
setting standby to 240 (20 minutes)
HDIO_DRIVE_CMD(setidle) failed: Invalid argument

Such drives can be spun down using hd-idle which ships with a systemd service. One need to edit /etc/conf.d/hd-idle and the HD_IDLE_OPTS value, then start and enable hd-idle.service.

Example using a 10 min idle time for /dev/sda and a 1 min idle time for /dev/disk/by-uuid/01CF0AC9AA5EAF70:

HD_IDLE_OPTS="-i 0 -a /dev/sda -i 600 -a /dev/disk/by-uuid/01CF0AC9AA5EAF70 -i 60"

the leading -i 0 parameter indicates that hd-idle is disabled on other drives.

Power management for Western Digital Green drives

Western Digital Green hard drives have a special idle3 timer which controls how long the drive waits before positioning its heads in their park position and entering a low power consumption state. The factory default is aggressively set to 8 seconds, which can result in thousands of head load/unload cycles in a short period of time and eventually premature failure, not to mention the performance impact of the drive often having to wake-up before doing routine I/O. Western Digital issued a statement, claiming that Linux is not optimized for low power storage devices and advising to reduce logging frequency. There are different ways to amend the idle3 state:

  • Western Digital supplies a DOS utility wdidle3.exe for download for tweaking this setting. This utility is designed to upgrade only the firmware of the following hard drives: WD1000FYPS, WD7500AYPS, WD7501AYPS but is known to be able to change the idle3 timer of other Green models as well.
  • hdparm features a reverse-engineered implementation behind the -J flag, which is not as complete as the original official program, even though it seems to work on at least a few drives.
  • Another unofficial utility is provided by the idle3-tools package. A raw idle3 value is passed as a command parameter and the correspondence between this value and the timeout in seconds is provided in the bottom table of the idle3-tool documentation page. The following command sets the timer to 5 min:
# idle3ctl -s 138 /dev/sdc
this one completely disables the timer:
# idle3ctl -d /dev/sdc
Note:
  • A full power cycle is required for any change to take effect regardless of which program above is used. It means the drive needs to be powered OFF and then ON, a simple reboot does not suffice.
  • Some Western Digital Green drives are also known to have a different interpretation of hparm's standby timeout parameter, -S 1 resulting in a 10 min timer rather than 5 sec.
  • The power consumption of a Green drive is typically around 5.3W during read/write, 4.7W in idle mode and 0.7W in standby mode

Troubleshooting

APM level reset after suspend

The APM level may get reset after a suspend requiring it to be re-executed after each resume. This can be automated with the following systemd unit (adapted from a forum thread):

/etc/systemd/system/apm.service
[Unit]
Description=Local system resume actions
After=suspend.target hybrid-sleep.target hibernate.target

[Service]
Type=simple
ExecStart=/usr/bin/hdparm -B 254 /dev/sda

[Install]
WantedBy=sleep.target
Note: The sleep.target is pulled by all suspend, hybrid-sleep and hibernate targets, but it finishes starting up before the system is suspended, so the three targets have to be specified explicitly. See [1].

Alternatively, create a hook in /usr/lib/systemd/system-sleep.