Matching up port numbers to devices
I found this question because I was trying to figure out the same thing. Here's what I figured out; it should work in bash or zsh:
for i in /dev/disk/by-path/*;do [[ ! "$i" =~ '-part[0-9]+$' ]] && echo "Port $(basename "$i"|grep -Po '(?<=ata-)[0-9]+'): $(readlink -f "$i")";done
The output should look something like this:
Port 1: /dev/sda
Port 2: /dev/sdb
Port 3: /dev/sdc
These port numbers SHOULD correspond with the numbers printed on your motherboard, although this assumes your motherboard vendor was considerate enough to match the numbers printed on the board to the port numbers in the SATA controller chip. At very least, device paths remain stable, so once you establish a mapping between the listed port numbers and the numbers on the panel, it won't ever change.
This snippet iterates over the /dev/disk/by-path
directory. It skips over files that end in -part<number>
as these are just partitions, and extracts the port number from the -ata<number>
at the end of the remaining filenames. These files are symlinks to the traditional /dev/sdX
nodes, which it gets using readlink -f
.
If you have multiple SATA controllers, you'll get multiple devices listed for the same port numbers, because each controller has its own port 1, port 2, etc. So, just run ls -l /dev/disk/by-path
and parse it out manually.
Figuring out which device is your boot device
To figure out which one is your boot device, run mount | grep ' on / ' | cut -f 1 -d ' '
. This shows the device mounted at /
.
/dev/(sda|sdb)
, which don't necessarily correspond to the physical ports the drives are attached to.ls -l /dev/disk/by-path/