Using hdparm for SATA Drive Parameters in Linux
The hdparm utility is a command-line Linux tool designed
to inspect, measure, and modify hardware parameters on SATA and IDE
storage devices. It functions by issuing low-level commands through the
Linux kernel directly to the drive controller, allowing administrators
to benchmark throughput, configure power-saving modes, toggle write
caching, and read hardware identity information. This article explains
the underlying mechanism hdparm uses to interact with SATA
storage and details how it performs both measurement and parameter
modification.
How hdparm Communicates with SATA Drives
Although modern SATA drives present themselves as SCSI devices in
Linux (typically under /dev/sdX) via the kernel's
libata subsystem, hdparm communicates using
ATA command sets.
- System Calls and IOCTLs:
hdparmusesioctl()(Input/Output Control) system calls, specificallyHDIO_*commands or the genericSG_IO(SCSI Generic) interface. - SAT (SCSI-to-ATA Translation): Because SATA drives
are managed by
libata,hdparmpackages ATA commands inside SCSI Pass-Through commands. The kernel's SCSI-to-ATA Translation Layer receives these commands, unpacks them into ATA Taskfile registers (Frame Information Structures or FIS), and transmits them across the physical SATA bus directly to the drive’s onboard firmware.
Measuring Drive Parameters and Performance
hdparm queries drive telemetry and evaluates read
throughput through dedicated flags.
1. Hardware Identification
(-I)
Running hdparm -I /dev/sdX instructs the drive
controller to execute the ATA IDENTIFY DEVICE command
(opcode 0xEC). The drive responds with a 512-byte data
structure containing hardcoded factory parameters and active runtime
configurations, such as:
- Supported and active Serial ATA signaling speeds (1.5 Gb/s, 3.0 Gb/s, 6.0 Gb/s).
- Native Command Queuing (NCQ) depth.
- Advanced Power Management (APM) states.
- Security states and TRIM support.
2. Cache and
Buffer Read Benchmarks (-t and -T)
- Timing Buffered Cache Reads (
-T): This measures the speed of reading directly from the Linux page cache without disk access.hdparmreads a sequence of data repeatedly from system memory to evaluate the host processor, memory bandwidth, and bus performance. - Timing Device Reads (
-t): This tests the drive's sustained sequential read performance.hdparmclears caches, opens the raw block device via the system callread(), and reads sequentially through 2 GB to 4 GB of data in standard block sizes (typically 128 KB or larger). It calculates the elapsed time with microsecond accuracy to compute the throughput in megabytes per second (MB/s).
Setting Drive Parameters
When used to alter hardware settings, hdparm constructs
specific ATA commands that change non-volatile or volatile configuration
registers within the drive's firmware.
1. Write Caching (-W)
- Command:
hdparm -W1 /dev/sdX(enable) orhdparm -W0 /dev/sdX(disable). - Mechanism: Issues the ATA
SET FEATUREScommand with subcommand0x02(enable write cache) or0x82(disable write cache). Disabling this forces all write requests to flush immediately to physical platters or NAND, increasing data safety at the expense of write performance.
2. Advanced Power Management
(-B)
- Command:
hdparm -B 1-255 /dev/sdX. - Mechanism: Issues the
SET FEATUREScommand with subcommand0x05. Values from 1 to 127 allow aggressive power management where the drive may spin down platters or enter low-power PHY states. Values from 128 to 254 prioritize performance over power consumption, and value 255 disables APM completely.
3. Standby/Spindown Timeout
(-S)
- Command:
hdparm -S [value] /dev/sdX. - Mechanism: Issues the ATA
IDLEcommand with a timer payload. The drive's internal firmware maintains this countdown timer. When the drive experiences inactivity for the specified duration, it spins down the motor and powers off internal mechanics until a new I/O request triggers a spin-up cycle.
4. Acoustic Management
(-M)
- Command:
hdparm -M 128-254 /dev/sdX. - Mechanism: Sends the ATA
SET FEATUREScommand with subcommand0x42to adjust Automatic Acoustic Management (AAM) on supported mechanical drives, altering head-actuator seek profiles to balance noise levels against seek times.
Persistence of Changes
Parameters set by hdparm operate at the drive's runtime
firmware level. Because most SATA parameters set via
SET FEATURES reset when the device loses power, Linux
distributions maintain persistent settings by re-applying
hdparm rules during the boot process via
/etc/hdparm.conf or udev rules triggered when
the block device node is created.