How to Find a Hard Drive’s SATA Port in Bash

📅 August 2, 2019
Uh, oh! A hard drive is displaying boot errors and needs attention! Which motherboard port is it connected to?

If you have a Linux system with many hard drives installed, such as a data server or RAID array, you will need to know which physical SATA port a problematic hard drive is connected to in order to repair or replace it.

For example, if hard drive /dev/sdg in a RAID array has failed, how can we find out which SATA port it is connected to on the motherboard if many other drives exist? This article shows a few ways to achieve this from the command line.

The Problem

One system I used displayed an error message upon each boot to inform me that the drive connected to ata6 had CRC errors. This happened upon each boot, and the delay added about six seconds to the overall system boot time.

If you see an error like this at boot time, then a hard drive needs attention.

Basically, this error says that communication with the hard drive is not 100% reliable (CRC errors). The culprit could be a faulty SATA cable, a loose 4-pin molex to SATA power cable, or a more serious issue involving a failing hard drive.

Before we can unplug the drive, we need to know which motherboard port it is connected to. There are several hard drives installed on this system, and we want to eliminate the game of “Guess the SATA Port.” Is there any way to identify the SATA port of this drive from the command line?

Yes!

ATA6?

The only clue we have to go on is the repeated ata6. Logic would tell us that this is SATA port number 6 on the motherboard (labeled SATA_6 in white text on the motherboard), but we want to make sure.

To identify the hard drive in the computer case, we need to find out the serial number of the hard drive connected to SATA port 6.

(Spoiler: Yes, the problem drive was indeed connected to SATA port 6 on this system. However, do not solely rely upon this reading without further investigation because motherboard manufacturers might change things up depending upon the quality of the motherboard. More expensive boards are usually better labeled and more informative. Some are not. You get what you pay for.)

BIOS

Checking BIOS is not necessary, but what drive information does BIOS show for SATA port 6?

What?! No drive?

Surprisingly, BIOS does not show a hard drive connected to SATA port 6 even though a hard drive is clearly connected (I can see it on the motherboard and the hard drive is active). Physical port numbers are listed in the motherboard’s BIOS. SATA6G_6 is physical port 6 on the motherboard (confirmed by visually inspecting the motherboard and consulting the motherboard manual). Linux even detects it as ata6. But not BIOS. BIOS reads, “Not Present” for SATA port 6.

Normally, the model numbers of the connected hard drives would appear for the connected ports.The plan was to view BIOS to get the model and possible serial number of the hard drive that was connected to SATA port 6, but this was not possible.

Let’s boot back into Linux.

dmesg

Linux reboots and displays the same ata6 error messages. These messages will disappear during the boot process, so if you need to peruse them at a leisurely pace or copy and paste for online research, use dmesg in a terminal.

dmesg displays boot information of the latest boot.

Linux seems to be having problems with the ata6 drive during boot even though it functions properly in Linux. We can even see it reverting to a slower 3Gbps link. Now that we are certain that ata6 is the offending drive, we can use grep to filter all ata6 lines to find any other errors.

dmesg showing all ata6 lines from another boot.

Notice that one line stands out: ATA-9: WDC WD20EFRX……. This line tells us the model number of ata6. However, we will need further investigation for the serial number and WWN if multiple drives of the same model are present in the system.

The Plan

We want to discover the serial number of the hard drive connected to SATA port 6. That way, we can open the system and locate the hard drive easier because the serial number is usually printed on the drive’s label. Each hard drive in this system has the serial number, model number, and WWN (World Wide Name) printed on the label. Hard drive label information can vary, so check labels to be sure of the kind of data you are looking for in Bash. We can use any combination of this information to identify the physical hard drive.

But how can we find this information from a command line?

lsblk

lsblk (List Block Devices) shows a wealth of information about all connected hard drives.

Running

lsblk -O

(upper-case O) returns more information than we need. Running lsblk by itself is not enough.

lsblk by itself does not show the information we need. (Irrelevant parts obscured.)

We can specify options to return the info we seek.

lsblk -o NAME,MODEL,SERIAL,WWN,HCTL

Yes! This contains the information we need. (Heavily obscured for privacy and to focus on the important parts.)

We can see the hard drive device name, model, serial, and WWN. This information is printed somewhere on the label of each hard drive.

“How did you know to use options like NAME, MODEL, and WWN?”

lsblk --help

The –help option displays a list of several option columns. This helps us obtain exactly the information we need.

lsblk –help shows available columns to filter the results.

Finding the Physical SATA Port Number

We can identify the SATA port from this information. (List truncated and obscured to hide irrelevant parts.)

Up to this point, we do not know the device name of ata6 (SATA 6). In the results, we see two unobscured lines representing two different hard drives: sdf and sdg. ata6 must be one of these, but which?

Look at the HCTL column. This is the Host:Channel:Target:Lun information meant for SCSI drives, but it works with SATA too. The first number in the four-digit, colon-delimited sequence is the SATA port. Each hard drive has a different number because it is connected to a different SATA port, but take note that it begins counting at 0, not 1.

To find the physical SATA port number, add one to the given host number. Adding 1 to the 6 in 6:0:0:0 identifies this drive as being connected to SATA port 7 (ata7) on the motherboard, so this is not the drive causing the problem seen with ata6.

Look at the other line whose HCTL is 5:0:0:0. Add 1 to the 5 and we see that this is the drive connected to SATA port 6. Therefore, /dev/sdf is the problematic drive connected to SATA 6 (ata6).

Verifying the Correct SATA Port

With the information we need, write down the model, serial, and WWN. In my case, I recorded only the last three digits of the WWN because they were so unique to the all hard drives installed they could be uniquely identified by that alone. Use what you need. In this case, the last three digits were 8a9 from the WWN, and this was printed clearly on the drive label for easy identification.

Power down the system and open it up. Locate the matching drive by reading the model, serial, and/or WWN on the label.

I confirmed the drive by unplugging the SATA cable from SATA port 6 and rebooting. The ata6 errors disappeared, and the system booted faster. This verifies that the correct drive was identified via Bash and by physically testing it.

Fixing the Problem

This could be anything related to hard drive failure. In my case, I could hear a clicking noise from this particular drive at SATA port 6, so the best action was to remove it given the imminent failure symptoms.

However, check that the SATA and power cables are firmly connected first. CRC errors are usually the result of a faulty SATA cable, so try a new one to be sure before tossing the drive.

lsscsi

lsblk is not the only way to find hard drive information. lsscsi will work too! This is probably not installed, so it will need to be installed first.

sudo apt install lsscsi

Then, run

lsscsi --verbose

This lists all drives including the HCTL numbers.

lsscsi shows similar information. (Partial listing showing only the relevant lines.)

We can see the same HCTL numbers (start counting from 0 to find the physical port number) as well as ata6 with the device. By looking at the output, 5:0:0:0 is indeed ata6 (add 1 to the 5 in 5:0:0:0). This is SATA port number 6 on the motherboard. 6:0:0:0 corresponds to ata7, which is not what we want so we can ignore this drive.

lshw

We can also use lshw (List Hardware) to view detailed hard drive information that includes SATA port numbers and corresponding serial numbers. Use with sudo for the most complete results.

sudo lshw

Unless the results are filtered, much information will scroll by. Hard drive information will begin with *-scsi:

lshw also returns the device file, serial, model, and physical SATA port number.

Recall the HCTL? The host number will be one less than the SATA port number, and this number will appear as part of the *-scsi group. We need to find ata6, so we look for *-scsi:5. (The 5 is from 5:0:0:0)

Each drive will have its own group of information. Shown above is the info for the problem drive. Physical ID is the physical SATA port number, which is 6. SATA port 6. Which device file? That is listed too as logical name.

  • *-scsi:5  is the host number (confirmed as bus info: scsi@5:0:0:0)
  • physical id: 6 is SATA port number 6 on the motherboard
  • logical name is the hard drive’s file representation in Linux

From this, we can gather that /dev/sdf is connected to SATA port 6 (ata6) on the motherboard. We can write down the product (model), serial, and version to identify the drive by its label after powering down the computer and opening it up.

Conclusion

After using these techniques to identify the drive and correct the hardware issue, the computer now boots faster by shaving off the six-second delay that previously occurred during boot. Locating the problematic drive before opening the system eliminated much time and guesswork. I was able to go straight to the drive and SATA port to resolve the problem. This is a huge convenience feature that makes troubleshooting easier.

The ability to identify which physical port a hard drive is connected to without powering down the system is a valuable skill. lsblk, lsscsi, and lshw are just three techniques of discovering which physical port is associated with which device file, but any of these ideas can be used together to confirm which drive is which.

Have fun!

, ,

  1. Leave a comment

Leave a comment