Thursday, July 29, 2010

Getting USB storage device (Pen Drive) serial from mount label in Linux

As I have described in my last post, retrieving USB storage device serial in Linux is a bit tricky if you want to find out serial of a given device.

In this post I will describe how to get this.

Your pen drive is mounted automatically as soon as you insert. (You can also mount manually; if not). By default it mounts in /media. To see:

# ls /media

Output can be like this

Disk MyDisk Disk2

"Disk", "MyDisk", "Disk2" is your device's mount labels.

And now run the script given in the last of this post using following command:

# sh '/media/Disk'

Notice the command line argument. It's your devices path where it is mounted. Give the path of the device which serial you want. "" is the name of the shell script given below (To run it with .sh extension ).


# This script takes the mount label as command line argument
# and print the serial of the USB mass storage device
# Author: Vishwesh Kumar Yadav
# Date: 28/05/2010

#Step1- Get mount Label of target device

echo "Mount Label is : $label"

#Step2- Set device name in variable retrieved from df -h or /proc/mounts

sd=`df -h | grep "$label" | cut -d ' ' -f 1 | cut -d '/' -f 3`
echo "Device is : /dev/$sd"

#Step3- Get SCSI channel id

#I use rev command as in some OS like ubuntu and suse one extra
# parameter before every line exist in dmesg command.
#So I was unable to correctly cut exact field count.

sid=`dmesg | grep -nA 1 ": $sd" | tail -1 | cut -d ':' -f 1 | rev | cut -d ' ' -f 1`
echo "SCSI ID : $sid"

#Step4-Get Serial from /proc/scsi/usb-storage/

serial=`cat "/proc/scsi/usb-storage/$sid" | grep Serial | cut -d ':' -f 2`
echo "Serial is : $serial"


Above script is tested on following linux distros:

Ubuntu 10.04
Fedora 8,11 and 13
openSUSE 11.3
Mandriva Linux 2010.1
Linux Mint 9
PCLinuxOS 2010.07
CentOS 5.5

Please let me know, your experiences of getting serial. And your feedback on this post. Also please share if you test it on other unix/linux flavours.


Getting USB device serial in Linux

Here is my experience of getting USB Storage device serial on Linux:

USB is a SCSI device. As per USB specification USB devices have
ProductID, VendorID and Serial. (Some Local pen drives may not have this serial). Serial of the USB device is unique and never change even you format the storage device.

In Linux you can get this information very easily by following ways:

Method 1:

# cat /proc/bus/usb/devices

You will get listing of all the USB devices something like this:

T: Bus=01 Lev=01 Prnt=01 Port=01 Cnt=01 Dev#= 3 Spd=480 MxCh= 0
D: Ver= 2.00 Cls=00(>ifc ) Sub=00 Prot=00 MxPS=64 #Cfgs= 1
P: Vendor=058f ProdID=6387 Rev= 1.41
S: Manufacturer=JetFlash
S: Product=Mass Storage Device
S: SerialNumber=WVIAZIUI
C:* #Ifs= 1 Cfg#= 1 Atr=80 MxPwr=100mA
I: If#= 0 Alt= 0 #EPs= 2 Cls=08(stor.) Sub=06 Prot=50 Driver=usb-storage
E: Ad=01(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms
E: Ad=82(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms

Notice the 6th line containing "SerialNumber=WVIAZIUI"

Method 2:

# ls /proc/scsi/usb-storage

You will get name of the directories containing usb information for each usb storage device like:


# cat /proc/scsi/usb-storage/4

You will get output like this:

Host scsi4: usb-storage
Vendor: JetFlash
Product: Mass Storage Device
Serial Number: WVIAZIUI
Protocol: Transparent SCSI
Transport: Bulk

Notice the 4th line containing "Serial Number: WVIAZIUI"

As you can see it is very easy to get USB storage device serial in Linux but if you have more than one USB storage devices (Pen Drives) inserted in the system then
how to identify the serial of the particular device is a little bit tricky. In my next post I will describe step by step description of my shell script to get the serial of given device.

All methods given above and my script is tested on following Linux flavours:
Ubuntu 10.04
Fedora 8,11 and 13
openSUSE 11.3
Mandriva Linux 2010.1
Linux Mint 9
PCLinuxOS 2010.07
CentOS 5.5