Linux hdparm Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux hdparm command to interact with and configure various parameters of your hard disk drives (HDDs) and solid-state drives (SSDs). The lab covers understanding the purpose and functionality of hdparm, measuring disk performance, and optimizing disk performance using this powerful utility. You will explore features such as disk performance measurement, disk configuration optimization, and disk information retrieval. The lab provides practical examples to help you effectively utilize hdparm in your Linux environment.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicSystemCommandsGroup -.-> linux/read("`Input Reading`") linux/SystemInformationandMonitoringGroup -.-> linux/dd("`File Converting/Copying`") linux/SystemInformationandMonitoringGroup -.-> linux/time("`Command Timing`") subgraph Lab Skills linux/cat -.-> lab-422717{{"`Linux hdparm Command with Practical Examples`"}} linux/read -.-> lab-422717{{"`Linux hdparm Command with Practical Examples`"}} linux/dd -.-> lab-422717{{"`Linux hdparm Command with Practical Examples`"}} linux/time -.-> lab-422717{{"`Linux hdparm Command with Practical Examples`"}} end

Understand the Purpose and Functionality of hdparm

In this step, you will learn about the purpose and functionality of the hdparm command in Linux. The hdparm command is a powerful utility that allows you to interact with and configure various parameters of your hard disk drives (HDDs) and solid-state drives (SSDs).

The hdparm command provides the following key functionalities:

  1. Disk Performance Measurement: You can use hdparm to measure the performance of your disk, including read/write speeds, cache settings, and other parameters.
  2. Disk Configuration Optimization: hdparm allows you to adjust various disk parameters to optimize performance, such as enabling advanced power management features, setting the read-ahead cache size, and more.
  3. Disk Information Retrieval: The hdparm command can provide detailed information about your disk, including the model, firmware version, and other technical specifications.

Let's start by using hdparm to retrieve information about your disk.

sudo hdparm -i /dev/sda

Example output:

/dev/sda:
 Model=VBOX HARDDISK, FwRev=1.0, SerialNo=
 Config={ HardSect NotMFM Motor(_) Removeable DTR(500Mbs) SpinUp(0.600s) }
 RawCHS=16383/16/63, TrkSize=0, SectSize=0, ECCbytes=0
 BuffType=unknown, BuffSize=0kB, MaxMultSect=0
 (maybe): CurCHS=16383/16/63, CurSects=16514064, LBA=yes, LBAsects=33554432
 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
 UDMA modes: udma0 udma1 udma2 udma3 udma4 *udma5
 AdvancedPM=no WriteCache=enabled
 Drive conforms to: unknown:  ATA/ATAPI-1 ATA/ATAPI-2 ATA/ATAPI-3 ATA/ATAPI-4 ATA/ATAPI-5 ATA/ATAPI-6 ATA/ATAPI-7

This command retrieves detailed information about the /dev/sda disk, including the model, firmware version, serial number, and supported disk modes.

Measure Disk Performance with hdparm

In this step, you will learn how to use the hdparm command to measure the performance of your disk. This information can be useful for identifying performance bottlenecks and optimizing your system's disk configuration.

Let's start by measuring the disk's read performance using the -t option:

sudo hdparm -t /dev/sda

Example output:

/dev/sda:
 Timing buffered disk reads: 236 MB in  3.01 seconds = 78.41 MB/sec

This command measures the disk's buffered read performance, which provides an estimate of the disk's maximum sustained read speed.

Next, let's measure the disk's cached read performance using the -T option:

sudo hdparm -T /dev/sda

Example output:

/dev/sda:
 Timing cached reads:   13624 MB in  2.00 seconds = 6812.00 MB/sec

The cached read performance measures the speed of reading data from the disk's cache, which is typically much faster than reading directly from the disk.

Finally, let's measure the disk's sequential read and write performance using the -t and -T options together:

sudo hdparm -tT /dev/sda

Example output:

/dev/sda:
 Timing buffered disk reads: 236 MB in  3.01 seconds = 78.41 MB/sec
 Timing cached reads:   13624 MB in  2.00 seconds = 6812.00 MB/sec

This command provides both the buffered disk read and cached read performance metrics, giving you a more comprehensive view of your disk's performance.

Optimize Disk Performance using hdparm

In this final step, you will learn how to use the hdparm command to optimize the performance of your disk.

First, let's enable the read-ahead cache, which can significantly improve read performance:

sudo hdparm -r1 /dev/sda

Example output:

/dev/sda:
 setting readonly to 1 (on)

This command sets the read-ahead cache size to 1, which is the maximum value. You can experiment with different values to find the optimal setting for your system.

Next, let's enable the advanced power management (APM) feature, which can help reduce power consumption and potentially improve performance:

sudo hdparm -B 254 /dev/sda

Example output:

/dev/sda:
 setting Advanced Power Management level to 0xfe (254) (maximum performance)

This command sets the APM level to 254, which is the maximum performance setting. You can experiment with lower values to find the right balance between performance and power savings.

Finally, let's enable the write-caching feature, which can improve write performance:

sudo hdparm -W1 /dev/sda

Example output:

/dev/sda:
 setting drive write-caching to 1 (on)

This command enables the write-caching feature, which can provide a significant boost to write performance.

After making these changes, let's verify the disk performance again using the steps from the previous section.

Summary

In this lab, you first learned about the purpose and functionality of the hdparm command in Linux. The hdparm command is a powerful utility that allows you to interact with and configure various parameters of your hard disk drives (HDDs) and solid-state drives (SSDs). It provides key functionalities such as disk performance measurement, disk configuration optimization, and disk information retrieval. You then explored how to use hdparm to measure the performance of your disk, including read/write speeds, cache settings, and other parameters. Finally, you learned how to optimize disk performance using hdparm by adjusting various disk parameters, such as enabling advanced power management features and setting the read-ahead cache size.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like