How to Programmatically Determine if a Disk is IDE, SATA, or Other Interface in Linux


4 views

On Linux systems, all storage devices appear under /dev with prefixes like sd (SCSI/SATA), hd (IDE), or nvme (NVMe). However, modern kernels use sd for most disk types due to the SCSI subsystem abstraction.

Here are several programmatic ways to determine the disk interface type:

# Method 1: Using lshw
sudo lshw -class disk

# Sample output for SATA:
#  *-disk
#       description: ATA Disk
#       product: Samsung SSD 860
#       physical id: 0
#       bus info: scsi@0:0.0.0
#       logical name: /dev/sda

smartmontools provides detailed interface information:

sudo smartctl -i /dev/sda | grep -i "transport protocol"

# Output examples:
# SATA: Transport protocol: SATA (AHCI)
# IDE: Transport protocol: ATA
# NVMe: Transport protocol: NVMe

Check dmesg for detection messages. The kernel logs the interface type during initialization:

dmesg | grep -A 10 "sda:"

# Sample output:
# [    3.180000] sd 0:0:0:0: [sda] 488397168 512-byte logical blocks
# [    3.180000] sd 0:0:0:0: [sda] Write Protect is off
# [    3.180000] sd 0:0:0:0: [sda] Mode Sense: 00 3a 00 00
# [    3.180000] sd 0:0:0:0: [sda] Write cache: enabled

Here's a Python script that combines multiple detection methods:

import subprocess
import re

def get_disk_interface(device):
    try:
        # Try smartctl first
        smart_output = subprocess.check_output(
            ["sudo", "smartctl", "-i", device],
            stderr=subprocess.PIPE, text=True)
        
        if "SATA" in smart_output:
            return "SATA"
        elif "ATA" in smart_output and "SATA" not in smart_output:
            return "IDE"
        elif "NVMe" in smart_output:
            return "NVMe"
        
        # Fallback to lshw
        lshw_output = subprocess.check_output(
            ["sudo", "lshw", "-class", "disk", "-quiet"],
            stderr=subprocess.PIPE, text=True)
        
        if "ATA" in lshw_output:
            return "IDE" if "hd" in device else "SATA"
        
        return "Unknown"
    except subprocess.CalledProcessError:
        return "Detection failed"

print(get_disk_interface("/dev/sda"))

Modern systems may show all drives as SCSI devices due to kernel abstractions. For virtual machines or USB drives, additional checks are needed.

For scripting purposes, the most reliable method is combining smartctl with lshw data, as shown in the Python example.


When working with storage devices in Linux, it's crucial to identify whether a disk uses IDE (PATA), SATA, or another interface like SCSI or NVMe. The device name (/dev/sda, etc.) alone doesn't always reveal the underlying interface type.

The most reliable method is to examine the /sys filesystem. Here's how:

cat /sys/block/sda/device/../scsi_host/host*/proc_name

This will typically return something like:

ahci    # For SATA
ata_piix  # For IDE (PATA)
mptspi   # For SCSI

The hdparm utility can provide interface information:

sudo hdparm -I /dev/sda | grep "Transport"

Sample output for different interfaces:

Transport: Serial, SATA 1.0a, SATA II Extensions, SATA Rev 2.5  # SATA
Transport: Parallel, ATA8-AST, ATA/ATAPI-7  # IDE

While dmesg often shows "SCSI" for modern drives due to the SCSI subsystem layer, you can find more specific information by searching for the device name:

dmesg | grep -i sda | grep -E 'ata[0-9]|ahci|scsi'

The lshw command provides detailed hardware information:

sudo lshw -class disk -class storage

Look for lines containing:

description: ATA Disk  # For IDE
description: SATA Disk  # For SATA
description: SCSI Disk  # For SCSI

Here's a bash script to automatically detect the interface type:

#!/bin/bash

DISK="/dev/sda"
SYSPATH="/sys/block/${DISK##*/}/device/../scsi_host/host*/proc_name"

detect_interface() {
    if [[ -e $SYSPATH ]]; then
        INTERFACE=$(cat $SYSPATH)
        case $INTERFACE in
            ahci) echo "SATA" ;;
            ata_piix) echo "IDE (PATA)" ;;
            mptspi) echo "SCSI" ;;
            *) echo "Unknown: $INTERFACE" ;;
        esac
    else
        echo "Could not determine interface"
    fi
}

echo "Disk $DISK interface type: $(detect_interface)"

Linux uses a unified naming scheme where all block storage devices appear as sdX (SCSI disk) regardless of their actual interface. This abstraction layer means you need to look deeper to determine the physical interface type.

Other useful commands include:

  • lsblk -d -o name,rota,type,transport
  • smartctl -i /dev/sda (from smartmontools package)
  • udevadm info --query=all --name=/dev/sda