Thursday, July 29, 2010

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:

4

Now,
# 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
Quirks:

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

No comments: