Jump to content

Finding drive serial by ATA Number


Recommended Posts

Posted

Hi All,

 

I am having an issue with ATA errors in my syslog.  I haven't solved this issue yet but my immediate issue was finding the drive serial given the ATA number.  The syslog didn't have enough info.  I ended up putting together bits and pieces of info I found and wrote the following script.  Hopefully, this will help someone else in the same situation.
 

#!/bin/bash

# Ask the user for the ATA number to search for
read -p "Enter the disk ATA number to find: " ata 

# Find the device name (e.g., sdd) corresponding to the ATA number
device=$(ls -l /sys/block/sd* | grep $(grep ^$ata$ /sys/class/scsi_host/host*/unique_id | awk -F'/' '{print $5}') | awk -F'/' '{print $NF}')

# Check if a device was found
if [ -z "$device" ]; then
  echo "No device found for ATA number $ata"
  exit 1
fi

# Find the serial number by searching the /dev/disk/by-id directory
serial=$(ls -l /dev/disk/by-id/ | grep "ata-" | grep "../../$device" | awk -F'ata-' '{print $2}' | awk -F' ->' '{print $1}')

# Check if a serial number was found
if [ -z "$serial" ]; then
  echo "No serial number found for device $device"
  exit 1
fi

# Output the device name and serial number
echo "Device: $device"
echo "Serial Number: $serial"

 

It takes the ATA number as input and creates output that looks like this:
 

root@Storm-Tower:~# ./find_disk_by_ata.sh 
Enter the disk ATA number to find: 6
Device: sdf
Serial Number: ST18000NM000J-2TV103_ZR5E11XN
ST18000NM000J-2TV103_ZR5E11XN-part1

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...